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