2 * Copyright (c) 2016, 2019, 2020-2021 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2015 Reyk Floeter <reyk@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/param.h>
19 #include <sys/queue.h>
20 #include <sys/socket.h>
24 #include <netinet/in.h>
42 #include "got_opentemp.h"
43 #include "got_reference.h"
47 __dead void usage(void);
49 int main(int, char **);
50 int gotwebd_configure(struct gotwebd *);
51 void gotwebd_configure_done(struct gotwebd *);
52 void gotwebd_sighdlr(int sig, short event, void *arg);
53 void gotwebd_shutdown(void);
54 void gotwebd_dispatch_sockets(int, short, void *);
56 struct gotwebd *gotwebd_env;
59 imsg_event_add(struct imsgev *iev)
61 if (iev->handler == NULL) {
62 imsg_flush(&iev->ibuf);
66 iev->events = EV_READ;
67 if (iev->ibuf.w.queued)
68 iev->events |= EV_WRITE;
71 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
72 event_add(&iev->ev, NULL);
76 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
77 pid_t pid, int fd, const void *data, uint16_t datalen)
81 ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data, datalen);
89 main_compose_sockets(struct gotwebd *env, uint32_t type, int fd,
90 const void *data, uint16_t len)
95 for (i = 0; i < env->nserver; ++i) {
97 if (fd != -1 && (d = dup(fd)) == -1)
100 ret = imsg_compose_event(&env->iev_server[i], type, 0, -1,
105 /* prevent fd exhaustion */
108 ret = imsg_flush(&env->iev_server[i].ibuf);
109 } while (ret == -1 && errno == EAGAIN);
112 imsg_event_add(&env->iev_server[i]);
123 sockets_compose_main(struct gotwebd *env, uint32_t type, const void *d,
126 return (imsg_compose_event(env->iev_parent, type, 0, -1, -1, d, len));
130 gotwebd_dispatch_sockets(int fd, short event, void *arg)
132 struct imsgev *iev = arg;
133 struct imsgbuf *ibuf;
135 struct gotwebd *env = gotwebd_env;
141 if (event & EV_READ) {
142 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
143 fatal("imsg_read error");
144 if (n == 0) /* Connection closed */
147 if (event & EV_WRITE) {
148 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
149 fatal("msgbuf_write");
150 if (n == 0) /* Connection closed */
155 if ((n = imsg_get(ibuf, &imsg)) == -1)
157 if (n == 0) /* No more messages. */
160 switch (imsg.hdr.type) {
162 gotwebd_configure_done(env);
165 fatalx("%s: unknown imsg type %d", __func__,
175 /* This pipe is dead. Remove its event handler */
177 event_loopexit(NULL);
182 gotwebd_sighdlr(int sig, short event, void *arg)
184 /* struct privsep *ps = arg; */
188 log_info("%s: ignoring SIGHUP", __func__);
191 log_info("%s: ignoring SIGPIPE", __func__);
194 log_info("%s: ignoring SIGUSR1", __func__);
201 fatalx("unexpected signal");
206 spawn_socket_process(struct gotwebd *env, const char *argv0, int n)
213 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
216 switch (pid = fork()) {
221 default: /* parent */
223 imsg_init(&env->iev_server[n].ibuf, p[1]);
224 env->iev_server[n].handler = gotwebd_dispatch_sockets;
225 env->iev_server[n].data = &env->iev_server[n];
226 event_set(&env->iev_server[n].ev, p[1], EV_READ,
227 gotwebd_dispatch_sockets, &env->iev_server[n]);
228 event_add(&env->iev_server[n].ev, NULL);
234 argv[argc++] = argv0;
236 if (env->gotwebd_debug)
238 if (env->gotwebd_verbose)
243 if (dup2(p[0], 3) == -1)
245 } else if (fcntl(p[0], F_SETFD, 0) == -1)
249 execvp(argv0, (char * const *)argv);
250 fatal("execvp %s", argv0);
256 fprintf(stderr, "usage: %s [-dnv] [-D macro=value] [-f file]\n",
262 main(int argc, char **argv)
264 struct event sigint, sigterm, sighup, sigpipe, sigusr1;
270 const char *conffile = GOTWEBD_CONF;
273 if ((argv0 = argv[0]) == NULL)
276 env = calloc(1, sizeof(*env));
278 fatal("%s: calloc", __func__);
281 /* log to stderr until daemonized */
282 log_init(1, LOG_DAEMON);
284 while ((ch = getopt(argc, argv, "D:df:nSv")) != -1) {
287 if (cmdline_symset(optarg) < 0)
288 log_warnx("could not parse macro definition %s",
292 env->gotwebd_debug = 1;
304 env->gotwebd_verbose++;
316 env->gotwebd_conffile = conffile;
318 if (parse_config(env->gotwebd_conffile, env) == -1)
322 fprintf(stderr, "configuration OK\n");
326 /* check for root privileges */
328 fatalx("need root privileges");
330 pw = getpwnam(GOTWEBD_USER);
332 fatalx("unknown user %s", GOTWEBD_USER);
335 log_init(env->gotwebd_debug, LOG_DAEMON);
336 log_setverbose(env->gotwebd_verbose);
339 setproctitle("sockets");
340 log_procinit("sockets");
342 if (chroot(pw->pw_dir) == -1)
343 fatal("chroot %s", pw->pw_dir);
344 if (chdir("/") == -1)
346 if (setgroups(1, &pw->pw_gid) == -1 ||
347 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) == -1 ||
348 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
349 fatal("failed to drop privileges");
355 if (!env->gotwebd_debug && daemon(0, 0) == -1)
360 env->nserver = env->prefork_gotwebd;
361 env->iev_server = calloc(env->nserver, sizeof(*env->iev_server));
362 if (env->iev_server == NULL)
365 for (i = 0; i < env->nserver; ++i) {
366 if (spawn_socket_process(env, argv0, i) == -1)
367 fatal("spawn_socket_process");
370 log_procinit("gotwebd");
372 log_info("%s startup", getprogname());
374 signal_set(&sigint, SIGINT, gotwebd_sighdlr, env);
375 signal_set(&sigterm, SIGTERM, gotwebd_sighdlr, env);
376 signal_set(&sighup, SIGHUP, gotwebd_sighdlr, env);
377 signal_set(&sigpipe, SIGPIPE, gotwebd_sighdlr, env);
378 signal_set(&sigusr1, SIGUSR1, gotwebd_sighdlr, env);
380 signal_add(&sigint, NULL);
381 signal_add(&sigterm, NULL);
382 signal_add(&sighup, NULL);
383 signal_add(&sigpipe, NULL);
384 signal_add(&sigusr1, NULL);
386 if (gotwebd_configure(env) == -1)
387 fatalx("configuration failed");
390 if (unveil("gmon.out", "rwc") != 0)
394 if (unveil(env->httpd_chroot, "r") == -1)
397 if (unveil(GOTWEBD_CONF, "r") == -1)
400 if (unveil(NULL, NULL) != 0)
404 if (pledge("stdio", NULL) == -1)
410 log_debug("%s gotwebd exiting", getprogname());
416 gotwebd_configure(struct gotwebd *env)
421 /* gotweb need to reload its config. */
422 env->gotwebd_reload = env->prefork_gotwebd;
424 /* send our gotweb servers */
425 TAILQ_FOREACH(srv, &env->servers, entry) {
426 if (config_setserver(env, srv) == -1)
427 fatalx("%s: send server error", __func__);
430 /* send our sockets */
431 TAILQ_FOREACH(sock, &env->sockets, entry) {
432 if (config_setsock(env, sock) == -1)
433 fatalx("%s: send socket error", __func__);
434 if (config_setfd(env, sock) == -1)
435 fatalx("%s: send priv_fd error", __func__);
438 if (main_compose_sockets(env, IMSG_CFG_DONE, -1, NULL, 0) == -1)
439 fatal("main_compose_sockets IMSG_CFG_DONE");
445 gotwebd_configure_done(struct gotwebd *env)
447 if (env->gotwebd_reload == 0) {
448 log_warnx("%s: configuration already finished", __func__);
452 env->gotwebd_reload--;
453 if (env->gotwebd_reload == 0 &&
454 main_compose_sockets(env, IMSG_CTL_START, -1, NULL, 0) == -1)
455 fatal("main_compose_sockets IMSG_CTL_START");
459 gotwebd_shutdown(void)
461 struct gotwebd *env = gotwebd_env;
465 for (i = 0; i < env->nserver; ++i) {
466 event_del(&env->iev_server[i].ev);
467 imsg_clear(&env->iev_server[i].ibuf);
468 close(env->iev_server[i].ibuf.fd);
469 env->iev_server[i].ibuf.fd = -1;
473 pid = waitpid(WAIT_ANY, &status, 0);
477 if (WIFSIGNALED(status))
478 log_warnx("lost child: pid %u terminated; signal %d",
479 pid, WTERMSIG(status));
480 else if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
481 log_warnx("lost child: pid %u exited abnormally",
483 } while (pid != -1 || (pid == -1 && errno == EINTR));
487 log_warnx("gotwebd terminating");