2023-02-24 03:09:34 +03:00
|
|
|
#include "git-compat-util.h"
|
|
|
|
#include "strbuf.h"
|
2011-12-10 14:34:14 +04:00
|
|
|
#include "unix-socket.h"
|
|
|
|
|
2021-03-16 00:08:25 +03:00
|
|
|
#define DEFAULT_UNIX_STREAM_LISTEN_BACKLOG (5)
|
|
|
|
|
2012-01-10 08:44:30 +04:00
|
|
|
static int chdir_len(const char *orig, int len)
|
|
|
|
{
|
|
|
|
char *path = xmemdupz(orig, len);
|
|
|
|
int r = chdir(path);
|
|
|
|
free(path);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct unix_sockaddr_context {
|
2014-07-28 22:25:40 +04:00
|
|
|
char *orig_dir;
|
2012-01-10 08:44:30 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static void unix_sockaddr_cleanup(struct unix_sockaddr_context *ctx)
|
|
|
|
{
|
2014-07-28 22:25:40 +04:00
|
|
|
if (!ctx->orig_dir)
|
2012-01-10 08:44:30 +04:00
|
|
|
return;
|
|
|
|
/*
|
|
|
|
* If we fail, we can't just return an error, since we have
|
|
|
|
* moved the cwd of the whole process, which could confuse calling
|
|
|
|
* code. We are better off to just die.
|
|
|
|
*/
|
|
|
|
if (chdir(ctx->orig_dir) < 0)
|
|
|
|
die("unable to restore original working directory");
|
2014-07-28 22:25:40 +04:00
|
|
|
free(ctx->orig_dir);
|
2012-01-10 08:44:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int unix_sockaddr_init(struct sockaddr_un *sa, const char *path,
|
2021-03-16 00:08:26 +03:00
|
|
|
struct unix_sockaddr_context *ctx,
|
|
|
|
int disallow_chdir)
|
2011-12-10 14:34:14 +04:00
|
|
|
{
|
|
|
|
int size = strlen(path) + 1;
|
2012-01-10 08:44:30 +04:00
|
|
|
|
2014-07-28 22:25:40 +04:00
|
|
|
ctx->orig_dir = NULL;
|
2012-01-10 08:44:30 +04:00
|
|
|
if (size > sizeof(sa->sun_path)) {
|
2021-03-16 00:08:26 +03:00
|
|
|
const char *slash;
|
2012-01-10 08:44:30 +04:00
|
|
|
const char *dir;
|
2014-07-28 22:25:40 +04:00
|
|
|
struct strbuf cwd = STRBUF_INIT;
|
2012-01-10 08:44:30 +04:00
|
|
|
|
2021-03-16 00:08:26 +03:00
|
|
|
if (disallow_chdir) {
|
|
|
|
errno = ENAMETOOLONG;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
slash = find_last_dir_sep(path);
|
2012-01-10 08:44:30 +04:00
|
|
|
if (!slash) {
|
|
|
|
errno = ENAMETOOLONG;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
dir = path;
|
|
|
|
path = slash + 1;
|
|
|
|
size = strlen(path) + 1;
|
|
|
|
if (size > sizeof(sa->sun_path)) {
|
|
|
|
errno = ENAMETOOLONG;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-07-28 22:25:40 +04:00
|
|
|
if (strbuf_getcwd(&cwd))
|
2012-01-10 08:44:30 +04:00
|
|
|
return -1;
|
2014-07-28 22:25:40 +04:00
|
|
|
ctx->orig_dir = strbuf_detach(&cwd, NULL);
|
2012-01-10 08:44:30 +04:00
|
|
|
if (chdir_len(dir, slash - dir) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-12-10 14:34:14 +04:00
|
|
|
memset(sa, 0, sizeof(*sa));
|
|
|
|
sa->sun_family = AF_UNIX;
|
|
|
|
memcpy(sa->sun_path, path, size);
|
2012-01-10 08:44:30 +04:00
|
|
|
return 0;
|
2011-12-10 14:34:14 +04:00
|
|
|
}
|
|
|
|
|
2021-03-16 00:08:26 +03:00
|
|
|
int unix_stream_connect(const char *path, int disallow_chdir)
|
2011-12-10 14:34:14 +04:00
|
|
|
{
|
2021-03-16 00:08:24 +03:00
|
|
|
int fd = -1, saved_errno;
|
2011-12-10 14:34:14 +04:00
|
|
|
struct sockaddr_un sa;
|
2012-01-10 08:44:30 +04:00
|
|
|
struct unix_sockaddr_context ctx;
|
2011-12-10 14:34:14 +04:00
|
|
|
|
2021-03-16 00:08:26 +03:00
|
|
|
if (unix_sockaddr_init(&sa, path, &ctx, disallow_chdir) < 0)
|
2012-01-10 08:44:30 +04:00
|
|
|
return -1;
|
2021-03-16 00:08:24 +03:00
|
|
|
fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (fd < 0)
|
|
|
|
goto fail;
|
|
|
|
|
2012-01-12 03:50:10 +04:00
|
|
|
if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
|
|
|
|
goto fail;
|
2012-01-10 08:44:30 +04:00
|
|
|
unix_sockaddr_cleanup(&ctx);
|
2011-12-10 14:34:14 +04:00
|
|
|
return fd;
|
2012-01-12 03:50:10 +04:00
|
|
|
|
|
|
|
fail:
|
|
|
|
saved_errno = errno;
|
2021-03-16 00:08:24 +03:00
|
|
|
if (fd != -1)
|
|
|
|
close(fd);
|
2012-01-12 03:50:10 +04:00
|
|
|
unix_sockaddr_cleanup(&ctx);
|
|
|
|
errno = saved_errno;
|
|
|
|
return -1;
|
2011-12-10 14:34:14 +04:00
|
|
|
}
|
|
|
|
|
2021-03-16 00:08:25 +03:00
|
|
|
int unix_stream_listen(const char *path,
|
|
|
|
const struct unix_stream_listen_opts *opts)
|
2011-12-10 14:34:14 +04:00
|
|
|
{
|
2021-03-16 00:08:24 +03:00
|
|
|
int fd = -1, saved_errno;
|
2021-03-16 00:08:25 +03:00
|
|
|
int backlog;
|
2011-12-10 14:34:14 +04:00
|
|
|
struct sockaddr_un sa;
|
2012-01-10 08:44:30 +04:00
|
|
|
struct unix_sockaddr_context ctx;
|
2011-12-10 14:34:14 +04:00
|
|
|
|
2014-07-20 12:00:41 +04:00
|
|
|
unlink(path);
|
|
|
|
|
2021-03-16 00:08:26 +03:00
|
|
|
if (unix_sockaddr_init(&sa, path, &ctx, opts->disallow_chdir) < 0)
|
2012-01-10 08:44:30 +04:00
|
|
|
return -1;
|
2021-03-16 00:08:24 +03:00
|
|
|
fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (fd < 0)
|
|
|
|
goto fail;
|
2011-12-10 14:34:14 +04:00
|
|
|
|
2012-01-12 03:50:10 +04:00
|
|
|
if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
|
|
|
|
goto fail;
|
2011-12-10 14:34:14 +04:00
|
|
|
|
2021-03-16 00:08:25 +03:00
|
|
|
backlog = opts->listen_backlog_size;
|
|
|
|
if (backlog <= 0)
|
|
|
|
backlog = DEFAULT_UNIX_STREAM_LISTEN_BACKLOG;
|
|
|
|
if (listen(fd, backlog) < 0)
|
2012-01-12 03:50:10 +04:00
|
|
|
goto fail;
|
2011-12-10 14:34:14 +04:00
|
|
|
|
2012-01-10 08:44:30 +04:00
|
|
|
unix_sockaddr_cleanup(&ctx);
|
2011-12-10 14:34:14 +04:00
|
|
|
return fd;
|
2012-01-12 03:50:10 +04:00
|
|
|
|
|
|
|
fail:
|
|
|
|
saved_errno = errno;
|
2021-03-16 00:08:24 +03:00
|
|
|
if (fd != -1)
|
|
|
|
close(fd);
|
2012-01-12 03:50:10 +04:00
|
|
|
unix_sockaddr_cleanup(&ctx);
|
|
|
|
errno = saved_errno;
|
|
|
|
return -1;
|
2011-12-10 14:34:14 +04:00
|
|
|
}
|