1 /* 2 * Copyright (c) 1988, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char copyright[] = 10 "@(#) Copyright (c) 1988, 1993, 1994\n\ 11 The Regents of the University of California. All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)kill.c 8.4 (Berkeley) 04/28/95"; 16 #endif /* not lint */ 17 18 #include <ctype.h> 19 #include <err.h> 20 #include <errno.h> 21 #include <signal.h> 22 #include <stdio.h> 23 #include <stdlib.h> 24 #include <string.h> 25 26 void nosig __P((char *)); 27 void printsignals __P((FILE *)); 28 int signame_to_signum __P((char *)); 29 void usage __P((void)); 30 31 int 32 main(argc, argv) 33 int argc; 34 char *argv[]; 35 { 36 const char *const *p; 37 int errors, numsig, pid; 38 char *ep; 39 40 if (argc < 2) 41 usage(); 42 43 numsig = SIGTERM; 44 45 argc--, argv++; 46 if (!strcmp(*argv, "-l")) { 47 argc--, argv++; 48 if (argc > 1) 49 usage(); 50 if (argc == 1) { 51 if (!isdigit(**argv)) 52 usage(); 53 numsig = strtol(*argv, &ep, 10); 54 if (!*argv || *ep) 55 errx(1, "illegal signal number: %s", *argv); 56 if (numsig >= 128) 57 numsig -= 128; 58 if (numsig <= 0 || numsig >= NSIG) 59 nosig(*argv); 60 printf("%s\n", sys_signame[numsig]); 61 exit(0); 62 } 63 printsignals(stdout); 64 exit(0); 65 } 66 67 if (!strcmp(*argv, "-s")) { 68 argc--, argv++; 69 if (argc < 1) { 70 warnx("option requires an argument -- s"); 71 usage(); 72 } 73 if (strcmp(*argv, "0")) { 74 if ((numsig = signame_to_signum(*argv)) < 0) 75 nosig(*argv); 76 } else 77 numsig = 0; 78 argc--, argv++; 79 } else if (**argv == '-') { 80 ++*argv; 81 if (isalpha(**argv)) { 82 if ((numsig = signame_to_signum(*argv)) < 0) 83 nosig(*argv); 84 } else if (isdigit(**argv)) { 85 numsig = strtol(*argv, &ep, 10); 86 if (!*argv || *ep) 87 errx(1, "illegal signal number: %s", *argv); 88 if (numsig <= 0 || numsig >= NSIG) 89 nosig(*argv); 90 } else 91 nosig(*argv); 92 argc--, argv++; 93 } 94 95 if (argc == 0) 96 usage(); 97 98 for (errors = 0; argc; argc--, argv++) { 99 pid = strtol(*argv, &ep, 10); 100 if (!*argv || *ep) { 101 warnx("illegal process id: %s", *argv); 102 errors = 1; 103 } else if (kill(pid, numsig) == -1) { 104 warn("%s", *argv); 105 errors = 1; 106 } 107 } 108 109 exit(errors); 110 } 111 112 int 113 signame_to_signum(sig) 114 char *sig; 115 { 116 int n; 117 118 if (!strncasecmp(sig, "sig", 3)) 119 sig += 3; 120 for (n = 1; n < NSIG; n++) { 121 if (!strcasecmp(sys_signame[n], sig)) 122 return (n); 123 } 124 return (-1); 125 } 126 127 void 128 nosig(name) 129 char *name; 130 { 131 132 warnx("unknown signal %s; valid signals:", name); 133 printsignals(stderr); 134 exit(1); 135 } 136 137 void 138 printsignals(fp) 139 FILE *fp; 140 { 141 int n; 142 143 for (n = 1; n < NSIG; n++) { 144 (void)fprintf(fp, "%s", sys_signame[n]); 145 if (n == (NSIG / 2) || n == (NSIG - 1)) 146 (void)fprintf(fp, "\n"); 147 else 148 (void)fprintf(fp, " "); 149 } 150 } 151 152 void 153 usage() 154 { 155 156 (void)fprintf(stderr, "usage: kill [-s signal_name] pid ...\n"); 157 (void)fprintf(stderr, " kill -l [exit_status]\n"); 158 (void)fprintf(stderr, " kill -signal_name pid ...\n"); 159 (void)fprintf(stderr, " kill -signal_number pid ...\n"); 160 exit(1); 161 } 162