2 3e4749f7 2020-10-02 op * Copyright (c) 2020 Omar Polo <op@omarpolo.com>
4 3e4749f7 2020-10-02 op * Permission to use, copy, modify, and distribute this software for any
5 3e4749f7 2020-10-02 op * purpose with or without fee is hereby granted, provided that the above
6 3e4749f7 2020-10-02 op * copyright notice and this permission notice appear in all copies.
8 3e4749f7 2020-10-02 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3e4749f7 2020-10-02 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3e4749f7 2020-10-02 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3e4749f7 2020-10-02 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3e4749f7 2020-10-02 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3e4749f7 2020-10-02 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3e4749f7 2020-10-02 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 52418c8d 2021-02-12 op #include "gmid.h"
19 8443bff7 2021-01-25 op #include <sys/stat.h>
21 592fd624 2020-10-07 op #include <errno.h>
22 3e4749f7 2020-10-02 op #include <fcntl.h>
23 7e1df73d 2021-03-31 op #include <libgen.h>
24 bcf5d929 2021-02-01 op #include <limits.h>
25 ae08ec7d 2021-01-25 op #include <pwd.h>
26 0cf902af 2020-11-03 op #include <signal.h>
27 3e4749f7 2020-10-02 op #include <string.h>
29 b8e64ccd 2021-03-31 op struct vhosthead hosts;
31 bc99d868 2021-03-19 op int sock4, sock6;
33 bc99d868 2021-03-19 op struct imsgbuf logibuf, exibuf, servibuf[PROC_MAX];
35 d672b8fb 2021-02-03 op const char *config_path, *certs_dir, *hostname;
37 15902770 2021-01-15 op struct conf conf;
39 ca21e100 2021-02-04 op struct tls_config *tlsconf;
40 ae08ec7d 2021-01-25 op struct tls *ctx;
43 b9c9123b 2021-03-20 op dummy_handler(int signo)
48 7e1df73d 2021-03-31 op /* wrapper around dirname(3). dn must be PATH_MAX+1 at least. */
50 7e1df73d 2021-03-31 op pdirname(const char *path, char *dn)
52 7e1df73d 2021-03-31 op char p[PATH_MAX+1];
55 7e1df73d 2021-03-31 op strlcpy(p, path, sizeof(p));
56 7e1df73d 2021-03-31 op t = dirname(p);
57 7e1df73d 2021-03-31 op memmove(dn, t, strlen(t)+1);
61 7e1df73d 2021-03-31 op mkdirs(const char *path, mode_t mode)
63 7e1df73d 2021-03-31 op char dname[PATH_MAX+1];
65 7e1df73d 2021-03-31 op pdirname(path, dname);
66 7e1df73d 2021-03-31 op if (!strcmp(dname, "/"))
68 7e1df73d 2021-03-31 op mkdirs(dname, mode);
69 7e1df73d 2021-03-31 op if (mkdir(path, mode) != 0 && errno != EEXIST)
70 8443bff7 2021-01-25 op fatal("can't mkdir %s: %s", path, strerror(errno));
73 8443bff7 2021-01-25 op /* $XDG_DATA_HOME/gmid */
75 8443bff7 2021-01-25 op data_dir(void)
77 8443bff7 2021-01-25 op const char *home, *xdg;
80 8443bff7 2021-01-25 op if ((xdg = getenv("XDG_DATA_HOME")) == NULL) {
81 8443bff7 2021-01-25 op if ((home = getenv("HOME")) == NULL)
82 8443bff7 2021-01-25 op errx(1, "XDG_DATA_HOME and HOME both empty");
83 8443bff7 2021-01-25 op if (asprintf(&t, "%s/.local/share/gmid", home) == -1)
84 8443bff7 2021-01-25 op err(1, "asprintf");
86 7e1df73d 2021-03-31 op if (asprintf(&t, "%s/gmid", xdg) == -1)
87 7e1df73d 2021-03-31 op err(1, "asprintf");
90 7e1df73d 2021-03-31 op mkdirs(t, 0755);
95 8443bff7 2021-01-25 op load_local_cert(const char *hostname, const char *dir)
97 8443bff7 2021-01-25 op char *cert, *key;
98 b8e64ccd 2021-03-31 op struct vhost *h;
100 8443bff7 2021-01-25 op if (asprintf(&cert, "%s/%s.cert.pem", dir, hostname) == -1)
101 8443bff7 2021-01-25 op errx(1, "asprintf");
102 8443bff7 2021-01-25 op if (asprintf(&key, "%s/%s.key.pem", dir, hostname) == -1)
103 8443bff7 2021-01-25 op errx(1, "asprintf");
105 8443bff7 2021-01-25 op if (access(cert, R_OK) == -1 || access(key, R_OK) == -1)
106 8443bff7 2021-01-25 op gen_certificate(hostname, cert, key);
108 b8e64ccd 2021-03-31 op h = TAILQ_FIRST(&hosts);
109 b8e64ccd 2021-03-31 op h->cert = cert;
110 b8e64ccd 2021-03-31 op h->key = key;
111 b8e64ccd 2021-03-31 op h->domain = hostname;
115 ae08ec7d 2021-01-25 op load_vhosts(void)
117 fdea6aa0 2021-04-30 op struct vhost *h;
118 fdea6aa0 2021-04-30 op struct location *l;
120 b8e64ccd 2021-03-31 op TAILQ_FOREACH(h, &hosts, vhosts) {
121 fdea6aa0 2021-04-30 op TAILQ_FOREACH(l, &h->locations, locations) {
122 fdea6aa0 2021-04-30 op if (l->dir == NULL)
124 fdea6aa0 2021-04-30 op if ((l->dirfd = open(l->dir, O_RDONLY | O_DIRECTORY)) == -1)
125 fdea6aa0 2021-04-30 op fatal("open %s for domain %s", l->dir, h->domain);
131 9468027b 2020-10-15 op make_socket(int port, int family)
134 9468027b 2020-10-15 op struct sockaddr_in addr4;
135 9468027b 2020-10-15 op struct sockaddr_in6 addr6;
136 9468027b 2020-10-15 op struct sockaddr *addr;
137 9468027b 2020-10-15 op socklen_t len;
139 9468027b 2020-10-15 op switch (family) {
140 9468027b 2020-10-15 op case AF_INET:
141 9468027b 2020-10-15 op bzero(&addr4, sizeof(addr4));
142 9468027b 2020-10-15 op addr4.sin_family = family;
143 9468027b 2020-10-15 op addr4.sin_port = htons(port);
144 9468027b 2020-10-15 op addr4.sin_addr.s_addr = INADDR_ANY;
145 9468027b 2020-10-15 op addr = (struct sockaddr*)&addr4;
146 9468027b 2020-10-15 op len = sizeof(addr4);
149 9468027b 2020-10-15 op case AF_INET6:
150 9468027b 2020-10-15 op bzero(&addr6, sizeof(addr6));
151 9468027b 2020-10-15 op addr6.sin6_family = AF_INET6;
152 9468027b 2020-10-15 op addr6.sin6_port = htons(port);
153 9468027b 2020-10-15 op addr6.sin6_addr = in6addr_any;
154 9468027b 2020-10-15 op addr = (struct sockaddr*)&addr6;
155 9468027b 2020-10-15 op len = sizeof(addr6);
159 9468027b 2020-10-15 op /* unreachable */
163 9468027b 2020-10-15 op if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
164 80bbcad5 2021-01-10 op fatal("socket: %s", strerror(errno));
167 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
168 80bbcad5 2021-01-10 op fatal("setsockopt(SO_REUSEADDR): %s", strerror(errno));
171 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
172 80bbcad5 2021-01-10 op fatal("setsockopt(SO_REUSEPORT): %s", strerror(errno));
174 592fd624 2020-10-07 op mark_nonblock(sock);
176 9468027b 2020-10-15 op if (bind(sock, addr, len) == -1)
177 80bbcad5 2021-01-10 op fatal("bind: %s", strerror(errno));
179 3e4749f7 2020-10-02 op if (listen(sock, 16) == -1)
180 80bbcad5 2021-01-10 op fatal("listen: %s", strerror(errno));
186 ae08ec7d 2021-01-25 op setup_tls(void)
188 ae08ec7d 2021-01-25 op struct vhost *h;
190 881a9dd9 2021-01-16 op if ((tlsconf = tls_config_new()) == NULL)
191 132cae8c 2021-01-18 op fatal("tls_config_new");
193 881a9dd9 2021-01-16 op /* optionally accept client certs, but don't try to verify them */
194 881a9dd9 2021-01-16 op tls_config_verify_client_optional(tlsconf);
195 881a9dd9 2021-01-16 op tls_config_insecure_noverifycert(tlsconf);
197 881a9dd9 2021-01-16 op if (tls_config_set_protocols(tlsconf, conf.protos) == -1)
198 132cae8c 2021-01-18 op fatal("tls_config_set_protocols");
200 881a9dd9 2021-01-16 op if ((ctx = tls_server()) == NULL)
201 132cae8c 2021-01-18 op fatal("tls_server failure");
203 b8e64ccd 2021-03-31 op h = TAILQ_FIRST(&hosts);
205 ae08ec7d 2021-01-25 op /* we need to set something, then we can add how many key we want */
206 b8e64ccd 2021-03-31 op if (tls_config_set_keypair_file(tlsconf, h->cert, h->key))
207 ca21e100 2021-02-04 op fatal("tls_config_set_keypair_file failed for (%s, %s)",
208 b8e64ccd 2021-03-31 op h->cert, h->key);
210 b8e64ccd 2021-03-31 op while ((h = TAILQ_NEXT(h, vhosts)) != NULL) {
211 ae08ec7d 2021-01-25 op if (tls_config_add_keypair_file(tlsconf, h->cert, h->key) == -1)
212 ae08ec7d 2021-01-25 op fatal("failed to load the keypair (%s, %s)",
213 ae08ec7d 2021-01-25 op h->cert, h->key);
216 881a9dd9 2021-01-16 op if (tls_configure(ctx, tlsconf) == -1)
217 132cae8c 2021-01-18 op fatal("tls_configure: %s", tls_error(ctx));
221 bc99d868 2021-03-19 op listener_main(struct imsgbuf *ibuf)
224 ae08ec7d 2021-01-25 op load_default_mime(&conf.mime);
225 ae08ec7d 2021-01-25 op load_vhosts();
226 bc99d868 2021-03-19 op loop(ctx, sock4, sock6, ibuf);
231 8d6ae384 2021-01-24 op init_config(void)
233 b8e64ccd 2021-03-31 op TAILQ_INIT(&hosts);
235 15902770 2021-01-15 op conf.port = 1965;
236 15902770 2021-01-15 op conf.ipv6 = 0;
237 5bc3c98e 2021-01-15 op conf.protos = TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3;
239 b2a6b613 2021-01-21 op init_mime(&conf.mime);
241 ae08ec7d 2021-01-25 op conf.chroot = NULL;
242 ae08ec7d 2021-01-25 op conf.user = NULL;
244 a709ddf5 2021-02-07 op conf.prefork = 3;
248 ca21e100 2021-02-04 op free_config(void)
250 b8e64ccd 2021-03-31 op struct vhost *h, *th;
251 b8e64ccd 2021-03-31 op struct location *l, *tl;
252 9cc630aa 2021-04-28 op struct envlist *e, *te;
253 cc8c2901 2021-04-29 op struct alist *a, *ta;
256 419a4235 2021-04-28 op v = conf.verbose;
258 ca21e100 2021-02-04 op free(conf.chroot);
259 ca21e100 2021-02-04 op free(conf.user);
260 ca21e100 2021-02-04 op memset(&conf, 0, sizeof(conf));
262 419a4235 2021-04-28 op conf.verbose = v;
264 b8e64ccd 2021-03-31 op TAILQ_FOREACH_SAFE(h, &hosts, vhosts, th) {
265 b8e64ccd 2021-03-31 op TAILQ_FOREACH_SAFE(l, &h->locations, locations, tl) {
266 b8e64ccd 2021-03-31 op TAILQ_REMOVE(&h->locations, l, locations);
268 ca21e100 2021-02-04 op free((char*)l->match);
269 ca21e100 2021-02-04 op free((char*)l->lang);
270 ca21e100 2021-02-04 op free((char*)l->default_mime);
271 ca21e100 2021-02-04 op free((char*)l->index);
272 6abda252 2021-02-06 op free((char*)l->block_fmt);
273 fdea6aa0 2021-04-30 op free((char*)l->dir);
275 fdea6aa0 2021-04-30 op if (l->dirfd != -1)
276 fdea6aa0 2021-04-30 op close(l->dirfd);
281 9cc630aa 2021-04-28 op TAILQ_FOREACH_SAFE(e, &h->env, envs, te) {
282 9cc630aa 2021-04-28 op free(e->name);
283 9cc630aa 2021-04-28 op free(e->value);
287 cc8c2901 2021-04-29 op TAILQ_FOREACH_SAFE(a, &h->aliases, aliases, ta) {
288 cc8c2901 2021-04-29 op free(a->alias);
292 fdea6aa0 2021-04-30 op free((char*)h->domain);
293 fdea6aa0 2021-04-30 op free((char*)h->cert);
294 fdea6aa0 2021-04-30 op free((char*)h->key);
295 fdea6aa0 2021-04-30 op free((char*)h->cgi);
296 fdea6aa0 2021-04-30 op free((char*)h->entrypoint);
298 b8e64ccd 2021-03-31 op TAILQ_REMOVE(&hosts, h, vhosts);
302 ca21e100 2021-02-04 op tls_free(ctx);
303 ca21e100 2021-02-04 op tls_config_free(tlsconf);
307 1d3eb470 2021-03-20 op wait_signal(void)
309 ca21e100 2021-02-04 op sigset_t mask;
312 ca21e100 2021-02-04 op sigemptyset(&mask);
313 ca21e100 2021-02-04 op sigaddset(&mask, SIGHUP);
314 1d3eb470 2021-03-20 op sigaddset(&mask, SIGINT);
315 1d3eb470 2021-03-20 op sigaddset(&mask, SIGTERM);
316 ca21e100 2021-02-04 op sigwait(&mask, &signo);
318 1d3eb470 2021-03-20 op return signo == SIGHUP;
322 ae08ec7d 2021-01-25 op drop_priv(void)
324 ae08ec7d 2021-01-25 op struct passwd *pw = NULL;
326 ae08ec7d 2021-01-25 op if (conf.chroot != NULL && conf.user == NULL)
327 ae08ec7d 2021-01-25 op fatal("can't chroot without an user to switch to after.");
329 ae08ec7d 2021-01-25 op if (conf.user != NULL) {
330 ae08ec7d 2021-01-25 op if ((pw = getpwnam(conf.user)) == NULL)
331 ae08ec7d 2021-01-25 op fatal("can't find user %s", conf.user);
334 ae08ec7d 2021-01-25 op if (conf.chroot != NULL) {
335 ae08ec7d 2021-01-25 op if (chroot(conf.chroot) != 0 || chdir("/") != 0)
336 ae08ec7d 2021-01-25 op fatal("%s: %s", conf.chroot, strerror(errno));
339 ae08ec7d 2021-01-25 op if (pw != NULL) {
340 ae08ec7d 2021-01-25 op if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
341 ae08ec7d 2021-01-25 op fatal("setresuid(%d): %s", pw->pw_uid,
342 ae08ec7d 2021-01-25 op strerror(errno));
345 ae08ec7d 2021-01-25 op if (getuid() == 0)
346 3abf91b0 2021-02-07 op log_warn(NULL, "not a good idea to run a network daemon as root");
350 3abf91b0 2021-02-07 op usage(const char *me)
352 3abf91b0 2021-02-07 op fprintf(stderr,
353 8e8b2e25 2021-04-28 op "USAGE: %s [-fn] [-c config] [-P pidfile] | [-6h] [-d certs-dir] [-H host]\n"
354 aa372875 2021-02-10 op " [-p port] [-x cgi] [dir]\n",
359 376a5407 2021-02-23 op logger_init(void)
363 376a5407 2021-02-23 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
364 376a5407 2021-02-23 op err(1, "socketpair");
366 376a5407 2021-02-23 op switch (fork()) {
368 376a5407 2021-02-23 op err(1, "fork");
370 bc99d868 2021-03-19 op signal(SIGHUP, SIG_IGN);
372 376a5407 2021-02-23 op setproctitle("logger");
373 bc99d868 2021-03-19 op imsg_init(&logibuf, p[1]);
375 bc99d868 2021-03-19 op _exit(logger_main(p[1], &logibuf));
378 bc99d868 2021-03-19 op imsg_init(&logibuf, p[0]);
384 bc99d868 2021-03-19 op serve(int argc, char **argv, struct imsgbuf *ibuf)
386 b8e64ccd 2021-03-31 op char path[PATH_MAX];
388 b8e64ccd 2021-03-31 op struct vhost *h;
389 b8e64ccd 2021-03-31 op struct location *l;
391 2c3e53da 2021-03-03 op if (config_path == NULL) {
392 d672b8fb 2021-02-03 op if (hostname == NULL)
393 d672b8fb 2021-02-03 op hostname = "localhost";
394 d672b8fb 2021-02-03 op if (certs_dir == NULL)
395 d672b8fb 2021-02-03 op certs_dir = data_dir();
396 d672b8fb 2021-02-03 op load_local_cert(hostname, certs_dir);
398 b8e64ccd 2021-03-31 op h = TAILQ_FIRST(&hosts);
399 b8e64ccd 2021-03-31 op h->domain = "*";
401 b8e64ccd 2021-03-31 op l = TAILQ_FIRST(&h->locations);
402 b8e64ccd 2021-03-31 op l->auto_index = 1;
403 b8e64ccd 2021-03-31 op l->match = "*";
405 d672b8fb 2021-02-03 op switch (argc) {
407 fdea6aa0 2021-04-30 op l->dir = getcwd(path, sizeof(path));
410 fdea6aa0 2021-04-30 op l->dir = absolutify_path(argv[0]);
413 d672b8fb 2021-02-03 op usage(getprogname());
417 fdea6aa0 2021-04-30 op log_notice(NULL, "serving %s on port %d", l->dir, conf.port);
420 d672b8fb 2021-02-03 op /* setup tls before dropping privileges: we don't want user
421 d672b8fb 2021-02-03 op * to put private certs inside the chroot. */
424 2c3e53da 2021-03-03 op for (i = 0; i < conf.prefork; ++i) {
425 2c3e53da 2021-03-03 op if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC,
426 2c3e53da 2021-03-03 op PF_UNSPEC, p) == -1)
427 2c3e53da 2021-03-03 op fatal("socketpair: %s", strerror(errno));
429 2c3e53da 2021-03-03 op switch (fork()) {
431 2c3e53da 2021-03-03 op fatal("fork: %s", strerror(errno));
432 2c3e53da 2021-03-03 op case 0: /* child */
434 bc99d868 2021-03-19 op imsg_init(&exibuf, p[1]);
435 2c3e53da 2021-03-03 op setproctitle("server");
436 bc99d868 2021-03-19 op _exit(listener_main(&exibuf));
439 bc99d868 2021-03-19 op imsg_init(&servibuf[i], p[0]);
443 2c3e53da 2021-03-03 op setproctitle("executor");
445 bc99d868 2021-03-19 op _exit(executor_main(ibuf));
449 419a4235 2021-04-28 op write_pidfile(const char *pidfile)
451 419a4235 2021-04-28 op struct flock lock;
454 419a4235 2021-04-28 op if (pidfile == NULL)
457 419a4235 2021-04-28 op if ((fd = open(pidfile, O_WRONLY|O_CREAT|O_CLOEXEC, 0600)) == -1)
458 419a4235 2021-04-28 op fatal("can't open pidfile %s: %s", pidfile, strerror(errno));
460 419a4235 2021-04-28 op lock.l_start = 0;
461 419a4235 2021-04-28 op lock.l_len = 0;
462 419a4235 2021-04-28 op lock.l_type = F_WRLCK;
463 419a4235 2021-04-28 op lock.l_whence = SEEK_SET;
465 419a4235 2021-04-28 op if (fcntl(fd, F_SETLK, &lock) == -1)
466 419a4235 2021-04-28 op fatal("can't lock %s, gmid is already running?", pidfile);
468 419a4235 2021-04-28 op if (ftruncate(fd, 0) == -1)
469 419a4235 2021-04-28 op fatal("ftruncate: %s: %s", pidfile, strerror(errno));
471 419a4235 2021-04-28 op dprintf(fd, "%d\n", getpid());
477 b8e64ccd 2021-03-31 op setup_configless(int argc, char **argv, const char *cgi)
479 b8e64ccd 2021-03-31 op struct vhost *host;
480 b8e64ccd 2021-03-31 op struct location *loc;
482 b8e64ccd 2021-03-31 op host = xcalloc(1, sizeof(*host));
483 b8e64ccd 2021-03-31 op host->cgi = cgi;
484 b8e64ccd 2021-03-31 op TAILQ_INSERT_HEAD(&hosts, host, vhosts);
486 b8e64ccd 2021-03-31 op loc = xcalloc(1, sizeof(*loc));
487 b8e64ccd 2021-03-31 op TAILQ_INSERT_HEAD(&host->locations, loc, locations);
489 b8e64ccd 2021-03-31 op serve(argc, argv, NULL);
490 b8e64ccd 2021-03-31 op imsg_compose(&logibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
491 b8e64ccd 2021-03-31 op imsg_flush(&logibuf);
495 8d6ae384 2021-01-24 op main(int argc, char **argv)
497 d632468d 2021-03-20 op struct imsgbuf exibuf;
498 2c3e53da 2021-03-03 op int ch, conftest = 0, configless = 0;
499 8e8b2e25 2021-04-28 op int pidfd, old_ipv6, old_port;
500 8e8b2e25 2021-04-28 op const char *pidfile = NULL, *cgi = NULL;
502 501e489c 2021-01-24 op init_config();
504 8e8b2e25 2021-04-28 op while ((ch = getopt(argc, argv, "6c:d:fH:hnP:p:vx:")) != -1) {
505 3e4749f7 2020-10-02 op switch (ch) {
507 15902770 2021-01-15 op conf.ipv6 = 1;
508 8443bff7 2021-01-25 op configless = 1;
512 ca21e100 2021-02-04 op config_path = absolutify_path(optarg);
516 8443bff7 2021-01-25 op certs_dir = optarg;
517 8443bff7 2021-01-25 op configless = 1;
521 3abf91b0 2021-02-07 op conf.foreground = 1;
525 8443bff7 2021-01-25 op hostname = optarg;
526 8443bff7 2021-01-25 op configless = 1;
530 3e4749f7 2020-10-02 op usage(*argv);
534 15902770 2021-01-15 op conftest = 1;
538 8e8b2e25 2021-04-28 op pidfile = optarg;
542 15902770 2021-01-15 op conf.port = parse_portno(optarg);
543 8443bff7 2021-01-25 op configless = 1;
547 3abf91b0 2021-02-07 op conf.verbose++;
551 15902770 2021-01-15 op /* drop the starting / (if any) */
552 15902770 2021-01-15 op if (*optarg == '/')
554 b8e64ccd 2021-03-31 op cgi = optarg;
555 8443bff7 2021-01-25 op configless = 1;
559 3e4749f7 2020-10-02 op usage(*argv);
563 8443bff7 2021-01-25 op argc -= optind;
564 8443bff7 2021-01-25 op argv += optind;
566 d672b8fb 2021-02-03 op if (config_path == NULL) {
567 72bbed91 2021-01-27 op configless = 1;
568 3abf91b0 2021-02-07 op conf.foreground = 1;
569 2c3e53da 2021-03-03 op conf.prefork = 1;
570 3abf91b0 2021-02-07 op conf.verbose++;
573 d672b8fb 2021-02-03 op if (config_path != NULL && (argc > 0 || configless))
574 48b69cb2 2021-04-28 op errx(1, "can't specify options in config mode.");
576 132cae8c 2021-01-18 op if (conftest) {
577 d672b8fb 2021-02-03 op parse_conf(config_path);
578 132cae8c 2021-01-18 op puts("config OK");
582 3abf91b0 2021-02-07 op if (!conf.foreground && !configless) {
583 0170ba02 2021-01-17 op if (daemon(1, 1) == -1)
584 d278a0c3 2021-02-23 op err(1, "daemon");
587 d672b8fb 2021-02-03 op if (config_path != NULL)
588 d672b8fb 2021-02-03 op parse_conf(config_path);
590 d278a0c3 2021-02-23 op logger_init();
592 d672b8fb 2021-02-03 op sock4 = make_socket(conf.port, AF_INET);
594 d672b8fb 2021-02-03 op if (conf.ipv6)
595 d672b8fb 2021-02-03 op sock6 = make_socket(conf.port, AF_INET6);
597 3841a369 2021-04-20 op signal(SIGPIPE, SIG_IGN);
598 3841a369 2021-04-20 op signal(SIGCHLD, SIG_IGN);
600 bc99d868 2021-03-19 op if (configless) {
601 b8e64ccd 2021-03-31 op setup_configless(argc, argv, cgi);
605 8e8b2e25 2021-04-28 op pidfd = write_pidfile(pidfile);
607 b9c9123b 2021-03-20 op /* Linux seems to call the event handlers even when we're
608 1b333122 2021-04-26 op * doing a sigwait. These dummy handlers are here to avoid
609 5d1474a5 2021-04-20 op * being terminated on SIGHUP, SIGINT or SIGTERM. */
610 b9c9123b 2021-03-20 op signal(SIGHUP, dummy_handler);
611 b9c9123b 2021-03-20 op signal(SIGINT, dummy_handler);
612 b9c9123b 2021-03-20 op signal(SIGTERM, dummy_handler);
614 ca21e100 2021-02-04 op /* wait a sighup and reload the daemon */
618 bc99d868 2021-03-19 op if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC,
619 bc99d868 2021-03-19 op PF_UNSPEC, p) == -1)
620 bc99d868 2021-03-19 op fatal("socketpair: %s", strerror(errno));
622 ca21e100 2021-02-04 op switch (fork()) {
624 ca21e100 2021-02-04 op fatal("fork: %s", strerror(errno));
627 bc99d868 2021-03-19 op imsg_init(&exibuf, p[1]);
628 bc99d868 2021-03-19 op _exit(serve(argc, argv, &exibuf));
632 bc99d868 2021-03-19 op imsg_init(&exibuf, p[0]);
634 1d3eb470 2021-03-20 op if (!wait_signal())
637 3abf91b0 2021-02-07 op log_info(NULL, "reloading configuration %s", config_path);
639 bc99d868 2021-03-19 op /* close the executor (it'll close the servers too) */
640 bc99d868 2021-03-19 op imsg_compose(&exibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
641 bc99d868 2021-03-19 op imsg_flush(&exibuf);
644 ca21e100 2021-02-04 op old_ipv6 = conf.ipv6;
645 ca21e100 2021-02-04 op old_port = conf.port;
647 ca21e100 2021-02-04 op free_config();
648 ca21e100 2021-02-04 op init_config();
649 ca21e100 2021-02-04 op parse_conf(config_path);
651 ca21e100 2021-02-04 op if (old_port != conf.port) {
652 ca21e100 2021-02-04 op close(sock4);
653 ca21e100 2021-02-04 op close(sock6);
658 ca21e100 2021-02-04 op if (sock6 != -1 && old_ipv6 != conf.ipv6) {
659 ca21e100 2021-02-04 op close(sock6);
663 ca21e100 2021-02-04 op if (sock4 == -1)
664 ca21e100 2021-02-04 op sock4 = make_socket(conf.port, AF_INET);
665 ca21e100 2021-02-04 op if (sock6 == -1 && conf.ipv6)
666 ca21e100 2021-02-04 op sock6 = make_socket(conf.port, AF_INET6);
669 1d3eb470 2021-03-20 op imsg_compose(&exibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
670 1d3eb470 2021-03-20 op imsg_flush(&exibuf);
672 1d3eb470 2021-03-20 op imsg_compose(&logibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
673 1d3eb470 2021-03-20 op imsg_flush(&logibuf);
675 8e8b2e25 2021-04-28 op if (pidfd != -1)
676 8e8b2e25 2021-04-28 op close(pidfd);