Skip to content

Commit

Permalink
Introduce connection context for Unix socket (valkey-io#1160)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
pizhenwei authored Oct 19, 2024
1 parent a62d1f1 commit 64cfdf6
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down
3 changes: 1 addition & 2 deletions src/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions src/rdma.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 == '-';
Expand Down Expand Up @@ -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);

Expand Down
3 changes: 1 addition & 2 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
12 changes: 10 additions & 2 deletions src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*----------------------------------------------------------------------------*/
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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. */
Expand Down
7 changes: 4 additions & 3 deletions src/unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down

0 comments on commit 64cfdf6

Please sign in to comment.