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