kill.c revision 22988
1155191Srwatson/* 2181053Srwatson * Copyright (c) 1988, 1993, 1994 3155191Srwatson * The Regents of the University of California. All rights reserved. 4155191Srwatson * 5155191Srwatson * Redistribution and use in source and binary forms, with or without 6155191Srwatson * modification, are permitted provided that the following conditions 7155191Srwatson * are met: 8155191Srwatson * 1. Redistributions of source code must retain the above copyright 9155191Srwatson * notice, this list of conditions and the following disclaimer. 10155191Srwatson * 2. Redistributions in binary form must reproduce the above copyright 11155191Srwatson * notice, this list of conditions and the following disclaimer in the 12155191Srwatson * documentation and/or other materials provided with the distribution. 13155191Srwatson * 3. All advertising materials mentioning features or use of this software 14155191Srwatson * must display the following acknowledgement: 15155191Srwatson * This product includes software developed by the University of 16155191Srwatson * California, Berkeley and its contributors. 17155191Srwatson * 4. Neither the name of the University nor the names of its contributors 18155191Srwatson * may be used to endorse or promote products derived from this software 19155191Srwatson * without specific prior written permission. 20155191Srwatson * 21155191Srwatson * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22155191Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23155191Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24155191Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25155191Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26155191Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27155191Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28155191Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29155191Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30155191Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31155191Srwatson * SUCH DAMAGE. 32155191Srwatson * 33171542Srwatson * $Id$ 34155191Srwatson */ 35155191Srwatson 36155191Srwatson#ifndef lint 37156289Srwatsonstatic char const copyright[] = 38156289Srwatson"@(#) Copyright (c) 1988, 1993, 1994\n\ 39155191Srwatson The Regents of the University of California. All rights reserved.\n"; 40156289Srwatson#endif /* not lint */ 41156289Srwatson 42156289Srwatson#ifndef lint 43156289Srwatsonstatic char const sccsid[] = "@(#)kill.c 8.4 (Berkeley) 4/28/95"; 44155191Srwatson#endif /* not lint */ 45155191Srwatson 46155191Srwatson#include <ctype.h> 47155191Srwatson#include <err.h> 48155191Srwatson#include <errno.h> 49155191Srwatson#include <signal.h> 50155191Srwatson#include <stdio.h> 51156289Srwatson#include <stdlib.h> 52156289Srwatson#include <string.h> 53156289Srwatson 54156289Srwatsonvoid nosig __P((char *)); 55156289Srwatsonvoid printsignals __P((FILE *)); 56155191Srwatsonint signame_to_signum __P((char *)); 57156289Srwatsonvoid usage __P((void)); 58156289Srwatson 59156289Srwatsonint 60156289Srwatsonmain(argc, argv) 61156289Srwatson int argc; 62156289Srwatson char *argv[]; 63156289Srwatson{ 64156289Srwatson int errors, numsig, pid; 65156289Srwatson char *ep; 66156289Srwatson 67156289Srwatson if (argc < 2) 68161635Srwatson usage(); 69161635Srwatson 70161870Srwatson numsig = SIGTERM; 71161635Srwatson 72161870Srwatson argc--, argv++; 73161635Srwatson if (!strcmp(*argv, "-l")) { 74155191Srwatson argc--, argv++; 75184856Scsjp if (argc > 1) 76161635Srwatson usage(); 77184856Scsjp if (argc == 1) { 78161635Srwatson if (!isdigit(**argv)) 79155191Srwatson usage(); 80155191Srwatson numsig = strtol(*argv, &ep, 10); 81155191Srwatson if (!*argv || *ep) 82155191Srwatson errx(1, "illegal signal number: %s", *argv); 83155191Srwatson if (numsig >= 128) 84155191Srwatson numsig -= 128; 85155191Srwatson if (numsig <= 0 || numsig >= NSIG) 86155191Srwatson nosig(*argv); 87155191Srwatson printf("%s\n", sys_signame[numsig]); 88155191Srwatson exit(0); 89155191Srwatson } 90155191Srwatson printsignals(stdout); 91155191Srwatson exit(0); 92155191Srwatson } 93155191Srwatson 94155191Srwatson if (!strcmp(*argv, "-s")) { 95155191Srwatson argc--, argv++; 96155191Srwatson if (argc < 1) { 97155191Srwatson warnx("option requires an argument -- s"); 98155191Srwatson usage(); 99155191Srwatson } 100155191Srwatson if (strcmp(*argv, "0")) { 101155191Srwatson if ((numsig = signame_to_signum(*argv)) < 0) 102155191Srwatson nosig(*argv); 103155191Srwatson } else 104155191Srwatson numsig = 0; 105155191Srwatson argc--, argv++; 106155191Srwatson } else if (**argv == '-') { 107155191Srwatson ++*argv; 108155191Srwatson if (isalpha(**argv)) { 109155191Srwatson if ((numsig = signame_to_signum(*argv)) < 0) 110155191Srwatson nosig(*argv); 111155191Srwatson } else if (isdigit(**argv)) { 112155191Srwatson numsig = strtol(*argv, &ep, 10); 113155191Srwatson if (!*argv || *ep) 114155191Srwatson errx(1, "illegal signal number: %s", *argv); 115155191Srwatson if (numsig < 0 || numsig > NSIG) 116155191Srwatson nosig(*argv); 117155191Srwatson } else 118156289Srwatson nosig(*argv); 119 argc--, argv++; 120 } 121 122 if (argc == 0) 123 usage(); 124 125 for (errors = 0; argc; argc--, argv++) { 126 pid = strtol(*argv, &ep, 10); 127 if (!*argv || *ep) { 128 warnx("illegal process id: %s", *argv); 129 errors = 1; 130 } else if (kill(pid, numsig) == -1) { 131 warn("%s", *argv); 132 errors = 1; 133 } 134 } 135 136 exit(errors); 137} 138 139int 140signame_to_signum(sig) 141 char *sig; 142{ 143 int n; 144 145 if (!strncasecmp(sig, "sig", 3)) 146 sig += 3; 147 for (n = 1; n < NSIG; n++) { 148 if (!strcasecmp(sys_signame[n], sig)) 149 return (n); 150 } 151 return (-1); 152} 153 154void 155nosig(name) 156 char *name; 157{ 158 159 warnx("unknown signal %s; valid signals:", name); 160 printsignals(stderr); 161 exit(1); 162} 163 164void 165printsignals(fp) 166 FILE *fp; 167{ 168 int n; 169 170 for (n = 1; n < NSIG; n++) { 171 (void)fprintf(fp, "%s", sys_signame[n]); 172 if (n == (NSIG / 2) || n == (NSIG - 1)) 173 (void)fprintf(fp, "\n"); 174 else 175 (void)fprintf(fp, " "); 176 } 177} 178 179void 180usage() 181{ 182 183 (void)fprintf(stderr, "usage: kill [-s signal_name] pid ...\n"); 184 (void)fprintf(stderr, " kill -l [exit_status]\n"); 185 (void)fprintf(stderr, " kill -signal_name pid ...\n"); 186 (void)fprintf(stderr, " kill -signal_number pid ...\n"); 187 exit(1); 188} 189