Home | History | Annotate | Line # | Download | only in reboot
reboot.c revision 1.10
      1 /*	$NetBSD: reboot.c,v 1.10 1996/08/10 00:20:58 mrg 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 #ifndef lint
     37 static char copyright[] =
     38 "@(#) Copyright (c) 1980, 1986, 1993\n\
     39 	The Regents of the University of California.  All rights reserved.\n";
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "@(#)reboot.c	8.1 (Berkeley) 6/5/93";
     45 #else
     46 static char rcsid[] = "$NetBSD: reboot.c,v 1.10 1996/08/10 00:20:58 mrg Exp $";
     47 #endif
     48 #endif /* not lint */
     49 
     50 #include <sys/reboot.h>
     51 #include <signal.h>
     52 #include <pwd.h>
     53 #include <errno.h>
     54 #include <syslog.h>
     55 #include <unistd.h>
     56 #include <stdio.h>
     57 #include <stdlib.h>
     58 #include <string.h>
     59 
     60 void err __P((const char *fmt, ...));
     61 void usage __P((void));
     62 
     63 int dohalt;
     64 
     65 int
     66 main(argc, argv)
     67 	int argc;
     68 	char *argv[];
     69 {
     70 	register int i;
     71 	struct passwd *pw;
     72 	int ch, howto, lflag, nflag, qflag, sverrno, len;
     73 	char *p, *user, *bootstr, **av;
     74 
     75 	if (!strcmp((p = rindex(*argv, '/')) ? p + 1 : *argv, "halt")) {
     76 		dohalt = 1;
     77 		howto = RB_HALT;
     78 	} else
     79 		howto = 0;
     80 	lflag = nflag = qflag = 0;
     81 	while ((ch = getopt(argc, argv, "lnqd")) != EOF)
     82 		switch(ch) {
     83 		case 'l':		/* Undocumented; used by shutdown. */
     84 			lflag = 1;
     85 			break;
     86 		case 'n':
     87 			nflag = 1;
     88 			howto |= RB_NOSYNC;
     89 			break;
     90 		case 'q':
     91 			qflag = 1;
     92 			break;
     93 		case 'd':
     94 			howto |= RB_DUMP;
     95 			break;
     96 		case '?':
     97 		default:
     98 			usage();
     99 		}
    100 	argc -= optind;
    101 	argv += optind;
    102 
    103 	if (argc) {
    104 		for (av = argv, len = 0; *av; av++)
    105 			len += strlen(*av);
    106 		bootstr = malloc(len + 1);
    107 		*bootstr = '\0';
    108 		for (av = argv; *av; av++)
    109 			strcpy(bootstr, *av);
    110 		howto |= RB_STRING;
    111 	} else
    112 		bootstr = NULL;
    113 
    114 	if (geteuid())
    115 		err("%s", strerror(EPERM));
    116 
    117 	if (qflag) {
    118 		reboot(howto, bootstr);
    119 		err("%s", strerror(errno));
    120 	}
    121 
    122 	/* Log the reboot. */
    123 	if (!lflag)  {
    124 		if ((user = getlogin()) == NULL)
    125 			user = (pw = getpwuid(getuid())) ?
    126 			    pw->pw_name : "???";
    127 		if (dohalt) {
    128 			openlog("halt", 0, LOG_AUTH | LOG_CONS);
    129 			syslog(LOG_CRIT, "halted by %s", user);
    130 		} else {
    131 			openlog("reboot", 0, LOG_AUTH | LOG_CONS);
    132 			syslog(LOG_CRIT, "rebooted by %s: %s", user, bootstr);
    133 		}
    134 	}
    135 	logwtmp("~", "shutdown", "");
    136 
    137 	/*
    138 	 * Do a sync early on, so disks start transfers while we're off
    139 	 * killing processes.  Don't worry about writes done before the
    140 	 * processes die, the reboot system call syncs the disks.
    141 	 */
    142 	if (!nflag)
    143 		sync();
    144 
    145 	/* Just stop init -- if we fail, we'll restart it. */
    146 	if (kill(1, SIGTSTP) == -1)
    147 		err("SIGTSTP init: %s", strerror(errno));
    148 
    149 	/* Ignore the SIGHUP we get when our parent shell dies. */
    150 	(void)signal(SIGHUP, SIG_IGN);
    151 
    152 	/* Send a SIGTERM first, a chance to save the buffers. */
    153 	if (kill(-1, SIGTERM) == -1)
    154 		err("SIGTERM processes: %s", strerror(errno));
    155 
    156 	/*
    157 	 * After the processes receive the signal, start the rest of the
    158 	 * buffers on their way.  Wait 5 seconds between the SIGTERM and
    159 	 * the SIGKILL to give everybody a chance.
    160 	 */
    161 	sleep(2);
    162 	if (!nflag)
    163 		sync();
    164 	sleep(3);
    165 
    166 	for (i = 1;; ++i) {
    167 		if (kill(-1, SIGKILL) == -1) {
    168 			if (errno == ESRCH)
    169 				break;
    170 			goto restart;
    171 		}
    172 		if (i > 5) {
    173 			(void)fprintf(stderr,
    174 			    "WARNING: some process(es) wouldn't die\n");
    175 			break;
    176 		}
    177 		(void)sleep(2 * i);
    178 	}
    179 
    180 	reboot(howto, bootstr);
    181 	/* FALLTHROUGH */
    182 
    183 restart:
    184 	sverrno = errno;
    185 	err("%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "",
    186 	    strerror(sverrno));
    187 	/* NOTREACHED */
    188 }
    189 
    190 void
    191 usage()
    192 {
    193 	(void)fprintf(stderr, "usage: %s [-nqd] [-- <boot string>]\n", dohalt ? "halt" : "reboot");
    194 	exit(1);
    195 }
    196 
    197 #if __STDC__
    198 #include <stdarg.h>
    199 #else
    200 #include <varargs.h>
    201 #endif
    202 
    203 void
    204 #if __STDC__
    205 err(const char *fmt, ...)
    206 #else
    207 err(fmt, va_alist)
    208 	char *fmt;
    209         va_dcl
    210 #endif
    211 {
    212 	va_list ap;
    213 #if __STDC__
    214 	va_start(ap, fmt);
    215 #else
    216 	va_start(ap);
    217 #endif
    218 	(void)fprintf(stderr, "%s: ", dohalt ? "halt" : "reboot");
    219 	(void)vfprintf(stderr, fmt, ap);
    220 	va_end(ap);
    221 	(void)fprintf(stderr, "\n");
    222 	exit(1);
    223 	/* NOTREACHED */
    224 }
    225