1 /*- 2 * Copyright (c) 2000 Peter Wemm <peter@FreeBSD.org> 3 * Copyright (c) 2000 Paul Saab <ps@FreeBSD.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/cdefs.h> 32 #include <sys/param.h> 33 #include <sys/stat.h> 34 #include <sys/user.h> 35 #include <sys/sysctl.h> 36 #include <fcntl.h> 37 #include <dirent.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <string.h> 41 #include <pwd.h> 42 #include <signal.h> 43 #include <regex.h> 44 #include <ctype.h> 45 #include <err.h> 46 #include <errno.h> 47 #include <unistd.h> 48 49 static char *prog; 50 51 static void __dead2 52 usage(void) 53 { 54 55 fprintf(stderr, "usage: %s [-l] [-v] [-m] [-sig] [-u user] [-t tty] [-c cmd] [cmd]...\n", prog); 56 fprintf(stderr, "At least one option or argument to specify processes must be given.\n"); 57 exit(1); 58 } 59 60 static char * 61 upper(const char *str) 62 { 63 static char buf[80]; 64 char *s; 65 66 strncpy(buf, str, sizeof(buf)); 67 buf[sizeof(buf) - 1] = '\0'; 68 for (s = buf; *s; s++) 69 *s = toupper(*s); 70 return buf; 71 } 72 73 74 static void 75 printsig(FILE *fp) 76 { 77 const char *const * p; 78 int cnt; 79 int offset = 0; 80 81 for (cnt = NSIG, p = sys_signame + 1; --cnt; ++p) { 82 offset += fprintf(fp, "%s ", upper(*p)); 83 if (offset >= 75 && cnt > 1) { 84 offset = 0; 85 fprintf(fp, "\n"); 86 } 87 } 88 fprintf(fp, "\n"); 89 } 90 91 static void 92 nosig(char *name) 93 { 94 95 warnx("unknown signal %s; valid signals:", name); 96 printsig(stderr); 97 exit(1); 98 } 99 100 int 101 main(int ac, char **av) 102 { 103 struct kinfo_proc *procs = NULL, *newprocs; 104 struct stat sb; 105 struct passwd *pw; 106 regex_t rgx; 107 regmatch_t pmatch; 108 int i, j; 109 char buf[256]; 110 char *user = NULL; 111 char *tty = NULL; 112 char *cmd = NULL; 113 int vflag = 0; 114 int sflag = 0; 115 int dflag = 0; 116 int mflag = 0; 117 int zflag = 0; 118 uid_t uid = 0; 119 dev_t tdev = 0; 120 char thiscmd[MAXCOMLEN + 1]; 121 pid_t thispid; 122 uid_t thisuid; 123 dev_t thistdev; 124 int sig = SIGTERM; 125 const char *const *p; 126 char *ep; 127 int errors = 0; 128 int mib[4]; 129 size_t miblen; 130 int st, nprocs; 131 size_t size; 132 int matched; 133 int killed = 0; 134 135 prog = av[0]; 136 av++; 137 ac--; 138 139 while (ac > 0) { 140 if (strcmp(*av, "-l") == 0) { 141 printsig(stdout); 142 exit(0); 143 } 144 if (strcmp(*av, "-help") == 0) 145 usage(); 146 if (**av == '-') { 147 ++*av; 148 switch (**av) { 149 case 'u': 150 ++*av; 151 if (**av == '\0') 152 ++av; 153 --ac; 154 user = *av; 155 break; 156 case 't': 157 ++*av; 158 if (**av == '\0') 159 ++av; 160 --ac; 161 tty = *av; 162 break; 163 case 'c': 164 ++*av; 165 if (**av == '\0') 166 ++av; 167 --ac; 168 cmd = *av; 169 break; 170 case 'v': 171 vflag++; 172 break; 173 case 's': 174 sflag++; 175 break; 176 case 'd': 177 dflag++; 178 break; 179 case 'm': 180 mflag++; 181 break; 182 case 'z': 183 zflag++; 184 break; 185 default: 186 if (isalpha(**av)) { 187 if (strncasecmp(*av, "sig", 3) == 0) 188 *av += 3; 189 for (sig = NSIG, p = sys_signame + 1; 190 --sig; ++p) 191 if (strcasecmp(*p, *av) == 0) { 192 sig = p - sys_signame; 193 break; 194 } 195 if (!sig) 196 nosig(*av); 197 } else if (isdigit(**av)) { 198 sig = strtol(*av, &ep, 10); 199 if (!*av || *ep) 200 errx(1, "illegal signal number: %s", *av); 201 if (sig < 0 || sig > NSIG) 202 nosig(*av); 203 } else 204 nosig(*av); 205 } 206 ++av; 207 --ac; 208 } else { 209 break; 210 } 211 } 212 213 if (user == NULL && tty == NULL && cmd == NULL && ac == 0) 214 usage(); 215 216 if (tty) { 217 if (strncmp(tty, "/dev/", 5) == 0) 218 snprintf(buf, sizeof(buf), "%s", tty); 219 else if (strncmp(tty, "tty", 3) == 0) 220 snprintf(buf, sizeof(buf), "/dev/%s", tty); 221 else 222 snprintf(buf, sizeof(buf), "/dev/tty%s", tty); 223 if (stat(buf, &sb) < 0) 224 err(1, "stat(%s)", buf); 225 if (!S_ISCHR(sb.st_mode)) 226 errx(1, "%s: not a character device", buf); 227 tdev = sb.st_rdev; 228 if (dflag) 229 printf("ttydev:0x%x\n", tdev); 230 } 231 if (user) { 232 uid = strtol(user, &ep, 10); 233 if (*user == '\0' || *ep != '\0') { /* was it a number? */ 234 pw = getpwnam(user); 235 if (pw == NULL) 236 errx(1, "user %s does not exist", user); 237 uid = pw->pw_uid; 238 if (dflag) 239 printf("uid:%d\n", uid); 240 } 241 } else { 242 uid = getuid(); 243 if (uid != 0) { 244 pw = getpwuid(uid); 245 if (pw) 246 user = pw->pw_name; 247 if (dflag) 248 printf("uid:%d\n", uid); 249 } 250 } 251 size = 0; 252 mib[0] = CTL_KERN; 253 mib[1] = KERN_PROC; 254 mib[2] = KERN_PROC_ALL; 255 mib[3] = 0; 256 miblen = 3; 257 258 if (user && mib[2] == KERN_PROC_ALL) { 259 mib[2] = KERN_PROC_RUID; 260 mib[3] = uid; 261 miblen = 4; 262 } 263 if (tty && mib[2] == KERN_PROC_ALL) { 264 mib[2] = KERN_PROC_TTY; 265 mib[3] = tdev; 266 miblen = 4; 267 } 268 269 st = sysctl(mib, miblen, NULL, &size, NULL, 0); 270 do { 271 size += size / 10; 272 newprocs = realloc(procs, size); 273 if (newprocs == 0) { 274 if (procs) 275 free(procs); 276 errx(1, "could not reallocate memory"); 277 } 278 procs = newprocs; 279 st = sysctl(mib, miblen, procs, &size, NULL, 0); 280 } while (st == -1 && errno == ENOMEM); 281 if (st == -1) 282 err(1, "could not sysctl(KERN_PROC)"); 283 if (size % sizeof(struct kinfo_proc) != 0) { 284 fprintf(stderr, "proc size mismatch (%d total, %d chunks)\n", 285 size, sizeof(struct kinfo_proc)); 286 fprintf(stderr, "userland out of sync with kernel, recompile libkvm etc\n"); 287 exit(1); 288 } 289 nprocs = size / sizeof(struct kinfo_proc); 290 if (dflag) 291 printf("nprocs %d\n", nprocs); 292 293 for (i = 0; i < nprocs; i++) { 294 if ((procs[i].ki_stat & SZOMB) == SZOMB && !zflag) 295 continue; 296 thispid = procs[i].ki_pid; 297 strncpy(thiscmd, procs[i].ki_comm, MAXCOMLEN); 298 thiscmd[MAXCOMLEN] = '\0'; 299 thistdev = procs[i].ki_tdev; 300 thisuid = procs[i].ki_ruid; /* real uid */ 301 302 matched = 1; 303 if (user) { 304 if (thisuid != uid) 305 matched = 0; 306 } 307 if (tty) { 308 if (thistdev != tdev) 309 matched = 0; 310 } 311 if (cmd) { 312 if (mflag) { 313 if (regcomp(&rgx, cmd, 314 REG_EXTENDED|REG_NOSUB) != 0) { 315 mflag = 0; 316 warnx("%s: illegal regexp", cmd); 317 } 318 } 319 if (mflag) { 320 pmatch.rm_so = 0; 321 pmatch.rm_eo = strlen(thiscmd); 322 if (regexec(&rgx, thiscmd, 0, &pmatch, 323 REG_STARTEND) != 0) 324 matched = 0; 325 regfree(&rgx); 326 } else { 327 if (strncmp(thiscmd, cmd, MAXCOMLEN) != 0) 328 matched = 0; 329 } 330 } 331 if (matched == 0) 332 continue; 333 if (ac > 0) 334 matched = 0; 335 for (j = 0; j < ac; j++) { 336 if (mflag) { 337 if (regcomp(&rgx, av[j], 338 REG_EXTENDED|REG_NOSUB) != 0) { 339 mflag = 0; 340 warnx("%s: illegal regexp", av[j]); 341 } 342 } 343 if (mflag) { 344 pmatch.rm_so = 0; 345 pmatch.rm_eo = strlen(thiscmd); 346 if (regexec(&rgx, thiscmd, 0, &pmatch, 347 REG_STARTEND) == 0) 348 matched = 1; 349 regfree(&rgx); 350 } else { 351 if (strcmp(thiscmd, av[j]) == 0) 352 matched = 1; 353 } 354 if (matched) 355 break; 356 } 357 if (matched == 0) 358 continue; 359 if (dflag) 360 printf("sig:%d, cmd:%s, pid:%d, dev:0x%x uid:%d\n", sig, 361 thiscmd, thispid, thistdev, thisuid); 362 363 if (vflag || sflag) 364 printf("kill -%s %d\n", upper(sys_signame[sig]), 365 thispid); 366 367 killed++; 368 if (!dflag && !sflag) { 369 if (kill(thispid, sig) < 0 /* && errno != ESRCH */ ) { 370 warn("warning: kill -%s %d", 371 upper(sys_signame[sig]), thispid); 372 errors = 1; 373 } 374 } 375 } 376 if (killed == 0) { 377 fprintf(stderr, "No matching processes %swere found\n", 378 getuid() != 0 ? "belonging to you " : ""); 379 errors = 1; 380 } 381 exit(errors); 382 } 383