summaryrefslogtreecommitdiff
path: root/serverchild.c
blob: cffe27c41f43227375d5aad0518d779bf79411bd (plain)
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
/*
 Copyright (C) 1999-2004 IC & S  dbmail@ic-s.nl
 Copyright (c) 2005-2006 NFG Net Facilities Group BV support@nfg.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
 *
 * $Id$
 * 
 * function implementations of server children code (connection handling)
 */

#include "dbmail.h"

int ChildStopRequested = 0;
int connected = 0;
volatile clientinfo_t client;

static void client_close(void);
static void disconnect_all(void);

int PerformChildTask(ChildInfo_t * info);

void client_close(void)
{
	if (client.tx) {
		fflush(client.tx);
		fclose(client.tx);	/* closes clientSocket as well */
		client.tx = NULL;
	}
	if (client.rx) {
		shutdown(fileno(client.rx), SHUT_RDWR);
		fclose(client.rx);
		client.rx = NULL;
	}
}

void disconnect_all(void)
{
	if (! connected)
		return;
	
	db_disconnect();
	auth_disconnect();
	connected = 0;
}

void noop_child_sig_handler(int sig, siginfo_t *info UNUSED, void *data UNUSED)
{
	int saved_errno = errno;
	
	if (sig == SIGSEGV)
		_exit(0);
	
	trace(TRACE_DEBUG, "%s,%s: ignoring signal [%d]", __FILE__, __func__, sig);

	errno = saved_errno;
}

void active_child_sig_handler(int sig, siginfo_t * info UNUSED, void *data UNUSED)
{
	int saved_errno = errno;
	
	/* perform reinit at SIGHUP otherwise exit, but do nothing on
	 *  SIGCHLD*/
	switch (sig) {
	case SIGCHLD:
		break;
	case SIGALRM:
		client_close();
		break;

	case SIGHUP:
	case SIGTERM:
	case SIGQUIT:
	case SIGSTOP:
		if (ChildStopRequested) {
			client_close();
			disconnect_all();
			child_unregister();
			exit(1);
		}
		DelChildSigHandler();
	 	ChildStopRequested = 1;
		break;

	default:
		child_unregister();
		_exit(1);
	}

	errno = saved_errno;
}



/*
 * 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(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__, __func__);
			_exit(0);
		}
	
 		ChildStopRequested = 0;
 		SetChildSigHandler();
		
 		trace(TRACE_INFO, "%s,%s: signal handler placed, going to perform task now",
			__FILE__, __func__);
 		
		if (PerformChildTask(info) == -1)
			return -1;
		
 		child_unregister();
 		exit(0);
	} else {
 		usleep(5000);
 		return pid;
	}
}

int PerformChildTask(ChildInfo_t * info)
{
	int i, len, serr, 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++) {
		if (db_check_connection()) {
			trace(TRACE_ERROR, "%s,%s: database has gone away", 
					__FILE__, __func__);
			ChildStopRequested=1;
			continue;
		}

		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, (socklen_t *)&len);

		if (clientSocket == -1) {
			serr = errno;
			i--;	/* don't count this as a connect */
			trace(TRACE_INFO, "%s,%s: accept failed [%s]", 
					__FILE__, __func__, strerror(serr));
			errno = serr;
			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_src, 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)] by pid [%d]",
					__FILE__, __func__,
			      client.ip_src,
			      client.clientname[0] ? client.clientname : "Lookup failed", getpid());
		} else {
			trace(TRACE_MESSAGE, "%s,%s: incoming connection from [%s] by pid [%d]", 
					__FILE__, __func__,
			      client.ip_src, getpid());
		}
		
		/* 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);

		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;
}

int manage_start_cli_server(ChildInfo_t * info)
{
	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;

	if (db_check_connection()) {
		trace(TRACE_ERROR, "%s,%s: database has gone away", 
				__FILE__, __func__);
		return -1;
	}

		
	memset(&client, 0, sizeof(client));	/* zero-init */

	client.timeoutMsg = info->timeoutMsg;
	client.timeout = info->timeout;

	/* make streams */
	client.rx = stdin;
	client.tx = stdout;

	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);

	trace(TRACE_DEBUG,
	      "%s,%s: client handling complete, closing streams", __FILE__, __func__);
	client_close();
	trace(TRACE_INFO, "%s,%s: connection closed", __FILE__, __func__);
	
	disconnect_all();
	
	return 0;
}