Elijah Newren | fc7bd51 | 2023-02-24 00:09:34 +0000 | [diff] [blame] | 1 | #include "git-compat-util.h" |
| 2 | #include "strbuf.h" |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 3 | #include "unix-socket.h" |
| 4 | |
Jeff Hostetler | 55144cc | 2021-03-15 21:08:25 +0000 | [diff] [blame] | 5 | #define DEFAULT_UNIX_STREAM_LISTEN_BACKLOG (5) |
| 6 | |
Jeff King | 1eb10f4 | 2012-01-09 23:44:30 -0500 | [diff] [blame] | 7 | static 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 | |
| 15 | struct unix_sockaddr_context { |
René Scharfe | d13a0a9 | 2014-07-28 20:25:40 +0200 | [diff] [blame] | 16 | char *orig_dir; |
Jeff King | 1eb10f4 | 2012-01-09 23:44:30 -0500 | [diff] [blame] | 17 | }; |
| 18 | |
| 19 | static void unix_sockaddr_cleanup(struct unix_sockaddr_context *ctx) |
| 20 | { |
René Scharfe | d13a0a9 | 2014-07-28 20:25:40 +0200 | [diff] [blame] | 21 | if (!ctx->orig_dir) |
Jeff King | 1eb10f4 | 2012-01-09 23:44:30 -0500 | [diff] [blame] | 22 | 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é Scharfe | d13a0a9 | 2014-07-28 20:25:40 +0200 | [diff] [blame] | 30 | free(ctx->orig_dir); |
Jeff King | 1eb10f4 | 2012-01-09 23:44:30 -0500 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | static int unix_sockaddr_init(struct sockaddr_un *sa, const char *path, |
Jeff Hostetler | 77e522c | 2021-03-15 21:08:26 +0000 | [diff] [blame] | 34 | struct unix_sockaddr_context *ctx, |
| 35 | int disallow_chdir) |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 36 | { |
| 37 | int size = strlen(path) + 1; |
Jeff King | 1eb10f4 | 2012-01-09 23:44:30 -0500 | [diff] [blame] | 38 | |
René Scharfe | d13a0a9 | 2014-07-28 20:25:40 +0200 | [diff] [blame] | 39 | ctx->orig_dir = NULL; |
Jeff King | 1eb10f4 | 2012-01-09 23:44:30 -0500 | [diff] [blame] | 40 | if (size > sizeof(sa->sun_path)) { |
Jeff Hostetler | 77e522c | 2021-03-15 21:08:26 +0000 | [diff] [blame] | 41 | const char *slash; |
Jeff King | 1eb10f4 | 2012-01-09 23:44:30 -0500 | [diff] [blame] | 42 | const char *dir; |
René Scharfe | d13a0a9 | 2014-07-28 20:25:40 +0200 | [diff] [blame] | 43 | struct strbuf cwd = STRBUF_INIT; |
Jeff King | 1eb10f4 | 2012-01-09 23:44:30 -0500 | [diff] [blame] | 44 | |
Jeff Hostetler | 77e522c | 2021-03-15 21:08:26 +0000 | [diff] [blame] | 45 | if (disallow_chdir) { |
| 46 | errno = ENAMETOOLONG; |
| 47 | return -1; |
| 48 | } |
| 49 | |
| 50 | slash = find_last_dir_sep(path); |
Jeff King | 1eb10f4 | 2012-01-09 23:44:30 -0500 | [diff] [blame] | 51 | 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é Scharfe | d13a0a9 | 2014-07-28 20:25:40 +0200 | [diff] [blame] | 63 | if (strbuf_getcwd(&cwd)) |
Jeff King | 1eb10f4 | 2012-01-09 23:44:30 -0500 | [diff] [blame] | 64 | return -1; |
René Scharfe | d13a0a9 | 2014-07-28 20:25:40 +0200 | [diff] [blame] | 65 | ctx->orig_dir = strbuf_detach(&cwd, NULL); |
Jeff King | 1eb10f4 | 2012-01-09 23:44:30 -0500 | [diff] [blame] | 66 | if (chdir_len(dir, slash - dir) < 0) |
| 67 | return -1; |
| 68 | } |
| 69 | |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 70 | memset(sa, 0, sizeof(*sa)); |
| 71 | sa->sun_family = AF_UNIX; |
| 72 | memcpy(sa->sun_path, path, size); |
Jeff King | 1eb10f4 | 2012-01-09 23:44:30 -0500 | [diff] [blame] | 73 | return 0; |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 74 | } |
| 75 | |
Jeff Hostetler | 77e522c | 2021-03-15 21:08:26 +0000 | [diff] [blame] | 76 | int unix_stream_connect(const char *path, int disallow_chdir) |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 77 | { |
Jeff Hostetler | 4f98ce5 | 2021-03-15 21:08:24 +0000 | [diff] [blame] | 78 | int fd = -1, saved_errno; |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 79 | struct sockaddr_un sa; |
Jeff King | 1eb10f4 | 2012-01-09 23:44:30 -0500 | [diff] [blame] | 80 | struct unix_sockaddr_context ctx; |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 81 | |
Jeff Hostetler | 77e522c | 2021-03-15 21:08:26 +0000 | [diff] [blame] | 82 | if (unix_sockaddr_init(&sa, path, &ctx, disallow_chdir) < 0) |
Jeff King | 1eb10f4 | 2012-01-09 23:44:30 -0500 | [diff] [blame] | 83 | return -1; |
Jeff Hostetler | 4f98ce5 | 2021-03-15 21:08:24 +0000 | [diff] [blame] | 84 | fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 85 | if (fd < 0) |
| 86 | goto fail; |
| 87 | |
Jonathan Nieder | 06121a0 | 2012-01-11 17:50:10 -0600 | [diff] [blame] | 88 | if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) |
| 89 | goto fail; |
Jeff King | 1eb10f4 | 2012-01-09 23:44:30 -0500 | [diff] [blame] | 90 | unix_sockaddr_cleanup(&ctx); |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 91 | return fd; |
Jonathan Nieder | 06121a0 | 2012-01-11 17:50:10 -0600 | [diff] [blame] | 92 | |
| 93 | fail: |
| 94 | saved_errno = errno; |
Jeff Hostetler | 4f98ce5 | 2021-03-15 21:08:24 +0000 | [diff] [blame] | 95 | if (fd != -1) |
| 96 | close(fd); |
Jonathan Nieder | 06121a0 | 2012-01-11 17:50:10 -0600 | [diff] [blame] | 97 | unix_sockaddr_cleanup(&ctx); |
Jonathan Nieder | 06121a0 | 2012-01-11 17:50:10 -0600 | [diff] [blame] | 98 | errno = saved_errno; |
| 99 | return -1; |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 100 | } |
| 101 | |
Jeff Hostetler | 55144cc | 2021-03-15 21:08:25 +0000 | [diff] [blame] | 102 | int unix_stream_listen(const char *path, |
| 103 | const struct unix_stream_listen_opts *opts) |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 104 | { |
Jeff Hostetler | 4f98ce5 | 2021-03-15 21:08:24 +0000 | [diff] [blame] | 105 | int fd = -1, saved_errno; |
Jeff Hostetler | 55144cc | 2021-03-15 21:08:25 +0000 | [diff] [blame] | 106 | int backlog; |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 107 | struct sockaddr_un sa; |
Jeff King | 1eb10f4 | 2012-01-09 23:44:30 -0500 | [diff] [blame] | 108 | struct unix_sockaddr_context ctx; |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 109 | |
René Scharfe | 2869b3e | 2014-07-20 10:00:41 +0200 | [diff] [blame] | 110 | unlink(path); |
| 111 | |
Jeff Hostetler | 77e522c | 2021-03-15 21:08:26 +0000 | [diff] [blame] | 112 | if (unix_sockaddr_init(&sa, path, &ctx, opts->disallow_chdir) < 0) |
Jeff King | 1eb10f4 | 2012-01-09 23:44:30 -0500 | [diff] [blame] | 113 | return -1; |
Jeff Hostetler | 4f98ce5 | 2021-03-15 21:08:24 +0000 | [diff] [blame] | 114 | fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 115 | if (fd < 0) |
| 116 | goto fail; |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 117 | |
Jonathan Nieder | 06121a0 | 2012-01-11 17:50:10 -0600 | [diff] [blame] | 118 | if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) |
| 119 | goto fail; |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 120 | |
Jeff Hostetler | 55144cc | 2021-03-15 21:08:25 +0000 | [diff] [blame] | 121 | backlog = opts->listen_backlog_size; |
| 122 | if (backlog <= 0) |
| 123 | backlog = DEFAULT_UNIX_STREAM_LISTEN_BACKLOG; |
| 124 | if (listen(fd, backlog) < 0) |
Jonathan Nieder | 06121a0 | 2012-01-11 17:50:10 -0600 | [diff] [blame] | 125 | goto fail; |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 126 | |
Jeff King | 1eb10f4 | 2012-01-09 23:44:30 -0500 | [diff] [blame] | 127 | unix_sockaddr_cleanup(&ctx); |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 128 | return fd; |
Jonathan Nieder | 06121a0 | 2012-01-11 17:50:10 -0600 | [diff] [blame] | 129 | |
| 130 | fail: |
| 131 | saved_errno = errno; |
Jeff Hostetler | 4f98ce5 | 2021-03-15 21:08:24 +0000 | [diff] [blame] | 132 | if (fd != -1) |
| 133 | close(fd); |
Jonathan Nieder | 06121a0 | 2012-01-11 17:50:10 -0600 | [diff] [blame] | 134 | unix_sockaddr_cleanup(&ctx); |
Jonathan Nieder | 06121a0 | 2012-01-11 17:50:10 -0600 | [diff] [blame] | 135 | errno = saved_errno; |
| 136 | return -1; |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 137 | } |