static char *sccsid
= "@(#)shutdown.c 4.15 (Berkeley) 83/01/16";
* /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 LOGFILE "shutdown.log"
#define LOGFILE "/usr/adm/shutdownlog"
#define REBOOT "/etc/reboot"
#define NLOG 20 /* no of args possible for message */
#define NOLOGTIME 5 MINUTES
#define IGNOREUSER "sleeper"
extern struct tm
*localtime();
int nlflag
= 1; /* nolog yet to be done */
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 nologin
[] = "/etc/nologin";
char *shutter
, *getlogin();
register char **mess
, *f
;
gethostname(hostname
, sizeof (hostname
));
while (argc
> 0 && (f
= argv
[0], *f
++ == '-')) {
while (i
= *f
++) switch (i
) {
fprintf(stderr
, "shutdown: '%c' - unknown flag\n", i
);
printf("Usage: %s [ -krh ] shutdowntime [ message ]\n",
fprintf(stderr
, "NOT super-user\n");
nowtime
= time((time_t *)0);
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" : "");
signal(SIGQUIT
, SIG_IGN
);
signal(SIGALRM
, do_nothing
);
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((time_t *) 0);
while (read(ufd
,&utmp
,sizeof utmp
)==sizeof utmp
)
strncmp(utmp
.ut_name
, IGNOREUSER
, sizeof(utmp
.ut_name
))) {
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
) {
for (mess
= nolog2
; *mess
; mess
++)
fprintf(termf
, " %s", *mess
);
printf("\n\007\007System shutdown time has arrived\007\007\n");
printf("but you'll have to do it yourself\n");
kill(-1, SIGTERM
); /* terminate everyone */
sleep(5); /* & wait while they die */
execle(REBOOT
, "reboot", 0, 0);
execle(HALT
, "halt", 0, 0);
kill(1, SIGTERM
); /* sync */
kill(1, SIGTERM
); /* sync */
printf("EXTERMINATE EXTERMINATE\n");
stogo
= sdt
- time((time_t *) 0);
if (stogo
> 0 && sint
> 0)
sleep(sint
<stogo
? sint
: stogo
);
if (strcmp(s
, "now") == 0)
tim
= time((time_t *) 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
, hostname
, shutter
);
"\007\007\t*** %sSystem shutdown message (%s) ***\r\n\n",
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");
unlink(nologin
); /* in case linked to std file */
if ((nologf
= fopen(nologin
, "w")) != NULL
) {
fprintf(nologf
, nolog1
, (ctime(&sdt
)) + 11);
for (mess
= nolog2
; *mess
; mess
++)
fprintf(nologf
, " %s", *mess
);
signal(SIGTERM
, SIG_IGN
);
signal(SIGALRM
, do_nothing
);
* make an entry in the shutdown log
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
struct tm
*tm
, *localtime();
fp
= fopen(LOGFILE
, "a");
printf("Shutdown: log entry failed\n");
fprintf(fp
, "%02d:%02d %s %s %2d, %4d. Shutdown:", tm
->tm_hour
,
tm
->tm_min
, days
[tm
->tm_wday
], months
[tm
->tm_mon
],
tm
->tm_mday
, tm
->tm_year
+ 1900);
for (mess
= nolog2
; *mess
; mess
++)
fprintf(fp
, " %s", *mess
);
fprintf(fp
, " (by %s!%s)", hostname
, shutter
);