-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpistopserver.c
189 lines (180 loc) · 4.62 KB
/
pistopserver.c
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* $Log: pistopserver.c,v $
* Revision 1.7 2023-06-30 23:08:32+05:30 Cprogrammer
* display remoteip
* fixed shutdown
*
* Revision 1.6 2021-04-27 18:33:09+05:30 Cprogrammer
* removed unused variable pid
*
* Revision 1.5 2021-04-27 12:49:31+05:30 Cprogrammer
* display pid of self when quitting
*
* Revision 1.4 2020-08-31 21:18:21+05:30 Cprogrammer
* fixed compiler warnings
*
* Revision 1.3 2020-08-29 17:12:58+05:30 Cprogrammer
* removed creation of /tmp/dietpiserver.pid
*
* Revision 1.2 2020-08-29 16:02:07+05:30 Cprogrammer
* change client message
*
* Revision 1.1 2020-08-28 20:37:41+05:30 Cprogrammer
* Initial revision
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#ifdef HAVE_QMAIL
#include <subfd.h>
#include <scan.h>
#include <open.h>
#include <strerr.h>
#include <env.h>
#include <str.h>
#include <sig.h>
#include <fmt.h>
#include <sgetopt.h>
#include <tcpremoteip.h>
#endif
#define FATAL "pistopserver: fatal: "
#define SELECTTIMEOUT 5
char *usage = "usage: pistopserver [-d timeout]";
void
out(char *str)
{
if (!str || !*str)
return;
if (substdio_puts(subfdout, str) == -1)
strerr_die2sys(111, FATAL, "write: ");
return;
}
void
sigterm()
{
substdio_put(subfdout, "shutdown\n", 9);
substdio_flush(subfdout);
substdio_flush(subfderr);
_exit(1);
}
static char strnum[FMT_ULONG];
int
main(int argc, char **argv)
{
int i, opt, length, dataTimeout = -1, retval;
struct timeval timeout;
struct timeval *tptr;
time_t last_timeout;
char buffer[256];
char *ptr, *remoteip;
fd_set rfds; /*- File descriptor mask for select -*/
sig_termcatch(sigterm);
while ((opt = getopt(argc, argv, "d:")) != opteof) {
switch (opt) {
case 'd':
scan_int(optarg, &dataTimeout);
break;
}
}
if (dataTimeout == -1) {
if (!(ptr = env_get("DATA_TIMEOUT")))
ptr = "120";
scan_int(ptr, &dataTimeout);
}
if (dataTimeout > 0)
timeout.tv_sec = (dataTimeout > SELECTTIMEOUT) ? dataTimeout : SELECTTIMEOUT;
else
timeout.tv_sec = 0 - dataTimeout;
timeout.tv_usec = 0;
tptr = (timeout.tv_sec ? &timeout : (struct timeval *) 0);
if (!(remoteip = env_get("TCP6REMOTEIP")) && !(remoteip = env_get("TCPREMOTEIP")))
remoteip = (char *) tcpremoteip(0);
out("Connection from ");
out(remoteip ? remoteip : "unknown");
out("\n");
last_timeout = timeout.tv_sec;
for (;;) {
FD_ZERO(&rfds);
FD_SET(0, &rfds);
if ((retval = select(1, &rfds, (fd_set *) NULL, (fd_set *) NULL, tptr)) < 0) {
#ifdef ERESTART
if (errno == EINTR || errno == ERESTART)
#else
if (errno == EINTR)
#endif
continue;
}
if (!retval) { /*- timeout occurred */
if (dataTimeout > 0) {
last_timeout += 2 * last_timeout;
timeout.tv_sec = last_timeout;
} else {
timeout.tv_sec = 0 - dataTimeout;
last_timeout = timeout.tv_sec;
}
timeout.tv_usec = 0;
} else {
if (dataTimeout > 0)
last_timeout = timeout.tv_sec = (dataTimeout > SELECTTIMEOUT) ? dataTimeout : SELECTTIMEOUT;
else
timeout.tv_sec = 0 - dataTimeout;
timeout.tv_usec = 0;
}
if (FD_ISSET(0, &rfds)) { /*- data from socket/stdin (client) */
if ((length = read(0, buffer, sizeof(buffer))) == -1)
strerr_die2sys(111, FATAL, "read-network: ");
if (!length) {
strnum[i = fmt_ulong(strnum, getpid())] = 0;
if (substdio_put(subfderr, strnum, i) == -1 ||
substdio_put(subfderr, ": ", 2) == -1 ||
substdio_puts(subfderr, remoteip) == -1 ||
substdio_put(subfderr, ": client dropped connection\n", 28) == -1 ||
substdio_flush(subfderr) == -1)
strerr_die2sys(111, FATAL, "write: ");
break;
}
#ifdef DEBUG
strnum[fmt_ulong(strnum, length)] = 0;
out("length = ");
out(strnum);
out("\n");
if (substdio_flush(subfdout) == -1)
strerr_die2sys(111, FATAL, "write: ");
#endif
if (!str_diffn(buffer, "shutdown", 8)) {
execl("/sbin/shutdown", "shutdown", "-h", "now", (char *) 0);
strerr_die2sys(111, FATAL, "execl: /sbin/shutdown -h now: ");
} else
if (!str_diffn(buffer, "quit", 4)) {
close(0);
close(1);
close(2);
_exit(0);
} else
if (!str_diffn(buffer, "wait", 4))
continue;
/*- display data from client */
if (substdio_put(subfderr, buffer, length) == -1)
strerr_die2sys(111, FATAL, "write: ");
if (substdio_flush(subfderr) == -1)
strerr_die2sys(111, FATAL, "write: ");
}
} /*- for (;;) { */
_exit(0);
}