minit

A small yet feature-complete init (http://fefe.de/minit/)
Log | Files | Refs | README | LICENSE

killall5.c (1559B)


      1 /*
      2 
      3 killall5 -- send a signal to all processes.
      4 
      5 killall5 is the SystemV killall command. It sends a signal
      6 to all processes except init(PID 1) and the processes in its
      7 own  session, so  it  won't kill the shell that is running the
      8 script it was called from. Its primary (only) use is in the rc
      9 scripts found in the /etc/init.d directory.
     10 
     11 This program is free software; you can redistribute it and/or
     12 modify it under the terms of the GNU General Public License
     13 as published by the Free Software Foundation; either version
     14 2 of the License, or (at your option) any later version.
     15 
     16 */
     17 
     18 #include <dirent.h>
     19 #include <signal.h>
     20 #include <stdlib.h>
     21 #include <string.h>
     22 #include <unistd.h>
     23 #include <sys/types.h>
     24 
     25 #define USAGE "Usage: killall5 SIGNAL\n"
     26 #define NOPROC "No processes found - /proc not mounted?\n"
     27 
     28 int main(int argc, char **argv)
     29 {
     30  struct dirent *dir;
     31  DIR *dirstream;
     32  register pid_t pid, sid, mypid, mysid;
     33  int signal=-1;
     34  unsigned int sig_sent=0;
     35 
     36  if (argc == 2) {
     37   if (argv[1][0] == '-') argv[1]++;
     38   signal=atoi(argv[1]);
     39  }
     40 
     41  if ( (signal < 1) || ( signal > 31) ) { write(2,USAGE,sizeof USAGE - 1); return 1; }
     42 
     43 
     44  kill(-1,SIGSTOP);
     45 
     46  if ( (dirstream=opendir("/proc"))) {
     47 
     48  mypid=getpid();
     49  mysid=getsid(0);
     50 
     51    while ( (dir=readdir(dirstream))){
     52       pid=atoi(dir->d_name);
     53 
     54        if (pid > 1 ){ 
     55         sig_sent=1;
     56         sid=getsid(pid);
     57          if ( (pid != mypid) &&
     58            ( sid !=mysid)) kill(pid,signal);
     59        }
     60    }
     61  }
     62 
     63  kill(-1,SIGCONT);
     64  if (!sig_sent) { write(2,NOPROC, sizeof NOPROC -1); return 1; }
     65 
     66 return 0;
     67 }
     68