diff options
Diffstat (limited to 'kill.c')
-rw-r--r-- | kill.c | 203 |
1 files changed, 100 insertions, 103 deletions
@@ -4,137 +4,134 @@ | |||
4 | #include <unistd.h> | 4 | #include <unistd.h> |
5 | #include <signal.h> | 5 | #include <signal.h> |
6 | 6 | ||
7 | const char kill_usage[] = "kill [-signal] process-id [process-id ...]\n"; | 7 | const char kill_usage[] = "kill [-signal] process-id [process-id ...]\n"; |
8 | 8 | ||
9 | struct signal_name { | 9 | struct signal_name { |
10 | const char * name; | 10 | const char *name; |
11 | int number; | 11 | int number; |
12 | }; | 12 | }; |
13 | 13 | ||
14 | const struct signal_name signames[] = { | 14 | const struct signal_name signames[] = { |
15 | { "HUP", SIGHUP }, | 15 | {"HUP", SIGHUP}, |
16 | { "INT", SIGINT }, | 16 | {"INT", SIGINT}, |
17 | { "QUIT", SIGQUIT }, | 17 | {"QUIT", SIGQUIT}, |
18 | { "ILL", SIGILL }, | 18 | {"ILL", SIGILL}, |
19 | { "TRAP", SIGTRAP }, | 19 | {"TRAP", SIGTRAP}, |
20 | { "ABRT", SIGABRT }, | 20 | {"ABRT", SIGABRT}, |
21 | #ifndef __alpha__ | 21 | #ifndef __alpha__ |
22 | { "IOT", SIGIOT }, | 22 | {"IOT", SIGIOT}, |
23 | #endif | 23 | #endif |
24 | #if defined(sparc) || defined(__alpha__) | 24 | #if defined(sparc) || defined(__alpha__) |
25 | { "EMT", SIGEMT }, | 25 | {"EMT", SIGEMT}, |
26 | #else | 26 | #else |
27 | { "BUS", SIGBUS }, | 27 | {"BUS", SIGBUS}, |
28 | #endif | 28 | #endif |
29 | { "FPE", SIGFPE }, | 29 | {"FPE", SIGFPE}, |
30 | { "KILL", SIGKILL }, | 30 | {"KILL", SIGKILL}, |
31 | #if defined(sparc) || defined(__alpha__) | 31 | #if defined(sparc) || defined(__alpha__) |
32 | { "BUS", SIGBUS }, | 32 | {"BUS", SIGBUS}, |
33 | #else | 33 | #else |
34 | { "USR1", SIGUSR1 }, | 34 | {"USR1", SIGUSR1}, |
35 | #endif | 35 | #endif |
36 | { "SEGV", SIGSEGV }, | 36 | {"SEGV", SIGSEGV}, |
37 | #if defined(sparc) || defined(__alpha__) | 37 | #if defined(sparc) || defined(__alpha__) |
38 | { "SYS", SIGSYS }, | 38 | {"SYS", SIGSYS}, |
39 | #else | 39 | #else |
40 | { "USR2", SIGUSR2 }, | 40 | {"USR2", SIGUSR2}, |
41 | #endif | 41 | #endif |
42 | { "PIPE", SIGPIPE }, | 42 | {"PIPE", SIGPIPE}, |
43 | { "ALRM", SIGALRM }, | 43 | {"ALRM", SIGALRM}, |
44 | { "TERM", SIGTERM }, | 44 | {"TERM", SIGTERM}, |
45 | #if defined(sparc) || defined(__alpha__) | 45 | #if defined(sparc) || defined(__alpha__) |
46 | { "URG", SIGURG }, | 46 | {"URG", SIGURG}, |
47 | { "STOP", SIGSTOP }, | 47 | {"STOP", SIGSTOP}, |
48 | { "TSTP", SIGTSTP }, | 48 | {"TSTP", SIGTSTP}, |
49 | { "CONT", SIGCONT }, | 49 | {"CONT", SIGCONT}, |
50 | { "CHLD", SIGCHLD }, | 50 | {"CHLD", SIGCHLD}, |
51 | { "TTIN", SIGTTIN }, | 51 | {"TTIN", SIGTTIN}, |
52 | { "TTOU", SIGTTOU }, | 52 | {"TTOU", SIGTTOU}, |
53 | { "IO", SIGIO }, | 53 | {"IO", SIGIO}, |
54 | # ifndef __alpha__ | 54 | # ifndef __alpha__ |
55 | { "POLL", SIGIO }, | 55 | {"POLL", SIGIO}, |
56 | # endif | 56 | # endif |
57 | { "XCPU", SIGXCPU }, | 57 | {"XCPU", SIGXCPU}, |
58 | { "XFSZ", SIGXFSZ }, | 58 | {"XFSZ", SIGXFSZ}, |
59 | { "VTALRM", SIGVTALRM }, | 59 | {"VTALRM", SIGVTALRM}, |
60 | { "PROF", SIGPROF }, | 60 | {"PROF", SIGPROF}, |
61 | { "WINCH", SIGWINCH }, | 61 | {"WINCH", SIGWINCH}, |
62 | # ifdef __alpha__ | 62 | # ifdef __alpha__ |
63 | { "INFO", SIGINFO }, | 63 | {"INFO", SIGINFO}, |
64 | # else | 64 | # else |
65 | { "LOST", SIGLOST }, | 65 | {"LOST", SIGLOST}, |
66 | # endif | 66 | # endif |
67 | { "USR1", SIGUSR1 }, | 67 | {"USR1", SIGUSR1}, |
68 | { "USR2", SIGUSR2 }, | 68 | {"USR2", SIGUSR2}, |
69 | #else | 69 | #else |
70 | { "STKFLT", SIGSTKFLT }, | 70 | {"STKFLT", SIGSTKFLT}, |
71 | { "CHLD", SIGCHLD }, | 71 | {"CHLD", SIGCHLD}, |
72 | { "CONT", SIGCONT }, | 72 | {"CONT", SIGCONT}, |
73 | { "STOP", SIGSTOP }, | 73 | {"STOP", SIGSTOP}, |
74 | { "TSTP", SIGTSTP }, | 74 | {"TSTP", SIGTSTP}, |
75 | { "TTIN", SIGTTIN }, | 75 | {"TTIN", SIGTTIN}, |
76 | { "TTOU", SIGTTOU }, | 76 | {"TTOU", SIGTTOU}, |
77 | { "URG", SIGURG }, | 77 | {"URG", SIGURG}, |
78 | { "XCPU", SIGXCPU }, | 78 | {"XCPU", SIGXCPU}, |
79 | { "XFSZ", SIGXFSZ }, | 79 | {"XFSZ", SIGXFSZ}, |
80 | { "VTALRM", SIGVTALRM }, | 80 | {"VTALRM", SIGVTALRM}, |
81 | { "PROF", SIGPROF }, | 81 | {"PROF", SIGPROF}, |
82 | { "WINCH", SIGWINCH }, | 82 | {"WINCH", SIGWINCH}, |
83 | { "IO", SIGIO }, | 83 | {"IO", SIGIO}, |
84 | { "POLL", SIGPOLL }, | 84 | {"POLL", SIGPOLL}, |
85 | { "PWR", SIGPWR }, | 85 | {"PWR", SIGPWR}, |
86 | { "UNUSED", SIGUNUSED }, | 86 | {"UNUSED", SIGUNUSED}, |
87 | #endif | 87 | #endif |
88 | { 0, 0 } | 88 | {0, 0} |
89 | }; | 89 | }; |
90 | 90 | ||
91 | extern int | 91 | extern int kill_main (int argc, char **argv) |
92 | kill_main(struct FileInfo * i, int argc, char * * argv) | ||
93 | { | 92 | { |
94 | int had_error = 0; | 93 | int had_error = 0; |
95 | int sig = SIGTERM; | 94 | int sig = SIGTERM; |
96 | if ( argv[1][0] == '-' ) { | ||
97 | if ( argv[1][1] >= '0' && argv[1][1] <= '9' ) { | ||
98 | sig = atoi(&argv[1][1]); | ||
99 | if ( sig < 0 || sig >= NSIG ) { | ||
100 | usage(kill_usage); | ||
101 | exit(-1); | ||
102 | } | ||
103 | } | ||
104 | else { | ||
105 | const struct signal_name * s = signames; | ||
106 | for ( ; ; ) { | ||
107 | if ( strcmp(s->name, &argv[1][1]) == 0 ) { | ||
108 | sig = s->number; | ||
109 | break; | ||
110 | } | ||
111 | s++; | ||
112 | if ( s->name == 0 ) { | ||
113 | usage(kill_usage); | ||
114 | exit(-1); | ||
115 | } | ||
116 | } | ||
117 | } | ||
118 | argv++; | ||
119 | argc--; | ||
120 | 95 | ||
121 | } | 96 | |
122 | while ( argc > 1 ) { | 97 | |
123 | int pid; | 98 | if (argv[1][0] == '-') { |
124 | if ( argv[1][0] < '0' || argv[1][0] > '9' ) { | 99 | if (argv[1][1] >= '0' && argv[1][1] <= '9') { |
125 | usage(kill_usage); | 100 | sig = atoi (&argv[1][1]); |
126 | exit(-1); | 101 | if (sig < 0 || sig >= NSIG) |
127 | } | 102 | goto end; |
128 | pid = atoi(argv[1]); | 103 | } else { |
129 | if ( kill(pid, sig) != 0 ) { | 104 | const struct signal_name *s = signames; |
130 | had_error = 1; | 105 | for (;;) { |
131 | perror(argv[1]); | 106 | if (strcmp (s->name, &argv[1][1]) == 0) { |
107 | sig = s->number; | ||
108 | break; | ||
132 | } | 109 | } |
133 | argv++; | 110 | s++; |
134 | argc--; | 111 | if (s->name == 0) |
112 | goto end; | ||
113 | } | ||
114 | } | ||
115 | argv++; | ||
116 | argc--; | ||
117 | |||
118 | } | ||
119 | while (argc > 1) { | ||
120 | int pid; | ||
121 | if (argv[1][0] < '0' || argv[1][0] > '9') | ||
122 | goto end; | ||
123 | pid = atoi (argv[1]); | ||
124 | if (kill (pid, sig) != 0) { | ||
125 | had_error = 1; | ||
126 | perror (argv[1]); | ||
135 | } | 127 | } |
136 | if ( had_error ) | 128 | argv++; |
137 | return -1; | 129 | argc--; |
138 | else | 130 | } |
139 | return 0; | 131 | if (had_error) { |
132 | end: | ||
133 | fprintf(stderr, "Usage: %s\n", kill_usage); | ||
134 | exit ( FALSE); | ||
135 | } | ||
136 | exit (TRUE); | ||
140 | } | 137 | } |