1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
/*
* A verbal system shutdown utility.
* $Id: down.c 1.2 Mon, 24 Mar 1997 12:25:37 -0500 dyfet $
* Copyright (c) 1997 by Tycho Softworks.
* For conditions of distribution and reuse see product license.
*
* Abstract:
* A verbal system shutdown utility. You may need to adjust the name
* of the shutdown program used. This gives some warning and then
* initiates the shutdown processing, unless cancelled in time.
*/
#include <std/signal.h>
#include <proc/process.h>
#include <other/string.h>
#include <std/files.h>
#include <other/config.h>
#include <net/stream.h>
static char host[128] = "localhost";
static char halt[60] = "/sbin/halt";
static char abort_msg[] = "System shutdown overriden. Resuming normal operation.\n";
static char down_msg[] = "%s system shutdown sequence initiated. You have %d seconds to override.\n";
static int timer = 10;
static STREAM fp = NULL;
/*
* If the user aborts the shutdown, then we send the cancellation message
* and gracefully exit the scene.
*/
static void abort_sig()
{
if(fp)
{
puttcp(abort_msg, fp);
closetcp(fp);
}
fputs("\n", stdout);
exit(-1);
}
void main(int argc, char **argv)
{
char *p;
char *reboot = halt;
int port = getservice("speak");
CONFIG *cfg = sys_config("speak");
char hostname[128];
if(argc < 1 || argc > 2)
fatal(EX_USAGE, "use: down [timer]\n");
if(!port)
fatal(EX_UNAVAILABLE, "down: speak: service unlisted in /etc/services\n");
if(!cfg)
fatal(EX_CONFIG, "down: speak.conf: config missing\n");
seek_config(cfg, "down");
while(read_config(cfg))
{
if(NULL != (p = get_config(cfg, "timer")))
{
timer = atoi(p);
continue;
}
if(NULL != (p = get_config(cfg, "halt")))
{
strcpy(halt, p);
continue;
}
if(NULL != (p = get_config(cfg, "host")))
{
strcpy(host, p);
continue;
}
if(NULL != (p = get_config(cfg, "server")))
{
strcpy(host, p);
continue;
}
}
close_config(cfg);
if(argc > 1)
timer = atoi(argv[1]);
fp = opentcp(host, port);
if(!fp)
fatal(EX_UNAVAILABLE, "down: %s: speak service unreachable\n", host);
printf("System shutdown initiated...use <break> to override; ");
fflush(stdout);
signal(SIGINT, abort_sig);
gettcp(hostname, 127, fp);
gethostname(hostname, 80);
fprintf(fp, down_msg, hostname, timer);
fflush(fp);
sleep(strlen(down_msg) / 9 + 1);
/* count down timer until 0 */
while(timer)
{
printf("%d ", timer);
fflush(stdout);
fprintf(fp, "%d\n", timer);
fflush(fp);
sleep(1);
--timer;
}
/* too late now!! */
signal(SIGINT, SIG_IGN);
printf("\n");
closetcp(fp);
/* detach return to user and execute reboot program */
pdetach(D_KEEPALL);
argv[0] = reboot;
argv[1] = NULL;
execvp(reboot, argv);
}
|