* Fix counting message lines in wall. Patch from Petr Lautrbach.
[sysvinit.git] / src / killall5.c
1 /*
2 * kilall5.c Kill all processes except processes that have the
3 * same session id, so that the shell that called us
4 * won't be killed. Typically used in shutdown scripts.
5 *
6 * pidof.c Tries to get the pid of the process[es] named.
7 *
8 * Version: 2.86 30-Jul-2004 MvS
9 *
10 * Usage: killall5 [-][signal]
11 * pidof [-s] [-o omitpid [-o omitpid]] program [program..]
12 *
13 * Authors: Miquel van Smoorenburg, miquels@cistron.nl
14 *
15 * Riku Meskanen, <mesrik@jyu.fi>
16 * - return all running pids of given program name
17 * - single shot '-s' option for backwards combatibility
18 * - omit pid '-o' option and %PPID (parent pid metavariable)
19 * - syslog() only if not a connected to controlling terminal
20 * - swapped out programs pids are caught now
21 *
22 * Werner Fink
23 * - make omit dynamic
24 * - provide '-n' to skip stat(2) syscall on network based FS
25 *
26 * This file is part of the sysvinit suite,
27 * Copyright (C) 1991-2004 Miquel van Smoorenburg.
28 *
29 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2 of the License, or
32 * (at your option) any later version.
33 *
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
38 *
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
42 */
43 #include <dirent.h>
44 #include <errno.h>
45 #include <getopt.h>
46 #include <limits.h>
47 #include <mntent.h>
48 #include <stdarg.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <signal.h>
52 #include <string.h>
53 #include <syslog.h>
54 #include <sys/mman.h>
55 #include <sys/param.h>
56 #include <sys/stat.h>
57 #include <sys/types.h>
58 #include <sys/wait.h>
59 #include <unistd.h>
60
61 char *Version = "@(#)killall5 2.86 31-Jul-2004 miquels@cistron.nl";
62
63 #ifndef PATH_MAX
64 # ifdef MAXPATHLEN
65 # define PATH_MAX MAXPATHLEN
66 # else
67 # define PATH_MAX 2048
68 # endif
69 #endif
70
71 #define STATNAMELEN 15
72 #define DO_NETFS 2
73 #define DO_STAT 1
74 #define NO_STAT 0
75
76 /* Info about a process. */
77 typedef struct proc {
78 char *pathname; /* full path to executable */
79 char *argv0; /* Name as found out from argv[0] */
80 char *argv0base; /* `basename argv[1]` */
81 char *argv1; /* Name as found out from argv[1] */
82 char *argv1base; /* `basename argv[1]` */
83 char *statname; /* the statname without braces */
84 ino_t ino; /* Inode number */
85 dev_t dev; /* Device it is on */
86 pid_t pid; /* Process ID. */
87 pid_t sid; /* Session ID. */
88 char kernel; /* Kernel thread or zombie. */
89 char nfs; /* Name found on network FS. */
90 struct proc *next; /* Pointer to next struct. */
91 } PROC;
92
93 /* pid queue */
94
95 typedef struct pidq {
96 PROC *proc;
97 struct pidq *next;
98 } PIDQ;
99
100 typedef struct {
101 PIDQ *head;
102 PIDQ *tail;
103 PIDQ *next;
104 } PIDQ_HEAD;
105
106 typedef struct _s_omit {
107 struct _s_omit *next;
108 struct _s_omit *prev;
109 pid_t pid;
110 } OMIT;
111
112 typedef struct _s_shadow
113 {
114 struct _s_shadow *next;
115 struct _s_shadow *prev;
116 size_t nlen;
117 char * name;
118 } SHADOW;
119
120 typedef struct _s_nfs
121 {
122 struct _s_nfs *next; /* Pointer to next struct. */
123 struct _s_nfs *prev; /* Pointer to previous st. */
124 SHADOW *shadow; /* Pointer to shadows */
125 size_t nlen;
126 char * name;
127 } NFS;
128
129 /* List of processes. */
130 PROC *plist;
131
132 /* List of processes to omit. */
133 OMIT *omit;
134
135 /* List of NFS mountes partitions. */
136 NFS *nlist;
137
138 /* Did we stop all processes ? */
139 int sent_sigstop;
140
141 int scripts_too = 0;
142
143 char *progname; /* the name of the running program */
144 #ifdef __GNUC__
145 __attribute__ ((format (printf, 2, 3)))
146 #endif
147 void nsyslog(int pri, char *fmt, ...);
148
149 #if !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L)
150 # ifndef inline
151 # define inline __inline__
152 # endif
153 # ifndef restrict
154 # define restrict __restrict__
155 # endif
156 #endif
157 #define alignof(type) ((sizeof(type)+(sizeof(void*)-1)) & ~(sizeof(void*)-1))
158
159 /*
160 * Malloc space, barf if out of memory.
161 */
162 #ifdef __GNUC__
163 static void *xmalloc(size_t) __attribute__ ((__malloc__));
164 #endif
165 static void *xmalloc(size_t bytes)
166 {
167 void *p;
168
169 if ((p = malloc(bytes)) == NULL) {
170 if (sent_sigstop) kill(-1, SIGCONT);
171 nsyslog(LOG_ERR, "out of memory");
172 exit(1);
173 }
174 return p;
175 }
176
177 #ifdef __GNUC__
178 static inline void xmemalign(void **, size_t, size_t) __attribute__ ((__nonnull__ (1)));
179 #endif
180 static inline void xmemalign(void **memptr, size_t alignment, size_t size)
181 {
182 if ((posix_memalign(memptr, alignment, size)) < 0) {
183 if (sent_sigstop) kill(-1, SIGCONT);
184 nsyslog(LOG_ERR, "out of memory");
185 exit(1);
186 }
187 }
188
189 /*
190 * See if the proc filesystem is there. Mount if needed.
191 */
192 int mount_proc(void)
193 {
194 struct stat st;
195 char *args[] = { "mount", "-t", "proc", "proc", "/proc", 0 };
196 pid_t pid, rc;
197 int wst;
198 int did_mount = 0;
199
200 /* Stat /proc/version to see if /proc is mounted. */
201 if (stat("/proc/version", &st) < 0 && errno == ENOENT) {
202
203 /* It's not there, so mount it. */
204 if ((pid = fork()) < 0) {
205 nsyslog(LOG_ERR, "cannot fork");
206 exit(1);
207 }
208 if (pid == 0) {
209 /* Try a few mount binaries. */
210 execv("/bin/mount", args);
211 execv("/sbin/mount", args);
212
213 /* Okay, I give up. */
214 nsyslog(LOG_ERR, "cannot execute mount");
215 exit(1);
216 }
217 /* Wait for child. */
218 while ((rc = wait(&wst)) != pid)
219 if (rc < 0 && errno == ECHILD)
220 break;
221 if (rc != pid || WEXITSTATUS(wst) != 0)
222 nsyslog(LOG_ERR, "mount returned non-zero exit status");
223
224 did_mount = 1;
225 }
226
227 /* See if mount succeeded. */
228 if (stat("/proc/version", &st) < 0) {
229 if (errno == ENOENT)
230 nsyslog(LOG_ERR, "/proc not mounted, failed to mount.");
231 else
232 nsyslog(LOG_ERR, "/proc unavailable.");
233 exit(1);
234 }
235
236 return did_mount;
237 }
238
239 static inline int isnetfs(const char * type)
240 {
241 static const char* netfs[] = {"nfs", "nfs4", "smbfs", "cifs", "afs", "ncpfs", (char*)0};
242 int n;
243 for (n = 0; netfs[n]; n++) {
244 if (!strcasecmp(netfs[n], type))
245 return 1;
246 }
247 return 0;
248 }
249
250 /*
251 * Remember all NFS typed partitions.
252 */
253 void init_nfs(void)
254 {
255 struct stat st;
256 struct mntent * ent;
257 FILE * mnt;
258
259 nlist = (NFS*)0;
260
261 if (stat("/proc/version", &st) < 0)
262 return;
263 if ((mnt = setmntent("/proc/mounts", "r")) == (FILE*)0)
264 return;
265
266 while ((ent = getmntent(mnt))) {
267 if (isnetfs(ent->mnt_type)) {
268 size_t nlen = strlen(ent->mnt_dir);
269 NFS *restrict p;
270 xmemalign((void*)&p, sizeof(void*), alignof(NFS)+(nlen+1));
271 p->name = ((char*)p)+alignof(NFS);
272 p->nlen = nlen;
273 p->shadow = (SHADOW*)0;
274
275 strcpy(p->name, ent->mnt_dir);
276 if (nlist)
277 nlist->prev = p;
278 p->next = nlist;
279 p->prev = (NFS*)0;
280 nlist = p;
281 }
282 }
283 endmntent(mnt);
284
285 if ((mnt = setmntent("/proc/mounts", "r")) == (FILE*)0)
286 return;
287
288 while ((ent = getmntent(mnt))) {
289 NFS *p;
290
291 for (p = nlist; p; p = p->next) {
292 SHADOW * restrict s;
293 size_t nlen;
294
295 if (strcmp(ent->mnt_dir, p->name) == 0)
296 continue;
297 if (strncmp(ent->mnt_dir, p->name, p->nlen) != 0)
298 continue;
299
300 nlen = strlen(ent->mnt_dir);
301 xmemalign((void*)&s, sizeof(void*), alignof(SHADOW)+(nlen+1));
302 s->name = ((char*)s)+alignof(SHADOW);
303 s->nlen = nlen;
304
305 strcpy(s->name, ent->mnt_dir);
306 if (p->shadow)
307 p->shadow->prev = s;
308 s->next = p->shadow;
309 s->prev = (SHADOW*)0;
310 p->shadow = s;
311 }
312 }
313 endmntent(mnt);
314 }
315
316 static void clear_shadow(SHADOW *restrict shadow)
317 {
318 SHADOW *s, *n, *l;
319
320 n = shadow;
321 l = (SHADOW*)0;
322 for (s = shadow; n; s = n) {
323 l = s->prev;
324 n = s->next;
325 if (s == shadow) {
326 if (n) n->prev = (SHADOW*)0;
327 shadow = n;
328 } else if (l) {
329 if (n) n->prev = l;
330 l->next = n;
331 }
332 free(s);
333 }
334 }
335
336 static void clear_mnt(void)
337 {
338 NFS *p, *n, *l;
339
340 n = nlist;
341 l = (NFS*)0;
342 for (p = nlist; n; p = n) {
343 l = p->prev;
344 n = p->next;
345 if (p == nlist) {
346 if (n) n->prev = (NFS*)0;
347 nlist = n;
348 } else if (l) {
349 if (n) n->prev = l;
350 l->next = n;
351 }
352 if (p->shadow)
353 clear_shadow(p->shadow);
354 free(p);
355 }
356 }
357
358 /*
359 * Check if path is a shadow off a NFS partition.
360 */
361 static int shadow(SHADOW *restrict this, const char *restrict name, const size_t nlen)
362 {
363 SHADOW *s;
364
365 if (!this)
366 goto out;
367 for (s = this; s; s = s->next) {
368 if (nlen < s->nlen)
369 continue;
370 if (name[s->nlen] != '\0' && name[s->nlen] != '/')
371 continue;
372 if (strncmp(name, s->name, s->nlen) == 0)
373 return 1;
374 }
375 out:
376 return 0;
377 }
378
379 /*
380 * Check path is located on a network based partition.
381 */
382 int check4nfs(const char * path, char * real)
383 {
384 char buf[PATH_MAX+1];
385 const char *curr;
386 int deep = MAXSYMLINKS;
387
388 if (!nlist) return 0;
389
390 curr = path;
391 do {
392 const char *prev;
393 int len;
394
395 if ((prev = strdupa(curr)) == NULL) {
396 nsyslog(LOG_ERR, "strdupa(): %s\n", strerror(errno));
397 return 0;
398 }
399
400 errno = 0;
401 if ((len = readlink(curr, buf, PATH_MAX)) < 0)
402 break;
403 buf[len] = '\0';
404
405 if (buf[0] != '/') {
406 const char *slash;
407
408 if ((slash = strrchr(prev, '/'))) {
409 size_t off = slash - prev + 1;
410
411 if (off + len > PATH_MAX)
412 len = PATH_MAX - off;
413
414 memmove(&buf[off], &buf[0], len + 1);
415 memcpy(&buf[0], prev, off);
416 }
417 }
418 curr = &buf[0];
419
420 if (deep-- <= 0) return 0;
421
422 } while (1);
423
424 if (real) strcpy(real, curr);
425
426 if (errno == EINVAL) {
427 const size_t nlen = strlen(curr);
428 NFS *p;
429 for (p = nlist; p; p = p->next) {
430 if (nlen < p->nlen)
431 continue;
432 if (curr[p->nlen] != '\0' && curr[p->nlen] != '/')
433 continue;
434 if (!strncmp(curr, p->name, p->nlen)) {
435 if (shadow(p->shadow, curr, nlen))
436 continue;
437 return 1;
438 }
439 }
440 }
441
442 return 0;
443 }
444
445 int readarg(FILE *fp, char *buf, int sz)
446 {
447 int c = 0, f = 0;
448
449 while (f < (sz-1) && (c = fgetc(fp)) != EOF && c)
450 buf[f++] = c;
451 buf[f] = 0;
452
453 return (c == EOF && f == 0) ? c : f;
454 }
455
456 /*
457 * Read the proc filesystem.
458 * CWD must be /proc to avoid problems if / is affected by the killing (ie depend on fuse).
459 */
460 int readproc(int do_stat)
461 {
462 DIR *dir;
463 FILE *fp;
464 PROC *p, *n;
465 struct dirent *d;
466 struct stat st;
467 char path[PATH_MAX+1];
468 char buf[PATH_MAX+1];
469 char *s, *q;
470 unsigned long startcode, endcode;
471 int pid, f;
472
473 /* Open the /proc directory. */
474 if (chdir("/proc") == -1) {
475 nsyslog(LOG_ERR, "chdir /proc failed");
476 return -1;
477 }
478 if ((dir = opendir(".")) == NULL) {
479 nsyslog(LOG_ERR, "cannot opendir(/proc)");
480 return -1;
481 }
482
483 /* Free the already existing process list. */
484 n = plist;
485 for (p = plist; n; p = n) {
486 n = p->next;
487 if (p->argv0) free(p->argv0);
488 if (p->argv1) free(p->argv1);
489 if (p->statname) free(p->statname);
490 free(p->pathname);
491 free(p);
492 }
493 plist = NULL;
494
495 /* Walk through the directory. */
496 while ((d = readdir(dir)) != NULL) {
497
498 /* See if this is a process */
499 if ((pid = atoi(d->d_name)) == 0) continue;
500
501 /* Get a PROC struct . */
502 p = (PROC *)xmalloc(sizeof(PROC));
503 memset(p, 0, sizeof(PROC));
504
505 /* Open the status file. */
506 snprintf(path, sizeof(path), "%s/stat", d->d_name);
507
508 /* Read SID & statname from it. */
509 if ((fp = fopen(path, "r")) != NULL) {
510 if (!fgets(buf, sizeof(buf), fp))
511 buf[0] = '\0';
512
513 if (buf[0] == '\0') {
514 nsyslog(LOG_ERR,
515 "can't read from %s\n", path);
516 fclose(fp);
517 free(p);
518 continue;
519 }
520
521 /* See if name starts with '(' */
522 s = buf;
523 while (*s && *s != ' ') s++;
524 if (*s) s++;
525 if (*s == '(') {
526 /* Read program name. */
527 q = strrchr(buf, ')');
528 if (q == NULL) {
529 p->sid = 0;
530 nsyslog(LOG_ERR,
531 "can't get program name from /proc/%s\n",
532 path);
533 fclose(fp);
534 if (p->argv0) free(p->argv0);
535 if (p->argv1) free(p->argv1);
536 if (p->statname) free(p->statname);
537 free(p->pathname);
538 free(p);
539 continue;
540 }
541 s++;
542 } else {
543 q = s;
544 while (*q && *q != ' ') q++;
545 }
546 if (*q) *q++ = 0;
547 while (*q == ' ') q++;
548 p->statname = (char *)xmalloc(strlen(s)+1);
549 strcpy(p->statname, s);
550
551 /* Get session, startcode, endcode. */
552 startcode = endcode = 0;
553 if (sscanf(q, "%*c %*d %*d %d %*d %*d %*u %*u "
554 "%*u %*u %*u %*u %*u %*d %*d "
555 "%*d %*d %*d %*d %*u %*u %*d "
556 "%*u %lu %lu",
557 &p->sid, &startcode, &endcode) != 3) {
558 p->sid = 0;
559 nsyslog(LOG_ERR, "can't read sid from %s\n",
560 path);
561 fclose(fp);
562 if (p->argv0) free(p->argv0);
563 if (p->argv1) free(p->argv1);
564 if (p->statname) free(p->statname);
565 free(p->pathname);
566 free(p);
567 continue;
568 }
569 if (startcode == 0 && endcode == 0)
570 p->kernel = 1;
571 fclose(fp);
572 } else {
573 /* Process disappeared.. */
574 if (p->argv0) free(p->argv0);
575 if (p->argv1) free(p->argv1);
576 if (p->statname) free(p->statname);
577 free(p->pathname);
578 free(p);
579 continue;
580 }
581
582 snprintf(path, sizeof(path), "%s/cmdline", d->d_name);
583 if ((fp = fopen(path, "r")) != NULL) {
584
585 /* Now read argv[0] */
586 f = readarg(fp, buf, sizeof(buf));
587
588 if (buf[0]) {
589 /* Store the name into malloced memory. */
590 p->argv0 = (char *)xmalloc(f + 1);
591 strcpy(p->argv0, buf);
592
593 /* Get a pointer to the basename. */
594 p->argv0base = strrchr(p->argv0, '/');
595 if (p->argv0base != NULL)
596 p->argv0base++;
597 else
598 p->argv0base = p->argv0;
599 }
600
601 /* And read argv[1] */
602 while ((f = readarg(fp, buf, sizeof(buf))) != EOF)
603 if (buf[0] != '-') break;
604
605 if (buf[0]) {
606 /* Store the name into malloced memory. */
607 p->argv1 = (char *)xmalloc(f + 1);
608 strcpy(p->argv1, buf);
609
610 /* Get a pointer to the basename. */
611 p->argv1base = strrchr(p->argv1, '/');
612 if (p->argv1base != NULL)
613 p->argv1base++;
614 else
615 p->argv1base = p->argv1;
616 }
617
618 fclose(fp);
619
620 } else {
621 /* Process disappeared.. */
622 if (p->argv0) free(p->argv0);
623 if (p->argv1) free(p->argv1);
624 if (p->statname) free(p->statname);
625 free(p->pathname);
626 free(p);
627 continue;
628 }
629
630 /* Try to stat the executable. */
631 snprintf(path, sizeof(path), "/proc/%s/exe", d->d_name);
632
633 p->nfs = 0;
634
635 switch (do_stat) {
636 case DO_NETFS:
637 if ((p->nfs = check4nfs(path, buf)))
638 break;
639 case DO_STAT:
640 if (stat(path, &st) != 0)
641 break;
642 else {
643 char buf[PATH_MAX];
644
645 f = readlink(path, buf, sizeof buf);
646 if (f > 0) {
647 p->pathname = (char *)xmalloc(f + 1);
648 memcpy(p->pathname, buf, f);
649 p->pathname[f] = '\0';
650 }
651 }
652 p->dev = st.st_dev;
653 p->ino = st.st_ino;
654 default:
655 break;
656 }
657
658 /* Link it into the list. */
659 p->next = plist;
660 plist = p;
661 p->pid = pid;
662 }
663 closedir(dir);
664
665 /* Done. */
666 return 0;
667 }
668
669 PIDQ_HEAD *init_pid_q(PIDQ_HEAD *q)
670 {
671 q->head = q->next = q->tail = NULL;
672 return q;
673 }
674
675 int empty_q(PIDQ_HEAD *q)
676 {
677 return (q->head == NULL);
678 }
679
680 int add_pid_to_q(PIDQ_HEAD *q, PROC *p)
681 {
682 PIDQ *tmp;
683
684 tmp = (PIDQ *)xmalloc(sizeof(PIDQ));
685
686 tmp->proc = p;
687 tmp->next = NULL;
688
689 if (empty_q(q)) {
690 q->head = tmp;
691 q->tail = tmp;
692 } else {
693 q->tail->next = tmp;
694 q->tail = tmp;
695 }
696 return 0;
697 }
698
699 PROC *get_next_from_pid_q(PIDQ_HEAD *q)
700 {
701 PROC *p;
702 PIDQ *tmp = q->head;
703
704 if (!empty_q(q)) {
705 p = q->head->proc;
706 q->head = tmp->next;
707 free(tmp);
708 return p;
709 }
710
711 return NULL;
712 }
713
714 /* Try to get the process ID of a given process. */
715 PIDQ_HEAD *pidof(char *prog)
716 {
717 PROC *p;
718 PIDQ_HEAD *q;
719 struct stat st;
720 char *s;
721 int nfs = 0;
722 int dostat = 0;
723 int foundone = 0;
724 int ok = 0;
725 char real[PATH_MAX+1];
726
727 if (! prog)
728 return NULL;
729
730 /* Try to stat the executable. */
731 if (prog[0] == '/') {
732 memset(&real[0], 0, sizeof(real));
733
734 if (check4nfs(prog, real))
735 nfs++;
736
737 if (real[0] != '\0')
738 prog = &real[0]; /* Binary located on network FS. */
739
740 if ((nfs == 0) && (stat(prog, &st) == 0))
741 dostat++; /* Binary located on a local FS. */
742 }
743
744 /* Get basename of program. */
745 if ((s = strrchr(prog, '/')) == NULL)
746 s = prog;
747 else
748 s++;
749
750 if (! *s)
751 return NULL;
752
753 q = (PIDQ_HEAD *)xmalloc(sizeof(PIDQ_HEAD));
754 q = init_pid_q(q);
755
756 /* First try to find a match based on dev/ino pair. */
757 if (dostat && !nfs) {
758 for (p = plist; p; p = p->next) {
759 if (p->nfs)
760 continue;
761 if (p->dev == st.st_dev && p->ino == st.st_ino) {
762 add_pid_to_q(q, p);
763 foundone++;
764 }
765 }
766 }
767
768 /* Second try to find a match based on full path name on
769 * network FS located binaries */
770 if (!foundone && nfs) {
771 for (p = plist; p; p = p->next) {
772 char exe [PATH_MAX+1];
773 char path[PATH_MAX+1];
774 int len;
775 if (!p->nfs)
776 continue;
777 snprintf(exe, sizeof(exe), "/proc/%d/exe", p->pid);
778 if ((len = readlink(exe, path, PATH_MAX)) < 0)
779 continue;
780 path[len] = '\0';
781 if (strcmp(prog, path) != 0)
782 continue;
783 add_pid_to_q(q, p);
784 foundone++;
785 }
786 }
787
788 /* If we didn't find a match based on dev/ino, try the name. */
789 if (!foundone) for (p = plist; p; p = p->next) {
790 if (prog[0] == '/') {
791 if (!p->pathname)
792 continue;
793 if (strcmp(prog, p->pathname)) {
794 int len = strlen(prog);
795 if (strncmp(prog, p->pathname, len))
796 continue;
797 if (strcmp(" (deleted)", p->pathname + len))
798 continue;
799 }
800 add_pid_to_q(q, p);
801 continue;
802 }
803
804 ok = 0;
805
806 /* matching nonmatching
807 * proc name prog name prog name
808 * --- ----------- ------------
809 * b b, p/b, q/b
810 * p/b b, p/b q/b
811 *
812 * Algorithm: Match if:
813 * cmd = arg
814 * or cmd = base(arg)
815 * or base(cmd) = arg
816 *
817 * Specifically, do not match just because base(cmd) = base(arg)
818 * as was done in earlier versions of this program, since this
819 * allows /aaa/foo to match /bbb/foo .
820 */
821 ok |=
822 (p->argv0 && strcmp(p->argv0, prog) == 0)
823 || (p->argv0 && s != prog && strcmp(p->argv0, s) == 0)
824 || (p->argv0base && strcmp(p->argv0base, prog) == 0);
825
826 /* For scripts, compare argv[1] as well. */
827 if (
828 scripts_too && p->statname && p->argv1base
829 && !strncmp(p->statname, p->argv1base, STATNAMELEN)
830 ) {
831 ok |=
832 (p->argv1 && strcmp(p->argv1, prog) == 0)
833 || (p->argv1 && s != prog && strcmp(p->argv1, s) == 0)
834 || (p->argv1base && strcmp(p->argv1base, prog) == 0);
835 }
836
837 /*
838 * if we have a space in argv0, process probably
839 * used setproctitle so try statname.
840 */
841 if (strlen(s) <= STATNAMELEN &&
842 (p->argv0 == NULL ||
843 p->argv0[0] == 0 ||
844 strchr(p->argv0, ' '))) {
845 ok |= (strcmp(p->statname, s) == 0);
846 }
847
848 /*
849 * if we have a `-' as the first character, process
850 * probably used as a login shell
851 */
852 if (strlen(s) <= STATNAMELEN &&
853 p->argv1 == NULL &&
854 (p->argv0 != NULL &&
855 p->argv0[0] == '-')) {
856 ok |= (strcmp(p->statname, s) == 0);
857 }
858
859 if (ok) add_pid_to_q(q, p);
860 }
861
862 return q;
863 }
864
865 /* Give usage message and exit. */
866 void usage(void)
867 {
868 nsyslog(LOG_ERR, "only one argument, a signal number, allowed");
869 closelog();
870 exit(1);
871 }
872
873 /* write to syslog file if not open terminal */
874 #ifdef __GNUC__
875 __attribute__ ((format (printf, 2, 3)))
876 #endif
877 void nsyslog(int pri, char *fmt, ...)
878 {
879 va_list args;
880
881 va_start(args, fmt);
882
883 if (ttyname(0) == NULL) {
884 vsyslog(pri, fmt, args);
885 } else {
886 fprintf(stderr, "%s: ",progname);
887 vfprintf(stderr, fmt, args);
888 fprintf(stderr, "\n");
889 }
890
891 va_end(args);
892 }
893
894 #define PIDOF_SINGLE 0x01
895 #define PIDOF_OMIT 0x02
896 #define PIDOF_NETFS 0x04
897
898 /*
899 * Pidof functionality.
900 */
901 int main_pidof(int argc, char **argv)
902 {
903 PIDQ_HEAD *q;
904 PROC *p;
905 char *token, *here;
906 int f;
907 int first = 1;
908 int opt, flags = 0;
909 int chroot_check = 0;
910 struct stat st;
911 char tmp[512];
912
913 omit = (OMIT*)0;
914 nlist = (NFS*)0;
915 opterr = 0;
916
917 if ((token = getenv("PIDOF_NETFS")) && (strcmp(token,"no") != 0))
918 flags |= PIDOF_NETFS;
919
920 while ((opt = getopt(argc,argv,"hco:sxn")) != EOF) switch (opt) {
921 case '?':
922 nsyslog(LOG_ERR,"invalid options on command line!\n");
923 closelog();
924 exit(1);
925 case 'c':
926 if (geteuid() == 0) chroot_check = 1;
927 break;
928 case 'o':
929 here = optarg;
930 while ((token = strsep(&here, ",;:"))) {
931 OMIT *restrict optr;
932 pid_t opid;
933
934 if (strcmp("%PPID", token) == 0)
935 opid = getppid();
936 else
937 opid = (pid_t)atoi(token);
938
939 if (opid < 1) {
940 nsyslog(LOG_ERR,
941 "illegal omit pid value "
942 "(%s)!\n", token);
943 continue;
944 }
945 xmemalign((void*)&optr, sizeof(void*), alignof(OMIT));
946 optr->next = omit;
947 optr->prev = (OMIT*)0;
948 optr->pid = opid;
949 omit = optr;
950 }
951 flags |= PIDOF_OMIT;
952 break;
953 case 's':
954 flags |= PIDOF_SINGLE;
955 break;
956 case 'x':
957 scripts_too++;
958 break;
959 case 'n':
960 flags |= PIDOF_NETFS;
961 break;
962 default:
963 /* Nothing */
964 break;
965 }
966 argc -= optind;
967 argv += optind;
968
969 /* Check if we are in a chroot */
970 if (chroot_check) {
971 snprintf(tmp, 512, "/proc/%d/root", getpid());
972 if (stat(tmp, &st) < 0) {
973 nsyslog(LOG_ERR, "stat failed for %s!\n", tmp);
974 closelog();
975 exit(1);
976 }
977 }
978
979 if (flags & PIDOF_NETFS)
980 init_nfs(); /* Which network based FS are online? */
981
982 /* Print out process-ID's one by one. */
983 readproc((flags & PIDOF_NETFS) ? DO_NETFS : DO_STAT);
984
985 for(f = 0; f < argc; f++) {
986 if ((q = pidof(argv[f])) != NULL) {
987 pid_t spid = 0;
988 while ((p = get_next_from_pid_q(q))) {
989 if ((flags & PIDOF_OMIT) && omit) {
990 OMIT * optr;
991 for (optr = omit; optr; optr = optr->next) {
992 if (optr->pid == p->pid)
993 break;
994 }
995
996 /*
997 * On a match, continue with
998 * the for loop above.
999 */
1000 if (optr)
1001 continue;
1002 }
1003 if (flags & PIDOF_SINGLE) {
1004 if (spid)
1005 continue;
1006 else
1007 spid = 1;
1008 }
1009 if (chroot_check) {
1010 struct stat st2;
1011 snprintf(tmp, 512, "/proc/%d/root",
1012 p->pid);
1013 if (stat(tmp, &st2) < 0 ||
1014 st.st_dev != st2.st_dev ||
1015 st.st_ino != st2.st_ino) {
1016 continue;
1017 }
1018 }
1019 if (!first)
1020 printf(" ");
1021 printf("%d", p->pid);
1022 first = 0;
1023 }
1024 }
1025 }
1026 if (!first)
1027 printf("\n");
1028
1029 clear_mnt();
1030
1031 closelog();
1032 return(first ? 1 : 0);
1033 }
1034
1035 /* Main for either killall or pidof. */
1036 int main(int argc, char **argv)
1037 {
1038 PROC *p;
1039 int pid, sid = -1;
1040 int sig = SIGKILL;
1041 int c;
1042
1043 /* return non-zero if no process was killed */
1044 int retval = 2;
1045
1046 /* Get program name. */
1047 if ((progname = strrchr(argv[0], '/')) == NULL)
1048 progname = argv[0];
1049 else
1050 progname++;
1051
1052 /* Now connect to syslog. */
1053 openlog(progname, LOG_CONS|LOG_PID, LOG_DAEMON);
1054
1055 /* Were we called as 'pidof' ? */
1056 if (strcmp(progname, "pidof") == 0)
1057 return main_pidof(argc, argv);
1058
1059 /* Right, so we are "killall". */
1060 omit = (OMIT*)0;
1061
1062 if (argc > 1) {
1063 for (c = 1; c < argc; c++) {
1064 if (argv[c][0] == '-') (argv[c])++;
1065 if (argv[c][0] == 'o') {
1066 char * token, * here;
1067
1068 if (++c >= argc)
1069 usage();
1070
1071 here = argv[c];
1072 while ((token = strsep(&here, ",;:"))) {
1073 OMIT *restrict optr;
1074 pid_t opid = (pid_t)atoi(token);
1075
1076 if (opid < 1) {
1077 nsyslog(LOG_ERR,
1078 "illegal omit pid value "
1079 "(%s)!\n", token);
1080 continue;
1081 }
1082 xmemalign((void*)&optr, sizeof(void*), alignof(OMIT));
1083 optr->next = omit;
1084 optr->prev = (OMIT*)0;
1085 optr->pid = opid;
1086 omit = optr;
1087 }
1088 }
1089 else if ((sig = atoi(argv[1])) <= 0 || sig > 31)
1090 usage();
1091 }
1092 }
1093
1094 /* First get the /proc filesystem online. */
1095 mount_proc();
1096
1097 /*
1098 * Ignoring SIGKILL and SIGSTOP do not make sense, but
1099 * someday kill(-1, sig) might kill ourself if we don't
1100 * do this. This certainly is a valid concern for SIGTERM-
1101 * Linux 2.1 might send the calling process the signal too.
1102 */
1103 signal(SIGTERM, SIG_IGN);
1104 signal(SIGSTOP, SIG_IGN);
1105 signal(SIGKILL, SIG_IGN);
1106
1107 /* lock us into memory */
1108 mlockall(MCL_CURRENT | MCL_FUTURE);
1109
1110 /* Now stop all processes. */
1111 kill(-1, SIGSTOP);
1112 sent_sigstop = 1;
1113
1114 /* Read /proc filesystem */
1115 if (readproc(NO_STAT) < 0) {
1116 kill(-1, SIGCONT);
1117 return(1);
1118 }
1119
1120 /* Now kill all processes except init (pid 1) and our session. */
1121 sid = (int)getsid(0);
1122 pid = (int)getpid();
1123 for (p = plist; p; p = p->next) {
1124 if (p->pid == 1 || p->pid == pid || p->sid == sid || p->kernel)
1125 continue;
1126
1127 if (omit) {
1128 OMIT * optr;
1129 for (optr = omit; optr; optr = optr->next) {
1130 if (optr->pid == p->pid)
1131 break;
1132 }
1133
1134 /* On a match, continue with the for loop above. */
1135 if (optr)
1136 continue;
1137 }
1138
1139 kill(p->pid, sig);
1140 retval = 0;
1141 }
1142
1143 /* And let them continue. */
1144 kill(-1, SIGCONT);
1145
1146 /* Done. */
1147 closelog();
1148
1149 /* Force the kernel to run the scheduler */
1150 usleep(1);
1151
1152 return retval;
1153 }