]>
Commit | Line | Data |
---|---|---|
a74aeac6 PR |
1 | /* |
2 | * shutdown.c Shut the system down. | |
3 | * | |
4 | * Usage: shutdown [-krhfnc] time [warning message] | |
5 | * -k: don't really shutdown, only warn. | |
6 | * -r: reboot after shutdown. | |
7 | * -h: halt after shutdown. | |
8 | * -f: do a 'fast' reboot (skip fsck). | |
9 | * -F: Force fsck on reboot. | |
10 | * -n: do not go through init but do it ourselves. | |
11 | * -c: cancel an already running shutdown. | |
12 | * -t secs: delay between SIGTERM and SIGKILL for init. | |
13 | * | |
14 | * Author: Miquel van Smoorenburg, miquels@cistron.nl | |
15 | * | |
16 | * Version: @(#)shutdown 2.86-1 31-Jul-2004 miquels@cistron.nl | |
17 | * | |
18 | * This file is part of the sysvinit suite, | |
19 | * Copyright (C) 1991-2004 Miquel van Smoorenburg. | |
20 | * | |
21 | * This program is free software; you can redistribute it and/or modify | |
22 | * it under the terms of the GNU General Public License as published by | |
23 | * the Free Software Foundation; either version 2 of the License, or | |
24 | * (at your option) any later version. | |
25 | * | |
26 | * This program is distributed in the hope that it will be useful, | |
27 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
28 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
29 | * GNU General Public License for more details. | |
30 | * | |
31 | * You should have received a copy of the GNU General Public License | |
32 | * along with this program; if not, write to the Free Software | |
33 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
34 | */ | |
35 | #include <sys/types.h> | |
36 | #include <sys/stat.h> | |
37 | #include <sys/wait.h> | |
38 | #include <time.h> | |
39 | #include <string.h> | |
40 | #include <unistd.h> | |
41 | #include <errno.h> | |
42 | #include <stdlib.h> | |
43 | #include <stdio.h> | |
44 | #include <signal.h> | |
45 | #include <fcntl.h> | |
46 | #include <stdarg.h> | |
47 | #include <utmp.h> | |
48 | #include <syslog.h> | |
49 | #include "paths.h" | |
50 | #include "reboot.h" | |
51 | #include "initreq.h" | |
52 | ||
53 | char *Version = "@(#) shutdown 2.86-1 31-Jul-2004 miquels@cistron.nl"; | |
54 | ||
55 | #define MESSAGELEN 256 | |
56 | ||
57 | int dontshut = 0; /* Don't shutdown, only warn */ | |
58 | char down_level[2]; /* What runlevel to go to. */ | |
59 | int dosync = 1; /* Sync before reboot or halt */ | |
60 | int fastboot = 0; /* Do a 'fast' reboot */ | |
61 | int forcefsck = 0; /* Force fsck on reboot */ | |
62 | char message[MESSAGELEN]; /* Warning message */ | |
63 | char *sltime = 0; /* Sleep time */ | |
64 | char newstate[64]; /* What are we gonna do */ | |
65 | int doself = 0; /* Don't use init */ | |
66 | int got_alrm = 0; | |
67 | ||
68 | char *clean_env[] = { | |
69 | "HOME=/", | |
70 | "PATH=/bin:/usr/bin:/sbin:/usr/sbin", | |
71 | "TERM=dumb", | |
72 | NULL, | |
73 | }; | |
74 | ||
75 | /* From "wall.c" */ | |
76 | extern void wall(char *, int, int); | |
77 | ||
78 | /* From "utmp.c" */ | |
79 | extern void write_wtmp(char *user, char *id, int pid, int type, char *line); | |
80 | ||
81 | /* | |
82 | * Sleep without being interrupted. | |
83 | */ | |
84 | void hardsleep(int secs) | |
85 | { | |
86 | struct timespec ts, rem; | |
87 | ||
88 | ts.tv_sec = secs; | |
89 | ts.tv_nsec = 0; | |
90 | ||
91 | while(nanosleep(&ts, &rem) < 0 && errno == EINTR) | |
92 | ts = rem; | |
93 | } | |
94 | ||
95 | /* | |
96 | * Break off an already running shutdown. | |
97 | */ | |
98 | void stopit(int sig) | |
99 | { | |
100 | unlink(NOLOGIN); | |
101 | unlink(FASTBOOT); | |
102 | unlink(FORCEFSCK); | |
103 | unlink(SDPID); | |
104 | printf("\r\nShutdown cancelled.\r\n"); | |
105 | exit(0); | |
106 | } | |
107 | ||
108 | /* | |
109 | * Show usage message. | |
110 | */ | |
111 | void usage(void) | |
112 | { | |
113 | fprintf(stderr, | |
114 | "Usage:\t shutdown [-akrhHPfnc] [-t secs] time [warning message]\n" | |
115 | "\t\t -a: use /etc/shutdown.allow\n" | |
116 | "\t\t -k: don't really shutdown, only warn.\n" | |
117 | "\t\t -r: reboot after shutdown.\n" | |
118 | "\t\t -h: halt after shutdown.\n" | |
119 | "\t\t -P: halt action is to turn off power.\n" | |
120 | "\t\t -H: halt action is to just halt.\n" | |
121 | "\t\t -f: do a 'fast' reboot (skip fsck).\n" | |
122 | "\t\t -F: Force fsck on reboot.\n" | |
123 | "\t\t -n: do not go through \"init\" but go down real fast.\n" | |
124 | "\t\t -c: cancel a running shutdown.\n" | |
125 | "\t\t -t secs: delay between warning and kill signal.\n" | |
126 | "\t\t ** the \"time\" argument is mandatory! (try \"now\") **\n"); | |
127 | exit(1); | |
128 | } | |
129 | ||
130 | ||
131 | void alrm_handler(int sig) | |
132 | { | |
133 | got_alrm = sig; | |
134 | } | |
135 | ||
136 | ||
137 | /* | |
138 | * Set environment variables in the init process. | |
139 | */ | |
140 | int init_setenv(char *name, char *value) | |
141 | { | |
142 | struct init_request request; | |
143 | struct sigaction sa; | |
144 | int fd; | |
145 | int nl, vl; | |
146 | ||
147 | memset(&request, 0, sizeof(request)); | |
148 | request.magic = INIT_MAGIC; | |
149 | request.cmd = INIT_CMD_SETENV; | |
150 | nl = strlen(name); | |
151 | vl = value ? strlen(value) : 0; | |
152 | ||
192c4567 | 153 | if (nl + vl + 3 >= (int)sizeof(request.i.data)) |
a74aeac6 PR |
154 | return -1; |
155 | ||
156 | memcpy(request.i.data, name, nl); | |
157 | if (value) { | |
158 | request.i.data[nl] = '='; | |
159 | memcpy(request.i.data + nl + 1, value, vl); | |
160 | } | |
161 | ||
162 | /* | |
163 | * Open the fifo and write the command. | |
164 | * Make sure we don't hang on opening /dev/initctl | |
165 | */ | |
166 | memset(&sa, 0, sizeof(sa)); | |
167 | sa.sa_handler = alrm_handler; | |
168 | sigaction(SIGALRM, &sa, NULL); | |
169 | got_alrm = 0; | |
170 | alarm(3); | |
171 | if ((fd = open(INIT_FIFO, O_WRONLY)) >= 0 && | |
172 | write(fd, &request, sizeof(request)) == sizeof(request)) { | |
173 | close(fd); | |
174 | alarm(0); | |
175 | return 0; | |
176 | } | |
177 | ||
178 | fprintf(stderr, "shutdown: "); | |
179 | if (got_alrm) { | |
180 | fprintf(stderr, "timeout opening/writing control channel %s\n", | |
181 | INIT_FIFO); | |
182 | } else { | |
183 | perror(INIT_FIFO); | |
184 | } | |
185 | return -1; | |
186 | } | |
187 | ||
188 | ||
189 | /* | |
190 | * Tell everyone the system is going down in 'mins' minutes. | |
191 | */ | |
192 | void warn(int mins) | |
193 | { | |
194 | char buf[MESSAGELEN + sizeof(newstate)]; | |
195 | int len; | |
196 | ||
197 | buf[0] = 0; | |
198 | strncat(buf, message, sizeof(buf) - 1); | |
199 | len = strlen(buf); | |
200 | ||
201 | if (mins == 0) | |
202 | snprintf(buf + len, sizeof(buf) - len, | |
203 | "\rThe system is going down %s NOW!\r\n", | |
204 | newstate); | |
205 | else | |
206 | snprintf(buf + len, sizeof(buf) - len, | |
207 | "\rThe system is going DOWN %s in %d minute%s!\r\n", | |
208 | newstate, mins, mins == 1 ? "" : "s"); | |
209 | wall(buf, 1, 0); | |
210 | } | |
211 | ||
212 | /* | |
213 | * Create the /etc/nologin file. | |
214 | */ | |
215 | void donologin(int min) | |
216 | { | |
217 | FILE *fp; | |
218 | time_t t; | |
219 | ||
220 | time(&t); | |
221 | t += 60 * min; | |
222 | ||
223 | if ((fp = fopen(NOLOGIN, "w")) != NULL) { | |
224 | fprintf(fp, "\rThe system is going down on %s\r\n", ctime(&t)); | |
225 | if (message[0]) fputs(message, fp); | |
226 | fclose(fp); | |
227 | } | |
228 | } | |
229 | ||
230 | /* | |
231 | * Spawn an external program. | |
232 | */ | |
233 | int spawn(int noerr, char *prog, ...) | |
234 | { | |
235 | va_list ap; | |
236 | pid_t pid, rc; | |
237 | int i; | |
238 | char *argv[8]; | |
239 | ||
240 | i = 0; | |
241 | while ((pid = fork()) < 0 && i < 10) { | |
242 | perror("fork"); | |
243 | sleep(5); | |
244 | i++; | |
245 | } | |
246 | ||
247 | if (pid < 0) return -1; | |
248 | ||
249 | if (pid > 0) { | |
250 | while((rc = wait(&i)) != pid) | |
251 | if (rc < 0 && errno == ECHILD) | |
252 | break; | |
253 | return (rc == pid) ? WEXITSTATUS(i) : -1; | |
254 | } | |
255 | ||
256 | if (noerr) fclose(stderr); | |
257 | ||
258 | argv[0] = prog; | |
259 | va_start(ap, prog); | |
260 | for (i = 1; i < 7 && (argv[i] = va_arg(ap, char *)) != NULL; i++) | |
261 | ; | |
262 | argv[i] = NULL; | |
263 | va_end(ap); | |
264 | ||
265 | chdir("/"); | |
266 | environ = clean_env; | |
267 | ||
268 | execvp(argv[0], argv); | |
269 | perror(argv[0]); | |
270 | exit(1); | |
271 | ||
272 | /*NOTREACHED*/ | |
273 | return 0; | |
274 | } | |
275 | ||
276 | /* | |
277 | * Kill all processes, call /etc/init.d/halt (if present) | |
278 | */ | |
279 | void fastdown() | |
280 | { | |
281 | int do_halt = (down_level[0] == '0'); | |
282 | int i; | |
283 | #if 0 | |
284 | char cmd[128]; | |
285 | char *script; | |
286 | ||
287 | /* | |
288 | * Currently, the halt script is either init.d/halt OR rc.d/rc.0, | |
289 | * likewise for the reboot script. Test for the presence | |
290 | * of either. | |
291 | */ | |
292 | if (do_halt) { | |
293 | if (access(HALTSCRIPT1, X_OK) == 0) | |
294 | script = HALTSCRIPT1; | |
295 | else | |
296 | script = HALTSCRIPT2; | |
297 | } else { | |
298 | if (access(REBOOTSCRIPT1, X_OK) == 0) | |
299 | script = REBOOTSCRIPT1; | |
300 | else | |
301 | script = REBOOTSCRIPT2; | |
302 | } | |
303 | #endif | |
304 | ||
305 | /* First close all files. */ | |
306 | for(i = 0; i < 3; i++) | |
307 | if (!isatty(i)) { | |
308 | close(i); | |
309 | open("/dev/null", O_RDWR); | |
310 | } | |
311 | for(i = 3; i < 20; i++) close(i); | |
312 | close(255); | |
313 | ||
314 | /* First idle init. */ | |
315 | if (kill(1, SIGTSTP) < 0) { | |
316 | fprintf(stderr, "shutdown: can't idle init.\r\n"); | |
317 | exit(1); | |
318 | } | |
319 | ||
320 | /* Kill all processes. */ | |
321 | fprintf(stderr, "shutdown: sending all processes the TERM signal...\r\n"); | |
322 | kill(-1, SIGTERM); | |
323 | sleep(sltime ? atoi(sltime) : 3); | |
324 | fprintf(stderr, "shutdown: sending all processes the KILL signal.\r\n"); | |
325 | (void) kill(-1, SIGKILL); | |
326 | ||
327 | #if 0 | |
328 | /* See if we can run /etc/init.d/halt */ | |
329 | if (access(script, X_OK) == 0) { | |
330 | spawn(1, cmd, "fast", NULL); | |
331 | fprintf(stderr, "shutdown: %s returned - falling back " | |
332 | "on default routines\r\n", script); | |
333 | } | |
334 | #endif | |
335 | ||
336 | /* script failed or not present: do it ourself. */ | |
337 | sleep(1); /* Give init the chance to collect zombies. */ | |
338 | ||
339 | /* Record the fact that we're going down */ | |
340 | write_wtmp("shutdown", "~~", 0, RUN_LVL, "~~"); | |
341 | ||
342 | /* This is for those who have quota installed. */ | |
343 | spawn(1, "accton", NULL); | |
344 | spawn(1, "quotaoff", "-a", NULL); | |
345 | ||
346 | sync(); | |
347 | fprintf(stderr, "shutdown: turning off swap\r\n"); | |
348 | spawn(0, "swapoff", "-a", NULL); | |
349 | fprintf(stderr, "shutdown: unmounting all file systems\r\n"); | |
350 | spawn(0, "umount", "-a", NULL); | |
351 | ||
352 | /* We're done, halt or reboot now. */ | |
353 | if (do_halt) { | |
354 | fprintf(stderr, "The system is halted. Press CTRL-ALT-DEL " | |
355 | "or turn off power\r\n"); | |
356 | init_reboot(BMAGIC_HALT); | |
357 | exit(0); | |
358 | } | |
359 | ||
360 | fprintf(stderr, "Please stand by while rebooting the system.\r\n"); | |
361 | init_reboot(BMAGIC_REBOOT); | |
362 | exit(0); | |
363 | } | |
364 | ||
365 | /* | |
366 | * Go to runlevel 0, 1 or 6. | |
367 | */ | |
368 | void shutdown(char *halttype) | |
369 | { | |
370 | char *args[8]; | |
371 | int argp = 0; | |
372 | int do_halt = (down_level[0] == '0'); | |
373 | ||
374 | /* Warn for the last time */ | |
375 | warn(0); | |
376 | if (dontshut) { | |
377 | hardsleep(1); | |
378 | stopit(0); | |
379 | } | |
380 | openlog("shutdown", LOG_PID, LOG_USER); | |
381 | if (do_halt) | |
382 | syslog(LOG_NOTICE, "shutting down for system halt"); | |
383 | else | |
384 | syslog(LOG_NOTICE, "shutting down for system reboot"); | |
385 | closelog(); | |
386 | ||
387 | /* See if we have to do it ourself. */ | |
388 | if (doself) fastdown(); | |
389 | ||
390 | /* Create the arguments for init. */ | |
391 | args[argp++] = INIT; | |
392 | if (sltime) { | |
393 | args[argp++] = "-t"; | |
394 | args[argp++] = sltime; | |
395 | } | |
396 | args[argp++] = down_level; | |
397 | args[argp] = (char *)NULL; | |
398 | ||
399 | unlink(SDPID); | |
400 | unlink(NOLOGIN); | |
401 | ||
402 | /* Now execute init to change runlevel. */ | |
403 | sync(); | |
404 | init_setenv("INIT_HALT", halttype); | |
405 | execv(INIT, args); | |
406 | ||
407 | /* Oops - failed. */ | |
408 | fprintf(stderr, "\rshutdown: cannot execute %s\r\n", INIT); | |
409 | unlink(FASTBOOT); | |
410 | unlink(FORCEFSCK); | |
411 | init_setenv("INIT_HALT", NULL); | |
412 | openlog("shutdown", LOG_PID, LOG_USER); | |
413 | syslog(LOG_NOTICE, "shutdown failed"); | |
414 | closelog(); | |
415 | exit(1); | |
416 | } | |
417 | ||
418 | /* | |
419 | * returns if a warning is to be sent for wt | |
420 | */ | |
421 | static int needwarning(int wt) | |
422 | { | |
423 | int ret; | |
424 | ||
425 | if (wt < 10) | |
426 | ret = 1; | |
427 | else if (wt < 60) | |
428 | ret = (wt % 15 == 0); | |
429 | else if (wt < 180) | |
430 | ret = (wt % 30 == 0); | |
431 | else | |
432 | ret = (wt % 60 == 0); | |
433 | ||
434 | return ret; | |
435 | } | |
436 | ||
437 | /* | |
438 | * Main program. | |
439 | * Process the options and do the final countdown. | |
440 | */ | |
441 | int main(int argc, char **argv) | |
442 | { | |
443 | FILE *fp; | |
444 | extern int getopt(); | |
445 | extern int optind; | |
446 | struct sigaction sa; | |
447 | struct tm *lt; | |
448 | struct stat st; | |
449 | struct utmp *ut; | |
450 | time_t t; | |
451 | uid_t realuid; | |
452 | char *halttype; | |
453 | char *downusers[32]; | |
454 | char buf[128]; | |
455 | char term[UT_LINESIZE + 6]; | |
456 | char *sp; | |
457 | char *when = NULL; | |
458 | int c, i, wt; | |
459 | int hours, mins; | |
460 | int didnolog = 0; | |
461 | int cancel = 0; | |
462 | int useacl = 0; | |
463 | int pid = 0; | |
464 | int user_ok = 0; | |
465 | ||
466 | /* We can be installed setuid root (executable for a special group) */ | |
467 | realuid = getuid(); | |
468 | setuid(geteuid()); | |
469 | ||
470 | if (getuid() != 0) { | |
471 | fprintf(stderr, "shutdown: you must be root to do that!\n"); | |
472 | exit(1); | |
473 | } | |
474 | strcpy(down_level, "1"); | |
475 | halttype = NULL; | |
476 | ||
477 | /* Process the options. */ | |
478 | while((c = getopt(argc, argv, "HPacqkrhnfFyt:g:i:")) != EOF) { | |
479 | switch(c) { | |
480 | case 'H': | |
481 | halttype = "HALT"; | |
482 | break; | |
483 | case 'P': | |
484 | halttype = "POWERDOWN"; | |
485 | break; | |
486 | case 'a': /* Access control. */ | |
487 | useacl = 1; | |
488 | break; | |
489 | case 'c': /* Cancel an already running shutdown. */ | |
490 | cancel = 1; | |
491 | break; | |
492 | case 'k': /* Don't really shutdown, only warn.*/ | |
493 | dontshut = 1; | |
494 | break; | |
495 | case 'r': /* Automatic reboot */ | |
496 | down_level[0] = '6'; | |
497 | break; | |
498 | case 'h': /* Halt after shutdown */ | |
499 | down_level[0] = '0'; | |
500 | break; | |
501 | case 'f': /* Don't perform fsck after next boot */ | |
502 | fastboot = 1; | |
503 | break; | |
504 | case 'F': /* Force fsck after next boot */ | |
505 | forcefsck = 1; | |
506 | break; | |
507 | case 'n': /* Don't switch runlevels. */ | |
508 | doself = 1; | |
509 | break; | |
510 | case 't': /* Delay between TERM and KILL */ | |
511 | sltime = optarg; | |
512 | break; | |
513 | case 'y': /* Ignored for sysV compatibility */ | |
514 | break; | |
515 | case 'g': /* sysv style to specify time. */ | |
516 | when = optarg; | |
517 | break; | |
518 | case 'i': /* Level to go to. */ | |
519 | if (!strchr("0156aAbBcCsS", optarg[0])) { | |
520 | fprintf(stderr, | |
521 | "shutdown: `%s': bad runlevel\n", | |
522 | optarg); | |
523 | exit(1); | |
524 | } | |
525 | down_level[0] = optarg[0]; | |
526 | break; | |
527 | default: | |
528 | usage(); | |
529 | break; | |
530 | } | |
531 | } | |
532 | ||
533 | if (NULL != halttype && down_level[0] != '0') { | |
534 | fprintf(stderr, "shutdown: -H and -P flags can only be used along with -h flag.\n"); | |
535 | usage(); | |
536 | exit(1); | |
537 | } | |
538 | ||
539 | /* Do we need to use the shutdown.allow file ? */ | |
540 | if (useacl && (fp = fopen(SDALLOW, "r")) != NULL) { | |
541 | ||
542 | /* Read /etc/shutdown.allow. */ | |
543 | i = 0; | |
544 | while(fgets(buf, 128, fp)) { | |
545 | if (buf[0] == '#' || buf[0] == '\n') continue; | |
546 | if (i > 31) continue; | |
547 | for(sp = buf; *sp; sp++) if (*sp == '\n') *sp = 0; | |
548 | downusers[i++] = strdup(buf); | |
549 | } | |
550 | if (i < 32) downusers[i] = 0; | |
551 | fclose(fp); | |
552 | ||
553 | /* Now walk through /var/run/utmp to find logged in users. */ | |
554 | while(!user_ok && (ut = getutent()) != NULL) { | |
555 | ||
556 | /* See if this is a user process on a VC. */ | |
557 | if (ut->ut_type != USER_PROCESS) continue; | |
558 | sprintf(term, "/dev/%.*s", UT_LINESIZE, ut->ut_line); | |
559 | if (stat(term, &st) < 0) continue; | |
560 | #ifdef major /* glibc */ | |
561 | if (major(st.st_rdev) != 4 || | |
562 | minor(st.st_rdev) > 63) continue; | |
563 | #else | |
564 | if ((st.st_rdev & 0xFFC0) != 0x0400) continue; | |
565 | #endif | |
566 | /* Root is always OK. */ | |
567 | if (strcmp(ut->ut_user, "root") == 0) { | |
568 | user_ok++; | |
569 | break; | |
570 | } | |
571 | ||
572 | /* See if this is an allowed user. */ | |
573 | for(i = 0; i < 32 && downusers[i]; i++) | |
574 | if (!strncmp(downusers[i], ut->ut_user, | |
575 | UT_NAMESIZE)) { | |
576 | user_ok++; | |
577 | break; | |
578 | } | |
579 | } | |
580 | endutent(); | |
581 | ||
582 | /* See if user was allowed. */ | |
583 | if (!user_ok) { | |
584 | if ((fp = fopen(CONSOLE, "w")) != NULL) { | |
585 | fprintf(fp, "\rshutdown: no authorized users " | |
586 | "logged in.\r\n"); | |
587 | fclose(fp); | |
588 | } | |
589 | exit(1); | |
590 | } | |
591 | } | |
592 | ||
593 | /* Read pid of running shutdown from a file */ | |
594 | if ((fp = fopen(SDPID, "r")) != NULL) { | |
595 | fscanf(fp, "%d", &pid); | |
596 | fclose(fp); | |
597 | } | |
598 | ||
599 | /* Read remaining words, skip time if needed. */ | |
600 | message[0] = 0; | |
601 | for(c = optind + (!cancel && !when); c < argc; c++) { | |
602 | if (strlen(message) + strlen(argv[c]) + 4 > MESSAGELEN) | |
603 | break; | |
604 | strcat(message, argv[c]); | |
605 | strcat(message, " "); | |
606 | } | |
607 | if (message[0]) strcat(message, "\r\n"); | |
608 | ||
609 | /* See if we want to run or cancel. */ | |
610 | if (cancel) { | |
611 | if (pid <= 0) { | |
612 | fprintf(stderr, "shutdown: cannot find pid " | |
613 | "of running shutdown.\n"); | |
614 | exit(1); | |
615 | } | |
616 | init_setenv("INIT_HALT", NULL); | |
617 | if (kill(pid, SIGINT) < 0) { | |
618 | fprintf(stderr, "shutdown: not running.\n"); | |
619 | exit(1); | |
620 | } | |
621 | if (message[0]) wall(message, 1, 0); | |
622 | exit(0); | |
623 | } | |
624 | ||
625 | /* Check syntax. */ | |
626 | if (when == NULL) { | |
627 | if (optind == argc) usage(); | |
628 | when = argv[optind++]; | |
629 | } | |
630 | ||
631 | /* See if we are already running. */ | |
632 | if (pid > 0 && kill(pid, 0) == 0) { | |
633 | fprintf(stderr, "\rshutdown: already running.\r\n"); | |
634 | exit(1); | |
635 | } | |
636 | ||
637 | /* Extra check. */ | |
638 | if (doself && down_level[0] != '0' && down_level[0] != '6') { | |
639 | fprintf(stderr, | |
640 | "shutdown: can use \"-n\" for halt or reboot only.\r\n"); | |
641 | exit(1); | |
642 | } | |
643 | ||
644 | /* Tell users what we're gonna do. */ | |
645 | switch(down_level[0]) { | |
646 | case '0': | |
647 | strcpy(newstate, "for system halt"); | |
648 | break; | |
649 | case '6': | |
650 | strcpy(newstate, "for reboot"); | |
651 | break; | |
652 | case '1': | |
653 | strcpy(newstate, "to maintenance mode"); | |
654 | break; | |
655 | default: | |
656 | sprintf(newstate, "to runlevel %s", down_level); | |
657 | break; | |
658 | } | |
659 | ||
660 | /* Create a new PID file. */ | |
661 | unlink(SDPID); | |
662 | umask(022); | |
663 | if ((fp = fopen(SDPID, "w")) != NULL) { | |
664 | fprintf(fp, "%d\n", getpid()); | |
665 | fclose(fp); | |
666 | } else if (errno != EROFS) | |
667 | fprintf(stderr, "shutdown: warning: cannot open %s\n", SDPID); | |
668 | ||
669 | /* | |
670 | * Catch some common signals. | |
671 | */ | |
672 | signal(SIGQUIT, SIG_IGN); | |
673 | signal(SIGCHLD, SIG_IGN); | |
674 | signal(SIGHUP, SIG_IGN); | |
675 | signal(SIGTSTP, SIG_IGN); | |
676 | signal(SIGTTIN, SIG_IGN); | |
677 | signal(SIGTTOU, SIG_IGN); | |
678 | ||
679 | memset(&sa, 0, sizeof(sa)); | |
680 | sa.sa_handler = stopit; | |
681 | sigaction(SIGINT, &sa, NULL); | |
682 | ||
683 | /* Go to the root directory */ | |
684 | chdir("/"); | |
685 | if (fastboot) close(open(FASTBOOT, O_CREAT | O_RDWR, 0644)); | |
686 | if (forcefsck) close(open(FORCEFSCK, O_CREAT | O_RDWR, 0644)); | |
687 | ||
688 | /* Alias now and take care of old '+mins' notation. */ | |
689 | if (!strcmp(when, "now")) strcpy(when, "0"); | |
690 | if (when[0] == '+') when++; | |
691 | ||
692 | /* Decode shutdown time. */ | |
693 | for (sp = when; *sp; sp++) { | |
694 | if (*sp != ':' && (*sp < '0' || *sp > '9')) | |
695 | usage(); | |
696 | } | |
697 | if (strchr(when, ':') == NULL) { | |
698 | /* Time in minutes. */ | |
699 | wt = atoi(when); | |
700 | if (wt == 0 && when[0] != '0') usage(); | |
701 | } else { | |
702 | /* Time in hh:mm format. */ | |
703 | if (sscanf(when, "%d:%2d", &hours, &mins) != 2) usage(); | |
704 | if (hours > 23 || mins > 59) usage(); | |
705 | time(&t); | |
706 | lt = localtime(&t); | |
707 | wt = (60*hours + mins) - (60*lt->tm_hour + lt->tm_min); | |
708 | if (wt < 0) wt += 1440; | |
709 | } | |
710 | /* Shutdown NOW if time == 0 */ | |
711 | if (wt == 0) shutdown(halttype); | |
712 | ||
713 | /* Give warnings on regular intervals and finally shutdown. */ | |
714 | if (wt < 15 && !needwarning(wt)) warn(wt); | |
715 | while(wt) { | |
716 | if (wt <= 5 && !didnolog) { | |
717 | donologin(wt); | |
718 | didnolog++; | |
719 | } | |
720 | if (needwarning(wt)) warn(wt); | |
721 | hardsleep(60); | |
722 | wt--; | |
723 | } | |
724 | shutdown(halttype); | |
725 | ||
726 | return 0; /* Never happens */ | |
727 | } |