-
Notifications
You must be signed in to change notification settings - Fork 12
/
kill.c
130 lines (112 loc) · 2.36 KB
/
kill.c
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
/* See LICENSE file for copyright and license details. */
#include <sys/wait.h>
#include <ctype.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include "util.h"
struct {
const char *name;
const int sig;
} sigs[] = {
{ "0", 0 },
#define SIG(n) { #n, SIG##n }
SIG(ABRT), SIG(ALRM), SIG(BUS), SIG(CHLD), SIG(CONT), SIG(FPE), SIG(HUP),
SIG(ILL), SIG(INT), SIG(KILL), SIG(PIPE), SIG(QUIT), SIG(SEGV), SIG(STOP),
SIG(TERM), SIG(TSTP), SIG(TTIN), SIG(TTOU), SIG(USR1), SIG(USR2), SIG(URG),
#undef SIG
};
const char *
sig2name(const int sig)
{
size_t i;
for (i = 0; i < LEN(sigs); i++)
if (sigs[i].sig == sig)
return sigs[i].name;
eprintf("%d: bad signal number\n", sig);
return NULL; /* not reached */
}
int
name2sig(const char *name)
{
size_t i;
for (i = 0; i < LEN(sigs); i++)
if (!strcasecmp(sigs[i].name, name))
return sigs[i].sig;
eprintf("%s: bad signal name\n", name);
return -1; /* not reached */
}
static void
usage(void)
{
eprintf("usage: %s [-s signame | -num | -signame] pid ...\n"
" %s -l [num]\n", argv0, argv0);
}
int
main(int argc, char *argv[])
{
pid_t pid;
size_t i;
int ret = 0, sig = SIGTERM;
argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0;
if (!argc)
usage();
if ((*argv)[0] == '-') {
switch ((*argv)[1]) {
case 'l':
if ((*argv)[2])
goto longopt;
argc--, argv++;
if (!argc) {
for (i = 0; i < LEN(sigs); i++)
puts(sigs[i].name);
} else if (argc == 1) {
sig = estrtonum(*argv, 0, INT_MAX);
if (sig > 128)
sig = WTERMSIG(sig);
puts(sig2name(sig));
} else {
usage();
}
return fshut(stdout, "<stdout>");
case 's':
if ((*argv)[2])
goto longopt;
argc--, argv++;
if (!argc)
usage();
sig = name2sig(*argv);
argc--, argv++;
break;
case '-':
if ((*argv)[2])
goto longopt;
argc--, argv++;
break;
default:
longopt:
/* XSI-extensions -argnum and -argname*/
if (isdigit((*argv)[1])) {
sig = estrtonum((*argv) + 1, 0, INT_MAX);
sig2name(sig);
} else {
sig = name2sig((*argv) + 1);
}
argc--, argv++;
}
}
if (argc && !strcmp(*argv, "--"))
argc--, argv++;
if (!argc)
usage();
for (; *argv; argc--, argv++) {
pid = estrtonum(*argv, INT_MIN, INT_MAX);
if (kill(pid, sig) < 0) {
weprintf("kill %d:", pid);
ret = 1;
}
}
return ret;
}