kill.c revision 1.5 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1988 Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.1 cgd #ifndef lint
35 1.1 cgd char copyright[] =
36 1.1 cgd "@(#) Copyright (c) 1988 Regents of the University of California.\n\
37 1.1 cgd All rights reserved.\n";
38 1.1 cgd #endif /* not lint */
39 1.1 cgd
40 1.1 cgd #ifndef lint
41 1.5 mycroft /*static char sccsid[] = "from: @(#)kill.c 5.3 (Berkeley) 7/1/91";*/
42 1.5 mycroft static char rcsid[] = "$Id: kill.c,v 1.5 1993/08/01 18:59:35 mycroft Exp $";
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd #include <signal.h>
46 1.1 cgd #include <errno.h>
47 1.1 cgd #include <stdio.h>
48 1.1 cgd #include <stdlib.h>
49 1.1 cgd #include <string.h>
50 1.1 cgd #include <ctype.h>
51 1.1 cgd
52 1.1 cgd static char *signals[] = {
53 1.4 jtc "HUP", "INT", "QUIT", "ILL", "TRAP", "ABRT", /* 1 - 6 */
54 1.4 jtc "EMT", "FPE", "KILL", "BUS", "SEGV", "SYS", /* 7 - 12 */
55 1.4 jtc "PIPE", "ALRM", "TERM", "URG", "STOP", "TSTP", /* 13 - 18 */
56 1.4 jtc "CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU", /* 19 - 24 */
57 1.4 jtc "XFSZ", "VTALRM", "PROF", "WINCH", "INFO", "USR1", /* 25 - 30 */
58 1.4 jtc "USR2", NULL, /* 31 - 32 */
59 1.1 cgd };
60 1.1 cgd
61 1.1 cgd main(argc, argv)
62 1.1 cgd int argc;
63 1.1 cgd char **argv;
64 1.1 cgd {
65 1.1 cgd register int errors, numsig, pid;
66 1.1 cgd register char **p;
67 1.1 cgd char *ep;
68 1.1 cgd
69 1.1 cgd if (argc < 2)
70 1.1 cgd usage();
71 1.1 cgd
72 1.4 jtc numsig = SIGTERM;
73 1.4 jtc argc--, argv++;
74 1.4 jtc if (strcmp(*argv, "-l") == 0) {
75 1.4 jtc if (argc > 2) {
76 1.4 jtc usage ();
77 1.4 jtc /* NOTREACHED */
78 1.4 jtc }
79 1.4 jtc if (argc == 2) {
80 1.4 jtc argv++;
81 1.4 jtc if (isdigit(**argv)) {
82 1.4 jtc numsig = strtol(*argv, &ep, 10);
83 1.4 jtc if (*argv && !*ep) {
84 1.4 jtc if (numsig > 0 && numsig < NSIG) {
85 1.4 jtc printsig (numsig);
86 1.4 jtc exit (0);
87 1.4 jtc }
88 1.4 jtc
89 1.4 jtc numsig -= 128;
90 1.4 jtc if (numsig > 0 && numsig < NSIG) {
91 1.4 jtc printsig (numsig);
92 1.4 jtc exit (0);
93 1.4 jtc }
94 1.4 jtc }
95 1.4 jtc (void)fprintf(stderr,
96 1.4 jtc "kill: illegal signal number %s\n", *argv);
97 1.4 jtc exit(1);
98 1.4 jtc }
99 1.4 jtc usage ();
100 1.4 jtc /* NOTREACHED */
101 1.4 jtc }
102 1.4 jtc printsignals(stdout);
103 1.1 cgd exit(0);
104 1.4 jtc } else if (strcmp(*argv, "-s") == 0) {
105 1.4 jtc if (argc < 2) {
106 1.4 jtc (void)fprintf(stderr,
107 1.4 jtc "kill: option requires an argument -- s\n");
108 1.4 jtc usage();
109 1.4 jtc }
110 1.4 jtc argc--,argv++;
111 1.4 jtc if (strcmp (*argv, "0") == 0) {
112 1.4 jtc numsig = 0;
113 1.4 jtc } else {
114 1.4 jtc if ((numsig = signame_to_signum (*argv)) < 0) {
115 1.4 jtc nosig(*argv);
116 1.4 jtc /* NOTREACHED */
117 1.4 jtc }
118 1.4 jtc }
119 1.4 jtc argc--,argv++;
120 1.4 jtc } else if (**argv == '-') {
121 1.1 cgd ++*argv;
122 1.1 cgd if (isalpha(**argv)) {
123 1.4 jtc if ((numsig = signame_to_signum (*argv)) < 0) {
124 1.4 jtc nosig(*argv);
125 1.4 jtc /* NOTREACHED */
126 1.1 cgd }
127 1.1 cgd } else if (isdigit(**argv)) {
128 1.1 cgd numsig = strtol(*argv, &ep, 10);
129 1.1 cgd if (!*argv || *ep) {
130 1.1 cgd (void)fprintf(stderr,
131 1.1 cgd "kill: illegal signal number %s\n", *argv);
132 1.1 cgd exit(1);
133 1.1 cgd }
134 1.4 jtc if (numsig <= 0 || numsig >= NSIG) {
135 1.1 cgd nosig(*argv);
136 1.4 jtc /* NOTREACHED */
137 1.4 jtc }
138 1.1 cgd } else
139 1.1 cgd nosig(*argv);
140 1.4 jtc argc--,argv++;
141 1.1 cgd }
142 1.1 cgd
143 1.1 cgd if (!*argv)
144 1.1 cgd usage();
145 1.1 cgd
146 1.1 cgd for (errors = 0; *argv; ++argv) {
147 1.1 cgd pid = strtol(*argv, &ep, 10);
148 1.1 cgd if (!*argv || *ep) {
149 1.1 cgd (void)fprintf(stderr,
150 1.1 cgd "kill: illegal process id %s\n", *argv);
151 1.1 cgd continue;
152 1.1 cgd }
153 1.1 cgd if (kill(pid, numsig) == -1) {
154 1.1 cgd (void)fprintf(stderr,
155 1.1 cgd "kill: %s: %s\n", *argv, strerror(errno));
156 1.1 cgd errors = 1;
157 1.1 cgd }
158 1.1 cgd }
159 1.1 cgd exit(errors);
160 1.1 cgd }
161 1.1 cgd
162 1.4 jtc int
163 1.4 jtc signame_to_signum (sig)
164 1.4 jtc char *sig;
165 1.4 jtc {
166 1.4 jtc char **p;
167 1.4 jtc
168 1.4 jtc if (!strncasecmp(sig, "sig", 3))
169 1.4 jtc sig += 3;
170 1.4 jtc for (p = signals; *p; ++p) {
171 1.4 jtc if (!strcasecmp(*p, sig)) {
172 1.4 jtc return p - signals + 1;
173 1.4 jtc }
174 1.4 jtc }
175 1.4 jtc return -1;
176 1.4 jtc }
177 1.4 jtc
178 1.1 cgd nosig(name)
179 1.1 cgd char *name;
180 1.1 cgd {
181 1.1 cgd (void)fprintf(stderr,
182 1.1 cgd "kill: unknown signal %s; valid signals:\n", name);
183 1.4 jtc printsignals(stderr);
184 1.1 cgd exit(1);
185 1.1 cgd }
186 1.1 cgd
187 1.4 jtc printsig(sig)
188 1.4 jtc int sig;
189 1.4 jtc {
190 1.4 jtc printf ("%s\n", signals[sig - 1]);
191 1.4 jtc }
192 1.4 jtc
193 1.4 jtc printsignals(fp)
194 1.1 cgd FILE *fp;
195 1.1 cgd {
196 1.4 jtc register char **p = signals;;
197 1.1 cgd
198 1.4 jtc /* From POSIX 1003.2, Draft 11.2:
199 1.4 jtc When the -l option is specified, the symbolic name of each
200 1.4 jtc signal shall be written in the following format:
201 1.4 jtc "%s%c", <signal_name>, <separator>
202 1.4 jtc where the <signal_name> is in uppercase, without the SIG prefix,
203 1.4 jtc and the <separator> shall either be a <newline> or a <space>.
204 1.4 jtc For the last signal written, <separator> shall be a <newline> */
205 1.4 jtc
206 1.4 jtc /* This looses if the signals array is empty; But, since it
207 1.4 jtc will "never happen", there is no need to add wrap this
208 1.4 jtc in a conditional that will always succeed. */
209 1.4 jtc (void)fprintf(fp, "%s", *p);
210 1.4 jtc
211 1.4 jtc for (++p ; *p; ++p) {
212 1.4 jtc (void)fprintf(fp, " %s", *p);
213 1.1 cgd }
214 1.1 cgd (void)fprintf(fp, "\n");
215 1.1 cgd }
216 1.1 cgd
217 1.1 cgd usage()
218 1.1 cgd {
219 1.4 jtc (void)fprintf(stderr, "usage: kill [-s signal_name] pid ...\n");
220 1.4 jtc (void)fprintf(stderr, " kill -l [exit_status]\n");
221 1.4 jtc (void)fprintf(stderr, "obsolete usage:\n");
222 1.4 jtc (void)fprintf(stderr, " kill -signal_name pid ...\n");
223 1.4 jtc (void)fprintf(stderr, " kill -signal_number pid ...\n");
224 1.1 cgd exit(1);
225 1.1 cgd }
226