Merge branch 'master' into timeheap
[ashd.git] / src / htparser.c
CommitLineData
f0bbedf7
FT
1/*
2 ashd - A Sane HTTP Daemon
3 Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#include <stdlib.h>
20#include <unistd.h>
21#include <stdio.h>
d341283f 22#include <fcntl.h>
f4cdf919 23#include <string.h>
f4cdf919 24#include <sys/socket.h>
f0cbd8d7 25#include <pwd.h>
15fa3fe8 26#include <sys/signal.h>
f4cdf919 27#include <errno.h>
f0bbedf7
FT
28
29#ifdef HAVE_CONFIG_H
30#include <config.h>
31#endif
32#include <utils.h>
f4cdf919 33#include <mt.h>
83723896 34#include <mtio.h>
f4cdf919 35#include <log.h>
66987955 36#include <req.h>
9d87a119 37#include <proc.h>
fc253e2f 38#include <bufio.h>
f4cdf919 39
8774c31b 40#include "htparser.h"
f4cdf919 41
8774c31b 42static int plex;
43c58ba2 43static char *pidfile = NULL;
d341283f 44static int daemonize, usesyslog;
cac13158 45struct mtbuf listeners;
f4cdf919 46
df431d1d
FT
47static void trimx(struct hthead *req)
48{
49 int i;
50
51 i = 0;
52 while(i < req->noheaders) {
53 if(!strncasecmp(req->headers[i][0], "x-ash-", 6)) {
54 free(req->headers[i][0]);
55 free(req->headers[i][1]);
56 free(req->headers[i]);
57 memmove(req->headers + i, req->headers + i + 1, sizeof(*req->headers) * (--req->noheaders - i));
58 } else {
59 i++;
60 }
61 }
62}
63
fc253e2f 64static struct hthead *parsereq(struct bufio *in)
66987955 65{
5fc1bf9f
FT
66 struct hthead *req;
67 struct charbuf method, url, ver;
68 int c;
66987955 69
5fc1bf9f
FT
70 req = NULL;
71 bufinit(method);
72 bufinit(url);
73 bufinit(ver);
74 while(1) {
fc253e2f 75 c = biogetc(in);
5fc1bf9f
FT
76 if(c == ' ') {
77 break;
78 } else if((c == EOF) || (c < 32) || (c >= 128)) {
79 goto fail;
80 } else {
81 bufadd(method, c);
90b0ba0f
FT
82 if(method.d >= 128)
83 goto fail;
66987955 84 }
c9955b14 85 }
c9955b14 86 while(1) {
fc253e2f 87 c = biogetc(in);
5fc1bf9f 88 if(c == ' ') {
c9955b14 89 break;
5fc1bf9f
FT
90 } else if((c == EOF) || (c < 32)) {
91 goto fail;
92 } else {
93 bufadd(url, c);
90b0ba0f
FT
94 if(url.d >= 65536)
95 goto fail;
66987955
FT
96 }
97 }
66987955 98 while(1) {
fc253e2f 99 c = biogetc(in);
5fc1bf9f 100 if(c == 10) {
66987955 101 break;
5fc1bf9f
FT
102 } else if(c == 13) {
103 } else if((c == EOF) || (c < 32) || (c >= 128)) {
c9955b14 104 goto fail;
5fc1bf9f
FT
105 } else {
106 bufadd(ver, c);
90b0ba0f
FT
107 if(ver.d >= 128)
108 goto fail;
5fc1bf9f 109 }
66987955 110 }
5fc1bf9f
FT
111 bufadd(method, 0);
112 bufadd(url, 0);
113 bufadd(ver, 0);
114 req = mkreq(method.b, url.b, ver.b);
fc253e2f 115 if(parseheadersb(req, in))
5fc1bf9f 116 goto fail;
df431d1d 117 trimx(req);
5fc1bf9f 118 goto out;
c9955b14
FT
119
120fail:
5fc1bf9f
FT
121 if(req != NULL) {
122 freehthead(req);
123 req = NULL;
124 }
125out:
126 buffree(method);
127 buffree(url);
128 buffree(ver);
129 return(req);
66987955
FT
130}
131
fc253e2f 132static off_t passdata(struct bufio *in, struct bufio *out, off_t max)
9d87a119 133{
fc253e2f 134 ssize_t read;
5fc1bf9f 135 off_t total;
5fc1bf9f
FT
136
137 total = 0;
fc253e2f
FT
138 while(!bioeof(in) && ((max < 0) || (total < max))) {
139 if((read = biordata(in)) > 0) {
140 if(max >= 0)
141 read = min(max - total, read);
142 if((read = biowritesome(out, in->rbuf.b + in->rh, read)) < 0)
143 return(-1);
144 in->rh += read;
145 total += read;
146 }
147 if(biorspace(in) && ((max < 0) || (biordata(in) < max - total)) && (biofillsome(in) < 0))
5fc1bf9f 148 return(-1);
9d87a119 149 }
5fc1bf9f
FT
150 return(total);
151}
152
fc253e2f 153static int recvchunks(struct bufio *in, struct bufio *out)
8d19a9ec 154{
f728df60 155 ssize_t read, chlen;
8d19a9ec
FT
156 int c, r;
157
158 while(1) {
159 chlen = 0;
160 r = 0;
161 while(1) {
fc253e2f 162 c = biogetc(in);
8d19a9ec
FT
163 if(c == 10) {
164 if(!r)
165 return(-1);
166 break;
167 } else if(c == 13) {
168 } else if((c >= '0') && (c <= '9')) {
169 chlen = (chlen << 4) + (c - '0');
170 r = 1;
171 } else if((c >= 'A') && (c <= 'F')) {
172 chlen = (chlen << 4) + (c + 10 - 'A');
173 r = 1;
174 } else if((c >= 'a') && (c <= 'f')) {
175 chlen = (chlen << 4) + (c + 10 - 'a');
176 r = 1;
177 } else {
178 /* XXX: Technically, there may be chunk extensions to
179 * be read, but since that will likely never actually
180 * happen in practice, I can just as well add support
181 * for that if it actually does become relevant. */
182 return(-1);
183 }
184 }
185 if(chlen == 0)
186 break;
187 while(chlen > 0) {
fc253e2f
FT
188 if((read = biordata(in)) > 0) {
189 if((read = biowritesome(out, in->rbuf.b + in->rh, min(read, chlen))) < 0)
190 return(-1);
191 in->rh += read;
192 chlen -= read;
193 }
194 if(biorspace(in) && (biordata(in) < chlen) && (biofillsome(in) <= 0))
8d19a9ec 195 return(-1);
8d19a9ec 196 }
fc253e2f 197 if((biogetc(in) != 13) || (biogetc(in) != 10))
8d19a9ec
FT
198 return(-1);
199 }
200 /* XXX: Technically, there may be trailers to be read, but that's
201 * just about as likely as chunk extensions. */
fc253e2f 202 if((biogetc(in) != 13) || (biogetc(in) != 10))
8d19a9ec
FT
203 return(-1);
204 return(0);
205}
206
fc253e2f 207static int passchunks(struct bufio *in, struct bufio *out)
5fc1bf9f 208{
5fc1bf9f
FT
209 size_t read;
210
fc253e2f
FT
211 while(!bioeof(in)) {
212 if((read = biordata(in)) > 0) {
213 bioprintf(out, "%zx\r\n", read);
214 if(biowrite(out, in->rbuf.b + in->rh, read) != read)
215 return(-1);
216 in->rh += read;
217 bioprintf(out, "\r\n");
218 if(bioflush(out) < 0)
219 return(-1);
220 }
221 if(biorspace(in) && (biofillsome(in) < 0))
5fc1bf9f 222 return(-1);
fc253e2f
FT
223 }
224 bioprintf(out, "0\r\n\r\n");
5fc1bf9f
FT
225 return(0);
226}
227
228static int hasheader(struct hthead *head, char *name, char *val)
229{
230 char *hd;
231
232 if((hd = getheader(head, name)) == NULL)
233 return(0);
234 return(!strcasecmp(hd, val));
9d87a119
FT
235}
236
64a9096a
FT
237static int canonreq(struct hthead *req)
238{
239 char *p, *p2, *r;
240 int n;
241
242 if(req->url[0] == '/') {
243 replrest(req, req->url + 1);
244 if((p = strchr(req->rest, '?')) != NULL)
245 *p = 0;
246 return(1);
247 }
248 if((p = strstr(req->url, "://")) != NULL) {
249 n = p - req->url;
250 if(((n == 4) && !strncasecmp(req->url, "http", 4)) ||
251 ((n == 5) && !strncasecmp(req->url, "https", 5))) {
252 if(getheader(req, "host"))
253 return(0);
254 p += 3;
255 if((p2 = strchr(p, '/')) == NULL) {
256 headappheader(req, "Host", p);
257 free(req->url);
258 req->url = sstrdup("/");
259 } else {
260 r = sstrdup(p2);
261 *(p2++) = 0;
262 headappheader(req, "Host", p);
263 free(req->url);
264 req->url = r;
265 }
266 replrest(req, req->url + 1);
267 if((p = strchr(req->rest, '?')) != NULL)
268 *p = 0;
269 return(1);
270 }
271 }
272 return(0);
273}
274
75bb20c8
FT
275static int http10keep(struct hthead *req, struct hthead *resp)
276{
277 int fc;
278
279 fc = hasheader(resp, "connection", "close");
280 headrmheader(resp, "connection");
281 if(!fc && hasheader(req, "connection", "keep-alive")) {
282 headappheader(resp, "Connection", "Keep-Alive");
283 return(1);
284 } else {
285 return(0);
286 }
287}
288
43066106
FT
289static char *connid(void)
290{
291 static struct charbuf cur;
292 int i;
293 char *ret;
294
295 for(i = 0; i < cur.d; i++) {
296 if((++cur.b[i]) > 'Z')
297 cur.b[i] = 'A';
298 else
299 goto done;
300 }
301 bufadd(cur, 'A');
302done:
303 ret = memcpy(smalloc(cur.d + 1), cur.b, cur.d);
304 ret[cur.d] = 0;
305 return(ret);
306}
307
a68db17d
FT
308static void passduplex(struct bufio *a, int afd, struct bufio *b, int bfd)
309{
310 struct selected pfd[4], sel;
311 struct bufio *sio;
312 int n, ev;
313
314 while(!bioeof(a) && !bioeof(b)) {
315 biocopybuf(b, a);
316 biocopybuf(a, b);
317 n = 0;
318 if(!a->eof) {
319 ev = 0;
320 if(biorspace(a))
321 ev |= EV_READ;
322 if(biowdata(a))
323 ev |= EV_WRITE;
324 if(ev)
325 pfd[n++] = (struct selected){.fd = afd, .ev = ev};
326 }
327 if(!b->eof) {
328 ev = 0;
329 if(!b->eof && biorspace(b))
330 ev |= EV_READ;
331 if(biowdata(b))
332 ev |= EV_WRITE;
333 if(ev)
334 pfd[n++] = (struct selected){.fd = bfd, .ev = ev};
335 }
4350acb5
FT
336 if((sel = mblock(600, n, pfd)).ev == 0)
337 break;
a68db17d
FT
338 if(sel.fd == afd)
339 sio = a;
340 else if(sel.fd == bfd)
341 sio = b;
342 else
343 break;
344 if((sel.ev & EV_READ) && (biofillsome(sio) < 0))
345 break;
346 if((sel.ev & EV_WRITE) && (bioflushsome(sio) < 0))
347 break;
348 }
349}
350
351void serve(struct bufio *in, int infd, struct conn *conn)
66987955 352{
af34331c 353 int pfds[2];
a68db17d
FT
354 struct bufio *out, *dout;
355 struct stdiofd *outi;
9d87a119 356 struct hthead *req, *resp;
43066106 357 char *hd, *id;
5fc1bf9f 358 off_t dlen;
a68db17d 359 int keep, duplex;
66987955 360
43066106 361 id = connid();
5fc1bf9f 362 out = NULL;
3c296bd4 363 req = resp = NULL;
cac13158 364 while(plex >= 0) {
fc253e2f 365 bioflush(in);
5fc1bf9f
FT
366 if((req = parsereq(in)) == NULL)
367 break;
64a9096a
FT
368 if(!canonreq(req))
369 break;
9d87a119 370
43066106 371 headappheader(req, "X-Ash-Connection-ID", id);
8774c31b
FT
372 if((conn->initreq != NULL) && conn->initreq(conn, req))
373 break;
374
cac13158 375 if((plex < 0) || block(plex, EV_WRITE, 60) <= 0)
5fc1bf9f 376 break;
af34331c 377 if(socketpair(PF_UNIX, SOCK_STREAM, 0, pfds))
5fc1bf9f 378 break;
af34331c 379 if(sendreq(plex, req, pfds[0]))
5fc1bf9f 380 break;
af34331c 381 close(pfds[0]);
a68db17d 382 out = mtbioopen(pfds[1], 1, 600, "r+", &outi);
a0327573 383
8d19a9ec
FT
384 if(getheader(req, "content-type") != NULL) {
385 if((hd = getheader(req, "content-length")) != NULL) {
386 dlen = atoo(hd);
387 if(dlen > 0) {
388 if(passdata(in, out, dlen) != dlen)
389 break;
390 }
391 } else if(((hd = getheader(req, "transfer-encoding")) != NULL) && !strcasecmp(hd, "chunked")) {
392 if(recvchunks(in, out))
5fc1bf9f 393 break;
8d19a9ec 394 } else {
2dbb9937
FT
395 /* Ignore rather than abort, to be kinder to broken clients. */
396 headrmheader(req, "content-type");
a06a2fbd 397 }
a0327573 398 }
fc253e2f 399 if(bioflush(out))
5fc1bf9f 400 break;
d93d9a05 401 /* Make sure to send EOF */
5fc1bf9f 402 shutdown(pfds[1], SHUT_WR);
9d87a119 403
fc253e2f 404 if((resp = parseresponseb(out)) == NULL)
f9255ddd 405 break;
5fc1bf9f 406 replstr(&resp->ver, req->ver);
1c3e0167
FT
407
408 if(!getheader(resp, "server"))
409 headappheader(resp, "Server", sprintf3("ashd/%s", VERSION));
a68db17d
FT
410 duplex = hasheader(resp, "x-ash-switch", "duplex");
411 trimx(resp);
5fc1bf9f 412
a68db17d
FT
413 if(duplex) {
414 if(outi->rights < 0)
415 break;
416 writerespb(in, resp);
417 bioprintf(in, "\r\n");
418 dout = mtbioopen(outi->rights, 1, 600, "r+", NULL);
419 passduplex(in, infd, dout, outi->rights);
420 outi->rights = -1;
421 bioclose(dout);
422 break;
423 } else if(!strcasecmp(req->ver, "HTTP/1.0")) {
18fb436d 424 if(!strcasecmp(req->method, "head")) {
75bb20c8 425 keep = http10keep(req, resp);
fc253e2f
FT
426 writerespb(in, resp);
427 bioprintf(in, "\r\n");
18fb436d 428 } else if((hd = getheader(resp, "content-length")) != NULL) {
75bb20c8 429 keep = http10keep(req, resp);
5226f7c5 430 dlen = atoo(hd);
fc253e2f
FT
431 writerespb(in, resp);
432 bioprintf(in, "\r\n");
5226f7c5 433 if(passdata(out, in, dlen) != dlen)
5fc1bf9f 434 break;
5fc1bf9f 435 } else {
75bb20c8 436 headrmheader(resp, "connection");
fc253e2f
FT
437 writerespb(in, resp);
438 bioprintf(in, "\r\n");
5fc1bf9f
FT
439 passdata(out, in, -1);
440 break;
9d87a119 441 }
75bb20c8 442 if(!keep)
5fc1bf9f 443 break;
cefb0f7a 444 } else if(!strcasecmp(req->ver, "HTTP/1.1")) {
18fb436d 445 if(!strcasecmp(req->method, "head")) {
fc253e2f
FT
446 writerespb(in, resp);
447 bioprintf(in, "\r\n");
18fb436d 448 } else if((hd = getheader(resp, "content-length")) != NULL) {
fc253e2f
FT
449 writerespb(in, resp);
450 bioprintf(in, "\r\n");
5226f7c5
FT
451 dlen = atoo(hd);
452 if(passdata(out, in, dlen) != dlen)
5fc1bf9f
FT
453 break;
454 } else if(!getheader(resp, "transfer-encoding")) {
455 headappheader(resp, "Transfer-Encoding", "chunked");
fc253e2f
FT
456 writerespb(in, resp);
457 bioprintf(in, "\r\n");
5fc1bf9f
FT
458 if(passchunks(out, in))
459 break;
460 } else {
fc253e2f
FT
461 writerespb(in, resp);
462 bioprintf(in, "\r\n");
5fc1bf9f 463 passdata(out, in, -1);
9d87a119 464 break;
5fc1bf9f
FT
465 }
466 if(hasheader(req, "connection", "close") || hasheader(resp, "connection", "close"))
9d87a119 467 break;
5fc1bf9f
FT
468 } else {
469 break;
9d87a119 470 }
5fc1bf9f 471
fc253e2f 472 bioclose(out);
5fc1bf9f 473 out = NULL;
9d87a119 474 freehthead(req);
9d87a119 475 freehthead(resp);
5fc1bf9f 476 req = resp = NULL;
66987955
FT
477 }
478
5fc1bf9f 479 if(out != NULL)
fc253e2f 480 bioclose(out);
9d87a119
FT
481 if(req != NULL)
482 freehthead(req);
483 if(resp != NULL)
484 freehthead(resp);
fc253e2f 485 bioclose(in);
43066106 486 free(id);
66987955
FT
487}
488
32e24c19
FT
489static void plexwatch(struct muth *muth, va_list args)
490{
491 vavar(int, fd);
492 char *buf;
4139016c 493 int i, s, ret;
32e24c19 494
4139016c 495 s = 0;
32e24c19 496 while(1) {
cac13158
FT
497 if(block(fd, EV_READ, 0) == 0)
498 break;
32e24c19
FT
499 buf = smalloc(65536);
500 ret = recv(fd, buf, 65536, 0);
501 if(ret < 0) {
502 flog(LOG_WARNING, "received error on rootplex read channel: %s", strerror(errno));
503 exit(1);
504 } else if(ret == 0) {
4139016c 505 s = 1;
8e9ec020
FT
506 free(buf);
507 break;
32e24c19
FT
508 }
509 /* Maybe I'd like to implement some protocol in this direction
510 * some day... */
511 free(buf);
512 }
ecd4208b 513 shutdown(plex, SHUT_RDWR);
8e9ec020
FT
514 for(i = 0; i < listeners.d; i++) {
515 if(listeners.b[i] == muth)
516 bufdel(listeners, i);
517 }
4139016c
FT
518 if(s) {
519 flog(LOG_INFO, "root handler exited, so shutting down listening...");
520 while(listeners.d > 0)
521 resume(listeners.b[0], 0);
522 }
32e24c19
FT
523}
524
d341283f
FT
525static void initroot(void *uu)
526{
527 int fd;
528
cf91aa53 529 setsid();
d341283f 530 if(daemonize) {
d341283f
FT
531 chdir("/");
532 if((fd = open("/dev/null", O_RDWR)) >= 0) {
533 dup2(fd, 0);
534 dup2(fd, 1);
535 dup2(fd, 2);
536 close(fd);
537 }
538 }
539 if(usesyslog)
540 putenv("ASHD_USESYSLOG=1");
541 else
542 unsetenv("ASHD_USESYSLOG");
543}
544
8774c31b
FT
545static void usage(FILE *out)
546{
43c58ba2 547 fprintf(out, "usage: htparser [-hSf] [-u USER] [-r ROOT] [-p PIDFILE] PORTSPEC... -- ROOT [ARGS...]\n");
8774c31b 548 fprintf(out, "\twhere PORTSPEC is HANDLER[:PAR[=VAL][(,PAR[=VAL])...]] (try HANDLER:help)\n");
6ca53b2e 549 fprintf(out, "\tavailable handlers are `plain' and `ssl'.\n");
8774c31b
FT
550}
551
552static void addport(char *spec)
553{
554 char *nm, *p, *p2, *n;
555 struct charvbuf pars, vals;
556
557 bufinit(pars);
558 bufinit(vals);
559 if((p = strchr(spec, ':')) == NULL) {
560 nm = spec;
561 } else {
562 nm = spec;
563 *(p++) = 0;
564 do {
565 if((n = strchr(p, ',')) != NULL)
566 *(n++) = 0;
567 if((p2 = strchr(p, '=')) != NULL)
568 *(p2++) = 0;
569 if(!*p) {
570 usage(stderr);
571 exit(1);
572 }
573 bufadd(pars, p);
574 if(p2)
575 bufadd(vals, p2);
576 else
577 bufadd(vals, "");
578 } while((p = n) != NULL);
579 }
580
581 /* XXX: It would be nice to decentralize this, but, meh... */
582 if(!strcmp(nm, "plain")) {
583 handleplain(pars.d, pars.b, vals.b);
6ca53b2e
FT
584#ifdef HAVE_GNUTLS
585 } else if(!strcmp(nm, "ssl")) {
586 handlegnussl(pars.d, pars.b, vals.b);
587#endif
8774c31b
FT
588 } else {
589 flog(LOG_ERR, "htparser: unknown port handler `%s'", nm);
590 exit(1);
591 }
592
593 buffree(pars);
594 buffree(vals);
595}
596
cac13158
FT
597static void sighandler(int sig)
598{
599 exitioloop(1);
600}
601
f0bbedf7
FT
602int main(int argc, char **argv)
603{
cac13158 604 int c, d;
8774c31b 605 int i, s1;
f0cbd8d7 606 char *root;
43c58ba2 607 FILE *pidout;
f0cbd8d7 608 struct passwd *pwent;
f4cdf919 609
d341283f 610 daemonize = usesyslog = 0;
f0cbd8d7
FT
611 root = NULL;
612 pwent = NULL;
43c58ba2 613 while((c = getopt(argc, argv, "+hSfu:r:p:")) >= 0) {
8774c31b
FT
614 switch(c) {
615 case 'h':
616 usage(stdout);
617 exit(0);
f0cbd8d7
FT
618 case 'f':
619 daemonize = 1;
620 break;
621 case 'S':
d341283f 622 usesyslog = 1;
f0cbd8d7
FT
623 break;
624 case 'u':
625 if((pwent = getpwnam(optarg)) == NULL) {
626 flog(LOG_ERR, "could not find user %s", optarg);
627 exit(1);
628 }
629 break;
630 case 'r':
631 root = optarg;
632 break;
43c58ba2
FT
633 case 'p':
634 pidfile = optarg;
635 break;
8774c31b
FT
636 default:
637 usage(stderr);
638 exit(1);
639 }
640 }
8774c31b
FT
641 s1 = 0;
642 for(i = optind; i < argc; i++) {
643 if(!strcmp(argv[i], "--"))
644 break;
645 s1 = 1;
646 addport(argv[i]);
9d87a119 647 }
8774c31b
FT
648 if(!s1 || (i == argc)) {
649 usage(stderr);
650 exit(1);
f4cdf919 651 }
d341283f 652 if((plex = stdmkchild(argv + ++i, initroot, NULL)) < 0) {
8774c31b
FT
653 flog(LOG_ERR, "could not spawn root multiplexer: %s", strerror(errno));
654 return(1);
f4cdf919 655 }
cac13158 656 bufadd(listeners, mustart(plexwatch, plex));
43c58ba2
FT
657 pidout = NULL;
658 if(pidfile != NULL) {
659 if((pidout = fopen(pidfile, "w")) == NULL) {
660 flog(LOG_ERR, "could not open %s for writing: %s", pidfile, strerror(errno));
661 return(1);
662 }
663 }
d341283f 664 if(usesyslog)
f0cbd8d7
FT
665 opensyslog();
666 if(root) {
0370bd82 667 if(chdir(root) || chroot(root)) {
f0cbd8d7
FT
668 flog(LOG_ERR, "could not chroot to %s: %s", root, strerror(errno));
669 exit(1);
670 }
671 }
672 if(pwent) {
673 if(setgid(pwent->pw_gid)) {
674 flog(LOG_ERR, "could not switch group to %i: %s", (int)pwent->pw_gid, strerror(errno));
675 exit(1);
676 }
677 if(setuid(pwent->pw_uid)) {
678 flog(LOG_ERR, "could not switch user to %i: %s", (int)pwent->pw_uid, strerror(errno));
679 exit(1);
680 }
681 }
15fa3fe8 682 signal(SIGPIPE, SIG_IGN);
cac13158
FT
683 signal(SIGCHLD, SIG_IGN);
684 signal(SIGINT, sighandler);
685 signal(SIGTERM, sighandler);
f0cbd8d7
FT
686 if(daemonize) {
687 daemon(0, 0);
688 }
ee036f74 689 if(pidout != NULL) {
43c58ba2 690 fprintf(pidout, "%i\n", getpid());
ee036f74
FT
691 fclose(pidout);
692 }
cac13158
FT
693 d = 0;
694 while(!d) {
695 switch(ioloop()) {
696 case 0:
697 d = 1;
698 break;
699 case 1:
700 if(listeners.d > 0) {
8e9ec020
FT
701 while(listeners.d > 0)
702 resume(listeners.b[0], 0);
cac13158
FT
703 flog(LOG_INFO, "no longer listening");
704 } else {
705 d = 1;
706 }
707 break;
708 }
709 }
f4cdf919 710 return(0);
f0bbedf7 711}