Line | Count | Source |
1 | | #define DISABLE_SIGN_COMPARE_WARNINGS |
2 | | |
3 | | #include "git-compat-util.h" |
4 | | #include "strbuf.h" |
5 | | #include "unix-socket.h" |
6 | | |
7 | 0 | #define DEFAULT_UNIX_STREAM_LISTEN_BACKLOG (5) |
8 | | |
9 | | static int chdir_len(const char *orig, int len) |
10 | 0 | { |
11 | 0 | char *path = xmemdupz(orig, len); |
12 | 0 | int r = chdir(path); |
13 | 0 | free(path); |
14 | 0 | return r; |
15 | 0 | } |
16 | | |
17 | | struct unix_sockaddr_context { |
18 | | char *orig_dir; |
19 | | }; |
20 | | |
21 | | static void unix_sockaddr_cleanup(struct unix_sockaddr_context *ctx) |
22 | 0 | { |
23 | 0 | if (!ctx->orig_dir) |
24 | 0 | return; |
25 | | /* |
26 | | * If we fail, we can't just return an error, since we have |
27 | | * moved the cwd of the whole process, which could confuse calling |
28 | | * code. We are better off to just die. |
29 | | */ |
30 | 0 | if (chdir(ctx->orig_dir) < 0) |
31 | 0 | die("unable to restore original working directory"); |
32 | 0 | free(ctx->orig_dir); |
33 | 0 | } |
34 | | |
35 | | static int unix_sockaddr_init(struct sockaddr_un *sa, const char *path, |
36 | | struct unix_sockaddr_context *ctx, |
37 | | int disallow_chdir) |
38 | 0 | { |
39 | 0 | int size = strlen(path) + 1; |
40 | |
|
41 | 0 | ctx->orig_dir = NULL; |
42 | 0 | if (size > sizeof(sa->sun_path)) { |
43 | 0 | const char *slash; |
44 | 0 | const char *dir; |
45 | 0 | struct strbuf cwd = STRBUF_INIT; |
46 | |
|
47 | 0 | if (disallow_chdir) { |
48 | 0 | errno = ENAMETOOLONG; |
49 | 0 | return -1; |
50 | 0 | } |
51 | | |
52 | 0 | slash = find_last_dir_sep(path); |
53 | 0 | if (!slash) { |
54 | 0 | errno = ENAMETOOLONG; |
55 | 0 | return -1; |
56 | 0 | } |
57 | | |
58 | 0 | dir = path; |
59 | 0 | path = slash + 1; |
60 | 0 | size = strlen(path) + 1; |
61 | 0 | if (size > sizeof(sa->sun_path)) { |
62 | 0 | errno = ENAMETOOLONG; |
63 | 0 | return -1; |
64 | 0 | } |
65 | 0 | if (strbuf_getcwd(&cwd)) |
66 | 0 | return -1; |
67 | 0 | ctx->orig_dir = strbuf_detach(&cwd, NULL); |
68 | 0 | if (chdir_len(dir, slash - dir) < 0) { |
69 | 0 | FREE_AND_NULL(ctx->orig_dir); |
70 | 0 | return -1; |
71 | 0 | } |
72 | 0 | } |
73 | | |
74 | 0 | memset(sa, 0, sizeof(*sa)); |
75 | 0 | sa->sun_family = AF_UNIX; |
76 | 0 | memcpy(sa->sun_path, path, size); |
77 | 0 | return 0; |
78 | 0 | } |
79 | | |
80 | | int unix_stream_connect(const char *path, int disallow_chdir) |
81 | 0 | { |
82 | 0 | int fd = -1, saved_errno; |
83 | 0 | struct sockaddr_un sa; |
84 | 0 | struct unix_sockaddr_context ctx; |
85 | |
|
86 | 0 | if (unix_sockaddr_init(&sa, path, &ctx, disallow_chdir) < 0) |
87 | 0 | return -1; |
88 | 0 | fd = socket(AF_UNIX, SOCK_STREAM, 0); |
89 | 0 | if (fd < 0) |
90 | 0 | goto fail; |
91 | | |
92 | 0 | if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) |
93 | 0 | goto fail; |
94 | 0 | unix_sockaddr_cleanup(&ctx); |
95 | 0 | return fd; |
96 | | |
97 | 0 | fail: |
98 | 0 | saved_errno = errno; |
99 | 0 | if (fd != -1) |
100 | 0 | close(fd); |
101 | 0 | unix_sockaddr_cleanup(&ctx); |
102 | 0 | errno = saved_errno; |
103 | 0 | return -1; |
104 | 0 | } |
105 | | |
106 | | int unix_stream_listen(const char *path, |
107 | | const struct unix_stream_listen_opts *opts) |
108 | 0 | { |
109 | 0 | int fd = -1, saved_errno; |
110 | 0 | int backlog; |
111 | 0 | struct sockaddr_un sa; |
112 | 0 | struct unix_sockaddr_context ctx; |
113 | |
|
114 | 0 | unlink(path); |
115 | |
|
116 | 0 | if (unix_sockaddr_init(&sa, path, &ctx, opts->disallow_chdir) < 0) |
117 | 0 | return -1; |
118 | 0 | fd = socket(AF_UNIX, SOCK_STREAM, 0); |
119 | 0 | if (fd < 0) |
120 | 0 | goto fail; |
121 | | |
122 | 0 | if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) |
123 | 0 | goto fail; |
124 | | |
125 | 0 | backlog = opts->listen_backlog_size; |
126 | 0 | if (backlog <= 0) |
127 | 0 | backlog = DEFAULT_UNIX_STREAM_LISTEN_BACKLOG; |
128 | 0 | if (listen(fd, backlog) < 0) |
129 | 0 | goto fail; |
130 | | |
131 | 0 | unix_sockaddr_cleanup(&ctx); |
132 | 0 | return fd; |
133 | | |
134 | 0 | fail: |
135 | 0 | saved_errno = errno; |
136 | 0 | if (fd != -1) |
137 | 0 | close(fd); |
138 | 0 | unix_sockaddr_cleanup(&ctx); |
139 | | errno = saved_errno; |
140 | 0 | return -1; |
141 | 0 | } |