]> git.ipfire.org Git - thirdparty/git.git/blame - unix-socket.c
Merge branch 'sb/userdiff-dts'
[thirdparty/git.git] / unix-socket.c
CommitLineData
e2770979
JK
1#include "cache.h"
2#include "unix-socket.h"
3
4static 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
1eb10f40
JK
12static int chdir_len(const char *orig, int len)
13{
14 char *path = xmemdupz(orig, len);
15 int r = chdir(path);
16 free(path);
17 return r;
18}
19
20struct unix_sockaddr_context {
d13a0a97 21 char *orig_dir;
1eb10f40
JK
22};
23
24static void unix_sockaddr_cleanup(struct unix_sockaddr_context *ctx)
25{
d13a0a97 26 if (!ctx->orig_dir)
1eb10f40
JK
27 return;
28 /*
29 * If we fail, we can't just return an error, since we have
30 * moved the cwd of the whole process, which could confuse calling
31 * code. We are better off to just die.
32 */
33 if (chdir(ctx->orig_dir) < 0)
34 die("unable to restore original working directory");
d13a0a97 35 free(ctx->orig_dir);
1eb10f40
JK
36}
37
38static int unix_sockaddr_init(struct sockaddr_un *sa, const char *path,
39 struct unix_sockaddr_context *ctx)
e2770979
JK
40{
41 int size = strlen(path) + 1;
1eb10f40 42
d13a0a97 43 ctx->orig_dir = NULL;
1eb10f40
JK
44 if (size > sizeof(sa->sun_path)) {
45 const char *slash = find_last_dir_sep(path);
46 const char *dir;
d13a0a97 47 struct strbuf cwd = STRBUF_INIT;
1eb10f40
JK
48
49 if (!slash) {
50 errno = ENAMETOOLONG;
51 return -1;
52 }
53
54 dir = path;
55 path = slash + 1;
56 size = strlen(path) + 1;
57 if (size > sizeof(sa->sun_path)) {
58 errno = ENAMETOOLONG;
59 return -1;
60 }
d13a0a97 61 if (strbuf_getcwd(&cwd))
1eb10f40 62 return -1;
d13a0a97 63 ctx->orig_dir = strbuf_detach(&cwd, NULL);
1eb10f40
JK
64 if (chdir_len(dir, slash - dir) < 0)
65 return -1;
66 }
67
e2770979
JK
68 memset(sa, 0, sizeof(*sa));
69 sa->sun_family = AF_UNIX;
70 memcpy(sa->sun_path, path, size);
1eb10f40 71 return 0;
e2770979
JK
72}
73
74int unix_stream_connect(const char *path)
75{
06121a0a 76 int fd, saved_errno;
e2770979 77 struct sockaddr_un sa;
1eb10f40 78 struct unix_sockaddr_context ctx;
e2770979 79
1eb10f40
JK
80 if (unix_sockaddr_init(&sa, path, &ctx) < 0)
81 return -1;
e2770979 82 fd = unix_stream_socket();
06121a0a
JN
83 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
84 goto fail;
1eb10f40 85 unix_sockaddr_cleanup(&ctx);
e2770979 86 return fd;
06121a0a
JN
87
88fail:
89 saved_errno = errno;
90 unix_sockaddr_cleanup(&ctx);
91 close(fd);
92 errno = saved_errno;
93 return -1;
e2770979
JK
94}
95
96int unix_stream_listen(const char *path)
97{
06121a0a 98 int fd, saved_errno;
e2770979 99 struct sockaddr_un sa;
1eb10f40 100 struct unix_sockaddr_context ctx;
e2770979 101
2869b3e5
RS
102 unlink(path);
103
1eb10f40
JK
104 if (unix_sockaddr_init(&sa, path, &ctx) < 0)
105 return -1;
e2770979
JK
106 fd = unix_stream_socket();
107
06121a0a
JN
108 if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
109 goto fail;
e2770979 110
06121a0a
JN
111 if (listen(fd, 5) < 0)
112 goto fail;
e2770979 113
1eb10f40 114 unix_sockaddr_cleanup(&ctx);
e2770979 115 return fd;
06121a0a
JN
116
117fail:
118 saved_errno = errno;
119 unix_sockaddr_cleanup(&ctx);
120 close(fd);
121 errno = saved_errno;
122 return -1;
e2770979 123}