init ibuf only once for the lifetime of a privsep child
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
diff --git a/lib/got_lib_repository.h b/lib/got_lib_repository.h
index ea05870..83664d5 100644
--- a/lib/got_lib_repository.h
+++ b/lib/got_lib_repository.h
@@ -47,6 +47,7 @@ struct got_object_cache {
struct got_privsep_child {
int imsg_fd;
pid_t pid;
+ struct imsgbuf *ibuf;
};
struct got_repository {
diff --git a/lib/object_parse.c b/lib/object_parse.c
index d4f2092..cffa8b7 100644
--- a/lib/object_parse.c
+++ b/lib/object_parse.c
@@ -93,18 +93,15 @@ static const struct got_error *
request_object(struct got_object **obj, struct got_repository *repo, int fd)
{
const struct got_error *err = NULL;
- struct imsgbuf ibuf;
+ struct imsgbuf *ibuf;
- imsg_init(&ibuf,
- repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd);
+ ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
- err = got_privsep_send_obj_req(&ibuf, fd, NULL);
+ err = got_privsep_send_obj_req(ibuf, fd, NULL);
if (err)
- goto done;
- err = got_privsep_recv_obj(obj, &ibuf);
-done:
- imsg_clear(&ibuf);
- return err;
+ return err;
+
+ return got_privsep_recv_obj(obj, ibuf);
}
static void
@@ -136,10 +133,15 @@ got_object_read_header_privsep(struct got_object **obj,
{
int imsg_fds[2];
pid_t pid;
+ struct imsgbuf *ibuf;
if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
return request_object(obj, repo, obj_fd);
+ ibuf = calloc(1, sizeof(*ibuf));
+ if (ibuf == NULL)
+ return got_error_from_errno();
+
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
return got_error_from_errno();
@@ -155,6 +157,8 @@ got_object_read_header_privsep(struct got_object **obj,
repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
imsg_fds[0];
repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
+ imsg_init(ibuf, imsg_fds[0]);
+ repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
return request_object(obj, repo, obj_fd);
}
@@ -608,19 +612,15 @@ request_commit(struct got_commit_object **commit, struct got_repository *repo,
struct got_object *obj, int fd)
{
const struct got_error *err = NULL;
- struct imsgbuf ibuf;
+ struct imsgbuf *ibuf;
- imsg_init(&ibuf,
- repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd);
+ ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
- err = got_privsep_send_obj_req(&ibuf, fd,obj);
+ err = got_privsep_send_obj_req(ibuf, fd,obj);
if (err)
- goto done;
+ return err;
- err = got_privsep_recv_commit(commit, &ibuf);
-done:
- imsg_clear(&ibuf);
- return err;
+ return got_privsep_recv_commit(commit, ibuf);
}
const struct got_error *
@@ -629,10 +629,15 @@ got_object_read_commit_privsep(struct got_commit_object **commit,
{
int imsg_fds[2];
pid_t pid;
+ struct imsgbuf *ibuf;
if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
return request_commit(commit, repo, obj, obj_fd);
+ ibuf = calloc(1, sizeof(*ibuf));
+ if (ibuf == NULL)
+ return got_error_from_errno();
+
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
return got_error_from_errno();
@@ -648,6 +653,8 @@ got_object_read_commit_privsep(struct got_commit_object **commit,
repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
imsg_fds[0];
repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
+ imsg_init(ibuf, imsg_fds[0]);
+ repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
return request_commit(commit, repo, obj, obj_fd);
}
@@ -657,19 +664,15 @@ request_tree(struct got_tree_object **tree, struct got_repository *repo,
struct got_object *obj, int fd)
{
const struct got_error *err = NULL;
- struct imsgbuf ibuf;
+ struct imsgbuf *ibuf;
- imsg_init(&ibuf,
- repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd);
+ ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
- err = got_privsep_send_obj_req(&ibuf, fd,obj);
+ err = got_privsep_send_obj_req(ibuf, fd,obj);
if (err)
- goto done;
+ return err;
- err = got_privsep_recv_tree(tree, &ibuf);
-done:
- imsg_clear(&ibuf);
- return err;
+ return got_privsep_recv_tree(tree, ibuf);
}
const struct got_error *
@@ -678,10 +681,15 @@ got_object_read_tree_privsep(struct got_tree_object **tree,
{
int imsg_fds[2];
pid_t pid;
+ struct imsgbuf *ibuf;
if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
return request_tree(tree, repo, obj, obj_fd);
+ ibuf = calloc(1, sizeof(*ibuf));
+ if (ibuf == NULL)
+ return got_error_from_errno();
+
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
return got_error_from_errno();
@@ -694,9 +702,13 @@ got_object_read_tree_privsep(struct got_tree_object **tree,
}
close(imsg_fds[1]);
+
repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
imsg_fds[0];
repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
+ imsg_init(ibuf, imsg_fds[0]);
+ repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
+
return request_tree(tree, repo, obj, obj_fd);
}
@@ -705,28 +717,26 @@ static const struct got_error *
request_blob(size_t *size, int outfd, int infd, struct got_repository *repo)
{
const struct got_error *err = NULL;
- struct imsgbuf ibuf;
int outfd_child;
+ struct imsgbuf *ibuf;
outfd_child = dup(outfd);
if (outfd_child == -1)
return got_error_from_errno();
- imsg_init(&ibuf,
- repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd);
+ ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
- err = got_privsep_send_blob_req(&ibuf, outfd_child, infd);
+ err = got_privsep_send_blob_req(ibuf, outfd_child, infd);
if (err)
- goto done;
+ return err;
- err = got_privsep_recv_blob(size, &ibuf);
+ err = got_privsep_recv_blob(size, ibuf);
if (err)
- goto done;
+ return err;
if (lseek(outfd, SEEK_SET, 0) == -1)
- err = got_error_from_errno();
-done:
- imsg_clear(&ibuf);
+ return got_error_from_errno();
+
return err;
}
@@ -736,10 +746,15 @@ got_object_read_blob_privsep(size_t *size, int outfd, int infd,
{
int imsg_fds[2];
pid_t pid;
+ struct imsgbuf *ibuf;
if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1)
return request_blob(size, outfd, infd, repo);
+ ibuf = calloc(1, sizeof(*ibuf));
+ if (ibuf == NULL)
+ return got_error_from_errno();
+
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
return got_error_from_errno();
@@ -755,6 +770,8 @@ got_object_read_blob_privsep(size_t *size, int outfd, int infd,
repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
imsg_fds[0];
repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
+ imsg_init(ibuf, imsg_fds[0]);
+ repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
return request_blob(size, outfd, infd, repo);
}
diff --git a/lib/repository.c b/lib/repository.c
index 7ebf16f..2a9822b 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -415,8 +415,9 @@ got_repo_open(struct got_repository **repop, const char *path)
}
for (i = 0; i < nitems(repo->privsep_children); i++) {
+ memset(&repo->privsep_children[i], 0,
+ sizeof(repo->privsep_children[0]));
repo->privsep_children[i].imsg_fd = -1;
- repo->privsep_children[i].pid = 0;
}
repo->objcache.type = GOT_OBJECT_CACHE_TYPE_OBJ;
@@ -586,6 +587,8 @@ got_repo_close(struct got_repository *repo)
for (i = 0; i < nitems(repo->privsep_children); i++) {
if (repo->privsep_children[i].imsg_fd == -1)
continue;
+ imsg_clear(repo->privsep_children[i].ibuf);
+ free(repo->privsep_children[i].ibuf);
err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
child_err = wait_for_child(repo->privsep_children[i].pid);
if (child_err && err == NULL)