* 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.8 (Berkeley) %G%";
#include <sys/resource.h>
#define REBOOT "/etc/reboot"
#define NOLOGIN "./nologin"
#define FASTBOOT "./fastboot"
#define NOLOGIN "/etc/nologin"
#define 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
];
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");
(void)strcat(mbuf
, *argv
);
(void)strcat(mbuf
, "\n");
else for (len
= sizeof(mbuf
), p
= mbuf
; fgets(p
, len
, stdin
);) {
printf("Shutdown at %.24s.", 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
);
#define WALL_CMD "/bin/wall"
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(WALL_CMD
, "w"))) {
syslog(LOG_ERR
, "shutdown: can't find %s: %m", WALL_CMD
);
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(" -l %s (without fsck's)\n", nosync
);
printf(" -l %s\n", nosync
);
execle(REBOOT
, "reboot", "-l", nosync
, 0, 0);
syslog(LOG_ERR
, "shutdown: can't exec %s: %m.", REBOOT
);
execle(HALT
, "halt", "-l", nosync
, 0, 0);
syslog(LOG_ERR
, "shutdown: can't exec %s: %m.", 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
) ?
DAYS_PER_LYEAR
: DAYS_PER_NYEAR
;
shuttime
+= dmsize
[month
];
shuttime
= HOURS_PER_DAY
* shuttime
+ hour
;
shuttime
= MINS_PER_HOUR
* shuttime
+ min
;
shuttime
*= SECS_PER_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(FASTBOOT
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0664)) >= 0) {
(void)write(fastfd
, FSMSG
, sizeof(FSMSG
) - 1);
#define NOMSG "\n\nNO LOGINS: System going down at "
(void)unlink(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(NOLOGIN
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0664)) >= 0) {
(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
));
fprintf(stderr
, "usage: shutdown [-fhknr] shutdowntime [ message ]\n");