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