blob: 79800d80636fc57fa8cc2696e33013c1c29e86b3 [file] [log] [blame]
Elijah Newrenfc7bd512023-02-24 00:09:34 +00001#include "git-compat-util.h"
2#include "strbuf.h"
Jeff Kinge2770972011-12-10 05:34:14 -05003#include "unix-socket.h"
4
Jeff Hostetler55144cc2021-03-15 21:08:25 +00005#define DEFAULT_UNIX_STREAM_LISTEN_BACKLOG (5)
6
Jeff King1eb10f42012-01-09 23:44:30 -05007static int chdir_len(const char *orig, int len)
8{
9 char *path = xmemdupz(orig, len);
10 int r = chdir(path);
11 free(path);
12 return r;
13}
14
15struct unix_sockaddr_context {
René Scharfed13a0a92014-07-28 20:25:40 +020016 char *orig_dir;
Jeff King1eb10f42012-01-09 23:44:30 -050017};
18
19static void unix_sockaddr_cleanup(struct unix_sockaddr_context *ctx)
20{
René Scharfed13a0a92014-07-28 20:25:40 +020021 if (!ctx->orig_dir)
Jeff King1eb10f42012-01-09 23:44:30 -050022 return;
23 /*
24 * If we fail, we can't just return an error, since we have
25 * moved the cwd of the whole process, which could confuse calling
26 * code. We are better off to just die.
27 */
28 if (chdir(ctx->orig_dir) < 0)
29 die("unable to restore original working directory");
René Scharfed13a0a92014-07-28 20:25:40 +020030 free(ctx->orig_dir);
Jeff King1eb10f42012-01-09 23:44:30 -050031}
32
33static int unix_sockaddr_init(struct sockaddr_un *sa, const char *path,
Jeff Hostetler77e522c2021-03-15 21:08:26 +000034 struct unix_sockaddr_context *ctx,
35 int disallow_chdir)
Jeff Kinge2770972011-12-10 05:34:14 -050036{
37 int size = strlen(path) + 1;
Jeff King1eb10f42012-01-09 23:44:30 -050038
René Scharfed13a0a92014-07-28 20:25:40 +020039 ctx->orig_dir = NULL;
Jeff King1eb10f42012-01-09 23:44:30 -050040 if (size > sizeof(sa->sun_path)) {
Jeff Hostetler77e522c2021-03-15 21:08:26 +000041 const char *slash;
Jeff King1eb10f42012-01-09 23:44:30 -050042 const char *dir;
René Scharfed13a0a92014-07-28 20:25:40 +020043 struct strbuf cwd = STRBUF_INIT;
Jeff King1eb10f42012-01-09 23:44:30 -050044
Jeff Hostetler77e522c2021-03-15 21:08:26 +000045 if (disallow_chdir) {
46 errno = ENAMETOOLONG;
47 return -1;
48 }
49
50 slash = find_last_dir_sep(path);
Jeff King1eb10f42012-01-09 23:44:30 -050051 if (!slash) {
52 errno = ENAMETOOLONG;
53 return -1;
54 }
55
56 dir = path;
57 path = slash + 1;
58 size = strlen(path) + 1;
59 if (size > sizeof(sa->sun_path)) {
60 errno = ENAMETOOLONG;
61 return -1;
62 }
René Scharfed13a0a92014-07-28 20:25:40 +020063 if (strbuf_getcwd(&cwd))
Jeff King1eb10f42012-01-09 23:44:30 -050064 return -1;
René Scharfed13a0a92014-07-28 20:25:40 +020065 ctx->orig_dir = strbuf_detach(&cwd, NULL);
Jeff King1eb10f42012-01-09 23:44:30 -050066 if (chdir_len(dir, slash - dir) < 0)
67 return -1;
68 }
69
Jeff Kinge2770972011-12-10 05:34:14 -050070 memset(sa, 0, sizeof(*sa));
71 sa->sun_family = AF_UNIX;
72 memcpy(sa->sun_path, path, size);
Jeff King1eb10f42012-01-09 23:44:30 -050073 return 0;
Jeff Kinge2770972011-12-10 05:34:14 -050074}
75
Jeff Hostetler77e522c2021-03-15 21:08:26 +000076int unix_stream_connect(const char *path, int disallow_chdir)
Jeff Kinge2770972011-12-10 05:34:14 -050077{
Jeff Hostetler4f98ce52021-03-15 21:08:24 +000078 int fd = -1, saved_errno;
Jeff Kinge2770972011-12-10 05:34:14 -050079 struct sockaddr_un sa;
Jeff King1eb10f42012-01-09 23:44:30 -050080 struct unix_sockaddr_context ctx;
Jeff Kinge2770972011-12-10 05:34:14 -050081
Jeff Hostetler77e522c2021-03-15 21:08:26 +000082 if (unix_sockaddr_init(&sa, path, &ctx, disallow_chdir) < 0)
Jeff King1eb10f42012-01-09 23:44:30 -050083 return -1;
Jeff Hostetler4f98ce52021-03-15 21:08:24 +000084 fd = socket(AF_UNIX, SOCK_STREAM, 0);
85 if (fd < 0)
86 goto fail;
87
Jonathan Nieder06121a02012-01-11 17:50:10 -060088 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
89 goto fail;
Jeff King1eb10f42012-01-09 23:44:30 -050090 unix_sockaddr_cleanup(&ctx);
Jeff Kinge2770972011-12-10 05:34:14 -050091 return fd;
Jonathan Nieder06121a02012-01-11 17:50:10 -060092
93fail:
94 saved_errno = errno;
Jeff Hostetler4f98ce52021-03-15 21:08:24 +000095 if (fd != -1)
96 close(fd);
Jonathan Nieder06121a02012-01-11 17:50:10 -060097 unix_sockaddr_cleanup(&ctx);
Jonathan Nieder06121a02012-01-11 17:50:10 -060098 errno = saved_errno;
99 return -1;
Jeff Kinge2770972011-12-10 05:34:14 -0500100}
101
Jeff Hostetler55144cc2021-03-15 21:08:25 +0000102int unix_stream_listen(const char *path,
103 const struct unix_stream_listen_opts *opts)
Jeff Kinge2770972011-12-10 05:34:14 -0500104{
Jeff Hostetler4f98ce52021-03-15 21:08:24 +0000105 int fd = -1, saved_errno;
Jeff Hostetler55144cc2021-03-15 21:08:25 +0000106 int backlog;
Jeff Kinge2770972011-12-10 05:34:14 -0500107 struct sockaddr_un sa;
Jeff King1eb10f42012-01-09 23:44:30 -0500108 struct unix_sockaddr_context ctx;
Jeff Kinge2770972011-12-10 05:34:14 -0500109
René Scharfe2869b3e2014-07-20 10:00:41 +0200110 unlink(path);
111
Jeff Hostetler77e522c2021-03-15 21:08:26 +0000112 if (unix_sockaddr_init(&sa, path, &ctx, opts->disallow_chdir) < 0)
Jeff King1eb10f42012-01-09 23:44:30 -0500113 return -1;
Jeff Hostetler4f98ce52021-03-15 21:08:24 +0000114 fd = socket(AF_UNIX, SOCK_STREAM, 0);
115 if (fd < 0)
116 goto fail;
Jeff Kinge2770972011-12-10 05:34:14 -0500117
Jonathan Nieder06121a02012-01-11 17:50:10 -0600118 if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
119 goto fail;
Jeff Kinge2770972011-12-10 05:34:14 -0500120
Jeff Hostetler55144cc2021-03-15 21:08:25 +0000121 backlog = opts->listen_backlog_size;
122 if (backlog <= 0)
123 backlog = DEFAULT_UNIX_STREAM_LISTEN_BACKLOG;
124 if (listen(fd, backlog) < 0)
Jonathan Nieder06121a02012-01-11 17:50:10 -0600125 goto fail;
Jeff Kinge2770972011-12-10 05:34:14 -0500126
Jeff King1eb10f42012-01-09 23:44:30 -0500127 unix_sockaddr_cleanup(&ctx);
Jeff Kinge2770972011-12-10 05:34:14 -0500128 return fd;
Jonathan Nieder06121a02012-01-11 17:50:10 -0600129
130fail:
131 saved_errno = errno;
Jeff Hostetler4f98ce52021-03-15 21:08:24 +0000132 if (fd != -1)
133 close(fd);
Jonathan Nieder06121a02012-01-11 17:50:10 -0600134 unix_sockaddr_cleanup(&ctx);
Jonathan Nieder06121a02012-01-11 17:50:10 -0600135 errno = saved_errno;
136 return -1;
Jeff Kinge2770972011-12-10 05:34:14 -0500137}