Annotation of 43BSDReno/sbin/halt/halt.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Copyright (c) 1980, 1986 The Regents of the University of California.
        !             3:  * All rights reserved.
        !             4:  *
        !             5:  * Redistribution and use in source and binary forms are permitted
        !             6:  * provided that: (1) source distributions retain this entire copyright
        !             7:  * notice and comment, and (2) distributions including binaries display
        !             8:  * the following acknowledgement:  ``This product includes software
        !             9:  * developed by the University of California, Berkeley and its contributors''
        !            10:  * in the documentation or other materials provided with the distribution
        !            11:  * and in all advertising materials mentioning features or use of this
        !            12:  * software. Neither the name of the University nor the names of its
        !            13:  * contributors may be used to endorse or promote products derived
        !            14:  * from this software without specific prior written permission.
        !            15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
        !            16:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
        !            17:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
        !            18:  */
        !            19: 
        !            20: #ifndef lint
        !            21: char copyright[] =
        !            22: "@(#) Copyright (c) 1980, 1986 The Regents of the University of California.\n\
        !            23:  All rights reserved.\n";
        !            24: #endif /* not lint */
        !            25: 
        !            26: #ifndef lint
        !            27: static char sccsid[] = "@(#)halt.c     5.7 (Berkeley) 6/1/90";
        !            28: #endif /* not lint */
        !            29: 
        !            30: /*
        !            31:  * Halt
        !            32:  */
        !            33: #include <sys/types.h>
        !            34: #include <sys/reboot.h>
        !            35: #include <sys/time.h>
        !            36: #include <sys/syslog.h>
        !            37: #include <sys/signal.h>
        !            38: #include <errno.h>
        !            39: #include <pwd.h>
        !            40: #include <stdio.h>
        !            41: #include <paths.h>
        !            42: 
        !            43: main(argc, argv)
        !            44:        int argc;
        !            45:        char **argv;
        !            46: {
        !            47:        register int i;
        !            48:        register int qflag = 0;
        !            49:        struct passwd *pw, *getpwuid();
        !            50:        int ch, howto, needlog = 1;
        !            51:        char *user, *ttyn, *getlogin(), *ttyname();
        !            52: 
        !            53:        howto = RB_HALT;
        !            54:        ttyn = ttyname(2);
        !            55:        while ((ch = getopt(argc, argv, "lnqy")) != EOF)
        !            56:                switch((char)ch) {
        !            57:                case 'l':               /* undocumented; for shutdown(8) */
        !            58:                        needlog = 0;
        !            59:                        break;
        !            60:                case 'n':
        !            61:                        howto |= RB_NOSYNC;
        !            62:                        break;
        !            63:                case 'q':
        !            64:                        qflag++;
        !            65:                        break;
        !            66:                case 'y':
        !            67:                        ttyn = 0;
        !            68:                        break;
        !            69:                case '?':
        !            70:                default:
        !            71:                        fprintf(stderr, "usage: halt [-nqy]\n");
        !            72:                        exit(1);
        !            73:                }
        !            74: 
        !            75:        if (ttyn && ttyn[sizeof(_PATH_TTY) - 1] == 'd') {
        !            76:                fprintf(stderr, "halt: dangerous on a dialup; use ``halt -y'' if you are really sure\n");
        !            77:                exit(1);
        !            78:        }
        !            79: 
        !            80:        if (needlog) {
        !            81:                openlog("halt", 0, LOG_AUTH);
        !            82:                if ((user = getlogin()) == NULL)
        !            83:                        if ((pw = getpwuid(getuid())))
        !            84:                                user = pw->pw_name;
        !            85:                        else
        !            86:                                user = "???";
        !            87:                syslog(LOG_CRIT, "halted by %s", user);
        !            88:        }
        !            89: 
        !            90:        signal(SIGHUP, SIG_IGN);                /* for network connections */
        !            91:        if (kill(1, SIGTSTP) == -1) {
        !            92:                fprintf(stderr, "halt: can't idle init\n");
        !            93:                exit(1);
        !            94:        }
        !            95:        sleep(1);
        !            96:        (void) kill(-1, SIGTERM);       /* one chance to catch it */
        !            97:        sleep(5);
        !            98: 
        !            99:        if (!qflag) for (i = 1; ; i++) {
        !           100:                if (kill(-1, SIGKILL) == -1) {
        !           101:                        extern int errno;
        !           102: 
        !           103:                        if (errno == ESRCH)
        !           104:                                break;
        !           105: 
        !           106:                        perror("halt: kill");
        !           107:                        kill(1, SIGHUP);
        !           108:                        exit(1);
        !           109:                }
        !           110:                if (i > 5) {
        !           111:                        fprintf(stderr,
        !           112:                            "CAUTION: some process(es) wouldn't die\n");
        !           113:                        break;
        !           114:                }
        !           115:                setalarm(2 * i);
        !           116:                pause();
        !           117:        }
        !           118: 
        !           119:        if (!qflag && (howto & RB_NOSYNC) == 0) {
        !           120:                logwtmp("~", "shutdown", "");
        !           121:                sync();
        !           122:                setalarm(5);
        !           123:                pause();
        !           124:        }
        !           125:        syscall(55, howto);
        !           126:        perror("halt");
        !           127: }
        !           128: 
        !           129: dingdong()
        !           130: {
        !           131:        /* RRRIIINNNGGG RRRIIINNNGGG */
        !           132: }
        !           133: 
        !           134: setalarm(n)
        !           135: {
        !           136:        signal(SIGALRM, dingdong);
        !           137:        alarm(n);
        !           138: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.