2002-10-19 00:35:36 +04:00
|
|
|
/* $NetBSD: shutdown.c,v 1.39 2002/10/18 20:35:36 atatat Exp $ */
|
1995-03-18 17:54:19 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1994-09-17 04:10:08 +04:00
|
|
|
* Copyright (c) 1988, 1990, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-09-15 15:17:10 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1997-09-15 15:17:10 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1988, 1990, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1995-03-18 17:54:19 +03:00
|
|
|
#if 0
|
1997-09-16 16:54:42 +04:00
|
|
|
static char sccsid[] = "@(#)shutdown.c 8.4 (Berkeley) 4/28/95";
|
1995-03-18 17:54:19 +03:00
|
|
|
#else
|
2002-10-19 00:35:36 +04:00
|
|
|
__RCSID("$NetBSD: shutdown.c,v 1.39 2002/10/18 20:35:36 atatat Exp $");
|
1995-03-18 17:54:19 +03:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/syslog.h>
|
1994-09-17 04:10:08 +04:00
|
|
|
|
|
|
|
#include <ctype.h>
|
1998-01-21 01:30:15 +03:00
|
|
|
#include <err.h>
|
1994-09-17 04:10:08 +04:00
|
|
|
#include <fcntl.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <pwd.h>
|
1994-09-17 04:10:08 +04:00
|
|
|
#include <setjmp.h>
|
|
|
|
#include <signal.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <stdio.h>
|
1994-09-17 04:10:08 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1998-04-01 19:17:31 +04:00
|
|
|
#include <time.h>
|
1994-09-17 04:10:08 +04:00
|
|
|
#include <tzfile.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#include "pathnames.h"
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
#undef _PATH_NOLOGIN
|
|
|
|
#define _PATH_NOLOGIN "./nologin"
|
|
|
|
#undef _PATH_FASTBOOT
|
|
|
|
#define _PATH_FASTBOOT "./fastboot"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define H *60*60
|
|
|
|
#define M *60
|
|
|
|
#define S *1
|
|
|
|
#define NOLOG_TIME 5*60
|
|
|
|
struct interval {
|
|
|
|
int timeleft, timetowait;
|
|
|
|
} tlist[] = {
|
1997-07-10 09:08:06 +04:00
|
|
|
{ 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 },
|
|
|
|
{ 0, 0 }
|
1994-09-17 04:10:08 +04:00
|
|
|
};
|
1993-03-21 12:45:37 +03:00
|
|
|
#undef H
|
|
|
|
#undef M
|
|
|
|
#undef S
|
|
|
|
|
|
|
|
static time_t offset, shuttime;
|
1998-03-23 08:00:15 +03:00
|
|
|
static int dofast, dohalt, doreboot, killflg, mbuflen, nofork, nosync, dodump;
|
1998-06-07 01:18:54 +04:00
|
|
|
static int dopowerdown;
|
1998-07-27 00:18:32 +04:00
|
|
|
static const char *whom;
|
|
|
|
static char mbuf[BUFSIZ];
|
2002-10-19 00:35:36 +04:00
|
|
|
char *bootstr;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-09-17 04:10:08 +04:00
|
|
|
void badtime __P((void));
|
|
|
|
void die_you_gravy_sucking_pig_dog __P((void));
|
|
|
|
void doitfast __P((void));
|
1998-10-29 23:12:56 +03:00
|
|
|
void dorcshutdown __P((void));
|
1994-09-17 04:10:08 +04:00
|
|
|
void finish __P((int));
|
|
|
|
void getoffset __P((char *));
|
|
|
|
void loop __P((void));
|
1997-09-15 15:17:10 +04:00
|
|
|
int main __P((int, char *[]));
|
1994-09-17 04:10:08 +04:00
|
|
|
void nolog __P((void));
|
|
|
|
void timeout __P((int));
|
|
|
|
void timewarn __P((int));
|
|
|
|
void usage __P((void));
|
|
|
|
|
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
1994-09-17 04:10:08 +04:00
|
|
|
char *argv[];
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-09-15 15:17:10 +04:00
|
|
|
char *p, *endp;
|
1993-03-21 12:45:37 +03:00
|
|
|
struct passwd *pw;
|
1998-01-21 01:30:15 +03:00
|
|
|
int arglen, ch, len;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
#ifndef DEBUG
|
1998-01-21 01:30:15 +03:00
|
|
|
if (geteuid())
|
|
|
|
errx(1, "NOT super-user");
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif
|
2002-10-19 00:35:36 +04:00
|
|
|
while ((ch = getopt(argc, argv, "b:Ddfhknpr")) != -1)
|
1993-03-21 12:45:37 +03:00
|
|
|
switch (ch) {
|
2002-10-19 00:35:36 +04:00
|
|
|
case 'b':
|
|
|
|
bootstr = optarg;
|
|
|
|
break;
|
1998-01-21 01:30:15 +03:00
|
|
|
case 'd':
|
|
|
|
dodump = 1;
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
1998-03-23 08:00:15 +03:00
|
|
|
case 'D':
|
|
|
|
nofork = 1;
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case 'f':
|
|
|
|
dofast = 1;
|
|
|
|
break;
|
1998-06-07 01:18:54 +04:00
|
|
|
case 'p':
|
|
|
|
dopowerdown = 1;
|
|
|
|
/* FALLTHROUGH */
|
1993-03-21 12:45:37 +03:00
|
|
|
case 'h':
|
|
|
|
dohalt = 1;
|
|
|
|
break;
|
|
|
|
case 'k':
|
|
|
|
killflg = 1;
|
|
|
|
break;
|
|
|
|
case 'n':
|
1997-01-23 08:48:06 +03:00
|
|
|
nosync = 1;
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
doreboot = 1;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc < 1)
|
|
|
|
usage();
|
|
|
|
|
1998-01-21 02:05:15 +03:00
|
|
|
if (dodump && !dohalt && !doreboot)
|
|
|
|
doreboot = 1;
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
if (dofast && nosync) {
|
1998-01-21 02:05:15 +03:00
|
|
|
warnx("incompatible switches -f and -n");
|
1993-03-21 12:45:37 +03:00
|
|
|
usage();
|
|
|
|
}
|
1998-01-21 02:05:15 +03:00
|
|
|
if (dohalt && doreboot) {
|
1998-06-07 01:18:54 +04:00
|
|
|
const char *which_flag = dopowerdown ? "p" : "h";
|
|
|
|
|
|
|
|
warnx("incompatible switches -%s and -r", which_flag);
|
1997-07-09 06:39:37 +04:00
|
|
|
usage();
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1998-01-21 01:30:15 +03:00
|
|
|
getoffset(*argv++);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1998-08-25 23:18:12 +04:00
|
|
|
if (argv[0]) {
|
1998-01-21 02:13:31 +03:00
|
|
|
if (strcmp(argv[0], "-") || argv[1]) {
|
1998-01-21 01:30:15 +03:00
|
|
|
for (p = mbuf, len = sizeof(mbuf); *argv; ++argv) {
|
|
|
|
arglen = strlen(*argv);
|
|
|
|
if ((len -= arglen) <= 2)
|
|
|
|
break;
|
|
|
|
if (p != mbuf)
|
|
|
|
*p++ = ' ';
|
|
|
|
memmove(p, *argv, arglen);
|
|
|
|
p += arglen;
|
|
|
|
}
|
|
|
|
*p = '\n';
|
|
|
|
*++p = '\0';
|
|
|
|
} else {
|
|
|
|
p = mbuf;
|
|
|
|
endp = mbuf + sizeof(mbuf) - 2;
|
|
|
|
for (;;) {
|
|
|
|
if (!fgets(p, endp - p + 1, stdin))
|
|
|
|
break;
|
|
|
|
for (; *p && p < endp; ++p);
|
|
|
|
if (p == endp) {
|
|
|
|
*p = '\n';
|
|
|
|
*++p = '\0';
|
|
|
|
break;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
1998-08-25 23:18:12 +04:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
mbuflen = strlen(mbuf);
|
|
|
|
|
|
|
|
if (offset)
|
|
|
|
(void)printf("Shutdown at %.24s.\n", ctime(&shuttime));
|
|
|
|
else
|
|
|
|
(void)printf("Shutdown NOW!\n");
|
|
|
|
|
|
|
|
if (!(whom = getlogin()))
|
|
|
|
whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
(void)putc('\n', stdout);
|
|
|
|
#else
|
|
|
|
(void)setpriority(PRIO_PROCESS, 0, PRIO_MIN);
|
1998-03-23 08:00:15 +03:00
|
|
|
if (nofork == 0) {
|
1993-03-21 12:45:37 +03:00
|
|
|
int forkpid;
|
|
|
|
|
|
|
|
forkpid = fork();
|
|
|
|
if (forkpid == -1) {
|
|
|
|
perror("shutdown: fork");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (forkpid) {
|
|
|
|
(void)printf("shutdown: [pid %d]\n", forkpid);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
openlog("shutdown", LOG_CONS, LOG_AUTH);
|
|
|
|
loop();
|
1994-09-17 04:10:08 +04:00
|
|
|
/* NOTREACHED */
|
1997-07-10 09:08:06 +04:00
|
|
|
#ifdef __GNUC__
|
|
|
|
return 1;
|
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1994-09-17 04:10:08 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
loop()
|
|
|
|
{
|
1994-09-17 04:10:08 +04:00
|
|
|
struct interval *tp;
|
1993-03-21 12:45:37 +03:00
|
|
|
u_int sltime;
|
|
|
|
int logged;
|
|
|
|
|
|
|
|
if (offset <= NOLOG_TIME) {
|
|
|
|
logged = 1;
|
|
|
|
nolog();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
logged = 0;
|
|
|
|
tp = tlist;
|
|
|
|
if (tp->timeleft < offset)
|
|
|
|
(void)sleep((u_int)(offset - tp->timeleft));
|
|
|
|
else {
|
|
|
|
while (offset < tp->timeleft)
|
|
|
|
++tp;
|
|
|
|
/*
|
1994-09-17 04:10:08 +04:00
|
|
|
* Warn now, if going to sleep more than a fifth of
|
1993-03-21 12:45:37 +03:00
|
|
|
* the next wait time.
|
|
|
|
*/
|
1997-07-10 09:08:06 +04:00
|
|
|
if ((sltime = offset - tp->timeleft) != 0) {
|
1993-03-21 12:45:37 +03:00
|
|
|
if (sltime > tp->timetowait / 5)
|
1994-09-17 04:10:08 +04:00
|
|
|
timewarn(offset);
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)sleep(sltime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (;; ++tp) {
|
1994-09-17 04:10:08 +04:00
|
|
|
timewarn(tp->timeleft);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (!logged && tp->timeleft <= NOLOG_TIME) {
|
|
|
|
logged = 1;
|
|
|
|
nolog();
|
|
|
|
}
|
|
|
|
(void)sleep((u_int)tp->timetowait);
|
|
|
|
if (!tp->timeleft)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
die_you_gravy_sucking_pig_dog();
|
|
|
|
}
|
|
|
|
|
|
|
|
static jmp_buf alarmbuf;
|
|
|
|
|
1994-09-17 04:10:08 +04:00
|
|
|
void
|
|
|
|
timewarn(timeleft)
|
1994-02-16 05:56:04 +03:00
|
|
|
int timeleft;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
static int first;
|
|
|
|
static char hostname[MAXHOSTNAMELEN + 1];
|
|
|
|
FILE *pf;
|
1994-09-17 04:10:08 +04:00
|
|
|
char wcmd[MAXPATHLEN + 4];
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1998-07-05 12:34:25 +04:00
|
|
|
if (!first++) {
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)gethostname(hostname, sizeof(hostname));
|
1998-07-05 12:34:25 +04:00
|
|
|
hostname[sizeof(hostname) - 1] = '\0';
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/* undoc -n option to wall suppresses normal wall banner */
|
1998-07-05 12:34:25 +04:00
|
|
|
(void)snprintf(wcmd, sizeof wcmd, "%s -n", _PATH_WALL);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (!(pf = popen(wcmd, "w"))) {
|
|
|
|
syslog(LOG_ERR, "shutdown: can't find %s: %m", _PATH_WALL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)fprintf(pf,
|
|
|
|
"\007*** %sSystem shutdown message from %s@%s ***\007\n",
|
1994-02-16 05:56:04 +03:00
|
|
|
timeleft ? "": "FINAL ", whom, hostname);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-02-16 05:56:04 +03:00
|
|
|
if (timeleft > 10*60)
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)fprintf(pf, "System going down at %5.5s\n\n",
|
|
|
|
ctime(&shuttime) + 11);
|
1994-02-16 05:56:04 +03:00
|
|
|
else if (timeleft > 59)
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)fprintf(pf, "System going down in %d minute%s\n\n",
|
1994-02-16 05:56:04 +03:00
|
|
|
timeleft / 60, (timeleft > 60) ? "s" : "");
|
|
|
|
else if (timeleft)
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)fprintf(pf, "System going down in 30 seconds\n\n");
|
|
|
|
else
|
|
|
|
(void)fprintf(pf, "System going down IMMEDIATELY\n\n");
|
|
|
|
|
|
|
|
if (mbuflen)
|
|
|
|
(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.
|
|
|
|
*/
|
|
|
|
if (!setjmp(alarmbuf)) {
|
|
|
|
(void)signal(SIGALRM, timeout);
|
|
|
|
(void)alarm((u_int)30);
|
|
|
|
(void)pclose(pf);
|
|
|
|
(void)alarm((u_int)0);
|
|
|
|
(void)signal(SIGALRM, SIG_DFL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1994-09-17 04:10:08 +04:00
|
|
|
timeout(signo)
|
|
|
|
int signo;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
longjmp(alarmbuf, 1);
|
|
|
|
}
|
|
|
|
|
1994-09-17 04:10:08 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
die_you_gravy_sucking_pig_dog()
|
|
|
|
{
|
|
|
|
|
|
|
|
syslog(LOG_NOTICE, "%s by %s: %s",
|
1998-01-21 02:05:15 +03:00
|
|
|
doreboot ? "reboot" : dohalt ? "halt" : "shutdown", whom, mbuf);
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)sleep(2);
|
|
|
|
|
|
|
|
(void)printf("\r\nSystem shutdown time has arrived\007\007\r\n");
|
|
|
|
if (killflg) {
|
|
|
|
(void)printf("\rbut you'll have to do it yourself\r\n");
|
1994-09-17 04:10:08 +04:00
|
|
|
finish(0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
if (dofast)
|
|
|
|
doitfast();
|
1998-10-29 23:12:56 +03:00
|
|
|
dorcshutdown();
|
1998-01-21 02:05:15 +03:00
|
|
|
if (doreboot || dohalt) {
|
1998-06-07 01:18:54 +04:00
|
|
|
char *args[16], **arg, *path;
|
1998-01-21 02:05:15 +03:00
|
|
|
|
|
|
|
arg = &args[0];
|
|
|
|
if (doreboot) {
|
|
|
|
path = _PATH_REBOOT;
|
|
|
|
*arg++ = "reboot";
|
|
|
|
} else {
|
|
|
|
path = _PATH_HALT;
|
|
|
|
*arg++ = "halt";
|
|
|
|
}
|
|
|
|
if (dodump)
|
|
|
|
*arg++ = "-d";
|
|
|
|
if (nosync)
|
|
|
|
*arg++ = "-n";
|
1998-06-07 01:18:54 +04:00
|
|
|
if (dopowerdown)
|
|
|
|
*arg++ = "-p";
|
1998-01-21 02:05:15 +03:00
|
|
|
*arg++ = "-l";
|
2002-10-19 00:35:36 +04:00
|
|
|
if (bootstr)
|
|
|
|
*arg++ = bootstr;
|
1998-01-21 02:05:15 +03:00
|
|
|
*arg++ = 0;
|
|
|
|
#ifndef DEBUG
|
1999-10-31 16:24:05 +03:00
|
|
|
execve(path, args, (char **)0);
|
1998-01-21 02:05:15 +03:00
|
|
|
syslog(LOG_ERR, "shutdown: can't exec %s: %m", path);
|
1993-03-21 12:45:37 +03:00
|
|
|
perror("shutdown");
|
1998-01-21 02:05:15 +03:00
|
|
|
#else
|
|
|
|
printf("%s", path);
|
|
|
|
for (arg = &args[0]; *arg; arg++)
|
|
|
|
printf(" %s", *arg);
|
|
|
|
printf("\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif
|
1998-01-21 02:05:15 +03:00
|
|
|
} else {
|
|
|
|
#ifndef DEBUG
|
|
|
|
(void)kill(1, SIGTERM); /* to single user */
|
|
|
|
#else
|
|
|
|
printf("kill 1\n");
|
|
|
|
#endif
|
|
|
|
}
|
1994-09-17 04:10:08 +04:00
|
|
|
finish(0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1998-01-21 03:32:53 +03:00
|
|
|
#define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-09-17 04:10:08 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
getoffset(timearg)
|
1997-09-15 15:17:10 +04:00
|
|
|
char *timearg;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-09-15 15:17:10 +04:00
|
|
|
struct tm *lt;
|
|
|
|
char *p;
|
1994-09-17 04:10:08 +04:00
|
|
|
time_t now;
|
1998-01-21 01:14:09 +03:00
|
|
|
int yearset;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1998-10-09 07:01:40 +04:00
|
|
|
(void)time(&now);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (!strcasecmp(timearg, "now")) { /* now */
|
|
|
|
offset = 0;
|
1998-10-09 07:01:40 +04:00
|
|
|
shuttime = now;
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*timearg == '+') { /* +minutes */
|
|
|
|
if (!isdigit(*++timearg))
|
|
|
|
badtime();
|
|
|
|
offset = atoi(timearg) * 60;
|
|
|
|
shuttime = now + offset;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handle hh:mm by getting rid of the colon */
|
|
|
|
for (p = timearg; *p; ++p)
|
1998-08-25 23:18:12 +04:00
|
|
|
if (!isascii(*p) || !isdigit(*p)) {
|
1993-03-21 12:45:37 +03:00
|
|
|
if (*p == ':' && strlen(p) == 3) {
|
|
|
|
p[0] = p[1];
|
|
|
|
p[1] = p[2];
|
|
|
|
p[2] = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
badtime();
|
1998-08-25 23:18:12 +04:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
unsetenv("TZ"); /* OUR timezone */
|
|
|
|
lt = localtime(&now); /* current time val */
|
|
|
|
|
1998-01-21 01:14:09 +03:00
|
|
|
lt->tm_sec = 0;
|
|
|
|
|
|
|
|
yearset = 0;
|
|
|
|
switch (strlen(timearg)) {
|
|
|
|
case 12:
|
1998-01-21 03:32:53 +03:00
|
|
|
lt->tm_year = ATOI2(timearg) * 100 - TM_YEAR_BASE;
|
1998-01-21 01:14:09 +03:00
|
|
|
yearset = 1;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 10:
|
|
|
|
if (yearset) {
|
1998-01-21 03:32:53 +03:00
|
|
|
lt->tm_year += ATOI2(timearg);
|
1998-01-21 01:14:09 +03:00
|
|
|
} else {
|
|
|
|
yearset = ATOI2(timearg);
|
|
|
|
if (yearset < 69)
|
1998-01-21 03:32:53 +03:00
|
|
|
lt->tm_year = yearset + 2000 - TM_YEAR_BASE;
|
1998-01-21 01:14:09 +03:00
|
|
|
else
|
1998-01-21 03:32:53 +03:00
|
|
|
lt->tm_year = yearset + 1900 - TM_YEAR_BASE;
|
1998-01-21 01:14:09 +03:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 8:
|
|
|
|
lt->tm_mon = ATOI2(timearg);
|
1998-01-21 01:14:09 +03:00
|
|
|
--lt->tm_mon;
|
1993-03-21 12:45:37 +03:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 6:
|
|
|
|
lt->tm_mday = ATOI2(timearg);
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 4:
|
|
|
|
lt->tm_hour = ATOI2(timearg);
|
1998-01-21 01:14:09 +03:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 2:
|
1993-03-21 12:45:37 +03:00
|
|
|
lt->tm_min = ATOI2(timearg);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
badtime();
|
|
|
|
}
|
1998-01-21 01:14:09 +03:00
|
|
|
|
|
|
|
if ((shuttime = mktime(lt)) == -1)
|
|
|
|
badtime();
|
1998-01-21 01:30:15 +03:00
|
|
|
if ((offset = shuttime - now) < 0)
|
|
|
|
errx(1, "time is already past");
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1998-10-29 23:12:56 +03:00
|
|
|
void
|
|
|
|
dorcshutdown()
|
|
|
|
{
|
|
|
|
(void)printf("\r\nAbout to run shutdown hooks...\r\n");
|
2000-12-20 03:31:41 +03:00
|
|
|
(void)system(". " _PATH_RCSHUTDOWN);
|
2000-07-20 21:33:57 +04:00
|
|
|
(void)sleep(5); /* Give operator a chance to abort this. */
|
2000-07-25 22:59:44 +04:00
|
|
|
(void)printf("\r\nDone running shutdown hooks.\r\n");
|
1998-10-29 23:12:56 +03:00
|
|
|
}
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#define FSMSG "fastboot file for fsck\n"
|
1994-09-17 04:10:08 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
doitfast()
|
|
|
|
{
|
|
|
|
int fastfd;
|
|
|
|
|
|
|
|
if ((fastfd = open(_PATH_FASTBOOT, O_WRONLY|O_CREAT|O_TRUNC,
|
|
|
|
0664)) >= 0) {
|
|
|
|
(void)write(fastfd, FSMSG, sizeof(FSMSG) - 1);
|
|
|
|
(void)close(fastfd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define NOMSG "\n\nNO LOGINS: System going down at "
|
1994-09-17 04:10:08 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
nolog()
|
|
|
|
{
|
|
|
|
int logfd;
|
1994-09-17 04:10:08 +04:00
|
|
|
char *ct;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
(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,
|
|
|
|
0664)) >= 0) {
|
|
|
|
(void)write(logfd, NOMSG, sizeof(NOMSG) - 1);
|
|
|
|
ct = ctime(&shuttime);
|
|
|
|
(void)write(logfd, ct + 11, 5);
|
|
|
|
(void)write(logfd, "\n\n", 2);
|
|
|
|
(void)write(logfd, mbuf, strlen(mbuf));
|
|
|
|
(void)close(logfd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1994-09-17 04:10:08 +04:00
|
|
|
finish(signo)
|
|
|
|
int signo;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1998-07-05 12:34:25 +04:00
|
|
|
|
1994-09-17 04:02:23 +04:00
|
|
|
if (!killflg)
|
|
|
|
(void)unlink(_PATH_NOLOGIN);
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
1994-09-17 04:10:08 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
badtime()
|
|
|
|
{
|
1998-07-05 12:34:25 +04:00
|
|
|
|
1998-01-21 01:30:15 +03:00
|
|
|
warnx("illegal time format");
|
|
|
|
usage();
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1994-09-17 04:10:08 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
usage()
|
|
|
|
{
|
1998-07-05 12:34:25 +04:00
|
|
|
|
1998-01-21 01:30:15 +03:00
|
|
|
(void)fprintf(stderr,
|
2000-07-25 22:59:44 +04:00
|
|
|
"usage: shutdown [-Ddfhknpr] time [message ... | -]\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(1);
|
|
|
|
}
|