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 $ */
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
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
43 extern char *__progname
;
44 extern char *const sys_signame
[NSIG
];
47 void printsignals(FILE *);
48 int signame_to_signum(char *);
52 main(int argc
, char *argv
[])
54 int errors
, numsig
, pid
;
57 if (pledge("stdio proc", NULL
) == -1)
66 if (!strcmp(*argv
, "-l")) {
68 if (argc
> 0 && !strcmp(*argv
, "--"))
73 if (!isdigit((unsigned char)**argv
))
75 numsig
= strtonum(*argv
, 1, NSIG
+ 127, &errstr
);
79 errx(1, "illegal signal number: %s", *argv
);
81 printf("%s\n", sys_signame
[numsig
& 127]);
88 if (!strcmp(*argv
, "-s")) {
90 if (argc
> 0 && !strcmp(*argv
, "--"))
93 warnx("option requires an argument -- s");
96 if (strcmp(*argv
, "0")) {
97 if ((numsig
= signame_to_signum(*argv
)) < 0)
102 } else if (**argv
== '-') {
103 if (strcmp(*argv
, "--")) {
105 if (isalpha((unsigned char)**argv
)) {
106 if ((numsig
= signame_to_signum(*argv
)) < 0)
108 } else if (isdigit((unsigned char)**argv
)) {
109 numsig
= strtonum(*argv
, 0, NSIG
- 1, &errstr
);
110 if (errstr
!= NULL
) {
113 errx(1, "illegal signal number: %s", *argv
);
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
);
129 } else if (kill(pid
, numsig
) == -1) {
139 signame_to_signum(char *sig
)
143 if (!strncasecmp(sig
, "sig", 3))
145 for (n
= 1; n
< NSIG
; n
++) {
146 if (!strcasecmp(sys_signame
[n
], sig
))
156 warnx("unknown signal %s; valid signals:", name
);
157 printsignals(stderr
);
162 printsignals(FILE *fp
)
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");
171 (void)fprintf(fp
, " ");
178 (void)fprintf(stderr
, "usage: %s [-s signal_name] pid ...\n",
180 (void)fprintf(stderr
, " %s -l [exit_status]\n", __progname
);
181 (void)fprintf(stderr
, " %s -signal_name pid ...\n",
183 (void)fprintf(stderr
, " %s -signal_number pid ...\n",