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 8ad1c570 2021-05-09 op struct fcgi fcgi[FCGI_MAX];
31 b8e64ccd 2021-03-31 op struct vhosthead hosts;
33 bc99d868 2021-03-19 op int sock4, sock6;
35 bc99d868 2021-03-19 op struct imsgbuf logibuf, exibuf, servibuf[PROC_MAX];
37 7b2d7432 2021-05-01 op const char *config_path, *certs_dir, *hostname, *pidfile, *cgi;
39 15902770 2021-01-15 op struct conf conf;
41 ca21e100 2021-02-04 op struct tls_config *tlsconf;
42 ae08ec7d 2021-01-25 op struct tls *ctx;
45 b9c9123b 2021-03-20 op dummy_handler(int signo)
50 7e1df73d 2021-03-31 op /* wrapper around dirname(3). dn must be PATH_MAX+1 at least. */
52 7e1df73d 2021-03-31 op pdirname(const char *path, char *dn)
54 7e1df73d 2021-03-31 op char p[PATH_MAX+1];
57 7e1df73d 2021-03-31 op strlcpy(p, path, sizeof(p));
58 7e1df73d 2021-03-31 op t = dirname(p);
59 7e1df73d 2021-03-31 op memmove(dn, t, strlen(t)+1);
63 7e1df73d 2021-03-31 op mkdirs(const char *path, mode_t mode)
65 7e1df73d 2021-03-31 op char dname[PATH_MAX+1];
67 7e1df73d 2021-03-31 op pdirname(path, dname);
68 7e1df73d 2021-03-31 op if (!strcmp(dname, "/"))
70 7e1df73d 2021-03-31 op mkdirs(dname, mode);
71 7e1df73d 2021-03-31 op if (mkdir(path, mode) != 0 && errno != EEXIST)
72 8443bff7 2021-01-25 op fatal("can't mkdir %s: %s", path, strerror(errno));
75 8443bff7 2021-01-25 op /* $XDG_DATA_HOME/gmid */
77 8443bff7 2021-01-25 op data_dir(void)
79 8443bff7 2021-01-25 op const char *home, *xdg;
82 8443bff7 2021-01-25 op if ((xdg = getenv("XDG_DATA_HOME")) == NULL) {
83 8443bff7 2021-01-25 op if ((home = getenv("HOME")) == NULL)
84 8443bff7 2021-01-25 op errx(1, "XDG_DATA_HOME and HOME both empty");
85 8443bff7 2021-01-25 op if (asprintf(&t, "%s/.local/share/gmid", home) == -1)
86 8443bff7 2021-01-25 op err(1, "asprintf");
88 7e1df73d 2021-03-31 op if (asprintf(&t, "%s/gmid", xdg) == -1)
89 7e1df73d 2021-03-31 op err(1, "asprintf");
92 7e1df73d 2021-03-31 op mkdirs(t, 0755);
97 8443bff7 2021-01-25 op load_local_cert(const char *hostname, const char *dir)
99 8443bff7 2021-01-25 op char *cert, *key;
100 b8e64ccd 2021-03-31 op struct vhost *h;
102 8443bff7 2021-01-25 op if (asprintf(&cert, "%s/%s.cert.pem", dir, hostname) == -1)
103 8443bff7 2021-01-25 op errx(1, "asprintf");
104 8443bff7 2021-01-25 op if (asprintf(&key, "%s/%s.key.pem", dir, hostname) == -1)
105 8443bff7 2021-01-25 op errx(1, "asprintf");
107 8443bff7 2021-01-25 op if (access(cert, R_OK) == -1 || access(key, R_OK) == -1)
108 8443bff7 2021-01-25 op gen_certificate(hostname, cert, key);
110 b8e64ccd 2021-03-31 op h = TAILQ_FIRST(&hosts);
111 b8e64ccd 2021-03-31 op h->cert = cert;
112 b8e64ccd 2021-03-31 op h->key = key;
113 b8e64ccd 2021-03-31 op h->domain = hostname;
117 ae08ec7d 2021-01-25 op load_vhosts(void)
119 fdea6aa0 2021-04-30 op struct vhost *h;
120 fdea6aa0 2021-04-30 op struct location *l;
122 b8e64ccd 2021-03-31 op TAILQ_FOREACH(h, &hosts, vhosts) {
123 fdea6aa0 2021-04-30 op TAILQ_FOREACH(l, &h->locations, locations) {
124 fdea6aa0 2021-04-30 op if (l->dir == NULL)
126 fdea6aa0 2021-04-30 op if ((l->dirfd = open(l->dir, O_RDONLY | O_DIRECTORY)) == -1)
127 fdea6aa0 2021-04-30 op fatal("open %s for domain %s", l->dir, h->domain);
133 9468027b 2020-10-15 op make_socket(int port, int family)
136 9468027b 2020-10-15 op struct sockaddr_in addr4;
137 9468027b 2020-10-15 op struct sockaddr_in6 addr6;
138 9468027b 2020-10-15 op struct sockaddr *addr;
139 9468027b 2020-10-15 op socklen_t len;
141 9468027b 2020-10-15 op switch (family) {
142 9468027b 2020-10-15 op case AF_INET:
143 9468027b 2020-10-15 op bzero(&addr4, sizeof(addr4));
144 9468027b 2020-10-15 op addr4.sin_family = family;
145 9468027b 2020-10-15 op addr4.sin_port = htons(port);
146 9468027b 2020-10-15 op addr4.sin_addr.s_addr = INADDR_ANY;
147 9468027b 2020-10-15 op addr = (struct sockaddr*)&addr4;
148 9468027b 2020-10-15 op len = sizeof(addr4);
151 9468027b 2020-10-15 op case AF_INET6:
152 9468027b 2020-10-15 op bzero(&addr6, sizeof(addr6));
153 9468027b 2020-10-15 op addr6.sin6_family = AF_INET6;
154 9468027b 2020-10-15 op addr6.sin6_port = htons(port);
155 9468027b 2020-10-15 op addr6.sin6_addr = in6addr_any;
156 9468027b 2020-10-15 op addr = (struct sockaddr*)&addr6;
157 9468027b 2020-10-15 op len = sizeof(addr6);
161 9468027b 2020-10-15 op /* unreachable */
165 9468027b 2020-10-15 op if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
166 80bbcad5 2021-01-10 op fatal("socket: %s", strerror(errno));
169 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
170 80bbcad5 2021-01-10 op fatal("setsockopt(SO_REUSEADDR): %s", strerror(errno));
173 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
174 80bbcad5 2021-01-10 op fatal("setsockopt(SO_REUSEPORT): %s", strerror(errno));
176 592fd624 2020-10-07 op mark_nonblock(sock);
178 9468027b 2020-10-15 op if (bind(sock, addr, len) == -1)
179 80bbcad5 2021-01-10 op fatal("bind: %s", strerror(errno));
181 3e4749f7 2020-10-02 op if (listen(sock, 16) == -1)
182 80bbcad5 2021-01-10 op fatal("listen: %s", strerror(errno));
188 ae08ec7d 2021-01-25 op setup_tls(void)
190 ae08ec7d 2021-01-25 op struct vhost *h;
192 881a9dd9 2021-01-16 op if ((tlsconf = tls_config_new()) == NULL)
193 132cae8c 2021-01-18 op fatal("tls_config_new");
195 881a9dd9 2021-01-16 op /* optionally accept client certs, but don't try to verify them */
196 881a9dd9 2021-01-16 op tls_config_verify_client_optional(tlsconf);
197 881a9dd9 2021-01-16 op tls_config_insecure_noverifycert(tlsconf);
199 881a9dd9 2021-01-16 op if (tls_config_set_protocols(tlsconf, conf.protos) == -1)
200 132cae8c 2021-01-18 op fatal("tls_config_set_protocols");
202 881a9dd9 2021-01-16 op if ((ctx = tls_server()) == NULL)
203 132cae8c 2021-01-18 op fatal("tls_server failure");
205 b8e64ccd 2021-03-31 op h = TAILQ_FIRST(&hosts);
207 ae08ec7d 2021-01-25 op /* we need to set something, then we can add how many key we want */
208 b8e64ccd 2021-03-31 op if (tls_config_set_keypair_file(tlsconf, h->cert, h->key))
209 ca21e100 2021-02-04 op fatal("tls_config_set_keypair_file failed for (%s, %s)",
210 b8e64ccd 2021-03-31 op h->cert, h->key);
212 b8e64ccd 2021-03-31 op while ((h = TAILQ_NEXT(h, vhosts)) != NULL) {
213 ae08ec7d 2021-01-25 op if (tls_config_add_keypair_file(tlsconf, h->cert, h->key) == -1)
214 ae08ec7d 2021-01-25 op fatal("failed to load the keypair (%s, %s)",
215 ae08ec7d 2021-01-25 op h->cert, h->key);
218 881a9dd9 2021-01-16 op if (tls_configure(ctx, tlsconf) == -1)
219 132cae8c 2021-01-18 op fatal("tls_configure: %s", tls_error(ctx));
223 bc99d868 2021-03-19 op listener_main(struct imsgbuf *ibuf)
226 ae08ec7d 2021-01-25 op load_default_mime(&conf.mime);
227 ae08ec7d 2021-01-25 op load_vhosts();
228 bc99d868 2021-03-19 op loop(ctx, sock4, sock6, ibuf);
233 8d6ae384 2021-01-24 op init_config(void)
235 b8e64ccd 2021-03-31 op TAILQ_INIT(&hosts);
237 15902770 2021-01-15 op conf.port = 1965;
238 15902770 2021-01-15 op conf.ipv6 = 0;
239 5bc3c98e 2021-01-15 op conf.protos = TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3;
241 b2a6b613 2021-01-21 op init_mime(&conf.mime);
243 ae08ec7d 2021-01-25 op conf.chroot = NULL;
244 ae08ec7d 2021-01-25 op conf.user = NULL;
246 a709ddf5 2021-02-07 op conf.prefork = 3;
250 ca21e100 2021-02-04 op free_config(void)
252 b8e64ccd 2021-03-31 op struct vhost *h, *th;
253 b8e64ccd 2021-03-31 op struct location *l, *tl;
254 9cc630aa 2021-04-28 op struct envlist *e, *te;
255 cc8c2901 2021-04-29 op struct alist *a, *ta;
258 419a4235 2021-04-28 op v = conf.verbose;
260 ca21e100 2021-02-04 op free(conf.chroot);
261 ca21e100 2021-02-04 op free(conf.user);
262 ca21e100 2021-02-04 op memset(&conf, 0, sizeof(conf));
264 419a4235 2021-04-28 op conf.verbose = v;
266 b8e64ccd 2021-03-31 op TAILQ_FOREACH_SAFE(h, &hosts, vhosts, th) {
267 b8e64ccd 2021-03-31 op TAILQ_FOREACH_SAFE(l, &h->locations, locations, tl) {
268 b8e64ccd 2021-03-31 op TAILQ_REMOVE(&h->locations, l, locations);
270 ca21e100 2021-02-04 op free((char*)l->match);
271 ca21e100 2021-02-04 op free((char*)l->lang);
272 ca21e100 2021-02-04 op free((char*)l->default_mime);
273 ca21e100 2021-02-04 op free((char*)l->index);
274 6abda252 2021-02-06 op free((char*)l->block_fmt);
275 fdea6aa0 2021-04-30 op free((char*)l->dir);
277 fdea6aa0 2021-04-30 op if (l->dirfd != -1)
278 fdea6aa0 2021-04-30 op close(l->dirfd);
283 9cc630aa 2021-04-28 op TAILQ_FOREACH_SAFE(e, &h->env, envs, te) {
284 9cc630aa 2021-04-28 op free(e->name);
285 9cc630aa 2021-04-28 op free(e->value);
289 cc8c2901 2021-04-29 op TAILQ_FOREACH_SAFE(a, &h->aliases, aliases, ta) {
290 cc8c2901 2021-04-29 op free(a->alias);
294 fdea6aa0 2021-04-30 op free((char*)h->domain);
295 fdea6aa0 2021-04-30 op free((char*)h->cert);
296 fdea6aa0 2021-04-30 op free((char*)h->key);
297 fdea6aa0 2021-04-30 op free((char*)h->cgi);
298 fdea6aa0 2021-04-30 op free((char*)h->entrypoint);
300 b8e64ccd 2021-03-31 op TAILQ_REMOVE(&hosts, h, vhosts);
304 8ad1c570 2021-05-09 op for (i = 0; i < FCGI_MAX; ++i) {
305 8ad1c570 2021-05-09 op if (fcgi[i].path == NULL && fcgi[i].prog == NULL)
307 8ad1c570 2021-05-09 op free(fcgi[i].path);
308 8ad1c570 2021-05-09 op free(fcgi[i].port);
309 8ad1c570 2021-05-09 op free(fcgi[i].prog);
312 ca21e100 2021-02-04 op tls_free(ctx);
313 ca21e100 2021-02-04 op tls_config_free(tlsconf);
317 1d3eb470 2021-03-20 op wait_signal(void)
319 ca21e100 2021-02-04 op sigset_t mask;
322 ca21e100 2021-02-04 op sigemptyset(&mask);
323 ca21e100 2021-02-04 op sigaddset(&mask, SIGHUP);
324 1d3eb470 2021-03-20 op sigaddset(&mask, SIGINT);
325 1d3eb470 2021-03-20 op sigaddset(&mask, SIGTERM);
326 ca21e100 2021-02-04 op sigwait(&mask, &signo);
328 1d3eb470 2021-03-20 op return signo == SIGHUP;
332 ae08ec7d 2021-01-25 op drop_priv(void)
334 ae08ec7d 2021-01-25 op struct passwd *pw = NULL;
336 ae08ec7d 2021-01-25 op if (conf.chroot != NULL && conf.user == NULL)
337 ae08ec7d 2021-01-25 op fatal("can't chroot without an user to switch to after.");
339 ae08ec7d 2021-01-25 op if (conf.user != NULL) {
340 ae08ec7d 2021-01-25 op if ((pw = getpwnam(conf.user)) == NULL)
341 ae08ec7d 2021-01-25 op fatal("can't find user %s", conf.user);
344 ae08ec7d 2021-01-25 op if (conf.chroot != NULL) {
345 ae08ec7d 2021-01-25 op if (chroot(conf.chroot) != 0 || chdir("/") != 0)
346 ae08ec7d 2021-01-25 op fatal("%s: %s", conf.chroot, strerror(errno));
349 ae08ec7d 2021-01-25 op if (pw != NULL) {
350 ae08ec7d 2021-01-25 op if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
351 ae08ec7d 2021-01-25 op fatal("setresuid(%d): %s", pw->pw_uid,
352 ae08ec7d 2021-01-25 op strerror(errno));
355 ae08ec7d 2021-01-25 op if (getuid() == 0)
356 3abf91b0 2021-02-07 op log_warn(NULL, "not a good idea to run a network daemon as root");
360 3abf91b0 2021-02-07 op usage(const char *me)
362 3abf91b0 2021-02-07 op fprintf(stderr,
363 8e8b2e25 2021-04-28 op "USAGE: %s [-fn] [-c config] [-P pidfile] | [-6h] [-d certs-dir] [-H host]\n"
364 aa372875 2021-02-10 op " [-p port] [-x cgi] [dir]\n",
369 376a5407 2021-02-23 op logger_init(void)
373 376a5407 2021-02-23 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
374 376a5407 2021-02-23 op err(1, "socketpair");
376 376a5407 2021-02-23 op switch (fork()) {
378 376a5407 2021-02-23 op err(1, "fork");
380 bc99d868 2021-03-19 op signal(SIGHUP, SIG_IGN);
382 376a5407 2021-02-23 op setproctitle("logger");
383 bc99d868 2021-03-19 op imsg_init(&logibuf, p[1]);
385 bc99d868 2021-03-19 op _exit(logger_main(p[1], &logibuf));
388 bc99d868 2021-03-19 op imsg_init(&logibuf, p[0]);
394 bc99d868 2021-03-19 op serve(int argc, char **argv, struct imsgbuf *ibuf)
396 b8e64ccd 2021-03-31 op char path[PATH_MAX];
398 b8e64ccd 2021-03-31 op struct vhost *h;
399 b8e64ccd 2021-03-31 op struct location *l;
401 2c3e53da 2021-03-03 op if (config_path == NULL) {
402 d672b8fb 2021-02-03 op if (hostname == NULL)
403 d672b8fb 2021-02-03 op hostname = "localhost";
404 d672b8fb 2021-02-03 op if (certs_dir == NULL)
405 d672b8fb 2021-02-03 op certs_dir = data_dir();
406 d672b8fb 2021-02-03 op load_local_cert(hostname, certs_dir);
408 b8e64ccd 2021-03-31 op h = TAILQ_FIRST(&hosts);
409 b8e64ccd 2021-03-31 op h->domain = "*";
411 b8e64ccd 2021-03-31 op l = TAILQ_FIRST(&h->locations);
412 b8e64ccd 2021-03-31 op l->auto_index = 1;
413 b8e64ccd 2021-03-31 op l->match = "*";
415 d672b8fb 2021-02-03 op switch (argc) {
417 fdea6aa0 2021-04-30 op l->dir = getcwd(path, sizeof(path));
420 fdea6aa0 2021-04-30 op l->dir = absolutify_path(argv[0]);
423 d672b8fb 2021-02-03 op usage(getprogname());
427 fdea6aa0 2021-04-30 op log_notice(NULL, "serving %s on port %d", l->dir, conf.port);
430 d672b8fb 2021-02-03 op /* setup tls before dropping privileges: we don't want user
431 d672b8fb 2021-02-03 op * to put private certs inside the chroot. */
434 2c3e53da 2021-03-03 op for (i = 0; i < conf.prefork; ++i) {
435 2c3e53da 2021-03-03 op if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC,
436 2c3e53da 2021-03-03 op PF_UNSPEC, p) == -1)
437 2c3e53da 2021-03-03 op fatal("socketpair: %s", strerror(errno));
439 2c3e53da 2021-03-03 op switch (fork()) {
441 2c3e53da 2021-03-03 op fatal("fork: %s", strerror(errno));
442 2c3e53da 2021-03-03 op case 0: /* child */
444 bc99d868 2021-03-19 op imsg_init(&exibuf, p[1]);
445 2c3e53da 2021-03-03 op setproctitle("server");
446 bc99d868 2021-03-19 op _exit(listener_main(&exibuf));
449 bc99d868 2021-03-19 op imsg_init(&servibuf[i], p[0]);
453 2c3e53da 2021-03-03 op setproctitle("executor");
455 bc99d868 2021-03-19 op _exit(executor_main(ibuf));
459 419a4235 2021-04-28 op write_pidfile(const char *pidfile)
461 419a4235 2021-04-28 op struct flock lock;
464 419a4235 2021-04-28 op if (pidfile == NULL)
467 419a4235 2021-04-28 op if ((fd = open(pidfile, O_WRONLY|O_CREAT|O_CLOEXEC, 0600)) == -1)
468 419a4235 2021-04-28 op fatal("can't open pidfile %s: %s", pidfile, strerror(errno));
470 419a4235 2021-04-28 op lock.l_start = 0;
471 419a4235 2021-04-28 op lock.l_len = 0;
472 419a4235 2021-04-28 op lock.l_type = F_WRLCK;
473 419a4235 2021-04-28 op lock.l_whence = SEEK_SET;
475 419a4235 2021-04-28 op if (fcntl(fd, F_SETLK, &lock) == -1)
476 419a4235 2021-04-28 op fatal("can't lock %s, gmid is already running?", pidfile);
478 419a4235 2021-04-28 op if (ftruncate(fd, 0) == -1)
479 419a4235 2021-04-28 op fatal("ftruncate: %s: %s", pidfile, strerror(errno));
481 419a4235 2021-04-28 op dprintf(fd, "%d\n", getpid());
487 b8e64ccd 2021-03-31 op setup_configless(int argc, char **argv, const char *cgi)
489 b8e64ccd 2021-03-31 op struct vhost *host;
490 b8e64ccd 2021-03-31 op struct location *loc;
492 b8e64ccd 2021-03-31 op host = xcalloc(1, sizeof(*host));
493 b8e64ccd 2021-03-31 op host->cgi = cgi;
494 b8e64ccd 2021-03-31 op TAILQ_INSERT_HEAD(&hosts, host, vhosts);
496 b8e64ccd 2021-03-31 op loc = xcalloc(1, sizeof(*loc));
497 b8e64ccd 2021-03-31 op TAILQ_INSERT_HEAD(&host->locations, loc, locations);
499 b8e64ccd 2021-03-31 op serve(argc, argv, NULL);
500 b8e64ccd 2021-03-31 op imsg_compose(&logibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
501 b8e64ccd 2021-03-31 op imsg_flush(&logibuf);
505 8d6ae384 2021-01-24 op main(int argc, char **argv)
507 d632468d 2021-03-20 op struct imsgbuf exibuf;
508 2c3e53da 2021-03-03 op int ch, conftest = 0, configless = 0;
509 8e8b2e25 2021-04-28 op int pidfd, old_ipv6, old_port;
511 501e489c 2021-01-24 op init_config();
513 8e8b2e25 2021-04-28 op while ((ch = getopt(argc, argv, "6c:d:fH:hnP:p:vx:")) != -1) {
514 3e4749f7 2020-10-02 op switch (ch) {
516 15902770 2021-01-15 op conf.ipv6 = 1;
517 8443bff7 2021-01-25 op configless = 1;
521 ca21e100 2021-02-04 op config_path = absolutify_path(optarg);
525 8443bff7 2021-01-25 op certs_dir = optarg;
526 8443bff7 2021-01-25 op configless = 1;
530 3abf91b0 2021-02-07 op conf.foreground = 1;
534 8443bff7 2021-01-25 op hostname = optarg;
535 8443bff7 2021-01-25 op configless = 1;
539 3e4749f7 2020-10-02 op usage(*argv);
543 15902770 2021-01-15 op conftest = 1;
547 8e8b2e25 2021-04-28 op pidfile = optarg;
551 15902770 2021-01-15 op conf.port = parse_portno(optarg);
552 8443bff7 2021-01-25 op configless = 1;
556 3abf91b0 2021-02-07 op conf.verbose++;
560 15902770 2021-01-15 op /* drop the starting / (if any) */
561 15902770 2021-01-15 op if (*optarg == '/')
563 b8e64ccd 2021-03-31 op cgi = optarg;
564 8443bff7 2021-01-25 op configless = 1;
568 3e4749f7 2020-10-02 op usage(*argv);
572 8443bff7 2021-01-25 op argc -= optind;
573 8443bff7 2021-01-25 op argv += optind;
575 d672b8fb 2021-02-03 op if (config_path == NULL) {
576 72bbed91 2021-01-27 op configless = 1;
577 3abf91b0 2021-02-07 op conf.foreground = 1;
578 2c3e53da 2021-03-03 op conf.prefork = 1;
579 3abf91b0 2021-02-07 op conf.verbose++;
582 d672b8fb 2021-02-03 op if (config_path != NULL && (argc > 0 || configless))
583 48b69cb2 2021-04-28 op errx(1, "can't specify options in config mode.");
585 132cae8c 2021-01-18 op if (conftest) {
586 d672b8fb 2021-02-03 op parse_conf(config_path);
587 132cae8c 2021-01-18 op puts("config OK");
591 3abf91b0 2021-02-07 op if (!conf.foreground && !configless) {
592 0170ba02 2021-01-17 op if (daemon(1, 1) == -1)
593 d278a0c3 2021-02-23 op err(1, "daemon");
596 d672b8fb 2021-02-03 op if (config_path != NULL)
597 d672b8fb 2021-02-03 op parse_conf(config_path);
599 d278a0c3 2021-02-23 op logger_init();
601 d672b8fb 2021-02-03 op sock4 = make_socket(conf.port, AF_INET);
603 d672b8fb 2021-02-03 op if (conf.ipv6)
604 d672b8fb 2021-02-03 op sock6 = make_socket(conf.port, AF_INET6);
606 3841a369 2021-04-20 op signal(SIGPIPE, SIG_IGN);
607 3841a369 2021-04-20 op signal(SIGCHLD, SIG_IGN);
609 bc99d868 2021-03-19 op if (configless) {
610 b8e64ccd 2021-03-31 op setup_configless(argc, argv, cgi);
614 8e8b2e25 2021-04-28 op pidfd = write_pidfile(pidfile);
616 b9c9123b 2021-03-20 op /* Linux seems to call the event handlers even when we're
617 1b333122 2021-04-26 op * doing a sigwait. These dummy handlers are here to avoid
618 5d1474a5 2021-04-20 op * being terminated on SIGHUP, SIGINT or SIGTERM. */
619 b9c9123b 2021-03-20 op signal(SIGHUP, dummy_handler);
620 b9c9123b 2021-03-20 op signal(SIGINT, dummy_handler);
621 b9c9123b 2021-03-20 op signal(SIGTERM, dummy_handler);
623 ca21e100 2021-02-04 op /* wait a sighup and reload the daemon */
627 bc99d868 2021-03-19 op if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC,
628 bc99d868 2021-03-19 op PF_UNSPEC, p) == -1)
629 bc99d868 2021-03-19 op fatal("socketpair: %s", strerror(errno));
631 ca21e100 2021-02-04 op switch (fork()) {
633 ca21e100 2021-02-04 op fatal("fork: %s", strerror(errno));
636 bc99d868 2021-03-19 op imsg_init(&exibuf, p[1]);
637 bc99d868 2021-03-19 op _exit(serve(argc, argv, &exibuf));
641 bc99d868 2021-03-19 op imsg_init(&exibuf, p[0]);
643 1d3eb470 2021-03-20 op if (!wait_signal())
646 3abf91b0 2021-02-07 op log_info(NULL, "reloading configuration %s", config_path);
648 bc99d868 2021-03-19 op /* close the executor (it'll close the servers too) */
649 bc99d868 2021-03-19 op imsg_compose(&exibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
650 bc99d868 2021-03-19 op imsg_flush(&exibuf);
653 ca21e100 2021-02-04 op old_ipv6 = conf.ipv6;
654 ca21e100 2021-02-04 op old_port = conf.port;
656 ca21e100 2021-02-04 op free_config();
657 ca21e100 2021-02-04 op init_config();
658 ca21e100 2021-02-04 op parse_conf(config_path);
660 ca21e100 2021-02-04 op if (old_port != conf.port) {
661 ca21e100 2021-02-04 op close(sock4);
662 ca21e100 2021-02-04 op close(sock6);
667 ca21e100 2021-02-04 op if (sock6 != -1 && old_ipv6 != conf.ipv6) {
668 ca21e100 2021-02-04 op close(sock6);
672 ca21e100 2021-02-04 op if (sock4 == -1)
673 ca21e100 2021-02-04 op sock4 = make_socket(conf.port, AF_INET);
674 ca21e100 2021-02-04 op if (sock6 == -1 && conf.ipv6)
675 ca21e100 2021-02-04 op sock6 = make_socket(conf.port, AF_INET6);
678 1d3eb470 2021-03-20 op imsg_compose(&exibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
679 1d3eb470 2021-03-20 op imsg_flush(&exibuf);
681 1d3eb470 2021-03-20 op imsg_compose(&logibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
682 1d3eb470 2021-03-20 op imsg_flush(&logibuf);
684 8e8b2e25 2021-04-28 op if (pidfd != -1)
685 8e8b2e25 2021-04-28 op close(pidfd);