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