CLEANUP: tree-wide: use fd_set_nonblock() and fd_set_cloexec()

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.
This commit is contained in:
Willy Tarreau 2022-04-26 10:24:14 +02:00
parent a80e4a3546
commit 382474348c
12 changed files with 21 additions and 31 deletions

View 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();

View File

@ -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;

View File

@ -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);

View File

@ -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);
}
}

View 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
*/

View 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;

View 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 */

View 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;

View 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;

View 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);
}
}

View 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;

View 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;