1994-05-26 06:35:07 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1988, 1990, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2003-05-03 18:41:59 +00:00
|
|
|
#if 0
|
1994-05-26 06:35:07 +00:00
|
|
|
#ifndef lint
|
1998-08-03 06:22:43 +00:00
|
|
|
static const char copyright[] =
|
1994-05-26 06:35:07 +00:00
|
|
|
"@(#) Copyright (c) 1988, 1990, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1998-12-11 11:21:47 +00:00
|
|
|
static char sccsid[] = "@(#)shutdown.c 8.4 (Berkeley) 4/28/95";
|
1994-05-26 06:35:07 +00:00
|
|
|
#endif /* not lint */
|
2003-05-03 18:41:59 +00:00
|
|
|
#endif
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/syslog.h>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
1998-08-03 06:22:43 +00:00
|
|
|
#include <err.h>
|
2014-12-16 08:29:02 +00:00
|
|
|
#include <errno.h>
|
1994-05-26 06:35:07 +00:00
|
|
|
#include <fcntl.h>
|
2003-05-05 22:49:23 +00:00
|
|
|
#include <paths.h>
|
1994-05-26 06:35:07 +00:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
#undef _PATH_NOLOGIN
|
|
|
|
#define _PATH_NOLOGIN "./nologin"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define H *60*60
|
|
|
|
#define M *60
|
|
|
|
#define S *1
|
|
|
|
#define NOLOG_TIME 5*60
|
2011-11-04 13:36:02 +00:00
|
|
|
static struct interval {
|
1994-05-26 06:35:07 +00:00
|
|
|
int timeleft, timetowait;
|
|
|
|
} tlist[] = {
|
1998-01-10 20:11:06 +00: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-05-26 06:35:07 +00:00
|
|
|
};
|
|
|
|
#undef H
|
|
|
|
#undef M
|
|
|
|
#undef S
|
|
|
|
|
|
|
|
static time_t offset, shuttime;
|
1999-06-21 16:06:21 +00:00
|
|
|
static int dohalt, dopower, doreboot, killflg, mbuflen, oflag;
|
2001-07-15 05:46:07 +00:00
|
|
|
static char mbuf[BUFSIZ];
|
|
|
|
static const char *nosync, *whom;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2009-09-28 07:42:52 +00:00
|
|
|
static void badtime(void);
|
2012-08-01 09:10:21 +00:00
|
|
|
static void die_you_gravy_sucking_pig_dog(void);
|
2009-09-28 07:42:52 +00:00
|
|
|
static void finish(int);
|
|
|
|
static void getoffset(char *);
|
|
|
|
static void loop(void);
|
|
|
|
static void nolog(void);
|
|
|
|
static void timeout(int);
|
|
|
|
static void timewarn(int);
|
|
|
|
static void usage(const char *);
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2005-01-25 08:37:04 +00:00
|
|
|
extern const char **environ;
|
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
int
|
2005-01-25 08:37:04 +00:00
|
|
|
main(int argc, char **argv)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2001-07-15 05:46:07 +00:00
|
|
|
char *p, *endp;
|
1994-05-26 06:35:07 +00:00
|
|
|
struct passwd *pw;
|
|
|
|
int arglen, ch, len, readstdin;
|
|
|
|
|
|
|
|
#ifndef DEBUG
|
1997-06-19 14:28:32 +00:00
|
|
|
if (geteuid())
|
|
|
|
errx(1, "NOT super-user");
|
1994-05-26 06:35:07 +00:00
|
|
|
#endif
|
2010-12-30 18:06:31 +00:00
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
nosync = NULL;
|
|
|
|
readstdin = 0;
|
2010-12-30 18:06:31 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Test for the special case where the utility is called as
|
|
|
|
* "poweroff", for which it runs 'shutdown -p now'.
|
|
|
|
*/
|
2012-01-03 18:51:58 +00:00
|
|
|
if ((p = strrchr(argv[0], '/')) == NULL)
|
2010-12-30 18:06:31 +00:00
|
|
|
p = argv[0];
|
|
|
|
else
|
|
|
|
++p;
|
|
|
|
if (strcmp(p, "poweroff") == 0) {
|
|
|
|
if (getopt(argc, argv, "") != -1)
|
|
|
|
usage((char *)NULL);
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
if (argc != 0)
|
|
|
|
usage((char *)NULL);
|
|
|
|
dopower = 1;
|
|
|
|
offset = 0;
|
|
|
|
(void)time(&shuttime);
|
|
|
|
goto poweroff;
|
|
|
|
}
|
|
|
|
|
1999-06-21 16:06:21 +00:00
|
|
|
while ((ch = getopt(argc, argv, "-hknopr")) != -1)
|
1994-05-26 06:35:07 +00:00
|
|
|
switch (ch) {
|
|
|
|
case '-':
|
|
|
|
readstdin = 1;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
dohalt = 1;
|
|
|
|
break;
|
|
|
|
case 'k':
|
|
|
|
killflg = 1;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
nosync = "-n";
|
|
|
|
break;
|
1999-06-21 16:06:21 +00:00
|
|
|
case 'o':
|
|
|
|
oflag = 1;
|
|
|
|
break;
|
1998-12-10 23:54:02 +00:00
|
|
|
case 'p':
|
|
|
|
dopower = 1;
|
|
|
|
break;
|
1994-05-26 06:35:07 +00:00
|
|
|
case 'r':
|
|
|
|
doreboot = 1;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
1999-06-21 16:06:21 +00:00
|
|
|
usage((char *)NULL);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc < 1)
|
1999-06-21 16:06:21 +00:00
|
|
|
usage((char *)NULL);
|
1994-05-26 06:35:07 +00:00
|
|
|
|
1999-06-21 16:06:21 +00:00
|
|
|
if (killflg + doreboot + dohalt + dopower > 1)
|
|
|
|
usage("incompatible switches -h, -k, -p and -r");
|
1999-06-21 06:21:05 +00:00
|
|
|
|
1999-06-21 16:06:21 +00:00
|
|
|
if (oflag && !(dohalt || dopower || doreboot))
|
|
|
|
usage("-o requires -h, -p or -r");
|
|
|
|
|
|
|
|
if (nosync != NULL && !oflag)
|
|
|
|
usage("-n requires -o");
|
1999-06-21 06:21:05 +00:00
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
getoffset(*argv++);
|
|
|
|
|
2010-12-30 18:06:31 +00:00
|
|
|
poweroff:
|
1994-05-26 06:35:07 +00:00
|
|
|
if (*argv) {
|
|
|
|
for (p = mbuf, len = sizeof(mbuf); *argv; ++argv) {
|
|
|
|
arglen = strlen(*argv);
|
|
|
|
if ((len -= arglen) <= 2)
|
|
|
|
break;
|
|
|
|
if (p != mbuf)
|
|
|
|
*p++ = ' ';
|
1998-12-11 11:21:47 +00:00
|
|
|
memmove(p, *argv, arglen);
|
1994-05-26 06:35:07 +00:00
|
|
|
p += arglen;
|
|
|
|
}
|
|
|
|
*p = '\n';
|
|
|
|
*++p = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (readstdin) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
{
|
|
|
|
int forkpid;
|
|
|
|
|
|
|
|
forkpid = fork();
|
1997-06-19 14:28:32 +00:00
|
|
|
if (forkpid == -1)
|
|
|
|
err(1, "fork");
|
|
|
|
if (forkpid)
|
|
|
|
errx(0, "[pid %d]", forkpid);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
1997-08-23 14:10:34 +00:00
|
|
|
setsid();
|
1994-05-26 06:35:07 +00:00
|
|
|
#endif
|
|
|
|
openlog("shutdown", LOG_CONS, LOG_AUTH);
|
|
|
|
loop();
|
1998-08-03 06:22:43 +00:00
|
|
|
return(0);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
2009-09-28 07:42:52 +00:00
|
|
|
static void
|
|
|
|
loop(void)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
|
|
|
struct interval *tp;
|
|
|
|
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 {
|
1999-06-18 14:26:07 +00:00
|
|
|
while (tp->timeleft && offset < tp->timeleft)
|
1994-05-26 06:35:07 +00:00
|
|
|
++tp;
|
|
|
|
/*
|
|
|
|
* Warn now, if going to sleep more than a fifth of
|
|
|
|
* the next wait time.
|
|
|
|
*/
|
1998-01-10 20:11:06 +00:00
|
|
|
if ((sltime = offset - tp->timeleft)) {
|
2001-07-15 05:46:07 +00:00
|
|
|
if (sltime > (u_int)(tp->timetowait / 5))
|
1994-05-26 06:35:07 +00:00
|
|
|
timewarn(offset);
|
|
|
|
(void)sleep(sltime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (;; ++tp) {
|
|
|
|
timewarn(tp->timeleft);
|
|
|
|
if (!logged && tp->timeleft <= NOLOG_TIME) {
|
|
|
|
logged = 1;
|
|
|
|
nolog();
|
|
|
|
}
|
|
|
|
(void)sleep((u_int)tp->timetowait);
|
|
|
|
if (!tp->timeleft)
|
|
|
|
break;
|
|
|
|
}
|
2012-08-01 09:10:21 +00:00
|
|
|
die_you_gravy_sucking_pig_dog();
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static jmp_buf alarmbuf;
|
|
|
|
|
2001-07-15 05:46:07 +00:00
|
|
|
static const char *restricted_environ[] = {
|
1998-01-10 20:11:06 +00:00
|
|
|
"PATH=" _PATH_STDPATH,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2009-09-28 07:42:52 +00:00
|
|
|
static void
|
2005-01-25 08:40:51 +00:00
|
|
|
timewarn(int timeleft)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
|
|
|
static int first;
|
|
|
|
static char hostname[MAXHOSTNAMELEN + 1];
|
|
|
|
FILE *pf;
|
|
|
|
char wcmd[MAXPATHLEN + 4];
|
|
|
|
|
|
|
|
if (!first++)
|
|
|
|
(void)gethostname(hostname, sizeof(hostname));
|
|
|
|
|
|
|
|
/* undoc -n option to wall suppresses normal wall banner */
|
|
|
|
(void)snprintf(wcmd, sizeof(wcmd), "%s -n", _PATH_WALL);
|
1998-01-10 20:11:06 +00:00
|
|
|
environ = restricted_environ;
|
1994-05-26 06:35:07 +00: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",
|
|
|
|
timeleft ? "": "FINAL ", whom, hostname);
|
|
|
|
|
|
|
|
if (timeleft > 10*60)
|
|
|
|
(void)fprintf(pf, "System going down at %5.5s\n\n",
|
|
|
|
ctime(&shuttime) + 11);
|
|
|
|
else if (timeleft > 59)
|
|
|
|
(void)fprintf(pf, "System going down in %d minute%s\n\n",
|
|
|
|
timeleft / 60, (timeleft > 60) ? "s" : "");
|
|
|
|
else if (timeleft)
|
2014-12-16 08:29:02 +00:00
|
|
|
(void)fprintf(pf, "System going down in %s30 seconds\n\n",
|
|
|
|
(offset > 0 && offset < 30 ? "less than " : ""));
|
1994-05-26 06:35:07 +00:00
|
|
|
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
|
1998-08-03 06:22:43 +00:00
|
|
|
* probably unnecessary, given that wall is careful.
|
1994-05-26 06:35:07 +00:00
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-28 07:42:52 +00:00
|
|
|
static void
|
2005-01-25 08:40:51 +00:00
|
|
|
timeout(int signo __unused)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
|
|
|
longjmp(alarmbuf, 1);
|
|
|
|
}
|
|
|
|
|
2009-09-28 07:42:52 +00:00
|
|
|
static void
|
2012-08-01 09:10:21 +00:00
|
|
|
die_you_gravy_sucking_pig_dog(void)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
1998-01-10 20:11:06 +00:00
|
|
|
char *empty_environ[] = { NULL };
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
syslog(LOG_NOTICE, "%s by %s: %s",
|
1998-12-10 23:54:02 +00:00
|
|
|
doreboot ? "reboot" : dohalt ? "halt" : dopower ? "power-down" :
|
|
|
|
"shutdown", whom, mbuf);
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
(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-11-27 23:52:13 +00:00
|
|
|
exit(0);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (doreboot)
|
|
|
|
(void)printf("reboot");
|
|
|
|
else if (dohalt)
|
|
|
|
(void)printf("halt");
|
1998-12-10 23:54:02 +00:00
|
|
|
else if (dopower)
|
|
|
|
(void)printf("power-down");
|
1999-06-21 16:06:21 +00:00
|
|
|
if (nosync != NULL)
|
1994-05-26 06:35:07 +00:00
|
|
|
(void)printf(" no sync");
|
|
|
|
(void)printf("\nkill -HUP 1\n");
|
|
|
|
#else
|
1999-06-21 16:06:21 +00:00
|
|
|
if (!oflag) {
|
|
|
|
(void)kill(1, doreboot ? SIGINT : /* reboot */
|
|
|
|
dohalt ? SIGUSR1 : /* halt */
|
|
|
|
dopower ? SIGUSR2 : /* power-down */
|
|
|
|
SIGTERM); /* single-user */
|
|
|
|
} else {
|
|
|
|
if (doreboot) {
|
|
|
|
execle(_PATH_REBOOT, "reboot", "-l", nosync,
|
|
|
|
(char *)NULL, empty_environ);
|
|
|
|
syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
|
|
|
|
_PATH_REBOOT);
|
|
|
|
warn(_PATH_REBOOT);
|
|
|
|
}
|
|
|
|
else if (dohalt) {
|
|
|
|
execle(_PATH_HALT, "halt", "-l", nosync,
|
|
|
|
(char *)NULL, empty_environ);
|
|
|
|
syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
|
|
|
|
_PATH_HALT);
|
|
|
|
warn(_PATH_HALT);
|
|
|
|
}
|
|
|
|
else if (dopower) {
|
|
|
|
execle(_PATH_HALT, "halt", "-l", "-p", nosync,
|
|
|
|
(char *)NULL, empty_environ);
|
|
|
|
syslog(LOG_ERR, "shutdown: can't exec %s: %m.",
|
|
|
|
_PATH_HALT);
|
|
|
|
warn(_PATH_HALT);
|
|
|
|
}
|
|
|
|
(void)kill(1, SIGTERM); /* to single-user */
|
1998-12-10 23:54:02 +00:00
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
#endif
|
|
|
|
finish(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define ATOI2(p) (p[0] - '0') * 10 + (p[1] - '0'); p += 2;
|
|
|
|
|
2009-09-28 07:42:52 +00:00
|
|
|
static void
|
2005-01-25 08:40:51 +00:00
|
|
|
getoffset(char *timearg)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2001-07-15 05:46:07 +00:00
|
|
|
struct tm *lt;
|
|
|
|
char *p;
|
1994-05-26 06:35:07 +00:00
|
|
|
time_t now;
|
1998-01-08 02:21:30 +00:00
|
|
|
int this_year;
|
2014-12-16 08:29:02 +00:00
|
|
|
char *timeunit;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
1999-06-21 06:21:05 +00:00
|
|
|
(void)time(&now);
|
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
if (!strcasecmp(timearg, "now")) { /* now */
|
|
|
|
offset = 0;
|
1999-06-21 06:21:05 +00:00
|
|
|
shuttime = now;
|
1994-05-26 06:35:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*timearg == '+') { /* +minutes */
|
|
|
|
if (!isdigit(*++timearg))
|
|
|
|
badtime();
|
2014-12-16 08:29:02 +00:00
|
|
|
errno = 0;
|
|
|
|
offset = strtol(timearg, &timeunit, 10);
|
|
|
|
if (offset < 0 || offset == LONG_MAX || errno != 0)
|
1999-06-18 14:26:07 +00:00
|
|
|
badtime();
|
2014-12-16 08:29:02 +00:00
|
|
|
if (timeunit[0] == '\0' || strcasecmp(timeunit, "m") == 0 ||
|
|
|
|
strcasecmp(timeunit, "min") == 0 ||
|
|
|
|
strcasecmp(timeunit, "mins") == 0) {
|
|
|
|
offset *= 60;
|
|
|
|
} else if (strcasecmp(timeunit, "h") == 0 ||
|
|
|
|
strcasecmp(timeunit, "hour") == 0 ||
|
|
|
|
strcasecmp(timeunit, "hours") == 0) {
|
|
|
|
offset *= 60 * 60;
|
|
|
|
} else if (strcasecmp(timeunit, "s") == 0 ||
|
|
|
|
strcasecmp(timeunit, "sec") == 0 ||
|
|
|
|
strcasecmp(timeunit, "secs") == 0) {
|
|
|
|
offset *= 1;
|
|
|
|
} else {
|
|
|
|
badtime();
|
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
shuttime = now + offset;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handle hh:mm by getting rid of the colon */
|
|
|
|
for (p = timearg; *p; ++p)
|
1999-07-21 02:49:42 +00:00
|
|
|
if (!isascii(*p) || !isdigit(*p)) {
|
1994-05-26 06:35:07 +00:00
|
|
|
if (*p == ':' && strlen(p) == 3) {
|
|
|
|
p[0] = p[1];
|
|
|
|
p[1] = p[2];
|
|
|
|
p[2] = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
badtime();
|
1999-07-21 02:49:42 +00:00
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
unsetenv("TZ"); /* OUR timezone */
|
|
|
|
lt = localtime(&now); /* current time val */
|
|
|
|
|
|
|
|
switch(strlen(timearg)) {
|
|
|
|
case 10:
|
1998-01-08 02:21:30 +00:00
|
|
|
this_year = lt->tm_year;
|
1994-05-26 06:35:07 +00:00
|
|
|
lt->tm_year = ATOI2(timearg);
|
1998-01-08 02:21:30 +00:00
|
|
|
/*
|
|
|
|
* check if the specified year is in the next century.
|
|
|
|
* allow for one year of user error as many people will
|
|
|
|
* enter n - 1 at the start of year n.
|
|
|
|
*/
|
|
|
|
if (lt->tm_year < (this_year % 100) - 1)
|
|
|
|
lt->tm_year += 100;
|
1998-01-08 02:23:59 +00:00
|
|
|
/* adjust for the year 2000 and beyond */
|
1998-01-08 02:21:30 +00:00
|
|
|
lt->tm_year += (this_year - (this_year % 100));
|
1994-05-26 06:35:07 +00:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 8:
|
|
|
|
lt->tm_mon = ATOI2(timearg);
|
|
|
|
if (--lt->tm_mon < 0 || lt->tm_mon > 11)
|
|
|
|
badtime();
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 6:
|
|
|
|
lt->tm_mday = ATOI2(timearg);
|
|
|
|
if (lt->tm_mday < 1 || lt->tm_mday > 31)
|
|
|
|
badtime();
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 4:
|
|
|
|
lt->tm_hour = ATOI2(timearg);
|
|
|
|
if (lt->tm_hour < 0 || lt->tm_hour > 23)
|
|
|
|
badtime();
|
|
|
|
lt->tm_min = ATOI2(timearg);
|
|
|
|
if (lt->tm_min < 0 || lt->tm_min > 59)
|
|
|
|
badtime();
|
|
|
|
lt->tm_sec = 0;
|
|
|
|
if ((shuttime = mktime(lt)) == -1)
|
|
|
|
badtime();
|
1997-06-19 14:28:32 +00:00
|
|
|
if ((offset = shuttime - now) < 0)
|
|
|
|
errx(1, "that time is already past.");
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
badtime();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define NOMSG "\n\nNO LOGINS: System going down at "
|
2009-09-28 07:42:52 +00:00
|
|
|
static void
|
|
|
|
nolog(void)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
|
|
|
int logfd;
|
|
|
|
char *ct;
|
|
|
|
|
|
|
|
(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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-28 07:42:52 +00:00
|
|
|
static void
|
2005-01-25 08:40:51 +00:00
|
|
|
finish(int signo __unused)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
1998-12-11 11:21:47 +00:00
|
|
|
if (!killflg)
|
|
|
|
(void)unlink(_PATH_NOLOGIN);
|
1994-05-26 06:35:07 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2009-09-28 07:42:52 +00:00
|
|
|
static void
|
2009-12-29 08:49:43 +00:00
|
|
|
badtime(void)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
1998-08-03 06:22:43 +00:00
|
|
|
errx(1, "bad time format");
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
2009-09-28 07:42:52 +00:00
|
|
|
static void
|
2005-01-25 08:40:51 +00:00
|
|
|
usage(const char *cp)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
1999-06-21 16:06:21 +00:00
|
|
|
if (cp != NULL)
|
|
|
|
warnx("%s", cp);
|
|
|
|
(void)fprintf(stderr,
|
2010-12-30 18:06:31 +00:00
|
|
|
"usage: shutdown [-] [-h | -p | -r | -k] [-o [-n]] time [warning-message ...]\n"
|
|
|
|
" poweroff\n");
|
1994-05-26 06:35:07 +00:00
|
|
|
exit(1);
|
|
|
|
}
|