1 /* $NetBSD: pkill.c,v 1.16 2005/10/10 22:13:20 kleink Exp $ */ 2 3 /*- 4 * Copyright (c) 2002 The NetBSD Foundation, Inc. 5 * Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org> 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Andrew Doran. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 * 32 * $FreeBSD: head/bin/pkill/pkill.c 256050 2013-10-04 16:08:44Z trasz $ 33 */ 34 35 #include <sys/user.h> 36 #include <sys/types.h> 37 #include <sys/param.h> 38 #include <sys/sysctl.h> 39 #include <sys/queue.h> 40 #include <sys/stat.h> 41 #include <sys/fcntl.h> 42 #include <sys/time.h> 43 44 #include <assert.h> 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <limits.h> 48 #include <paths.h> 49 #include <string.h> 50 #include <unistd.h> 51 #include <signal.h> 52 #include <regex.h> 53 #include <ctype.h> 54 #include <kvm.h> 55 #include <err.h> 56 #include <pwd.h> 57 #include <grp.h> 58 #include <errno.h> 59 #include <locale.h> 60 61 #define STATUS_MATCH 0 62 #define STATUS_NOMATCH 1 63 #define STATUS_BADUSAGE 2 64 #define STATUS_ERROR 3 65 66 #define MIN_PID 5 67 #define MAX_PID 99999 68 69 /* Ignore system-processes (if '-S' flag is not specified) and myself. */ 70 #define PSKIP(kp) ((kp)->kp_pid == mypid || \ 71 (!kthreads && ((kp)->kp_flags & P_KTHREADP) != 0)) 72 73 enum listtype { 74 LT_GENERIC, 75 LT_USER, 76 LT_GROUP, 77 LT_TTY, 78 LT_PGRP, 79 LT_JID, 80 LT_SID, 81 LT_CLASS 82 }; 83 84 struct list { 85 SLIST_ENTRY(list) li_chain; 86 long li_number; 87 char *li_name; 88 }; 89 90 SLIST_HEAD(listhead, list); 91 92 static struct kinfo_proc *plist; 93 static char *selected; 94 static const char *delim = "\n"; 95 static int nproc; 96 static int pgrep; 97 static int signum = SIGTERM; 98 static int newest; 99 static int oldest; 100 static int interactive; 101 static int inverse; 102 static int longfmt; 103 static int matchargs; 104 static int fullmatch; 105 static int kthreads; 106 static int cflags = REG_EXTENDED; 107 static int quiet; 108 static kvm_t *kd; 109 static pid_t mypid; 110 111 static struct listhead euidlist = SLIST_HEAD_INITIALIZER(euidlist); 112 static struct listhead ruidlist = SLIST_HEAD_INITIALIZER(ruidlist); 113 static struct listhead rgidlist = SLIST_HEAD_INITIALIZER(rgidlist); 114 static struct listhead pgrplist = SLIST_HEAD_INITIALIZER(pgrplist); 115 static struct listhead ppidlist = SLIST_HEAD_INITIALIZER(ppidlist); 116 static struct listhead tdevlist = SLIST_HEAD_INITIALIZER(tdevlist); 117 static struct listhead sidlist = SLIST_HEAD_INITIALIZER(sidlist); 118 static struct listhead jidlist = SLIST_HEAD_INITIALIZER(jidlist); 119 static struct listhead classlist = SLIST_HEAD_INITIALIZER(classlist); 120 121 static void usage(void) __attribute__((__noreturn__)); 122 static int killact(const struct kinfo_proc *); 123 static int grepact(const struct kinfo_proc *); 124 static void makelist(struct listhead *, enum listtype, char *); 125 static int takepid(const char *, int); 126 127 int 128 main(int argc, char **argv) 129 { 130 char buf[_POSIX2_LINE_MAX], *mstr, **pargv, *p, *q, *pidfile; 131 const char *execf, *coref; 132 int ancestors, debug_opt, did_action; 133 int i, ch, bestidx, rv, criteria, pidfromfile, pidfilelock; 134 size_t jsz; 135 int (*action)(const struct kinfo_proc *); 136 struct kinfo_proc *kp; 137 struct list *li; 138 struct timeval best_tval; 139 regex_t reg; 140 regmatch_t regmatch; 141 pid_t pid; 142 143 setlocale(LC_ALL, ""); 144 145 if (strcmp(getprogname(), "pgrep") == 0) { 146 action = grepact; 147 pgrep = 1; 148 } else { 149 action = killact; 150 p = argv[1]; 151 152 if (argc > 1 && p[0] == '-') { 153 p++; 154 i = (int)strtol(p, &q, 10); 155 if (*q == '\0') { 156 signum = i; 157 argv++; 158 argc--; 159 } else { 160 if (strncasecmp(p, "SIG", 3) == 0) 161 p += 3; 162 for (i = 1; i < NSIG; i++) 163 if (strcasecmp(sys_signame[i], p) == 0) 164 break; 165 if (i != NSIG) { 166 signum = i; 167 argv++; 168 argc--; 169 } 170 } 171 } 172 } 173 174 ancestors = 0; 175 criteria = 0; 176 debug_opt = 0; 177 pidfile = NULL; 178 pidfilelock = 0; 179 quiet = 0; 180 execf = NULL; 181 coref = _PATH_DEVNULL; 182 183 while ((ch = getopt(argc, argv, "DF:G:ILM:N:P:SU:ac:d:fg:ij:lnoqs:t:u:vx")) != -1) 184 switch (ch) { 185 case 'D': 186 debug_opt++; 187 break; 188 case 'F': 189 pidfile = optarg; 190 criteria = 1; 191 break; 192 case 'G': 193 makelist(&rgidlist, LT_GROUP, optarg); 194 criteria = 1; 195 break; 196 case 'I': 197 if (pgrep) 198 usage(); 199 interactive = 1; 200 break; 201 case 'L': 202 pidfilelock = 1; 203 break; 204 case 'M': 205 coref = optarg; 206 break; 207 case 'N': 208 execf = optarg; 209 break; 210 case 'P': 211 makelist(&ppidlist, LT_GENERIC, optarg); 212 criteria = 1; 213 break; 214 case 'S': 215 if (!pgrep) 216 usage(); 217 kthreads = 1; 218 break; 219 case 'U': 220 makelist(&ruidlist, LT_USER, optarg); 221 criteria = 1; 222 break; 223 case 'a': 224 ancestors++; 225 break; 226 case 'c': 227 makelist(&classlist, LT_CLASS, optarg); 228 criteria = 1; 229 break; 230 case 'd': 231 if (!pgrep) 232 usage(); 233 delim = optarg; 234 break; 235 case 'f': 236 matchargs = 1; 237 break; 238 case 'g': 239 makelist(&pgrplist, LT_PGRP, optarg); 240 criteria = 1; 241 break; 242 case 'i': 243 cflags |= REG_ICASE; 244 break; 245 case 'j': 246 makelist(&jidlist, LT_JID, optarg); 247 criteria = 1; 248 break; 249 case 'l': 250 longfmt = 1; 251 break; 252 case 'n': 253 newest = 1; 254 criteria = 1; 255 break; 256 case 'o': 257 oldest = 1; 258 criteria = 1; 259 break; 260 case 'q': 261 if (!pgrep) 262 usage(); 263 quiet = 1; 264 break; 265 case 's': 266 makelist(&sidlist, LT_SID, optarg); 267 criteria = 1; 268 break; 269 case 't': 270 makelist(&tdevlist, LT_TTY, optarg); 271 criteria = 1; 272 break; 273 case 'u': 274 makelist(&euidlist, LT_USER, optarg); 275 criteria = 1; 276 break; 277 case 'v': 278 inverse = 1; 279 break; 280 case 'x': 281 fullmatch = 1; 282 break; 283 default: 284 usage(); 285 /* NOTREACHED */ 286 } 287 288 argc -= optind; 289 argv += optind; 290 if (argc != 0) 291 criteria = 1; 292 if (!criteria) 293 usage(); 294 if (newest && oldest) 295 errx(STATUS_ERROR, "Options -n and -o are mutually exclusive"); 296 if (pidfile != NULL) 297 pidfromfile = takepid(pidfile, pidfilelock); 298 else { 299 if (pidfilelock) { 300 errx(STATUS_ERROR, 301 "Option -L doesn't make sense without -F"); 302 } 303 pidfromfile = -1; 304 } 305 306 mypid = getpid(); 307 308 /* 309 * Retrieve the list of running processes from the kernel. 310 */ 311 kd = kvm_openfiles(execf, coref, NULL, O_RDONLY, buf); 312 if (kd == NULL) 313 errx(STATUS_ERROR, "Cannot open kernel files (%s)", buf); 314 315 plist = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc); 316 if (plist == NULL) { 317 errx(STATUS_ERROR, "Cannot get process list (%s)", 318 kvm_geterr(kd)); 319 } 320 321 /* 322 * Allocate memory which will be used to keep track of the 323 * selection. 324 */ 325 if ((selected = malloc(nproc)) == NULL) { 326 err(STATUS_ERROR, "Cannot allocate memory for %d processes", 327 nproc); 328 } 329 memset(selected, 0, nproc); 330 331 /* 332 * Refine the selection. 333 */ 334 for (; *argv != NULL; argv++) { 335 if ((rv = regcomp(®, *argv, cflags)) != 0) { 336 regerror(rv, ®, buf, sizeof(buf)); 337 errx(STATUS_BADUSAGE, 338 "Cannot compile regular expression `%s' (%s)", 339 *argv, buf); 340 } 341 342 for (i = 0, kp = plist; i < nproc; i++, kp++) { 343 if (PSKIP(kp)) { 344 if (debug_opt > 0) 345 fprintf(stderr, "* Skipped %5d %3d %s\n", 346 kp->kp_pid, kp->kp_uid, kp->kp_comm); 347 continue; 348 } 349 350 if (matchargs && 351 (pargv = kvm_getargv(kd, kp, 0)) != NULL) { 352 jsz = 0; 353 while (jsz < sizeof(buf) && *pargv != NULL) { 354 jsz += snprintf(buf + jsz, 355 sizeof(buf) - jsz, 356 pargv[1] != NULL ? "%s " : "%s", 357 pargv[0]); 358 pargv++; 359 } 360 mstr = buf; 361 } else 362 mstr = kp->kp_comm; 363 364 rv = regexec(®, mstr, 1, ®match, 0); 365 if (rv == 0) { 366 if (fullmatch) { 367 if (regmatch.rm_so == 0 && 368 regmatch.rm_eo == 369 (off_t)strlen(mstr)) 370 selected[i] = 1; 371 } else 372 selected[i] = 1; 373 } else if (rv != REG_NOMATCH) { 374 regerror(rv, ®, buf, sizeof(buf)); 375 errx(STATUS_ERROR, 376 "Regular expression evaluation error (%s)", 377 buf); 378 } 379 if (debug_opt > 1) { 380 const char *rv_res = "NoMatch"; 381 if (selected[i]) 382 rv_res = "Matched"; 383 fprintf(stderr, "* %s %5d %3d %s\n", rv_res, 384 kp->kp_pid, kp->kp_uid, mstr); 385 } 386 } 387 388 regfree(®); 389 } 390 391 for (i = 0, kp = plist; i < nproc; i++, kp++) { 392 if (PSKIP(kp)) 393 continue; 394 395 if (pidfromfile >= 0 && kp->kp_pid != pidfromfile) { 396 selected[i] = 0; 397 continue; 398 } 399 400 SLIST_FOREACH(li, &ruidlist, li_chain) 401 if (kp->kp_ruid == (uid_t)li->li_number) 402 break; 403 if (SLIST_FIRST(&ruidlist) != NULL && li == NULL) { 404 selected[i] = 0; 405 continue; 406 } 407 408 SLIST_FOREACH(li, &rgidlist, li_chain) 409 if (kp->kp_rgid == (gid_t)li->li_number) 410 break; 411 if (SLIST_FIRST(&rgidlist) != NULL && li == NULL) { 412 selected[i] = 0; 413 continue; 414 } 415 416 SLIST_FOREACH(li, &euidlist, li_chain) 417 if (kp->kp_uid == (uid_t)li->li_number) 418 break; 419 if (SLIST_FIRST(&euidlist) != NULL && li == NULL) { 420 selected[i] = 0; 421 continue; 422 } 423 424 SLIST_FOREACH(li, &ppidlist, li_chain) 425 if (kp->kp_ppid == (pid_t)li->li_number) 426 break; 427 if (SLIST_FIRST(&ppidlist) != NULL && li == NULL) { 428 selected[i] = 0; 429 continue; 430 } 431 432 SLIST_FOREACH(li, &pgrplist, li_chain) 433 if (kp->kp_pgid == (pid_t)li->li_number) 434 break; 435 if (SLIST_FIRST(&pgrplist) != NULL && li == NULL) { 436 selected[i] = 0; 437 continue; 438 } 439 440 SLIST_FOREACH(li, &tdevlist, li_chain) { 441 if (li->li_number == -1 && 442 (kp->kp_flags & P_CONTROLT) == 0) 443 break; 444 if (kp->kp_tdev == (dev_t)li->li_number) 445 break; 446 } 447 if (SLIST_FIRST(&tdevlist) != NULL && li == NULL) { 448 selected[i] = 0; 449 continue; 450 } 451 452 SLIST_FOREACH(li, &sidlist, li_chain) 453 if (kp->kp_sid == (pid_t)li->li_number) 454 break; 455 if (SLIST_FIRST(&sidlist) != NULL && li == NULL) { 456 selected[i] = 0; 457 continue; 458 } 459 460 SLIST_FOREACH(li, &jidlist, li_chain) { 461 /* A particular jail ID, including 0 (not in jail) */ 462 if (kp->kp_jailid == (int)li->li_number) 463 break; 464 /* Any jail */ 465 if (kp->kp_jailid > 0 && li->li_number == -1) 466 break; 467 } 468 if (SLIST_FIRST(&jidlist) != NULL && li == NULL) { 469 selected[i] = 0; 470 continue; 471 } 472 473 SLIST_FOREACH(li, &classlist, li_chain) { 474 /* 475 * We skip P_SYSTEM processes to match ps(1) output. 476 */ 477 if ((kp->kp_flags & P_SYSTEM) == 0) 478 break; 479 } 480 if (SLIST_FIRST(&classlist) != NULL && li == NULL) { 481 selected[i] = 0; 482 continue; 483 } 484 485 if (argc == 0) 486 selected[i] = 1; 487 } 488 489 if (!ancestors) { 490 pid = mypid; 491 while (pid) { 492 for (i = 0, kp = plist; i < nproc; i++, kp++) { 493 if (PSKIP(kp)) 494 continue; 495 if (kp->kp_pid == pid) { 496 selected[i] = 0; 497 pid = kp->kp_ppid; 498 break; 499 } 500 } 501 if (i == nproc) { 502 if (pid == mypid) 503 pid = getppid(); 504 else 505 break; /* Maybe we're in a jail ? */ 506 } 507 } 508 } 509 510 if (newest || oldest) { 511 best_tval.tv_sec = 0; 512 best_tval.tv_usec = 0; 513 bestidx = -1; 514 515 for (i = 0, kp = plist; i < nproc; i++, kp++) { 516 if (!selected[i]) 517 continue; 518 if (bestidx == -1) { 519 /* The first entry of the list which matched. */ 520 ; 521 } else if (timercmp(&kp->kp_start, &best_tval, >)) { 522 /* This entry is newer than previous "best". */ 523 if (oldest) /* but we want the oldest */ 524 continue; 525 } else { 526 /* This entry is older than previous "best". */ 527 if (newest) /* but we want the newest */ 528 continue; 529 } 530 /* This entry is better than previous "best" entry. */ 531 best_tval.tv_sec = kp->kp_start.tv_sec; 532 best_tval.tv_usec = kp->kp_start.tv_usec; 533 bestidx = i; 534 } 535 536 memset(selected, 0, nproc); 537 if (bestidx != -1) 538 selected[bestidx] = 1; 539 } 540 541 /* 542 * Take the appropriate action for each matched process, if any. 543 */ 544 did_action = 0; 545 for (i = 0, rv = 0, kp = plist; i < nproc; i++, kp++) { 546 if (PSKIP(kp)) 547 continue; 548 if (selected[i]) { 549 if (longfmt && !pgrep) { 550 did_action = 1; 551 printf("kill -%d %d\n", signum, kp->kp_pid); 552 } 553 if (inverse) 554 continue; 555 } else if (!inverse) 556 continue; 557 rv |= (*action)(kp); 558 } 559 if (!did_action && !pgrep && longfmt) 560 fprintf(stderr, 561 "No matching processes belonging to you were found\n"); 562 563 exit(rv ? STATUS_MATCH : STATUS_NOMATCH); 564 } 565 566 static void 567 usage(void) 568 { 569 const char *ustr; 570 571 if (pgrep) 572 ustr = "[-LSfilnoqvx] [-d delim]"; 573 else 574 ustr = "[-signal] [-ILfilnovx]"; 575 576 fprintf(stderr, 577 "usage: %s %s [-F pidfile] [-G gid] [-M core] [-N system]\n" 578 " [-P ppid] [-U uid] [-c class] [-g pgrp] [-j jid]\n" 579 " [-s sid] [-t tty] [-u euid] pattern ...\n", 580 getprogname(), ustr); 581 582 exit(STATUS_BADUSAGE); 583 } 584 585 static void 586 show_process(const struct kinfo_proc *kp) 587 { 588 char **argv; 589 590 if (quiet) { 591 assert(pgrep); 592 return; 593 } 594 if ((longfmt || !pgrep) && matchargs && 595 (argv = kvm_getargv(kd, kp, 0)) != NULL) { 596 printf("%d ", (int)kp->kp_pid); 597 for (; *argv != NULL; argv++) { 598 printf("%s", *argv); 599 if (argv[1] != NULL) 600 putchar(' '); 601 } 602 } else if (longfmt || !pgrep) 603 printf("%d %s", (int)kp->kp_pid, kp->kp_comm); 604 else 605 printf("%d", (int)kp->kp_pid); 606 } 607 608 static int 609 killact(const struct kinfo_proc *kp) 610 { 611 int ch, first; 612 613 if (interactive) { 614 /* 615 * Be careful, ask before killing. 616 */ 617 printf("kill "); 618 show_process(kp); 619 printf("? "); 620 fflush(stdout); 621 first = ch = getchar(); 622 while (ch != '\n' && ch != EOF) 623 ch = getchar(); 624 if (first != 'y' && first != 'Y') 625 return (1); 626 } 627 if (kill(kp->kp_pid, signum) == -1) { 628 /* 629 * Check for ESRCH, which indicates that the process 630 * disappeared between us matching it and us 631 * signalling it; don't issue a warning about it. 632 */ 633 if (errno != ESRCH) 634 warn("signalling pid %d", (int)kp->kp_pid); 635 /* 636 * Return 0 to indicate that the process should not be 637 * considered a match, since we didn't actually get to 638 * signal it. 639 */ 640 return (0); 641 } 642 return (1); 643 } 644 645 static int 646 grepact(const struct kinfo_proc *kp) 647 { 648 649 show_process(kp); 650 if (!quiet) 651 printf("%s", delim); 652 return (1); 653 } 654 655 static void 656 makelist(struct listhead *head, enum listtype type, char *src) 657 { 658 struct list *li; 659 struct passwd *pw; 660 struct group *gr; 661 struct stat st; 662 const char *cp; 663 char *sp, *ep, buf[MAXPATHLEN]; 664 int empty; 665 666 empty = 1; 667 668 while ((sp = strsep(&src, ",")) != NULL) { 669 if (*sp == '\0') 670 usage(); 671 672 if ((li = malloc(sizeof(*li))) == NULL) { 673 err(STATUS_ERROR, "Cannot allocate %zu bytes", 674 sizeof(*li)); 675 } 676 677 SLIST_INSERT_HEAD(head, li, li_chain); 678 empty = 0; 679 680 if (type != LT_CLASS) 681 li->li_number = (uid_t)strtol(sp, &ep, 0); 682 683 if (type != LT_CLASS && *ep == '\0') { 684 switch (type) { 685 case LT_PGRP: 686 if (li->li_number == 0) 687 li->li_number = getpgrp(); 688 break; 689 case LT_SID: 690 if (li->li_number == 0) 691 li->li_number = getsid(mypid); 692 break; 693 case LT_JID: 694 if (li->li_number < 0) 695 errx(STATUS_BADUSAGE, 696 "Negative jail ID `%s'", sp); 697 /* For compatibility with old -j */ 698 if (li->li_number == 0) 699 li->li_number = -1; /* any jail */ 700 break; 701 case LT_TTY: 702 if (li->li_number < 0) 703 errx(STATUS_BADUSAGE, 704 "Negative /dev/pts tty `%s'", sp); 705 snprintf(buf, sizeof(buf), _PATH_DEV "pts/%s", 706 sp); 707 if (stat(buf, &st) != -1) 708 goto foundtty; 709 if (errno == ENOENT) 710 errx(STATUS_BADUSAGE, "No such tty: `" 711 _PATH_DEV "pts/%s'", sp); 712 err(STATUS_ERROR, "Cannot access `" 713 _PATH_DEV "pts/%s'", sp); 714 break; 715 default: 716 break; 717 } 718 continue; 719 } 720 721 switch (type) { 722 case LT_USER: 723 if ((pw = getpwnam(sp)) == NULL) 724 errx(STATUS_BADUSAGE, "Unknown user `%s'", sp); 725 li->li_number = pw->pw_uid; 726 break; 727 case LT_GROUP: 728 if ((gr = getgrnam(sp)) == NULL) 729 errx(STATUS_BADUSAGE, "Unknown group `%s'", sp); 730 li->li_number = gr->gr_gid; 731 break; 732 case LT_TTY: 733 if (strcmp(sp, "-") == 0) { 734 li->li_number = -1; 735 break; 736 } else if (strcmp(sp, "co") == 0) { 737 cp = "console"; 738 } else { 739 cp = sp; 740 } 741 742 snprintf(buf, sizeof(buf), _PATH_DEV "%s", cp); 743 if (stat(buf, &st) != -1) 744 goto foundtty; 745 746 snprintf(buf, sizeof(buf), _PATH_DEV "tty%s", cp); 747 if (stat(buf, &st) != -1) 748 goto foundtty; 749 750 if (errno == ENOENT) 751 errx(STATUS_BADUSAGE, "No such tty: `%s'", sp); 752 err(STATUS_ERROR, "Cannot access `%s'", sp); 753 754 foundtty: if ((st.st_mode & S_IFCHR) == 0) 755 errx(STATUS_BADUSAGE, "Not a tty: `%s'", sp); 756 757 li->li_number = st.st_rdev; 758 break; 759 case LT_JID: 760 if (strcmp(sp, "none") == 0) 761 li->li_number = 0; 762 else if (strcmp(sp, "any") == 0) 763 li->li_number = -1; 764 else if (*ep != '\0') 765 errx(STATUS_BADUSAGE, 766 "Invalid jail ID `%s'", sp); 767 break; 768 case LT_CLASS: 769 li->li_number = -1; 770 li->li_name = strdup(sp); 771 if (li->li_name == NULL) 772 err(STATUS_ERROR, "Cannot allocate memory"); 773 break; 774 default: 775 usage(); 776 } 777 } 778 779 if (empty) 780 usage(); 781 } 782 783 static int 784 takepid(const char *pidfile, int pidfilelock) 785 { 786 char *endp, line[BUFSIZ]; 787 FILE *fh; 788 long rval; 789 790 fh = fopen(pidfile, "r"); 791 if (fh == NULL) 792 err(STATUS_ERROR, "Cannot open pidfile `%s'", pidfile); 793 794 if (pidfilelock) { 795 /* 796 * If we can lock pidfile, this means that daemon is not 797 * running, so would be better not to kill some random process. 798 */ 799 if (flock(fileno(fh), LOCK_EX | LOCK_NB) == 0) { 800 (void)fclose(fh); 801 errx(STATUS_ERROR, "File '%s' can be locked", pidfile); 802 } else { 803 if (errno != EWOULDBLOCK) { 804 errx(STATUS_ERROR, 805 "Error while locking file '%s'", pidfile); 806 } 807 } 808 } 809 810 if (fgets(line, sizeof(line), fh) == NULL) { 811 if (feof(fh)) { 812 (void)fclose(fh); 813 errx(STATUS_ERROR, "Pidfile `%s' is empty", pidfile); 814 } 815 (void)fclose(fh); 816 err(STATUS_ERROR, "Cannot read from pid file `%s'", pidfile); 817 } 818 (void)fclose(fh); 819 820 rval = strtol(line, &endp, 10); 821 if (*endp != '\0' && !isspace((unsigned char)*endp)) 822 errx(STATUS_ERROR, "Invalid pid in file `%s'", pidfile); 823 else if (rval < MIN_PID || rval > MAX_PID) 824 errx(STATUS_ERROR, "Invalid pid in file `%s'", pidfile); 825 return (rval); 826 } 827