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