mkfs, mkproto: minor improvements
[minix.git] / commands / reboot / halt.c
blob361d6835edced361fc51ef7667520786e6d1cafc
1 /* halt / reboot - halt or reboot system (depends on name)
3 halt - calling reboot() with RBT_HALT
4 reboot - calling reboot() with RBT_REBOOT
6 author: Edvard Tuinder v892231@si.hhs.NL
8 This program calls the library function reboot(2) which performs
9 the system-call do_reboot.
13 #define _POSIX_SOURCE 1
14 #include <sys/types.h>
15 #include <fcntl.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <signal.h>
19 #include <string.h>
20 #include <errno.h>
21 #include <unistd.h>
22 #include <sys/stat.h>
23 #include <sys/wait.h>
25 #include "wtmp.h"
27 void write_log( char *fn );
28 void usage( void );
29 int main( int argc, char *argv[] );
31 char *prog;
32 char *reboot_code = "delay; boot";
34 void
35 usage()
37 fprintf(stderr, "Usage: %s [-hrRfd] [-x reboot-code]\n", prog);
38 exit(1);
41 int
42 main(argc,argv)
43 int argc;
44 char **argv;
46 int flag = -1; /* default action unknown */
47 int fast = 0; /* fast halt/reboot, don't bother being nice. */
48 int i;
49 struct stat dummy;
50 char *monitor_code = "";
51 pid_t pid;
53 if ((prog = strrchr(argv[0],'/')) == NULL) prog = argv[0]; else prog++;
55 if (strcmp(prog, "halt") == 0) flag = RBT_HALT;
56 if (strcmp(prog, "reboot") == 0) flag = RBT_REBOOT;
58 i = 1;
59 while (i < argc && argv[i][0] == '-') {
60 char *opt = argv[i++] + 1;
62 if (*opt == '-' && opt[1] == 0) break; /* -- */
64 while (*opt != 0) switch (*opt++) {
65 case 'h': flag = RBT_HALT; break;
66 case 'r': flag = RBT_REBOOT; break;
67 case 'R': flag = RBT_RESET; break;
68 case 'd': flag = RBT_DEFAULT; break;
69 case 'f': fast = 1; break;
70 case 'x':
71 flag = RBT_MONITOR;
72 if (*opt == 0) {
73 if (i == argc) usage();
74 opt = argv[i++];
76 monitor_code = opt;
77 opt = "";
78 break;
79 default:
80 usage();
84 if (i != argc) usage();
86 if (flag == -1) {
87 fprintf(stderr, "Don't know what to do when named '%s'\n", prog);
88 exit(1);
91 if (flag == RBT_REBOOT) {
92 flag = RBT_MONITOR; /* set monitor code for reboot */
93 monitor_code = reboot_code;
96 if (stat("/usr/bin", &dummy) < 0) {
97 /* It seems that /usr isn't present, let's assume "-f." */
98 fast = 1;
101 signal(SIGHUP, SIG_IGN);
102 signal(SIGTERM, SIG_IGN);
104 /* Skip this part for fast shut down. */
105 if (! fast) {
106 /* Run the shutdown scripts. */
107 switch ((pid = fork())) {
108 case -1:
109 fprintf(stderr, "%s: can't fork(): %s\n", prog, strerror(errno));
110 exit(1);
111 case 0:
112 execl("/bin/sh", "sh", "/etc/rc", "down", (char *) NULL);
113 fprintf(stderr, "%s: can't execute: /bin/sh: %s\n",
114 prog, strerror(errno));
115 exit(1);
116 default:
117 while (waitpid(pid, NULL, 0) != pid) {}
121 /* Tell init to stop spawning getty's. */
122 kill(1, SIGTERM);
124 /* Extra sync for the case where SIGTERM causes deadlock */
125 sync();
127 /* Give everybody a chance to die peacefully. */
128 printf("Sending SIGTERM to all processes ...\n");
129 kill(-1, SIGTERM);
130 sleep(1);
132 write_log(STR_WTMP);
133 write_log(STR_ROOT_WTMP);
135 sync();
137 reboot(flag, monitor_code, strlen(monitor_code));
138 fprintf(stderr, "%s: reboot(): %s\n", prog, strerror(errno));
139 return 1;