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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
|
/*
$Id$
Copyright (C) 1999-2004 IC & S dbmail@ic-s.nl
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* serverchild.c
*
* function implementations of server children code (connection handling)
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "debug.h"
#include "serverchild.h"
#include "db.h"
#include "auth.h"
#include "clientinfo.h"
#include "pool.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <signal.h>
#ifdef PROC_TITLES
#include "proctitleutils.h"
#endif
int ChildStopRequested = 0;
int connected = 0;
clientinfo_t client;
int PerformChildTask(ChildInfo_t * info);
void client_close(void);
void client_close(void)
{
if (client.tx) {
trace(TRACE_DEBUG,"%s,%s: closing write stream", __FILE__,__FUNCTION__);
fflush(client.tx);
fclose(client.tx); /* closes clientSocket as well */
client.tx = NULL;
}
if (client.rx) {
trace(TRACE_DEBUG,"%s,%s: closing read stream", __FILE__,__FUNCTION__);
shutdown(fileno(client.rx), SHUT_RDWR);
fclose(client.rx);
client.rx = NULL;
}
}
void disconnect_all(void);
void disconnect_all(void)
{
if (! connected)
return;
trace(TRACE_DEBUG,
"%s,%s: database connection still open, closing",
__FILE__,__FUNCTION__);
db_disconnect();
auth_disconnect();
connected = 0; /* FIXME a signal between this line and the previous one
* would screw things up. Would like to have all this in
* db_disconnect() making 'connected' obsolete
*/
}
void noop_child_sig_handler(int sig, siginfo_t *info UNUSED, void *data UNUSED)
{
if (sig == SIGSEGV)
_exit(0);
trace(TRACE_DEBUG, "%s,%s: ignoring signal [%d]", __FILE__, __FUNCTION__, sig);
}
void active_child_sig_handler(int sig, siginfo_t * info UNUSED, void *data UNUSED)
{
static int triedDisconnect = 0;
#ifdef _USE_STR_SIGNAL
trace(TRACE_ERROR, "%s,%s: got signal [%s]", __FILE__, __FUNCTION__,
strsignal(sig));
#else
trace(TRACE_ERROR, "%s,%s: got signal [%d]", __FILE__, __FUNCTION__, sig);
#endif
/* perform reinit at SIGHUP otherwise exit, but do nothing on
* SIGCHLD*/
switch (sig) {
case SIGCHLD:
trace(TRACE_DEBUG, "%s,%s: SIGCHLD received... ignoring",
__FILE__, __FUNCTION__);
break;
case SIGALRM:
trace(TRACE_DEBUG, "%s,%s: timeout received", __FILE__, __FUNCTION__);
client_close();
break;
case SIGHUP:
case SIGTERM:
case SIGQUIT:
case SIGSTOP:
if (ChildStopRequested) {
trace(TRACE_DEBUG,
"%s,%s: already caught a stop request. Closing right now",
__FILE__,__FUNCTION__);
/* already caught this signal, exit the hard way now */
client_close();
disconnect_all();
child_unregister();
trace(TRACE_DEBUG, "%s,%s: exit",__FILE__,__FUNCTION__);
exit(1);
}
trace(TRACE_DEBUG, "%s,%s: setting stop request",__FILE__,__FUNCTION__);
DelChildSigHandler();
ChildStopRequested = 1;
break;
default:
/* bad shtuff, exit */
trace(TRACE_DEBUG,
"%s,%s: cannot ignore this. Terminating",__FILE__,__FUNCTION__);
/*
* For some reason i have not yet determined the process starts eating up
* all CPU time when trying to disconnect.
* For now: just bail out :-)
*/
sleep(60);
child_unregister();
_exit(1);
if (!triedDisconnect) {
triedDisconnect = 1;
client_close();
disconnect_all();
}
trace(TRACE_DEBUG, "%s,%s: exit", __FILE__, __FUNCTION__);
child_unregister();
exit(1);
}
}
/*
* SetChildSigHandler()
*
* sets the signal handler for a child proces
*/
int SetChildSigHandler()
{
struct sigaction act;
/* init & install signal handlers */
memset(&act, 0, sizeof(act));
act.sa_sigaction = active_child_sig_handler;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_SIGINFO;
sigaction(SIGINT, &act, 0);
sigaction(SIGQUIT, &act, 0);
sigaction(SIGILL, &act, 0);
sigaction(SIGBUS, &act, 0);
sigaction(SIGPIPE, &act, 0);
sigaction(SIGFPE, &act, 0);
sigaction(SIGSEGV, &act, 0);
sigaction(SIGTERM, &act, 0);
sigaction(SIGHUP, &act, 0);
sigaction(SIGALRM, &act, 0);
sigaction(SIGCHLD, &act, 0);
return 0;
}
int DelChildSigHandler()
{
struct sigaction act;
/* init & install signal handlers */
memset(&act, 0, sizeof(act));
act.sa_sigaction = noop_child_sig_handler;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_SIGINFO;
sigaction(SIGINT, &act, 0);
sigaction(SIGQUIT, &act, 0);
sigaction(SIGILL, &act, 0);
sigaction(SIGBUS, &act, 0);
sigaction(SIGPIPE, &act, 0);
sigaction(SIGFPE, &act, 0);
sigaction(SIGSEGV, &act, 0);
sigaction(SIGTERM, &act, 0);
sigaction(SIGHUP, &act, 0);
sigaction(SIGALRM, &act, 0);
return 0;
}
/*
* CreateChild()
*
* creates a new child, returning only to the parent process
*/
pid_t CreateChild(ChildInfo_t * info)
{
pid_t pid = fork();
if (! pid) {
if (child_register() == -1) {
trace(TRACE_FATAL, "%s,%s: child_register failed",
__FILE__, __FUNCTION__);
exit(0);
}
ChildStopRequested = 0;
SetChildSigHandler();
trace(TRACE_INFO, "%s,%s: signal handler placed, going to perform task now",
__FILE__, __FUNCTION__);
PerformChildTask(info);
child_unregister();
exit(0);
} else {
usleep(5000);
return pid;
}
}
/*
* checks if a child is still alive (i.e. exists & not a zombie)
* FIXME seems to be not functional (kill succeeds on zombies)
*/
int CheckChildAlive(pid_t pid)
{
return (kill(pid, 0) == -1) ? 0 : 1;
}
int PerformChildTask(ChildInfo_t * info)
{
int i, len, clientSocket;
struct sockaddr_in saClient;
struct hostent *clientHost;
if (!info) {
trace(TRACE_ERROR,
"%s,%s: NULL info supplied", __FILE__, __func__);
return -1;
}
if (db_connect() != 0) {
trace(TRACE_ERROR,
"%s,%s: could not connect to database", __FILE__, __func__);
return -1;
}
if (auth_connect() != 0) {
trace(TRACE_ERROR,
"%s,%s: could not connect to authentication", __FILE__, __func__);
return -1;
}
srand((int) ((int) time(NULL) + (int) getpid()));
connected = 1;
for (i = 0; i < info->maxConnect && !ChildStopRequested; i++) {
trace(TRACE_INFO,
"%s,%s: waiting for connection", __FILE__, __func__);
child_reg_disconnected();
/* wait for connect */
len = sizeof(saClient);
clientSocket =
accept(info->listenSocket,
(struct sockaddr *) &saClient, &len);
if (clientSocket == -1) {
i--; /* don't count this as a connect */
trace(TRACE_INFO,
"%s,%s: accept failed", __FILE__, __func__);
continue; /* accept failed, refuse connection & continue */
}
child_reg_connected();
memset(&client, 0, sizeof(client)); /* zero-init */
client.timeoutMsg = info->timeoutMsg;
client.timeout = info->timeout;
strncpy(client.ip, inet_ntoa(saClient.sin_addr),
IPNUM_LEN);
client.clientname[0] = '\0';
if (info->resolveIP) {
clientHost =
gethostbyaddr((char *) &saClient.sin_addr,
sizeof(saClient.sin_addr),
saClient.sin_family);
if (clientHost && clientHost->h_name)
strncpy(client.clientname,
clientHost->h_name, FIELDSIZE);
trace(TRACE_MESSAGE,
"%s,%s: incoming connection from [%s (%s)]", __FILE__, __func__,
client.ip,
client.clientname[0] ? client.
clientname : "Lookup failed");
} else {
trace(TRACE_MESSAGE,
"%s,%s: incoming connection from [%s]", __FILE__, __func__,
client.ip);
}
/* make streams */
if (!(client.rx = fdopen(dup(clientSocket), "r"))) {
/* read-FILE opening failure */
trace(TRACE_ERROR,
"%s,%s: error opening read file stream", __FILE__, __func__);
close(clientSocket);
continue;
}
if (!(client.tx = fdopen(clientSocket, "w"))) {
/* write-FILE opening failure */
trace(TRACE_ERROR,
"%s,%s: error opening write file stream", __FILE__, __func__);
fclose(client.rx);
close(clientSocket);
memset(&client, 0, sizeof(client));
continue;
}
setvbuf(client.tx, (char *) NULL, _IOLBF, 0);
setvbuf(client.rx, (char *) NULL, _IOLBF, 0);
trace(TRACE_DEBUG,
"%s,%s: client info init complete, calling client handler", __FILE__, __func__);
/* streams are ready, perform handling */
info->ClientHandler(&client);
#ifdef PROC_TITLES
set_proc_title("%s", "Idle");
#endif
trace(TRACE_DEBUG,
"%s,%s: client handling complete, closing streams", __FILE__, __func__);
client_close();
trace(TRACE_INFO, "%s,%s: connection closed", __FILE__, __func__);
}
if (!ChildStopRequested)
trace(TRACE_ERROR,
"%s,%s: maximum number of connections reached, stopping now", __FILE__, __func__);
else
trace(TRACE_ERROR, "%s,%s: stop requested", __FILE__, __func__);
child_reg_disconnected();
disconnect_all();
return 0;
}
|