- consolidate "Sending SIG%s to all processes"; untested..
[oweals/busybox.git] / init / init_shared.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Stuff shared between init, reboot, halt, and poweroff
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8  */
9
10 #include <signal.h>
11 #include <stdlib.h>
12 #include <sys/types.h>
13 #include <unistd.h>
14 #include <getopt.h>
15 #include <sys/reboot.h>
16 #include <sys/syslog.h>
17 #include "busybox.h"
18 #include "init_shared.h"
19
20 #ifndef CONFIG_INIT
21 const char * const bb_shutdown_format = "\r%s\n";
22 int bb_shutdown_system(unsigned long magic)
23 {
24         int pri = LOG_KERN|LOG_NOTICE|LOG_FACMASK;
25         const char *message;
26
27         /* Don't kill ourself */
28         signal(SIGTERM,SIG_IGN);
29         signal(SIGHUP,SIG_IGN);
30         bb_setpgrp;
31
32         /* Allow Ctrl-Alt-Del to reboot system. */
33 #ifndef RB_ENABLE_CAD
34 #define RB_ENABLE_CAD   0x89abcdef
35 #endif
36         reboot(RB_ENABLE_CAD);
37
38         openlog(bb_applet_name, 0, pri);
39
40         message = "\nThe system is going down NOW !!";
41         syslog(pri, "%s", message);
42         printf(bb_shutdown_format, message);
43
44         sync();
45
46         /* Send signals to every process _except_ pid 1 */
47         message = "TERM";
48         syslog(pri, init_sending_format, message);
49         printf(bb_shutdown_format, message);
50
51         kill(-1, SIGTERM);
52         sleep(1);
53         sync();
54
55         message = "KILL";
56         syslog(pri, init_sending_format, message);
57         printf(bb_shutdown_format, message);
58
59         kill(-1, SIGKILL);
60         sleep(1);
61
62         sync();
63
64         reboot(magic);
65         return 0; /* Shrug */
66 }
67 #endif