summaryrefslogtreecommitdiff
path: root/corebinutils/timeout/timeout.c
blob: bcb56f978c22acc6e4fe6ad9bf55d1398afdea8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
/*-
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2014 Baptiste Daroussin <bapt@FreeBSD.org>
 * Copyright (c) 2014 Vsevolod Stakhov <vsevolod@FreeBSD.org>
 * Copyright (c) 2025 Aaron LI <aly@aaronly.me>
 * Copyright (c) 2026 Project Tick
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer
 *    in this position and unchanged.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#define _POSIX_C_SOURCE 200809L

#include <ctype.h>
#include <errno.h>
#include <getopt.h>
#include <inttypes.h>
#include <limits.h>
#include <math.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/prctl.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>

#define EXIT_TIMEOUT 124
#define EXIT_INVALID 125
#define EXIT_CMD_ERROR 126
#define EXIT_CMD_NOENT 127

#ifndef NSIG
#define NSIG 128
#endif

struct signal_entry {
	const char *name;
	int number;
};

#define SIGNAL_ENTRY(name) { #name, SIG##name }
#define SIGNAL_ALIAS(name, signal) { name, signal }

static const struct signal_entry canonical_signals[] = {
#ifdef SIGHUP
	SIGNAL_ENTRY(HUP),
#endif
#ifdef SIGINT
	SIGNAL_ENTRY(INT),
#endif
#ifdef SIGQUIT
	SIGNAL_ENTRY(QUIT),
#endif
#ifdef SIGILL
	SIGNAL_ENTRY(ILL),
#endif
#ifdef SIGTRAP
	SIGNAL_ENTRY(TRAP),
#endif
#ifdef SIGABRT
	SIGNAL_ENTRY(ABRT),
#endif
#ifdef SIGBUS
	SIGNAL_ENTRY(BUS),
#endif
#ifdef SIGFPE
	SIGNAL_ENTRY(FPE),
#endif
#ifdef SIGKILL
	SIGNAL_ENTRY(KILL),
#endif
#ifdef SIGUSR1
	SIGNAL_ENTRY(USR1),
#endif
#ifdef SIGSEGV
	SIGNAL_ENTRY(SEGV),
#endif
#ifdef SIGUSR2
	SIGNAL_ENTRY(USR2),
#endif
#ifdef SIGPIPE
	SIGNAL_ENTRY(PIPE),
#endif
#ifdef SIGALRM
	SIGNAL_ENTRY(ALRM),
#endif
#ifdef SIGTERM
	SIGNAL_ENTRY(TERM),
#endif
#ifdef SIGSTKFLT
	SIGNAL_ENTRY(STKFLT),
#endif
#ifdef SIGCHLD
	SIGNAL_ENTRY(CHLD),
#endif
#ifdef SIGCONT
	SIGNAL_ENTRY(CONT),
#endif
#ifdef SIGSTOP
	SIGNAL_ENTRY(STOP),
#endif
#ifdef SIGTSTP
	SIGNAL_ENTRY(TSTP),
#endif
#ifdef SIGTTIN
	SIGNAL_ENTRY(TTIN),
#endif
#ifdef SIGTTOU
	SIGNAL_ENTRY(TTOU),
#endif
#ifdef SIGURG
	SIGNAL_ENTRY(URG),
#endif
#ifdef SIGXCPU
	SIGNAL_ENTRY(XCPU),
#endif
#ifdef SIGXFSZ
	SIGNAL_ENTRY(XFSZ),
#endif
#ifdef SIGVTALRM
	SIGNAL_ENTRY(VTALRM),
#endif
#ifdef SIGPROF
	SIGNAL_ENTRY(PROF),
#endif
#ifdef SIGWINCH
	SIGNAL_ENTRY(WINCH),
#endif
#ifdef SIGIO
	SIGNAL_ENTRY(IO),
#endif
#ifdef SIGPWR
	SIGNAL_ENTRY(PWR),
#endif
#ifdef SIGSYS
	SIGNAL_ENTRY(SYS),
#endif
};

static const struct signal_entry signal_aliases[] = {
#ifdef SIGIOT
	SIGNAL_ALIAS("IOT", SIGIOT),
#endif
#ifdef SIGCLD
	SIGNAL_ALIAS("CLD", SIGCLD),
#endif
#ifdef SIGPOLL
	SIGNAL_ALIAS("POLL", SIGPOLL),
#endif
#ifdef SIGUNUSED
	SIGNAL_ALIAS("UNUSED", SIGUNUSED),
#endif
};

struct options {
	bool foreground;
	bool preserve;
	bool verbose;
	bool kill_after_set;
	int timeout_signal;
	long double duration;
	long double kill_after;
	const char *command_name;
	char **command_argv;
};

struct child_state {
	bool reaped;
	int status;
};

enum deadline_kind {
	DEADLINE_NONE = 0,
	DEADLINE_FIRST,
	DEADLINE_SECOND,
};

struct runtime_state {
	bool timed_out;
	bool first_timer_active;
	bool second_timer_active;
	bool second_pending;
	long double first_deadline;
	long double second_deadline;
	int active_signal;
};

static const char *program_name = "timeout";

static void usage(void) __attribute__((noreturn));
static void die(int status, const char *fmt, ...)
    __attribute__((noreturn, format(printf, 2, 3)));
static void die_errno(int status, const char *context) __attribute__((noreturn));
static void vlog(const struct options *opt, const char *fmt, ...)
    __attribute__((format(printf, 2, 3)));
static const char *basename_const(const char *path);
static bool parse_nonnegative_int(const char *text, int *value);
static char *normalize_signal_name(const char *token);
static bool signal_name_for_number(int signum, char *buffer, size_t buffer_size);
static bool parse_signal_name(const char *token, int *signum);
static bool parse_signal_token(const char *token, int *signum);
static long double parse_duration_or_die(const char *text);
static long double monotonic_seconds(void);
static long double max_time_t_seconds(void);
static struct timespec seconds_to_timeout(long double seconds);
static enum deadline_kind next_deadline(const struct runtime_state *state,
    struct timespec *timeout);
static void enable_subreaper_or_die(void);
static bool is_terminating_signal(int signo);
static void send_signal_to_command(pid_t pid, int signo, const struct options *opt);
static void arm_second_timer(struct runtime_state *state, const struct options *opt);
static void reap_children(pid_t command_pid, struct child_state *child,
    bool *have_children, const struct options *opt);
static void child_exec(const struct options *opt, const sigset_t *oldmask)
    __attribute__((noreturn));
static void kill_self(int signo) __attribute__((noreturn));
static bool process_group_exists(pid_t pgid);

static void
usage(void)
{
	fprintf(stderr,
	    "Usage: %s [-f | --foreground] [-k time | --kill-after time]"
	    " [-p | --preserve-status] [-s signal | --signal signal] "
	    "[-v | --verbose] <duration> <command> [arg ...]\n",
	    program_name);
	exit(EXIT_INVALID);
}

static void
die(int status, const char *fmt, ...)
{
	va_list ap;

	fprintf(stderr, "%s: ", program_name);
	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	va_end(ap);
	fputc('\n', stderr);
	exit(status);
}

static void
die_errno(int status, const char *context)
{
	die(status, "%s: %s", context, strerror(errno));
}

static void
vlog(const struct options *opt, const char *fmt, ...)
{
	va_list ap;

	if (!opt->verbose)
		return;

	fprintf(stderr, "%s: ", program_name);
	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	va_end(ap);
	fputc('\n', stderr);
}

static const char *
basename_const(const char *path)
{
	const char *base;

	base = strrchr(path, '/');
	if (base == NULL || base[1] == '\0')
		return (path);
	return (base + 1);
}

static bool
parse_nonnegative_int(const char *text, int *value)
{
	intmax_t parsed;
	char *end;

	if (text[0] == '\0')
		return (false);

	errno = 0;
	parsed = strtoimax(text, &end, 10);
	if (errno == ERANGE || *end != '\0' || parsed < 0 || parsed > INT_MAX)
		return (false);
	*value = (int)parsed;
	return (true);
}

static char *
normalize_signal_name(const char *token)
{
	size_t i, length, offset;
	char *name;

	length = strlen(token);
	name = malloc(length + 1);
	if (name == NULL)
		die(EXIT_INVALID, "out of memory");

	for (i = 0; i < length; i++)
		name[i] = (char)toupper((unsigned char)token[i]);
	name[length] = '\0';

	offset = 0;
	if (length > 3 && name[0] == 'S' && name[1] == 'I' && name[2] == 'G')
		offset = 3;
	if (offset != 0)
		memmove(name, name + offset, length - offset + 1);
	return (name);
}

static bool
signal_name_for_number(int signum, char *buffer, size_t buffer_size)
{
	size_t i;

	if (signum == 0) {
		if (buffer != NULL && buffer_size > 0) {
			buffer[0] = '0';
			if (buffer_size > 1)
				buffer[1] = '\0';
		}
		return (true);
	}

	for (i = 0; i < sizeof(canonical_signals) / sizeof(canonical_signals[0]); i++) {
		if (canonical_signals[i].number != signum)
			continue;
		if (buffer != NULL && buffer_size > 0) {
			if (snprintf(buffer, buffer_size, "%s",
			    canonical_signals[i].name) >= (int)buffer_size)
				die(EXIT_INVALID, "internal signal name overflow");
		}
		return (true);
	}

#ifdef SIGRTMIN
#ifdef SIGRTMAX
	if (signum == SIGRTMIN) {
		if (buffer != NULL && buffer_size > 0)
			snprintf(buffer, buffer_size, "RTMIN");
		return (true);
	}
	if (signum == SIGRTMAX) {
		if (buffer != NULL && buffer_size > 0)
			snprintf(buffer, buffer_size, "RTMAX");
		return (true);
	}
	if (signum > SIGRTMIN && signum < SIGRTMAX) {
		if (buffer != NULL && buffer_size > 0) {
			if (snprintf(buffer, buffer_size, "RTMIN+%d",
			    signum - SIGRTMIN) >= (int)buffer_size)
				die(EXIT_INVALID, "internal signal name overflow");
		}
		return (true);
	}
#endif
#endif

	return (false);
}

static bool
parse_signal_name(const char *token, int *signum)
{
	char *name, *end;
	int offset;
	size_t i;

	if (strcmp(token, "0") == 0) {
		*signum = 0;
		return (true);
	}

	name = normalize_signal_name(token);

	for (i = 0; i < sizeof(canonical_signals) / sizeof(canonical_signals[0]); i++) {
		if (strcmp(name, canonical_signals[i].name) == 0) {
			*signum = canonical_signals[i].number;
			free(name);
			return (true);
		}
	}
	for (i = 0; i < sizeof(signal_aliases) / sizeof(signal_aliases[0]); i++) {
		if (strcmp(name, signal_aliases[i].name) == 0) {
			*signum = signal_aliases[i].number;
			free(name);
			return (true);
		}
	}

#ifdef SIGRTMIN
#ifdef SIGRTMAX
	if (strcmp(name, "RTMIN") == 0) {
		*signum = SIGRTMIN;
		free(name);
		return (true);
	}
	if (strncmp(name, "RTMIN+", 6) == 0) {
		errno = 0;
		offset = (int)strtol(name + 6, &end, 10);
		if (errno == 0 && *end == '\0' && offset >= 0 &&
		    SIGRTMIN + offset <= SIGRTMAX) {
			*signum = SIGRTMIN + offset;
			free(name);
			return (true);
		}
	}
	if (strcmp(name, "RTMAX") == 0) {
		*signum = SIGRTMAX;
		free(name);
		return (true);
	}
	if (strncmp(name, "RTMAX-", 6) == 0) {
		errno = 0;
		offset = (int)strtol(name + 6, &end, 10);
		if (errno == 0 && *end == '\0' && offset >= 0 &&
		    SIGRTMAX - offset >= SIGRTMIN) {
			*signum = SIGRTMAX - offset;
			free(name);
			return (true);
		}
	}
#endif
#endif

	free(name);
	return (false);
}

static bool
parse_signal_token(const char *token, int *signum)
{
	if (parse_nonnegative_int(token, signum)) {
		if (*signum >= NSIG)
			return (false);
		return (true);
	}
	return (parse_signal_name(token, signum));
}

static long double
parse_duration_or_die(const char *text)
{
	long double value;
	char *end;

	if (text[0] == '\0' || isspace((unsigned char)text[0]))
		die(EXIT_INVALID, "duration is not a number");

	errno = 0;
	value = strtold(text, &end);
	if (end == text)
		die(EXIT_INVALID, "duration is not a number");
	if (errno == ERANGE || !isfinite(value))
		die(EXIT_INVALID, "duration out of range");
	if (*end != '\0') {
		if (end[1] != '\0')
			die(EXIT_INVALID, "duration unit suffix too long");
		switch (*end) {
		case 's':
			break;
		case 'm':
			value *= 60.0L;
			break;
		case 'h':
			value *= 3600.0L;
			break;
		case 'd':
			value *= 86400.0L;
			break;
		default:
			die(EXIT_INVALID, "duration unit suffix invalid");
		}
	}
	if (!isfinite(value) || value < 0.0L)
		die(EXIT_INVALID, "duration out of range");
	return (value);
}

static long double
monotonic_seconds(void)
{
	struct timespec ts;

	if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
		die_errno(EXIT_INVALID, "clock_gettime(CLOCK_MONOTONIC)");

	return ((long double)ts.tv_sec +
	    (long double)ts.tv_nsec / 1000000000.0L);
}

static long double
max_time_t_seconds(void)
{
	int bits;

	bits = (int)(sizeof(time_t) * CHAR_BIT);
	if ((time_t)-1 < (time_t)0)
		return (ldexpl(1.0L, bits - 1) - 1.0L);
	return (ldexpl(1.0L, bits) - 1.0L);
}

static struct timespec
seconds_to_timeout(long double seconds)
{
	struct timespec ts;
	long double whole, fractional, max_seconds;

	if (seconds <= 0.0L) {
		ts.tv_sec = 0;
		ts.tv_nsec = 0;
		return (ts);
	}

	max_seconds = max_time_t_seconds();
	if (seconds > max_seconds)
		seconds = max_seconds;

	fractional = modfl(seconds, &whole);
	ts.tv_sec = (time_t)whole;
	ts.tv_nsec = (long)(fractional * 1000000000.0L);
	if (ts.tv_nsec < 0)
		ts.tv_nsec = 0;
	if (ts.tv_nsec >= 1000000000L)
		ts.tv_nsec = 999999999L;
	return (ts);
}

static enum deadline_kind
next_deadline(const struct runtime_state *state, struct timespec *timeout)
{
	long double now, best, remaining;
	enum deadline_kind kind;

	now = monotonic_seconds();
	best = 0.0L;
	kind = DEADLINE_NONE;

	if (state->first_timer_active) {
		remaining = state->first_deadline - now;
		if (remaining < 0.0L)
			remaining = 0.0L;
		best = remaining;
		kind = DEADLINE_FIRST;
	}
	if (state->second_timer_active) {
		remaining = state->second_deadline - now;
		if (remaining < 0.0L)
			remaining = 0.0L;
		if (kind == DEADLINE_NONE || remaining < best) {
			best = remaining;
			kind = DEADLINE_SECOND;
		}
	}

	if (kind != DEADLINE_NONE)
		*timeout = seconds_to_timeout(best);
	return (kind);
}

static void
enable_subreaper_or_die(void)
{
#ifdef PR_SET_CHILD_SUBREAPER
	if (prctl(PR_SET_CHILD_SUBREAPER, 1L, 0L, 0L, 0L) != 0)
		die_errno(EXIT_INVALID, "prctl(PR_SET_CHILD_SUBREAPER)");
#else
	die(EXIT_INVALID,
	    "Linux child subreaper API is unavailable; use -f/--foreground");
#endif
}

static bool
is_terminating_signal(int signo)
{
	switch (signo) {
#ifdef SIGHUP
	case SIGHUP:
#endif
#ifdef SIGINT
	case SIGINT:
#endif
#ifdef SIGQUIT
	case SIGQUIT:
#endif
#ifdef SIGILL
	case SIGILL:
#endif
#ifdef SIGTRAP
	case SIGTRAP:
#endif
#ifdef SIGABRT
	case SIGABRT:
#endif
#ifdef SIGBUS
	case SIGBUS:
#endif
#ifdef SIGFPE
	case SIGFPE:
#endif
#ifdef SIGSEGV
	case SIGSEGV:
#endif
#ifdef SIGPIPE
	case SIGPIPE:
#endif
#ifdef SIGTERM
	case SIGTERM:
#endif
#ifdef SIGXCPU
	case SIGXCPU:
#endif
#ifdef SIGXFSZ
	case SIGXFSZ:
#endif
#ifdef SIGVTALRM
	case SIGVTALRM:
#endif
#ifdef SIGPROF
	case SIGPROF:
#endif
#ifdef SIGUSR1
	case SIGUSR1:
#endif
#ifdef SIGUSR2
	case SIGUSR2:
#endif
#ifdef SIGSYS
	case SIGSYS:
#endif
		return (true);
	default:
		return (false);
	}
}

static void
send_signal_to_command(pid_t pid, int signo, const struct options *opt)
{
	char sigbuf[32];
	const char *signame;
	pid_t target;

	if (!signal_name_for_number(signo, sigbuf, sizeof(sigbuf)))
		snprintf(sigbuf, sizeof(sigbuf), "%d", signo);
	signame = sigbuf;

	if (opt->foreground) {
		target = pid;
		vlog(opt, "sending signal %s(%d) to command '%s'",
		    signame, signo, opt->command_name);
	} else {
		target = -pid;
		vlog(opt, "sending signal %s(%d) to command group '%s'",
		    signame, signo, opt->command_name);
	}

	if (kill(target, signo) != 0 && errno != ESRCH) {
		fprintf(stderr, "%s: kill(%ld, %s): %s\n", program_name,
		    (long)target, signame, strerror(errno));
	}

	if (signo == SIGKILL || signo == SIGSTOP || signo == SIGCONT)
		return;

	if (!signal_name_for_number(SIGCONT, sigbuf, sizeof(sigbuf)))
		snprintf(sigbuf, sizeof(sigbuf), "%d", SIGCONT);
	vlog(opt, "sending signal %s(%d) to command '%s'",
	    sigbuf, SIGCONT, opt->command_name);
	(void)kill(target, SIGCONT);
}

static void
arm_second_timer(struct runtime_state *state, const struct options *opt)
{
	long double now;

	if (!state->second_pending)
		return;

	now = monotonic_seconds();
	state->second_deadline = now + opt->kill_after;
	state->second_timer_active = true;
	state->second_pending = false;
	state->active_signal = SIGKILL;
	state->first_timer_active = false;
}

static void
reap_children(pid_t command_pid, struct child_state *child, bool *have_children,
    const struct options *opt)
{
	pid_t waited;
	int status;

	*have_children = false;
	for (;;) {
		waited = waitpid(-1, &status, WNOHANG);
		if (waited > 0) {
			if (waited == command_pid) {
				child->reaped = true;
				child->status = status;
				if (WIFEXITED(status)) {
					vlog(opt, "child terminated: pid=%d exit=%d",
					    (int)waited, WEXITSTATUS(status));
				} else if (WIFSIGNALED(status)) {
					vlog(opt, "child terminated: pid=%d sig=%d",
					    (int)waited, WTERMSIG(status));
				} else {
					vlog(opt, "child changed state: pid=%d status=0x%x",
					    (int)waited, status);
				}
			} else {
				vlog(opt, "collected descendant: pid=%d status=0x%x",
				    (int)waited, status);
			}
			continue;
		}
		if (waited == 0) {
			*have_children = true;
			return;
		}
		if (errno == EINTR)
			continue;
		if (errno == ECHILD)
			return;
		die_errno(EXIT_INVALID, "waitpid");
	}
}

static void
child_exec(const struct options *opt, const sigset_t *oldmask)
{
	struct sigaction sa;
	int saved_errno;

	if (!opt->foreground) {
		if (setpgid(0, 0) != 0) {
			dprintf(STDERR_FILENO, "%s: setpgid: %s\n",
			    program_name, strerror(errno));
			_exit(EXIT_INVALID);
		}
	}

	if (opt->timeout_signal != SIGKILL && opt->timeout_signal != SIGSTOP) {
		memset(&sa, 0, sizeof(sa));
		sa.sa_handler = SIG_DFL;
		sigemptyset(&sa.sa_mask);
		if (sigaction(opt->timeout_signal, &sa, NULL) != 0) {
			dprintf(STDERR_FILENO, "%s: sigaction(%d): %s\n",
			    program_name, opt->timeout_signal, strerror(errno));
			_exit(EXIT_INVALID);
		}
	}

	if (sigprocmask(SIG_SETMASK, oldmask, NULL) != 0) {
		dprintf(STDERR_FILENO, "%s: sigprocmask: %s\n",
		    program_name, strerror(errno));
		_exit(EXIT_INVALID);
	}

	execvp(opt->command_argv[0], opt->command_argv);
	saved_errno = errno;
	dprintf(STDERR_FILENO, "%s: exec(%s): %s\n", program_name,
	    opt->command_argv[0], strerror(saved_errno));
	_exit(saved_errno == ENOENT ? EXIT_CMD_NOENT : EXIT_CMD_ERROR);
}

static void
kill_self(int signo)
{
	struct sigaction sa;
	struct rlimit rl;
	sigset_t mask;

	memset(&rl, 0, sizeof(rl));
	(void)setrlimit(RLIMIT_CORE, &rl);

	memset(&sa, 0, sizeof(sa));
	sa.sa_handler = SIG_DFL;
	sigemptyset(&sa.sa_mask);
	(void)sigaction(signo, &sa, NULL);

	sigfillset(&mask);
	sigdelset(&mask, signo);
	(void)sigprocmask(SIG_SETMASK, &mask, NULL);

	raise(signo);
	_exit(128 + signo);
}

static bool
process_group_exists(pid_t pgid)
{
	if (pgid <= 0)
		return (false);
	if (kill(-pgid, 0) == 0)
		return (true);
	if (errno == EPERM)
		return (true);
	if (errno == ESRCH)
		return (false);
	return (true);
}

int
main(int argc, char **argv)
{
	const char optstr[] = "+fk:ps:v";
	const struct option longopts[] = {
		{ "foreground", no_argument, NULL, 'f' },
		{ "kill-after", required_argument, NULL, 'k' },
		{ "preserve-status", no_argument, NULL, 'p' },
		{ "signal", required_argument, NULL, 's' },
		{ "verbose", no_argument, NULL, 'v' },
		{ NULL, 0, NULL, 0 },
	};
	struct options opt;
	struct child_state child;
	struct runtime_state state;
	struct timespec timeout;
	sigset_t allmask, oldmask;
	pid_t child_pid;
	int ch;

	if (argv[0] != NULL && argv[0][0] != '\0')
		program_name = basename_const(argv[0]);

	memset(&opt, 0, sizeof(opt));
	opt.timeout_signal = SIGTERM;

	opterr = 0;
	while ((ch = getopt_long(argc, argv, optstr, longopts, NULL)) != -1) {
		switch (ch) {
		case 'f':
			opt.foreground = true;
			break;
		case 'k':
			opt.kill_after = parse_duration_or_die(optarg);
			opt.kill_after_set = true;
			break;
		case 'p':
			opt.preserve = true;
			break;
		case 's':
			if (!parse_signal_token(optarg, &opt.timeout_signal))
				die(EXIT_INVALID, "invalid signal: %s", optarg);
			break;
		case 'v':
			opt.verbose = true;
			break;
		default:
			usage();
		}
	}

	argc -= optind;
	argv += optind;
	if (argc < 2)
		usage();

	opt.duration = parse_duration_or_die(argv[0]);
	opt.command_argv = &argv[1];
	opt.command_name = argv[1];

	if (!opt.foreground)
		enable_subreaper_or_die();

	sigfillset(&allmask);
	sigdelset(&allmask, SIGKILL);
	sigdelset(&allmask, SIGSTOP);
	if (sigprocmask(SIG_SETMASK, &allmask, &oldmask) != 0)
		die_errno(EXIT_INVALID, "sigprocmask");

	child_pid = fork();
	if (child_pid < 0)
		die_errno(EXIT_INVALID, "fork");
	if (child_pid == 0)
		child_exec(&opt, &oldmask);

	if (!opt.foreground) {
		if (setpgid(child_pid, child_pid) != 0 &&
		    errno != EACCES && errno != ESRCH) {
			die_errno(EXIT_INVALID, "setpgid");
		}
	}

	memset(&child, 0, sizeof(child));
	memset(&state, 0, sizeof(state));
	state.active_signal = opt.timeout_signal;
	state.second_pending = opt.kill_after_set;
	if (opt.duration > 0.0L) {
		state.first_timer_active = true;
		state.first_deadline = monotonic_seconds() + opt.duration;
	}

	for (;;) {
		enum deadline_kind deadline_kind;
		bool have_children;
		siginfo_t si;
		int signo;

		reap_children(child_pid, &child, &have_children, &opt);
		if (opt.foreground) {
			if (child.reaped)
				break;
		} else {
			if (state.timed_out && have_children &&
			    !process_group_exists(child_pid)) {
				die(EXIT_INVALID,
				    "descendant processes escaped command process group; "
				    "unsupported on Linux without --foreground");
			}
			if (child.reaped && !have_children)
				break;
		}

		deadline_kind = next_deadline(&state, &timeout);
		if (deadline_kind == DEADLINE_NONE)
			signo = sigwaitinfo(&allmask, &si);
		else
			signo = sigtimedwait(&allmask, &si, &timeout);

		if (signo >= 0) {
			char sigbuf[32];

			if (signo == SIGCHLD)
				continue;

			if (!signal_name_for_number(signo, sigbuf, sizeof(sigbuf)))
				snprintf(sigbuf, sizeof(sigbuf), "%d", signo);

			if (signo == SIGALRM) {
				vlog(&opt, "received SIGALRM, treating as timeout");
				state.timed_out = true;
				state.first_timer_active = false;
				send_signal_to_command(child_pid, state.active_signal, &opt);
				arm_second_timer(&state, &opt);
				continue;
			}

			if (signo == state.active_signal || is_terminating_signal(signo)) {
				vlog(&opt, "received terminating signal %s(%d)",
				    sigbuf, signo);
				send_signal_to_command(child_pid, signo, &opt);
				arm_second_timer(&state, &opt);
			} else {
				vlog(&opt, "received signal %s(%d)", sigbuf, signo);
				send_signal_to_command(child_pid, signo, &opt);
			}
			continue;
		}

		if (errno == EINTR)
			continue;
		if (errno != EAGAIN)
			die_errno(EXIT_INVALID, "sigwait");

		if (deadline_kind == DEADLINE_FIRST) {
			state.timed_out = true;
			state.first_timer_active = false;
			vlog(&opt, "time limit reached");
			send_signal_to_command(child_pid, state.active_signal, &opt);
			arm_second_timer(&state, &opt);
		} else if (deadline_kind == DEADLINE_SECOND) {
			state.second_timer_active = false;
			vlog(&opt, "kill-after limit reached");
			send_signal_to_command(child_pid, SIGKILL, &opt);
		}
	}

	(void)sigprocmask(SIG_SETMASK, &oldmask, NULL);

	if (!child.reaped)
		die(EXIT_INVALID, "failed to retrieve command status");

	if (state.timed_out && !opt.preserve)
		return (EXIT_TIMEOUT);

	if (WIFEXITED(child.status))
		return (WEXITSTATUS(child.status));
	if (WIFSIGNALED(child.status))
		kill_self(WTERMSIG(child.status));

	return (EXIT_INVALID);
}