From ae6f563623c765e574bd9a211be7524f7495ea28 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 9 Jun 2020 14:22:44 +0100 Subject: [Project] Preliminary usage of the reuseport --- src/libserver/worker_util.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'src/libserver') diff --git a/src/libserver/worker_util.c b/src/libserver/worker_util.c index 639fc9480..d5e603cfa 100644 --- a/src/libserver/worker_util.c +++ b/src/libserver/worker_util.c @@ -933,6 +933,36 @@ rspamd_main_heartbeat_start (struct rspamd_worker *wrk, struct ev_loop *event_lo ev_timer_start (event_loop, &wrk->hb.heartbeat_ev); } +static bool +rspamd_maybe_reuseport_socket (struct rspamd_worker_listen_socket *ls) +{ + if (ls->fd == -1 || ls->is_systemd || + rspamd_inet_address_get_af (ls->addr) == AF_UNIX) { + return false; + } + +#if defined(SO_REUSEPORT) && defined(SO_REUSEADDR) + gint nfd; + + nfd = rspamd_inet_address_listen (ls->addr, + (ls->type == RSPAMD_WORKER_SOCKET_UDP ? SOCK_DGRAM : SOCK_STREAM), + RSPAMD_INET_ADDRESS_LISTEN_ASYNC|RSPAMD_INET_ADDRESS_LISTEN_REUSEPORT, + -1); + + if (nfd == -1) { + msg_warn ("cannot create reuseport listen socket for %d: %s", + ls->fd, strerror (errno)); + } + else { + close (ls->fd); + ls->fd = nfd; + } +#else + +#endif + + return false; +} /** * Handles worker after fork returned zero @@ -1006,6 +1036,17 @@ rspamd_handle_child_fork (struct rspamd_worker *wrk, } } + /* Reuseport before dropping privs */ + GList *cur = cf->listen_socks; + + while (cur) { + struct rspamd_worker_listen_socket *ls = + (struct rspamd_worker_listen_socket *)cur->data; + + rspamd_maybe_reuseport_socket (ls); + cur = g_list_next (cur); + } + /* Drop privileges */ rspamd_worker_drop_priv (rspamd_main); /* Set limits */ -- cgit v1.2.3