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