18fae3551SRodney W. Grimes /* 28fae3551SRodney W. Grimes * Copyright (c) 1988, 1990, 1993 38fae3551SRodney W. Grimes * The Regents of the University of California. All rights reserved. 48fae3551SRodney W. Grimes * 58fae3551SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 68fae3551SRodney W. Grimes * modification, are permitted provided that the following conditions 78fae3551SRodney W. Grimes * are met: 88fae3551SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 98fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 108fae3551SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 118fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 128fae3551SRodney W. Grimes * documentation and/or other materials provided with the distribution. 138fae3551SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 148fae3551SRodney W. Grimes * may be used to endorse or promote products derived from this software 158fae3551SRodney W. Grimes * without specific prior written permission. 168fae3551SRodney W. Grimes * 178fae3551SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 188fae3551SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 198fae3551SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 208fae3551SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 218fae3551SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 228fae3551SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 238fae3551SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 248fae3551SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 258fae3551SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 268fae3551SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 278fae3551SRodney W. Grimes * SUCH DAMAGE. 288fae3551SRodney W. Grimes */ 298fae3551SRodney W. Grimes 30c69284caSDavid E. O'Brien #if 0 318fae3551SRodney W. Grimes #ifndef lint 32d3714863SPhilippe Charnier static const char copyright[] = 338fae3551SRodney W. Grimes "@(#) Copyright (c) 1988, 1990, 1993\n\ 348fae3551SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 358fae3551SRodney W. Grimes #endif /* not lint */ 368fae3551SRodney W. Grimes 378fae3551SRodney W. Grimes #ifndef lint 3839b831afSBruce Evans static char sccsid[] = "@(#)shutdown.c 8.4 (Berkeley) 4/28/95"; 398fae3551SRodney W. Grimes #endif /* not lint */ 40c69284caSDavid E. O'Brien #endif 41c69284caSDavid E. O'Brien #include <sys/cdefs.h> 42c69284caSDavid E. O'Brien __FBSDID("$FreeBSD$"); 438fae3551SRodney W. Grimes 448fae3551SRodney W. Grimes #include <sys/param.h> 458fae3551SRodney W. Grimes #include <sys/time.h> 468fae3551SRodney W. Grimes #include <sys/resource.h> 478fae3551SRodney W. Grimes #include <sys/syslog.h> 488fae3551SRodney W. Grimes 498fae3551SRodney W. Grimes #include <ctype.h> 50d3714863SPhilippe Charnier #include <err.h> 51*c7d73a4dSGleb Kurtsou #include <errno.h> 528fae3551SRodney W. Grimes #include <fcntl.h> 538d5c19ffSDavid E. O'Brien #include <paths.h> 548fae3551SRodney W. Grimes #include <pwd.h> 558fae3551SRodney W. Grimes #include <setjmp.h> 568fae3551SRodney W. Grimes #include <signal.h> 578fae3551SRodney W. Grimes #include <stdio.h> 588fae3551SRodney W. Grimes #include <stdlib.h> 598fae3551SRodney W. Grimes #include <string.h> 608fae3551SRodney W. Grimes #include <unistd.h> 618fae3551SRodney W. Grimes 628fae3551SRodney W. Grimes #ifdef DEBUG 638fae3551SRodney W. Grimes #undef _PATH_NOLOGIN 648fae3551SRodney W. Grimes #define _PATH_NOLOGIN "./nologin" 658fae3551SRodney W. Grimes #endif 668fae3551SRodney W. Grimes 678fae3551SRodney W. Grimes #define H *60*60 688fae3551SRodney W. Grimes #define M *60 698fae3551SRodney W. Grimes #define S *1 708fae3551SRodney W. Grimes #define NOLOG_TIME 5*60 711efe3c6bSEd Schouten static struct interval { 728fae3551SRodney W. Grimes int timeleft, timetowait; 738fae3551SRodney W. Grimes } tlist[] = { 74f6faa785SAlexander Langer { 10 H, 5 H }, 75f6faa785SAlexander Langer { 5 H, 3 H }, 76f6faa785SAlexander Langer { 2 H, 1 H }, 77f6faa785SAlexander Langer { 1 H, 30 M }, 78f6faa785SAlexander Langer { 30 M, 10 M }, 79f6faa785SAlexander Langer { 20 M, 10 M }, 80f6faa785SAlexander Langer { 10 M, 5 M }, 81f6faa785SAlexander Langer { 5 M, 3 M }, 82f6faa785SAlexander Langer { 2 M, 1 M }, 83f6faa785SAlexander Langer { 1 M, 30 S }, 84f6faa785SAlexander Langer { 30 S, 30 S }, 85f6faa785SAlexander Langer { 0 , 0 } 868fae3551SRodney W. Grimes }; 878fae3551SRodney W. Grimes #undef H 888fae3551SRodney W. Grimes #undef M 898fae3551SRodney W. Grimes #undef S 908fae3551SRodney W. Grimes 918fae3551SRodney W. Grimes static time_t offset, shuttime; 9235cd460fSRuslan Ermilov static int dohalt, dopower, doreboot, killflg, mbuflen, oflag; 9304983c4fSDima Dorfman static char mbuf[BUFSIZ]; 9404983c4fSDima Dorfman static const char *nosync, *whom; 958fae3551SRodney W. Grimes 969534110fSXin LI static void badtime(void); 9739faed57SDag-Erling Smørgrav static void die_you_gravy_sucking_pig_dog(void); 989534110fSXin LI static void finish(int); 999534110fSXin LI static void getoffset(char *); 1009534110fSXin LI static void loop(void); 1019534110fSXin LI static void nolog(void); 1029534110fSXin LI static void timeout(int); 1039534110fSXin LI static void timewarn(int); 1049534110fSXin LI static void usage(const char *); 1058fae3551SRodney W. Grimes 106f906f843SXin LI extern const char **environ; 107f906f843SXin LI 1088fae3551SRodney W. Grimes int 109f906f843SXin LI main(int argc, char **argv) 1108fae3551SRodney W. Grimes { 11104983c4fSDima Dorfman char *p, *endp; 1128fae3551SRodney W. Grimes struct passwd *pw; 1138fae3551SRodney W. Grimes int arglen, ch, len, readstdin; 1148fae3551SRodney W. Grimes 1158fae3551SRodney W. Grimes #ifndef DEBUG 116a2bfcdfdSPhilippe Charnier if (geteuid()) 117a2bfcdfdSPhilippe Charnier errx(1, "NOT super-user"); 1188fae3551SRodney W. Grimes #endif 1196868734cSPawel Jakub Dawidek 1208fae3551SRodney W. Grimes nosync = NULL; 1218fae3551SRodney W. Grimes readstdin = 0; 1226868734cSPawel Jakub Dawidek 1236868734cSPawel Jakub Dawidek /* 1246868734cSPawel Jakub Dawidek * Test for the special case where the utility is called as 1256868734cSPawel Jakub Dawidek * "poweroff", for which it runs 'shutdown -p now'. 1266868734cSPawel Jakub Dawidek */ 127b3608ae1SEd Schouten if ((p = strrchr(argv[0], '/')) == NULL) 1286868734cSPawel Jakub Dawidek p = argv[0]; 1296868734cSPawel Jakub Dawidek else 1306868734cSPawel Jakub Dawidek ++p; 1316868734cSPawel Jakub Dawidek if (strcmp(p, "poweroff") == 0) { 1326868734cSPawel Jakub Dawidek if (getopt(argc, argv, "") != -1) 1336868734cSPawel Jakub Dawidek usage((char *)NULL); 1346868734cSPawel Jakub Dawidek argc -= optind; 1356868734cSPawel Jakub Dawidek argv += optind; 1366868734cSPawel Jakub Dawidek if (argc != 0) 1376868734cSPawel Jakub Dawidek usage((char *)NULL); 1386868734cSPawel Jakub Dawidek dopower = 1; 1396868734cSPawel Jakub Dawidek offset = 0; 1406868734cSPawel Jakub Dawidek (void)time(&shuttime); 1416868734cSPawel Jakub Dawidek goto poweroff; 1426868734cSPawel Jakub Dawidek } 1436868734cSPawel Jakub Dawidek 14435cd460fSRuslan Ermilov while ((ch = getopt(argc, argv, "-hknopr")) != -1) 1458fae3551SRodney W. Grimes switch (ch) { 1468fae3551SRodney W. Grimes case '-': 1478fae3551SRodney W. Grimes readstdin = 1; 1488fae3551SRodney W. Grimes break; 1498fae3551SRodney W. Grimes case 'h': 1508fae3551SRodney W. Grimes dohalt = 1; 1518fae3551SRodney W. Grimes break; 1528fae3551SRodney W. Grimes case 'k': 1538fae3551SRodney W. Grimes killflg = 1; 1548fae3551SRodney W. Grimes break; 1558fae3551SRodney W. Grimes case 'n': 1568fae3551SRodney W. Grimes nosync = "-n"; 1578fae3551SRodney W. Grimes break; 15835cd460fSRuslan Ermilov case 'o': 15935cd460fSRuslan Ermilov oflag = 1; 16035cd460fSRuslan Ermilov break; 1617e2d04d7SMike Smith case 'p': 1627e2d04d7SMike Smith dopower = 1; 1637e2d04d7SMike Smith break; 1648fae3551SRodney W. Grimes case 'r': 1658fae3551SRodney W. Grimes doreboot = 1; 1668fae3551SRodney W. Grimes break; 1678fae3551SRodney W. Grimes case '?': 1688fae3551SRodney W. Grimes default: 16935cd460fSRuslan Ermilov usage((char *)NULL); 1708fae3551SRodney W. Grimes } 1718fae3551SRodney W. Grimes argc -= optind; 1728fae3551SRodney W. Grimes argv += optind; 1738fae3551SRodney W. Grimes 1748fae3551SRodney W. Grimes if (argc < 1) 17535cd460fSRuslan Ermilov usage((char *)NULL); 1768fae3551SRodney W. Grimes 17735cd460fSRuslan Ermilov if (killflg + doreboot + dohalt + dopower > 1) 17835cd460fSRuslan Ermilov usage("incompatible switches -h, -k, -p and -r"); 1791d4f4c0dSJoseph Koshy 18035cd460fSRuslan Ermilov if (oflag && !(dohalt || dopower || doreboot)) 18135cd460fSRuslan Ermilov usage("-o requires -h, -p or -r"); 18235cd460fSRuslan Ermilov 18335cd460fSRuslan Ermilov if (nosync != NULL && !oflag) 18435cd460fSRuslan Ermilov usage("-n requires -o"); 1851d4f4c0dSJoseph Koshy 1868fae3551SRodney W. Grimes getoffset(*argv++); 1878fae3551SRodney W. Grimes 1886868734cSPawel Jakub Dawidek poweroff: 1898fae3551SRodney W. Grimes if (*argv) { 1908fae3551SRodney W. Grimes for (p = mbuf, len = sizeof(mbuf); *argv; ++argv) { 1918fae3551SRodney W. Grimes arglen = strlen(*argv); 1928fae3551SRodney W. Grimes if ((len -= arglen) <= 2) 1938fae3551SRodney W. Grimes break; 1948fae3551SRodney W. Grimes if (p != mbuf) 1958fae3551SRodney W. Grimes *p++ = ' '; 19639b831afSBruce Evans memmove(p, *argv, arglen); 1978fae3551SRodney W. Grimes p += arglen; 1988fae3551SRodney W. Grimes } 1998fae3551SRodney W. Grimes *p = '\n'; 2008fae3551SRodney W. Grimes *++p = '\0'; 2018fae3551SRodney W. Grimes } 2028fae3551SRodney W. Grimes 2038fae3551SRodney W. Grimes if (readstdin) { 2048fae3551SRodney W. Grimes p = mbuf; 2058fae3551SRodney W. Grimes endp = mbuf + sizeof(mbuf) - 2; 2068fae3551SRodney W. Grimes for (;;) { 2078fae3551SRodney W. Grimes if (!fgets(p, endp - p + 1, stdin)) 2088fae3551SRodney W. Grimes break; 2098fae3551SRodney W. Grimes for (; *p && p < endp; ++p); 2108fae3551SRodney W. Grimes if (p == endp) { 2118fae3551SRodney W. Grimes *p = '\n'; 2128fae3551SRodney W. Grimes *++p = '\0'; 2138fae3551SRodney W. Grimes break; 2148fae3551SRodney W. Grimes } 2158fae3551SRodney W. Grimes } 2168fae3551SRodney W. Grimes } 2178fae3551SRodney W. Grimes mbuflen = strlen(mbuf); 2188fae3551SRodney W. Grimes 2198fae3551SRodney W. Grimes if (offset) 2208fae3551SRodney W. Grimes (void)printf("Shutdown at %.24s.\n", ctime(&shuttime)); 2218fae3551SRodney W. Grimes else 2228fae3551SRodney W. Grimes (void)printf("Shutdown NOW!\n"); 2238fae3551SRodney W. Grimes 2248fae3551SRodney W. Grimes if (!(whom = getlogin())) 2258fae3551SRodney W. Grimes whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???"; 2268fae3551SRodney W. Grimes 2278fae3551SRodney W. Grimes #ifdef DEBUG 2288fae3551SRodney W. Grimes (void)putc('\n', stdout); 2298fae3551SRodney W. Grimes #else 2308fae3551SRodney W. Grimes (void)setpriority(PRIO_PROCESS, 0, PRIO_MIN); 2318fae3551SRodney W. Grimes { 2328fae3551SRodney W. Grimes int forkpid; 2338fae3551SRodney W. Grimes 2348fae3551SRodney W. Grimes forkpid = fork(); 235a2bfcdfdSPhilippe Charnier if (forkpid == -1) 236a2bfcdfdSPhilippe Charnier err(1, "fork"); 237a2bfcdfdSPhilippe Charnier if (forkpid) 238a2bfcdfdSPhilippe Charnier errx(0, "[pid %d]", forkpid); 2398fae3551SRodney W. Grimes } 24032c9dffbSJoerg Wunsch setsid(); 2418fae3551SRodney W. Grimes #endif 2428fae3551SRodney W. Grimes openlog("shutdown", LOG_CONS, LOG_AUTH); 2438fae3551SRodney W. Grimes loop(); 244d3714863SPhilippe Charnier return(0); 2458fae3551SRodney W. Grimes } 2468fae3551SRodney W. Grimes 2479534110fSXin LI static void 2489534110fSXin LI loop(void) 2498fae3551SRodney W. Grimes { 2508fae3551SRodney W. Grimes struct interval *tp; 2518fae3551SRodney W. Grimes u_int sltime; 2528fae3551SRodney W. Grimes int logged; 2538fae3551SRodney W. Grimes 2548fae3551SRodney W. Grimes if (offset <= NOLOG_TIME) { 2558fae3551SRodney W. Grimes logged = 1; 2568fae3551SRodney W. Grimes nolog(); 2578fae3551SRodney W. Grimes } 2588fae3551SRodney W. Grimes else 2598fae3551SRodney W. Grimes logged = 0; 2608fae3551SRodney W. Grimes tp = tlist; 2618fae3551SRodney W. Grimes if (tp->timeleft < offset) 2628fae3551SRodney W. Grimes (void)sleep((u_int)(offset - tp->timeleft)); 2638fae3551SRodney W. Grimes else { 264966ea00aSRuslan Ermilov while (tp->timeleft && offset < tp->timeleft) 2658fae3551SRodney W. Grimes ++tp; 2668fae3551SRodney W. Grimes /* 2678fae3551SRodney W. Grimes * Warn now, if going to sleep more than a fifth of 2688fae3551SRodney W. Grimes * the next wait time. 2698fae3551SRodney W. Grimes */ 270f6faa785SAlexander Langer if ((sltime = offset - tp->timeleft)) { 27104983c4fSDima Dorfman if (sltime > (u_int)(tp->timetowait / 5)) 2728fae3551SRodney W. Grimes timewarn(offset); 2738fae3551SRodney W. Grimes (void)sleep(sltime); 2748fae3551SRodney W. Grimes } 2758fae3551SRodney W. Grimes } 2768fae3551SRodney W. Grimes for (;; ++tp) { 2778fae3551SRodney W. Grimes timewarn(tp->timeleft); 2788fae3551SRodney W. Grimes if (!logged && tp->timeleft <= NOLOG_TIME) { 2798fae3551SRodney W. Grimes logged = 1; 2808fae3551SRodney W. Grimes nolog(); 2818fae3551SRodney W. Grimes } 2828fae3551SRodney W. Grimes (void)sleep((u_int)tp->timetowait); 2838fae3551SRodney W. Grimes if (!tp->timeleft) 2848fae3551SRodney W. Grimes break; 2858fae3551SRodney W. Grimes } 28639faed57SDag-Erling Smørgrav die_you_gravy_sucking_pig_dog(); 2878fae3551SRodney W. Grimes } 2888fae3551SRodney W. Grimes 2898fae3551SRodney W. Grimes static jmp_buf alarmbuf; 2908fae3551SRodney W. Grimes 29104983c4fSDima Dorfman static const char *restricted_environ[] = { 292f6faa785SAlexander Langer "PATH=" _PATH_STDPATH, 293f6faa785SAlexander Langer NULL 294f6faa785SAlexander Langer }; 295f6faa785SAlexander Langer 2969534110fSXin LI static void 297f74b6bc2SXin LI timewarn(int timeleft) 2988fae3551SRodney W. Grimes { 2998fae3551SRodney W. Grimes static int first; 3008fae3551SRodney W. Grimes static char hostname[MAXHOSTNAMELEN + 1]; 3018fae3551SRodney W. Grimes FILE *pf; 3028fae3551SRodney W. Grimes char wcmd[MAXPATHLEN + 4]; 3038fae3551SRodney W. Grimes 3048fae3551SRodney W. Grimes if (!first++) 3058fae3551SRodney W. Grimes (void)gethostname(hostname, sizeof(hostname)); 3068fae3551SRodney W. Grimes 3078fae3551SRodney W. Grimes /* undoc -n option to wall suppresses normal wall banner */ 3088fae3551SRodney W. Grimes (void)snprintf(wcmd, sizeof(wcmd), "%s -n", _PATH_WALL); 309f6faa785SAlexander Langer environ = restricted_environ; 3108fae3551SRodney W. Grimes if (!(pf = popen(wcmd, "w"))) { 3118fae3551SRodney W. Grimes syslog(LOG_ERR, "shutdown: can't find %s: %m", _PATH_WALL); 3128fae3551SRodney W. Grimes return; 3138fae3551SRodney W. Grimes } 3148fae3551SRodney W. Grimes 3158fae3551SRodney W. Grimes (void)fprintf(pf, 3168fae3551SRodney W. Grimes "\007*** %sSystem shutdown message from %s@%s ***\007\n", 3178fae3551SRodney W. Grimes timeleft ? "": "FINAL ", whom, hostname); 3188fae3551SRodney W. Grimes 3198fae3551SRodney W. Grimes if (timeleft > 10*60) 3208fae3551SRodney W. Grimes (void)fprintf(pf, "System going down at %5.5s\n\n", 3218fae3551SRodney W. Grimes ctime(&shuttime) + 11); 3228fae3551SRodney W. Grimes else if (timeleft > 59) 3238fae3551SRodney W. Grimes (void)fprintf(pf, "System going down in %d minute%s\n\n", 3248fae3551SRodney W. Grimes timeleft / 60, (timeleft > 60) ? "s" : ""); 3258fae3551SRodney W. Grimes else if (timeleft) 326*c7d73a4dSGleb Kurtsou (void)fprintf(pf, "System going down in %s30 seconds\n\n", 327*c7d73a4dSGleb Kurtsou (offset > 0 && offset < 30 ? "less than " : "")); 3288fae3551SRodney W. Grimes else 3298fae3551SRodney W. Grimes (void)fprintf(pf, "System going down IMMEDIATELY\n\n"); 3308fae3551SRodney W. Grimes 3318fae3551SRodney W. Grimes if (mbuflen) 3328fae3551SRodney W. Grimes (void)fwrite(mbuf, sizeof(*mbuf), mbuflen, pf); 3338fae3551SRodney W. Grimes 3348fae3551SRodney W. Grimes /* 3358fae3551SRodney W. Grimes * play some games, just in case wall doesn't come back 336d3714863SPhilippe Charnier * probably unnecessary, given that wall is careful. 3378fae3551SRodney W. Grimes */ 3388fae3551SRodney W. Grimes if (!setjmp(alarmbuf)) { 3398fae3551SRodney W. Grimes (void)signal(SIGALRM, timeout); 3408fae3551SRodney W. Grimes (void)alarm((u_int)30); 3418fae3551SRodney W. Grimes (void)pclose(pf); 3428fae3551SRodney W. Grimes (void)alarm((u_int)0); 3438fae3551SRodney W. Grimes (void)signal(SIGALRM, SIG_DFL); 3448fae3551SRodney W. Grimes } 3458fae3551SRodney W. Grimes } 3468fae3551SRodney W. Grimes 3479534110fSXin LI static void 348f74b6bc2SXin LI timeout(int signo __unused) 3498fae3551SRodney W. Grimes { 3508fae3551SRodney W. Grimes longjmp(alarmbuf, 1); 3518fae3551SRodney W. Grimes } 3528fae3551SRodney W. Grimes 3539534110fSXin LI static void 35439faed57SDag-Erling Smørgrav die_you_gravy_sucking_pig_dog(void) 3558fae3551SRodney W. Grimes { 356f6faa785SAlexander Langer char *empty_environ[] = { NULL }; 3578fae3551SRodney W. Grimes 3588fae3551SRodney W. Grimes syslog(LOG_NOTICE, "%s by %s: %s", 3597e2d04d7SMike Smith doreboot ? "reboot" : dohalt ? "halt" : dopower ? "power-down" : 3607e2d04d7SMike Smith "shutdown", whom, mbuf); 3618fae3551SRodney W. Grimes 3628fae3551SRodney W. Grimes (void)printf("\r\nSystem shutdown time has arrived\007\007\r\n"); 3638fae3551SRodney W. Grimes if (killflg) { 3648fae3551SRodney W. Grimes (void)printf("\rbut you'll have to do it yourself\r\n"); 3657dd4667fSAndreas Schulz exit(0); 3668fae3551SRodney W. Grimes } 3678fae3551SRodney W. Grimes #ifdef DEBUG 3688fae3551SRodney W. Grimes if (doreboot) 3698fae3551SRodney W. Grimes (void)printf("reboot"); 3708fae3551SRodney W. Grimes else if (dohalt) 3718fae3551SRodney W. Grimes (void)printf("halt"); 3727e2d04d7SMike Smith else if (dopower) 3737e2d04d7SMike Smith (void)printf("power-down"); 37435cd460fSRuslan Ermilov if (nosync != NULL) 3758fae3551SRodney W. Grimes (void)printf(" no sync"); 3768fae3551SRodney W. Grimes (void)printf("\nkill -HUP 1\n"); 3778fae3551SRodney W. Grimes #else 37835cd460fSRuslan Ermilov if (!oflag) { 37935cd460fSRuslan Ermilov (void)kill(1, doreboot ? SIGINT : /* reboot */ 38035cd460fSRuslan Ermilov dohalt ? SIGUSR1 : /* halt */ 38135cd460fSRuslan Ermilov dopower ? SIGUSR2 : /* power-down */ 38235cd460fSRuslan Ermilov SIGTERM); /* single-user */ 38335cd460fSRuslan Ermilov } else { 3848fae3551SRodney W. Grimes if (doreboot) { 385f6faa785SAlexander Langer execle(_PATH_REBOOT, "reboot", "-l", nosync, 386f6faa785SAlexander Langer (char *)NULL, empty_environ); 38735cd460fSRuslan Ermilov syslog(LOG_ERR, "shutdown: can't exec %s: %m.", 38835cd460fSRuslan Ermilov _PATH_REBOOT); 389f6faa785SAlexander Langer warn(_PATH_REBOOT); 3908fae3551SRodney W. Grimes } 3918fae3551SRodney W. Grimes else if (dohalt) { 392f6faa785SAlexander Langer execle(_PATH_HALT, "halt", "-l", nosync, 393f6faa785SAlexander Langer (char *)NULL, empty_environ); 39435cd460fSRuslan Ermilov syslog(LOG_ERR, "shutdown: can't exec %s: %m.", 39535cd460fSRuslan Ermilov _PATH_HALT); 396f6faa785SAlexander Langer warn(_PATH_HALT); 3978fae3551SRodney W. Grimes } 3987e2d04d7SMike Smith else if (dopower) { 3997e2d04d7SMike Smith execle(_PATH_HALT, "halt", "-l", "-p", nosync, 4007e2d04d7SMike Smith (char *)NULL, empty_environ); 40135cd460fSRuslan Ermilov syslog(LOG_ERR, "shutdown: can't exec %s: %m.", 40235cd460fSRuslan Ermilov _PATH_HALT); 4037e2d04d7SMike Smith warn(_PATH_HALT); 4047e2d04d7SMike Smith } 40535cd460fSRuslan Ermilov (void)kill(1, SIGTERM); /* to single-user */ 40635cd460fSRuslan Ermilov } 4078fae3551SRodney W. Grimes #endif 4088fae3551SRodney W. Grimes finish(0); 4098fae3551SRodney W. Grimes } 4108fae3551SRodney W. Grimes 4118fae3551SRodney W. Grimes #define ATOI2(p) (p[0] - '0') * 10 + (p[1] - '0'); p += 2; 4128fae3551SRodney W. Grimes 4139534110fSXin LI static void 414f74b6bc2SXin LI getoffset(char *timearg) 4158fae3551SRodney W. Grimes { 41604983c4fSDima Dorfman struct tm *lt; 41704983c4fSDima Dorfman char *p; 4188fae3551SRodney W. Grimes time_t now; 4192dc34227SAlexander Langer int this_year; 420*c7d73a4dSGleb Kurtsou char *timeunit; 4218fae3551SRodney W. Grimes 4221d4f4c0dSJoseph Koshy (void)time(&now); 4231d4f4c0dSJoseph Koshy 4248fae3551SRodney W. Grimes if (!strcasecmp(timearg, "now")) { /* now */ 4258fae3551SRodney W. Grimes offset = 0; 4261d4f4c0dSJoseph Koshy shuttime = now; 4278fae3551SRodney W. Grimes return; 4288fae3551SRodney W. Grimes } 4298fae3551SRodney W. Grimes 4308fae3551SRodney W. Grimes if (*timearg == '+') { /* +minutes */ 4318fae3551SRodney W. Grimes if (!isdigit(*++timearg)) 4328fae3551SRodney W. Grimes badtime(); 433*c7d73a4dSGleb Kurtsou errno = 0; 434*c7d73a4dSGleb Kurtsou offset = strtol(timearg, &timeunit, 10); 435*c7d73a4dSGleb Kurtsou if (offset < 0 || offset == LONG_MAX || errno != 0) 436966ea00aSRuslan Ermilov badtime(); 437*c7d73a4dSGleb Kurtsou if (timeunit[0] == '\0' || strcasecmp(timeunit, "m") == 0 || 438*c7d73a4dSGleb Kurtsou strcasecmp(timeunit, "min") == 0 || 439*c7d73a4dSGleb Kurtsou strcasecmp(timeunit, "mins") == 0) { 440*c7d73a4dSGleb Kurtsou offset *= 60; 441*c7d73a4dSGleb Kurtsou } else if (strcasecmp(timeunit, "h") == 0 || 442*c7d73a4dSGleb Kurtsou strcasecmp(timeunit, "hour") == 0 || 443*c7d73a4dSGleb Kurtsou strcasecmp(timeunit, "hours") == 0) { 444*c7d73a4dSGleb Kurtsou offset *= 60 * 60; 445*c7d73a4dSGleb Kurtsou } else if (strcasecmp(timeunit, "s") == 0 || 446*c7d73a4dSGleb Kurtsou strcasecmp(timeunit, "sec") == 0 || 447*c7d73a4dSGleb Kurtsou strcasecmp(timeunit, "secs") == 0) { 448*c7d73a4dSGleb Kurtsou offset *= 1; 449*c7d73a4dSGleb Kurtsou } else { 450*c7d73a4dSGleb Kurtsou badtime(); 451*c7d73a4dSGleb Kurtsou } 4528fae3551SRodney W. Grimes shuttime = now + offset; 4538fae3551SRodney W. Grimes return; 4548fae3551SRodney W. Grimes } 4558fae3551SRodney W. Grimes 4568fae3551SRodney W. Grimes /* handle hh:mm by getting rid of the colon */ 4578fae3551SRodney W. Grimes for (p = timearg; *p; ++p) 458c1160fe4SBill Fumerola if (!isascii(*p) || !isdigit(*p)) { 4598fae3551SRodney W. Grimes if (*p == ':' && strlen(p) == 3) { 4608fae3551SRodney W. Grimes p[0] = p[1]; 4618fae3551SRodney W. Grimes p[1] = p[2]; 4628fae3551SRodney W. Grimes p[2] = '\0'; 4638fae3551SRodney W. Grimes } 4648fae3551SRodney W. Grimes else 4658fae3551SRodney W. Grimes badtime(); 466c1160fe4SBill Fumerola } 4678fae3551SRodney W. Grimes 4688fae3551SRodney W. Grimes unsetenv("TZ"); /* OUR timezone */ 4698fae3551SRodney W. Grimes lt = localtime(&now); /* current time val */ 4708fae3551SRodney W. Grimes 4718fae3551SRodney W. Grimes switch(strlen(timearg)) { 4728fae3551SRodney W. Grimes case 10: 4732dc34227SAlexander Langer this_year = lt->tm_year; 4748fae3551SRodney W. Grimes lt->tm_year = ATOI2(timearg); 4752dc34227SAlexander Langer /* 4762dc34227SAlexander Langer * check if the specified year is in the next century. 4772dc34227SAlexander Langer * allow for one year of user error as many people will 4782dc34227SAlexander Langer * enter n - 1 at the start of year n. 4792dc34227SAlexander Langer */ 4802dc34227SAlexander Langer if (lt->tm_year < (this_year % 100) - 1) 4812dc34227SAlexander Langer lt->tm_year += 100; 48274179785SAlexander Langer /* adjust for the year 2000 and beyond */ 4832dc34227SAlexander Langer lt->tm_year += (this_year - (this_year % 100)); 4848fae3551SRodney W. Grimes /* FALLTHROUGH */ 4858fae3551SRodney W. Grimes case 8: 4868fae3551SRodney W. Grimes lt->tm_mon = ATOI2(timearg); 4878fae3551SRodney W. Grimes if (--lt->tm_mon < 0 || lt->tm_mon > 11) 4888fae3551SRodney W. Grimes badtime(); 4898fae3551SRodney W. Grimes /* FALLTHROUGH */ 4908fae3551SRodney W. Grimes case 6: 4918fae3551SRodney W. Grimes lt->tm_mday = ATOI2(timearg); 4928fae3551SRodney W. Grimes if (lt->tm_mday < 1 || lt->tm_mday > 31) 4938fae3551SRodney W. Grimes badtime(); 4948fae3551SRodney W. Grimes /* FALLTHROUGH */ 4958fae3551SRodney W. Grimes case 4: 4968fae3551SRodney W. Grimes lt->tm_hour = ATOI2(timearg); 4978fae3551SRodney W. Grimes if (lt->tm_hour < 0 || lt->tm_hour > 23) 4988fae3551SRodney W. Grimes badtime(); 4998fae3551SRodney W. Grimes lt->tm_min = ATOI2(timearg); 5008fae3551SRodney W. Grimes if (lt->tm_min < 0 || lt->tm_min > 59) 5018fae3551SRodney W. Grimes badtime(); 5028fae3551SRodney W. Grimes lt->tm_sec = 0; 5038fae3551SRodney W. Grimes if ((shuttime = mktime(lt)) == -1) 5048fae3551SRodney W. Grimes badtime(); 505a2bfcdfdSPhilippe Charnier if ((offset = shuttime - now) < 0) 506a2bfcdfdSPhilippe Charnier errx(1, "that time is already past."); 5078fae3551SRodney W. Grimes break; 5088fae3551SRodney W. Grimes default: 5098fae3551SRodney W. Grimes badtime(); 5108fae3551SRodney W. Grimes } 5118fae3551SRodney W. Grimes } 5128fae3551SRodney W. Grimes 5138fae3551SRodney W. Grimes #define NOMSG "\n\nNO LOGINS: System going down at " 5149534110fSXin LI static void 5159534110fSXin LI nolog(void) 5168fae3551SRodney W. Grimes { 5178fae3551SRodney W. Grimes int logfd; 5188fae3551SRodney W. Grimes char *ct; 5198fae3551SRodney W. Grimes 5208fae3551SRodney W. Grimes (void)unlink(_PATH_NOLOGIN); /* in case linked to another file */ 5218fae3551SRodney W. Grimes (void)signal(SIGINT, finish); 5228fae3551SRodney W. Grimes (void)signal(SIGHUP, finish); 5238fae3551SRodney W. Grimes (void)signal(SIGQUIT, finish); 5248fae3551SRodney W. Grimes (void)signal(SIGTERM, finish); 5258fae3551SRodney W. Grimes if ((logfd = open(_PATH_NOLOGIN, O_WRONLY|O_CREAT|O_TRUNC, 5268fae3551SRodney W. Grimes 0664)) >= 0) { 5278fae3551SRodney W. Grimes (void)write(logfd, NOMSG, sizeof(NOMSG) - 1); 5288fae3551SRodney W. Grimes ct = ctime(&shuttime); 5298fae3551SRodney W. Grimes (void)write(logfd, ct + 11, 5); 5308fae3551SRodney W. Grimes (void)write(logfd, "\n\n", 2); 5318fae3551SRodney W. Grimes (void)write(logfd, mbuf, strlen(mbuf)); 5328fae3551SRodney W. Grimes (void)close(logfd); 5338fae3551SRodney W. Grimes } 5348fae3551SRodney W. Grimes } 5358fae3551SRodney W. Grimes 5369534110fSXin LI static void 537f74b6bc2SXin LI finish(int signo __unused) 5388fae3551SRodney W. Grimes { 53939b831afSBruce Evans if (!killflg) 5408fae3551SRodney W. Grimes (void)unlink(_PATH_NOLOGIN); 5418fae3551SRodney W. Grimes exit(0); 5428fae3551SRodney W. Grimes } 5438fae3551SRodney W. Grimes 5449534110fSXin LI static void 54527d434afSEd Schouten badtime(void) 5468fae3551SRodney W. Grimes { 547d3714863SPhilippe Charnier errx(1, "bad time format"); 5488fae3551SRodney W. Grimes } 5498fae3551SRodney W. Grimes 5509534110fSXin LI static void 551f74b6bc2SXin LI usage(const char *cp) 5528fae3551SRodney W. Grimes { 55335cd460fSRuslan Ermilov if (cp != NULL) 55435cd460fSRuslan Ermilov warnx("%s", cp); 55535cd460fSRuslan Ermilov (void)fprintf(stderr, 5566868734cSPawel Jakub Dawidek "usage: shutdown [-] [-h | -p | -r | -k] [-o [-n]] time [warning-message ...]\n" 5576868734cSPawel Jakub Dawidek " poweroff\n"); 5588fae3551SRodney W. Grimes exit(1); 5598fae3551SRodney W. Grimes } 560