Changeset View
Changeset View
Standalone View
Standalone View
sbin/shutdown/shutdown.c
Show First 20 Lines • Show All 83 Lines • ▼ Show 20 Lines | static struct interval { | ||||
{ 30 S, 30 S }, | { 30 S, 30 S }, | ||||
{ 0 , 0 } | { 0 , 0 } | ||||
}; | }; | ||||
#undef H | #undef H | ||||
#undef M | #undef M | ||||
#undef S | #undef S | ||||
static time_t offset, shuttime; | static time_t offset, shuttime; | ||||
static int dohalt, dopower, doreboot, killflg, mbuflen, oflag; | static int docycle, dohalt, dopower, doreboot, killflg, mbuflen, oflag; | ||||
static char mbuf[BUFSIZ]; | static char mbuf[BUFSIZ]; | ||||
static const char *nosync, *whom; | static const char *nosync, *whom; | ||||
static void badtime(void); | static void badtime(void); | ||||
static void die_you_gravy_sucking_pig_dog(void); | static void die_you_gravy_sucking_pig_dog(void); | ||||
static void finish(int); | static void finish(int); | ||||
static void getoffset(char *); | static void getoffset(char *); | ||||
static void loop(void); | static void loop(void); | ||||
Show All 35 Lines | if (strcmp(p, "poweroff") == 0) { | ||||
if (argc != 0) | if (argc != 0) | ||||
usage((char *)NULL); | usage((char *)NULL); | ||||
dopower = 1; | dopower = 1; | ||||
offset = 0; | offset = 0; | ||||
(void)time(&shuttime); | (void)time(&shuttime); | ||||
goto poweroff; | goto poweroff; | ||||
} | } | ||||
while ((ch = getopt(argc, argv, "-hknopr")) != -1) | while ((ch = getopt(argc, argv, "-chknopr")) != -1) | ||||
switch (ch) { | switch (ch) { | ||||
case '-': | case '-': | ||||
readstdin = 1; | readstdin = 1; | ||||
break; | break; | ||||
case 'c': | |||||
docycle = 1; | |||||
break; | |||||
case 'h': | case 'h': | ||||
dohalt = 1; | dohalt = 1; | ||||
break; | break; | ||||
case 'k': | case 'k': | ||||
killflg = 1; | killflg = 1; | ||||
break; | break; | ||||
case 'n': | case 'n': | ||||
nosync = "-n"; | nosync = "-n"; | ||||
Show All 12 Lines | default: | ||||
usage((char *)NULL); | usage((char *)NULL); | ||||
} | } | ||||
argc -= optind; | argc -= optind; | ||||
argv += optind; | argv += optind; | ||||
if (argc < 1) | if (argc < 1) | ||||
usage((char *)NULL); | usage((char *)NULL); | ||||
if (killflg + doreboot + dohalt + dopower > 1) | if (killflg + doreboot + dohalt + dopower + docycle > 1) | ||||
usage("incompatible switches -h, -k, -p and -r"); | usage("incompatible switches -c, -h, -k, -p and -r"); | ||||
if (oflag && !(dohalt || dopower || doreboot)) | if (oflag && !(dohalt || dopower || doreboot || docycle)) | ||||
usage("-o requires -h, -p or -r"); | usage("-o requires -c, -h, -p or -r"); | ||||
if (nosync != NULL && !oflag) | if (nosync != NULL && !oflag) | ||||
usage("-n requires -o"); | usage("-n requires -o"); | ||||
getoffset(*argv++); | getoffset(*argv++); | ||||
poweroff: | poweroff: | ||||
if (*argv) { | if (*argv) { | ||||
▲ Show 20 Lines • Show All 161 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
static void | static void | ||||
die_you_gravy_sucking_pig_dog(void) | die_you_gravy_sucking_pig_dog(void) | ||||
{ | { | ||||
char *empty_environ[] = { NULL }; | char *empty_environ[] = { NULL }; | ||||
syslog(LOG_NOTICE, "%s by %s: %s", | syslog(LOG_NOTICE, "%s by %s: %s", | ||||
doreboot ? "reboot" : dohalt ? "halt" : dopower ? "power-down" : | doreboot ? "reboot" : dohalt ? "halt" : dopower ? "power-down" : | ||||
"shutdown", whom, mbuf); | docycle ? "power-cycle" : "shutdown", whom, mbuf); | ||||
(void)printf("\r\nSystem shutdown time has arrived\007\007\r\n"); | (void)printf("\r\nSystem shutdown time has arrived\007\007\r\n"); | ||||
if (killflg) { | if (killflg) { | ||||
(void)printf("\rbut you'll have to do it yourself\r\n"); | (void)printf("\rbut you'll have to do it yourself\r\n"); | ||||
exit(0); | exit(0); | ||||
} | } | ||||
#ifdef DEBUG | #ifdef DEBUG | ||||
if (doreboot) | if (doreboot) | ||||
(void)printf("reboot"); | (void)printf("reboot"); | ||||
else if (docycle) | |||||
(void)printf("power-cycle"); | |||||
else if (dohalt) | else if (dohalt) | ||||
(void)printf("halt"); | (void)printf("halt"); | ||||
else if (dopower) | else if (dopower) | ||||
(void)printf("power-down"); | (void)printf("power-down"); | ||||
if (nosync != NULL) | if (nosync != NULL) | ||||
(void)printf(" no sync"); | (void)printf(" no sync"); | ||||
(void)printf("\nkill -HUP 1\n"); | (void)printf("\nkill -HUP 1\n"); | ||||
#else | #else | ||||
if (!oflag) { | if (!oflag) { | ||||
(void)kill(1, doreboot ? SIGINT : /* reboot */ | (void)kill(1, doreboot ? SIGINT : /* reboot */ | ||||
dohalt ? SIGUSR1 : /* halt */ | dohalt ? SIGUSR1 : /* halt */ | ||||
dopower ? SIGUSR2 : /* power-down */ | dopower ? SIGUSR2 : /* power-down */ | ||||
docycle ? SIGWINCH : /* power-cycle */ | |||||
SIGTERM); /* single-user */ | SIGTERM); /* single-user */ | ||||
} else { | } else { | ||||
if (doreboot) { | if (doreboot) { | ||||
execle(_PATH_REBOOT, "reboot", "-l", nosync, | execle(_PATH_REBOOT, "reboot", "-l", nosync, | ||||
(char *)NULL, empty_environ); | (char *)NULL, empty_environ); | ||||
syslog(LOG_ERR, "shutdown: can't exec %s: %m.", | syslog(LOG_ERR, "shutdown: can't exec %s: %m.", | ||||
_PATH_REBOOT); | _PATH_REBOOT); | ||||
warn(_PATH_REBOOT); | warn(_PATH_REBOOT); | ||||
} | } | ||||
else if (dohalt) { | else if (dohalt) { | ||||
execle(_PATH_HALT, "halt", "-l", nosync, | execle(_PATH_HALT, "halt", "-l", nosync, | ||||
(char *)NULL, empty_environ); | (char *)NULL, empty_environ); | ||||
syslog(LOG_ERR, "shutdown: can't exec %s: %m.", | syslog(LOG_ERR, "shutdown: can't exec %s: %m.", | ||||
_PATH_HALT); | _PATH_HALT); | ||||
warn(_PATH_HALT); | warn(_PATH_HALT); | ||||
} | } | ||||
else if (dopower) { | else if (dopower) { | ||||
execle(_PATH_HALT, "halt", "-l", "-p", nosync, | 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); | |||||
} | |||||
else if (docycle) { | |||||
execle(_PATH_HALT, "halt", "-l", "-c", nosync, | |||||
(char *)NULL, empty_environ); | (char *)NULL, empty_environ); | ||||
syslog(LOG_ERR, "shutdown: can't exec %s: %m.", | syslog(LOG_ERR, "shutdown: can't exec %s: %m.", | ||||
_PATH_HALT); | _PATH_HALT); | ||||
warn(_PATH_HALT); | warn(_PATH_HALT); | ||||
} | } | ||||
(void)kill(1, SIGTERM); /* to single-user */ | (void)kill(1, SIGTERM); /* to single-user */ | ||||
} | } | ||||
#endif | #endif | ||||
▲ Show 20 Lines • Show All 152 Lines • Show Last 20 Lines |