From: Willy Tarreau Date: Fri, 30 Aug 2019 12:41:47 +0000 (+0200) Subject: MINOR: fd/log/sink: make the non-blocking initialization depend on the initialized bit X-Git-Tag: v2.1-dev2~128 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=7e9776ad7b81cff4f0c1ff3781bc6388323a3655;p=thirdparty%2Fhaproxy.git 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. --- diff --git a/src/fd.c b/src/fd.c index 17cf52b2d4..58d73d4ab4 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 b2e6231faa..72602b7694 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 ed49062d8e..bd06f1ebbe 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: