commit af1dab18702cf135aa80bf15065f73050c915347 from: Omar Polo date: Fri Jun 09 17:18:04 2023 UTC don't have the config being a global commit - e45334e6ae0b658a2d3d4f47bc3e9ddfdb83a44f commit + af1dab18702cf135aa80bf15065f73050c915347 blob - 5c38d746a682d6e1236ba2f29690b65d0e0e0016 blob + 185bc0aea4001aef1ef15e1443722b95c8d94cbe --- config.c +++ config.c @@ -25,28 +25,32 @@ #include "log.h" #include "proc.h" -void -config_init(void) +struct conf * +config_new(void) { - memset(&conf, 0, sizeof(conf)); + struct conf *conf; - TAILQ_INIT(&conf.fcgi); - TAILQ_INIT(&conf.hosts); + conf = xcalloc(1, sizeof(*conf)); - conf.port = 1965; - conf.ipv6 = 0; - conf.protos = TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3; + TAILQ_INIT(&conf->fcgi); + TAILQ_INIT(&conf->hosts); - init_mime(&conf.mime); + conf->port = 1965; + conf->ipv6 = 0; + conf->protos = TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3; - conf.prefork = 3; + init_mime(&conf->mime); - conf.sock4 = -1; - conf.sock6 = -1; + conf->prefork = 3; + + conf->sock4 = -1; + conf->sock6 = -1; + + return conf; } void -config_free(void) +config_purge(struct conf *conf) { struct privsep *ps; struct fcgi *f, *tf; @@ -56,25 +60,25 @@ config_free(void) struct envlist *e, *te; struct alist *a, *ta; - ps = conf.ps; + ps = conf->ps; - if (conf.sock4 != -1) { - event_del(&conf.evsock4); - close(conf.sock4); + if (conf->sock4 != -1) { + event_del(&conf->evsock4); + close(conf->sock4); } - if (conf.sock6 != -1) { - event_del(&conf.evsock6); - close(conf.sock6); + if (conf->sock6 != -1) { + event_del(&conf->evsock6); + close(conf->sock6); } - free_mime(&conf.mime); - TAILQ_FOREACH_SAFE(f, &conf.fcgi, fcgi, tf) { - TAILQ_REMOVE(&conf.fcgi, f, fcgi); + free_mime(&conf->mime); + TAILQ_FOREACH_SAFE(f, &conf->fcgi, fcgi, tf) { + TAILQ_REMOVE(&conf->fcgi, f, fcgi); free(f); } - TAILQ_FOREACH_SAFE(h, &conf.hosts, vhosts, th) { + TAILQ_FOREACH_SAFE(h, &conf->hosts, vhosts, th) { free(h->cert_path); free(h->key_path); free(h->ocsp_path); @@ -114,18 +118,18 @@ config_free(void) free(p); } - TAILQ_REMOVE(&conf.hosts, h, vhosts); + TAILQ_REMOVE(&conf->hosts, h, vhosts); free(h); } - memset(&conf, 0, sizeof(conf)); + memset(conf, 0, sizeof(*conf)); - conf.ps = ps; - conf.sock4 = conf.sock6 = -1; - conf.protos = TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3; - init_mime(&conf.mime); - TAILQ_INIT(&conf.fcgi); - TAILQ_INIT(&conf.hosts); + conf->ps = ps; + conf->sock4 = conf->sock6 = -1; + conf->protos = TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3; + init_mime(&conf->mime); + TAILQ_INIT(&conf->fcgi); + TAILQ_INIT(&conf->hosts); } static int @@ -461,7 +465,7 @@ config_recv(struct conf *conf, struct imsg *imsg) switch (imsg->hdr.type) { case IMSG_RECONF_START: - config_free(); + config_purge(conf); h = NULL; p = NULL; break; @@ -494,7 +498,7 @@ config_recv(struct conf *conf, struct imsg *imsg) fatalx("missing socket for IMSG_RECONF_SOCK4"); conf->sock4 = imsg->fd; event_set(&conf->evsock4, conf->sock4, EV_READ|EV_PERSIST, - do_accept, NULL); + do_accept, conf); break; case IMSG_RECONF_SOCK6: @@ -504,7 +508,7 @@ config_recv(struct conf *conf, struct imsg *imsg) fatalx("missing socket for IMSG_RECONF_SOCK6"); conf->sock6 = imsg->fd; event_set(&conf->evsock6, conf->sock6, EV_READ|EV_PERSIST, - do_accept, NULL); + do_accept, conf); break; case IMSG_RECONF_FCGI: blob - 47ed883805ee02ede1a49148a7c24de2765bc410 blob + 36b9dd9e410f68e5a4b59ffe052cf44e14b96857 --- ge.c +++ ge.c @@ -31,7 +31,6 @@ #include "log.h" -struct conf conf; int privsep_process; static const struct option opts[] = { @@ -159,7 +158,7 @@ data_dir(void) } static int -serve(const char *host, int port, const char *dir) +serve(struct conf *conf, const char *host, int port, const char *dir) { struct addrinfo hints, *res, *res0; int r, error, saved_errno, sock = -1; @@ -210,12 +209,12 @@ serve(const char *host, int port, const char *dir) event_init(); /* cheating */ - conf.sock4 = sock; - event_set(&conf.evsock4, conf.sock4, EV_READ|EV_PERSIST, - do_accept, NULL); + conf->sock4 = sock; + event_set(&conf->evsock4, conf->sock4, EV_READ|EV_PERSIST, + do_accept, conf); server_init(NULL, NULL, NULL); - if (server_configure_done(&conf) == -1) + if (server_configure_done(conf) == -1) fatalx("server configuration failed"); log_info("serving %s on port %d", dir, port); @@ -237,6 +236,7 @@ usage(void) int main(int argc, char **argv) { + struct conf *conf; struct vhost *host; struct location *loc; const char *errstr, *certs_dir = NULL, *hostname = "localhost"; @@ -247,7 +247,7 @@ main(int argc, char **argv) log_init(1, LOG_DAEMON); log_setverbose(0); - config_init(); + conf = config_new(); while ((ch = getopt_long(argc, argv, "d:H:hp:Vv", opts, NULL)) != -1) { switch (ch) { @@ -261,7 +261,7 @@ main(int argc, char **argv) usage(); break; case 'p': - conf.port = strtonum(optarg, 0, UINT16_MAX, &errstr); + conf->port = strtonum(optarg, 0, UINT16_MAX, &errstr); if (errstr) fatalx("port number is %s: %s", errstr, optarg); @@ -281,14 +281,14 @@ main(int argc, char **argv) usage(); /* prepare the configuration */ - init_mime(&conf.mime); + init_mime(&conf->mime); if (certs_dir == NULL) certs_dir = data_dir(); /* set up the implicit vhost and location */ host = xcalloc(1, sizeof(*host)); - TAILQ_INSERT_HEAD(&conf.hosts, host, vhosts); + TAILQ_INSERT_HEAD(&conf->hosts, host, vhosts); loc = xcalloc(1, sizeof(*loc)); loc->fcgi = -1; @@ -315,5 +315,5 @@ main(int argc, char **argv) /* start the server */ signal(SIGPIPE, SIG_IGN); setproctitle("%s", loc->dir); - return serve(hostname, conf.port, loc->dir); + return serve(conf, hostname, conf->port, loc->dir); } blob - 7a7a47d59153945a0f83db89e815b90bdcf2b801 blob + 1a796d2268061ad537a0bf8be69a618ce59687c8 --- gmid.c +++ gmid.c @@ -67,8 +67,6 @@ int debug, verbose; const char *config_path = "/etc/gmid.conf"; const char *pidfile; -struct conf conf; - static void usage(void) { @@ -82,6 +80,7 @@ usage(void) void log_request(struct client *c, char *meta, size_t l) { + struct conf *conf = c->conf; char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN]; char *fmted; const char *t; @@ -129,7 +128,7 @@ log_request(struct client *c, char *meta, size_t l) if (ec == -1) err(1, "asprintf"); - proc_compose(conf.ps, PROC_LOGGER, IMSG_LOG_REQUEST, + proc_compose(conf->ps, PROC_LOGGER, IMSG_LOG_REQUEST, fmted, ec + 1); free(fmted); @@ -166,6 +165,7 @@ write_pidfile(const char *pidfile) int main(int argc, char **argv) { + struct conf *conf; struct privsep *ps; const char *errstr, *title = NULL; size_t i; @@ -178,7 +178,6 @@ main(int argc, char **argv) /* log to stderr until daemonized */ log_init(1, LOG_DAEMON); - config_init(); while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) { switch (ch) { @@ -229,8 +228,10 @@ main(int argc, char **argv) if (argc - optind != 0) usage(); - parse_conf(config_path); - if (*conf.chroot != '\0' && *conf.user == '\0') + conf = config_new(); + + parse_conf(conf, config_path); + if (*conf->chroot != '\0' && *conf->user == '\0') fatalx("can't chroot without a user to switch to after."); if (conftest) { @@ -242,23 +243,23 @@ main(int argc, char **argv) if ((ps = calloc(1, sizeof(*ps))) == NULL) fatal("calloc"); - ps->ps_env = &conf; - conf.ps = ps; - if (*conf.user) { + ps->ps_env = conf; + conf->ps = ps; + if (*conf->user) { if (geteuid()) fatalx("need root privileges"); - if ((ps->ps_pw = getpwnam(conf.user)) == NULL) - fatalx("unknown user %s", conf.user); + if ((ps->ps_pw = getpwnam(conf->user)) == NULL) + fatalx("unknown user %s", conf->user); } - ps->ps_instances[PROC_SERVER] = conf.prefork; + ps->ps_instances[PROC_SERVER] = conf->prefork; ps->ps_instance = proc_instance; if (title != NULL) ps->ps_title[proc_id] = title; - if (*conf.chroot != '\0') { + if (*conf->chroot != '\0') { for (i = 0; i < nitems(procs); ++i) - procs[i].p_chroot = conf.chroot; + procs[i].p_chroot = conf->chroot; } log_init(debug, LOG_DAEMON); @@ -293,11 +294,11 @@ main(int argc, char **argv) proc_connect(ps); - if (main_configure(&conf) == -1) + if (main_configure(conf) == -1) fatal("configuration failed"); event_dispatch(); - main_shutdown(&conf); + main_shutdown(conf); /* NOTREACHED */ return 0; } @@ -343,8 +344,8 @@ main_reload(struct conf *conf) } log_debug("%s: config file %s", __func__, config_path); - config_free(); - parse_conf(config_path); /* XXX should handle error here */ + config_purge(conf); + parse_conf(conf, config_path); /* XXX should handle error here */ main_configure(conf); } @@ -416,7 +417,7 @@ static void __dead main_shutdown(struct conf *conf) { proc_kill(conf->ps); - config_free(); + config_purge(conf); free(conf->ps); /* free(conf); */ blob - 20e20561ad8897f10d46b36c5dc54841afa437e0 blob + 0e99e135a60502c3ccdf154fde2eb2c05e1d9e52 --- gmid.h +++ gmid.h @@ -230,7 +230,6 @@ struct conf { }; extern const char *config_path; -extern struct conf conf; extern int servpipes[PROC_MAX_INSTANCES]; extern int privsep_process; @@ -247,6 +246,7 @@ enum { }; struct client { + struct conf *conf; uint32_t id; struct tls *ctx; char *req; @@ -339,14 +339,14 @@ void load_local_cert(struct vhost*, const char*, con void log_request(struct client *, char *, size_t); /* config.c */ -void config_init(void); -void config_free(void); +struct conf *config_new(void); +void config_purge(struct conf *); int config_send(struct conf *); int config_recv(struct conf *, struct imsg *); /* parse.y */ void yyerror(const char*, ...); -void parse_conf(const char*); +void parse_conf(struct conf *, const char*); void print_conf(void); int cmdline_symset(char *); @@ -355,7 +355,7 @@ void init_mime(struct mime*); int add_mime(struct mime*, const char*, const char*); int load_default_mime(struct mime*); void sort_mime(struct mime *); -const char *mime(struct vhost*, const char*); +const char *mime(struct conf *, struct vhost*, const char*); void free_mime(struct mime *); /* server.c */ blob - 9197c8b131e1e9c670c9d7bf13635eaa8b9b1919 blob + 6176c2a11cae4e0677a0fd92707df08778b42fd0 --- mime.c +++ mime.c @@ -136,7 +136,7 @@ mime_find(const void *a, const void *b) } const char * -mime(struct vhost *host, const char *path) +mime(struct conf *conf, struct vhost *host, const char *path) { const char *def, *ext; struct etm *t; @@ -146,7 +146,7 @@ mime(struct vhost *host, const char *path) if ((ext = path_ext(path)) == NULL) return def; - t = bsearch(ext, conf.mime.t, conf.mime.len, sizeof(*conf.mime.t), + t = bsearch(ext, conf->mime.t, conf->mime.len, sizeof(*conf->mime.t), mime_find); if (t != NULL) return t->mime; blob - d4ac93a011e898d39a759ea126c85333b8b9368d blob + de0405b9c92b72911abcfbe7bf85806d153eb714 --- parse.y +++ parse.y @@ -35,6 +35,8 @@ #include "log.h" +struct conf *conf; + TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files); static struct file { TAILQ_ENTRY(file) entry; @@ -214,22 +216,22 @@ varset : STRING '=' string { ; option : CHROOT string { - if (strlcpy(conf.chroot, $2, sizeof(conf.chroot)) >= - sizeof(conf.chroot)) + if (strlcpy(conf->chroot, $2, sizeof(conf->chroot)) >= + sizeof(conf->chroot)) yyerror("chroot path too long"); free($2); } - | IPV6 bool { conf.ipv6 = $2; } - | PORT NUM { conf.port = check_port_num($2); } - | PREFORK NUM { conf.prefork = check_prefork_num($2); } + | IPV6 bool { conf->ipv6 = $2; } + | PORT NUM { conf->port = check_port_num($2); } + | PREFORK NUM { conf->prefork = check_prefork_num($2); } | PROTOCOLS string { - if (tls_config_parse_protocols(&conf.protos, $2) == -1) + if (tls_config_parse_protocols(&conf->protos, $2) == -1) yyerror("invalid protocols string \"%s\"", $2); free($2); } | USER string { - if (strlcpy(conf.user, $2, sizeof(conf.user)) >= - sizeof(conf.user)) + if (strlcpy(conf->user, $2, sizeof(conf->user)) >= + sizeof(conf->user)) yyerror("user name too long"); free($2); } @@ -237,7 +239,7 @@ option : CHROOT string { vhost : SERVER string { host = new_vhost(); - TAILQ_INSERT_HEAD(&conf.hosts, host, vhosts); + TAILQ_INSERT_HEAD(&conf->hosts, host, vhosts); loc = new_location(); TAILQ_INSERT_HEAD(&host->locations, loc, locations); @@ -473,7 +475,7 @@ medianames_l : medianames_l medianamesl ; medianamesl : numberstring { - if (add_mime(&conf.mime, current_media, $1) == -1) + if (add_mime(&conf->mime, current_media, $1) == -1) err(1, "add_mime"); free($1); } @@ -907,9 +909,11 @@ popfile(void) } void -parse_conf(const char *filename) +parse_conf(struct conf *c, const char *filename) { struct sym *sym, *next; + + conf = c; file = pushfile(filename, 0); if (file == NULL) @@ -943,17 +947,17 @@ print_conf(void) /* struct envlist *e; */ /* struct alist *a; */ - if (*conf.chroot != '\0') - printf("chroot \"%s\"\n", conf.chroot); - printf("ipv6 %s\n", conf.ipv6 ? "on" : "off"); + if (*conf->chroot != '\0') + printf("chroot \"%s\"\n", conf->chroot); + printf("ipv6 %s\n", conf->ipv6 ? "on" : "off"); /* XXX: defined mimes? */ - printf("port %d\n", conf.port); - printf("prefork %d\n", conf.prefork); + printf("port %d\n", conf->port); + printf("prefork %d\n", conf->prefork); /* XXX: protocols? */ - if (*conf.user != '\0') - printf("user \"%s\"\n", conf.user); + if (*conf->user != '\0') + printf("user \"%s\"\n", conf->user); - TAILQ_FOREACH(h, &conf.hosts, vhosts) { + TAILQ_FOREACH(h, &conf->hosts, vhosts) { printf("\nserver \"%s\" {\n", h->domain); printf(" cert \"%s\"\n", h->cert); printf(" key \"%s\"\n", h->key); @@ -1126,7 +1130,7 @@ fastcgi_conf(const char *path, const char *port) struct fcgi *f; int i = 0; - TAILQ_FOREACH(f, &conf.fcgi, fcgi) { + TAILQ_FOREACH(f, &conf->fcgi, fcgi) { if (!strcmp(f->path, path) && ((port == NULL && *f->port == '\0') || !strcmp(f->port, port))) @@ -1139,7 +1143,7 @@ fastcgi_conf(const char *path, const char *port) (void)strlcpy(f->path, path, sizeof(f->path)); if (port != NULL) (void)strlcpy(f->port, port, sizeof(f->port)); - TAILQ_INSERT_TAIL(&conf.fcgi, f, fcgi); + TAILQ_INSERT_TAIL(&conf->fcgi, f, fcgi); return f->id; } blob - f3dfbd2ea5fd93fedc7ea006d82cbcf592de9fcb blob + df7253b8a7c6a656bc519e4118cb726ce0821cab --- regress/puny-test.c +++ regress/puny-test.c @@ -19,9 +19,6 @@ #include "../gmid.h" -/* to make the linker happy */ -struct conf conf; - const struct suite { const char *src; const char *res; blob - dd7dcc875e069678853006010393e58f52d7e815 blob + 6ae720a4a1e6d36b707e495b7e0acc3f43cacabf --- server.c +++ server.c @@ -356,7 +356,7 @@ open_file(struct client *c) switch (check_path(c, c->iri.path, &c->pfd)) { case FILE_EXISTS: c->type = REQUEST_FILE; - start_reply(c, SUCCESS, mime(c->host, c->iri.path)); + start_reply(c, SUCCESS, mime(c->conf, c->host, c->iri.path)); return; case FILE_DIRECTORY: @@ -388,6 +388,7 @@ static void handle_handshake(int fd, short ev, void *d) { struct client *c = d; + struct conf *conf = c->conf; struct vhost *h; struct alist *a; const char *servname; @@ -433,7 +434,7 @@ handle_handshake(int fd, short ev, void *d) goto err; } - TAILQ_FOREACH(h, &conf.hosts, vhosts) { + TAILQ_FOREACH(h, &conf->hosts, vhosts) { if (matches(h->domain, c->domain)) goto found; TAILQ_FOREACH(a, &h->aliases, aliases) { @@ -478,6 +479,7 @@ strip_path(const char *path, int strip) static void fmt_sbuf(const char *fmt, struct client *c, const char *path) { + struct conf *conf = c->conf; size_t i; char buf[32]; @@ -507,7 +509,7 @@ fmt_sbuf(const char *fmt, struct client *c, const char strlcat(c->sbuf, c->iri.query, sizeof(c->sbuf)); break; case 'P': - snprintf(buf, sizeof(buf), "%d", conf.port); + snprintf(buf, sizeof(buf), "%d", conf->port); strlcat(c->sbuf, buf, sizeof(c->sbuf)); memset(buf, 0, sizeof(buf)); break; @@ -740,7 +742,7 @@ apply_fastcgi(struct client *c) if ((id = vhost_fastcgi(c->host, c->iri.path)) == -1) return 0; - TAILQ_FOREACH(f, &conf.fcgi, fcgi) { + TAILQ_FOREACH(f, &c->conf->fcgi, fcgi) { if (i == id) break; ++i; @@ -828,7 +830,7 @@ open_dir(struct client *c) switch (check_path(c, c->iri.path, &c->pfd)) { case FILE_EXISTS: c->type = REQUEST_FILE; - start_reply(c, SUCCESS, mime(c->host, c->iri.path)); + start_reply(c, SUCCESS, mime(c->conf, c->host, c->iri.path)); break; case FILE_DIRECTORY: @@ -1302,6 +1304,7 @@ client_close(struct client *c) void do_accept(int sock, short et, void *d) { + struct conf *conf = d; struct client *c; struct sockaddr_storage addr; struct sockaddr *saddr; @@ -1320,6 +1323,7 @@ do_accept(int sock, short et, void *d) mark_nonblock(fd); c = xcalloc(1, sizeof(*c)); + c->conf = conf; c->id = ++server_client_id; c->fd = fd; c->pfd = -1; @@ -1369,7 +1373,7 @@ add_keypair(struct vhost *h, struct tls_config *conf) } static void -setup_tls(void) +setup_tls(struct conf *conf) { struct tls_config *tlsconf; struct vhost *h; @@ -1386,11 +1390,11 @@ setup_tls(void) tls_config_verify_client_optional(tlsconf); tls_config_insecure_noverifycert(tlsconf); - if (tls_config_set_protocols(tlsconf, conf.protos) == -1) + if (tls_config_set_protocols(tlsconf, conf->protos) == -1) fatalx("tls_config_set_protocols: %s", tls_config_error(tlsconf)); - h = TAILQ_FIRST(&conf.hosts); + h = TAILQ_FIRST(&conf->hosts); /* we need to set something, then we can add how many key we want */ if (tls_config_set_keypair_mem(tlsconf, h->cert, h->certlen, @@ -1416,12 +1420,12 @@ setup_tls(void) } static void -load_vhosts(void) +load_vhosts(struct conf *conf) { struct vhost *h; struct location *l; - TAILQ_FOREACH(h, &conf.hosts, vhosts) { + TAILQ_FOREACH(h, &conf->hosts, vhosts) { TAILQ_FOREACH(l, &h->locations, locations) { if (*l->dir == '\0') continue; @@ -1461,8 +1465,8 @@ server_configure_done(struct conf *conf) if (load_default_mime(&conf->mime) == -1) fatal("can't load default mime"); sort_mime(&conf->mime); - setup_tls(); - load_vhosts(); + setup_tls(conf); + load_vhosts(conf); if (conf->sock4 != -1) event_add(&conf->evsock4, NULL); if (conf->sock6 != -1)