aboutsummaryrefslogtreecommitdiffstats
path: root/pdeath.c
blob: 2cdbee2679ea3b40409e2fd0909ec342fa6530ed (plain) (blame)
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
/* See LICENSE file for copyright and license details. */
#include <sys/prctl.h>
#include <errno.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>

struct sig {
	int signo;
	const char *name;
} sigs[] = {
	{SIGHUP,    "HUP"},
	{SIGINT,    "INT"},
	{SIGQUIT,   "QUIT"},
	{SIGILL,    "ILL"},
	{SIGTRAP,   "TRAP"},
	{SIGABRT,   "ABRT"},
	{SIGIOT,    "IOT"},
	{SIGBUS,    "BUS"},
	{SIGFPE,    "FPE"},
	{SIGKILL,   "KILL"},
	{SIGUSR1,   "USR1"},
	{SIGSEGV,   "SEGV"},
	{SIGUSR2,   "USR2"},
	{SIGPIPE,   "PIPE"},
	{SIGALRM,   "ALRM"},
	{SIGTERM,   "TERM"},
	{SIGSTKFLT, "TKFLT"},
	{SIGCLD,    "CLD"},
	{SIGCHLD,   "CHLD"},
	{SIGCONT,   "CONT"},
	{SIGSTOP,   "STOP"},
	{SIGTSTP,   "TSTP"},
	{SIGTTIN,   "TTIN"},
	{SIGTTOU,   "TTOU"},
	{SIGURG,    "URG"},
	{SIGXCPU,   "XCPU"},
	{SIGXFSZ,   "XFSZ"},
	{SIGVTALRM, "VTALRM"},
	{SIGPROF,   "PROF"},
	{SIGWINCH,  "WINCH"},
	{SIGPOLL,   "POLL"},
	{SIGIO,     "IO"},
	{SIGPWR,    "PWR"},
	{SIGSYS,    "SYS"},
	{SIGUNUSED, "UNUSED"},
	{0,         NULL}
};

const char *argv0;

static void
usage(void)
{
	fprintf(stderr, "usage: %s (-L | (signal)[(+|-)off] command [argument] ...)\n", argv0);
	exit(127);
}

static void
invalid_signal(void)
{
	fprintf(stderr, "%s: invalid signal\n", argv0);
	exit(127);
}

static void
print_signals(void)
{
	struct sig *sig = sigs;
	for (; sig->name; sig++)
		printf("%s\n", sig->name);
	printf("RTMIN\nRTMAX\n");
}

static int
strict_strtoi(const char *str, int min, int max)
{
	char *end;
	long int ret;
	errno = 0;
	ret = strtol(str, &end, 10);
	if (errno || *end)
		usage();
	if (ret < min || ret > max)
		invalid_signal();
	return (int)ret;
}

static int
get_signal(const char *str)
{
	struct sig *sig = sigs;
	int have_prefix = 0;
	char *end;
	long int ret;
	if (!strncasecmp(str, "SIG", 3)) {
		str += 3;
		have_prefix = 1;
	}
	if (!strcasecmp(str, "RTMIN"))
		return SIGRTMIN;
	if (!strcasecmp(str, "RTMAX"))
		return SIGRTMAX;
	for (; sig->name; sig++) {
		if (!strcasecmp(str, sig->name))
		  return sig->signo;
	}
	if (!have_prefix) {
		errno = 0;
		ret = strtol(str, &end, 10);
		if (!errno && !*end && 0 <= ret && ret < _NSIG)
			return (int)ret;

	}
	invalid_signal();
	return -1;
}

int
main(int argc, char *argv[])
{
	char *off;
	char sign = '\0';
	int signo;

	argv0 = *argv ? (argc--, *argv++) : "pdeath";

	if (*argv && **argv == '-') {
		if (argc == 1 && !strcmp(*argv, "-L")) {
			print_signals();
			if (fflush(stdout) || fclose(stdout)) {
				perror(argv0);
				return 127;
			}
			return 0;
		}
		usage();
	}

	if (argc < 2)
		usage();

	if ((off = strpbrk(*argv, "+-"))) {
		sign = *off;
		*off++ = '\0';
	}
	signo = get_signal(*argv);
	if (sign == '-')
		signo -= strict_strtoi(off, 0, signo);
	else if (sign == '+')
		signo += strict_strtoi(off, 0, _NSIG - 1 - signo);
	argv++, argc--;

	if (prctl(PR_SET_PDEATHSIG, signo) == -1) {
		perror(argv0);
		return 127;
	}

	execvp(*argv, argv);
	fprintf(stderr, "%s: %s: %s\n", argv0, strerror(errno), *argv);
	return 127;
}