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