kill.c revision 26404
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 * 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 * $Id: kill.c,v 1.6 1997/02/22 14:03:45 peter Exp $ 34 */ 35 36#ifndef lint 37static char const copyright[] = 38"@(#) Copyright (c) 1988, 1993, 1994\n\ 39 The Regents of the University of California. All rights reserved.\n"; 40#endif /* not lint */ 41 42#ifndef lint 43static char const sccsid[] = "@(#)kill.c 8.4 (Berkeley) 4/28/95"; 44#endif /* not lint */ 45 46#include <ctype.h> 47#include <err.h> 48#include <errno.h> 49#include <signal.h> 50#include <stdio.h> 51#include <stdlib.h> 52#include <string.h> 53 54void nosig __P((char *)); 55void printsignals __P((FILE *)); 56int signame_to_signum __P((char *)); 57void usage __P((void)); 58 59int 60main(argc, argv) 61 int argc; 62 char *argv[]; 63{ 64 int errors, numsig, pid; 65 char *ep; 66 67 if (argc < 2) 68 usage(); 69 70 numsig = SIGTERM; 71 72 argc--, argv++; 73 if (!strcmp(*argv, "-l")) { 74 argc--, argv++; 75 if (argc > 1) 76 usage(); 77 if (argc == 1) { 78 if (!isdigit(**argv)) 79 usage(); 80 numsig = strtol(*argv, &ep, 10); 81 if (!*argv || *ep) 82 errx(1, "illegal signal number: %s", *argv); 83 if (numsig >= 128) 84 numsig -= 128; 85 if (numsig <= 0 || numsig >= NSIG) 86 nosig(*argv); 87 printf("%s\n", sys_signame[numsig]); 88 exit(0); 89 } 90 printsignals(stdout); 91 exit(0); 92 } 93 94 if (!strcmp(*argv, "-s")) { 95 argc--, argv++; 96 if (argc < 1) { 97 warnx("option requires an argument -- s"); 98 usage(); 99 } 100 if (strcmp(*argv, "0")) { 101 if ((numsig = signame_to_signum(*argv)) < 0) 102 nosig(*argv); 103 } else 104 numsig = 0; 105 argc--, argv++; 106 } else if (**argv == '-') { 107 ++*argv; 108 if (isalpha(**argv)) { 109 if ((numsig = signame_to_signum(*argv)) < 0) 110 nosig(*argv); 111 } else if (isdigit(**argv)) { 112 numsig = strtol(*argv, &ep, 10); 113 if (!*argv || *ep) 114 errx(1, "illegal signal number: %s", *argv); 115 if (numsig < 0 || numsig >= NSIG) 116 nosig(*argv); 117 } else 118 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