Annotation of prex/usr/bin/kill/kill.c, Revision 1.1.1.1
1.1 nbrk 1: /*
2: * Copyright (c) 1988, 1993, 1994
3: * The Regents of the University of California. All rights reserved.
4: *
5: * Redistribution and use in source and binary forms, with or without
6: * modification, are permitted provided that the following conditions
7: * are met:
8: * 1. Redistributions of source code must retain the above copyright
9: * notice, this list of conditions and the following disclaimer.
10: * 2. Redistributions in binary form must reproduce the above copyright
11: * notice, this list of conditions and the following disclaimer in the
12: * documentation and/or other materials provided with the distribution.
13: * 3. Neither the name of the University nor the names of its contributors
14: * may be used to endorse or promote products derived from this software
15: * without specific prior written permission.
16: *
17: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27: * SUCH DAMAGE.
28: */
29:
30: #include <ctype.h>
31: #include <err.h>
32: #include <errno.h>
33: #include <signal.h>
34: #include <stdio.h>
35: #include <stdlib.h>
36: #include <string.h>
37:
38: #ifdef CMDBOX
39: #define main(argc, argv) kill_main(argc, argv)
40: #endif
41:
42: static void nosig(char *);
43: static void printsignals(FILE *);
44: static int signame_to_signum(char *);
45: static void usage(void);
46:
47: int
48: main(int argc, char *argv[])
49: {
50: int errors, numsig, pid;
51: char *ep;
52:
53: if (argc < 2)
54: usage();
55:
56: numsig = SIGTERM;
57:
58: argc--, argv++;
59: if (!strcmp(*argv, "-l")) {
60: argc--, argv++;
61: if (argc > 1)
62: usage();
63: if (argc == 1) {
64: if (isdigit((unsigned char)**argv) == 0)
65: usage();
66: numsig = strtol(*argv, &ep, 10);
67: if (!*argv || *ep)
68: errx(1, "illegal signal number: %s", *argv);
69: if (numsig >= 128)
70: numsig -= 128;
71: if (numsig <= 0 || numsig >= NSIG)
72: nosig(*argv);
73: printf("%s\n", sys_signame[numsig]);
74: exit(0);
75: }
76: printsignals(stdout);
77: exit(0);
78: }
79:
80: if (!strcmp(*argv, "-s")) {
81: argc--, argv++;
82: if (argc < 1) {
83: warnx("option requires an argument -- s");
84: usage();
85: }
86: if (strcmp(*argv, "0")) {
87: if ((numsig = signame_to_signum(*argv)) < 0)
88: nosig(*argv);
89: } else
90: numsig = 0;
91: argc--, argv++;
92: } else if (**argv == '-') {
93: ++*argv;
94: if (isalpha(**argv)) {
95: if ((numsig = signame_to_signum(*argv)) < 0)
96: nosig(*argv);
97: } else if (isdigit(**argv)) {
98: numsig = strtol(*argv, &ep, 10);
99: if (!*argv || *ep)
100: errx(1, "illegal signal number: %s", *argv);
101: if (numsig <= 0 || numsig >= NSIG)
102: nosig(*argv);
103: } else
104: nosig(*argv);
105: argc--, argv++;
106: }
107:
108: if (argc == 0)
109: usage();
110:
111: for (errors = 0; argc; argc--, argv++) {
112: pid = strtol(*argv, &ep, 10);
113: if (!*argv || *ep) {
114: warnx("illegal process id: %s", *argv);
115: errors = 1;
116: } else if (kill(pid, numsig) == -1) {
117: warn("%s", *argv);
118: errors = 1;
119: }
120: }
121:
122: exit(errors);
123: }
124:
125: static int
126: signame_to_signum(sig)
127: char *sig;
128: {
129: int n;
130:
131: if (!strncasecmp(sig, "sig", 3))
132: sig += 3;
133: for (n = 1; n < NSIG; n++) {
134: if (!strcasecmp(sys_signame[n], sig))
135: return n;
136: }
137: return -1;
138: }
139:
140: static void
141: nosig(name)
142: char *name;
143: {
144:
145: warnx("unknown signal %s; valid signals:", name);
146: printsignals(stderr);
147: exit(1);
148: /* NOTREACHED */
149: }
150:
151: static void
152: printsignals(fp)
153: FILE *fp;
154: {
155: int n;
156:
157: for (n = 1; n < NSIG; n++) {
158: (void)fprintf(fp, "%s", sys_signame[n]);
159: if (n == (NSIG / 2) || n == (NSIG - 1))
160: (void)fprintf(fp, "\n");
161: else
162: (void)fprintf(fp, " ");
163: }
164: }
165:
166: static void
167: usage(void)
168: {
169:
170: fprintf(stderr, "usage: kill [-s signal_name] pid ...\n"
171: " kill -l [exit_status]\n"
172: " kill -signal_name pid ...\n"
173: " kill -signal_number pid ...\n");
174: exit(1);
175: /* NOTREACHED */
176: }
CVSweb