blob: 19ed48be9902f321285bfbec3feaf8357fd5e9b9 [file] [log] [blame]
Jeff Kinge2770972011-12-10 05:34:14 -05001#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
Jeff King1eb10f42012-01-09 23:44:30 -050012static 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 {
René Scharfed13a0a92014-07-28 20:25:40 +020021 char *orig_dir;
Jeff King1eb10f42012-01-09 23:44:30 -050022};
23
24static void unix_sockaddr_cleanup(struct unix_sockaddr_context *ctx)
25{
René Scharfed13a0a92014-07-28 20:25:40 +020026 if (!ctx->orig_dir)
Jeff King1eb10f42012-01-09 23:44:30 -050027 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");
René Scharfed13a0a92014-07-28 20:25:40 +020035 free(ctx->orig_dir);
Jeff King1eb10f42012-01-09 23:44:30 -050036}
37
38static int unix_sockaddr_init(struct sockaddr_un *sa, const char *path,
39 struct unix_sockaddr_context *ctx)
Jeff Kinge2770972011-12-10 05:34:14 -050040{
41 int size = strlen(path) + 1;
Jeff King1eb10f42012-01-09 23:44:30 -050042
René Scharfed13a0a92014-07-28 20:25:40 +020043 ctx->orig_dir = NULL;
Jeff King1eb10f42012-01-09 23:44:30 -050044 if (size > sizeof(sa->sun_path)) {
45 const char *slash = find_last_dir_sep(path);
46 const char *dir;
René Scharfed13a0a92014-07-28 20:25:40 +020047 struct strbuf cwd = STRBUF_INIT;
Jeff King1eb10f42012-01-09 23:44:30 -050048
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 }
René Scharfed13a0a92014-07-28 20:25:40 +020061 if (strbuf_getcwd(&cwd))
Jeff King1eb10f42012-01-09 23:44:30 -050062 return -1;
René Scharfed13a0a92014-07-28 20:25:40 +020063 ctx->orig_dir = strbuf_detach(&cwd, NULL);
Jeff King1eb10f42012-01-09 23:44:30 -050064 if (chdir_len(dir, slash - dir) < 0)
65 return -1;
66 }
67
Jeff Kinge2770972011-12-10 05:34:14 -050068 memset(sa, 0, sizeof(*sa));
69 sa->sun_family = AF_UNIX;
70 memcpy(sa->sun_path, path, size);
Jeff King1eb10f42012-01-09 23:44:30 -050071 return 0;
Jeff Kinge2770972011-12-10 05:34:14 -050072}
73
74int unix_stream_connect(const char *path)
75{
Jonathan Nieder06121a02012-01-11 17:50:10 -060076 int fd, saved_errno;
Jeff Kinge2770972011-12-10 05:34:14 -050077 struct sockaddr_un sa;
Jeff King1eb10f42012-01-09 23:44:30 -050078 struct unix_sockaddr_context ctx;
Jeff Kinge2770972011-12-10 05:34:14 -050079
Jeff King1eb10f42012-01-09 23:44:30 -050080 if (unix_sockaddr_init(&sa, path, &ctx) < 0)
81 return -1;
Jeff Kinge2770972011-12-10 05:34:14 -050082 fd = unix_stream_socket();
Jonathan Nieder06121a02012-01-11 17:50:10 -060083 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
84 goto fail;
Jeff King1eb10f42012-01-09 23:44:30 -050085 unix_sockaddr_cleanup(&ctx);
Jeff Kinge2770972011-12-10 05:34:14 -050086 return fd;
Jonathan Nieder06121a02012-01-11 17:50:10 -060087
88fail:
89 saved_errno = errno;
90 unix_sockaddr_cleanup(&ctx);
91 close(fd);
92 errno = saved_errno;
93 return -1;
Jeff Kinge2770972011-12-10 05:34:14 -050094}
95
96int unix_stream_listen(const char *path)
97{
Jonathan Nieder06121a02012-01-11 17:50:10 -060098 int fd, saved_errno;
Jeff Kinge2770972011-12-10 05:34:14 -050099 struct sockaddr_un sa;
Jeff King1eb10f42012-01-09 23:44:30 -0500100 struct unix_sockaddr_context ctx;
Jeff Kinge2770972011-12-10 05:34:14 -0500101
René Scharfe2869b3e2014-07-20 10:00:41 +0200102 unlink(path);
103
Jeff King1eb10f42012-01-09 23:44:30 -0500104 if (unix_sockaddr_init(&sa, path, &ctx) < 0)
105 return -1;
Jeff Kinge2770972011-12-10 05:34:14 -0500106 fd = unix_stream_socket();
107
Jonathan Nieder06121a02012-01-11 17:50:10 -0600108 if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
109 goto fail;
Jeff Kinge2770972011-12-10 05:34:14 -0500110
Jonathan Nieder06121a02012-01-11 17:50:10 -0600111 if (listen(fd, 5) < 0)
112 goto fail;
Jeff Kinge2770972011-12-10 05:34:14 -0500113
Jeff King1eb10f42012-01-09 23:44:30 -0500114 unix_sockaddr_cleanup(&ctx);
Jeff Kinge2770972011-12-10 05:34:14 -0500115 return fd;
Jonathan Nieder06121a02012-01-11 17:50:10 -0600116
117fail:
118 saved_errno = errno;
119 unix_sockaddr_cleanup(&ctx);
120 close(fd);
121 errno = saved_errno;
122 return -1;
Jeff Kinge2770972011-12-10 05:34:14 -0500123}