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.
37 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
40 static int main_configure(struct conf *);
41 static void main_configure_done(struct conf *);
42 static void main_reload(struct conf *);
43 static void main_sig_handler(int, short, void *);
44 static int main_dispatch_server(int, struct privsep_proc *, struct imsg *);
45 static int main_dispatch_logger(int, struct privsep_proc *, struct imsg *);
46 static void __dead main_shutdown(struct conf *);
48 static struct privsep_proc procs[] = {
49 { "server", PROC_SERVER, main_dispatch_server, server },
50 { "logger", PROC_LOGGER, main_dispatch_logger, logger },
53 static const char *opts = "c:D:fI:hnP:T:Vv";
55 static const struct option longopts[] = {
56 {"help", no_argument, NULL, 'h'},
57 {"version", no_argument, NULL, 'V'},
61 struct fcgi fcgi[FCGI_MAX];
63 struct vhosthead hosts;
71 const char *config_path = "/etc/gmid.conf";
80 "Version: " GMID_STRING "\n"
81 "Usage: %s [-fnv] [-c config] [-D macro=value] [-P pidfile]\n",
85 /* used by the server process, defined here so gg can provide its own impl. */
87 log_request(struct client *c, char *meta, size_t l)
89 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
95 len = sizeof(c->addr);
96 ec = getnameinfo((struct sockaddr*)&c->addr, len,
99 NI_NUMERICHOST | NI_NUMERICSERV);
101 fatalx("getnameinfo: %s", gai_strerror(ec));
103 if (c->iri.schema != NULL) {
104 /* serialize the IRI */
105 strlcpy(b, c->iri.schema, sizeof(b));
106 strlcat(b, "://", sizeof(b));
108 /* log the decoded host name, but if it was invalid
109 * use the raw one. */
110 if (*c->domain != '\0')
111 strlcat(b, c->domain, sizeof(b));
113 strlcat(b, c->iri.host, sizeof(b));
115 if (*c->iri.path != '/')
116 strlcat(b, "/", sizeof(b));
117 strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
118 if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
119 strlcat(b, "?", sizeof(b));
120 strlcat(b, c->iri.query, sizeof(b));
123 if ((t = c->req) == NULL)
125 strlcpy(b, t, sizeof(b));
128 if ((t = memchr(meta, '\r', l)) == NULL)
131 ec = asprintf(&fmted, "%s:%s GET %s %.*s", hbuf, sbuf, b,
132 (int)(t-meta), meta);
136 proc_compose(conf.ps, PROC_LOGGER, IMSG_LOG_REQUEST,
143 write_pidfile(const char *pidfile)
151 if ((fd = open(pidfile, O_WRONLY|O_CREAT|O_CLOEXEC, 0600)) == -1)
152 fatal("can't open pidfile %s", pidfile);
156 lock.l_type = F_WRLCK;
157 lock.l_whence = SEEK_SET;
159 if (fcntl(fd, F_SETLK, &lock) == -1)
160 fatalx("can't lock %s, gmid is already running?", pidfile);
162 if (ftruncate(fd, 0) == -1)
163 fatal("ftruncate %s", pidfile);
165 dprintf(fd, "%d\n", getpid());
171 main(int argc, char **argv)
174 const char *errstr, *title = NULL;
176 int ch, conftest = 0;
177 int proc_instance = 0;
178 int proc_id = PROC_PARENT;
181 setlocale(LC_CTYPE, "");
183 /* log to stderr until daemonized */
184 log_init(1, LOG_DAEMON);
187 while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
190 config_path = absolutify_path(optarg);
193 if (cmdline_symset(optarg) == -1)
194 fatalx("could not parse macro definition: %s",
204 proc_instance = strtonum(optarg, 0, PROC_MAX_INSTANCES,
207 fatalx("invalid process instance");
213 pidfile = absolutify_path(optarg);
217 proc_id = proc_getid(procs, nitems(procs), title);
218 if (proc_id == PROC_MAX)
219 fatalx("invalid process name");
222 puts("Version: " GMID_STRING);
233 if (argc - optind != 0)
236 parse_conf(config_path);
237 if (*conf.chroot != '\0' && *conf.user == '\0')
238 fatalx("can't chroot without a user to switch to after.");
241 fprintf(stderr, "config OK\n");
247 if ((ps = calloc(1, sizeof(*ps))) == NULL)
253 fatalx("need root privileges");
254 if ((ps->ps_pw = getpwnam(conf.user)) == NULL)
255 fatalx("unknown user %s", conf.user);
258 ps->ps_instances[PROC_SERVER] = conf.prefork;
259 ps->ps_instance = proc_instance;
261 ps->ps_title[proc_id] = title;
263 if (*conf.chroot != '\0') {
264 for (i = 0; i < nitems(procs); ++i)
265 procs[i].p_chroot = conf.chroot;
268 log_init(debug, LOG_DAEMON);
269 log_setverbose(verbose);
273 /* only the parent returns */
274 proc_init(ps, procs, nitems(procs), debug, argc0, argv, proc_id);
276 log_procinit("main");
277 if (!debug && daemon(0, 0) == -1)
280 pidfd = write_pidfile(pidfile);
282 sandbox_main_process();
286 signal(SIGPIPE, SIG_IGN);
288 signal_set(&ps->ps_evsigint, SIGINT, main_sig_handler, ps);
289 signal_set(&ps->ps_evsigterm, SIGTERM, main_sig_handler, ps);
290 signal_set(&ps->ps_evsigchld, SIGCHLD, main_sig_handler, ps);
291 signal_set(&ps->ps_evsighup, SIGHUP, main_sig_handler, ps);
293 signal_add(&ps->ps_evsigint, NULL);
294 signal_add(&ps->ps_evsigterm, NULL);
295 signal_add(&ps->ps_evsigchld, NULL);
296 signal_add(&ps->ps_evsighup, NULL);
300 if (main_configure(&conf) == -1)
301 fatal("configuration failed");
304 main_shutdown(&conf);
310 main_configure(struct conf *conf)
312 struct privsep *ps = conf->ps;
314 conf->reload = conf->prefork;
316 if (proc_compose(ps, PROC_SERVER, IMSG_RECONF_START, NULL, 0) == -1)
319 if (config_send(conf, fcgi, &hosts) == -1)
322 if (proc_compose(ps, PROC_SERVER, IMSG_RECONF_END, NULL, 0) == -1)
329 main_configure_done(struct conf *conf)
331 if (conf->reload == 0) {
332 log_warnx("configuration already done");
337 /* send IMSG_CTL_START? */
341 main_reload(struct conf *conf)
344 log_debug("%s: already in progress: %d pending",
345 __func__, conf->reload);
349 log_debug("%s: config file %s", __func__, config_path);
351 parse_conf(config_path); /* XXX should handle error here */
353 main_configure(conf);
357 main_sig_handler(int sig, short ev, void *arg)
359 struct privsep *ps = arg;
362 * Normal signal handler rules don't apply here because libevent
368 if (privsep_process != PROC_PARENT)
370 log_info("reload requested with SIGHUP");
371 main_reload(ps->ps_env);
374 log_warnx("one child died, quitting");
378 main_shutdown(ps->ps_env);
381 fatalx("unexpected signal %d", sig);
386 main_dispatch_server(int fd, struct privsep_proc *p, struct imsg *imsg)
388 struct privsep *ps = p->p_ps;
389 struct conf *conf = ps->ps_env;
391 switch (imsg->hdr.type) {
392 case IMSG_RECONF_DONE:
393 main_configure_done(conf);
403 main_dispatch_logger(int fd, struct privsep_proc *p, struct imsg *imsg)
405 struct privsep *ps = p->p_ps;
406 struct conf *conf = ps->ps_env;
408 switch (imsg->hdr.type) {
409 case IMSG_RECONF_DONE:
410 main_configure_done(conf);
420 main_shutdown(struct conf *conf)
427 log_info("parent terminating, pid %d", getpid());