]>
git.wh0rd.org - sysvinit.git/blob - src/killall5.c
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.
6 * pidof.c Tries to get the pid of the process[es] named.
8 * Version: 2.86 30-Jul-2004 MvS
10 * Usage: killall5 [-][signal]
11 * pidof [-s] [-o omitpid [-o omitpid]] program [program..]
13 * Authors: Miquel van Smoorenburg, miquels@cistron.nl
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
24 * - provide '-n' to skip stat(2) syscall on network based FS
26 * This file is part of the sysvinit suite,
27 * Copyright (C) 1991-2004 Miquel van Smoorenburg.
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.
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.
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
55 #include <sys/param.h>
57 #include <sys/types.h>
61 char *Version
= "@(#)killall5 2.86 31-Jul-2004 miquels@cistron.nl";
65 # define PATH_MAX MAXPATHLEN
67 # define PATH_MAX 2048
71 #define STATNAMELEN 15
76 /* Info about a process. */
78 char *argv0
; /* Name as found out from argv[0] */
79 char *argv0base
; /* `basename argv[1]` */
80 char *argv1
; /* Name as found out from argv[1] */
81 char *argv1base
; /* `basename argv[1]` */
82 char *statname
; /* the statname without braces */
83 ino_t ino
; /* Inode number */
84 dev_t dev
; /* Device it is on */
85 pid_t pid
; /* Process ID. */
86 pid_t sid
; /* Session ID. */
87 char kernel
; /* Kernel thread or zombie. */
88 char nfs
; /* Name found on network FS. */
89 struct proc
*next
; /* Pointer to next struct. */
105 typedef struct _s_omit
{
106 struct _s_omit
*next
;
107 struct _s_omit
*prev
;
111 typedef struct _s_shadow
113 struct _s_shadow
*next
;
114 struct _s_shadow
*prev
;
119 typedef struct _s_nfs
121 struct _s_nfs
*next
; /* Pointer to next struct. */
122 struct _s_nfs
*prev
; /* Pointer to previous st. */
123 SHADOW
*shadow
; /* Pointer to shadows */
128 /* List of processes. */
131 /* List of processes to omit. */
134 /* List of NFS mountes partitions. */
137 /* Did we stop all processes ? */
142 char *progname
; /* the name of the running program */
144 __attribute__ ((format (printf
, 2, 3)))
146 void nsyslog(int pri
, char *fmt
, ...);
148 #if !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L)
150 # define inline __inline__
153 # define restrict __restrict__
156 #define alignof(type) ((sizeof(type)+(sizeof(void*)-1)) & ~(sizeof(void*)-1))
159 * Malloc space, barf if out of memory.
162 static void *xmalloc(size_t) __attribute__ ((__malloc__
));
164 static void *xmalloc(size_t bytes
)
168 if ((p
= malloc(bytes
)) == NULL
) {
169 if (sent_sigstop
) kill(-1, SIGCONT
);
170 nsyslog(LOG_ERR
, "out of memory");
177 static inline void xmemalign(void **, size_t, size_t) __attribute__ ((__nonnull__ (1)));
179 static inline void xmemalign(void **memptr
, size_t alignment
, size_t size
)
181 if ((posix_memalign(memptr
, alignment
, size
)) < 0) {
182 if (sent_sigstop
) kill(-1, SIGCONT
);
183 nsyslog(LOG_ERR
, "out of memory");
189 * See if the proc filesystem is there. Mount if needed.
194 char *args
[] = { "mount", "-t", "proc", "proc", "/proc", 0 };
199 /* Stat /proc/version to see if /proc is mounted. */
200 if (stat("/proc/version", &st
) < 0 && errno
== ENOENT
) {
202 /* It's not there, so mount it. */
203 if ((pid
= fork()) < 0) {
204 nsyslog(LOG_ERR
, "cannot fork");
208 /* Try a few mount binaries. */
209 execv("/sbin/mount", args
);
210 execv("/bin/mount", args
);
212 /* Okay, I give up. */
213 nsyslog(LOG_ERR
, "cannot execute mount");
216 /* Wait for child. */
217 while ((rc
= wait(&wst
)) != pid
)
218 if (rc
< 0 && errno
== ECHILD
)
220 if (rc
!= pid
|| WEXITSTATUS(wst
) != 0)
221 nsyslog(LOG_ERR
, "mount returned non-zero exit status");
226 /* See if mount succeeded. */
227 if (stat("/proc/version", &st
) < 0) {
229 nsyslog(LOG_ERR
, "/proc not mounted, failed to mount.");
231 nsyslog(LOG_ERR
, "/proc unavailable.");
238 static inline int isnetfs(const char * type
)
240 static const char* netfs
[] = {"nfs", "nfs4", "smbfs", "cifs", "afs", "ncpfs", (char*)0};
242 for (n
= 0; netfs
[n
]; n
++) {
243 if (!strcasecmp(netfs
[n
], type
))
250 * Remember all NFS typed partitions.
260 if (stat("/proc/version", &st
) < 0)
262 if ((mnt
= setmntent("/proc/mounts", "r")) == (FILE*)0)
265 while ((ent
= getmntent(mnt
))) {
266 if (isnetfs(ent
->mnt_type
)) {
267 size_t nlen
= strlen(ent
->mnt_dir
);
269 xmemalign((void*)&p
, sizeof(void*), alignof(NFS
)+(nlen
+1));
270 p
->name
= ((char*)p
)+alignof(NFS
);
272 p
->shadow
= (SHADOW
*)0;
274 strcpy(p
->name
, ent
->mnt_dir
);
284 if ((mnt
= setmntent("/proc/mounts", "r")) == (FILE*)0)
287 while ((ent
= getmntent(mnt
))) {
290 for (p
= nlist
; p
; p
= p
->next
) {
294 if (strcmp(ent
->mnt_dir
, p
->name
) == 0)
296 if (strncmp(ent
->mnt_dir
, p
->name
, p
->nlen
) != 0)
299 nlen
= strlen(ent
->mnt_dir
);
300 xmemalign((void*)&s
, sizeof(void*), alignof(SHADOW
)+(nlen
+1));
301 s
->name
= ((char*)s
)+alignof(SHADOW
);
304 strcpy(s
->name
, ent
->mnt_dir
);
308 s
->prev
= (SHADOW
*)0;
315 static void clear_shadow(SHADOW
*restrict shadow
)
321 for (s
= shadow
; n
; s
= n
) {
325 if (n
) n
->prev
= (SHADOW
*)0;
335 static void clear_mnt(void)
341 for (p
= nlist
; n
; p
= n
) {
345 if (n
) n
->prev
= (NFS
*)0;
352 clear_shadow(p
->shadow
);
358 * Check if path is ia shadow off a NFS partition.
360 static int shadow(SHADOW
*restrict
this, const char *restrict name
, const size_t nlen
)
366 for (s
= this; s
; s
= s
->next
) {
369 if (name
[s
->nlen
] != '\0' && name
[s
->nlen
] != '/')
371 if (strncmp(name
, s
->name
, s
->nlen
) == 0)
379 * Check path is located on a network based partition.
381 int check4nfs(const char * path
, char * real
)
383 char buf
[PATH_MAX
+1];
385 int deep
= MAXSYMLINKS
;
387 if (!nlist
) return 0;
394 if ((prev
= strdupa(curr
)) == NULL
) {
395 nsyslog(LOG_ERR
, "strdupa(): %s\n", strerror(errno
));
400 if ((len
= readlink(curr
, buf
, PATH_MAX
)) < 0)
407 if ((slash
= strrchr(prev
, '/'))) {
408 size_t off
= slash
- prev
+ 1;
410 if (off
+ len
> PATH_MAX
)
411 len
= PATH_MAX
- off
;
413 memmove(&buf
[off
], &buf
[0], len
+ 1);
414 memcpy(&buf
[0], prev
, off
);
419 if (deep
-- <= 0) return 0;
423 if (real
) strcpy(real
, curr
);
425 if (errno
== EINVAL
) {
426 const size_t nlen
= strlen(curr
);
428 for (p
= nlist
; p
; p
= p
->next
) {
431 if (curr
[p
->nlen
] != '\0' && curr
[p
->nlen
] != '/')
433 if (!strncmp(curr
, p
->name
, p
->nlen
)) {
434 if (shadow(p
->shadow
, curr
, nlen
))
444 int readarg(FILE *fp
, char *buf
, int sz
)
448 while (f
< (sz
-1) && (c
= fgetc(fp
)) != EOF
&& c
)
452 return (c
== EOF
&& f
== 0) ? c
: f
;
456 * Read the proc filesystem.
457 * CWD must be /proc to avoid problems if / is affected by the killing (ie depend on fuse).
459 int readproc(int do_stat
)
466 char path
[PATH_MAX
+1];
467 char buf
[PATH_MAX
+1];
469 unsigned long startcode
, endcode
;
472 /* Open the /proc directory. */
473 if (chdir("/proc") == -1) {
474 nsyslog(LOG_ERR
, "chdir /proc failed");
477 if ((dir
= opendir(".")) == NULL
) {
478 nsyslog(LOG_ERR
, "cannot opendir(/proc)");
482 /* Free the already existing process list. */
484 for (p
= plist
; n
; p
= n
) {
486 if (p
->argv0
) free(p
->argv0
);
487 if (p
->argv1
) free(p
->argv1
);
488 if (p
->statname
) free(p
->statname
);
493 /* Walk through the directory. */
494 while ((d
= readdir(dir
)) != NULL
) {
496 /* See if this is a process */
497 if ((pid
= atoi(d
->d_name
)) == 0) continue;
499 /* Get a PROC struct . */
500 p
= (PROC
*)xmalloc(sizeof(PROC
));
501 memset(p
, 0, sizeof(PROC
));
503 /* Open the status file. */
504 snprintf(path
, sizeof(path
), "%s/stat", d
->d_name
);
506 /* Read SID & statname from it. */
507 if ((fp
= fopen(path
, "r")) != NULL
) {
509 fgets(buf
, sizeof(buf
), fp
);
511 /* See if name starts with '(' */
513 while (*s
!= ' ') s
++;
516 /* Read program name. */
517 q
= strrchr(buf
, ')');
521 "can't get program name from /proc/%s\n",
523 if (p
->argv0
) free(p
->argv0
);
524 if (p
->argv1
) free(p
->argv1
);
525 if (p
->statname
) free(p
->statname
);
532 while (*q
!= ' ') q
++;
535 while (*q
== ' ') q
++;
536 p
->statname
= (char *)xmalloc(strlen(s
)+1);
537 strcpy(p
->statname
, s
);
539 /* Get session, startcode, endcode. */
540 startcode
= endcode
= 0;
541 if (sscanf(q
, "%*c %*d %*d %d %*d %*d %*u %*u "
542 "%*u %*u %*u %*u %*u %*d %*d "
543 "%*d %*d %*d %*d %*u %*u %*d "
545 &p
->sid
, &startcode
, &endcode
) != 3) {
547 nsyslog(LOG_ERR
, "can't read sid from %s\n",
549 if (p
->argv0
) free(p
->argv0
);
550 if (p
->argv1
) free(p
->argv1
);
551 if (p
->statname
) free(p
->statname
);
555 if (startcode
== 0 && endcode
== 0)
559 /* Process disappeared.. */
560 if (p
->argv0
) free(p
->argv0
);
561 if (p
->argv1
) free(p
->argv1
);
562 if (p
->statname
) free(p
->statname
);
567 snprintf(path
, sizeof(path
), "%s/cmdline", d
->d_name
);
568 if ((fp
= fopen(path
, "r")) != NULL
) {
570 /* Now read argv[0] */
571 f
= readarg(fp
, buf
, sizeof(buf
));
574 /* Store the name into malloced memory. */
575 p
->argv0
= (char *)xmalloc(f
+ 1);
576 strcpy(p
->argv0
, buf
);
578 /* Get a pointer to the basename. */
579 p
->argv0base
= strrchr(p
->argv0
, '/');
580 if (p
->argv0base
!= NULL
)
583 p
->argv0base
= p
->argv0
;
586 /* And read argv[1] */
587 while ((f
= readarg(fp
, buf
, sizeof(buf
))) != EOF
)
588 if (buf
[0] != '-') break;
591 /* Store the name into malloced memory. */
592 p
->argv1
= (char *)xmalloc(f
+ 1);
593 strcpy(p
->argv1
, buf
);
595 /* Get a pointer to the basename. */
596 p
->argv1base
= strrchr(p
->argv1
, '/');
597 if (p
->argv1base
!= NULL
)
600 p
->argv1base
= p
->argv1
;
606 /* Process disappeared.. */
607 if (p
->argv0
) free(p
->argv0
);
608 if (p
->argv1
) free(p
->argv1
);
609 if (p
->statname
) free(p
->statname
);
614 /* Try to stat the executable. */
615 snprintf(path
, sizeof(path
), "/proc/%s/exe", d
->d_name
);
621 if ((p
->nfs
= check4nfs(path
, buf
)))
624 if (stat(path
, &st
) != 0)
632 /* Link it into the list. */
643 PIDQ_HEAD
*init_pid_q(PIDQ_HEAD
*q
)
645 q
->head
= q
->next
= q
->tail
= NULL
;
649 int empty_q(PIDQ_HEAD
*q
)
651 return (q
->head
== NULL
);
654 int add_pid_to_q(PIDQ_HEAD
*q
, PROC
*p
)
658 tmp
= (PIDQ
*)xmalloc(sizeof(PIDQ
));
673 PROC
*get_next_from_pid_q(PIDQ_HEAD
*q
)
688 /* Try to get the process ID of a given process. */
689 PIDQ_HEAD
*pidof(char *prog
)
699 char real
[PATH_MAX
+1];
704 /* Try to stat the executable. */
705 if (prog
[0] == '/') {
706 memset(&real
[0], 0, sizeof(real
));
708 if (check4nfs(prog
, real
))
712 prog
= &real
[0]; /* Binary located on network FS. */
714 if ((nfs
== 0) && (stat(prog
, &st
) == 0))
715 dostat
++; /* Binary located on a local FS. */
718 /* Get basename of program. */
719 if ((s
= strrchr(prog
, '/')) == NULL
)
727 q
= (PIDQ_HEAD
*)xmalloc(sizeof(PIDQ_HEAD
));
730 /* First try to find a match based on dev/ino pair. */
731 if (dostat
&& !nfs
) {
732 for (p
= plist
; p
; p
= p
->next
) {
735 if (p
->dev
== st
.st_dev
&& p
->ino
== st
.st_ino
) {
742 /* Second try to find a match based on full path name on
743 * network FS located binaries */
744 if (!foundone
&& nfs
) {
745 for (p
= plist
; p
; p
= p
->next
) {
746 char exe
[PATH_MAX
+1];
747 char path
[PATH_MAX
+1];
751 snprintf(exe
, sizeof(exe
), "/proc/%d/exe", p
->pid
);
752 if ((len
= readlink(exe
, path
, PATH_MAX
)) < 0)
755 if (strcmp(prog
, path
) != 0)
762 /* If we didn't find a match based on dev/ino, try the name. */
763 if (!foundone
) for (p
= plist
; p
; p
= p
->next
) {
766 /* matching nonmatching
767 * proc name prog name prog name
768 * --- ----------- ------------
772 * Algorithm: Match if:
777 * Specifically, do not match just because base(cmd) = base(arg)
778 * as was done in earlier versions of this program, since this
779 * allows /aaa/foo to match /bbb/foo .
782 (p
->argv0
&& strcmp(p
->argv0
, prog
) == 0)
783 || (p
->argv0
&& s
!= prog
&& strcmp(p
->argv0
, s
) == 0)
784 || (p
->argv0base
&& strcmp(p
->argv0base
, prog
) == 0);
786 /* For scripts, compare argv[1] as well. */
788 scripts_too
&& p
->statname
&& p
->argv1base
789 && !strncmp(p
->statname
, p
->argv1base
, STATNAMELEN
)
792 (p
->argv1
&& strcmp(p
->argv1
, prog
) == 0)
793 || (p
->argv1
&& s
!= prog
&& strcmp(p
->argv1
, s
) == 0)
794 || (p
->argv1base
&& strcmp(p
->argv1base
, prog
) == 0);
798 * if we have a space in argv0, process probably
799 * used setproctitle so try statname.
801 if (strlen(s
) <= STATNAMELEN
&&
804 strchr(p
->argv0
, ' '))) {
805 ok
|= (strcmp(p
->statname
, s
) == 0);
809 * if we have a `-' as the first character, process
810 * probably used as a login shell
812 if (strlen(s
) <= STATNAMELEN
&&
815 p
->argv0
[0] == '-')) {
816 ok
|= (strcmp(p
->statname
, s
) == 0);
819 if (ok
) add_pid_to_q(q
, p
);
825 /* Give usage message and exit. */
828 nsyslog(LOG_ERR
, "only one argument, a signal number, allowed");
833 /* write to syslog file if not open terminal */
835 __attribute__ ((format (printf
, 2, 3)))
837 void nsyslog(int pri
, char *fmt
, ...)
843 if (ttyname(0) == NULL
) {
844 vsyslog(pri
, fmt
, args
);
846 fprintf(stderr
, "%s: ",progname
);
847 vfprintf(stderr
, fmt
, args
);
848 fprintf(stderr
, "\n");
854 #define PIDOF_SINGLE 0x01
855 #define PIDOF_OMIT 0x02
856 #define PIDOF_NETFS 0x04
859 * Pidof functionality.
861 int main_pidof(int argc
, char **argv
)
869 int chroot_check
= 0;
877 if ((token
= getenv("PIDOF_NETFS")) && (strcmp(token
,"no") != 0))
878 flags
|= PIDOF_NETFS
;
880 while ((opt
= getopt(argc
,argv
,"hco:sxn")) != EOF
) switch (opt
) {
882 nsyslog(LOG_ERR
,"invalid options on command line!\n");
886 if (geteuid() == 0) chroot_check
= 1;
890 while ((token
= strsep(&here
, ",;:"))) {
894 if (strcmp("%PPID", token
) == 0)
897 opid
= (pid_t
)atoi(token
);
901 "illegal omit pid value "
905 xmemalign((void*)&optr
, sizeof(void*), alignof(OMIT
));
907 optr
->prev
= (OMIT
*)0;
914 flags
|= PIDOF_SINGLE
;
920 flags
|= PIDOF_NETFS
;
929 /* Check if we are in a chroot */
931 snprintf(tmp
, 512, "/proc/%d/root", getpid());
932 if (stat(tmp
, &st
) < 0) {
933 nsyslog(LOG_ERR
, "stat failed for %s!\n", tmp
);
939 if (flags
& PIDOF_NETFS
)
940 init_nfs(); /* Which network based FS are online? */
942 /* Print out process-ID's one by one. */
943 readproc((flags
& PIDOF_NETFS
) ? DO_NETFS
: DO_STAT
);
945 for(f
= 0; f
< argc
; f
++) {
946 if ((q
= pidof(argv
[f
])) != NULL
) {
948 while ((p
= get_next_from_pid_q(q
))) {
949 if ((flags
& PIDOF_OMIT
) && omit
) {
951 for (optr
= omit
; optr
; optr
= optr
->next
) {
952 if (optr
->pid
== p
->pid
)
957 * On a match, continue with
958 * the for loop above.
963 if (flags
& PIDOF_SINGLE
) {
971 snprintf(tmp
, 512, "/proc/%d/root",
973 if (stat(tmp
, &st2
) < 0 ||
974 st
.st_dev
!= st2
.st_dev
||
975 st
.st_ino
!= st2
.st_ino
) {
981 printf("%d", p
->pid
);
992 return(first
? 1 : 0);
995 /* Main for either killall or pidof. */
996 int main(int argc
, char **argv
)
1003 /* return non-zero if no process was killed */
1006 /* Get program name. */
1007 if ((progname
= strrchr(argv
[0], '/')) == NULL
)
1012 /* Now connect to syslog. */
1013 openlog(progname
, LOG_CONS
|LOG_PID
, LOG_DAEMON
);
1015 /* Were we called as 'pidof' ? */
1016 if (strcmp(progname
, "pidof") == 0)
1017 return main_pidof(argc
, argv
);
1019 /* Right, so we are "killall". */
1023 for (c
= 1; c
< argc
; c
++) {
1024 if (argv
[c
][0] == '-') (argv
[c
])++;
1025 if (argv
[c
][0] == 'o') {
1026 char * token
, * here
;
1032 while ((token
= strsep(&here
, ",;:"))) {
1033 OMIT
*restrict optr
;
1034 pid_t opid
= (pid_t
)atoi(token
);
1038 "illegal omit pid value "
1042 xmemalign((void*)&optr
, sizeof(void*), alignof(OMIT
));
1044 optr
->prev
= (OMIT
*)0;
1049 else if ((sig
= atoi(argv
[1])) <= 0 || sig
> 31)
1054 /* First get the /proc filesystem online. */
1058 * Ignoring SIGKILL and SIGSTOP do not make sense, but
1059 * someday kill(-1, sig) might kill ourself if we don't
1060 * do this. This certainly is a valid concern for SIGTERM-
1061 * Linux 2.1 might send the calling process the signal too.
1063 signal(SIGTERM
, SIG_IGN
);
1064 signal(SIGSTOP
, SIG_IGN
);
1065 signal(SIGKILL
, SIG_IGN
);
1067 /* lock us into memory */
1068 mlockall(MCL_CURRENT
| MCL_FUTURE
);
1070 /* Now stop all processes. */
1074 /* Read /proc filesystem */
1075 if (readproc(NO_STAT
) < 0) {
1080 /* Now kill all processes except init (pid 1) and our session. */
1081 sid
= (int)getsid(0);
1082 pid
= (int)getpid();
1083 for (p
= plist
; p
; p
= p
->next
) {
1084 if (p
->pid
== 1 || p
->pid
== pid
|| p
->sid
== sid
|| p
->kernel
)
1089 for (optr
= omit
; optr
; optr
= optr
->next
) {
1090 if (optr
->pid
== p
->pid
)
1094 /* On a match, continue with the for loop above. */
1103 /* And let them continue. */
1109 /* Force the kernel to run the scheduler */