Skip to content

Commit 4f98ce5

Browse files
jeffhostetlergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 59c7b88 commit 4f98ce5

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

unix-socket.c

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
#include "cache.h"
22
#include "unix-socket.h"
33

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-
124
static int chdir_len(const char *orig, int len)
135
{
146
char *path = xmemdupz(orig, len);
@@ -73,37 +65,43 @@ static int unix_sockaddr_init(struct sockaddr_un *sa, const char *path,
7365

7466
int unix_stream_connect(const char *path)
7567
{
76-
int fd, saved_errno;
68+
int fd = -1, saved_errno;
7769
struct sockaddr_un sa;
7870
struct unix_sockaddr_context ctx;
7971

8072
if (unix_sockaddr_init(&sa, path, &ctx) < 0)
8173
return -1;
82-
fd = unix_stream_socket();
74+
fd = socket(AF_UNIX, SOCK_STREAM, 0);
75+
if (fd < 0)
76+
goto fail;
77+
8378
if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
8479
goto fail;
8580
unix_sockaddr_cleanup(&ctx);
8681
return fd;
8782

8883
fail:
8984
saved_errno = errno;
85+
if (fd != -1)
86+
close(fd);
9087
unix_sockaddr_cleanup(&ctx);
91-
close(fd);
9288
errno = saved_errno;
9389
return -1;
9490
}
9591

9692
int unix_stream_listen(const char *path)
9793
{
98-
int fd, saved_errno;
94+
int fd = -1, saved_errno;
9995
struct sockaddr_un sa;
10096
struct unix_sockaddr_context ctx;
10197

10298
unlink(path);
10399

104100
if (unix_sockaddr_init(&sa, path, &ctx) < 0)
105101
return -1;
106-
fd = unix_stream_socket();
102+
fd = socket(AF_UNIX, SOCK_STREAM, 0);
103+
if (fd < 0)
104+
goto fail;
107105

108106
if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
109107
goto fail;
@@ -116,8 +114,9 @@ int unix_stream_listen(const char *path)
116114

117115
fail:
118116
saved_errno = errno;
117+
if (fd != -1)
118+
close(fd);
119119
unix_sockaddr_cleanup(&ctx);
120-
close(fd);
121120
errno = saved_errno;
122121
return -1;
123122
}

0 commit comments

Comments
 (0)