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