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