From 0c918c095d8f611f8d0072db468e37683597ef01 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 6 May 2013 22:35:06 +0000 Subject: favor "struct mog_fd" for acceptors over int FDs There's no reason to be referencing FDs for these acceptors since they're infrequently accessed by svc, so this should make our internals more consistent. This also removes our use of mog_fd_get (outside of test code). --- accept.c | 10 +++++----- bind_listen.c | 23 +++-------------------- cfg.c | 19 +++++++++++-------- cmogstored.c | 13 ++++++------- cmogstored.h | 13 +++++++------ exit.c | 15 +++++++-------- mgmt_parser.rl | 2 +- svc.c | 13 ++++++------- svc_dev.c | 2 +- upgrade.c | 12 ++++++------ 10 files changed, 53 insertions(+), 69 deletions(-) diff --git a/accept.c b/accept.c index 6148279..3415e47 100644 --- a/accept.c +++ b/accept.c @@ -4,16 +4,16 @@ */ #include "cmogstored.h" -struct mog_accept * -mog_accept_init(int fd, struct mog_svc *svc, mog_post_accept_fn fn) +struct mog_fd * +mog_accept_init(int fd, struct mog_svc *svc, + struct mog_addrinfo *a, mog_post_accept_fn fn) { - struct mog_fd *mfd = mog_fd_get(fd); + struct mog_fd *mfd = mog_fd_init(fd, MOG_FD_TYPE_ACCEPT); struct mog_accept *ac = &mfd->as.accept; - mfd->fd = fd; ac->post_accept_fn = fn; ac->svc = svc; memset(&ac->thrpool, 0, sizeof(struct mog_thrpool)); - return ac; + return mfd; } diff --git a/bind_listen.c b/bind_listen.c index 104f6ee..e1f0168 100644 --- a/bind_listen.c +++ b/bind_listen.c @@ -4,18 +4,6 @@ */ #include "cmogstored.h" -/* Under FreeBSD, TCP_NOPUSH is inherited by accepted sockets */ -static int tcp_nopush_prepare(int fd) -{ - socklen_t len = (socklen_t)sizeof(int); - int val = 1; - - if (MOG_TCP_NOPUSH == 0) - return 0; - - return setsockopt(fd, IPPROTO_TCP, MOG_TCP_NOPUSH, &val, len); -} - /* * TODO * - configurable socket buffer sizes (where to put config?) @@ -27,7 +15,7 @@ static int tcp_nopush_prepare(int fd) * http://labs.apnic.net/blabs/?p=57 */ -static int set_tcp_opts(int fd, const char *accept_filter) +static int set_tcp_opts(int fd) { int val; socklen_t len = sizeof(int); @@ -45,15 +33,10 @@ static int set_tcp_opts(int fd, const char *accept_filter) rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, len); if (rc < 0) return rc; - if (accept_filter) { - if (strcmp(accept_filter, "httpready") == 0) - rc = tcp_nopush_prepare(fd); - } - return rc; } -int mog_bind_listen(struct addrinfo *r, const char *accept_filter) +int mog_bind_listen(struct addrinfo *r) { /* see if we inherited the socket, first */ int fd = mog_inherit_get(r->ai_addr, r->ai_addrlen); @@ -73,7 +56,7 @@ int mog_bind_listen(struct addrinfo *r, const char *accept_filter) * everywhere yet (in 2012). */ if (mog_set_cloexec(fd, true) == 0 && - set_tcp_opts(fd, accept_filter) == 0 && + set_tcp_opts(fd) == 0 && bind(fd, r->ai_addr, r->ai_addrlen) == 0 && listen(fd, 1024) == 0) break; diff --git a/cfg.c b/cfg.c index 428729f..c689aa3 100644 --- a/cfg.c +++ b/cfg.c @@ -181,15 +181,17 @@ void mog_cfg_validate_or_die(struct mog_cfg *cli) mog_set_maxconns(cli->maxconns); } -static int bind_or_die(struct mog_addrinfo *a, const char *accept_filter) +static struct mog_fd * +bind_or_die(struct mog_addrinfo *a, struct mog_svc *svc, mog_post_accept_fn fn) { int fd; - if (a == NULL) return -1; - fd = mog_bind_listen(a->addr, accept_filter); - if (fd >= 0) return fd; + if (a == NULL) return NULL; + fd = mog_bind_listen(a->addr); + if (fd < 0) + die_errno("addr=%s failed to bind+listen", a->orig); - die_errno("addr=%s failed to bind+listen", a->orig); + return mog_accept_init(fd, svc, a, fn); } static bool svc_from_cfg(void *cfg_ptr, void *ignored) @@ -202,13 +204,14 @@ static bool svc_from_cfg(void *cfg_ptr, void *ignored) if (!svc) die("failed to load svc from docroot=%s", cfg->docroot); - svc->mgmt_fd = bind_or_die(cfg->mgmtlisten, "dataready"); + svc->mgmt_mfd = bind_or_die(cfg->mgmtlisten, svc, mog_mgmt_post_accept); if (cfg->server && strcmp(cfg->server, "none") == 0) return true; - svc->http_fd = bind_or_die(cfg->httplisten, "httpready"); - svc->httpget_fd = bind_or_die(cfg->httpgetlisten, "httpready"); + svc->http_mfd = bind_or_die(cfg->httplisten, svc, mog_http_post_accept); + svc->httpget_mfd = bind_or_die(cfg->httpgetlisten, svc, + mog_httpget_post_accept); return true; } diff --git a/cmogstored.c b/cmogstored.c index 35b08f9..1c2f7db 100644 --- a/cmogstored.c +++ b/cmogstored.c @@ -287,9 +287,9 @@ static bool svc_start_each(void *svcptr, void *qptr) svc->queue = q; - if (svc->mgmt_fd >= 0) { + if (svc->mgmt_mfd) { have_mgmt = true; - ac = mog_accept_init(svc->mgmt_fd, svc, mog_mgmt_post_accept); + ac = &svc->mgmt_mfd->as.accept; /* * mgmt port is rarely used and always persistent, so it @@ -298,14 +298,13 @@ static bool svc_start_each(void *svcptr, void *qptr) mog_thrpool_start(&ac->thrpool, 1, mog_accept_loop, ac); } - if (svc->http_fd >= 0) { - ac = mog_accept_init(svc->http_fd, svc, mog_http_post_accept); + if (svc->http_mfd) { + ac = &svc->http_mfd->as.accept; mog_thrpool_start(&ac->thrpool, athr, mog_accept_loop, ac); } - if (svc->httpget_fd >= 0) { - ac = mog_accept_init(svc->httpget_fd, svc, - mog_httpget_post_accept); + if (svc->httpget_mfd) { + ac = &svc->httpget_mfd->as.accept; mog_thrpool_start(&ac->thrpool, athr, mog_accept_loop, ac); } diff --git a/cmogstored.h b/cmogstored.h index 62e9136..c958a87 100644 --- a/cmogstored.h +++ b/cmogstored.h @@ -146,9 +146,9 @@ struct mog_svc { LIST_HEAD(mgmt_head, mog_mgmt) devstats_subscribers; mode_t put_perms; mode_t mkcol_perms; - int http_fd; - int httpget_fd; - int mgmt_fd; + struct mog_fd *http_mfd; + struct mog_fd *httpget_mfd; + struct mog_fd *mgmt_mfd; uint32_t idle_timeout; }; @@ -224,10 +224,11 @@ typedef void (*mog_post_accept_fn)(int fd, struct mog_svc *, struct mog_accept { struct mog_svc *svc; mog_post_accept_fn post_accept_fn; + struct mog_addrinfo *addrinfo; /* shared with cfg */ struct mog_thrpool thrpool; }; -struct mog_accept *mog_accept_init(int fd, struct mog_svc *, - mog_post_accept_fn); +struct mog_fd *mog_accept_init(int fd, struct mog_svc *, + struct mog_addrinfo *, mog_post_accept_fn); void * mog_accept_loop(void *ac); struct mog_digest { @@ -411,7 +412,7 @@ struct mog_addrinfo { void mog_addrinfo_free(struct mog_addrinfo **); /* bind_listen.c */ -int mog_bind_listen(struct addrinfo *, const char *accept_filter); +int mog_bind_listen(struct addrinfo *); /* close.c */ void mog_close(int fd); diff --git a/exit.c b/exit.c index 5f2a0a8..719d1bd 100644 --- a/exit.c +++ b/exit.c @@ -5,17 +5,16 @@ #include "cmogstored.h" #include "nostd/setproctitle.h" -static void acceptor_quit(int *fdp) +static void acceptor_quit(struct mog_fd **mfdp) { - int fd = *fdp; + struct mog_fd *mfd = *mfdp; - if (fd >= 0) { - struct mog_fd *mfd = mog_fd_get(fd); + if (mfd) { struct mog_accept *ac = &mfd->as.accept; mog_thrpool_quit(&ac->thrpool, NULL); mog_fd_put(mfd); - *fdp = -1; + *mfdp = NULL; } } @@ -23,9 +22,9 @@ static bool svc_quit_accept_i(void *svcptr, void *ignored) { struct mog_svc *svc = svcptr; - acceptor_quit(&svc->mgmt_fd); - acceptor_quit(&svc->http_fd); - acceptor_quit(&svc->httpget_fd); + acceptor_quit(&svc->mgmt_mfd); + acceptor_quit(&svc->http_mfd); + acceptor_quit(&svc->httpget_mfd); return true; } diff --git a/mgmt_parser.rl b/mgmt_parser.rl index e183cc3..ead5d8c 100644 --- a/mgmt_parser.rl +++ b/mgmt_parser.rl @@ -11,7 +11,7 @@ */ static void set_prio_fsck(struct mog_mgmt *mgmt) { - if (mgmt->svc->mgmt_fd >= 0) + if (mgmt->svc->mgmt_mfd) mgmt->prio = MOG_PRIO_FSCK; } diff --git a/svc.c b/svc.c index 1b5a812..1bf0d45 100644 --- a/svc.c +++ b/svc.c @@ -86,7 +86,6 @@ struct mog_svc * mog_svc_new(const char *docroot) svc_once(); svc = xzalloc(sizeof(struct mog_svc)); - svc->http_fd = svc->httpget_fd = svc->mgmt_fd = -1; svc->docroot = docroot; svc->docroot_fd = fd; svc->dir = dir; @@ -119,10 +118,10 @@ size_t mog_svc_each(Hash_processor processor, void *data) return rv; } -static bool cloexec_disable(int fd) +static bool cloexec_disable(struct mog_fd *mfd) { - if (fd >= 0) - CHECK(int, 0, mog_set_cloexec(fd, false)); + if (mfd) + CHECK(int, 0, mog_set_cloexec(mfd->fd, false)); return true; } @@ -130,9 +129,9 @@ static bool svc_cloexec_off_i(void *svcptr, void *unused) { struct mog_svc *svc = svcptr; - return (cloexec_disable(svc->mgmt_fd) - && cloexec_disable(svc->http_fd) - && cloexec_disable(svc->httpget_fd)); + return (cloexec_disable(svc->mgmt_mfd) + && cloexec_disable(svc->http_mfd) + && cloexec_disable(svc->httpget_mfd)); } /* diff --git a/svc_dev.c b/svc_dev.c index 497d1bb..47a67da 100644 --- a/svc_dev.c +++ b/svc_dev.c @@ -108,7 +108,7 @@ static int svc_scandev(struct mog_svc *svc, size_t *nr, mog_scandev_cb cb) struct dirent *ent; int rc = 0; - if (svc->mgmt_fd < 0) + if (svc->mgmt_mfd == NULL) return 0; CHECK(int, 0, pthread_mutex_lock(&svc->devstats_lock)); diff --git a/upgrade.c b/upgrade.c index 1c69dee..15772ab 100644 --- a/upgrade.c +++ b/upgrade.c @@ -58,16 +58,16 @@ void mog_upgrade_prepare(int argc, char *argv[], char *envp[]) } /* writes one comma-delimited fd to fp */ -static bool emit_fd(FILE *fp, int fd) +static bool emit_fd(FILE *fp, struct mog_fd *mfd) { int r; /* no error, just the FD isn't used */ - if (fd < 0) + if (mfd == NULL) return true; errno = 0; - r = fprintf(fp, "%d,", fd); + r = fprintf(fp, "%d,", mfd->fd); if (r > 0) return true; if (errno == 0) @@ -81,9 +81,9 @@ static bool svc_emit_fd_i(void *svcptr, void *_fp) FILE *fp = _fp; struct mog_svc *svc = svcptr; - return (emit_fd(fp, svc->mgmt_fd) - && emit_fd(fp, svc->http_fd) - && emit_fd(fp, svc->httpget_fd)); + return (emit_fd(fp, svc->mgmt_mfd) + && emit_fd(fp, svc->http_mfd) + && emit_fd(fp, svc->httpget_mfd)); } /* returns the PID of the newly spawned child */ -- cgit v1.2.3-24-ge0c7