umount: getopt return value is int, not char
[minix.git] / commands / reboot / halt.c
blobdd0bff28be3087297c997fce5f9e4ff275716ea1
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;
33 void
34 usage()
36 fprintf(stderr, "Usage: %s [-hrRfpd]\n", prog);
37 exit(1);
40 int
41 main(argc,argv)
42 int argc;
43 char **argv;
45 int flag = -1; /* default action unknown */
46 int fast = 0; /* fast halt/reboot, don't bother being nice. */
47 int i;
48 struct stat dummy;
49 pid_t pid;
51 if ((prog = strrchr(argv[0],'/')) == NULL) prog = argv[0]; else prog++;
53 if (strcmp(prog, "halt") == 0) flag = RBT_HALT;
54 if (strcmp(prog, "reboot") == 0) flag = RBT_REBOOT;
56 i = 1;
57 while (i < argc && argv[i][0] == '-') {
58 char *opt = argv[i++] + 1;
60 if (*opt == '-' && opt[1] == 0) break; /* -- */
62 while (*opt != 0) switch (*opt++) {
63 case 'h': flag = RBT_HALT; break;
64 case 'r': flag = RBT_REBOOT; break;
65 case 'R': flag = RBT_RESET; break;
66 case 'd': flag = RBT_DEFAULT; break;
67 case 'p': flag = RBT_POWEROFF; break;
68 case 'f': fast = 1; break;
69 default:
70 usage();
74 if (i != argc) usage();
76 if (flag == -1) {
77 fprintf(stderr, "Don't know what to do when named '%s'\n", prog);
78 exit(1);
81 if (stat("/usr/bin", &dummy) < 0) {
82 /* It seems that /usr isn't present, let's assume "-f." */
83 fast = 1;
86 signal(SIGHUP, SIG_IGN);
87 signal(SIGTERM, SIG_IGN);
89 /* Skip this part for fast shut down. */
90 if (! fast) {
91 /* Run the shutdown scripts. */
92 switch ((pid = fork())) {
93 case -1:
94 fprintf(stderr, "%s: can't fork(): %s\n", prog, strerror(errno));
95 exit(1);
96 case 0:
97 execl("/bin/sh", "sh", "/etc/rc", "down", (char *) NULL);
98 fprintf(stderr, "%s: can't execute: /bin/sh: %s\n",
99 prog, strerror(errno));
100 exit(1);
101 default:
102 while (waitpid(pid, NULL, 0) != pid) {}
106 /* Tell init to stop spawning getty's. */
107 kill(1, SIGTERM);
109 /* Extra sync for the case where SIGTERM causes deadlock */
110 sync();
112 /* Give everybody a chance to die peacefully. */
113 printf("Sending SIGTERM to all processes ...\n");
114 kill(-1, SIGTERM);
115 sleep(1);
117 write_log(STR_WTMP);
118 write_log(STR_ROOT_WTMP);
120 sync();
122 reboot(flag);
123 fprintf(stderr, "%s: reboot(): %s\n", prog, strerror(errno));
124 return 1;