2 * Copyright (c) 2020, 2021, 2022 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
34 static const char *opts = "c:D:fhnP:Vv";
36 static const struct option longopts[] = {
37 {"help", no_argument, NULL, 'h'},
38 {"version", no_argument, NULL, 'V'},
42 struct fcgi fcgi[FCGI_MAX];
44 struct vhosthead hosts;
48 struct imsgbuf logibuf, servibuf[PREFORK_MAX];
50 const char *config_path = "/etc/gmid.conf";
56 dummy_handler(int signo)
62 make_socket(int port, int family)
65 struct sockaddr_in addr4;
66 struct sockaddr_in6 addr6;
67 struct sockaddr *addr;
72 memset(&addr4, 0, sizeof(addr4));
73 addr4.sin_family = family;
74 addr4.sin_port = htons(port);
75 addr4.sin_addr.s_addr = INADDR_ANY;
76 addr = (struct sockaddr*)&addr4;
81 memset(&addr6, 0, sizeof(addr6));
82 addr6.sin6_family = AF_INET6;
83 addr6.sin6_port = htons(port);
84 addr6.sin6_addr = in6addr_any;
85 addr = (struct sockaddr*)&addr6;
94 if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
98 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
99 fatal("setsockopt(SO_REUSEADDR)");
102 if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
103 fatal("setsockopt(SO_REUSEPORT)");
107 if (bind(sock, addr, len) == -1)
110 if (listen(sock, 16) == -1)
123 sigaddset(&mask, SIGHUP);
124 sigaddset(&mask, SIGINT);
125 sigaddset(&mask, SIGTERM);
126 sigwait(&mask, &signo);
128 return signo == SIGHUP;
134 struct passwd *pw = NULL;
136 if (*conf.chroot != '\0' && *conf.user == '\0')
137 fatalx("can't chroot without an user to switch to after.");
139 if (*conf.user != '\0') {
140 if ((pw = getpwnam(conf.user)) == NULL)
141 fatalx("can't find user %s", conf.user);
144 if (*conf.chroot != '\0') {
145 if (chroot(conf.chroot) != 0 || chdir("/") != 0)
146 fatal("%s", conf.chroot);
150 if (setgroups(1, &pw->pw_gid) == -1 ||
151 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) == -1 ||
152 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
153 fatal("cannot drop privileges");
158 "not a good idea to run a network daemon as root");
165 "Version: " GMID_STRING "\n"
166 "Usage: %s [-fnv] [-c config] [-D macro=value] [-P pidfile]\n",
175 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
176 err(1, "socketpair");
182 signal(SIGHUP, SIG_IGN);
184 setproctitle("logger");
185 imsg_init(&logibuf, p[1]);
187 _exit(logger_main(p[1], &logibuf));
190 imsg_init(&logibuf, p[0]);
200 for (i = 0; i < conf.prefork; ++i) {
201 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC,
210 imsg_init(&servibuf[i], p[1]);
211 setproctitle("server");
212 _exit(server_main(&servibuf[i], sock4, sock6));
215 imsg_init(&servibuf[i], p[0]);
221 write_pidfile(const char *pidfile)
229 if ((fd = open(pidfile, O_WRONLY|O_CREAT|O_CLOEXEC, 0600)) == -1)
230 fatal("can't open pidfile %s", pidfile);
234 lock.l_type = F_WRLCK;
235 lock.l_whence = SEEK_SET;
237 if (fcntl(fd, F_SETLK, &lock) == -1)
238 fatalx("can't lock %s, gmid is already running?", pidfile);
240 if (ftruncate(fd, 0) == -1)
241 fatal("ftruncate %s", pidfile);
243 dprintf(fd, "%d\n", getpid());
249 main(int argc, char **argv)
251 int i, ch, conftest = 0;
252 int pidfd, old_ipv6, old_port;
254 setlocale(LC_CTYPE, "");
259 while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
262 config_path = absolutify_path(optarg);
265 if (cmdline_symset(optarg) == -1)
266 fatalx("could not parse macro definition: %s",
282 puts("Version: " GMID_STRING);
298 parse_conf(config_path);
301 fprintf(stderr, "config OK\n");
307 if (!conf.foreground) {
309 imsg_compose(&logibuf, IMSG_LOG_TYPE, 0, 0, -1, NULL, 0);
310 imsg_flush(&logibuf);
312 if (daemon(1, 1) == -1)
316 sock4 = make_socket(conf.port, AF_INET);
319 sock6 = make_socket(conf.port, AF_INET6);
321 signal(SIGPIPE, SIG_IGN);
323 pidfd = write_pidfile(pidfile);
326 * Linux seems to call the event handlers even when we're
327 * doing a sigwait. These dummy handlers are here to avoid
328 * being terminated on SIGHUP, SIGINT or SIGTERM.
330 signal(SIGHUP, dummy_handler);
331 signal(SIGINT, dummy_handler);
332 signal(SIGTERM, dummy_handler);
334 /* wait a sighup and reload the daemon */
341 log_info(NULL, "reloading configuration %s", config_path);
343 /* close the servers */
344 for (i = 0; i < conf.prefork; ++i) {
345 imsg_compose(&servibuf[i], IMSG_QUIT, 0, 0, -1,
347 imsg_flush(&servibuf[i]);
348 close(servibuf[i].fd);
351 old_ipv6 = conf.ipv6;
352 old_port = conf.port;
356 parse_conf(config_path);
358 if (old_port != conf.port) {
365 if (sock6 != -1 && old_ipv6 != conf.ipv6) {
371 sock4 = make_socket(conf.port, AF_INET);
372 if (sock6 == -1 && conf.ipv6)
373 sock6 = make_socket(conf.port, AF_INET6);
376 for (i = 0; i < conf.prefork; ++i) {
377 imsg_compose(&servibuf[i], IMSG_QUIT, 0, 0, -1, NULL, 0);
378 imsg_flush(&servibuf[i]);
379 close(servibuf[i].fd);
382 imsg_compose(&logibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
383 imsg_flush(&logibuf);