From 7e9776ad7b81cff4f0c1ff3781bc6388323a3655 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Fri, 30 Aug 2019 14:41:47 +0200 Subject: [PATCH] MINOR: fd/log/sink: make the non-blocking initialization depend on the initialized bit Logs and sinks were resorting to dirty hacks to initialize an FD to non-blocking mode. Now we have a bit for this in the fd tab so we can do it on the fly on first use of the file descriptor. Previously it was set per log server by writing value 1 to the port, or during a sink initialization regardless of the usage of the fd. --- src/fd.c | 6 ++++++ src/log.c | 4 ++-- src/sink.c | 7 ------- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/fd.c b/src/fd.c index 17cf52b2d..58d73d4ab 100644 --- a/src/fd.c +++ b/src/fd.c @@ -414,6 +414,12 @@ ssize_t fd_write_frag_line(int fd, size_t maxlen, const struct ist pfx[], size_t vec++; } + if (unlikely(!fdtab[fd].initialized)) { + fdtab[fd].initialized = 1; + if (!isatty(fd)) + fcntl(fd, F_SETFL, O_NONBLOCK); + } + HA_SPIN_LOCK(FD_LOCK, &fdtab[fd].lock); sent = writev(fd, iovec, vec); HA_SPIN_UNLOCK(FD_LOCK, &fdtab[fd].lock); diff --git a/src/log.c b/src/log.c index b2e6231fa..72602b769 100644 --- a/src/log.c +++ b/src/log.c @@ -1504,13 +1504,13 @@ static inline void __do_send_log(struct logsrv *logsrv, int nblogger, char *pid_ if (logsrv->addr.ss_family == AF_UNSPEC) { /* the socket's address is a file descriptor */ plogfd = (int *)&((struct sockaddr_in *)&logsrv->addr)->sin_addr.s_addr; - if (unlikely(!((struct sockaddr_in *)&logsrv->addr)->sin_port)) { + if (!fdtab[*plogfd].initialized) { /* FD not yet initialized to non-blocking mode. * DON'T DO IT ON A TERMINAL! */ + fdtab[*plogfd].initialized = 1; if (!isatty(*plogfd)) fcntl(*plogfd, F_SETFL, O_NONBLOCK); - ((struct sockaddr_in *)&logsrv->addr)->sin_port = 1; } } else if (logsrv->addr.ss_family == AF_UNIX) diff --git a/src/sink.c b/src/sink.c index ed49062d8..bd06f1ebb 100644 --- a/src/sink.c +++ b/src/sink.c @@ -18,8 +18,6 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include -#include #include #include #include @@ -93,11 +91,6 @@ struct sink *sink_new_fd(const char *name, const char *desc, enum sink_fmt fmt, goto end; } - /* FD not yet initialized to non-blocking mode. - * DON'T DO IT ON A TERMINAL! - */ - if (!isatty(fd)) - fcntl(fd, F_SETFL, O_NONBLOCK); sink->type = SINK_TYPE_FD; sink->ctx.fd = fd; end: