unix-socket: eliminate static unix_stream_socket() helper function

The static helper function `unix_stream_socket()` calls `die()`. This is not appropriate for all callers. Eliminate the wrapper function and make the callers propagate the error. 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 4f98ce586542a9fc1a2f4b5633dc7edc8922fa8f
1 file changed +13 -14
unix-socket.c
+13 -14
@@ -1,14 +1,6 @@
1 #include "cache.h"
2 #include "unix-socket.h"
3
4 -static int unix_stream_socket(void)
5 -{
6 - int fd = socket(AF_UNIX, SOCK_STREAM, 0);
7 - if (fd < 0)
8 - die_errno("unable to create socket");
9 - return fd;
10 -}
11 -
4 static int chdir_len(const char *orig, int len)
5 {
6 char *path = xmemdupz(orig, len);
@@ -73,13 +65,16 @@ static int unix_sockaddr_init(struct sockaddr_un *sa, const char *path,
65
66 int unix_stream_connect(const char *path)
67 {
76 - int fd, saved_errno;
68 + int fd = -1, saved_errno;
69 struct sockaddr_un sa;
70 struct unix_sockaddr_context ctx;
71
72 if (unix_sockaddr_init(&sa, path, &ctx) < 0)
73 return -1;
82 - fd = unix_stream_socket();
74 + fd = socket(AF_UNIX, SOCK_STREAM, 0);
75 + if (fd < 0)
76 + goto fail;
77 +
78 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
79 goto fail;
80 unix_sockaddr_cleanup(&ctx);
@@ -87,15 +82,16 @@ int unix_stream_connect(const char *path)
82
83 fail:
84 saved_errno = errno;
85 + if (fd != -1)
86 + close(fd);
87 unix_sockaddr_cleanup(&ctx);
91 - close(fd);
88 errno = saved_errno;
89 return -1;
90 }
91
92 int unix_stream_listen(const char *path)
93 {
98 - int fd, saved_errno;
94 + int fd = -1, saved_errno;
95 struct sockaddr_un sa;
96 struct unix_sockaddr_context ctx;
97
@@ -103,7 +99,9 @@ int unix_stream_listen(const char *path)
99
100 if (unix_sockaddr_init(&sa, path, &ctx) < 0)
101 return -1;
106 - fd = unix_stream_socket();
102 + fd = socket(AF_UNIX, SOCK_STREAM, 0);
103 + if (fd < 0)
104 + goto fail;
105
106 if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
107 goto fail;
@@ -116,8 +114,9 @@ int unix_stream_listen(const char *path)
114
115 fail:
116 saved_errno = errno;
117 + if (fd != -1)
118 + close(fd);
119 unix_sockaddr_cleanup(&ctx);
120 - close(fd);
120 errno = saved_errno;
121 return -1;
122 }