don't bother resolving onbld python module deps
[unleashed.git] / bin / kill / kill.c
blobf8da90bde07f0e73f955d5bd212bed347b7ec58f
1 /* $OpenBSD: kill.c,v 1.14 2017/03/29 22:40:15 millert Exp $ */
2 /* $NetBSD: kill.c,v 1.11 1995/09/07 06:30:27 jtc Exp $ */
4 /*
5 * Copyright (c) 1988, 1993, 1994
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #include <ctype.h>
34 #include <err.h>
35 #include <errno.h>
36 #include <limits.h>
37 #include <signal.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
43 extern char *__progname;
44 extern char *const sys_signame[NSIG];
46 void nosig(char *);
47 void printsignals(FILE *);
48 int signame_to_signum(char *);
49 void usage(void);
51 int
52 main(int argc, char *argv[])
54 int errors, numsig, pid;
55 const char *errstr;
57 if (pledge("stdio proc", NULL) == -1)
58 err(1, "pledge");
60 if (argc < 2)
61 usage();
63 numsig = SIGTERM;
65 argc--, argv++;
66 if (!strcmp(*argv, "-l")) {
67 argc--, argv++;
68 if (argc > 0 && !strcmp(*argv, "--"))
69 argc--, argv++;
70 if (argc > 1)
71 usage();
72 if (argc == 1) {
73 if (!isdigit((unsigned char)**argv))
74 usage();
75 numsig = strtonum(*argv, 1, NSIG + 127, &errstr);
76 if (errstr != NULL) {
77 if (errno == ERANGE)
78 nosig(*argv);
79 errx(1, "illegal signal number: %s", *argv);
81 printf("%s\n", sys_signame[numsig & 127]);
82 exit(0);
84 printsignals(stdout);
85 exit(0);
88 if (!strcmp(*argv, "-s")) {
89 argc--, argv++;
90 if (argc > 0 && !strcmp(*argv, "--"))
91 argc--, argv++;
92 if (argc < 1) {
93 warnx("option requires an argument -- s");
94 usage();
96 if (strcmp(*argv, "0")) {
97 if ((numsig = signame_to_signum(*argv)) < 0)
98 nosig(*argv);
99 } else
100 numsig = 0;
101 argc--, argv++;
102 } else if (**argv == '-') {
103 if (strcmp(*argv, "--")) {
104 ++*argv;
105 if (isalpha((unsigned char)**argv)) {
106 if ((numsig = signame_to_signum(*argv)) < 0)
107 nosig(*argv);
108 } else if (isdigit((unsigned char)**argv)) {
109 numsig = strtonum(*argv, 0, NSIG - 1, &errstr);
110 if (errstr != NULL) {
111 if (errno == ERANGE)
112 nosig(*argv);
113 errx(1, "illegal signal number: %s", *argv);
115 } else
116 nosig(*argv);
118 argc--, argv++;
121 if (argc == 0)
122 usage();
124 for (errors = 0; argc; argc--, argv++) {
125 pid = strtonum(*argv, -INT_MAX, INT_MAX, &errstr);
126 if (errstr != NULL) {
127 warnx("illegal process id: %s", *argv);
128 errors = 1;
129 } else if (kill(pid, numsig) == -1) {
130 warn("%s", *argv);
131 errors = 1;
135 exit(errors);
139 signame_to_signum(char *sig)
141 int n;
143 if (!strncasecmp(sig, "sig", 3))
144 sig += 3;
145 for (n = 1; n < NSIG; n++) {
146 if (!strcasecmp(sys_signame[n], sig))
147 return (n);
149 return (-1);
152 void
153 nosig(char *name)
156 warnx("unknown signal %s; valid signals:", name);
157 printsignals(stderr);
158 exit(1);
161 void
162 printsignals(FILE *fp)
164 int n;
166 for (n = 1; n < NSIG; n++) {
167 (void)fprintf(fp, "%s", sys_signame[n]);
168 if (n == (NSIG / 2) || n == (NSIG - 1))
169 (void)fprintf(fp, "\n");
170 else
171 (void)fprintf(fp, " ");
175 void
176 usage(void)
178 (void)fprintf(stderr, "usage: %s [-s signal_name] pid ...\n",
179 __progname);
180 (void)fprintf(stderr, " %s -l [exit_status]\n", __progname);
181 (void)fprintf(stderr, " %s -signal_name pid ...\n",
182 __progname);
183 (void)fprintf(stderr, " %s -signal_number pid ...\n",
184 __progname);
185 exit(1);