]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
CLEANUP: tree-wide: use fd_set_nonblock() and fd_set_cloexec()
authorWilly Tarreau <w@1wt.eu>
Tue, 26 Apr 2022 08:24:14 +0000 (10:24 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 26 Apr 2022 08:59:48 +0000 (10:59 +0200)
This gets rid of most open-coded fcntl() calls, some of which were passed
through DISGUISE() to avoid a useless test. The FD_CLOEXEC was most often
set without preserving previous flags, which could become a problem once
new flags are created. Now this will not happen anymore.

12 files changed:
src/debug.c
src/dns.c
src/fd.c
src/log.c
src/mworker.c
src/proto_quic.c
src/proto_sockpair.c
src/proto_tcp.c
src/proto_uxst.c
src/sock.c
src/sock_inet.c
src/sock_unix.c

index 285def16c4bb0b585a888ffb05d213bf36220bb0..1719036a2211e07e80e8d12af41cac81e6771ab6 100644 (file)
@@ -516,10 +516,10 @@ static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appct
        if (pipe(pipefd) < 0)
                goto fail_pipe;
 
-       if (fcntl(pipefd[0], F_SETFD, fcntl(pipefd[0], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
+       if (fd_set_cloexec(pipefd[0]) == -1)
                goto fail_fcntl;
 
-       if (fcntl(pipefd[1], F_SETFD, fcntl(pipefd[1], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
+       if (fd_set_cloexec(pipefd[1]) == -1)
                goto fail_fcntl;
 
        pid = fork();
index 4a776a1effab32070c6f07e6496d2222fb8f8b98..29d3cef39881aed53168d6fb9654df124e99156e 100644 (file)
--- a/src/dns.c
+++ b/src/dns.c
@@ -11,7 +11,6 @@
  */
 
 #include <errno.h>
-#include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -71,7 +70,7 @@ static int dns_connect_nameserver(struct dns_nameserver *ns)
        }
 
        /* Make the socket non blocking */
-       fcntl(fd, F_SETFL, O_NONBLOCK);
+       fd_set_nonblock(fd);
 
        /* Add the fd in the fd list and update its parameters */
        dgram->t.sock.fd = fd;
index 529cba739fbc15e00fc879ad7bb4d9bc6083eedb..6b2942d134aeb5cfb1cf4cacadebd2819f7ee797 100644 (file)
--- a/src/fd.c
+++ b/src/fd.c
@@ -630,7 +630,7 @@ ssize_t fd_write_frag_line(int fd, size_t maxlen, const struct ist pfx[], size_t
        if (unlikely(!(fdtab[fd].state & FD_INITIALIZED))) {
                HA_ATOMIC_OR(&fdtab[fd].state, FD_INITIALIZED);
                if (!isatty(fd))
-                       fcntl(fd, F_SETFL, O_NONBLOCK);
+                       fd_set_nonblock(fd);
        }
        sent = writev(fd, iovec, vec);
        HA_ATOMIC_BTR(&fdtab[fd].state, FD_EXCL_SYSCALL_BIT);
@@ -788,7 +788,7 @@ static int init_pollers_per_thread()
 
        poller_rd_pipe = mypipe[0];
        poller_wr_pipe[tid] = mypipe[1];
-       fcntl(poller_rd_pipe, F_SETFL, O_NONBLOCK);
+       fd_set_nonblock(poller_rd_pipe);
        fd_insert(poller_rd_pipe, poller_pipe_io_handler, poller_pipe_io_handler, tid_bit);
        fd_insert(poller_wr_pipe[tid], poller_pipe_io_handler, poller_pipe_io_handler, tid_bit);
        fd_want_recv(poller_rd_pipe);
index de9561d84a4adb569f32e43b6e57ad8c16d47f0f..522c2f602df063f28bc1cc1c23dccb9f53dd9ad8 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -11,7 +11,6 @@
  */
 
 #include <ctype.h>
-#include <fcntl.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -1668,7 +1667,7 @@ static inline void __do_send_log(struct logsrv *logsrv, int nblogger, int level,
                        setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
                        /* does nothing under Linux, maybe needed for others */
                        shutdown(*plogfd, SHUT_RD);
-                       fcntl(*plogfd, F_SETFD, fcntl(*plogfd, F_GETFD, FD_CLOEXEC) | FD_CLOEXEC);
+                       fd_set_cloexec(*plogfd);
                }
        }
 
index ccfcb5c6b7193279f40a30c73d23e44bb1e0afa5..1ee93fbd53af5af93cc9db427805706fa3f07f1c 100644 (file)
@@ -13,7 +13,6 @@
 #define _GNU_SOURCE
 
 #include <errno.h>
-#include <fcntl.h>
 #include <signal.h>
 #include <stdlib.h>
 #include <string.h>
@@ -422,7 +421,7 @@ static int mworker_pipe_register_per_thread()
        if (tid != 0)
                return 1;
 
-       fcntl(proc_self->ipc_fd[1], F_SETFL, O_NONBLOCK);
+       fd_set_nonblock(proc_self->ipc_fd[1]);
        /* In multi-tread, we need only one thread to process
         * events on the pipe with master
         */
index eb3a316df321c8c741aff5e71358d7a3624cc3cd..2186aa5aa81bf85c15d1bbaf5f34552349bbd8df 100644 (file)
@@ -12,7 +12,6 @@
 
 #include <ctype.h>
 #include <errno.h>
-#include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -334,7 +333,7 @@ int quic_connect_server(struct connection *conn, int flags)
                return SF_ERR_PRXCOND; /* it is a configuration limit */
        }
 
-       if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1)) {
+       if (fd_set_nonblock(fd) == -1) {
                qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
                close(fd);
                conn->err_code = CO_ER_SOCK_ERR;
@@ -342,7 +341,7 @@ int quic_connect_server(struct connection *conn, int flags)
                return SF_ERR_INTERNAL;
        }
 
-       if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
+       if (master == 1 && fd_set_cloexec(fd) == -1) {
                ha_alert("Cannot set CLOEXEC on client socket.\n");
                close(fd);
                conn->err_code = CO_ER_SOCK_ERR;
index b244dd1b369e361b8af1917cc9e859a7d14f0fa6..a621ecbea2df6b84322f490c84f02d61e20b96ea 100644 (file)
@@ -12,7 +12,6 @@
 
 #include <ctype.h>
 #include <errno.h>
-#include <fcntl.h>
 #include <pwd.h>
 #include <grp.h>
 #include <stdio.h>
@@ -150,7 +149,7 @@ int sockpair_bind_receiver(struct receiver *rx, char **errmsg)
                goto bind_close_return;
        }
 
-       if (fcntl(rx->fd, F_SETFL, O_NONBLOCK) == -1) {
+       if (fd_set_nonblock(rx->fd) == -1) {
                err |= ERR_FATAL | ERR_ALERT;
                memprintf(errmsg, "cannot make socket non-blocking");
                goto bind_close_return;
@@ -313,7 +312,7 @@ static int sockpair_connect_server(struct connection *conn, int flags)
                return SF_ERR_PRXCOND; /* it is a configuration limit */
        }
 
-       if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
+       if (fd_set_nonblock(fd) == -1) {
                qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
                close(sv[0]);
                close(sv[1]);
@@ -322,7 +321,7 @@ static int sockpair_connect_server(struct connection *conn, int flags)
                return SF_ERR_INTERNAL;
        }
 
-       if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
+       if (master == 1 && fd_set_cloexec(fd) == -1) {
                ha_alert("Cannot set CLOEXEC on client socket.\n");
                close(sv[0]);
                close(sv[1]);
@@ -469,7 +468,7 @@ struct connection *sockpair_accept_conn(struct listener *l, int *status)
        int cfd;
 
        if ((cfd = recv_fd_uxst(l->rx.fd)) != -1)
-               DISGUISE(fcntl(cfd, F_SETFL, O_NONBLOCK));
+               fd_set_nonblock(cfd);
 
        if (likely(cfd != -1)) {
                /* Perfect, the connection was accepted */
index 738d2d113f8c5c6c7319f6128a35f589b7475912..a160b1b87af9bbab19c3b674d28cc8b70ad4a20e 100644 (file)
@@ -12,7 +12,6 @@
 
 #include <ctype.h>
 #include <errno.h>
-#include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -341,7 +340,7 @@ int tcp_connect_server(struct connection *conn, int flags)
                return SF_ERR_PRXCOND; /* it is a configuration limit */
        }
 
-       if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) ||
+       if (fd_set_nonblock(fd) == -1 ||
            (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) {
                qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
                close(fd);
@@ -350,7 +349,7 @@ int tcp_connect_server(struct connection *conn, int flags)
                return SF_ERR_INTERNAL;
        }
 
-       if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
+       if (master == 1 && fd_set_cloexec(fd) == -1) {
                ha_alert("Cannot set CLOEXEC on client socket.\n");
                close(fd);
                conn->err_code = CO_ER_SOCK_ERR;
index 2db4fa20b0b40bce19f53fdd22c30a7d1071745f..cc98e0d5cd016c2835de20c29796f535af82f9d5 100644 (file)
@@ -12,7 +12,6 @@
 
 #include <ctype.h>
 #include <errno.h>
-#include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -269,7 +268,7 @@ static int uxst_connect_server(struct connection *conn, int flags)
                return SF_ERR_PRXCOND; /* it is a configuration limit */
        }
 
-       if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
+       if (fd_set_nonblock(fd) == -1) {
                qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
                close(fd);
                conn->err_code = CO_ER_SOCK_ERR;
@@ -277,7 +276,7 @@ static int uxst_connect_server(struct connection *conn, int flags)
                return SF_ERR_INTERNAL;
        }
 
-       if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
+       if (master == 1 && fd_set_cloexec(fd) == -1) {
                ha_alert("Cannot set CLOEXEC on client socket.\n");
                close(fd);
                conn->err_code = CO_ER_SOCK_ERR;
index f4d8ba4d07aad435a817427c411d9677f9145d7e..6f58aec2f4d5f102550f9b037bdbc47554f97998 100644 (file)
@@ -13,7 +13,6 @@
 #define _GNU_SOURCE
 #include <ctype.h>
 #include <errno.h>
-#include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -96,9 +95,9 @@ struct connection *sock_accept_conn(struct listener *l, int *status)
        {
                laddr = sizeof(*conn->src);
                if ((cfd = accept(l->rx.fd, (struct sockaddr*)addr, &laddr)) != -1) {
-                       fcntl(cfd, F_SETFL, O_NONBLOCK);
+                       fd_set_nonblock(cfd);
                        if (master)
-                               fcntl(cfd, F_SETFD, FD_CLOEXEC);
+                               fd_set_cloexec(cfd);
                }
        }
 
index fa04dfed0b7a97c10cd410380133c2ed401afc6a..46cc16a1c3ebd10f78931bec5ed63a05c567f18a 100644 (file)
@@ -11,7 +11,6 @@
  */
 
 #include <errno.h>
-#include <fcntl.h>
 #include <string.h>
 #include <unistd.h>
 
@@ -319,7 +318,7 @@ int sock_inet_bind_receiver(struct receiver *rx, char **errmsg)
                goto bind_close_return;
        }
 
-       if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
+       if (fd_set_nonblock(fd) == -1) {
                err |= ERR_FATAL | ERR_ALERT;
                memprintf(errmsg, "cannot make socket non-blocking");
                goto bind_close_return;
index 45ba89f46dd871fcf0c5cc321f045656b6154809..143295e79af4025c4795d2a9250bd0ce11843642 100644 (file)
@@ -12,7 +12,6 @@
 
 #include <ctype.h>
 #include <errno.h>
-#include <fcntl.h>
 #include <string.h>
 #include <unistd.h>
 
@@ -233,7 +232,7 @@ int sock_unix_bind_receiver(struct receiver *rx, char **errmsg)
                goto bind_close_return;
        }
 
-       if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
+       if (fd_set_nonblock(fd) == -1) {
                err |= ERR_FATAL | ERR_ALERT;
                memprintf(errmsg, "cannot make socket non-blocking");
                goto bind_close_return;