ANSI fixes
[unix-history] / usr / src / sbin / reboot / reboot.c
CommitLineData
c018628f 1/*
61dbfbf1
KB
2 * Copyright (c) 1980, 1986 The Regents of the University of California.
3 * All rights reserved.
4 *
70ab3c27 5 * %sccs.include.redist.c%
c018628f
DF
6 */
7
8#ifndef lint
9char copyright[] =
61dbfbf1 10"@(#) Copyright (c) 1980, 1986 The Regents of the University of California.\n\
c018628f 11 All rights reserved.\n";
61dbfbf1 12#endif /* not lint */
c018628f 13
37c640e2 14#ifndef lint
e0060ef1 15static char sccsid[] = "@(#)reboot.c 5.11 (Berkeley) %G%";
61dbfbf1 16#endif /* not lint */
37c640e2 17
cf5fda66
BJ
18/*
19 * Reboot
20 */
3111ba40 21#include <sys/types.h>
ce4fd43b 22#include <sys/time.h>
bd699e5d 23#include <sys/syslog.h>
77b19d4d 24#include <sys/syscall.h>
07beee98
KB
25#include <sys/reboot.h>
26#include <sys/signal.h>
27#include <pwd.h>
28#include <stdio.h>
29#include <errno.h>
cf5fda66
BJ
30
31main(argc, argv)
32 int argc;
33 char **argv;
34{
35 int howto;
36 register char *argp;
fe6cb666
BJ
37 register i;
38 register ok = 0;
39 register qflag = 0;
b0af3d20 40 int needlog = 1;
bd699e5d 41 char *user, *getlogin();
f0d98f59 42 struct passwd *pw;
cf5fda66 43
f5a538b6 44 openlog("reboot", 0, LOG_AUTH);
cf5fda66
BJ
45 argc--, argv++;
46 howto = 0;
47 while (argc > 0) {
fe6cb666
BJ
48 if (!strcmp(*argv, "-q"))
49 qflag++;
cf5fda66
BJ
50 else if (!strcmp(*argv, "-n"))
51 howto |= RB_NOSYNC;
b0af3d20
MK
52 else if (!strcmp(*argv, "-l"))
53 needlog = 0;
cf5fda66
BJ
54 else {
55 fprintf(stderr,
fe6cb666 56 "usage: reboot [ -n ][ -q ]\n");
cf5fda66
BJ
57 exit(1);
58 }
59 argc--, argv++;
60 }
fe6cb666 61
b0af3d20
MK
62 if (needlog) {
63 user = getlogin();
64 if (user == (char *)0 && (pw = getpwuid(getuid())))
65 user = pw->pw_name;
66 if (user == (char *)0)
67 user = "root";
68 syslog(LOG_CRIT, "rebooted by %s", user);
69 }
bd699e5d 70
7c280ab6 71 signal(SIGHUP, SIG_IGN); /* for remote connections */
fe6cb666
BJ
72 if (kill(1, SIGTSTP) == -1) {
73 fprintf(stderr, "reboot: can't idle init\n");
74 exit(1);
75 }
bd699e5d 76 sleep(1);
b0af3d20
MK
77 (void) kill(-1, SIGTERM); /* one chance to catch it */
78 sleep(5);
fe6cb666
BJ
79
80 if (!qflag) for (i = 1; ; i++) {
81 if (kill(-1, SIGKILL) == -1) {
82 extern int errno;
83
84 if (errno == ESRCH)
85 break;
86
87 perror("reboot: kill");
88 kill(1, SIGHUP);
89 exit(1);
90 }
91 if (i > 5) {
bd699e5d
MK
92 fprintf(stderr,
93 "CAUTION: some process(es) wouldn't die\n");
fe6cb666
BJ
94 break;
95 }
96 setalarm(2 * i);
97 pause();
98 }
99
bd699e5d 100 if (!qflag && (howto & RB_NOSYNC) == 0) {
ffaf5d37 101 logwtmp("~", "shutdown", "");
bd699e5d 102 sync();
fe6cb666
BJ
103 setalarm(5);
104 pause();
105 }
77b19d4d 106 syscall(SYS_reboot, howto);
cf5fda66 107 perror("reboot");
fe6cb666
BJ
108 kill(1, SIGHUP);
109 exit(1);
110}
111
112dingdong()
113{
114 /* RRRIIINNNGGG RRRIIINNNGGG */
115}
116
117setalarm(n)
f0d98f59 118 int n;
fe6cb666
BJ
119{
120 signal(SIGALRM, dingdong);
121 alarm(n);
122}