|
|
1.1 root 1: #ifndef lint
2: static char *sccsid = "@(#)halt.c 4.9 (Berkeley) 7/2/83";
3: #endif
4:
5: /*
6: * Halt
7: */
8: #include <stdio.h>
9: #include <sys/reboot.h>
10: #include <sys/types.h>
11: #include <sys/time.h>
12: #include <sys/dir.h>
13: #include <errno.h>
14: #include <signal.h>
15:
16: #define SHUTDOWNLOG "/usr/adm/shutdownlog"
17:
18: main(argc, argv)
19: int argc;
20: char **argv;
21: {
22: int howto;
23: char *ttyn = (char *)ttyname(2);
24: register i;
25: register qflag = 0;
26: int w, pid, status;
27: char *unixname = NULL;
28:
29: howto = RB_HALT;
30: argc--, argv++;
31: while (argc > 0) {
32: if (!strcmp(*argv, "-n"))
33: howto |= RB_NOSYNC;
34: else if (!strcmp(*argv, "-y"))
35: ttyn = 0;
36: else if (!strcmp(*argv, "-q"))
37: qflag++;
38: else if (argc != 1) {
39: fprintf(stderr, "usage: halt [ -n ]\n");
40: exit(1);
41: } else
42: unixname = *argv;
43: argc--, argv++;
44: }
45: if (ttyn && *(ttyn+strlen("/dev/tty")) == 'd') {
46: fprintf(stderr, "halt: dangerous on a dialup; use ``halt -y'' if you are really sure\n");
47: exit(1);
48: }
49:
50: signal(SIGHUP, SIG_IGN); /* for network connections */
51: if (kill(1, SIGTSTP) == -1) {
52: fprintf(stderr, "reboot: can't idle init\n");
53: exit(1);
54: }
55:
56: if (!qflag) for (i = 1; ; i++) {
57: if (kill(-1, SIGKILL) == -1) {
58: extern int errno;
59:
60: if (errno == ESRCH)
61: break;
62:
63: perror("reboot: kill");
64: kill(1, SIGHUP);
65: exit(1);
66: }
67: if (i > 5) {
68: fprintf(stderr, "CAUTION: some process(es) wouldn't die\n");
69: if ((pid = fork()) < 0)
70: fprintf(stderr, "halt: could not fork\n");
71: if (pid == 0) {
72: fprintf(stderr, "Running a ps of %s...\n",
73: (unixname == NULL) ? "/vmunix" : unixname);
74: execl("/bin/ps", "ps", "-alx",
75: (unixname == NULL) ? "/vmunix" : unixname, 0);
76: } else
77: while ((w = wait(&status)) != pid && w != -1)
78: ;
79: break;
80: }
81: setalarm(2 * i);
82: pause();
83: }
84:
85: if ((howto & RB_NOSYNC) == 0)
86: log_entry();
87: if (!qflag) {
88: if ((howto & RB_NOSYNC)==0) {
89: markdown();
90: sync();
91: sync();
92: }
93: setalarm(5);
94: pause();
95: }
96: syscall(55, howto);
97: perror("reboot");
98: }
99:
100: dingdong()
101: {
102: /* RRRIIINNNGGG RRRIIINNNGGG */
103: }
104:
105: setalarm(n)
106: {
107: signal(SIGALRM, dingdong);
108: alarm(n);
109: }
110:
111: #include <utmp.h>
112: #define SCPYN(a, b) strncpy(a, b, sizeof(a))
113: char wtmpf[] = "/usr/adm/wtmp";
114: struct utmp wtmp;
115:
116: markdown()
117: {
118: register f = open(wtmpf, 1);
119: if (f >= 0) {
120: lseek(f, 0L, 2);
121: SCPYN(wtmp.ut_line, "~");
122: SCPYN(wtmp.ut_name, "shutdown");
123: SCPYN(wtmp.ut_host, "");
124: time(&wtmp.ut_time);
125: write(f, (char *)&wtmp, sizeof(wtmp));
126: close(f);
127: }
128: }
129:
130: char *days[] = {
131: "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
132: };
133:
134: char *months[] = {
135: "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
136: "Oct", "Nov", "Dec"
137: };
138:
139: log_entry()
140: {
141: FILE *fp;
142: struct tm *tm, *localtime();
143: time_t now;
144:
145: time(&now);
146: tm = localtime(&now);
147: fp = fopen(SHUTDOWNLOG, "a");
148: if (fp == NULL)
149: return;
150: fseek(fp, 0L, 2);
151: fprintf(fp, "%02d:%02d %s %s %2d, %4d. Halted.\n", tm->tm_hour,
152: tm->tm_min, days[tm->tm_wday], months[tm->tm_mon],
153: tm->tm_mday, tm->tm_year + 1900);
154: fclose(fp);
155: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.