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