vm: fix failed alloc condition
[minix.git] / commands / reboot / shutdown.c
blob418aab8c2c852d475d4e87cc9aaf9f65777dd91e
1 /*
2 shutdown - close down the system graciously
4 Author: Edvard Tuinder <v892231@si.hhs.NL>
6 This program informs the users that the system is going
7 down, when and why. After that a shutdown notice is written in
8 both /usr/adm/wtmp and by syslog(3). Then reboot(2) is called
9 to really close the system.
11 This actually is a ``nice'' halt(8).
13 Options are supposed to be as with BSD
14 -h: shutdown and halt the system
15 -r: shutdown and reboot
16 -k: stop an already running shutdown
17 -o: obsolete: not implemented
19 New Minix options:
20 -C: crash check, i.e. is the last wtmp entry a shutdown entry?
21 -x: let the monitor execute the given code
22 -R: reset the system
23 -d: default CTRL-ALT-DEL shutdown for current bootloader
26 #define _POSIX_SOURCE 1
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <stdio.h>
30 #include <ctype.h>
31 #include <fcntl.h>
32 #include <time.h>
33 #include <stdlib.h>
34 #include <signal.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <utmp.h>
38 #include <errno.h>
40 #include "wtmp.h"
42 static char SHUT_PID[] = "/usr/run/shutdown.pid";
43 static char NOLOGIN[] = "/etc/nologin";
45 #ifndef __STDC__
46 #define inform_user_time inf_time
47 #define inform_user inf_user
48 #endif
50 void usage( void );
51 void write_pid( void );
52 int inform_user_time( void );
53 void inform_user( void );
54 void terminate( void );
55 void wall( char *when, char *extra );
56 int crash_check( void );
57 void parse_time( char *arg );
58 void get_message( void );
59 int main( int argc, char *argv[] );
60 char *itoa( int n );
62 long wait_time=0L;
63 char message[1024];
64 char info[80];
65 int reboot_flag='h'; /* default is halt */
66 char *reboot_code=""; /* optional monitor code */
67 int info_min, info_hour;
68 char *prog;
70 void parse_time (arg)
71 char *arg;
73 char *p = arg;
74 int hours, minutes;
75 time_t now;
76 struct tm *tm;
77 int delta = 0;
78 int bad = 0;
80 if (p[0] == '+') { delta = 1; p++; }
82 hours = strtoul(p, &p, 10);
83 if (*p == 0 && delta) {
84 minutes = hours;
85 hours = 0;
86 } else {
87 if (*p != ':' && *p != '.')
88 bad = 1;
89 else
90 p++;
91 minutes = strtoul(p, &p, 10);
92 if (*p != 0) bad = 1;
94 if (bad) {
95 fprintf(stderr,"Invalid time specification `%s'\n",arg);
96 usage();
99 time(&now);
100 tm = localtime(&now);
102 if (!delta) {
103 hours -= tm->tm_hour;
104 minutes -= tm->tm_min;
107 if (minutes < 0) {
108 minutes += 60;
109 hours--;
111 if (hours < 0) hours += 24; /* Time after midnight. */
113 tm->tm_hour += hours;
114 tm->tm_min += minutes;
115 (void) mktime(tm);
116 info_hour = tm->tm_hour;
117 info_min = tm->tm_min;
119 sprintf(info,
120 "The system will shutdown in %d hour%s and %d minute%s at %02d:%02d\n\n",
121 hours,hours==1?"":"s",minutes,minutes==1?"":"s",info_hour,info_min);
123 wait_time += hours * 3600 + minutes * 60;
124 return;
127 int main(argc,argv)
128 int argc;
129 char *argv[];
131 int i, now = 0, nologin = 0, want_terminate = 0, want_message = 0, check = 0;
132 char *opt;
133 int tty;
134 static char HALT1[] = "-?";
135 static char *HALT[] = { "shutdown", HALT1, NULL, NULL };
137 /* Parse options. */
138 for (i = 1; i < argc && argv[i][0] == '-'; i++) {
139 if (argv[i][1] == '-' && argv[i][2] == 0) {
140 /* -- */
141 i++;
142 break;
144 for (opt = argv[i] + 1; *opt != 0; opt++) {
145 switch (*opt) {
146 case 'k':
147 want_terminate = 1;
148 break;
149 case 'h':
150 case 'r':
151 case 'x':
152 case 'd':
153 reboot_flag = *opt;
154 if (reboot_flag == 'x') {
155 if (*++opt == 0) {
156 if (++i == argc) {
157 fprintf (stderr,"shutdown: option '-x' requires an argument\n");
158 usage();
160 opt=argv[i];
162 reboot_code=opt;
163 opt+=strlen(opt)-1;
165 break;
166 case 'R':
167 reboot_flag = 'R';
168 break;
169 case 'm':
170 want_message = 1;
171 break;
172 case 'C':
173 check = 1;
174 break;
175 default:
176 fprintf (stderr,"shutdown: invalid option '-%c'\n",*opt);
177 usage();
178 break;
182 if ((argc - i) > 2) usage();
184 if (check) exit(crash_check() ? 0 : 2);
186 if (i == argc) {
187 /* No timespec, assume "now". */
188 now = 1;
189 } else {
190 if (!strcmp(argv[i], "now"))
191 now++;
192 else
193 parse_time(argv[i]);
196 if ((argc - i) == 2) {
197 /* One line message */
198 strcat(message, argv[i+1]);
199 strcat(message, "\n");
202 if (want_terminate) terminate();
203 if (want_message) get_message();
205 puts(info);
207 prog = strrchr(*argv,'/');
208 if (prog == (char *)0)
209 prog = *argv;
210 else
211 prog++;
213 if (!now) {
214 /* Daemonize. */
215 switch (fork()) {
216 case 0:
217 break;
218 case -1:
219 fprintf(stderr, "%s: can't fork\n", prog);
220 exit(1);
221 default:
222 exit(0);
224 /* Detach from the terminal (if any). */
225 if ((tty = open("/dev/tty", O_RDONLY)) != -1) {
226 close(tty);
227 setsid();
229 write_pid();
232 for (;;) {
233 if (wait_time <= 5 * 60 && !nologin && !now) {
234 close(creat(NOLOGIN,00644));
235 nologin = 1;
237 if (wait_time <= 60) break;
238 if(inform_user_time())
239 inform_user();
240 sleep (60);
241 wait_time -= 60;
244 if (!now) {
245 inform_user();
246 sleep (30); /* Last minute before shutdown */
247 wait_time -= 30;
248 inform_user();
249 sleep (30); /* Last 30 seconds before shutdown */
251 wait_time = 0;
252 inform_user();
254 unlink(SHUT_PID); /* No way of stopping anymore */
255 unlink(NOLOGIN);
257 HALT[1][1] = reboot_flag;
258 if (reboot_flag == 'x') HALT[2] = reboot_code;
259 #if __minix_vmd
260 execv("/usr/sbin/halt", HALT);
261 #else
262 execv("/usr/bin/halt", HALT);
263 #endif
264 if (errno != ENOENT)
265 fprintf(stderr, "Can't execute 'halt': %s\n", strerror(errno));
267 sleep(2);
268 reboot(RBT_HALT);
269 fprintf(stderr, "Reboot call failed: %s\n", strerror(errno));
271 return(1);
274 void usage()
276 fputs("Usage: shutdown [-hrRmkd] [-x code] [time [message]]\n", stderr);
277 fputs(" -h -> halt system after shutdown\n", stderr);
278 fputs(" -r -> reboot system after shutdown\n", stderr);
279 fputs(" -R -> reset system after shutdown\n", stderr);
280 fputs(" -x -> return to the monitor doing...\n", stderr);
281 fputs(" -d -> default CTRL-ALT-DEL shutdown for current bootloader\n", stderr);
282 fputs(" -m -> read a shutdown message from standard input\n", stderr);
283 fputs(" -k -> stop an already running shutdown\n", stderr);
284 fputs(" code -> boot monitor code to be executed\n", stderr);
285 fputs(" time -> keyword ``now'', minutes before shutdown ``+5'',\n", stderr);
286 fputs(" or absolute time specification ``11:20''\n", stderr);
287 fputs(" message -> short shutdown message\n", stderr);
288 exit(1);
291 void terminate()
293 FILE *in;
294 pid_t pid;
295 char c_pid[5];
297 in = fopen(SHUT_PID,"r");
298 if (in == (FILE *)0) {
299 fputs ("Can't get pid of shutdown process, probably not running shutdown\n", stderr);
300 exit(1);
302 fgets(c_pid,5,in);
303 fclose(in);
304 pid = atoi(c_pid);
305 if (kill(pid,9) == -1)
306 fputs("Can't kill the shutdown process, probably not running anymore\n",stderr);
307 else
308 puts("Shutdown process terminated");
309 unlink(SHUT_PID);
310 unlink(NOLOGIN);
311 exit(0);
314 void get_message()
316 char line[80];
318 puts ("Type your message. End with ^D at an empty line");
319 fputs ("shutdown> ",stdout);fflush(stdout);
320 while (fgets(line,80,stdin) != (char *)0) {
321 strcat (message,line);
322 bzero(line,strlen(line));
323 fputs ("shutdown> ",stdout);fflush(stdout);
325 putc('\n',stdout);fflush(stdout);
328 int inform_user_time()
330 int min;
332 min = wait_time /60;
334 if (min == 60 || min == 30 || min == 15 || min == 10 || min <= 5)
335 return 1;
336 else
337 return 0;
340 void inform_user()
342 int hour, minute;
343 char mes[80];
345 hour = 0;
346 minute = wait_time / 60;
347 while (minute >= 60) {
348 minute -= 60;
349 hour++;
352 if (hour)
353 sprintf(mes,
354 "\nThe system will shutdown in %d hour%s and %d minute%s at %.02d:%.02d\n\n",
355 hour,hour==1?"":"s",minute,minute==1?"":"s",info_hour,info_min);
356 else
357 if (minute > 1)
358 sprintf(mes,
359 "\nThe system will shutdown in %d minutes at %.02d:%.02d\n\n",
360 minute,info_hour,info_min);
361 else
362 if (wait_time > 1)
363 sprintf(mes,
364 "\nThe system will shutdown in %ld seconds\n\n",
365 wait_time);
366 else
367 sprintf(mes,
368 "\nThe system will shutdown NOW\n\n");
370 wall(mes,message);
373 void write_pid()
375 char pid[5];
376 int fd;
378 fd = creat(SHUT_PID,00600);
379 if (!fd)
380 return;
381 strncpy (pid,itoa(getpid()), sizeof(pid));
382 write (fd,pid,sizeof(pid));
383 close(fd);
384 return;
387 int crash_check()
389 struct utmp last;
390 int fd = -1, crashed;
391 struct stat st;
393 if (stat(STR_ROOT_WTMP, &st) < 0 || st.st_size == 0) {
394 if (stat(STR_WTMP, &st) < 0 || st.st_size == 0) {
395 return 0;
397 if ((fd = open(STR_WTMP, O_RDONLY)) < 0) return 0;
398 } else if ((fd = open(STR_ROOT_WTMP, O_RDONLY)) < 0) return 0;
400 crashed = (lseek(fd, - (off_t) sizeof(last), SEEK_END) == -1
401 || read(fd, (void *) &last, sizeof(last)) != sizeof(last)
402 || last.ut_line[0] != '~'
403 #ifdef __NBSD_LIBC
404 || (strncmp(last.ut_name, "shutdown", sizeof(last.ut_name))
405 && strncmp(last.ut_name, "halt", sizeof(last.ut_name)))
406 #else
407 || (strncmp(last.ut_user, "shutdown", sizeof(last.ut_user))
408 && strncmp(last.ut_user, "halt", sizeof(last.ut_user)))
409 #endif
411 close(fd);
412 return crashed;