umount: getopt return value is int, not char
[minix.git] / commands / reboot / shutdown.c
blobc5e40873ae899b0e1ade888159d368887d435013
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 -R: reset the system
22 -d: default CTRL-ALT-DEL shutdown for current bootloader
25 #define _POSIX_SOURCE 1
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <stdio.h>
29 #include <ctype.h>
30 #include <fcntl.h>
31 #include <time.h>
32 #include <stdlib.h>
33 #include <signal.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <utmp.h>
37 #include <errno.h>
39 #include "wtmp.h"
41 static char SHUT_PID[] = "/usr/run/shutdown.pid";
42 static char NOLOGIN[] = "/etc/nologin";
44 #ifndef __STDC__
45 #define inform_user_time inf_time
46 #define inform_user inf_user
47 #endif
49 void usage( void );
50 void write_pid( void );
51 int inform_user_time( void );
52 void inform_user( void );
53 void terminate( void );
54 void wall( char *when, char *extra );
55 int crash_check( void );
56 void parse_time( char *arg );
57 void get_message( void );
58 int main( int argc, char *argv[] );
59 char *itoa( int n );
61 long wait_time=0L;
62 char message[1024];
63 char info[80];
64 int reboot_flag='h'; /* default is halt */
65 int info_min, info_hour;
66 char *prog;
68 void parse_time (arg)
69 char *arg;
71 char *p = arg;
72 int hours, minutes;
73 time_t now;
74 struct tm *tm;
75 int delta = 0;
76 int bad = 0;
78 if (p[0] == '+') { delta = 1; p++; }
80 hours = strtoul(p, &p, 10);
81 if (*p == 0 && delta) {
82 minutes = hours;
83 hours = 0;
84 } else {
85 if (*p != ':' && *p != '.')
86 bad = 1;
87 else
88 p++;
89 minutes = strtoul(p, &p, 10);
90 if (*p != 0) bad = 1;
92 if (bad) {
93 fprintf(stderr,"Invalid time specification `%s'\n",arg);
94 usage();
97 time(&now);
98 tm = localtime(&now);
100 if (!delta) {
101 hours -= tm->tm_hour;
102 minutes -= tm->tm_min;
105 if (minutes < 0) {
106 minutes += 60;
107 hours--;
109 if (hours < 0) hours += 24; /* Time after midnight. */
111 tm->tm_hour += hours;
112 tm->tm_min += minutes;
113 (void) mktime(tm);
114 info_hour = tm->tm_hour;
115 info_min = tm->tm_min;
117 sprintf(info,
118 "The system will shutdown in %d hour%s and %d minute%s at %02d:%02d\n\n",
119 hours,hours==1?"":"s",minutes,minutes==1?"":"s",info_hour,info_min);
121 wait_time += hours * 3600 + minutes * 60;
122 return;
125 int main(argc,argv)
126 int argc;
127 char *argv[];
129 int i, now = 0, nologin = 0, want_terminate = 0, want_message = 0, check = 0;
130 char *opt;
131 int tty;
132 static char HALT1[] = "-?";
133 static char *HALT[] = { "shutdown", HALT1, NULL };
135 /* Parse options. */
136 for (i = 1; i < argc && argv[i][0] == '-'; i++) {
137 if (argv[i][1] == '-' && argv[i][2] == 0) {
138 /* -- */
139 i++;
140 break;
142 for (opt = argv[i] + 1; *opt != 0; opt++) {
143 switch (*opt) {
144 case 'k':
145 want_terminate = 1;
146 break;
147 case 'h':
148 case 'r':
149 case 'p':
150 case 'd':
151 case 'R':
152 reboot_flag = *opt;
153 break;
154 case 'm':
155 want_message = 1;
156 break;
157 case 'C':
158 check = 1;
159 break;
160 default:
161 fprintf (stderr,"shutdown: invalid option '-%c'\n",*opt);
162 usage();
163 break;
167 if ((argc - i) > 2) usage();
169 if (check) exit(crash_check() ? 0 : 2);
171 if (i == argc) {
172 /* No timespec, assume "now". */
173 now = 1;
174 } else {
175 if (!strcmp(argv[i], "now"))
176 now++;
177 else
178 parse_time(argv[i]);
181 if ((argc - i) == 2) {
182 /* One line message */
183 strcat(message, argv[i+1]);
184 strcat(message, "\n");
187 if (want_terminate) terminate();
188 if (want_message) get_message();
190 puts(info);
192 prog = strrchr(*argv,'/');
193 if (prog == (char *)0)
194 prog = *argv;
195 else
196 prog++;
198 if (!now) {
199 /* Daemonize. */
200 switch (fork()) {
201 case 0:
202 break;
203 case -1:
204 fprintf(stderr, "%s: can't fork\n", prog);
205 exit(1);
206 default:
207 exit(0);
209 /* Detach from the terminal (if any). */
210 if ((tty = open("/dev/tty", O_RDONLY)) != -1) {
211 close(tty);
212 setsid();
214 write_pid();
217 for (;;) {
218 if (wait_time <= 5 * 60 && !nologin && !now) {
219 close(creat(NOLOGIN,00644));
220 nologin = 1;
222 if (wait_time <= 60) break;
223 if(inform_user_time())
224 inform_user();
225 sleep (60);
226 wait_time -= 60;
229 if (!now) {
230 inform_user();
231 sleep (30); /* Last minute before shutdown */
232 wait_time -= 30;
233 inform_user();
234 sleep (30); /* Last 30 seconds before shutdown */
236 wait_time = 0;
237 inform_user();
239 unlink(SHUT_PID); /* No way of stopping anymore */
240 unlink(NOLOGIN);
242 HALT[1][1] = reboot_flag;
243 #if __minix_vmd
244 execv("/usr/sbin/halt", HALT);
245 #else
246 execv("/usr/bin/halt", HALT);
247 #endif
248 if (errno != ENOENT)
249 fprintf(stderr, "Can't execute 'halt': %s\n", strerror(errno));
251 sleep(2);
252 reboot(RBT_HALT);
253 fprintf(stderr, "Reboot call failed: %s\n", strerror(errno));
255 return(1);
258 void usage()
260 fputs("Usage: shutdown [-hrRpmkd] [time [message]]\n", stderr);
261 fputs(" -h -> halt system after shutdown\n", stderr);
262 fputs(" -r -> reboot system after shutdown\n", stderr);
263 fputs(" -R -> reset system after shutdown\n", stderr);
264 fputs(" -p -> power system off after shutdown\n", stderr);
265 fputs(" -d -> default CTRL-ALT-DEL shutdown for current bootloader\n", stderr);
266 fputs(" -m -> read a shutdown message from standard input\n", stderr);
267 fputs(" -k -> stop an already running shutdown\n", stderr);
268 fputs(" time -> keyword ``now'', minutes before shutdown ``+5'',\n", stderr);
269 fputs(" or absolute time specification ``11:20''\n", stderr);
270 fputs(" message -> short shutdown message\n", stderr);
271 exit(1);
274 void terminate()
276 FILE *in;
277 pid_t pid;
278 char c_pid[5];
280 in = fopen(SHUT_PID,"r");
281 if (in == (FILE *)0) {
282 fputs ("Can't get pid of shutdown process, probably not running shutdown\n", stderr);
283 exit(1);
285 fgets(c_pid,5,in);
286 fclose(in);
287 pid = atoi(c_pid);
288 if (kill(pid,9) == -1)
289 fputs("Can't kill the shutdown process, probably not running anymore\n",stderr);
290 else
291 puts("Shutdown process terminated");
292 unlink(SHUT_PID);
293 unlink(NOLOGIN);
294 exit(0);
297 void get_message()
299 char line[80];
301 puts ("Type your message. End with ^D at an empty line");
302 fputs ("shutdown> ",stdout);fflush(stdout);
303 while (fgets(line,80,stdin) != (char *)0) {
304 strcat (message,line);
305 bzero(line,strlen(line));
306 fputs ("shutdown> ",stdout);fflush(stdout);
308 putc('\n',stdout);fflush(stdout);
311 int inform_user_time()
313 int min;
315 min = wait_time /60;
317 if (min == 60 || min == 30 || min == 15 || min == 10 || min <= 5)
318 return 1;
319 else
320 return 0;
323 void inform_user()
325 int hour, minute;
326 char mes[80];
328 hour = 0;
329 minute = wait_time / 60;
330 while (minute >= 60) {
331 minute -= 60;
332 hour++;
335 if (hour)
336 sprintf(mes,
337 "\nThe system will shutdown in %d hour%s and %d minute%s at %.02d:%.02d\n\n",
338 hour,hour==1?"":"s",minute,minute==1?"":"s",info_hour,info_min);
339 else
340 if (minute > 1)
341 sprintf(mes,
342 "\nThe system will shutdown in %d minutes at %.02d:%.02d\n\n",
343 minute,info_hour,info_min);
344 else
345 if (wait_time > 1)
346 sprintf(mes,
347 "\nThe system will shutdown in %ld seconds\n\n",
348 wait_time);
349 else
350 sprintf(mes,
351 "\nThe system will shutdown NOW\n\n");
353 wall(mes,message);
356 void write_pid()
358 char pid[5];
359 int fd;
361 fd = creat(SHUT_PID,00600);
362 if (!fd)
363 return;
364 strncpy (pid,itoa(getpid()), sizeof(pid));
365 write (fd,pid,sizeof(pid));
366 close(fd);
367 return;
370 int crash_check()
372 struct utmp last;
373 int fd = -1, crashed;
374 struct stat st;
376 if (stat(STR_ROOT_WTMP, &st) < 0 || st.st_size == 0) {
377 if (stat(STR_WTMP, &st) < 0 || st.st_size == 0) {
378 return 0;
380 if ((fd = open(STR_WTMP, O_RDONLY)) < 0) return 0;
381 } else if ((fd = open(STR_ROOT_WTMP, O_RDONLY)) < 0) return 0;
383 crashed = (lseek(fd, - (off_t) sizeof(last), SEEK_END) == -1
384 || read(fd, (void *) &last, sizeof(last)) != sizeof(last)
385 || last.ut_line[0] != '~'
386 #ifdef __NBSD_LIBC
387 || (strncmp(last.ut_name, "shutdown", sizeof(last.ut_name))
388 && strncmp(last.ut_name, "halt", sizeof(last.ut_name)))
389 #else
390 || (strncmp(last.ut_user, "shutdown", sizeof(last.ut_user))
391 && strncmp(last.ut_user, "halt", sizeof(last.ut_user)))
392 #endif
394 close(fd);
395 return crashed;