2 * Copyright (c) 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#) Copyright (c) 1988, 1990, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)shutdown.c 8.4 (Berkeley) 4/28/95
31 * $FreeBSD: src/sbin/shutdown/shutdown.c,v 1.21.2.1 2001/07/30 10:38:08 dd Exp $
34 #include <sys/param.h>
36 #include <sys/resource.h>
37 #include <sys/syslog.h>
50 #include "pathnames.h"
54 #define _PATH_NOLOGIN "./nologin"
60 #define NOLOG_TIME 5*60
61 static struct interval
{
62 int timeleft
, timetowait
;
81 static time_t offset
, shuttime
;
82 static int dohalt
, dopower
, doreboot
, killflg
, mbuflen
, oflag
;
83 static char mbuf
[BUFSIZ
];
84 static const char *nosync
, *whom
;
86 static void badtime(void);
87 static void die_you_gravy_sucking_pig_dog(void);
88 static void finish(int);
89 static void getoffset(char *);
90 static void loop(void);
91 static void nolog(void);
92 static void timeout(int);
93 static void timewarn(int);
94 static void usage(const char *);
96 extern const char **environ
;
99 main(int argc
, char **argv
)
103 int arglen
, ch
, len
, readstdin
;
107 errx(1, "NOT super-user");
114 * Test for the special case where the utility is called as
115 * "poweroff", for which it runs 'shutdown -p now'.
117 if ((p
= strrchr(argv
[0], '/')) == NULL
)
121 if (strcmp(p
, "poweroff") == 0) {
122 if (getopt(argc
, argv
, "") != -1)
130 (void)time(&shuttime
);
134 while ((ch
= getopt(argc
, argv
, "-hknopr")) != -1)
167 if (killflg
+ doreboot
+ dohalt
+ dopower
> 1)
168 usage("incompatible switches -h, -k, -p and -r");
170 if (oflag
&& !(dohalt
|| dopower
|| doreboot
))
171 usage("-o requires -h, -p or -r");
173 if (nosync
!= NULL
&& !oflag
)
174 usage("-n requires -o");
180 for (p
= mbuf
, len
= sizeof(mbuf
); *argv
; ++argv
) {
181 arglen
= strlen(*argv
);
182 if ((len
-= arglen
) <= 2)
186 memmove(p
, *argv
, arglen
);
195 endp
= mbuf
+ sizeof(mbuf
) - 2;
197 if (!fgets(p
, endp
- p
+ 1, stdin
))
199 for (; *p
&& p
< endp
; ++p
);
207 mbuflen
= strlen(mbuf
);
210 printf("Shutdown at %.24s.\n", ctime(&shuttime
));
212 printf("Shutdown NOW!\n");
214 if (!(whom
= getlogin()))
215 whom
= (pw
= getpwuid(getuid())) ? pw
->pw_name
: "???";
220 setpriority(PRIO_PROCESS
, 0, PRIO_MIN
);
228 errx(0, "[pid %d]", forkpid
);
232 openlog("shutdown", LOG_CONS
, LOG_AUTH
);
244 if (offset
<= NOLOG_TIME
) {
251 if (tp
->timeleft
< offset
)
252 sleep((u_int
)(offset
- tp
->timeleft
));
254 while (tp
->timeleft
&& offset
< tp
->timeleft
)
257 * Warn now, if going to sleep more than a fifth of
258 * the next wait time.
260 if ((sltime
= offset
- tp
->timeleft
)) {
261 if (sltime
> (u_int
)(tp
->timetowait
/ 5))
267 timewarn(tp
->timeleft
);
268 if (!logged
&& tp
->timeleft
<= NOLOG_TIME
) {
272 sleep((u_int
)tp
->timetowait
);
276 die_you_gravy_sucking_pig_dog();
279 static jmp_buf alarmbuf
;
281 static const char *restricted_environ
[] = {
282 "PATH=" _PATH_STDPATH
,
287 timewarn(int timeleft
)
290 static char hostname
[MAXHOSTNAMELEN
+ 1];
292 char wcmd
[MAXPATHLEN
+ 4];
295 gethostname(hostname
, sizeof(hostname
));
297 /* undoc -n option to wall suppresses normal wall banner */
298 snprintf(wcmd
, sizeof(wcmd
), "%s -n", _PATH_WALL
);
299 environ
= restricted_environ
;
300 if (!(pf
= popen(wcmd
, "w"))) {
301 syslog(LOG_ERR
, "shutdown: can't find %s: %m", _PATH_WALL
);
306 "\007*** %sSystem shutdown message from %s@%s ***\007\n",
307 timeleft
? "": "FINAL ", whom
, hostname
);
309 if (timeleft
> 10*60)
310 fprintf(pf
, "System going down at %5.5s\n\n",
311 ctime(&shuttime
) + 11);
312 else if (timeleft
> 59)
313 fprintf(pf
, "System going down in %d minute%s\n\n",
314 timeleft
/ 60, (timeleft
> 60) ? "s" : "");
316 fprintf(pf
, "System going down in 30 seconds\n\n");
318 fprintf(pf
, "System going down IMMEDIATELY\n\n");
321 fwrite(mbuf
, sizeof(*mbuf
), mbuflen
, pf
);
324 * play some games, just in case wall doesn't come back
325 * probably unnecessary, given that wall is careful.
327 if (!setjmp(alarmbuf
)) {
328 signal(SIGALRM
, timeout
);
332 signal(SIGALRM
, SIG_DFL
);
337 timeout(int signo __unused
)
339 longjmp(alarmbuf
, 1);
343 die_you_gravy_sucking_pig_dog(void)
345 char *empty_environ
[] = { NULL
};
347 syslog(LOG_NOTICE
, "%s by %s: %s",
348 doreboot
? "reboot" : dohalt
? "halt" : dopower
? "power-down" :
349 "shutdown", whom
, mbuf
);
352 printf("\r\nSystem shutdown time has arrived\007\007\r\n");
354 printf("\rbut you'll have to do it yourself\r\n");
363 printf("power-down");
366 printf("\nkill -HUP 1\n");
369 kill(1, doreboot
? SIGINT
: /* reboot */
370 dohalt
? SIGUSR1
: /* halt */
371 dopower
? SIGUSR2
: /* power-down */
372 SIGTERM
); /* single-user */
375 execle(_PATH_REBOOT
, "reboot", "-l", nosync
,
376 NULL
, empty_environ
);
377 syslog(LOG_ERR
, "shutdown: can't exec %s: %m.",
382 execle(_PATH_HALT
, "halt", "-l", nosync
,
383 NULL
, empty_environ
);
384 syslog(LOG_ERR
, "shutdown: can't exec %s: %m.",
389 execle(_PATH_HALT
, "halt", "-l", "-p", nosync
,
390 NULL
, empty_environ
);
391 syslog(LOG_ERR
, "shutdown: can't exec %s: %m.",
395 kill(1, SIGTERM
); /* to single-user */
401 #define ATOI2(p) (p[0] - '0') * 10 + (p[1] - '0'); p += 2;
404 getoffset(char *timearg
)
413 if (!strcasecmp(timearg
, "now")) { /* now */
419 if (*timearg
== '+') { /* +minutes */
420 if (!isdigit(*++timearg
))
422 if ((offset
= atoi(timearg
) * 60) < 0)
424 shuttime
= now
+ offset
;
428 /* handle hh:mm by getting rid of the colon */
429 for (p
= timearg
; *p
; ++p
)
430 if (!isascii(*p
) || !isdigit(*p
)) {
431 if (*p
== ':' && strlen(p
) == 3) {
440 unsetenv("TZ"); /* OUR timezone */
441 lt
= localtime(&now
); /* current time val */
443 switch(strlen(timearg
)) {
445 this_year
= lt
->tm_year
;
446 lt
->tm_year
= ATOI2(timearg
);
448 * check if the specified year is in the next century.
449 * allow for one year of user error as many people will
450 * enter n - 1 at the start of year n.
452 if (lt
->tm_year
< (this_year
% 100) - 1)
454 /* adjust for the year 2000 and beyond */
455 lt
->tm_year
+= (this_year
- (this_year
% 100));
458 lt
->tm_mon
= ATOI2(timearg
);
459 if (--lt
->tm_mon
< 0 || lt
->tm_mon
> 11)
463 lt
->tm_mday
= ATOI2(timearg
);
464 if (lt
->tm_mday
< 1 || lt
->tm_mday
> 31)
468 lt
->tm_hour
= ATOI2(timearg
);
469 if (lt
->tm_hour
< 0 || lt
->tm_hour
> 23)
471 lt
->tm_min
= ATOI2(timearg
);
472 if (lt
->tm_min
< 0 || lt
->tm_min
> 59)
475 if ((shuttime
= mktime(lt
)) == -1)
477 if ((offset
= shuttime
- now
) < 0)
478 errx(1, "that time is already past.");
485 #define NOMSG "\n\nNO LOGINS: System going down at "
492 unlink(_PATH_NOLOGIN
); /* in case linked to another file */
493 signal(SIGINT
, finish
);
494 signal(SIGHUP
, finish
);
495 signal(SIGQUIT
, finish
);
496 signal(SIGTERM
, finish
);
497 if ((logfd
= open(_PATH_NOLOGIN
, O_WRONLY
|O_CREAT
|O_TRUNC
,
499 write(logfd
, NOMSG
, sizeof(NOMSG
) - 1);
500 ct
= ctime(&shuttime
);
501 write(logfd
, ct
+ 11, 5);
502 write(logfd
, "\n\n", 2);
503 write(logfd
, mbuf
, strlen(mbuf
));
509 finish(int signo __unused
)
512 unlink(_PATH_NOLOGIN
);
519 errx(1, "bad time format");
523 usage(const char *cp
)
528 "usage: shutdown [-] [-h | -p | -r | -k] [-o [-n]]"
529 " time [warning-message ...]\n"