182
186
/* Open the /proc directory. */
183
if ((dir = opendir("/proc")) == NULL) {
187
if (chdir("/proc") == -1) {
188
nsyslog(LOG_ERR, "chdir /proc failed");
191
if ((dir = opendir(".")) == NULL) {
184
192
nsyslog(LOG_ERR, "cannot opendir(/proc)");
206
214
memset(p, 0, sizeof(PROC));
208
216
/* Open the status file. */
209
snprintf(path, sizeof(path), "/proc/%s/stat", d->d_name);
217
snprintf(path, sizeof(path), "%s/stat", d->d_name);
211
219
/* Read SID & statname from it. */
212
if ((fp = fopen(path, "r")) != NULL) {
220
if ((fp = fopen(path, "r")) != NULL) {
214
222
fgets(buf, sizeof(buf), fp);
263
snprintf(path, sizeof(path), "/proc/%s/cmdline", d->d_name);
271
snprintf(path, sizeof(path), "%s/cmdline", d->d_name);
264
272
if ((fp = fopen(path, "r")) != NULL) {
266
274
/* Now read argv[0] */
307
315
/* Try to stat the executable. */
308
316
snprintf(path, sizeof(path), "/proc/%s/exe", d->d_name);
309
if (stat(path, &st) == 0) {
317
if (do_stat && stat(path, &st) == 0) {
310
318
p->dev = st.st_dev;
311
319
p->ino = st.st_ino;
378
386
int foundone = 0;
381
/* Try to stat the executable. */
382
if (prog[0] == '/' && stat(prog, &st) == 0) dostat++;
384
392
/* Get basename of program. */
385
393
if ((s = strrchr(prog, '/')) == NULL)
390
401
q = (PIDQ_HEAD *)xmalloc(sizeof(PIDQ_HEAD));
391
402
q = init_pid_q(q);
404
/* Try to stat the executable. */
405
if (prog[0] == '/' && stat(prog, &st) == 0)
393
408
/* First try to find a match based on dev/ino pair. */
395
410
for (p = plist; p; p = p->next) {
404
419
if (!foundone) for (p = plist; p; p = p->next) {
407
/* Compare name (both basename and full path) */
408
ok += (p->argv0 && strcmp(p->argv0, prog) == 0);
409
ok += (p->argv0 && strcmp(p->argv0base, s) == 0);
422
/* matching nonmatching
423
* proc name prog name prog name
424
* --- ----------- ------------
428
* Algorithm: Match if:
433
* Specifically, do not match just because base(cmd) = base(arg)
434
* as was done in earlier versions of this program, since this
435
* allows /aaa/foo to match /bbb/foo .
438
(p->argv0 && strcmp(p->argv0, prog) == 0)
439
|| (p->argv0 && s != prog && strcmp(p->argv0, s) == 0)
440
|| (p->argv0base && strcmp(p->argv0base, prog) == 0);
411
442
/* For scripts, compare argv[1] as well. */
412
if (scripts_too && p->argv1 &&
413
!strncmp(p->statname, p->argv1base, STATNAMELEN)) {
414
ok += (strcmp(p->argv1, prog) == 0);
415
ok += (strcmp(p->argv1base, s) == 0);
444
scripts_too && p->statname && p->argv1base
445
&& !strncmp(p->statname, p->argv1base, STATNAMELEN)
448
(p->argv1 && strcmp(p->argv1, prog) == 0)
449
|| (p->argv1 && s != prog && strcmp(p->argv1, s) == 0)
450
|| (p->argv1base && strcmp(p->argv1base, prog) == 0);
423
458
(p->argv0 == NULL ||
424
459
p->argv0[0] == 0 ||
425
460
strchr(p->argv0, ' '))) {
426
ok += (strcmp(p->statname, s) == 0);
461
ok |= (strcmp(p->statname, s) == 0);
428
463
if (ok) add_pid_to_q(q, p);
478
513
int i, oind, opt, flags = 0;
514
int chroot_check = 0;
480
518
for (oind = PIDOF_OMITSZ-1; oind > 0; oind--)
484
while ((opt = getopt(argc,argv,"ho:sx")) != EOF) switch (opt) {
522
while ((opt = getopt(argc,argv,"hco:sx")) != EOF) switch (opt) {
486
524
nsyslog(LOG_ERR,"invalid options on command line!\n");
528
if (geteuid() == 0) chroot_check = 1;
490
531
if (oind >= PIDOF_OMITSZ -1) {
491
532
nsyslog(LOG_ERR,"omit pid buffer size %d "
562
/* Check if we are in a chroot */
564
snprintf(tmp, 512, "/proc/%d/root", getpid());
565
if (stat(tmp, &st) < 0) {
566
nsyslog(LOG_ERR, "stat failed for %s!\n", tmp);
521
572
/* Print out process-ID's one by one. */
523
574
for(f = 0; f < argc; f++) {
524
575
if ((q = pidof(argv[f])) != NULL) {
597
snprintf(tmp, 512, "/proc/%d/root",
599
if (stat(tmp, &st2) < 0 ||
600
st.st_dev != st2.st_dev ||
601
st.st_ino != st2.st_ino) {
546
607
printf("%d", p->pid);
553
615
return(first ? 1 : 0);
620
#define KILLALL_OMITSZ 16
558
622
/* Main for either killall or pidof. */
559
623
int main(int argc, char **argv)
562
626
int pid, sid = -1;
627
pid_t opid[KILLALL_OMITSZ];
628
int i, oind, omit = 0;
563
629
int sig = SIGKILL;
631
/* return non-zero if no process was killed */
565
634
/* Get program name. */
566
635
if ((progname = strrchr(argv[0], '/')) == NULL)
567
636
progname = argv[0];
576
645
return main_pidof(argc, argv);
578
647
/* Right, so we are "killall". */
648
for (oind = KILLALL_OMITSZ-1; oind > 0; oind--)
580
if (argc != 2) usage();
581
if (argv[1][0] == '-') (argv[1])++;
582
if ((sig = atoi(argv[1])) <= 0 || sig > 31) usage();
652
for (i = 1; i < argc; i++) {
653
if (argv[i][0] == '-') (argv[i])++;
654
if (argv[i][0] == 'o') {
655
if (++i >= argc) usage();
656
if (oind >= KILLALL_OMITSZ -1) {
657
nsyslog(LOG_ERR,"omit pid buffer size "
663
if ((opid[oind] = atoi(argv[i])) < 1) {
665
"illegal omit pid value "
673
else if ((sig = atoi(argv[1])) <= 0 || sig > 31)
585
678
/* First get the /proc filesystem online. */
595
688
signal(SIGSTOP, SIG_IGN);
596
689
signal(SIGKILL, SIG_IGN);
691
/* lock us into memory */
692
mlockall(MCL_CURRENT | MCL_FUTURE);
598
694
/* Now stop all processes. */
599
695
kill(-1, SIGSTOP);
600
696
sent_sigstop = 1;
602
698
/* Read /proc filesystem */
603
if (readproc() < 0) {
699
if (readproc(NO_STAT) < 0) {
604
700
kill(-1, SIGCONT);
608
/* Now kill all processes except our session. */
704
/* Now kill all processes except init (pid 1) and our session. */
609
705
sid = (int)getsid(0);
610
706
pid = (int)getpid();
611
for (p = plist; p; p = p->next)
612
if (p->pid != pid && p->sid != sid && !p->kernel)
707
for (p = plist; p; p = p->next) {
708
if (p->pid == 1 || p->pid == pid || p->sid == sid || p->kernel)
711
for (i = 0; i < oind; i++)
712
if (opid[i] == p->pid)
714
/* On a match, continue with the for loop above. */
615
722
/* And let them continue. */
616
723
kill(-1, SIGCONT);