Home | History | Annotate | Line # | Download | only in reboot
reboot.c revision 1.33
      1 /*	$NetBSD: reboot.c,v 1.33 2003/08/07 10:04:37 agc 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. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 
     34 #ifndef lint
     35 __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1993\n"
     36 "	The Regents of the University of California.  All rights reserved.\n");
     37 #endif /* not lint */
     38 
     39 #ifndef lint
     40 #if 0
     41 static char sccsid[] = "@(#)reboot.c	8.1 (Berkeley) 6/5/93";
     42 #else
     43 __RCSID("$NetBSD: reboot.c,v 1.33 2003/08/07 10:04:37 agc Exp $");
     44 #endif
     45 #endif /* not lint */
     46 
     47 #include <sys/reboot.h>
     48 
     49 #include <err.h>
     50 #include <errno.h>
     51 #include <pwd.h>
     52 #include <signal.h>
     53 #include <stdio.h>
     54 #include <stdlib.h>
     55 #include <string.h>
     56 #include <syslog.h>
     57 #include <unistd.h>
     58 #include <util.h>
     59 
     60 int main(int, char *[]);
     61 void usage(void);
     62 
     63 int dohalt;
     64 int dopoweroff;
     65 
     66 int
     67 main(int argc, char *argv[])
     68 {
     69 	const char *progname;
     70 	int i;
     71 	struct passwd *pw;
     72 	int ch, howto, lflag, nflag, qflag, sverrno, len;
     73 	const char *user;
     74 	char *bootstr, **av;
     75 
     76 	progname = getprogname();
     77 	if (!strcmp(progname, "halt") || !strcmp(progname, "-halt")) {
     78 		dohalt = 1;
     79 		howto = RB_HALT;
     80 	} else if (!strcmp(progname, "poweroff")
     81 		   || !strcmp(progname, "-poweroff")) {
     82 		dopoweroff = 1;
     83 		howto = RB_HALT | RB_POWERDOWN;
     84 	} else
     85 		howto = 0;
     86 	lflag = nflag = qflag = 0;
     87 	while ((ch = getopt(argc, argv, "dlnpq")) != -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 'p':
    100 			if (dohalt == 0)
    101 				usage();
    102 			howto |= RB_POWERDOWN;
    103 			break;
    104 		case 'q':
    105 			qflag = 1;
    106 			break;
    107 		case '?':
    108 		default:
    109 			usage();
    110 		}
    111 	argc -= optind;
    112 	argv += optind;
    113 
    114 	if (argc) {
    115 		for (av = argv, len = 0; *av; av++)
    116 			len += strlen(*av) + 1;
    117 		bootstr = malloc(len + 1);
    118 		*bootstr = '\0';		/* for first strcat */
    119 		for (av = argv; *av; av++) {
    120 			strcat(bootstr, *av);
    121 			strcat(bootstr, " ");
    122 		}
    123 		bootstr[len - 1] = '\0';	/* to kill last space */
    124 		howto |= RB_STRING;
    125 	} else
    126 		bootstr = NULL;
    127 
    128 	if (geteuid())
    129 		errx(1, "%s", strerror(EPERM));
    130 
    131 	if (qflag) {
    132 		reboot(howto, bootstr);
    133 		err(1, "reboot");
    134 	}
    135 
    136 	/* Log the reboot. */
    137 	if (!lflag)  {
    138 		if ((user = getlogin()) == NULL)
    139 			user = (pw = getpwuid(getuid())) ?
    140 			    pw->pw_name : "???";
    141 		if (dohalt) {
    142 			openlog("halt", LOG_CONS, LOG_AUTH);
    143 			syslog(LOG_CRIT, "halted by %s", user);
    144 		} else if (dopoweroff) {
    145 			openlog("poweroff", LOG_CONS, LOG_AUTH);
    146 			syslog(LOG_CRIT, "powered off by %s", user);
    147 		} else {
    148 			openlog("reboot", LOG_CONS, LOG_AUTH);
    149 			if (bootstr)
    150 				syslog(LOG_CRIT, "rebooted by %s: %s", user,
    151 				    bootstr);
    152 			else
    153 				syslog(LOG_CRIT, "rebooted by %s", user);
    154 		}
    155 	}
    156 #ifdef SUPPORT_UTMP
    157 	logwtmp("~", "shutdown", "");
    158 #endif
    159 #ifdef SUPPORT_UTMPX
    160 	logwtmpx("~", "shutdown", "", INIT_PROCESS, 0);
    161 #endif
    162 
    163 	/*
    164 	 * Do a sync early on, so disks start transfers while we're off
    165 	 * killing processes.  Don't worry about writes done before the
    166 	 * processes die, the reboot system call syncs the disks.
    167 	 */
    168 	if (!nflag)
    169 		sync();
    170 
    171 	/*
    172 	 * Ignore signals that we can get as a result of killing
    173 	 * parents, group leaders, etc.
    174 	 */
    175 	(void)signal(SIGHUP,  SIG_IGN);
    176 	(void)signal(SIGINT,  SIG_IGN);
    177 	(void)signal(SIGQUIT, SIG_IGN);
    178 	(void)signal(SIGTERM, SIG_IGN);
    179 	(void)signal(SIGTSTP, SIG_IGN);
    180 
    181 	/*
    182 	 * If we're running in a pipeline, we don't want to die
    183 	 * after killing whatever we're writing to.
    184 	 */
    185 	(void)signal(SIGPIPE, SIG_IGN);
    186 
    187 	/* Just stop init -- if we fail, we'll restart it. */
    188 	if (kill(1, SIGTSTP) == -1)
    189 		err(1, "SIGTSTP init");
    190 
    191 	/* Send a SIGTERM first, a chance to save the buffers. */
    192 	if (kill(-1, SIGTERM) == -1) {
    193 		/*
    194 		 * If ESRCH, everything's OK: we're the only non-system
    195 		 * process!  That can happen e.g. via 'exec reboot' in
    196 		 * single-user mode.
    197 		 */
    198 		if (errno != ESRCH) {
    199 			warn("SIGTERM processes");
    200 			goto restart;
    201 		}
    202 	}
    203 
    204 	/*
    205 	 * After the processes receive the signal, start the rest of the
    206 	 * buffers on their way.  Wait 5 seconds between the SIGTERM and
    207 	 * the SIGKILL to pretend to give everybody a chance.
    208 	 */
    209 	sleep(2);
    210 	if (!nflag)
    211 		sync();
    212 	sleep(3);
    213 
    214 	for (i = 1;; ++i) {
    215 		if (kill(-1, SIGKILL) == -1) {
    216 			if (errno == ESRCH)
    217 				break;
    218 			goto restart;
    219 		}
    220 		if (i > 5) {
    221 			warnx("WARNING: some process(es) wouldn't die");
    222 			break;
    223 		}
    224 		(void)sleep(2 * i);
    225 	}
    226 
    227 	reboot(howto, bootstr);
    228 	/* FALLTHROUGH */
    229 
    230 restart:
    231 	sverrno = errno;
    232 	errx(1, "%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "",
    233 	    strerror(sverrno));
    234 	/* NOTREACHED */
    235 }
    236 
    237 void
    238 usage(void)
    239 {
    240 	const char *pflag = dohalt ? "p" : "";
    241 
    242 	(void)fprintf(stderr, "usage: %s [-dln%sq] [-- <boot string>]\n",
    243 	    getprogname(), pflag);
    244 	exit(1);
    245 }
    246