* Copyright (c) 1988 Regents of the University of California.
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the University of California, Berkeley. The name of the
* University may not be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
"@(#) Copyright (c) 1988 Regents of the University of California.\n\
static char sccsid
[] = "@(#)shutdown.c 5.11 (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
;
struct passwd
*pw
, *getpwuid();
char *strcat(), *getlogin();
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
);
printf("Shutdown at %.24s.\n", ctime(&shuttime
));
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");
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
];
(void)signal(SIGALRM
, timeout
);
(void)gethostname(hostname
, sizeof(hostname
));
if (!(pf
= popen(_PATH_WALL
, "w"))) {
syslog(LOG_ERR
, "shutdown: can't find %s: %m", _PATH_WALL
);
fprintf(pf
, "*** %sSystem shutdown message ***\n",
tp
->timeleft
? "": "FINAL ");
if (tp
->timeleft
> 10*60)
fprintf(pf
, "System going down at %5.5s\n\n",
else if (tp
->timeleft
> 59)
fprintf(pf
, "System going down in %d minute%s\n\n",
tp
->timeleft
/ 60, (tp
->timeleft
> 60) ? "s" : "");
fprintf(pf
, "System going down in 30 seconds\n\n");
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.
die_you_gravy_sucking_pig_dog()
syslog(LOG_NOTICE
, "%s by %s: %s",
doreboot
? "reboot" : dohalt
? "halt" : "shutdown", whom
, mbuf
);
printf("\r\nSystem shutdown time has arrived\007\007\r\n");
printf("\rbut you'll have to do it yourself\r\n");
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;
{ -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int year
, month
, day
, hour
, min
;
if (!strcasecmp(timearg
, "now")) { /* now */
if (*timearg
== '+') { /* +minutes */
if (!isdigit(*++timearg
))
/* 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
); /* [yymmdd]hhmm */
switch(strlen(timearg
)) {
if (month
< 1 || month
> 12 || day
< 1 || day
> 31 ||
hour
< 0 || hour
> 23 || min
< 0 || min
> 59)
if (isleap(year
) && month
> 2)
for (--year
; year
>= EPOCH_YEAR
; --year
)
shuttime
+= isleap(year
) ?
DAYSPERLYEAR
: DAYSPERNYEAR
;
shuttime
+= dmsize
[month
];
shuttime
= HOURSPERDAY
* shuttime
+ hour
;
shuttime
= MINSPERHOUR
* shuttime
+ min
;
shuttime
-= lt
->tm_gmtoff
;
if ((offset
= shuttime
- now
) >= 0)
"shutdown: bad time format, or 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
);
fprintf(stderr
, "usage: shutdown [-fhknr] shutdowntime [ message ]\n");