xref: /csrg-svn/sbin/reboot/halt.c (revision 47743)
119062Sdist /*
235595Sbostic  * Copyright (c) 1980, 1986 The Regents of the University of California.
335595Sbostic  * All rights reserved.
435595Sbostic  *
542701Sbostic  * %sccs.include.redist.c%
619062Sdist  */
719062Sdist 
812682Ssam #ifndef lint
919062Sdist char copyright[] =
1035595Sbostic "@(#) Copyright (c) 1980, 1986 The Regents of the University of California.\n\
1119062Sdist  All rights reserved.\n";
1235595Sbostic #endif /* not lint */
1312682Ssam 
1419062Sdist #ifndef lint
15*47743Sbostic static char sccsid[] = "@(#)halt.c	5.10 (Berkeley) 04/03/91";
1635595Sbostic #endif /* not lint */
1719062Sdist 
181360Sbill /*
191360Sbill  * Halt
201360Sbill  */
2137947Sbostic #include <sys/types.h>
221360Sbill #include <sys/reboot.h>
2313606Ssam #include <sys/time.h>
2428718Skarels #include <sys/syslog.h>
2537947Sbostic #include <sys/signal.h>
262823Swnj #include <errno.h>
2728718Skarels #include <pwd.h>
2837947Sbostic #include <stdio.h>
2937947Sbostic #include <paths.h>
301360Sbill 
main(argc,argv)311360Sbill main(argc, argv)
321360Sbill 	int argc;
331360Sbill 	char **argv;
341360Sbill {
3535595Sbostic 	register int i;
3635595Sbostic 	register int qflag = 0;
3746236Storek 	struct passwd *pw;
3835595Sbostic 	int ch, howto, needlog = 1;
3935595Sbostic 	char *user, *ttyn, *getlogin(), *ttyname();
401360Sbill 
411360Sbill 	howto = RB_HALT;
4235595Sbostic 	ttyn = ttyname(2);
4335595Sbostic 	while ((ch = getopt(argc, argv, "lnqy")) != EOF)
4435595Sbostic 		switch((char)ch) {
4535595Sbostic 		case 'l':		/* undocumented; for shutdown(8) */
4635595Sbostic 			needlog = 0;
4735595Sbostic 			break;
4835595Sbostic 		case 'n':
491360Sbill 			howto |= RB_NOSYNC;
5035595Sbostic 			break;
5135595Sbostic 		case 'q':
5235595Sbostic 			qflag++;
5335595Sbostic 			break;
5435595Sbostic 		case 'y':
551800Sbill 			ttyn = 0;
5635595Sbostic 			break;
5735595Sbostic 		case '?':
5835595Sbostic 		default:
5935595Sbostic 			fprintf(stderr, "usage: halt [-nqy]\n");
601360Sbill 			exit(1);
611360Sbill 		}
6235595Sbostic 
6337947Sbostic 	if (ttyn && ttyn[sizeof(_PATH_TTY) - 1] == 'd') {
641800Sbill 		fprintf(stderr, "halt: dangerous on a dialup; use ``halt -y'' if you are really sure\n");
651800Sbill 		exit(1);
661800Sbill 	}
672823Swnj 
6828801Skarels 	if (needlog) {
6935595Sbostic 		openlog("halt", 0, LOG_AUTH);
7035595Sbostic 		if ((user = getlogin()) == NULL)
7135595Sbostic 			if ((pw = getpwuid(getuid())))
7235595Sbostic 				user = pw->pw_name;
7335595Sbostic 			else
7435595Sbostic 				user = "???";
7528801Skarels 		syslog(LOG_CRIT, "halted by %s", user);
7628801Skarels 	}
7728718Skarels 
7810074Ssam 	signal(SIGHUP, SIG_IGN);		/* for network connections */
792823Swnj 	if (kill(1, SIGTSTP) == -1) {
8035595Sbostic 		fprintf(stderr, "halt: can't idle init\n");
812823Swnj 		exit(1);
822823Swnj 	}
8328718Skarels 	sleep(1);
8428801Skarels 	(void) kill(-1, SIGTERM);	/* one chance to catch it */
8528801Skarels 	sleep(5);
862823Swnj 
872823Swnj 	if (!qflag) for (i = 1; ; i++) {
882823Swnj 		if (kill(-1, SIGKILL) == -1) {
892823Swnj 			extern int errno;
902823Swnj 
912823Swnj 			if (errno == ESRCH)
922823Swnj 				break;
932823Swnj 
9435595Sbostic 			perror("halt: kill");
952823Swnj 			kill(1, SIGHUP);
962823Swnj 			exit(1);
972823Swnj 		}
982823Swnj 		if (i > 5) {
9928718Skarels 			fprintf(stderr,
10028718Skarels 			    "CAUTION: some process(es) wouldn't die\n");
1012823Swnj 			break;
1022823Swnj 		}
1032823Swnj 		setalarm(2 * i);
1042823Swnj 		pause();
1052823Swnj 	}
1062823Swnj 
10728718Skarels 	if (!qflag && (howto & RB_NOSYNC) == 0) {
10835595Sbostic 		logwtmp("~", "shutdown", "");
10928718Skarels 		sync();
1102823Swnj 		setalarm(5);
1112823Swnj 		pause();
1122823Swnj 	}
113*47743Sbostic 	reboot(howto);
11435595Sbostic 	perror("halt");
1151360Sbill }
1162823Swnj 
11746707Sbostic void
dingdong()1182823Swnj dingdong()
1192823Swnj {
1202823Swnj 	/* RRRIIINNNGGG RRRIIINNNGGG */
1212823Swnj }
1222823Swnj 
setalarm(n)1232823Swnj setalarm(n)
12446236Storek 	int n;
1252823Swnj {
1262823Swnj 	signal(SIGALRM, dingdong);
1272823Swnj 	alarm(n);
1282823Swnj }
129