Home | History | Annotate | Line # | Download | only in reboot
reboot.c revision 1.6
      1 /*	$NetBSD: reboot.c,v 1.6 1995/03/18 14:59:27 cgd Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1980, 1986 The Regents of the University of California.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #ifndef lint
     37 char copyright[] =
     38 "@(#) Copyright (c) 1980, 1986 The Regents of the University of California.\n\
     39  All rights reserved.\n";
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "@(#)reboot.c	5.11 (Berkeley) 2/27/91";
     45 #else
     46 static char rcsid[] = "$NetBSD: reboot.c,v 1.6 1995/03/18 14:59:27 cgd Exp $";
     47 #endif
     48 #endif /* not lint */
     49 
     50 #include <sys/types.h>
     51 #include <sys/time.h>
     52 #include <sys/syslog.h>
     53 #include <sys/syscall.h>
     54 #include <sys/reboot.h>
     55 #include <sys/signal.h>
     56 #include <pwd.h>
     57 #include <stdio.h>
     58 #include <errno.h>
     59 #include <signal.h>
     60 #include <unistd.h>
     61 
     62 void setalarm __P((int));
     63 
     64 int
     65 main(argc, argv)
     66 	int argc;
     67 	char **argv;
     68 {
     69 	int howto;
     70 	register i;
     71 	register qflag = 0;
     72 	int needlog = 1;
     73 	char *user, *getlogin();
     74 	struct passwd *pw;
     75 
     76 	openlog("reboot", 0, LOG_AUTH);
     77 	argc--, argv++;
     78 	howto = 0;
     79 	while (argc > 0) {
     80 		if (!strcmp(*argv, "-q"))
     81 			qflag++;
     82 		else if (!strcmp(*argv, "-n"))
     83 			howto |= RB_NOSYNC;
     84 		else if (!strcmp(*argv, "-l"))
     85 			needlog = 0;
     86 		else {
     87 			fprintf(stderr,
     88 			    "usage: reboot [ -n ][ -q ]\n");
     89 			exit(1);
     90 		}
     91 		argc--, argv++;
     92 	}
     93 
     94 	if (needlog) {
     95 		user = getlogin();
     96 		if (user == (char *)0 && (pw = getpwuid(getuid())))
     97 			user = pw->pw_name;
     98 		if (user == (char *)0)
     99 			user = "root";
    100 		syslog(LOG_CRIT, "rebooted by %s", user);
    101 	}
    102 
    103 	signal(SIGHUP, SIG_IGN);	/* for remote connections */
    104 	if (kill(1, SIGTSTP) == -1) {
    105 		fprintf(stderr, "reboot: can't idle init\n");
    106 		exit(1);
    107 	}
    108 	sleep(1);
    109 	(void) kill(-1, SIGTERM);	/* one chance to catch it */
    110 	sleep(5);
    111 
    112 	if (!qflag) for (i = 1; ; i++) {
    113 		if (kill(-1, SIGKILL) == -1) {
    114 			extern int errno;
    115 
    116 			if (errno == ESRCH)
    117 				break;
    118 
    119 			perror("reboot: kill");
    120 			kill(1, SIGHUP);
    121 			exit(1);
    122 		}
    123 		if (i > 5) {
    124 			fprintf(stderr,
    125 			    "CAUTION: some process(es) wouldn't die\n");
    126 			break;
    127 		}
    128 		setalarm(2 * i);
    129 		pause();
    130 	}
    131 
    132 	if (!qflag && (howto & RB_NOSYNC) == 0) {
    133 		logwtmp("~", "shutdown", "");
    134 		sync();
    135 		setalarm(5);
    136 		pause();
    137 	}
    138 	syscall(SYS_reboot, howto);
    139 	perror("reboot");
    140 	kill(1, SIGHUP);
    141 	exit(1);
    142 }
    143 
    144 void
    145 dingdong()
    146 {
    147 	/* RRRIIINNNGGG RRRIIINNNGGG */
    148 }
    149 
    150 void
    151 setalarm(n)
    152 	int n;
    153 {
    154 	signal(SIGALRM, dingdong);
    155 	alarm(n);
    156 }
    157