summaryrefslogtreecommitdiff
path: root/src/anet.c
diff options
context:
space:
mode:
authorOran Agra <oran@redislabs.com>2021-07-05 10:34:20 +0300
committerGitHub <noreply@github.com>2021-07-05 10:34:20 +0300
commit5a3de81925130792f78c35e9e4d0204213a3a41e (patch)
tree34e46fe7b782a64f0ed7e0f04e59e6a419020714 /src/anet.c
parentec582cc7ad0706e252bc905822226e49f4c4d0e4 (diff)
downloadredis-5a3de81925130792f78c35e9e4d0204213a3a41e.tar.gz
Use accept4 on linux instead of fcntl to make a client socket non-blocking (#9177)
This reduces system calls on linux when a new connection is made / accepted. Changes: * Add the SOCK_CLOEXEC option to the accept4() call This ensure that a fork/exec call does not leak a file descriptor. * Move anetCloexec and connNonBlock info anetGenericAccept * Moving connNonBlock from accept handlers to anetGenericAccept Moving connNonBlock from createClient, is safe because createClient is used in the following ways: 1. without a connection (fake client) 2. on an accepted connection (see above) 3. creating the master client by using connConnect (see below) The third case, can either use anetTcpNonBlockConnect, or connTLSConnect which is by default non-blocking. Co-authored-by: Rajiv Kurian <geetasen@gmail.com> Co-authored-by: Oran Agra <oran@redislabs.com> Co-authored-by: Yoav Steinberg <yoav@redislabs.com>
Diffstat (limited to 'src/anet.c')
-rw-r--r--src/anet.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/anet.c b/src/anet.c
index 381dda375..f4f3a98bf 100644
--- a/src/anet.c
+++ b/src/anet.c
@@ -47,6 +47,7 @@
#include <stdio.h>
#include "anet.h"
+#include "config.h"
static void anetSetError(char *err, const char *fmt, ...)
{
@@ -491,18 +492,39 @@ int anetUnixServer(char *err, char *path, mode_t perm, int backlog)
return s;
}
+/* Accept a connection and also make sure the socket is non-blocking, and CLOEXEC.
+ * returns the new socket FD, or -1 on error. */
static int anetGenericAccept(char *err, int s, struct sockaddr *sa, socklen_t *len) {
int fd;
do {
+ /* Use the accept4() call on linux to simultaneously accept and
+ * set a socket as non-blocking. */
+#ifdef HAVE_ACCEPT4
+ fd = accept4(s, sa, len, SOCK_NONBLOCK | SOCK_CLOEXEC);
+#else
fd = accept(s,sa,len);
+#endif
} while(fd == -1 && errno == EINTR);
if (fd == -1) {
anetSetError(err, "accept: %s", strerror(errno));
return ANET_ERR;
}
+#ifndef HAVE_ACCEPT4
+ if (anetCloexec(fd) == -1) {
+ anetSetError(err, "anetCloexec: %s", strerror(errno));
+ close(fd);
+ return ANET_ERR;
+ }
+ if (anetNonBlock(err, fd) != ANET_OK) {
+ close(fd);
+ return ANET_ERR;
+ }
+#endif
return fd;
}
+/* Accept a connection and also make sure the socket is non-blocking, and CLOEXEC.
+ * returns the new socket FD, or -1 on error. */
int anetTcpAccept(char *err, int s, char *ip, size_t ip_len, int *port) {
int fd;
struct sockaddr_storage sa;
@@ -522,6 +544,8 @@ int anetTcpAccept(char *err, int s, char *ip, size_t ip_len, int *port) {
return fd;
}
+/* Accept a connection and also make sure the socket is non-blocking, and CLOEXEC.
+ * returns the new socket FD, or -1 on error. */
int anetUnixAccept(char *err, int s) {
int fd;
struct sockaddr_un sa;