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