* Copyright (c) 1983 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
"@(#) Copyright (c) 1983 Regents of the University of California.\n\
static char sccsid
[] = "@(#)shutdown.c 5.5 (Berkeley) %G%";
#include <sys/resource.h>
* /etc/shutdown when [messages]
* allow super users to tell users and remind users
* of iminent shutdown of unix
* and shut it down automatically
* and even reboot or halt the machine if they desire
#define REBOOT "/etc/reboot"
#define NLOG 600 /* no of bytes possible for message */
#define NOLOGTIME 5 MINUTES
#define IGNOREUSER "sleeper"
extern struct tm
*localtime();
int nlflag
= 1; /* nolog yet to be done */
char nosyncflag
[] = "-n";
char term
[sizeof tpath
+ sizeof utmp
.ut_line
];
char nolog1
[] = "\n\nNO LOGINS: System going down at %5.5s\n\n";
char nologin
[] = "nologin";
char fastboot
[] = "fastboot";
char nologin
[] = "/etc/nologin";
char fastboot
[] = "/fastboot";
char *shutter
, *getlogin();
(void) gethostname(hostname
, sizeof (hostname
));
openlog("shutdown", 0, LOG_AUTH
);
while (argc
> 0 && (f
= argv
[0], *f
++ == '-')) {
while (i
= *f
++) switch (i
) {
fprintf(stderr
, "shutdown: '%c' - unknown flag\n", i
);
/* argv[0] is not available after the argument handling. */
printf("Usage: shutdown [ -krhfn ] shutdowntime [ message ]\n");
if (fast
&& (nosync
== nosyncflag
)) {
printf ("shutdown: Incompatible switches 'fast' & 'nosync'\n");
fprintf(stderr
, "NOT super-user\n");
nowtime
= time((long *)0);
(void) strcat(nolog2
, " ");
(void) strcat(nolog2
, *argv
++);
m
= ((stogo
= sdt
- nowtime
) + 30)/60;
printf("Shutdown at %5.5s (in ", ts
+11);
printf("%d hour%s ", h
, h
!= 1 ? "s" : "");
printf("%d minute%s) ", m
, m
!= 1 ? "s" : "");
(void) signal(SIGHUP
, SIG_IGN
);
(void) signal(SIGQUIT
, SIG_IGN
);
(void) signal(SIGINT
, SIG_IGN
);
(void) signal(SIGTTOU
, SIG_IGN
);
(void) signal(SIGTERM
, finish
);
(void) signal(SIGALRM
, timeout
);
(void) setpriority(PRIO_PROCESS
, 0, PRIO_MIN
);
(void) putc('\n', stdout
);
ufd
= open("/etc/utmp",0);
perror("shutdown: /etc/utmp");
for (i
= 0; stogo
<= interval
[i
].stogo
&& interval
[i
].sint
; i
++)
if (stogo
> 0 && (stogo
-sint
) < interval
[i
].stogo
)
sint
= stogo
- interval
[i
].stogo
;
if (stogo
<= NOLOGTIME
&& nlflag
) {
if (sint
>= stogo
|| sint
== 0)
nowtime
= time((long *)0);
(void) lseek(ufd
, 0L, 0);
while (read(ufd
,(char *)&utmp
,sizeof utmp
)==sizeof utmp
)
strncmp(utmp
.ut_name
, IGNOREUSER
, sizeof(utmp
.ut_name
))) {
(void) strcpy(term
, tpath
);
(void) strncat(term
, utmp
.ut_line
, sizeof utmp
.ut_line
);
if ((termf
= stdout
) != NULL
)
if ((termf
= fopen(term
, "w")) != NULL
)
fprintf(termf
, "\n\r\n");
warn(termf
, sdt
, nowtime
, f
);
if (first
|| sdt
- nowtime
> 1 MINUTES
) {
fprintf(termf
, "\t...%s", nolog2
);
(void) fputc('\r', termf
);
(void) fputc('\n', termf
);
printf("\n\007\007System shutdown time has arrived\007\007\n");
syslog(LOG_CRIT
, "%s!%s: %s", hostname
, shutter
, nolog2
);
printf("but you'll have to do it yourself\n");
(void) kill(-1, SIGTERM
); /* terminate everyone */
sleep(5); /* & wait while they die */
execle(REBOOT
, "reboot", nosync
, 0, 0);
execle(HALT
, "halt", nosync
, 0, 0);
(void) kill(1, SIGTERM
); /* sync */
(void) kill(1, SIGTERM
); /* sync */
printf("EXTERMINATE EXTERMINATE\n");
printf(" %s (without fsck's)\n", nosync
);
stogo
= sdt
- time((long *) 0);
if (stogo
> 0 && sint
> 0)
sleep((unsigned)(sint
<stogo
? sint
: stogo
));
if (strcmp(s
, "now") == 0)
tim
= time((long *) 0) + t
;
while (strlen(s
) > 2 && isdigit(*s
))
t
= lt
->tm_sec
+ lt
->tm_min
*60 + lt
->tm_hour
*3600;
if (tim
< t
|| tim
>= (24*3600)) {
/* before now or after midnight */
printf("That must be tomorrow\nCan't you wait till then?\n");
printf("Bad time format\n");
warn(term
, sdt
, now
, type
)
register delay
= sdt
- now
;
"\007\007\t*** %sSystem shutdown message from %s@%s ***\r\n\n",
type
, shutter
, hostname
);
fprintf(term
, "System going down at %5.5s\r\n", ts
+11);
else if (delay
> 95 SECONDS
) {
fprintf(term
, "System going down in %d minute%s\r\n",
(delay
+30)/60, (delay
+30)/60 != 1 ? "s" : "");
fprintf(term
, "System going down in %d second%s\r\n",
delay
, delay
!= 1 ? "s" : "");
fprintf(term
, "System going down IMMEDIATELY\r\n");
if ((fastd
= fopen(fastboot
, "w")) != NULL
) {
(void) unlink(nologin
); /* in case linked to std file */
if ((nologf
= fopen(nologin
, "w")) != NULL
) {
fprintf(nologf
, nolog1
, (ctime(&sdt
)) + 11);
fprintf(nologf
, "\t%s\n", nolog2
+ 1);
(void) signal(SIGTERM
, SIG_IGN
);