blob: e0be1badb58dee8af8e28c4a4486bf0fdba18a9c [file] [log] [blame]
Jeff Kinge2770972011-12-10 05:34:14 -05001#include "cache.h"
2#include "unix-socket.h"
3
Jeff Hostetler55144cc2021-03-15 21:08:25 +00004#define DEFAULT_UNIX_STREAM_LISTEN_BACKLOG (5)
5
Jeff King1eb10f42012-01-09 23:44:30 -05006static int chdir_len(const char *orig, int len)
7{
8 char *path = xmemdupz(orig, len);
9 int r = chdir(path);
10 free(path);
11 return r;
12}
13
14struct unix_sockaddr_context {
René Scharfed13a0a92014-07-28 20:25:40 +020015 char *orig_dir;
Jeff King1eb10f42012-01-09 23:44:30 -050016};
17
18static void unix_sockaddr_cleanup(struct unix_sockaddr_context *ctx)
19{
René Scharfed13a0a92014-07-28 20:25:40 +020020 if (!ctx->orig_dir)
Jeff King1eb10f42012-01-09 23:44:30 -050021 return;
22 /*
23 * If we fail, we can't just return an error, since we have
24 * moved the cwd of the whole process, which could confuse calling
25 * code. We are better off to just die.
26 */
27 if (chdir(ctx->orig_dir) < 0)
28 die("unable to restore original working directory");
René Scharfed13a0a92014-07-28 20:25:40 +020029 free(ctx->orig_dir);
Jeff King1eb10f42012-01-09 23:44:30 -050030}
31
32static int unix_sockaddr_init(struct sockaddr_un *sa, const char *path,
Jeff Hostetler77e522c2021-03-15 21:08:26 +000033 struct unix_sockaddr_context *ctx,
34 int disallow_chdir)
Jeff Kinge2770972011-12-10 05:34:14 -050035{
36 int size = strlen(path) + 1;
Jeff King1eb10f42012-01-09 23:44:30 -050037
René Scharfed13a0a92014-07-28 20:25:40 +020038 ctx->orig_dir = NULL;
Jeff King1eb10f42012-01-09 23:44:30 -050039 if (size > sizeof(sa->sun_path)) {
Jeff Hostetler77e522c2021-03-15 21:08:26 +000040 const char *slash;
Jeff King1eb10f42012-01-09 23:44:30 -050041 const char *dir;
René Scharfed13a0a92014-07-28 20:25:40 +020042 struct strbuf cwd = STRBUF_INIT;
Jeff King1eb10f42012-01-09 23:44:30 -050043
Jeff Hostetler77e522c2021-03-15 21:08:26 +000044 if (disallow_chdir) {
45 errno = ENAMETOOLONG;
46 return -1;
47 }
48
49 slash = find_last_dir_sep(path);
Jeff King1eb10f42012-01-09 23:44:30 -050050 if (!slash) {
51 errno = ENAMETOOLONG;
52 return -1;
53 }
54
55 dir = path;
56 path = slash + 1;
57 size = strlen(path) + 1;
58 if (size > sizeof(sa->sun_path)) {
59 errno = ENAMETOOLONG;
60 return -1;
61 }
René Scharfed13a0a92014-07-28 20:25:40 +020062 if (strbuf_getcwd(&cwd))
Jeff King1eb10f42012-01-09 23:44:30 -050063 return -1;
René Scharfed13a0a92014-07-28 20:25:40 +020064 ctx->orig_dir = strbuf_detach(&cwd, NULL);
Jeff King1eb10f42012-01-09 23:44:30 -050065 if (chdir_len(dir, slash - dir) < 0)
66 return -1;
67 }
68
Jeff Kinge2770972011-12-10 05:34:14 -050069 memset(sa, 0, sizeof(*sa));
70 sa->sun_family = AF_UNIX;
71 memcpy(sa->sun_path, path, size);
Jeff King1eb10f42012-01-09 23:44:30 -050072 return 0;
Jeff Kinge2770972011-12-10 05:34:14 -050073}
74
Jeff Hostetler77e522c2021-03-15 21:08:26 +000075int unix_stream_connect(const char *path, int disallow_chdir)
Jeff Kinge2770972011-12-10 05:34:14 -050076{
Jeff Hostetler4f98ce52021-03-15 21:08:24 +000077 int fd = -1, saved_errno;
Jeff Kinge2770972011-12-10 05:34:14 -050078 struct sockaddr_un sa;
Jeff King1eb10f42012-01-09 23:44:30 -050079 struct unix_sockaddr_context ctx;
Jeff Kinge2770972011-12-10 05:34:14 -050080
Jeff Hostetler77e522c2021-03-15 21:08:26 +000081 if (unix_sockaddr_init(&sa, path, &ctx, disallow_chdir) < 0)
Jeff King1eb10f42012-01-09 23:44:30 -050082 return -1;
Jeff Hostetler4f98ce52021-03-15 21:08:24 +000083 fd = socket(AF_UNIX, SOCK_STREAM, 0);
84 if (fd < 0)
85 goto fail;
86
Jonathan Nieder06121a02012-01-11 17:50:10 -060087 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
88 goto fail;
Jeff King1eb10f42012-01-09 23:44:30 -050089 unix_sockaddr_cleanup(&ctx);
Jeff Kinge2770972011-12-10 05:34:14 -050090 return fd;
Jonathan Nieder06121a02012-01-11 17:50:10 -060091
92fail:
93 saved_errno = errno;
Jeff Hostetler4f98ce52021-03-15 21:08:24 +000094 if (fd != -1)
95 close(fd);
Jonathan Nieder06121a02012-01-11 17:50:10 -060096 unix_sockaddr_cleanup(&ctx);
Jonathan Nieder06121a02012-01-11 17:50:10 -060097 errno = saved_errno;
98 return -1;
Jeff Kinge2770972011-12-10 05:34:14 -050099}
100
Jeff Hostetler55144cc2021-03-15 21:08:25 +0000101int unix_stream_listen(const char *path,
102 const struct unix_stream_listen_opts *opts)
Jeff Kinge2770972011-12-10 05:34:14 -0500103{
Jeff Hostetler4f98ce52021-03-15 21:08:24 +0000104 int fd = -1, saved_errno;
Jeff Hostetler55144cc2021-03-15 21:08:25 +0000105 int backlog;
Jeff Kinge2770972011-12-10 05:34:14 -0500106 struct sockaddr_un sa;
Jeff King1eb10f42012-01-09 23:44:30 -0500107 struct unix_sockaddr_context ctx;
Jeff Kinge2770972011-12-10 05:34:14 -0500108
René Scharfe2869b3e2014-07-20 10:00:41 +0200109 unlink(path);
110
Jeff Hostetler77e522c2021-03-15 21:08:26 +0000111 if (unix_sockaddr_init(&sa, path, &ctx, opts->disallow_chdir) < 0)
Jeff King1eb10f42012-01-09 23:44:30 -0500112 return -1;
Jeff Hostetler4f98ce52021-03-15 21:08:24 +0000113 fd = socket(AF_UNIX, SOCK_STREAM, 0);
114 if (fd < 0)
115 goto fail;
Jeff Kinge2770972011-12-10 05:34:14 -0500116
Jonathan Nieder06121a02012-01-11 17:50:10 -0600117 if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
118 goto fail;
Jeff Kinge2770972011-12-10 05:34:14 -0500119
Jeff Hostetler55144cc2021-03-15 21:08:25 +0000120 backlog = opts->listen_backlog_size;
121 if (backlog <= 0)
122 backlog = DEFAULT_UNIX_STREAM_LISTEN_BACKLOG;
123 if (listen(fd, backlog) < 0)
Jonathan Nieder06121a02012-01-11 17:50:10 -0600124 goto fail;
Jeff Kinge2770972011-12-10 05:34:14 -0500125
Jeff King1eb10f42012-01-09 23:44:30 -0500126 unix_sockaddr_cleanup(&ctx);
Jeff Kinge2770972011-12-10 05:34:14 -0500127 return fd;
Jonathan Nieder06121a02012-01-11 17:50:10 -0600128
129fail:
130 saved_errno = errno;
Jeff Hostetler4f98ce52021-03-15 21:08:24 +0000131 if (fd != -1)
132 close(fd);
Jonathan Nieder06121a02012-01-11 17:50:10 -0600133 unix_sockaddr_cleanup(&ctx);
Jonathan Nieder06121a02012-01-11 17:50:10 -0600134 errno = saved_errno;
135 return -1;
Jeff Kinge2770972011-12-10 05:34:14 -0500136}