summaryrefslogtreecommitdiffhomepage
path: root/src/child.c
blob: 107475b75026c92ef4075a72d1a3f6d57a40a8eb (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
/* tinyproxy - A fast light-weight HTTP proxy
 * Copyright (C) 2000 Robert James Kaes <rjkaes@users.sourceforge.net>
 *
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/* Handles the creation/destruction of the various children required for
 * processing incoming connections.
 */

#include "main.h"

#include "child.h"
#include "daemon.h"
#include "filter.h"
#include "heap.h"
#include "log.h"
#include "reqs.h"
#include "sock.h"
#include "utils.h"
#include "conf.h"
#include "sblist.h"
#include "loop.h"
#include "conns.h"
#include <pthread.h>

static vector_t listen_fds;

struct client {
        union sockaddr_union addr;
};

struct child {
	pthread_t thread;
	struct client client;
	struct conn_s conn;
	volatile int done;
};

static void* child_thread(void* data)
{
	struct child *c = data;
	handle_connection (&c->conn, &c->client.addr);
	c->done = 1;
	return NULL;
}

static sblist *childs;

static void collect_threads(void)
{
	size_t i;
	for (i = 0; i < sblist_getsize(childs); ) {
		struct child *c = *((struct child**)sblist_get(childs, i));
		if (c->done) {
			pthread_join(c->thread, 0);
			sblist_delete(childs, i);
			safefree(c);
		} else i++;
	}
}

/*
 * This is the main loop accepting new connections.
 */
void child_main_loop (void)
{
        int connfd;
        union sockaddr_union cliaddr_storage;
        struct sockaddr *cliaddr = (void*) &cliaddr_storage;
        socklen_t clilen = sizeof(cliaddr_storage);
        fd_set rfds;
        ssize_t i;
        int ret, listenfd, maxfd, was_full = 0;
        pthread_attr_t *attrp, attr;
        struct child *child;

        childs = sblist_new(sizeof (struct child*), config->maxclients);

        loop_records_init();

        /*
         * We have to wait for connections on multiple fds,
         * so use select.
         */
        while (!config->quit) {

                collect_threads();

                if (sblist_getsize(childs) >= config->maxclients) {
                        if (!was_full)
                                log_message (LOG_WARNING,
                                             "Maximum number of connections reached. "
                                             "Refusing new connections.");
                        was_full = 1;
                        usleep(16);
                        continue;
                }

                was_full = 0;
                listenfd = -1;
                maxfd = 0;

                /* Handle log rotation if it was requested */
                if (received_sighup) {

                        reload_config (1);

#ifdef FILTER_ENABLE
                        filter_reload ();
#endif /* FILTER_ENABLE */

                        received_sighup = FALSE;
                }


                FD_ZERO(&rfds);

                for (i = 0; i < vector_length(listen_fds); i++) {
                        int *fd = (int *) vector_getentry(listen_fds, i, NULL);

                        FD_SET(*fd, &rfds);
                        maxfd = max(maxfd, *fd);
                }

                ret = select(maxfd + 1, &rfds, NULL, NULL, NULL);
                if (ret == -1) {
                        if (errno == EINTR) {
                                continue;
                        }
                        log_message (LOG_ERR, "error calling select: %s",
                                     strerror(errno));
                        continue;
                } else if (ret == 0) {
                        log_message (LOG_WARNING, "Strange: select returned 0 "
                                     "but we did not specify a timeout...");
                        continue;
                }

                for (i = 0; i < vector_length(listen_fds); i++) {
                        int *fd = (int *) vector_getentry(listen_fds, i, NULL);

                        if (FD_ISSET(*fd, &rfds)) {
                                /*
                                 * only accept the connection on the first
                                 * fd that we find readable. - fair?
                                 */
                                listenfd = *fd;
                                break;
                        }
                }

                if (listenfd == -1) {
                        log_message(LOG_WARNING, "Strange: None of our listen "
                                    "fds was readable after select");
                        continue;
                }

                /*
                 * We have a socket that is readable.
                 * Continue handling this connection.
                 */

                connfd = accept (listenfd, cliaddr, &clilen);


                /*
                 * Make sure no error occurred...
                 */
                if (connfd < 0) {
                        log_message (LOG_ERR,
                                     "Accept returned an error (%s) ... retrying.",
                                     strerror (errno));
                        continue;
                }

                child = safecalloc(1, sizeof(struct child));
                if (!child) {
oom:
                        close(connfd);
                        log_message (LOG_CRIT,
                                     "Could not allocate memory for child.");
                        usleep(16); /* prevent 100% CPU usage in OOM situation */
                        continue;
                }

                child->done = 0;

                if (!sblist_add(childs, &child)) {
                        free(child);
                        goto oom;
                }

                conn_struct_init(&child->conn);
                child->conn.client_fd = connfd;

                memcpy(&child->client.addr, &cliaddr_storage, sizeof(cliaddr_storage));

                attrp = 0;
                if (pthread_attr_init(&attr) == 0) {
                        attrp = &attr;
                        pthread_attr_setstacksize(attrp, 256*1024);
                }

                if (pthread_create(&child->thread, attrp, child_thread, child) != 0) {
                        sblist_delete(childs, sblist_getsize(childs) -1);
                        free(child);
                        goto oom;
		}
        }
}

/*
 * Go through all the non-empty children and cancel them.
 */
void child_kill_children (int sig)
{
	size_t i;

	if (sig != SIGTERM) return;

	for (i = 0; i < sblist_getsize(childs); i++) {
		struct child *c = *((struct child**)sblist_get(childs, i));
		if (!c->done) {
			/* interrupt blocking operations.
			   this should cause the threads to shutdown orderly. */
			close(c->conn.client_fd);
		}
	}
	usleep(16);
	collect_threads();
	if (sblist_getsize(childs) != 0)
		log_message (LOG_CRIT,
		             "child_kill_children: %zu threads still alive!",
		             sblist_getsize(childs)
		);
}

void child_free_children(void) {
	sblist_free(childs);
	childs = 0;
}

/**
 * Listen on the various configured interfaces
 */
int child_listening_sockets(vector_t listen_addrs, uint16_t port)
{
        int ret;
        ssize_t i;

        assert (port > 0);

        if (listen_fds == NULL) {
                listen_fds = vector_create();
                if (listen_fds == NULL) {
                        log_message (LOG_ERR, "Could not create the list "
                                     "of listening fds");
                        return -1;
                }
        }

        if ((listen_addrs == NULL) ||
            (vector_length(listen_addrs) == 0))
        {
                /*
                 * no Listen directive:
                 * listen on the wildcard address(es)
                 */
                ret = listen_sock(NULL, port, listen_fds);
                return ret;
        }

        for (i = 0; i < vector_length(listen_addrs); i++) {
                const char *addr;

                addr = (char *)vector_getentry(listen_addrs, i, NULL);
                if (addr == NULL) {
                        log_message(LOG_WARNING,
                                    "got NULL from listen_addrs - skipping");
                        continue;
                }

                ret = listen_sock(addr, port, listen_fds);
                if (ret != 0) {
                        return ret;
                }
        }

        return 0;
}

void child_close_sock (void)
{
        ssize_t i;

        for (i = 0; i < vector_length(listen_fds); i++) {
                int *fd = (int *) vector_getentry(listen_fds, i, NULL);
                close (*fd);
        }

        vector_delete(listen_fds);

        listen_fds = NULL;
}