]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: sock: add sock_accept_conn() to test a listening socket
authorWilly Tarreau <w@1wt.eu>
Tue, 13 Oct 2020 15:06:12 +0000 (17:06 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 13 Oct 2020 16:15:33 +0000 (18:15 +0200)
At several places we need to check if a socket is still valid and still
willing to accept connections. Instead of open-coding this, each time,
let's add a new function for this.

include/haproxy/sock.h
src/sock.c

index c43f4d88b92594bb04ad87dfedbd344f3271e4bd..40856f42e07abe1e7748d8e7e25751272a0f2b74 100644 (file)
@@ -40,6 +40,7 @@ int sock_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir);
 int sock_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir);
 int sock_get_old_sockets(const char *unixsocket);
 int sock_find_compatible_fd(const struct receiver *rx);
+int sock_accept_conn(const struct receiver *rx);
 
 #endif /* _HAPROXY_SOCK_H */
 
index 4ef0076900c27713bf149223d4ad16c53da03af0..ea6e3667248dbb7d381fd9cc71b5f470cb509755 100644 (file)
@@ -466,6 +466,22 @@ int sock_find_compatible_fd(const struct receiver *rx)
        return ret;
 }
 
+/* Tests if the receiver supports accepting connections. Returns positive on
+ * success, 0 if not possible, negative if the socket is non-recoverable. The
+ * rationale behind this is that inherited FDs may be broken and that shared
+ * FDs might have been paused by another process.
+ */
+int sock_accept_conn(const struct receiver *rx)
+{
+       int opt_val = 0;
+       socklen_t opt_len = sizeof(opt_val);
+
+       if (getsockopt(rx->fd, SOL_SOCKET, SO_ACCEPTCONN, &opt_val, &opt_len) == -1)
+               return -1;
+
+       return opt_val;
+}
+
 /*
  * Local variables:
  *  c-indent-level: 8