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