From 64cfdf61eb8352e93f128db2065dc6528895e7de Mon Sep 17 00:00:00 2001 From: zhenwei pi Date: Sat, 19 Oct 2024 08:48:18 +0800 Subject: [PATCH] Introduce connection context for Unix socket (#1160) Hide 'unixsocketgroup' and 'unixsocketperm' into a Unix socket specific data structure. A single opaque pointer 'void *priv' is enough for a listener. Once any new config is added, we don't need 'void *priv2', 'void *priv3' and so on. Signed-off-by: zhenwei pi --- src/config.c | 4 ++-- src/connection.h | 3 +-- src/rdma.c | 8 ++++---- src/server.c | 3 +-- src/server.h | 12 ++++++++++-- src/unix.c | 7 ++++--- 6 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/config.c b/src/config.c index 663cf5da38..7bee87946d 100644 --- a/src/config.c +++ b/src/config.c @@ -3140,7 +3140,7 @@ standardConfig static_configs[] = { /* String Configs */ createStringConfig("aclfile", NULL, IMMUTABLE_CONFIG, ALLOW_EMPTY_STRING, server.acl_filename, "", NULL, NULL), createStringConfig("unixsocket", NULL, IMMUTABLE_CONFIG, EMPTY_STRING_IS_NULL, server.unixsocket, NULL, NULL, NULL), - createStringConfig("unixsocketgroup", NULL, IMMUTABLE_CONFIG, EMPTY_STRING_IS_NULL, server.unixsocketgroup, NULL, NULL, NULL), + createStringConfig("unixsocketgroup", NULL, IMMUTABLE_CONFIG, EMPTY_STRING_IS_NULL, server.unix_ctx_config.group, NULL, NULL, NULL), createStringConfig("pidfile", NULL, IMMUTABLE_CONFIG, EMPTY_STRING_IS_NULL, server.pidfile, NULL, NULL, NULL), createStringConfig("replica-announce-ip", "slave-announce-ip", MODIFIABLE_CONFIG, EMPTY_STRING_IS_NULL, server.replica_announce_ip, NULL, NULL, NULL), createStringConfig("primaryuser", "masteruser", MODIFIABLE_CONFIG | SENSITIVE_CONFIG, EMPTY_STRING_IS_NULL, server.primary_user, NULL, NULL, NULL), @@ -3234,7 +3234,7 @@ standardConfig static_configs[] = { /* Unsigned int configs */ createUIntConfig("maxclients", NULL, MODIFIABLE_CONFIG, 1, UINT_MAX, server.maxclients, 10000, INTEGER_CONFIG, NULL, updateMaxclients), - createUIntConfig("unixsocketperm", NULL, IMMUTABLE_CONFIG, 0, 0777, server.unixsocketperm, 0, OCTAL_CONFIG, NULL, NULL), + createUIntConfig("unixsocketperm", NULL, IMMUTABLE_CONFIG, 0, 0777, server.unix_ctx_config.perm, 0, OCTAL_CONFIG, NULL, NULL), createUIntConfig("socket-mark-id", NULL, IMMUTABLE_CONFIG, 0, UINT_MAX, server.socket_mark_id, 0, INTEGER_CONFIG, NULL, NULL), createUIntConfig("max-new-connections-per-cycle", NULL, MODIFIABLE_CONFIG, 1, 1000, server.max_new_conns_per_cycle, 10, INTEGER_CONFIG, NULL, NULL), createUIntConfig("max-new-tls-connections-per-cycle", NULL, MODIFIABLE_CONFIG, 1, 1000, server.max_new_tls_conns_per_cycle, 1, INTEGER_CONFIG, NULL, NULL), diff --git a/src/connection.h b/src/connection.h index 97d79e5655..0762441732 100644 --- a/src/connection.h +++ b/src/connection.h @@ -144,8 +144,7 @@ struct connListener { int bindaddr_count; int port; ConnectionType *ct; - void *priv1; /* used by connection type specified data */ - void *priv2; /* used by connection type specified data */ + void *priv; /* used by connection type specified data */ }; /* The connection module does not deal with listening and accepting sockets, diff --git a/src/rdma.c b/src/rdma.c index 6cbc5c70b5..dd6de395d0 100644 --- a/src/rdma.c +++ b/src/rdma.c @@ -748,7 +748,7 @@ static rdma_listener *rdmaFdToListener(connListener *listener, int fd) { for (int i = 0; i < listener->count; i++) { if (listener->fd[i] != fd) continue; - return (rdma_listener *)listener->priv1 + i; + return (rdma_listener *)listener->priv + i; } return NULL; @@ -1537,7 +1537,7 @@ int connRdmaListen(connListener *listener) { bindaddr = default_bindaddr; } - listener->priv1 = rdma_listener = zcalloc_num(bindaddr_count, sizeof(*rdma_listener)); + listener->priv = rdma_listener = zcalloc_num(bindaddr_count, sizeof(*rdma_listener)); for (j = 0; j < bindaddr_count; j++) { char *addr = bindaddr[j]; int optional = *addr == '-'; @@ -1757,13 +1757,13 @@ static int rdmaChangeListener(void) { aeDeleteFileEvent(server.el, listener->fd[i], AE_READABLE); listener->fd[i] = -1; - struct rdma_listener *rdma_listener = (struct rdma_listener *)listener->priv1 + i; + struct rdma_listener *rdma_listener = (struct rdma_listener *)listener->priv + i; rdma_destroy_id(rdma_listener->cm_id); rdma_destroy_event_channel(rdma_listener->cm_channel); } listener->count = 0; - zfree(listener->priv1); + zfree(listener->priv); closeListener(listener); diff --git a/src/server.c b/src/server.c index ab95f84346..531fb07b76 100644 --- a/src/server.c +++ b/src/server.c @@ -2818,8 +2818,7 @@ void initListeners(void) { listener->bindaddr = &server.unixsocket; listener->bindaddr_count = 1; listener->ct = connectionByType(CONN_TYPE_UNIX); - listener->priv1 = &server.unixsocketperm; /* Unix socket specified */ - listener->priv2 = server.unixsocketgroup; /* Unix socket group specified */ + listener->priv = &server.unix_ctx_config; /* Unix socket specified */ } /* create all the configured listener, and add handler to start to accept */ diff --git a/src/server.h b/src/server.h index 4fad8d2508..280c439323 100644 --- a/src/server.h +++ b/src/server.h @@ -1593,6 +1593,15 @@ typedef struct serverTLSContextConfig { int session_cache_timeout; } serverTLSContextConfig; +/*----------------------------------------------------------------------------- + * Unix Context Configuration + *----------------------------------------------------------------------------*/ + +typedef struct serverUnixContextConfig { + char *group; /* UNIX socket group */ + unsigned int perm; /* UNIX socket permission (see mode_t) */ +} serverUnixContextConfig; + /*----------------------------------------------------------------------------- * AOF manifest definition *----------------------------------------------------------------------------*/ @@ -1704,8 +1713,6 @@ struct valkeyServer { int bindaddr_count; /* Number of addresses in server.bindaddr[] */ char *bind_source_addr; /* Source address to bind on for outgoing connections */ char *unixsocket; /* UNIX socket path */ - char *unixsocketgroup; /* UNIX socket group */ - unsigned int unixsocketperm; /* UNIX socket permission (see mode_t) */ connListener listeners[CONN_TYPE_MAX]; /* TCP/Unix/TLS even more types */ uint32_t socket_mark_id; /* ID for listen socket marking */ connListener clistener; /* Cluster bus listener */ @@ -2202,6 +2209,7 @@ struct valkeyServer { int tls_replication; int tls_auth_clients; serverTLSContextConfig tls_ctx_config; + serverUnixContextConfig unix_ctx_config; /* cpu affinity */ char *server_cpulist; /* cpu affinity list of server main/io thread. */ char *bio_cpulist; /* cpu affinity list of bio thread. */ diff --git a/src/unix.c b/src/unix.c index ddfd73465a..35778779f9 100644 --- a/src/unix.c +++ b/src/unix.c @@ -51,8 +51,9 @@ static int connUnixIsLocal(connection *conn) { static int connUnixListen(connListener *listener) { int fd; - mode_t *perm = (mode_t *)listener->priv1; - char *group = (char *)listener->priv2; + serverUnixContextConfig *ctx_cfg = listener->priv; + mode_t perm = ctx_cfg->perm; + char *group = ctx_cfg->group; if (listener->bindaddr_count == 0) return C_OK; @@ -62,7 +63,7 @@ static int connUnixListen(connListener *listener) { char *addr = listener->bindaddr[j]; unlink(addr); /* don't care if this fails */ - fd = anetUnixServer(server.neterr, addr, *perm, server.tcp_backlog, group); + fd = anetUnixServer(server.neterr, addr, perm, server.tcp_backlog, group); if (fd == ANET_ERR) { serverLog(LL_WARNING, "Failed opening Unix socket: %s", server.neterr); exit(1);