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