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 (str2sig(*argv, &numsig) < 0) 103 nosig(*argv); 104 argc--, argv++; 105 } 106 107 if (argc > 0 && strncmp(*argv, "--", 2) == 0) 108 argc--, argv++; 109 110 if (argc == 0) 111 usage(); 112 113 for (errors = 0; argc; argc--, argv++) { 114 #ifdef SHELL 115 if (**argv == '%') 116 ret = killjob(*argv, numsig); 117 else 118 #endif 119 { 120 pidl = strtol(*argv, &ep, 10); 121 /* Check for overflow of pid_t. */ 122 pid = (pid_t)pidl; 123 if (!**argv || *ep || pid != pidl) 124 errx(2, "illegal process id: %s", *argv); 125 ret = kill(pid, numsig); 126 } 127 if (ret == -1) { 128 warn("%s", *argv); 129 errors = 1; 130 } 131 } 132 133 return (errors); 134 } 135 136 static void 137 nosig(const char *name) 138 { 139 140 warnx("unknown signal %s; valid signals:", name); 141 printsignals(stderr); 142 #ifdef SHELL 143 error(NULL); 144 #else 145 exit(2); 146 #endif 147 } 148 149 static void 150 printsignals(FILE *fp) 151 { 152 int n; 153 154 for (n = 1; n < sys_nsig; n++) { 155 (void)fprintf(fp, "%s", sys_signame[n]); 156 if (n == (sys_nsig / 2) || n == (sys_nsig - 1)) 157 (void)fprintf(fp, "\n"); 158 else 159 (void)fprintf(fp, " "); 160 } 161 } 162 163 static void 164 usage(void) 165 { 166 167 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n", 168 "usage: kill [-s signal_name] pid ...", 169 " kill -l [exit_status]", 170 " kill -signal_name pid ...", 171 " kill -signal_number pid ..."); 172 #ifdef SHELL 173 error(NULL); 174 #else 175 exit(2); 176 #endif 177 } 178