unix-socket: add backlog size option to unix_stream_listen()

Update `unix_stream_listen()` to take an options structure to override default behaviors. This commit includes the size of the `listen()` backlog. Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff Hostetler committed Mar 15, 2021 at 21:08 UTC 55144ccb0ac48bd2db0b907a8e8123b2befe83d1
3 files changed +19 -4
builtin/credential-cache--daemon.c
+2 -1
@@ -203,9 +203,10 @@ static int serve_cache_loop(int fd)
203
204 static void serve_cache(const char *socket_path, int debug)
205 {
206 + struct unix_stream_listen_opts opts = UNIX_STREAM_LISTEN_OPTS_INIT;
207 int fd;
208
208 - fd = unix_stream_listen(socket_path);
209 + fd = unix_stream_listen(socket_path, &opts);
210 if (fd < 0)
211 die_errno("unable to bind to '%s'", socket_path);
212
unix-socket.c
+9 -2
@@ -1,6 +1,8 @@
1 #include "cache.h"
2 #include "unix-socket.h"
3
4 +#define DEFAULT_UNIX_STREAM_LISTEN_BACKLOG (5)
5 +
6 static int chdir_len(const char *orig, int len)
7 {
8 char *path = xmemdupz(orig, len);
@@ -89,9 +91,11 @@ fail:
91 return -1;
92 }
93
92 -int unix_stream_listen(const char *path)
94 +int unix_stream_listen(const char *path,
95 + const struct unix_stream_listen_opts *opts)
96 {
97 int fd = -1, saved_errno;
98 + int backlog;
99 struct sockaddr_un sa;
100 struct unix_sockaddr_context ctx;
101
@@ -106,7 +110,10 @@ int unix_stream_listen(const char *path)
110 if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
111 goto fail;
112
109 - if (listen(fd, 5) < 0)
113 + backlog = opts->listen_backlog_size;
114 + if (backlog <= 0)
115 + backlog = DEFAULT_UNIX_STREAM_LISTEN_BACKLOG;
116 + if (listen(fd, backlog) < 0)
117 goto fail;
118
119 unix_sockaddr_cleanup(&ctx);
unix-socket.h
+8 -1
@@ -1,7 +1,14 @@
1 #ifndef UNIX_SOCKET_H
2 #define UNIX_SOCKET_H
3
4 +struct unix_stream_listen_opts {
5 + int listen_backlog_size;
6 +};
7 +
8 +#define UNIX_STREAM_LISTEN_OPTS_INIT { 0 }
9 +
10 int unix_stream_connect(const char *path);
5 -int unix_stream_listen(const char *path);
11 +int unix_stream_listen(const char *path,
12 + const struct unix_stream_listen_opts *opts);
13
14 #endif /* UNIX_SOCKET_H */