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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#) Copyright (c) 1988, 1990, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)shutdown.c 8.4 (Berkeley) 4/28/95
35 * $FreeBSD: src/sbin/shutdown/shutdown.c,v 1.21.2.1 2001/07/30 10:38:08 dd Exp $
36 * $DragonFly: src/sbin/shutdown/shutdown.c,v 1.6 2005/01/02 01:22:49 cpressey Exp $
39 #include <sys/param.h>
41 #include <sys/resource.h>
42 #include <sys/syslog.h>
55 #include "pathnames.h"
59 #define _PATH_NOLOGIN "./nologin"
65 #define NOLOG_TIME 5*60
67 int timeleft
, timetowait
;
86 static time_t offset
, shuttime
;
87 static int dohalt
, dopower
, doreboot
, killflg
, mbuflen
, oflag
;
88 static char mbuf
[BUFSIZ
];
89 static const char *nosync
, *whom
;
92 void die_you_gravy_sucking_pig_dog(void);
94 void getoffset(char *);
99 void usage(const char *);
102 main(int argc
, char **argv
)
106 int arglen
, ch
, len
, readstdin
;
110 errx(1, "NOT super-user");
114 while ((ch
= getopt(argc
, argv
, "-hknopr")) != -1)
147 if (killflg
+ doreboot
+ dohalt
+ dopower
> 1)
148 usage("incompatible switches -h, -k, -p and -r");
150 if (oflag
&& !(dohalt
|| dopower
|| doreboot
))
151 usage("-o requires -h, -p or -r");
153 if (nosync
!= NULL
&& !oflag
)
154 usage("-n requires -o");
159 for (p
= mbuf
, len
= sizeof(mbuf
); *argv
; ++argv
) {
160 arglen
= strlen(*argv
);
161 if ((len
-= arglen
) <= 2)
165 memmove(p
, *argv
, arglen
);
174 endp
= mbuf
+ sizeof(mbuf
) - 2;
176 if (!fgets(p
, endp
- p
+ 1, stdin
))
178 for (; *p
&& p
< endp
; ++p
);
186 mbuflen
= strlen(mbuf
);
189 printf("Shutdown at %.24s.\n", ctime(&shuttime
));
191 printf("Shutdown NOW!\n");
193 if (!(whom
= getlogin()))
194 whom
= (pw
= getpwuid(getuid())) ? pw
->pw_name
: "???";
199 setpriority(PRIO_PROCESS
, 0, PRIO_MIN
);
207 errx(0, "[pid %d]", forkpid
);
211 openlog("shutdown", LOG_CONS
, LOG_AUTH
);
223 if (offset
<= NOLOG_TIME
) {
230 if (tp
->timeleft
< offset
)
231 sleep((u_int
)(offset
- tp
->timeleft
));
233 while (tp
->timeleft
&& offset
< tp
->timeleft
)
236 * Warn now, if going to sleep more than a fifth of
237 * the next wait time.
239 if ((sltime
= offset
- tp
->timeleft
)) {
240 if (sltime
> (u_int
)(tp
->timetowait
/ 5))
246 timewarn(tp
->timeleft
);
247 if (!logged
&& tp
->timeleft
<= NOLOG_TIME
) {
251 sleep((u_int
)tp
->timetowait
);
255 die_you_gravy_sucking_pig_dog();
258 static jmp_buf alarmbuf
;
260 static const char *restricted_environ
[] = {
261 "PATH=" _PATH_STDPATH
,
265 extern const char **environ
;
268 timewarn(int timeleft
)
271 static char hostname
[MAXHOSTNAMELEN
+ 1];
273 char wcmd
[MAXPATHLEN
+ 4];
276 gethostname(hostname
, sizeof(hostname
));
278 /* undoc -n option to wall suppresses normal wall banner */
279 snprintf(wcmd
, sizeof(wcmd
), "%s -n", _PATH_WALL
);
280 environ
= restricted_environ
;
281 if (!(pf
= popen(wcmd
, "w"))) {
282 syslog(LOG_ERR
, "shutdown: can't find %s: %m", _PATH_WALL
);
287 "\007*** %sSystem shutdown message from %s@%s ***\007\n",
288 timeleft
? "": "FINAL ", whom
, hostname
);
290 if (timeleft
> 10*60)
291 fprintf(pf
, "System going down at %5.5s\n\n",
292 ctime(&shuttime
) + 11);
293 else if (timeleft
> 59)
294 fprintf(pf
, "System going down in %d minute%s\n\n",
295 timeleft
/ 60, (timeleft
> 60) ? "s" : "");
297 fprintf(pf
, "System going down in 30 seconds\n\n");
299 fprintf(pf
, "System going down IMMEDIATELY\n\n");
302 fwrite(mbuf
, sizeof(*mbuf
), mbuflen
, pf
);
305 * play some games, just in case wall doesn't come back
306 * probably unnecessary, given that wall is careful.
308 if (!setjmp(alarmbuf
)) {
309 signal(SIGALRM
, timeout
);
313 signal(SIGALRM
, SIG_DFL
);
318 timeout(int signo __unused
)
320 longjmp(alarmbuf
, 1);
324 die_you_gravy_sucking_pig_dog(void)
326 char *empty_environ
[] = { NULL
};
328 syslog(LOG_NOTICE
, "%s by %s: %s",
329 doreboot
? "reboot" : dohalt
? "halt" : dopower
? "power-down" :
330 "shutdown", whom
, mbuf
);
333 printf("\r\nSystem shutdown time has arrived\007\007\r\n");
335 printf("\rbut you'll have to do it yourself\r\n");
344 printf("power-down");
347 printf("\nkill -HUP 1\n");
350 kill(1, doreboot
? SIGINT
: /* reboot */
351 dohalt
? SIGUSR1
: /* halt */
352 dopower
? SIGUSR2
: /* power-down */
353 SIGTERM
); /* single-user */
356 execle(_PATH_REBOOT
, "reboot", "-l", nosync
,
357 NULL
, empty_environ
);
358 syslog(LOG_ERR
, "shutdown: can't exec %s: %m.",
363 execle(_PATH_HALT
, "halt", "-l", nosync
,
364 NULL
, empty_environ
);
365 syslog(LOG_ERR
, "shutdown: can't exec %s: %m.",
370 execle(_PATH_HALT
, "halt", "-l", "-p", nosync
,
371 NULL
, empty_environ
);
372 syslog(LOG_ERR
, "shutdown: can't exec %s: %m.",
376 kill(1, SIGTERM
); /* to single-user */
382 #define ATOI2(p) (p[0] - '0') * 10 + (p[1] - '0'); p += 2;
385 getoffset(char *timearg
)
394 if (!strcasecmp(timearg
, "now")) { /* now */
400 if (*timearg
== '+') { /* +minutes */
401 if (!isdigit(*++timearg
))
403 if ((offset
= atoi(timearg
) * 60) < 0)
405 shuttime
= now
+ offset
;
409 /* handle hh:mm by getting rid of the colon */
410 for (p
= timearg
; *p
; ++p
)
411 if (!isascii(*p
) || !isdigit(*p
)) {
412 if (*p
== ':' && strlen(p
) == 3) {
421 unsetenv("TZ"); /* OUR timezone */
422 lt
= localtime(&now
); /* current time val */
424 switch(strlen(timearg
)) {
426 this_year
= lt
->tm_year
;
427 lt
->tm_year
= ATOI2(timearg
);
429 * check if the specified year is in the next century.
430 * allow for one year of user error as many people will
431 * enter n - 1 at the start of year n.
433 if (lt
->tm_year
< (this_year
% 100) - 1)
435 /* adjust for the year 2000 and beyond */
436 lt
->tm_year
+= (this_year
- (this_year
% 100));
439 lt
->tm_mon
= ATOI2(timearg
);
440 if (--lt
->tm_mon
< 0 || lt
->tm_mon
> 11)
444 lt
->tm_mday
= ATOI2(timearg
);
445 if (lt
->tm_mday
< 1 || lt
->tm_mday
> 31)
449 lt
->tm_hour
= ATOI2(timearg
);
450 if (lt
->tm_hour
< 0 || lt
->tm_hour
> 23)
452 lt
->tm_min
= ATOI2(timearg
);
453 if (lt
->tm_min
< 0 || lt
->tm_min
> 59)
456 if ((shuttime
= mktime(lt
)) == -1)
458 if ((offset
= shuttime
- now
) < 0)
459 errx(1, "that time is already past.");
466 #define NOMSG "\n\nNO LOGINS: System going down at "
473 unlink(_PATH_NOLOGIN
); /* in case linked to another file */
474 signal(SIGINT
, finish
);
475 signal(SIGHUP
, finish
);
476 signal(SIGQUIT
, finish
);
477 signal(SIGTERM
, finish
);
478 if ((logfd
= open(_PATH_NOLOGIN
, O_WRONLY
|O_CREAT
|O_TRUNC
,
480 write(logfd
, NOMSG
, sizeof(NOMSG
) - 1);
481 ct
= ctime(&shuttime
);
482 write(logfd
, ct
+ 11, 5);
483 write(logfd
, "\n\n", 2);
484 write(logfd
, mbuf
, strlen(mbuf
));
490 finish(int signo __unused
)
493 unlink(_PATH_NOLOGIN
);
500 errx(1, "bad time format");
504 usage(const char *cp
)
509 "usage: shutdown [-] [-h | -p | -r | -k] [-o [-n]]"
510 " time [warning-message ...]\n");