blob: 01f119f9700791ddc59a81cdc80fd365e678622a [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 {
21 char orig_dir[PATH_MAX];
22};
23
24static void unix_sockaddr_cleanup(struct unix_sockaddr_context *ctx)
25{
26 if (!ctx->orig_dir[0])
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");
35}
36
37static int unix_sockaddr_init(struct sockaddr_un *sa, const char *path,
38 struct unix_sockaddr_context *ctx)
Jeff Kinge2770972011-12-10 05:34:14 -050039{
40 int size = strlen(path) + 1;
Jeff King1eb10f42012-01-09 23:44:30 -050041
42 ctx->orig_dir[0] = '\0';
43 if (size > sizeof(sa->sun_path)) {
44 const char *slash = find_last_dir_sep(path);
45 const char *dir;
46
47 if (!slash) {
48 errno = ENAMETOOLONG;
49 return -1;
50 }
51
52 dir = path;
53 path = slash + 1;
54 size = strlen(path) + 1;
55 if (size > sizeof(sa->sun_path)) {
56 errno = ENAMETOOLONG;
57 return -1;
58 }
59
60 if (!getcwd(ctx->orig_dir, sizeof(ctx->orig_dir))) {
61 errno = ENAMETOOLONG;
62 return -1;
63 }
64 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
Jeff King1eb10f42012-01-09 23:44:30 -0500102 if (unix_sockaddr_init(&sa, path, &ctx) < 0)
103 return -1;
Jeff Kinge2770972011-12-10 05:34:14 -0500104 fd = unix_stream_socket();
105
106 unlink(path);
Jonathan Nieder06121a02012-01-11 17:50:10 -0600107 if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
108 goto fail;
Jeff Kinge2770972011-12-10 05:34:14 -0500109
Jonathan Nieder06121a02012-01-11 17:50:10 -0600110 if (listen(fd, 5) < 0)
111 goto fail;
Jeff Kinge2770972011-12-10 05:34:14 -0500112
Jeff King1eb10f42012-01-09 23:44:30 -0500113 unix_sockaddr_cleanup(&ctx);
Jeff Kinge2770972011-12-10 05:34:14 -0500114 return fd;
Jonathan Nieder06121a02012-01-11 17:50:10 -0600115
116fail:
117 saved_errno = errno;
118 unix_sockaddr_cleanup(&ctx);
119 close(fd);
120 errno = saved_errno;
121 return -1;
Jeff Kinge2770972011-12-10 05:34:14 -0500122}