Home | History | Annotate | Line # | Download | only in reboot
reboot.c revision 1.4
      1 /*
      2  * Copyright (c) 1980, 1986 The Regents of the University of California.
      3  * 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 char copyright[] =
     36 "@(#) Copyright (c) 1980, 1986 The Regents of the University of California.\n\
     37  All rights reserved.\n";
     38 #endif /* not lint */
     39 
     40 #ifndef lint
     41 /*static char sccsid[] = "from: @(#)reboot.c	5.11 (Berkeley) 2/27/91";*/
     42 static char rcsid[] = "$Id: reboot.c,v 1.4 1993/08/01 18:25:35 mycroft Exp $";
     43 #endif /* not lint */
     44 
     45 #include <sys/types.h>
     46 #include <sys/time.h>
     47 #include <sys/syslog.h>
     48 #include <sys/syscall.h>
     49 #include <sys/reboot.h>
     50 #include <sys/signal.h>
     51 #include <pwd.h>
     52 #include <stdio.h>
     53 #include <errno.h>
     54 
     55 main(argc, argv)
     56 	int argc;
     57 	char **argv;
     58 {
     59 	int howto;
     60 	register char *argp;
     61 	register i;
     62 	register ok = 0;
     63 	register qflag = 0;
     64 	int needlog = 1;
     65 	char *user, *getlogin();
     66 	struct passwd *pw;
     67 
     68 	openlog("reboot", 0, LOG_AUTH);
     69 	argc--, argv++;
     70 	howto = 0;
     71 	while (argc > 0) {
     72 		if (!strcmp(*argv, "-q"))
     73 			qflag++;
     74 		else if (!strcmp(*argv, "-n"))
     75 			howto |= RB_NOSYNC;
     76 		else if (!strcmp(*argv, "-l"))
     77 			needlog = 0;
     78 		else {
     79 			fprintf(stderr,
     80 			    "usage: reboot [ -n ][ -q ]\n");
     81 			exit(1);
     82 		}
     83 		argc--, argv++;
     84 	}
     85 
     86 	if (needlog) {
     87 		user = getlogin();
     88 		if (user == (char *)0 && (pw = getpwuid(getuid())))
     89 			user = pw->pw_name;
     90 		if (user == (char *)0)
     91 			user = "root";
     92 		syslog(LOG_CRIT, "rebooted by %s", user);
     93 	}
     94 
     95 	signal(SIGHUP, SIG_IGN);	/* for remote connections */
     96 	if (kill(1, SIGTSTP) == -1) {
     97 		fprintf(stderr, "reboot: can't idle init\n");
     98 		exit(1);
     99 	}
    100 	sleep(1);
    101 	(void) kill(-1, SIGTERM);	/* one chance to catch it */
    102 	sleep(5);
    103 
    104 	if (!qflag) for (i = 1; ; i++) {
    105 		if (kill(-1, SIGKILL) == -1) {
    106 			extern int errno;
    107 
    108 			if (errno == ESRCH)
    109 				break;
    110 
    111 			perror("reboot: kill");
    112 			kill(1, SIGHUP);
    113 			exit(1);
    114 		}
    115 		if (i > 5) {
    116 			fprintf(stderr,
    117 			    "CAUTION: some process(es) wouldn't die\n");
    118 			break;
    119 		}
    120 		setalarm(2 * i);
    121 		pause();
    122 	}
    123 
    124 	if (!qflag && (howto & RB_NOSYNC) == 0) {
    125 		logwtmp("~", "shutdown", "");
    126 		sync();
    127 		setalarm(5);
    128 		pause();
    129 	}
    130 	syscall(SYS_reboot, howto);
    131 	perror("reboot");
    132 	kill(1, SIGHUP);
    133 	exit(1);
    134 }
    135 
    136 void
    137 dingdong()
    138 {
    139 	/* RRRIIINNNGGG RRRIIINNNGGG */
    140 }
    141 
    142 setalarm(n)
    143 	int n;
    144 {
    145 	signal(SIGALRM, dingdong);
    146 	alarm(n);
    147 }
    148