* Copyright (c) 1988, 1990 Regents of the University of California.
* %sccs.include.redist.c%
"@(#) Copyright (c) 1988 Regents of the University of California.\n\
static char sccsid
[] = "@(#)shutdown.c 5.16 (Berkeley) %G%";
#include <sys/resource.h>
#define _PATH_NOLOGIN "./nologin"
#define _PATH_FASTBOOT "./fastboot"
int timeleft
, timetowait
;
10 H
, 5 H
, 5 H
, 3 H
, 2 H
, 1 H
, 1 H
, 30 M
,
30 M
, 10 M
, 20 M
, 10 M
, 10 M
, 5 M
, 5 M
, 3 M
,
2 M
, 1 M
, 1 M
, 30 S
, 30 S
, 30 S
,
static time_t offset
, shuttime
;
static int dofast
, dohalt
, doreboot
, killflg
, mbuflen
;
static char *nosync
, *whom
, mbuf
[BUFSIZ
];
int arglen
, ch
, len
, readstdin
;
char *strcat(), *getlogin();
(void)fprintf(stderr
, "shutdown: NOT super-user\n");
while ((ch
= getopt(argc
, argv
, "-fhknr")) != EOF
)
"shutdown: incompatible switches -f and -n.\n");
if (doreboot
&& dohalt
) {
"shutdown: incompatible switches -h and -r.\n");
for (p
= mbuf
, len
= sizeof(mbuf
); *argv
; ++argv
) {
if ((len
-= arglen
) <= 2)
endp
= mbuf
+ sizeof(mbuf
) - 2;
if (!fgets(p
, endp
- p
+ 1, stdin
))
for (; *p
&& p
< endp
; ++p
);
(void)printf("Shutdown at %.24s.\n", ctime(&shuttime
));
(void)printf("Shutdown NOW!\n");
if (!(whom
= getlogin()))
whom
= (pw
= getpwuid(getuid())) ? pw
->pw_name
: "???";
(void)putc('\n', stdout
);
(void)setpriority(PRIO_PROCESS
, 0, PRIO_MIN
);
perror("shutdown: fork");
(void)printf("shutdown: [pid %d]\n", forkpid
);
openlog("shutdown", LOG_CONS
, LOG_AUTH
);
if (offset
<= NOLOG_TIME
) {
if (tp
->timeleft
< offset
)
(void)sleep((u_int
)(offset
- tp
->timeleft
));
while (offset
< tp
->timeleft
)
* warn now, if going to sleep more than a fifth of
if (sltime
= offset
- tp
->timeleft
) {
if (sltime
> tp
->timetowait
/ 5)
if (!logged
&& tp
->timeleft
<= NOLOG_TIME
) {
(void)sleep((u_int
)tp
->timetowait
);
die_you_gravy_sucking_pig_dog();
static char hostname
[MAXHOSTNAMELEN
+ 1];
char wcmd
[MAXPATHLEN
+ 4];
(void)gethostname(hostname
, sizeof(hostname
));
/* undoc -n option to wall suppresses normal wall banner */
(void)sprintf(wcmd
, "%s -n", _PATH_WALL
);
if (!(pf
= popen(wcmd
, "w"))) {
syslog(LOG_ERR
, "shutdown: can't find %s: %m", _PATH_WALL
);
"\007*** %sSystem shutdown message from %s@%s ***\007\n",
tp
->timeleft
? "": "FINAL ", whom
, hostname
);
if (tp
->timeleft
> 10*60)
(void)fprintf(pf
, "System going down at %5.5s\n\n",
else if (tp
->timeleft
> 59)
(void)fprintf(pf
, "System going down in %d minute%s\n\n",
tp
->timeleft
/ 60, (tp
->timeleft
> 60) ? "s" : "");
(void)fprintf(pf
, "System going down in 30 seconds\n\n");
(void)fprintf(pf
, "System going down IMMEDIATELY\n\n");
(void)fwrite(mbuf
, sizeof(*mbuf
), mbuflen
, pf
);
* play some games, just in case wall doesn't come back
* probably unecessary, given that wall is careful.
(void)signal(SIGALRM
, timeout
);
(void)signal(SIGALRM
, SIG_DFL
);
die_you_gravy_sucking_pig_dog()
syslog(LOG_NOTICE
, "%s by %s: %s",
doreboot
? "reboot" : dohalt
? "halt" : "shutdown", whom
, mbuf
);
(void)printf("\r\nSystem shutdown time has arrived\007\007\r\n");
(void)printf("\rbut you'll have to do it yourself\r\n");
(void)printf(" no sync");
(void)printf(" no fsck");
(void)printf("\nkill -HUP 1\n");
execle(_PATH_REBOOT
, "reboot", "-l", nosync
, 0);
syslog(LOG_ERR
, "shutdown: can't exec %s: %m.", _PATH_REBOOT
);
execle(_PATH_HALT
, "halt", "-l", nosync
, 0);
syslog(LOG_ERR
, "shutdown: can't exec %s: %m.", _PATH_HALT
);
(void)kill(1, SIGTERM
); /* to single user */
#define ATOI2(p) (p[0] - '0') * 10 + (p[1] - '0'); p += 2;
if (!strcasecmp(timearg
, "now")) { /* now */
if (*timearg
== '+') { /* +minutes */
if (!isdigit(*++timearg
))
offset
= atoi(timearg
) * 60;
/* handle hh:mm by getting rid of the colon */
for (p
= timearg
; *p
; ++p
)
if (!isascii(*p
) || !isdigit(*p
))
if (*p
== ':' && strlen(p
) == 3) {
unsetenv("TZ"); /* OUR timezone */
lt
= localtime(&now
); /* current time val */
switch(strlen(timearg
)) {
lt
->tm_year
= ATOI2(timearg
);
lt
->tm_mon
= ATOI2(timearg
);
if (--lt
->tm_mon
< 0 || lt
->tm_mon
> 11)
lt
->tm_mday
= ATOI2(timearg
);
if (lt
->tm_mday
< 1 || lt
->tm_mday
> 31)
lt
->tm_hour
= ATOI2(timearg
);
if (lt
->tm_hour
< 0 || lt
->tm_hour
> 23)
lt
->tm_min
= ATOI2(timearg
);
if (lt
->tm_min
< 0 || lt
->tm_min
> 59)
if ((shuttime
= mktime(lt
)) == -1)
if ((offset
= shuttime
- now
) < 0) {
"shutdown: that time is already past.\n");
#define FSMSG "fastboot file for fsck\n"
if ((fastfd
= open(_PATH_FASTBOOT
, O_WRONLY
|O_CREAT
|O_TRUNC
,
(void)write(fastfd
, FSMSG
, sizeof(FSMSG
) - 1);
#define NOMSG "\n\nNO LOGINS: System going down at "
(void)unlink(_PATH_NOLOGIN
); /* in case linked to another file */
(void)signal(SIGINT
, finish
);
(void)signal(SIGHUP
, finish
);
(void)signal(SIGQUIT
, finish
);
(void)signal(SIGTERM
, finish
);
if ((logfd
= open(_PATH_NOLOGIN
, O_WRONLY
|O_CREAT
|O_TRUNC
,
(void)write(logfd
, NOMSG
, sizeof(NOMSG
) - 1);
(void)write(logfd
, ct
+ 11, 5);
(void)write(logfd
, "\n\n", 2);
(void)write(logfd
, mbuf
, strlen(mbuf
));
(void)unlink(_PATH_NOLOGIN
);
(void)fprintf(stderr
, "shutdown: bad time format.\n");
fprintf(stderr
, "usage: shutdown [-fhknr] shutdowntime [ message ]\n");