kill.c revision 1.9 1 1.1 cgd /*
2 1.7 mycroft * Copyright (c) 1988, 1993, 1994
3 1.7 mycroft * The Regents of the University of California. 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.7 mycroft static char copyright[] =
36 1.7 mycroft "@(#) Copyright (c) 1988, 1993, 1994\n\
37 1.7 mycroft The Regents of the University of California. All rights reserved.\n";
38 1.1 cgd #endif /* not lint */
39 1.1 cgd
40 1.1 cgd #ifndef lint
41 1.7 mycroft /*static char sccsid[] = "from: @(#)kill.c 8.3 (Berkeley) 4/2/94";*/
42 1.9 jtc static char *rcsid = "$Id: kill.c,v 1.9 1995/03/17 05:48:48 jtc Exp $";
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.7 mycroft #include <ctype.h>
46 1.7 mycroft #include <err.h>
47 1.7 mycroft #include <errno.h>
48 1.1 cgd #include <signal.h>
49 1.1 cgd #include <stdio.h>
50 1.1 cgd #include <stdlib.h>
51 1.1 cgd #include <string.h>
52 1.1 cgd
53 1.7 mycroft void nosig __P((char *));
54 1.7 mycroft void printsignals __P((FILE *));
55 1.7 mycroft int signame_to_signum __P((char *));
56 1.7 mycroft void usage __P((void));
57 1.7 mycroft
58 1.7 mycroft int
59 1.1 cgd main(argc, argv)
60 1.1 cgd int argc;
61 1.7 mycroft char *argv[];
62 1.1 cgd {
63 1.7 mycroft int errors, numsig, pid;
64 1.1 cgd char *ep;
65 1.1 cgd
66 1.1 cgd if (argc < 2)
67 1.1 cgd usage();
68 1.1 cgd
69 1.4 jtc numsig = SIGTERM;
70 1.7 mycroft
71 1.4 jtc argc--, argv++;
72 1.7 mycroft if (!strcmp(*argv, "-l")) {
73 1.7 mycroft argc--, argv++;
74 1.7 mycroft if (argc > 1)
75 1.7 mycroft usage();
76 1.7 mycroft if (argc == 1) {
77 1.7 mycroft if (!isdigit(**argv))
78 1.7 mycroft usage();
79 1.7 mycroft numsig = strtol(*argv, &ep, 10);
80 1.7 mycroft if (!*argv || *ep)
81 1.7 mycroft errx(1, "illegal signal number: %s", *argv);
82 1.7 mycroft if (numsig >= 128)
83 1.7 mycroft numsig -= 128;
84 1.7 mycroft if (numsig <= 0 || numsig >= NSIG)
85 1.7 mycroft nosig(*argv);
86 1.7 mycroft printf("%s\n", sys_signame[numsig]);
87 1.7 mycroft exit(0);
88 1.4 jtc }
89 1.4 jtc printsignals(stdout);
90 1.1 cgd exit(0);
91 1.7 mycroft }
92 1.7 mycroft
93 1.7 mycroft if (!strcmp(*argv, "-s")) {
94 1.7 mycroft argc--, argv++;
95 1.7 mycroft if (argc < 1) {
96 1.7 mycroft warnx("option requires an argument -- s");
97 1.4 jtc usage();
98 1.4 jtc }
99 1.7 mycroft if (strcmp(*argv, "0")) {
100 1.7 mycroft if ((numsig = signame_to_signum(*argv)) < 0)
101 1.7 mycroft nosig(*argv);
102 1.7 mycroft } else
103 1.4 jtc numsig = 0;
104 1.7 mycroft argc--, argv++;
105 1.4 jtc } else if (**argv == '-') {
106 1.1 cgd ++*argv;
107 1.1 cgd if (isalpha(**argv)) {
108 1.7 mycroft if ((numsig = signame_to_signum(*argv)) < 0)
109 1.4 jtc nosig(*argv);
110 1.1 cgd } else if (isdigit(**argv)) {
111 1.1 cgd numsig = strtol(*argv, &ep, 10);
112 1.7 mycroft if (!*argv || *ep)
113 1.7 mycroft errx(1, "illegal signal number: %s", *argv);
114 1.9 jtc if (numsig < 0 || numsig >= NSIG)
115 1.1 cgd nosig(*argv);
116 1.1 cgd } else
117 1.1 cgd nosig(*argv);
118 1.7 mycroft argc--, argv++;
119 1.1 cgd }
120 1.1 cgd
121 1.7 mycroft if (argc == 0)
122 1.1 cgd usage();
123 1.1 cgd
124 1.7 mycroft for (errors = 0; argc; argc--, argv++) {
125 1.1 cgd pid = strtol(*argv, &ep, 10);
126 1.1 cgd if (!*argv || *ep) {
127 1.7 mycroft warnx("illegal process id: %s", *argv);
128 1.7 mycroft errors = 1;
129 1.7 mycroft } else if (kill(pid, numsig) == -1) {
130 1.7 mycroft warn("%s", *argv);
131 1.1 cgd errors = 1;
132 1.1 cgd }
133 1.1 cgd }
134 1.7 mycroft
135 1.1 cgd exit(errors);
136 1.1 cgd }
137 1.1 cgd
138 1.4 jtc int
139 1.7 mycroft signame_to_signum(sig)
140 1.4 jtc char *sig;
141 1.4 jtc {
142 1.6 mycroft int n;
143 1.4 jtc
144 1.4 jtc if (!strncasecmp(sig, "sig", 3))
145 1.4 jtc sig += 3;
146 1.6 mycroft for (n = 1; n < NSIG; n++) {
147 1.6 mycroft if (!strcasecmp(sys_signame[n], sig))
148 1.7 mycroft return (n);
149 1.4 jtc }
150 1.7 mycroft return (-1);
151 1.4 jtc }
152 1.4 jtc
153 1.7 mycroft void
154 1.1 cgd nosig(name)
155 1.1 cgd char *name;
156 1.1 cgd {
157 1.7 mycroft
158 1.7 mycroft warnx("unknown signal %s; valid signals:", name);
159 1.4 jtc printsignals(stderr);
160 1.1 cgd exit(1);
161 1.1 cgd }
162 1.1 cgd
163 1.7 mycroft void
164 1.4 jtc printsignals(fp)
165 1.1 cgd FILE *fp;
166 1.1 cgd {
167 1.6 mycroft int n;
168 1.1 cgd
169 1.7 mycroft for (n = 1; n < NSIG; n++) {
170 1.7 mycroft (void)fprintf(fp, "%s", sys_signame[n]);
171 1.7 mycroft if (n == (NSIG / 2) || n == (NSIG - 1))
172 1.7 mycroft (void)fprintf(fp, "\n");
173 1.6 mycroft else
174 1.6 mycroft (void)fprintf(fp, " ");
175 1.7 mycroft }
176 1.1 cgd }
177 1.1 cgd
178 1.7 mycroft void
179 1.1 cgd usage()
180 1.1 cgd {
181 1.7 mycroft
182 1.4 jtc (void)fprintf(stderr, "usage: kill [-s signal_name] pid ...\n");
183 1.4 jtc (void)fprintf(stderr, " kill -l [exit_status]\n");
184 1.4 jtc (void)fprintf(stderr, " kill -signal_name pid ...\n");
185 1.4 jtc (void)fprintf(stderr, " kill -signal_number pid ...\n");
186 1.1 cgd exit(1);
187 1.1 cgd }
188