Home | History | Annotate | Line # | Download | only in reboot
reboot.c revision 1.18
      1 /*	$NetBSD: reboot.c,v 1.18 1998/01/20 23:41:57 mycroft Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1980, 1986, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 
     38 #ifndef lint
     39 __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1993\n"
     40 "	The Regents of the University of California.  All rights reserved.\n");
     41 #endif /* not lint */
     42 
     43 #ifndef lint
     44 #if 0
     45 static char sccsid[] = "@(#)reboot.c	8.1 (Berkeley) 6/5/93";
     46 #else
     47 __RCSID("$NetBSD: reboot.c,v 1.18 1998/01/20 23:41:57 mycroft Exp $");
     48 #endif
     49 #endif /* not lint */
     50 
     51 #include <sys/reboot.h>
     52 
     53 #include <err.h>
     54 #include <errno.h>
     55 #include <pwd.h>
     56 #include <signal.h>
     57 #include <stdio.h>
     58 #include <stdlib.h>
     59 #include <string.h>
     60 #include <syslog.h>
     61 #include <unistd.h>
     62 #include <util.h>
     63 
     64 int main __P((int, char *[]));
     65 void usage __P((void));
     66 
     67 extern char *__progname;
     68 
     69 int dohalt;
     70 
     71 int
     72 main(argc, argv)
     73 	int argc;
     74 	char *argv[];
     75 {
     76 	int i;
     77 	struct passwd *pw;
     78 	int ch, howto, lflag, nflag, qflag, sverrno, len;
     79 	char *user, *bootstr, **av;
     80 
     81 	if (!strcmp(__progname, "halt") || !strcmp(__progname, "-halt")) {
     82 		dohalt = 1;
     83 		howto = RB_HALT;
     84 	} else
     85 		howto = 0;
     86 	lflag = nflag = qflag = 0;
     87 	while ((ch = getopt(argc, argv, "dlnq")) != -1)
     88 		switch(ch) {
     89 		case 'd':
     90 			howto |= RB_DUMP;
     91 			break;
     92 		case 'l':
     93 			lflag = 1;
     94 			break;
     95 		case 'n':
     96 			nflag = 1;
     97 			howto |= RB_NOSYNC;
     98 			break;
     99 		case 'q':
    100 			qflag = 1;
    101 			break;
    102 		case '?':
    103 		default:
    104 			usage();
    105 		}
    106 	argc -= optind;
    107 	argv += optind;
    108 
    109 	if (argc) {
    110 		for (av = argv, len = 0; *av; av++)
    111 			len += strlen(*av) + 1;
    112 		bootstr = malloc(len + 1);
    113 		*bootstr = '\0';		/* for first strcat */
    114 		for (av = argv; *av; av++) {
    115 			strcat(bootstr, *av);
    116 			strcat(bootstr, " ");
    117 		}
    118 		bootstr[len] = '\0';		/* to kill last space */
    119 		howto |= RB_STRING;
    120 	} else
    121 		bootstr = NULL;
    122 
    123 	if (geteuid())
    124 		errx(1, "%s", strerror(EPERM));
    125 
    126 	if (qflag) {
    127 		reboot(howto, bootstr);
    128 		err(1, "reboot");
    129 	}
    130 
    131 	/* Log the reboot. */
    132 	if (!lflag)  {
    133 		if ((user = getlogin()) == NULL)
    134 			user = (pw = getpwuid(getuid())) ?
    135 			    pw->pw_name : "???";
    136 		if (dohalt) {
    137 			openlog("halt", 0, LOG_AUTH | LOG_CONS);
    138 			syslog(LOG_CRIT, "halted by %s", user);
    139 		} else {
    140 			openlog("reboot", 0, LOG_AUTH | LOG_CONS);
    141 			if (bootstr)
    142 				syslog(LOG_CRIT, "rebooted by %s: %s", user,
    143 				    bootstr);
    144 			else
    145 				syslog(LOG_CRIT, "rebooted by %s", user);
    146 		}
    147 	}
    148 	logwtmp("~", "shutdown", "");
    149 
    150 	/*
    151 	 * Do a sync early on, so disks start transfers while we're off
    152 	 * killing processes.  Don't worry about writes done before the
    153 	 * processes die, the reboot system call syncs the disks.
    154 	 */
    155 	if (!nflag)
    156 		sync();
    157 
    158 	/* Just stop init -- if we fail, we'll restart it. */
    159 	if (kill(1, SIGTSTP) == -1)
    160 		err(1, "SIGTSTP init");
    161 
    162 	/* Ignore the SIGHUP we get when our parent shell dies. */
    163 	(void)signal(SIGHUP, SIG_IGN);
    164 
    165 	/* Send a SIGTERM first, a chance to save the buffers. */
    166 	if (kill(-1, SIGTERM) == -1) {
    167 		/*
    168 		 * If ESRCH, everything's OK: we're the only non-system
    169 		 * process!  That can happen e.g. via 'exec reboot' in
    170 		 * single-user mode.
    171 		 */
    172 		if (errno != ESRCH) {
    173 			warn("SIGTERM processes");
    174 			goto restart;
    175 		}
    176 	}
    177 
    178 	/*
    179 	 * After the processes receive the signal, start the rest of the
    180 	 * buffers on their way.  Wait 5 seconds between the SIGTERM and
    181 	 * the SIGKILL to give everybody a chance.
    182 	 */
    183 	sleep(2);
    184 	if (!nflag)
    185 		sync();
    186 	sleep(3);
    187 
    188 	for (i = 1;; ++i) {
    189 		if (kill(-1, SIGKILL) == -1) {
    190 			if (errno == ESRCH)
    191 				break;
    192 			goto restart;
    193 		}
    194 		if (i > 5) {
    195 			warnx("WARNING: some process(es) wouldn't die");
    196 			break;
    197 		}
    198 		(void)sleep(2 * i);
    199 	}
    200 
    201 	reboot(howto, bootstr);
    202 	/* FALLTHROUGH */
    203 
    204 restart:
    205 	sverrno = errno;
    206 	errx(1, "%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "",
    207 	    strerror(sverrno));
    208 	/* NOTREACHED */
    209 }
    210 
    211 void
    212 usage()
    213 {
    214 	(void)fprintf(stderr, "usage: %s [-dlnq] [-- <boot string>]\n",
    215 	    __progname);
    216 	exit(1);
    217 }
    218