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