]>
git.wh0rd.org - sysvinit.git/blob - src/killall5.c
3a3dec31aec56403e010b8e07a9749482e9dff0f
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
54 #include <sys/param.h>
56 #include <sys/types.h>
60 char *Version
= "@(#)killall5 2.86 31-Jul-2004 miquels@cistron.nl";
62 #define STATNAMELEN 15
67 /* Info about a process. */
69 char *argv0
; /* Name as found out from argv[0] */
70 char *argv0base
; /* `basename argv[1]` */
71 char *argv1
; /* Name as found out from argv[1] */
72 char *argv1base
; /* `basename argv[1]` */
73 char *statname
; /* the statname without braces */
74 ino_t ino
; /* Inode number */
75 dev_t dev
; /* Device it is on */
76 pid_t pid
; /* Process ID. */
77 pid_t sid
; /* Session ID. */
78 char kernel
; /* Kernel thread or zombie. */
79 char nfs
; /* Name found on network FS. */
80 struct proc
*next
; /* Pointer to next struct. */
96 typedef struct _s_omit
{
102 typedef struct _s_shadow
104 struct _s_shadow
*next
;
105 struct _s_shadow
*prev
;
110 typedef struct _s_nfs
112 struct _s_nfs
*next
; /* Pointer to next struct. */
113 struct _s_nfs
*prev
; /* Pointer to previous st. */
114 SHADOW
*shadow
; /* Pointer to shadows */
119 /* List of processes. */
122 /* List of processes to omit. */
125 /* List of NFS mountes partitions. */
128 /* Did we stop all processes ? */
133 char *progname
; /* the name of the running program */
135 __attribute__ ((format (printf
, 2, 3)))
137 void nsyslog(int pri
, char *fmt
, ...);
139 #if !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L)
141 # define inline __inline__
144 # define restrict __restrict__
147 #define alignof(type) ((sizeof(type)+(sizeof(void*)-1)) & ~(sizeof(void*)-1))
150 * Malloc space, barf if out of memory.
153 static void *xmalloc(size_t) __attribute__ ((__malloc__
));
155 static void *xmalloc(size_t bytes
)
159 if ((p
= malloc(bytes
)) == NULL
) {
160 if (sent_sigstop
) kill(-1, SIGCONT
);
161 nsyslog(LOG_ERR
, "out of memory");
168 static inline void xmemalign(void **, size_t, size_t) __attribute__ ((__nonnull__ (1)));
170 static inline void xmemalign(void **memptr
, size_t alignment
, size_t size
)
172 if ((posix_memalign(memptr
, alignment
, size
)) < 0) {
173 if (sent_sigstop
) kill(-1, SIGCONT
);
174 nsyslog(LOG_ERR
, "out of memory");
180 * See if the proc filesystem is there. Mount if needed.
185 char *args
[] = { "mount", "-t", "proc", "proc", "/proc", 0 };
190 /* Stat /proc/version to see if /proc is mounted. */
191 if (stat("/proc/version", &st
) < 0 && errno
== ENOENT
) {
193 /* It's not there, so mount it. */
194 if ((pid
= fork()) < 0) {
195 nsyslog(LOG_ERR
, "cannot fork");
199 /* Try a few mount binaries. */
200 execv("/sbin/mount", args
);
201 execv("/bin/mount", args
);
203 /* Okay, I give up. */
204 nsyslog(LOG_ERR
, "cannot execute mount");
207 /* Wait for child. */
208 while ((rc
= wait(&wst
)) != pid
)
209 if (rc
< 0 && errno
== ECHILD
)
211 if (rc
!= pid
|| WEXITSTATUS(wst
) != 0)
212 nsyslog(LOG_ERR
, "mount returned non-zero exit status");
217 /* See if mount succeeded. */
218 if (stat("/proc/version", &st
) < 0) {
220 nsyslog(LOG_ERR
, "/proc not mounted, failed to mount.");
222 nsyslog(LOG_ERR
, "/proc unavailable.");
229 static inline int isnetfs(const char * type
)
231 static const char* netfs
[] = {"nfs", "nfs4", "smbfs", "cifs", "afs", "ncpfs", (char*)0};
233 for (n
= 0; netfs
[n
]; n
++) {
234 if (!strcasecmp(netfs
[n
], type
))
241 * Remember all NFS typed partitions.
251 if (stat("/proc/version", &st
) < 0)
253 if ((mnt
= setmntent("/proc/mounts", "r")) == (FILE*)0)
256 while ((ent
= getmntent(mnt
))) {
257 if (isnetfs(ent
->mnt_type
)) {
258 size_t nlen
= strlen(ent
->mnt_dir
);
260 xmemalign((void*)&p
, sizeof(void*), alignof(NFS
)+(nlen
+1));
261 p
->name
= ((char*)p
)+alignof(NFS
);
263 p
->shadow
= (SHADOW
*)0;
265 strcpy(p
->name
, ent
->mnt_dir
);
275 if ((mnt
= setmntent("/proc/mounts", "r")) == (FILE*)0)
278 while ((ent
= getmntent(mnt
))) {
281 for (p
= nlist
; p
; p
= p
->next
) {
285 if (strcmp(ent
->mnt_dir
, p
->name
) == 0)
287 if (strncmp(ent
->mnt_dir
, p
->name
, p
->nlen
) != 0)
290 nlen
= strlen(ent
->mnt_dir
);
291 xmemalign((void*)&s
, sizeof(void*), alignof(SHADOW
)+(nlen
+1));
292 s
->name
= ((char*)s
)+alignof(SHADOW
);
295 strcpy(s
->name
, ent
->mnt_dir
);
299 s
->prev
= (SHADOW
*)0;
306 static void clear_shadow(SHADOW
*restrict shadow
)
312 for (s
= shadow
; n
; s
= n
) {
316 if (n
) n
->prev
= (SHADOW
*)0;
326 static void clear_mnt(void)
332 for (p
= nlist
; n
; p
= n
) {
336 if (n
) n
->prev
= (NFS
*)0;
343 clear_shadow(p
->shadow
);
349 * Check if path is ia shadow off a NFS partition.
351 static int shadow(SHADOW
*restrict
this, const char *restrict name
, const size_t nlen
)
357 for (s
= this; s
; s
= s
->next
) {
360 if (name
[s
->nlen
] != '\0' && name
[s
->nlen
] != '/')
362 if (strncmp(name
, s
->name
, s
->nlen
) == 0)
370 * Check path is located on a network based partition.
372 int check4nfs(const char * path
, char * real
)
374 char buf
[PATH_MAX
+1];
376 int deep
= MAXSYMLINKS
;
378 if (!nlist
) return 0;
385 if ((prev
= strdupa(curr
)) == NULL
) {
386 nsyslog(LOG_ERR
, "strdupa(): %s\n", strerror(errno
));
391 if ((len
= readlink(curr
, buf
, PATH_MAX
)) < 0)
398 if ((slash
= strrchr(prev
, '/'))) {
399 size_t off
= slash
- prev
+ 1;
401 if (off
+ len
> PATH_MAX
)
402 len
= PATH_MAX
- off
;
404 memmove(&buf
[off
], &buf
[0], len
+ 1);
405 memcpy(&buf
[0], prev
, off
);
410 if (deep
-- <= 0) return 0;
414 if (real
) strcpy(real
, curr
);
416 if (errno
== EINVAL
) {
417 const size_t nlen
= strlen(curr
);
419 for (p
= nlist
; p
; p
= p
->next
) {
422 if (curr
[p
->nlen
] != '\0' && curr
[p
->nlen
] != '/')
424 if (!strncmp(curr
, p
->name
, p
->nlen
)) {
425 if (shadow(p
->shadow
, curr
, nlen
))
435 int readarg(FILE *fp
, char *buf
, int sz
)
439 while (f
< (sz
-1) && (c
= fgetc(fp
)) != EOF
&& c
)
443 return (c
== EOF
&& f
== 0) ? c
: f
;
447 * Read the proc filesystem.
448 * CWD must be /proc to avoid problems if / is affected by the killing (ie depend on fuse).
450 int readproc(int do_stat
)
460 unsigned long startcode
, endcode
;
463 /* Open the /proc directory. */
464 if (chdir("/proc") == -1) {
465 nsyslog(LOG_ERR
, "chdir /proc failed");
468 if ((dir
= opendir(".")) == NULL
) {
469 nsyslog(LOG_ERR
, "cannot opendir(/proc)");
473 /* Free the already existing process list. */
475 for (p
= plist
; n
; p
= n
) {
477 if (p
->argv0
) free(p
->argv0
);
478 if (p
->argv1
) free(p
->argv1
);
479 if (p
->statname
) free(p
->statname
);
484 /* Walk through the directory. */
485 while ((d
= readdir(dir
)) != NULL
) {
487 /* See if this is a process */
488 if ((pid
= atoi(d
->d_name
)) == 0) continue;
490 /* Get a PROC struct . */
491 p
= (PROC
*)xmalloc(sizeof(PROC
));
492 memset(p
, 0, sizeof(PROC
));
494 /* Open the status file. */
495 snprintf(path
, sizeof(path
), "%s/stat", d
->d_name
);
497 /* Read SID & statname from it. */
498 if ((fp
= fopen(path
, "r")) != NULL
) {
500 fgets(buf
, sizeof(buf
), fp
);
502 /* See if name starts with '(' */
504 while (*s
!= ' ') s
++;
507 /* Read program name. */
508 q
= strrchr(buf
, ')');
512 "can't get program name from /proc/%s\n",
514 if (p
->argv0
) free(p
->argv0
);
515 if (p
->argv1
) free(p
->argv1
);
516 if (p
->statname
) free(p
->statname
);
523 while (*q
!= ' ') q
++;
526 while (*q
== ' ') q
++;
527 p
->statname
= (char *)xmalloc(strlen(s
)+1);
528 strcpy(p
->statname
, s
);
530 /* Get session, startcode, endcode. */
531 startcode
= endcode
= 0;
532 if (sscanf(q
, "%*c %*d %*d %d %*d %*d %*u %*u "
533 "%*u %*u %*u %*u %*u %*d %*d "
534 "%*d %*d %*d %*d %*u %*u %*d "
536 &p
->sid
, &startcode
, &endcode
) != 3) {
538 nsyslog(LOG_ERR
, "can't read sid from %s\n",
540 if (p
->argv0
) free(p
->argv0
);
541 if (p
->argv1
) free(p
->argv1
);
542 if (p
->statname
) free(p
->statname
);
546 if (startcode
== 0 && endcode
== 0)
550 /* Process disappeared.. */
551 if (p
->argv0
) free(p
->argv0
);
552 if (p
->argv1
) free(p
->argv1
);
553 if (p
->statname
) free(p
->statname
);
558 snprintf(path
, sizeof(path
), "%s/cmdline", d
->d_name
);
559 if ((fp
= fopen(path
, "r")) != NULL
) {
561 /* Now read argv[0] */
562 f
= readarg(fp
, buf
, sizeof(buf
));
565 /* Store the name into malloced memory. */
566 p
->argv0
= (char *)xmalloc(f
+ 1);
567 strcpy(p
->argv0
, buf
);
569 /* Get a pointer to the basename. */
570 p
->argv0base
= strrchr(p
->argv0
, '/');
571 if (p
->argv0base
!= NULL
)
574 p
->argv0base
= p
->argv0
;
577 /* And read argv[1] */
578 while ((f
= readarg(fp
, buf
, sizeof(buf
))) != EOF
)
579 if (buf
[0] != '-') break;
582 /* Store the name into malloced memory. */
583 p
->argv1
= (char *)xmalloc(f
+ 1);
584 strcpy(p
->argv1
, buf
);
586 /* Get a pointer to the basename. */
587 p
->argv1base
= strrchr(p
->argv1
, '/');
588 if (p
->argv1base
!= NULL
)
591 p
->argv1base
= p
->argv1
;
597 /* Process disappeared.. */
598 if (p
->argv0
) free(p
->argv0
);
599 if (p
->argv1
) free(p
->argv1
);
600 if (p
->statname
) free(p
->statname
);
605 /* Try to stat the executable. */
606 snprintf(path
, sizeof(path
), "/proc/%s/exe", d
->d_name
);
612 if ((p
->nfs
= check4nfs(path
, buf
)))
615 if (stat(path
, &st
) != 0)
623 /* Link it into the list. */
634 PIDQ_HEAD
*init_pid_q(PIDQ_HEAD
*q
)
636 q
->head
= q
->next
= q
->tail
= NULL
;
640 int empty_q(PIDQ_HEAD
*q
)
642 return (q
->head
== NULL
);
645 int add_pid_to_q(PIDQ_HEAD
*q
, PROC
*p
)
649 tmp
= (PIDQ
*)xmalloc(sizeof(PIDQ
));
664 PROC
*get_next_from_pid_q(PIDQ_HEAD
*q
)
679 /* Try to get the process ID of a given process. */
680 PIDQ_HEAD
*pidof(char *prog
)
690 char real
[PATH_MAX
+1];
695 /* Try to stat the executable. */
696 if (prog
[0] == '/') {
697 memset(&real
[0], 0, sizeof(real
));
699 if (check4nfs(prog
, real
))
703 prog
= &real
[0]; /* Binary located on network FS. */
705 if ((nfs
== 0) && (stat(prog
, &st
) == 0))
706 dostat
++; /* Binary located on a local FS. */
709 /* Get basename of program. */
710 if ((s
= strrchr(prog
, '/')) == NULL
)
718 q
= (PIDQ_HEAD
*)xmalloc(sizeof(PIDQ_HEAD
));
721 /* First try to find a match based on dev/ino pair. */
722 if (dostat
&& !nfs
) {
723 for (p
= plist
; p
; p
= p
->next
) {
726 if (p
->dev
== st
.st_dev
&& p
->ino
== st
.st_ino
) {
733 /* Second try to find a match based on full path name on
734 * network FS located binaries */
735 if (!foundone
&& nfs
) {
736 for (p
= plist
; p
; p
= p
->next
) {
737 char exe
[PATH_MAX
+1];
738 char path
[PATH_MAX
+1];
741 snprintf(exe
, sizeof(exe
), "/proc/%d/exe", p
->pid
);
742 if ((len
= readlink(exe
, path
, PATH_MAX
)) < 0)
745 if (strcmp(prog
, path
) != 0)
752 /* If we didn't find a match based on dev/ino, try the name. */
753 if (!foundone
) for (p
= plist
; p
; p
= p
->next
) {
756 /* matching nonmatching
757 * proc name prog name prog name
758 * --- ----------- ------------
762 * Algorithm: Match if:
767 * Specifically, do not match just because base(cmd) = base(arg)
768 * as was done in earlier versions of this program, since this
769 * allows /aaa/foo to match /bbb/foo .
772 (p
->argv0
&& strcmp(p
->argv0
, prog
) == 0)
773 || (p
->argv0
&& s
!= prog
&& strcmp(p
->argv0
, s
) == 0)
774 || (p
->argv0base
&& strcmp(p
->argv0base
, prog
) == 0);
776 /* For scripts, compare argv[1] as well. */
778 scripts_too
&& p
->statname
&& p
->argv1base
779 && !strncmp(p
->statname
, p
->argv1base
, STATNAMELEN
)
782 (p
->argv1
&& strcmp(p
->argv1
, prog
) == 0)
783 || (p
->argv1
&& s
!= prog
&& strcmp(p
->argv1
, s
) == 0)
784 || (p
->argv1base
&& strcmp(p
->argv1base
, prog
) == 0);
788 * if we have a space in argv0, process probably
789 * used setproctitle so try statname.
791 if (strlen(s
) <= STATNAMELEN
&&
794 strchr(p
->argv0
, ' '))) {
795 ok
|= (strcmp(p
->statname
, s
) == 0);
799 * if we have a `-' as the first character, process
800 * probably used as a login shell
802 if (strlen(s
) <= STATNAMELEN
&&
805 p
->argv0
[0] == '-')) {
806 ok
|= (strcmp(p
->statname
, s
) == 0);
809 if (ok
) add_pid_to_q(q
, p
);
815 /* Give usage message and exit. */
818 nsyslog(LOG_ERR
, "only one argument, a signal number, allowed");
823 /* write to syslog file if not open terminal */
825 __attribute__ ((format (printf
, 2, 3)))
827 void nsyslog(int pri
, char *fmt
, ...)
833 if (ttyname(0) == NULL
) {
834 vsyslog(pri
, fmt
, args
);
836 fprintf(stderr
, "%s: ",progname
);
837 vfprintf(stderr
, fmt
, args
);
838 fprintf(stderr
, "\n");
844 #define PIDOF_SINGLE 0x01
845 #define PIDOF_OMIT 0x02
846 #define PIDOF_NETFS 0x04
849 * Pidof functionality.
851 int main_pidof(int argc
, char **argv
)
859 int chroot_check
= 0;
867 if ((token
= getenv("PIDOF_NETFS")) && (strcmp(token
,"no") != 0))
868 flags
|= PIDOF_NETFS
;
870 while ((opt
= getopt(argc
,argv
,"hco:sxn")) != EOF
) switch (opt
) {
872 nsyslog(LOG_ERR
,"invalid options on command line!\n");
876 if (geteuid() == 0) chroot_check
= 1;
880 while ((token
= strsep(&here
, ",;:"))) {
884 if (strcmp("%PPID", token
) == 0)
887 opid
= (pid_t
)atoi(token
);
891 "illegal omit pid value "
895 xmemalign((void*)&optr
, sizeof(void*), alignof(OMIT
));
897 optr
->prev
= (OMIT
*)0;
904 flags
|= PIDOF_SINGLE
;
910 flags
|= PIDOF_NETFS
;
919 /* Check if we are in a chroot */
921 snprintf(tmp
, 512, "/proc/%d/root", getpid());
922 if (stat(tmp
, &st
) < 0) {
923 nsyslog(LOG_ERR
, "stat failed for %s!\n", tmp
);
929 if (flags
& PIDOF_NETFS
)
930 init_nfs(); /* Which network based FS are online? */
932 /* Print out process-ID's one by one. */
933 readproc((flags
& PIDOF_NETFS
) ? DO_NETFS
: DO_STAT
);
935 for(f
= 0; f
< argc
; f
++) {
936 if ((q
= pidof(argv
[f
])) != NULL
) {
938 while ((p
= get_next_from_pid_q(q
))) {
939 if ((flags
& PIDOF_OMIT
) && omit
) {
941 for (optr
= omit
; optr
; optr
= optr
->next
) {
942 if (optr
->pid
== p
->pid
)
947 * On a match, continue with
948 * the for loop above.
953 if (flags
& PIDOF_SINGLE
) {
961 snprintf(tmp
, 512, "/proc/%d/root",
963 if (stat(tmp
, &st2
) < 0 ||
964 st
.st_dev
!= st2
.st_dev
||
965 st
.st_ino
!= st2
.st_ino
) {
971 printf("%d", p
->pid
);
982 return(first
? 1 : 0);
985 /* Main for either killall or pidof. */
986 int main(int argc
, char **argv
)
993 /* return non-zero if no process was killed */
996 /* Get program name. */
997 if ((progname
= strrchr(argv
[0], '/')) == NULL
)
1002 /* Now connect to syslog. */
1003 openlog(progname
, LOG_CONS
|LOG_PID
, LOG_DAEMON
);
1005 /* Were we called as 'pidof' ? */
1006 if (strcmp(progname
, "pidof") == 0)
1007 return main_pidof(argc
, argv
);
1009 /* Right, so we are "killall". */
1013 for (c
= 1; c
< argc
; c
++) {
1014 if (argv
[c
][0] == '-') (argv
[c
])++;
1015 if (argv
[c
][0] == 'o') {
1016 char * token
, * here
;
1022 while ((token
= strsep(&here
, ",;:"))) {
1023 OMIT
*restrict optr
;
1024 pid_t opid
= (pid_t
)atoi(token
);
1028 "illegal omit pid value "
1032 xmemalign((void*)&optr
, sizeof(void*), alignof(OMIT
));
1034 optr
->prev
= (OMIT
*)0;
1039 else if ((sig
= atoi(argv
[1])) <= 0 || sig
> 31)
1044 /* First get the /proc filesystem online. */
1048 * Ignoring SIGKILL and SIGSTOP do not make sense, but
1049 * someday kill(-1, sig) might kill ourself if we don't
1050 * do this. This certainly is a valid concern for SIGTERM-
1051 * Linux 2.1 might send the calling process the signal too.
1053 signal(SIGTERM
, SIG_IGN
);
1054 signal(SIGSTOP
, SIG_IGN
);
1055 signal(SIGKILL
, SIG_IGN
);
1057 /* lock us into memory */
1058 mlockall(MCL_CURRENT
| MCL_FUTURE
);
1060 /* Now stop all processes. */
1064 /* Read /proc filesystem */
1065 if (readproc(NO_STAT
) < 0) {
1070 /* Now kill all processes except init (pid 1) and our session. */
1071 sid
= (int)getsid(0);
1072 pid
= (int)getpid();
1073 for (p
= plist
; p
; p
= p
->next
) {
1074 if (p
->pid
== 1 || p
->pid
== pid
|| p
->sid
== sid
|| p
->kernel
)
1079 for (optr
= omit
; optr
; optr
= optr
->next
) {
1080 if (optr
->pid
== p
->pid
)
1084 /* On a match, continue with the for loop above. */
1093 /* And let them continue. */
1099 /* Force the kernel to run the scheduler */