Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion hiredis.c
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ redisContext *redisConnectWithOptions(const redisOptions *options) {
c->flags |= REDIS_PREFER_IPV6;
}
if (options->options & REDIS_OPT_SET_SOCK_CLOEXEC) {
c->flags |= REDIS_OPT_SET_SOCK_CLOEXEC;
c->flags |= REDIS_SET_SOCK_CLOEXEC;
}

/* Set any user supplied RESP3 PUSH handler or use freeReplyObject
Expand Down
3 changes: 3 additions & 0 deletions hiredis.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ typedef intptr_t ssize_t;
#define REDIS_PREFER_IPV4 0x800
#define REDIS_PREFER_IPV6 0x1000

/* Flag that is set when we should create the socket with SOCK_CLOEXEC */
#define REDIS_SET_SOCK_CLOEXEC 0x2000

#define REDIS_KEEPALIVE_INTERVAL 15 /* seconds */

/* number of times we retry to connect in the case of EADDRNOTAVAIL and
Expand Down
4 changes: 2 additions & 2 deletions net.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ static int redisCreateSocket(redisContext *c, int type) {
int flags = SOCK_STREAM;

#ifdef SOCK_CLOEXEC
if (c->flags & REDIS_OPT_SET_SOCK_CLOEXEC) {
if (c->flags & REDIS_SET_SOCK_CLOEXEC) {
flags |= SOCK_CLOEXEC;
}
#endif
Expand Down Expand Up @@ -570,7 +570,7 @@ addrretry: {
int sock_type = p->ai_socktype;

#ifdef SOCK_CLOEXEC
if (c->flags & REDIS_OPT_SET_SOCK_CLOEXEC) {
if (c->flags & REDIS_SET_SOCK_CLOEXEC) {
sock_type |= SOCK_CLOEXEC;
}
#endif
Expand Down
42 changes: 42 additions & 0 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#ifndef _WIN32
#include <strings.h>
#include <sys/time.h>
#include <fcntl.h>
#endif
#include <assert.h>
#include <signal.h>
Expand Down Expand Up @@ -442,6 +443,44 @@ static void test_tcp_options(struct config cfg) {
redisFree(c);
}

#ifdef SOCK_CLOEXEC
/* REDIS_OPT_REUSEADDR and REDIS_OPT_SET_SOCK_CLOEXEC must not leak into
* each other through the context flags. */
static void test_tcp_socket_flags(struct config cfg) {
redisContext *c;
redisOptions options = {0};
struct sockaddr_storage local;
socklen_t locallen = sizeof(local);
char source[128];
int reuse = 0;
socklen_t reuselen = sizeof(reuse);

REDIS_OPTIONS_SET_TCP(&options, cfg.tcp.host, cfg.tcp.port);
options.options = REDIS_OPT_REUSEADDR;
c = redisConnectWithOptions(&options);
assert(c != NULL && c->err == 0);
test("REDIS_OPT_REUSEADDR does not set FD_CLOEXEC: ");
test_cond(!(fcntl(c->fd, F_GETFD) & FD_CLOEXEC));
/* Bind the next connection to the local address of this one, which is
* assigned to this host even when the server is remote. */
assert(getsockname(c->fd, (struct sockaddr *)&local, &locallen) == 0);
assert(getnameinfo((struct sockaddr *)&local, locallen, source, sizeof(source),
NULL, 0, NI_NUMERICHOST) == 0);
redisFree(c);

options.options = REDIS_OPT_SET_SOCK_CLOEXEC;
options.endpoint.tcp.source_addr = source;
c = redisConnectWithOptions(&options);
assert(c != NULL && c->err == 0);
test("REDIS_OPT_SET_SOCK_CLOEXEC sets FD_CLOEXEC: ");
test_cond(fcntl(c->fd, F_GETFD) & FD_CLOEXEC);
test("REDIS_OPT_SET_SOCK_CLOEXEC does not set SO_REUSEADDR: ");
test_cond(getsockopt(c->fd, SOL_SOCKET, SO_REUSEADDR, &reuse, &reuselen) == 0 &&
reuse == 0);
redisFree(c);
}
#endif

static void test_unix_keepalive(struct config cfg) {
redisContext *c;
redisReply *r;
Expand Down Expand Up @@ -2759,6 +2798,9 @@ int main(int argc, char **argv) {
test_invalid_timeout_errors(cfg);
test_append_formatted_commands(cfg);
test_tcp_options(cfg);
#ifdef SOCK_CLOEXEC
test_tcp_socket_flags(cfg);
#endif
if (throughput) test_throughput(cfg);

printf("\nTesting against Unix socket connection (%s): ", cfg.unix_sock.path);
Expand Down
Loading