]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib, global: Change net_accept() to return only -1 on failure
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Mon, 20 Oct 2025 09:30:45 +0000 (12:30 +0300)
committeraki.tuomi <aki.tuomi@open-xchange.com>
Tue, 21 Oct 2025 11:03:24 +0000 (11:03 +0000)
Use NET_ACCEPT_ENOCONN() to further check the errno if it's a failure that
should be logged or not. The previous way of using -1 vs -2 return value
wasn't very understandable.

14 files changed:
src/lib-auth-client/test-auth-client.c
src/lib-auth-client/test-auth-master.c
src/lib-http/test-http-client-errors.c
src/lib-http/test-http-server-errors.c
src/lib-http/test-http-server.c
src/lib-login/test-login-server-auth.c
src/lib-lua/test-lua-http-client.c
src/lib-master/master-service.c
src/lib-smtp/test-smtp-client-errors.c
src/lib-smtp/test-smtp-server-errors.c
src/lib-smtp/test-smtp-submit.c
src/lib/net.c
src/lib/net.h
src/master/master-client.c

index fe4154b7a52f7ed1954afe2d058d573685b6f4b5..2d8333a68841e3194c34caf611a30bef2d761785 100644 (file)
@@ -1119,10 +1119,11 @@ static void server_connection_accept(void *context ATTR_UNUSED)
 
        /* accept new client */
        fd = net_accept(fd_listen, NULL, NULL);
-       if (fd == -1)
+       if (fd == -1) {
+               if (!NET_ACCEPT_ENOCONN(errno))
+                       i_fatal("test server: accept() failed: %m");
                return;
-       if (fd == -2)
-               i_fatal("test server: accept() failed: %m");
+       }
 
        server_connection_init(fd);
 }
index a0ed073d9ea97e97c874277935662dd165821afb..7fa7089856e1b51c514b8edbe7958b3dad91364d 100644 (file)
@@ -1769,10 +1769,10 @@ static void server_connection_accept(void *context ATTR_UNUSED)
 
        /* accept new client */
        fd = net_accept(fd_listen, NULL, NULL);
-       if (fd == -1)
+       if (fd == -1) {
+               if (!NET_ACCEPT_ENOCONN(errno))
+                       i_fatal("test server: accept() failed: %m");
                return;
-       if (fd == -2) {
-               i_fatal("test server: accept() failed: %m");
        }
 
        server_connection_init(fd);
index 7cdd54456ab7aeed9177cba0687cf6e78640cf78..d80ad4348370e69fd463c5b709951096ede428f9 100644 (file)
@@ -3597,10 +3597,10 @@ static void server_connection_accept(void *context ATTR_UNUSED)
 
        /* accept new client */
        fd = net_accept(fd_listen, NULL, NULL);
-       if (fd == -1)
+       if (fd == -1) {
+               if (!NET_ACCEPT_ENOCONN(errno))
+                       i_fatal("test server: accept() failed: %m");
                return;
-       if (fd == -2) {
-               i_fatal("test server: accept() failed: %m");
        }
 
        server_connection_init(fd);
index 93ef14b4b0d9bef67357e990a1e55966477b1fc6..98336284a2c5c366bd9f2c8d829aa5cfd4581875 100644 (file)
@@ -862,10 +862,10 @@ static void server_connection_accept(void *context ATTR_UNUSED)
 
        /* accept new client */
        fd = net_accept(fd_listen, NULL, NULL);
-       if (fd == -1)
+       if (fd == -1) {
+               if (!NET_ACCEPT_ENOCONN(errno))
+                       i_fatal("test server: accept() failed: %m");
                return;
-       if (fd == -2) {
-               i_fatal("test server: accept() failed: %m");
        }
 
        (void)http_server_connection_create(http_server, fd, fd, FALSE,
index 3ca7bf14b571d0b44013b6665d6548089e7c13cd..5c853b7897edfbfb107e8103e64d5557b2002dec 100644 (file)
@@ -157,10 +157,11 @@ static void client_accept(void *context ATTR_UNUSED)
        int fd;
 
        fd = net_accept(fd_listen, &client_ip, &client_port);
-       if (fd == -1)
+       if (fd == -1) {
+               if (!NET_ACCEPT_ENOCONN(errno))
+                       i_fatal("accept() failed: %m");
                return;
-       if (fd == -2)
-               i_fatal("accept() failed: %m");
+       }
 
        client_init(fd, &client_ip, client_port);
 }
index cc65744ffb6cb77fb12a767a65c1404f03c9f146..631c000c57eacd0e72697a2f87c6f9f7a732bfdb 100644 (file)
@@ -825,10 +825,10 @@ static void server_connection_accept(void *context ATTR_UNUSED)
 
        /* accept new client */
        fd = net_accept(fd_listen, NULL, NULL);
-       if (fd == -1)
+       if (fd == -1) {
+               if (!NET_ACCEPT_ENOCONN(errno))
+                       i_fatal("test server: accept() failed: %m");
                return;
-       if (fd == -2) {
-               i_fatal("test server: accept() failed: %m");
        }
 
        server_connection_init(fd);
index 345a03229ad5e7911a10682918564c4b0b3926ee..198ee0ecc15b5f0958e1a090fb4d2ce7f57b94fb 100644 (file)
@@ -579,10 +579,11 @@ static void server_connection_accept(void *context ATTR_UNUSED)
 
        /* accept new client */
        fd = net_accept(fd_listen, NULL, NULL);
-       if (fd == -1)
+       if (fd == -1) {
+               if (!NET_ACCEPT_ENOCONN(errno))
+                       i_fatal("test server: accept() failed: %m");
                return;
-       if (fd == -2)
-               i_fatal("test server: accept() failed: %m");
+       }
 
        server_connection_init(fd);
 }
index e9f73cc1ffc0e6bcba3e2cd065eba6b8314222b9..582744e081b3e5e0d767faef9408d8fc276876f1 100644 (file)
@@ -1778,11 +1778,11 @@ master_service_accept(struct master_service_listener *l, const char *conn_name,
        i_zero(&conn);
        conn.listen_fd = l->fd;
        conn.fd = net_accept(l->fd, &conn.remote_ip, &conn.remote_port);
-       if (conn.fd < 0) {
+       if (conn.fd == -1) {
                struct stat st;
                int orig_errno = errno;
 
-               if (conn.fd == -1)
+               if (NET_ACCEPT_ENOCONN(errno))
                        return;
 
                if (errno == ENOTSOCK) {
index 60a20cb981f4ebd96fbf479c051ac105f79157d3..07ba6c535e7ed6940e7517411dd4ffdca39d0a5c 100644 (file)
@@ -4110,10 +4110,11 @@ static void server_connection_accept(void *context ATTR_UNUSED)
 
        /* accept new client */
        fd = net_accept(fd_listen, NULL, NULL);
-       if (fd == -1)
+       if (fd == -1) {
+               if (!NET_ACCEPT_ENOCONN(errno))
+                       i_fatal("test server: accept() failed: %m");
                return;
-       if (fd == -2)
-               i_fatal("test server: accept() failed: %m");
+       }
 
        server_connection_init(fd);
 }
index 999a44da1e7cf45e2e91c6c8e559df7553ae80de..3113b94b111c32a4209ffec4a0885da8d433b0d6 100644 (file)
@@ -3914,10 +3914,10 @@ static void server_connection_accept(void *context ATTR_UNUSED)
 
        /* accept new client */
        fd = net_accept(fd_listen, NULL, NULL);
-       if (fd == -1)
+       if (fd == -1) {
+               if (!NET_ACCEPT_ENOCONN(errno))
+                       i_fatal("test server: accept() failed: %m");
                return;
-       if (fd == -2) {
-               i_fatal("test server: accept() failed: %m");
        }
 
        if (debug)
index b19c1802d8344da9d50dff75d282262180cfc89e..28a90970b6f724ea0a038613e07488f4c2588afc 100644 (file)
@@ -1938,10 +1938,10 @@ static void server_connection_accept(void *context ATTR_UNUSED)
 
        /* accept new client */
        fd = net_accept(fd_listen, NULL, NULL);
-       if (fd == -1)
+       if (fd == -1) {
+               if (!NET_ACCEPT_ENOCONN(errno))
+                       i_fatal("test server: accept() failed: %m");
                return;
-       if (fd == -2) {
-               i_fatal("test server: accept() failed: %m");
        }
 
        server_connection_init(fd);
index d657882d393adf5af43f15c50ecfd41ac06a4ebd..083d2af10637774d023e83f022e98c525f76e574 100644 (file)
@@ -628,12 +628,8 @@ int net_accept(int fd, struct ip_addr *addr_r, in_port_t *port_r)
        addrlen = sizeof(so);
        ret = accept(fd, &so.sa, &addrlen);
 
-       if (ret < 0) {
-               if (errno == EAGAIN || errno == ECONNABORTED)
-                       return -1;
-               else
-                       return -2;
-       }
+       if (ret < 0)
+               return -1;
        if (so.sin.sin_family == AF_UNIX) {
                if (addr_r != NULL)
                        i_zero(addr_r);
index bf773fd14366d8629d726fbe4bd0680456bcec63..6f8bca7d6e186a7097317460cc3f57f98ef47d03 100644 (file)
@@ -116,10 +116,15 @@ int net_listen_unix(const char *path, int backlog);
    If it fails with ECONNREFUSED, unlink the socket and try creating it
    again. */
 int net_listen_unix_unlink_stale(const char *path, int backlog);
-/* Accept a connection on a socket. Returns -1 if the connection got closed,
-   -2 for other failures. For UNIX sockets addr_r->family=port=0. */
+/* Accept a connection on a socket. Returns the new connection's fd, or -1 on
+   error. Check the failure with NET_ACCEPT_ENOCONN(errno). For UNIX sockets
+   addr_r->family=port=0. */
 int net_accept(int fd, struct ip_addr *addr_r, in_port_t *port_r)
        ATTR_NULL(2, 3);
+/* Returns TRUE if the connection was already closed or was accepted by another
+   process, i.e. normal failures that shouldn't be logged. */
+#define NET_ACCEPT_ENOCONN(err) \
+       ((err) == EAGAIN || (err) == ECONNABORTED)
 
 /* Read data from socket, return number of bytes read,
    -1 = error, -2 = disconnected */
index ac332f926987b134e0e36d15f164cd0bd6219dc2..f0e8b0f8ae0cb15a18f7cd60c705ef5716d4aac8 100644 (file)
@@ -200,8 +200,8 @@ void master_client_connected(struct service_list *service_list)
        int fd;
 
        fd = net_accept(service_list->master_fd, NULL, NULL);
-       if (fd < 0) {
-               if (fd == -2)
+       if (fd == -1) {
+               if (!NET_ACCEPT_ENOCONN(errno))
                        e_error(service_list->event, "net_accept() failed: %m");
                return;
        }