reboot.c revision 37674
11558Srgrimes/*
21558Srgrimes * Copyright (c) 1980, 1986, 1993
31558Srgrimes *	The Regents of the University of California.  All rights reserved.
41558Srgrimes *
51558Srgrimes * Redistribution and use in source and binary forms, with or without
61558Srgrimes * modification, are permitted provided that the following conditions
71558Srgrimes * are met:
81558Srgrimes * 1. Redistributions of source code must retain the above copyright
91558Srgrimes *    notice, this list of conditions and the following disclaimer.
101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111558Srgrimes *    notice, this list of conditions and the following disclaimer in the
121558Srgrimes *    documentation and/or other materials provided with the distribution.
131558Srgrimes * 3. All advertising materials mentioning features or use of this software
141558Srgrimes *    must display the following acknowledgement:
151558Srgrimes *	This product includes software developed by the University of
161558Srgrimes *	California, Berkeley and its contributors.
171558Srgrimes * 4. Neither the name of the University nor the names of its contributors
181558Srgrimes *    may be used to endorse or promote products derived from this software
191558Srgrimes *    without specific prior written permission.
201558Srgrimes *
211558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311558Srgrimes * SUCH DAMAGE.
321558Srgrimes */
331558Srgrimes
341558Srgrimes#ifndef lint
3537674Scharnierstatic const char copyright[] =
361558Srgrimes"@(#) Copyright (c) 1980, 1986, 1993\n\
371558Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381558Srgrimes#endif /* not lint */
391558Srgrimes
401558Srgrimes#ifndef lint
4137674Scharnier#if 0
421558Srgrimesstatic char sccsid[] = "@(#)reboot.c	8.1 (Berkeley) 6/5/93";
4337674Scharnier#endif
4437674Scharnierstatic const char rcsid[] =
4537674Scharnier	"$Id$";
461558Srgrimes#endif /* not lint */
471558Srgrimes
481558Srgrimes#include <sys/reboot.h>
4937674Scharnier#include <sys/types.h>
501558Srgrimes#include <signal.h>
5137674Scharnier#include <err.h>
5237674Scharnier#include <errno.h>
5337674Scharnier#include <libutil.h>
541558Srgrimes#include <pwd.h>
551558Srgrimes#include <syslog.h>
561558Srgrimes#include <stdio.h>
571558Srgrimes#include <string.h>
5837674Scharnier#include <unistd.h>
591558Srgrimes
601558Srgrimesvoid usage __P((void));
611558Srgrimes
621558Srgrimesint dohalt;
631558Srgrimes
641558Srgrimesint
651558Srgrimesmain(argc, argv)
661558Srgrimes	int argc;
671558Srgrimes	char *argv[];
681558Srgrimes{
691558Srgrimes	register int i;
701558Srgrimes	struct passwd *pw;
7117801Sjulian	int ch, howto, lflag, nflag, qflag, pflag, sverrno;
721558Srgrimes	char *p, *user;
731558Srgrimes
742171Sdg	if (strstr((p = rindex(*argv, '/')) ? p + 1 : *argv, "halt")) {
751558Srgrimes		dohalt = 1;
761558Srgrimes		howto = RB_HALT;
771558Srgrimes	} else
781558Srgrimes		howto = 0;
791558Srgrimes	lflag = nflag = qflag = 0;
8024359Simp	while ((ch = getopt(argc, argv, "lnpq")) != -1)
811558Srgrimes		switch(ch) {
821558Srgrimes		case 'l':		/* Undocumented; used by shutdown. */
831558Srgrimes			lflag = 1;
841558Srgrimes			break;
851558Srgrimes		case 'n':
861558Srgrimes			nflag = 1;
871558Srgrimes			howto |= RB_NOSYNC;
881558Srgrimes			break;
8917801Sjulian		case 'p':
9017801Sjulian			pflag = 1;
9117801Sjulian			howto |= (RB_POWEROFF | RB_HALT);
9217801Sjulian			break;
931558Srgrimes		case 'q':
941558Srgrimes			qflag = 1;
951558Srgrimes			break;
961558Srgrimes		case '?':
971558Srgrimes		default:
981558Srgrimes			usage();
991558Srgrimes		}
1001558Srgrimes	argc -= optind;
1011558Srgrimes	argv += optind;
1021558Srgrimes
10326676Scharnier	if (geteuid()) {
10426676Scharnier		errno = EPERM;
10526676Scharnier		err(1, NULL);
10626676Scharnier	}
1071558Srgrimes
1081558Srgrimes	if (qflag) {
1091558Srgrimes		reboot(howto);
11026676Scharnier		err(1, NULL);
1111558Srgrimes	}
1121558Srgrimes
1131558Srgrimes	/* Log the reboot. */
1141558Srgrimes	if (!lflag)  {
1151558Srgrimes		if ((user = getlogin()) == NULL)
1161558Srgrimes			user = (pw = getpwuid(getuid())) ?
1171558Srgrimes			    pw->pw_name : "???";
1181558Srgrimes		if (dohalt) {
1191558Srgrimes			openlog("halt", 0, LOG_AUTH | LOG_CONS);
1201558Srgrimes			syslog(LOG_CRIT, "halted by %s", user);
1211558Srgrimes		} else {
1221558Srgrimes			openlog("reboot", 0, LOG_AUTH | LOG_CONS);
1231558Srgrimes			syslog(LOG_CRIT, "rebooted by %s", user);
1241558Srgrimes		}
1251558Srgrimes	}
1261558Srgrimes	logwtmp("~", "shutdown", "");
1271558Srgrimes
1281558Srgrimes	/*
1291558Srgrimes	 * Do a sync early on, so disks start transfers while we're off
1301558Srgrimes	 * killing processes.  Don't worry about writes done before the
1311558Srgrimes	 * processes die, the reboot system call syncs the disks.
1321558Srgrimes	 */
1331558Srgrimes	if (!nflag)
1341558Srgrimes		sync();
1351558Srgrimes
1361558Srgrimes	/* Just stop init -- if we fail, we'll restart it. */
1371558Srgrimes	if (kill(1, SIGTSTP) == -1)
13826676Scharnier		err(1, "SIGTSTP init");
1391558Srgrimes
1401558Srgrimes	/* Ignore the SIGHUP we get when our parent shell dies. */
1411558Srgrimes	(void)signal(SIGHUP, SIG_IGN);
1421558Srgrimes
1431558Srgrimes	/* Send a SIGTERM first, a chance to save the buffers. */
1441558Srgrimes	if (kill(-1, SIGTERM) == -1)
14526676Scharnier		err(1, "SIGTERM processes");
1461558Srgrimes
1471558Srgrimes	/*
1481558Srgrimes	 * After the processes receive the signal, start the rest of the
1491558Srgrimes	 * buffers on their way.  Wait 5 seconds between the SIGTERM and
1501558Srgrimes	 * the SIGKILL to give everybody a chance.
1511558Srgrimes	 */
1521558Srgrimes	sleep(2);
1531558Srgrimes	if (!nflag)
1541558Srgrimes		sync();
1551558Srgrimes	sleep(3);
1561558Srgrimes
1571558Srgrimes	for (i = 1;; ++i) {
1581558Srgrimes		if (kill(-1, SIGKILL) == -1) {
1591558Srgrimes			if (errno == ESRCH)
1601558Srgrimes				break;
1611558Srgrimes			goto restart;
1621558Srgrimes		}
1631558Srgrimes		if (i > 5) {
1641558Srgrimes			(void)fprintf(stderr,
1651558Srgrimes			    "WARNING: some process(es) wouldn't die\n");
1661558Srgrimes			break;
1671558Srgrimes		}
1681558Srgrimes		(void)sleep(2 * i);
1691558Srgrimes	}
1701558Srgrimes
1711558Srgrimes	reboot(howto);
1721558Srgrimes	/* FALLTHROUGH */
1731558Srgrimes
1741558Srgrimesrestart:
1751558Srgrimes	sverrno = errno;
17626676Scharnier	errx(1, "%s%s", kill(1, SIGHUP) == -1 ? "(can't restart init): " : "",
1771558Srgrimes	    strerror(sverrno));
1781558Srgrimes	/* NOTREACHED */
1791558Srgrimes}
1801558Srgrimes
1811558Srgrimesvoid
1821558Srgrimesusage()
1831558Srgrimes{
18437674Scharnier	(void)fprintf(stderr, "usage: %s [-npq]\n", dohalt ? "halt" : "reboot");
1851558Srgrimes	exit(1);
1861558Srgrimes}
187