1 /* $NetBSD: shutdown.c,v 1.50 2008/02/09 04:09:22 dholland Exp $ */
4 * Copyright (c) 1988, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1988, 1990, 1993\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)shutdown.c 8.4 (Berkeley) 4/28/95";
42 __RCSID("$NetBSD: shutdown.c,v 1.50 2008/02/09 04:09:22 dholland Exp $");
46 #include <sys/param.h>
48 #include <sys/resource.h>
49 #include <sys/syslog.h>
65 #include "pathnames.h"
69 #define _PATH_NOLOGIN "./nologin"
71 #define _PATH_FASTBOOT "./fastboot"
77 #define NOLOG_TIME 5*60
78 static const struct interval
{
79 time_t timeleft
, timetowait
;
81 { 10 H
, 5 H
}, { 5 H
, 3 H
}, { 2 H
, 1 H
}, { 1 H
, 30 M
},
82 { 30 M
, 10 M
}, { 20 M
, 10 M
}, { 10 M
, 5 M
}, { 5 M
, 3 M
},
83 { 2 M
, 1 M
}, { 1 M
, 30 S
}, { 30 S
, 30 S
},
90 static time_t offset
, shuttime
;
91 static int dofast
, dohalt
, doreboot
, killflg
, nofork
, nosync
, dodump
;
92 static size_t mbuflen
;
93 static int dopowerdown
;
94 static const char *whom
;
95 static char mbuf
[BUFSIZ
];
98 static void badtime(void) __dead
;
99 static void die_you_gravy_sucking_pig_dog(void) __dead
;
100 static void doitfast(void);
101 void dorcshutdown(void);
102 static void finish(int) __dead
;
103 static void getoffset(char *);
104 static void loop(void);
105 static void nolog(void);
106 static void timeout(int);
107 static void timewarn(time_t);
108 static void usage(void) __dead
;
111 main(int argc
, char *argv
[])
118 (void)setprogname(argv
[0]);
121 errx(1, "%s: Not super-user", strerror(EPERM
));
123 while ((ch
= getopt(argc
, argv
, "b:Ddfhknpr")) != -1)
162 if (dodump
&& !dohalt
&& !doreboot
)
165 if (dofast
&& nosync
) {
166 warnx("Incompatible options -f and -n");
169 if (dohalt
&& doreboot
) {
170 const char *which_flag
= dopowerdown
? "p" : "h";
172 warnx("Incompatible options -%s and -r", which_flag
);
179 if (strcmp(argv
[0], "-") || argv
[1]) {
180 for (p
= mbuf
, len
= sizeof(mbuf
); *argv
; ++argv
) {
181 arglen
= strlen(*argv
);
182 if ((len
-= arglen
) <= 2)
186 (void)memmove(p
, *argv
, arglen
);
193 endp
= mbuf
+ sizeof(mbuf
) - 2;
195 if (!fgets(p
, endp
- p
+ 1, stdin
))
197 for (; *p
&& p
< endp
; ++p
);
206 mbuflen
= strlen(mbuf
);
209 (void)printf("Shutdown at %.24s.\n", ctime(&shuttime
));
211 (void)printf("Shutdown NOW!\n");
213 if (!(whom
= getlogin()))
214 whom
= (pw
= getpwuid(getuid())) ? pw
->pw_name
: "???";
217 (void)putc('\n', stdout
);
219 (void)setpriority(PRIO_PROCESS
, 0, PRIO_MIN
);
225 perror("shutdown: fork");
229 (void)printf("shutdown: [pid %d]\n", forkpid
);
235 openlog("shutdown", LOG_CONS
, LOG_AUTH
);
246 const struct interval
*tp
;
250 if (offset
<= NOLOG_TIME
) {
257 if (tp
->timeleft
< offset
)
258 (void)sleep((u_int
)(offset
- tp
->timeleft
));
260 while (offset
< tp
->timeleft
)
263 * Warn now, if going to sleep more than a fifth of
264 * the next wait time.
266 if ((sltime
= offset
- tp
->timeleft
) != 0) {
267 if (sltime
> tp
->timetowait
/ 5)
273 timewarn(tp
->timeleft
);
274 if (!logged
&& tp
->timeleft
<= NOLOG_TIME
) {
278 (void)sleep((u_int
)tp
->timetowait
);
282 die_you_gravy_sucking_pig_dog();
285 static jmp_buf alarmbuf
;
288 timewarn(time_t timeleft
)
291 static char hostname
[MAXHOSTNAMELEN
+ 1];
293 char wcmd
[MAXPATHLEN
+ 4];
296 (void)gethostname(hostname
, sizeof(hostname
));
297 hostname
[sizeof(hostname
) - 1] = '\0';
300 /* undoc -n option to wall suppresses normal wall banner */
301 (void)snprintf(wcmd
, sizeof wcmd
, "%s -n", _PATH_WALL
);
302 if ((pf
= popen(wcmd
, "w")) == NULL
) {
303 syslog(LOG_ERR
, "%s: Can't find `%s' (%m)", getprogname(),
309 "\007*** %sSystem shutdown message from %s@%s ***\007\n",
310 timeleft
? "": "FINAL ", whom
, hostname
);
312 if (timeleft
> 10*60)
313 (void)fprintf(pf
, "System going down at %5.5s\n\n",
314 ctime(&shuttime
) + 11);
315 else if (timeleft
> 59)
316 (void)fprintf(pf
, "System going down in %ld minute%s\n\n",
317 (long)timeleft
/ 60, (timeleft
> 60) ? "s" : "");
319 (void)fprintf(pf
, "System going down in 30 seconds\n\n");
321 (void)fprintf(pf
, "System going down IMMEDIATELY\n\n");
324 (void)fwrite(mbuf
, sizeof(*mbuf
), mbuflen
, pf
);
327 * play some games, just in case wall doesn't come back
328 * probably unnecessary, given that wall is careful.
330 if (!setjmp(alarmbuf
)) {
331 (void)signal(SIGALRM
, timeout
);
332 (void)alarm((u_int
)30);
334 (void)alarm((u_int
)0);
335 (void)signal(SIGALRM
, SIG_DFL
);
343 longjmp(alarmbuf
, 1);
347 die_you_gravy_sucking_pig_dog(void)
353 } else if (dohalt
&& dopowerdown
) {
361 syslog(LOG_NOTICE
, "%s by %s: %s", what
, whom
, mbuf
);
364 (void)printf("\r\nSystem shutdown time has arrived\007\007\r\n");
366 (void)printf("\rbut you'll have to do it yourself\r\n");
372 if (doreboot
|| dohalt
) {
373 const char *args
[16];
374 const char **arg
, *path
;
398 (void)execve(path
, __UNCONST(args
), NULL
);
400 syslog(LOG_ERR
, "Can't exec `%s' (%m)", path
);
402 warn("Can't exec `%s'", path
);
405 for (arg
= &args
[0]; *arg
; arg
++)
411 (void)kill(1, SIGTERM
); /* to single user */
419 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
422 getoffset(char *timearg
)
430 if (!strcasecmp(timearg
, "now")) { /* now */
436 if (*timearg
== '+') { /* +minutes */
437 if (!isdigit((unsigned char)*++timearg
))
439 offset
= atoi(timearg
) * 60;
440 shuttime
= now
+ offset
;
444 /* handle hh:mm by getting rid of the colon */
445 for (p
= timearg
; *p
; ++p
)
446 if (!isascii(*p
) || !isdigit((unsigned char)*p
)) {
447 if (*p
== ':' && strlen(p
) == 3) {
456 (void)unsetenv("TZ"); /* OUR timezone */
457 lt
= localtime(&now
); /* current time val */
462 switch (strlen(timearg
)) {
464 lt
->tm_year
= ATOI2(timearg
) * 100 - TM_YEAR_BASE
;
469 lt
->tm_year
+= ATOI2(timearg
);
471 yearset
= ATOI2(timearg
);
473 lt
->tm_year
= yearset
+ 2000 - TM_YEAR_BASE
;
475 lt
->tm_year
= yearset
+ 1900 - TM_YEAR_BASE
;
479 lt
->tm_mon
= ATOI2(timearg
);
483 lt
->tm_mday
= ATOI2(timearg
);
486 lt
->tm_hour
= ATOI2(timearg
);
489 lt
->tm_min
= ATOI2(timearg
);
495 if ((shuttime
= mktime(lt
)) == -1)
497 if ((offset
= shuttime
- now
) < 0)
498 errx(1, "time is already past");
504 (void)printf("\r\nAbout to run shutdown hooks...\r\n");
507 (void)system(". " _PATH_RCSHUTDOWN
);
509 (void)sleep(5); /* Give operator a chance to abort this. */
510 (void)printf("\r\nDone running shutdown hooks.\r\n");
513 #define FSMSG "fastboot file for fsck\n"
519 if ((fastfd
= open(_PATH_FASTBOOT
, O_WRONLY
|O_CREAT
|O_TRUNC
,
521 (void)write(fastfd
, FSMSG
, sizeof(FSMSG
) - 1);
526 #define NOMSG "\n\nNO LOGINS: System going down at "
533 (void)unlink(_PATH_NOLOGIN
); /* in case linked to another file */
534 (void)signal(SIGINT
, finish
);
535 (void)signal(SIGHUP
, finish
);
536 (void)signal(SIGQUIT
, finish
);
537 (void)signal(SIGTERM
, finish
);
538 if ((logfd
= open(_PATH_NOLOGIN
, O_WRONLY
|O_CREAT
|O_TRUNC
,
540 (void)write(logfd
, NOMSG
, sizeof(NOMSG
) - 1);
541 ct
= ctime(&shuttime
);
542 (void)write(logfd
, ct
+ 11, 5);
543 (void)write(logfd
, "\n\n", 2);
544 (void)write(logfd
, mbuf
, strlen(mbuf
));
555 (void)unlink(_PATH_NOLOGIN
);
563 warnx("illegal time format");
571 (void)fprintf(stderr
,
572 "Usage: %s [-Ddfhknpr] time [message ... | -]\n",