simple-init

A set of init binaries for linux
git clone git://depsterr.com/git/simple-init
Log | Files | Refs | README | LICENSE

shutdown.c (922B)


      1 #include <sys/reboot.h>
      2 #include <sys/types.h>
      3 #include <sys/wait.h>
      4 #include <signal.h>
      5 #include <unistd.h>
      6 
      7 #include "util.h"
      8 
      9 int main(int argc, char* argv[]) {
     10 	if (argc < 2)
     11 		return -1;
     12 
     13 	log_message("Saving random seed");
     14 	save_random_seed();
     15 
     16 	log_message("Syncing");
     17 	sync();
     18 
     19 	log_message("Sending TERM signal to all processes");
     20 	kill(-1, SIGSTOP);
     21 	kill(-1, SIGTERM);
     22 	kill(-1, SIGCONT);
     23 	sleep(1);
     24 
     25 	log_message("Sending KILL signal to all processes");
     26 	kill(-1, SIGSTOP);
     27 	kill(-1, SIGKILL);
     28 	kill(-1, SIGCONT);
     29 
     30 	log_message("Unmounting all filesystems");
     31 	char* swapcmd[] = { "swapoff", "-a", 0};
     32 	char* umountcmd[] = { "umount", "-lra", 0};
     33 	spawn_process(swapcmd);
     34 	spawn_process(umountcmd);
     35 	/* wait for children without caring about failing */
     36 	while(wait(0) > 0);
     37 
     38 	log_message("Requesting shutdown from kernel");
     39 	if(argv[1][0] == 'p') {
     40 		reboot(RB_POWER_OFF);
     41 	} else {
     42 		reboot(RB_AUTOBOOT);
     43 	}
     44 }