From: Timo Sirainen Date: Mon, 20 Oct 2025 09:30:45 +0000 (+0300) Subject: lib, global: Change net_accept() to return only -1 on failure X-Git-Tag: 2.4.2~38 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=4eff08ba8e4fb626665dfb03b9ab9ba88db85772;p=thirdparty%2Fdovecot%2Fcore.git lib, global: Change net_accept() to return only -1 on failure 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. --- diff --git a/src/lib-auth-client/test-auth-client.c b/src/lib-auth-client/test-auth-client.c index fe4154b7a5..2d8333a688 100644 --- a/src/lib-auth-client/test-auth-client.c +++ b/src/lib-auth-client/test-auth-client.c @@ -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); } diff --git a/src/lib-auth-client/test-auth-master.c b/src/lib-auth-client/test-auth-master.c index a0ed073d9e..7fa7089856 100644 --- a/src/lib-auth-client/test-auth-master.c +++ b/src/lib-auth-client/test-auth-master.c @@ -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); diff --git a/src/lib-http/test-http-client-errors.c b/src/lib-http/test-http-client-errors.c index 7cdd54456a..d80ad43483 100644 --- a/src/lib-http/test-http-client-errors.c +++ b/src/lib-http/test-http-client-errors.c @@ -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); diff --git a/src/lib-http/test-http-server-errors.c b/src/lib-http/test-http-server-errors.c index 93ef14b4b0..98336284a2 100644 --- a/src/lib-http/test-http-server-errors.c +++ b/src/lib-http/test-http-server-errors.c @@ -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, diff --git a/src/lib-http/test-http-server.c b/src/lib-http/test-http-server.c index 3ca7bf14b5..5c853b7897 100644 --- a/src/lib-http/test-http-server.c +++ b/src/lib-http/test-http-server.c @@ -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); } diff --git a/src/lib-login/test-login-server-auth.c b/src/lib-login/test-login-server-auth.c index cc65744ffb..631c000c57 100644 --- a/src/lib-login/test-login-server-auth.c +++ b/src/lib-login/test-login-server-auth.c @@ -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); diff --git a/src/lib-lua/test-lua-http-client.c b/src/lib-lua/test-lua-http-client.c index 345a03229a..198ee0ecc1 100644 --- a/src/lib-lua/test-lua-http-client.c +++ b/src/lib-lua/test-lua-http-client.c @@ -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); } diff --git a/src/lib-master/master-service.c b/src/lib-master/master-service.c index e9f73cc1ff..582744e081 100644 --- a/src/lib-master/master-service.c +++ b/src/lib-master/master-service.c @@ -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) { diff --git a/src/lib-smtp/test-smtp-client-errors.c b/src/lib-smtp/test-smtp-client-errors.c index 60a20cb981..07ba6c535e 100644 --- a/src/lib-smtp/test-smtp-client-errors.c +++ b/src/lib-smtp/test-smtp-client-errors.c @@ -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); } diff --git a/src/lib-smtp/test-smtp-server-errors.c b/src/lib-smtp/test-smtp-server-errors.c index 999a44da1e..3113b94b11 100644 --- a/src/lib-smtp/test-smtp-server-errors.c +++ b/src/lib-smtp/test-smtp-server-errors.c @@ -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) diff --git a/src/lib-smtp/test-smtp-submit.c b/src/lib-smtp/test-smtp-submit.c index b19c1802d8..28a90970b6 100644 --- a/src/lib-smtp/test-smtp-submit.c +++ b/src/lib-smtp/test-smtp-submit.c @@ -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); diff --git a/src/lib/net.c b/src/lib/net.c index d657882d39..083d2af106 100644 --- a/src/lib/net.c +++ b/src/lib/net.c @@ -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); diff --git a/src/lib/net.h b/src/lib/net.h index bf773fd143..6f8bca7d6e 100644 --- a/src/lib/net.h +++ b/src/lib/net.h @@ -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 */ diff --git a/src/master/master-client.c b/src/master/master-client.c index ac332f9269..f0e8b0f8ae 100644 --- a/src/master/master-client.c +++ b/src/master/master-client.c @@ -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; }