]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: fd/log/sink: make the non-blocking initialization depend on the initialized bit
authorWilly Tarreau <w@1wt.eu>
Fri, 30 Aug 2019 12:41:47 +0000 (14:41 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 30 Aug 2019 13:07:25 +0000 (15:07 +0200)
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
src/log.c
src/sink.c

index 17cf52b2d46ba98e69a44f2280b24a82fa49d623..58d73d4ab471a99f38ab047446c420ad09b8555f 100644 (file)
--- 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);
index b2e6231faa7d3299aab912b74ba7712baa958d0a..72602b7694a096bd16ea645125fe2fd3ad0a743f 100644 (file)
--- 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)
index ed49062d8e6bfbfa02d22198951a62654f2721d0..bd06f1ebbe2fb0ba6f3ef5b56e41e2b566861b7d 100644 (file)
@@ -18,8 +18,6 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
-#include <fcntl.h>
-#include <unistd.h>
 #include <common/compat.h>
 #include <common/config.h>
 #include <common/ist.h>
@@ -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: