1 /* $NetBSD: pkill.c,v 1.7 2004/02/15 17:03:30 soren Exp $ */ 2 3 /*- 4 * Copyright (c) 2002 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Andrew Doran. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 #ifndef lint 41 __RCSID("$NetBSD: pkill.c,v 1.7 2004/02/15 17:03:30 soren Exp $"); 42 #endif /* !lint */ 43 44 #include <sys/types.h> 45 #include <sys/param.h> 46 #include <sys/sysctl.h> 47 #include <sys/proc.h> 48 #include <sys/queue.h> 49 #include <sys/stat.h> 50 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <limits.h> 54 #include <string.h> 55 #include <unistd.h> 56 #include <signal.h> 57 #include <regex.h> 58 #include <ctype.h> 59 #include <kvm.h> 60 #include <err.h> 61 #include <pwd.h> 62 #include <grp.h> 63 #include <errno.h> 64 65 #define STATUS_MATCH 0 66 #define STATUS_NOMATCH 1 67 #define STATUS_BADUSAGE 2 68 #define STATUS_ERROR 3 69 70 enum listtype { 71 LT_GENERIC, 72 LT_USER, 73 LT_GROUP, 74 LT_TTY, 75 LT_PGRP, 76 LT_SID 77 }; 78 79 struct list { 80 SLIST_ENTRY(list) li_chain; 81 long li_number; 82 }; 83 84 SLIST_HEAD(listhead, list); 85 86 struct kinfo_proc2 *plist; 87 char *selected; 88 char *delim = "\n"; 89 int nproc; 90 int pgrep; 91 int signum = SIGTERM; 92 int newest; 93 int inverse; 94 int longfmt; 95 int matchargs; 96 int fullmatch; 97 kvm_t *kd; 98 pid_t mypid; 99 100 struct listhead euidlist = SLIST_HEAD_INITIALIZER(list); 101 struct listhead ruidlist = SLIST_HEAD_INITIALIZER(list); 102 struct listhead rgidlist = SLIST_HEAD_INITIALIZER(list); 103 struct listhead pgrplist = SLIST_HEAD_INITIALIZER(list); 104 struct listhead ppidlist = SLIST_HEAD_INITIALIZER(list); 105 struct listhead tdevlist = SLIST_HEAD_INITIALIZER(list); 106 struct listhead sidlist = SLIST_HEAD_INITIALIZER(list); 107 108 int main(int, char **); 109 void usage(void); 110 void killact(struct kinfo_proc2 *); 111 void grepact(struct kinfo_proc2 *); 112 void makelist(struct listhead *, enum listtype, char *); 113 114 int 115 main(int argc, char **argv) 116 { 117 extern char *optarg; 118 extern int optind; 119 char buf[_POSIX2_LINE_MAX], *mstr, **pargv, *p, *q; 120 int i, j, ch, bestidx, rv, criteria; 121 void (*action)(struct kinfo_proc2 *); 122 struct kinfo_proc2 *kp; 123 struct list *li; 124 u_int32_t bestsec, bestusec; 125 regex_t reg; 126 regmatch_t regmatch; 127 128 if (strcmp(getprogname(), "pgrep") == 0) { 129 action = grepact; 130 pgrep = 1; 131 } else { 132 action = killact; 133 p = argv[1]; 134 135 if (argc > 1 && p[0] == '-') { 136 p++; 137 i = (int)strtol(p, &q, 10); 138 if (*q == '\0') { 139 signum = i; 140 argv++; 141 argc--; 142 } else { 143 if (strncasecmp(p, "sig", 3) == 0) 144 p += 3; 145 for (i = 1; i < NSIG; i++) 146 if (strcasecmp(sys_signame[i], p) == 0) 147 break; 148 if (i != NSIG) { 149 signum = i; 150 argv++; 151 argc--; 152 } 153 } 154 } 155 } 156 157 criteria = 0; 158 159 while ((ch = getopt(argc, argv, "G:P:U:d:fg:lns:t:u:vx")) != -1) 160 switch (ch) { 161 case 'G': 162 makelist(&rgidlist, LT_GROUP, optarg); 163 criteria = 1; 164 break; 165 case 'P': 166 makelist(&ppidlist, LT_GENERIC, optarg); 167 criteria = 1; 168 break; 169 case 'U': 170 makelist(&ruidlist, LT_USER, optarg); 171 criteria = 1; 172 break; 173 case 'd': 174 if (!pgrep) 175 usage(); 176 delim = optarg; 177 break; 178 case 'f': 179 matchargs = 1; 180 break; 181 case 'g': 182 makelist(&pgrplist, LT_PGRP, optarg); 183 criteria = 1; 184 break; 185 case 'l': 186 if (!pgrep) 187 usage(); 188 longfmt = 1; 189 break; 190 case 'n': 191 newest = 1; 192 criteria = 1; 193 break; 194 case 's': 195 makelist(&sidlist, LT_SID, optarg); 196 criteria = 1; 197 break; 198 case 't': 199 makelist(&tdevlist, LT_TTY, optarg); 200 criteria = 1; 201 break; 202 case 'u': 203 makelist(&euidlist, LT_USER, optarg); 204 criteria = 1; 205 break; 206 case 'v': 207 inverse = 1; 208 break; 209 case 'x': 210 fullmatch = 1; 211 break; 212 default: 213 usage(); 214 /* NOTREACHED */ 215 } 216 217 argc -= optind; 218 argv += optind; 219 if (argc != 0) 220 criteria = 1; 221 if (!criteria) 222 usage(); 223 224 mypid = getpid(); 225 226 /* 227 * Retrieve the list of running processes from the kernel. 228 */ 229 kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, buf); 230 if (kd == NULL) 231 errx(STATUS_ERROR, "kvm_openfiles(): %s", buf); 232 233 plist = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(*plist), &nproc); 234 if (plist == NULL) 235 errx(STATUS_ERROR, "kvm_getproc2() failed"); 236 237 /* 238 * Allocate memory which will be used to keep track of the 239 * selection. 240 */ 241 if ((selected = malloc(nproc)) == NULL) 242 errx(STATUS_ERROR, "memory allocation failure"); 243 memset(selected, 0, nproc); 244 245 /* 246 * Refine the selection. 247 */ 248 for (; *argv != NULL; argv++) { 249 if ((rv = regcomp(®, *argv, REG_EXTENDED)) != 0) { 250 regerror(rv, ®, buf, sizeof(buf)); 251 errx(STATUS_BADUSAGE, "bad expression: %s", buf); 252 } 253 254 for (i = 0, kp = plist; i < nproc; i++, kp++) { 255 if ((kp->p_flag & P_SYSTEM) != 0 || kp->p_pid == mypid) 256 continue; 257 258 if (matchargs) { 259 if ((pargv = kvm_getargv2(kd, kp, 0)) == NULL) 260 continue; 261 262 j = 0; 263 while (j < sizeof(buf) && *pargv != NULL) { 264 j += snprintf(buf + j, sizeof(buf) - j, 265 pargv[1] != NULL ? "%s " : "%s", 266 pargv[0]); 267 pargv++; 268 } 269 270 mstr = buf; 271 } else 272 mstr = kp->p_comm; 273 274 rv = regexec(®, mstr, 1, ®match, 0); 275 if (rv == 0) { 276 if (fullmatch) { 277 if (regmatch.rm_so == 0 && 278 regmatch.rm_eo == strlen(mstr)) 279 selected[i] = 1; 280 } else 281 selected[i] = 1; 282 } else if (rv != REG_NOMATCH) { 283 regerror(rv, ®, buf, sizeof(buf)); 284 errx(STATUS_ERROR, "regexec(): %s", buf); 285 } 286 } 287 288 regfree(®); 289 } 290 291 for (i = 0, kp = plist; i < nproc; i++, kp++) { 292 if ((kp->p_flag & P_SYSTEM) != 0) 293 continue; 294 295 SLIST_FOREACH(li, &ruidlist, li_chain) 296 if (kp->p_ruid == (uid_t)li->li_number) 297 break; 298 if (SLIST_FIRST(&ruidlist) != NULL && li == NULL) { 299 selected[i] = 0; 300 continue; 301 } 302 303 SLIST_FOREACH(li, &rgidlist, li_chain) 304 if (kp->p_rgid == (gid_t)li->li_number) 305 break; 306 if (SLIST_FIRST(&rgidlist) != NULL && li == NULL) { 307 selected[i] = 0; 308 continue; 309 } 310 311 SLIST_FOREACH(li, &euidlist, li_chain) 312 if (kp->p_uid == (uid_t)li->li_number) 313 break; 314 if (SLIST_FIRST(&euidlist) != NULL && li == NULL) { 315 selected[i] = 0; 316 continue; 317 } 318 319 SLIST_FOREACH(li, &ppidlist, li_chain) 320 if (kp->p_ppid == (uid_t)li->li_number) 321 break; 322 if (SLIST_FIRST(&ppidlist) != NULL && li == NULL) { 323 selected[i] = 0; 324 continue; 325 } 326 327 SLIST_FOREACH(li, &pgrplist, li_chain) 328 if (kp->p__pgid == (uid_t)li->li_number) 329 break; 330 if (SLIST_FIRST(&pgrplist) != NULL && li == NULL) { 331 selected[i] = 0; 332 continue; 333 } 334 335 SLIST_FOREACH(li, &tdevlist, li_chain) { 336 if (li->li_number == -1 && 337 (kp->p_flag & P_CONTROLT) == 0) 338 break; 339 if (kp->p_tdev == (uid_t)li->li_number) 340 break; 341 } 342 if (SLIST_FIRST(&tdevlist) != NULL && li == NULL) { 343 selected[i] = 0; 344 continue; 345 } 346 347 SLIST_FOREACH(li, &sidlist, li_chain) 348 if (kp->p_sid == (uid_t)li->li_number) 349 break; 350 if (SLIST_FIRST(&sidlist) != NULL && li == NULL) { 351 selected[i] = 0; 352 continue; 353 } 354 355 if (argc == 0) 356 selected[i] = 1; 357 } 358 359 if (newest) { 360 bestsec = 0; 361 bestusec = 0; 362 bestidx = -1; 363 364 for (i = 0, kp = plist; i < nproc; i++, kp++) { 365 if (!selected[i]) 366 continue; 367 368 if (kp->p_ustart_sec > bestsec || 369 (kp->p_ustart_sec == bestsec 370 && kp->p_ustart_usec > bestusec)) { 371 bestsec = kp->p_ustart_sec; 372 bestusec = kp->p_ustart_usec; 373 bestidx = i; 374 } 375 } 376 377 memset(selected, 0, nproc); 378 if (bestidx != -1) 379 selected[bestidx] = 1; 380 } 381 382 /* 383 * Take the appropriate action for each matched process, if any. 384 */ 385 for (i = 0, rv = 0, kp = plist; i < nproc; i++, kp++) { 386 if (kp->p_pid == mypid) 387 continue; 388 if (selected[i]) { 389 if (inverse) 390 continue; 391 } else if (!inverse) 392 continue; 393 394 if ((kp->p_flag & P_SYSTEM) != 0) 395 continue; 396 397 rv = 1; 398 (*action)(kp); 399 } 400 401 exit(rv ? STATUS_MATCH : STATUS_NOMATCH); 402 } 403 404 void 405 usage(void) 406 { 407 const char *ustr; 408 409 if (pgrep) 410 ustr = "[-flnvx] [-d delim]"; 411 else 412 ustr = "[-signal] [-fnvx]"; 413 414 fprintf(stderr, 415 "usage: %s %s [-G gid] [-P ppid] [-U uid] [-g pgrp] [-s sid]\n" 416 " [-t tty] [-u euid] pattern ...\n", getprogname(), 417 ustr); 418 419 exit(STATUS_ERROR); 420 } 421 422 void 423 killact(struct kinfo_proc2 *kp) 424 { 425 426 if (kill(kp->p_pid, signum) == -1) 427 err(STATUS_ERROR, "signalling pid %d", (int)kp->p_pid); 428 } 429 430 void 431 grepact(struct kinfo_proc2 *kp) 432 { 433 char **argv; 434 435 if (longfmt && matchargs) { 436 if ((argv = kvm_getargv2(kd, kp, 0)) == NULL) 437 return; 438 439 printf("%d ", (int)kp->p_pid); 440 for (; *argv != NULL; argv++) { 441 printf("%s", *argv); 442 if (argv[1] != NULL) 443 putchar(' '); 444 } 445 } else if (longfmt) 446 printf("%d %s", (int)kp->p_pid, kp->p_comm); 447 else 448 printf("%d", (int)kp->p_pid); 449 450 printf("%s", delim); 451 } 452 453 void 454 makelist(struct listhead *head, enum listtype type, char *src) 455 { 456 struct list *li; 457 struct passwd *pw; 458 struct group *gr; 459 struct stat st; 460 char *sp, *p, buf[MAXPATHLEN]; 461 int empty; 462 463 empty = 1; 464 465 while ((sp = strsep(&src, ",")) != NULL) { 466 if (*sp == '\0') 467 usage(); 468 469 if ((li = malloc(sizeof(*li))) == NULL) 470 errx(STATUS_ERROR, "memory allocation failure"); 471 SLIST_INSERT_HEAD(head, li, li_chain); 472 empty = 0; 473 474 li->li_number = (uid_t)strtol(sp, &p, 0); 475 if (*p == '\0') { 476 switch (type) { 477 case LT_PGRP: 478 if (li->li_number == 0) 479 li->li_number = getpgrp(); 480 break; 481 case LT_SID: 482 if (li->li_number == 0) 483 li->li_number = getsid(mypid); 484 break; 485 case LT_TTY: 486 usage(); 487 default: 488 break; 489 } 490 continue; 491 } 492 493 switch (type) { 494 case LT_USER: 495 if ((pw = getpwnam(sp)) == NULL) 496 errx(STATUS_BADUSAGE, "unknown user `%s'", 497 optarg); 498 li->li_number = pw->pw_uid; 499 break; 500 case LT_GROUP: 501 if ((gr = getgrnam(sp)) == NULL) 502 errx(STATUS_BADUSAGE, "unknown group `%s'", 503 optarg); 504 li->li_number = gr->gr_gid; 505 break; 506 case LT_TTY: 507 if (strcmp(sp, "-") == 0) { 508 li->li_number = -1; 509 break; 510 } else if (strcmp(sp, "co") == 0) 511 p = "console"; 512 else if (strncmp(sp, "tty", 3) == 0) 513 p = sp; 514 else 515 p = NULL; 516 517 if (p == NULL) 518 snprintf(buf, sizeof(buf), "/dev/tty%s", sp); 519 else 520 snprintf(buf, sizeof(buf), "/dev/%s", p); 521 522 if (stat(buf, &st) < 0) { 523 if (errno == ENOENT) 524 errx(STATUS_BADUSAGE, 525 "no such tty: `%s'", sp); 526 err(STATUS_ERROR, "stat(%s)", sp); 527 } 528 529 if ((st.st_mode & S_IFCHR) == 0) 530 errx(STATUS_BADUSAGE, "not a tty: `%s'", sp); 531 532 li->li_number = st.st_rdev; 533 break; 534 default: 535 usage(); 536 }; 537 } 538 539 if (empty) 540 usage(); 541 } 542