Coverage Report

Created: 2023-11-19 07:08

/src/git/unix-socket.c
Line
Count
Source (jump to first uncovered line)
1
#include "git-compat-util.h"
2
#include "strbuf.h"
3
#include "unix-socket.h"
4
5
0
#define DEFAULT_UNIX_STREAM_LISTEN_BACKLOG (5)
6
7
static int chdir_len(const char *orig, int len)
8
0
{
9
0
  char *path = xmemdupz(orig, len);
10
0
  int r = chdir(path);
11
0
  free(path);
12
0
  return r;
13
0
}
14
15
struct unix_sockaddr_context {
16
  char *orig_dir;
17
};
18
19
static void unix_sockaddr_cleanup(struct unix_sockaddr_context *ctx)
20
0
{
21
0
  if (!ctx->orig_dir)
22
0
    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
0
  if (chdir(ctx->orig_dir) < 0)
29
0
    die("unable to restore original working directory");
30
0
  free(ctx->orig_dir);
31
0
}
32
33
static int unix_sockaddr_init(struct sockaddr_un *sa, const char *path,
34
            struct unix_sockaddr_context *ctx,
35
            int disallow_chdir)
36
0
{
37
0
  int size = strlen(path) + 1;
38
39
0
  ctx->orig_dir = NULL;
40
0
  if (size > sizeof(sa->sun_path)) {
41
0
    const char *slash;
42
0
    const char *dir;
43
0
    struct strbuf cwd = STRBUF_INIT;
44
45
0
    if (disallow_chdir) {
46
0
      errno = ENAMETOOLONG;
47
0
      return -1;
48
0
    }
49
50
0
    slash = find_last_dir_sep(path);
51
0
    if (!slash) {
52
0
      errno = ENAMETOOLONG;
53
0
      return -1;
54
0
    }
55
56
0
    dir = path;
57
0
    path = slash + 1;
58
0
    size = strlen(path) + 1;
59
0
    if (size > sizeof(sa->sun_path)) {
60
0
      errno = ENAMETOOLONG;
61
0
      return -1;
62
0
    }
63
0
    if (strbuf_getcwd(&cwd))
64
0
      return -1;
65
0
    ctx->orig_dir = strbuf_detach(&cwd, NULL);
66
0
    if (chdir_len(dir, slash - dir) < 0)
67
0
      return -1;
68
0
  }
69
70
0
  memset(sa, 0, sizeof(*sa));
71
0
  sa->sun_family = AF_UNIX;
72
0
  memcpy(sa->sun_path, path, size);
73
0
  return 0;
74
0
}
75
76
int unix_stream_connect(const char *path, int disallow_chdir)
77
0
{
78
0
  int fd = -1, saved_errno;
79
0
  struct sockaddr_un sa;
80
0
  struct unix_sockaddr_context ctx;
81
82
0
  if (unix_sockaddr_init(&sa, path, &ctx, disallow_chdir) < 0)
83
0
    return -1;
84
0
  fd = socket(AF_UNIX, SOCK_STREAM, 0);
85
0
  if (fd < 0)
86
0
    goto fail;
87
88
0
  if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
89
0
    goto fail;
90
0
  unix_sockaddr_cleanup(&ctx);
91
0
  return fd;
92
93
0
fail:
94
0
  saved_errno = errno;
95
0
  if (fd != -1)
96
0
    close(fd);
97
0
  unix_sockaddr_cleanup(&ctx);
98
0
  errno = saved_errno;
99
0
  return -1;
100
0
}
101
102
int unix_stream_listen(const char *path,
103
           const struct unix_stream_listen_opts *opts)
104
0
{
105
0
  int fd = -1, saved_errno;
106
0
  int backlog;
107
0
  struct sockaddr_un sa;
108
0
  struct unix_sockaddr_context ctx;
109
110
0
  unlink(path);
111
112
0
  if (unix_sockaddr_init(&sa, path, &ctx, opts->disallow_chdir) < 0)
113
0
    return -1;
114
0
  fd = socket(AF_UNIX, SOCK_STREAM, 0);
115
0
  if (fd < 0)
116
0
    goto fail;
117
118
0
  if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
119
0
    goto fail;
120
121
0
  backlog = opts->listen_backlog_size;
122
0
  if (backlog <= 0)
123
0
    backlog = DEFAULT_UNIX_STREAM_LISTEN_BACKLOG;
124
0
  if (listen(fd, backlog) < 0)
125
0
    goto fail;
126
127
0
  unix_sockaddr_cleanup(&ctx);
128
0
  return fd;
129
130
0
fail:
131
0
  saved_errno = errno;
132
0
  if (fd != -1)
133
0
    close(fd);
134
0
  unix_sockaddr_cleanup(&ctx);
135
0
  errno = saved_errno;
136
0
  return -1;
137
0
}