Commit 3d589bee0bbbe812bb91f3b0284fbf2596304132

Stefan Sperling 2022-06-25T14:54:41

allow start_pack_privsep_child() to be called from outside lib/object.c

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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
diff --git a/lib/got_lib_pack.h b/lib/got_lib_pack.h
index 0aed875..bfee6f4 100644
--- a/lib/got_lib_pack.h
+++ b/lib/got_lib_pack.h
@@ -30,6 +30,8 @@ struct got_pack {
 
 struct got_packidx;
 
+const struct got_error *got_pack_start_privsep_child(struct got_pack *,
+    struct got_packidx *);
 const struct got_error *got_pack_close(struct got_pack *);
 
 const struct got_error *got_pack_parse_offset_delta(off_t *, size_t *,
diff --git a/lib/object.c b/lib/object.c
index 7a1fbe3..06dc960 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -21,7 +21,6 @@
 #include <sys/uio.h>
 #include <sys/socket.h>
 #include <sys/wait.h>
-#include <sys/resource.h>
 #include <sys/mman.h>
 
 #include <errno.h>
@@ -246,80 +245,6 @@ request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
 	return NULL;
 }
 
-static void
-set_max_datasize(void)
-{
-	struct rlimit rl;
-
-	if (getrlimit(RLIMIT_DATA, &rl) != 0)
-		return;
-
-	rl.rlim_cur = rl.rlim_max;
-	setrlimit(RLIMIT_DATA, &rl);
-}
-
-static const struct got_error *
-start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
-{
-	const struct got_error *err = NULL;
-	int imsg_fds[2];
-	pid_t pid;
-	struct imsgbuf *ibuf;
-
-	ibuf = calloc(1, sizeof(*ibuf));
-	if (ibuf == NULL)
-		return got_error_from_errno("calloc");
-
-	pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
-	if (pack->privsep_child == NULL) {
-		err = got_error_from_errno("calloc");
-		free(ibuf);
-		return err;
-	}
-	pack->child_has_tempfiles = 0;
-	pack->child_has_delta_outfd = 0;
-
-	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
-		err = got_error_from_errno("socketpair");
-		goto done;
-	}
-
-	pid = fork();
-	if (pid == -1) {
-		err = got_error_from_errno("fork");
-		goto done;
-	} else if (pid == 0) {
-		set_max_datasize();
-		got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
-		    pack->path_packfile);
-		/* not reached */
-	}
-
-	if (close(imsg_fds[1]) == -1)
-		return got_error_from_errno("close");
-	pack->privsep_child->imsg_fd = imsg_fds[0];
-	pack->privsep_child->pid = pid;
-	imsg_init(ibuf, imsg_fds[0]);
-	pack->privsep_child->ibuf = ibuf;
-
-	err = got_privsep_init_pack_child(ibuf, pack, packidx);
-	if (err) {
-		const struct got_error *child_err;
-		err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
-		child_err = got_privsep_wait_for_child(
-		    pack->privsep_child->pid);
-		if (child_err && err == NULL)
-			err = child_err;
-	}
-done:
-	if (err) {
-		free(ibuf);
-		free(pack->privsep_child);
-		pack->privsep_child = NULL;
-	}
-	return err;
-}
-
 static const struct got_error *
 read_packed_object_privsep(struct got_object **obj,
     struct got_repository *repo, struct got_pack *pack,
@@ -328,7 +253,7 @@ read_packed_object_privsep(struct got_object **obj,
 	const struct got_error *err = NULL;
 
 	if (pack->privsep_child == NULL) {
-		err = start_pack_privsep_child(pack, packidx);
+		err = got_pack_start_privsep_child(pack, packidx);
 		if (err)
 			return err;
 	}
@@ -344,7 +269,7 @@ read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
 	const struct got_error *err = NULL;
 
 	if (pack->privsep_child == NULL) {
-		err = start_pack_privsep_child(pack, packidx);
+		err = got_pack_start_privsep_child(pack, packidx);
 		if (err)
 			return err;
 	}
@@ -427,7 +352,7 @@ got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
 	}
 
 	if (pack->privsep_child == NULL) {
-		err = start_pack_privsep_child(pack, packidx);
+		err = got_pack_start_privsep_child(pack, packidx);
 		if (err)
 			return err;
 	}
@@ -454,55 +379,6 @@ got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
 	    pack->privsep_child->ibuf);
 }
 
-/*
- * XXX This function does not really belong in object.c. It is only here
- * because it needs start_pack_privsep_child(); relevant code should
- * probably be moved to pack.c/pack_create.c.
- */
-const struct got_error *
-got_object_prepare_delta_reuse(struct got_pack **pack,
-    struct got_packidx *packidx, int delta_outfd, struct got_repository *repo)
-{
-	const struct got_error *err = NULL;
-	char *path_packfile = NULL;
-
-	err = got_packidx_get_packfile_path(&path_packfile,
-	    packidx->path_packidx);
-	if (err)
-		return err;
-
-	*pack = got_repo_get_cached_pack(repo, path_packfile);
-	if (*pack == NULL) {
-		err = got_repo_cache_pack(pack, repo, path_packfile, packidx);
-		if (err)
-			goto done;
-	}
-	if ((*pack)->privsep_child == NULL) {
-		err = start_pack_privsep_child(*pack, packidx);
-		if (err)
-			goto done;
-	}
-
-	if (!(*pack)->child_has_delta_outfd) {
-		int outfd_child;
-		outfd_child = dup(delta_outfd);
-		if (outfd_child == -1) {
-			err = got_error_from_errno("dup");
-			goto done;
-		}
-		err = got_privsep_send_raw_delta_outfd(
-		    (*pack)->privsep_child->ibuf, outfd_child);
-		if (err)
-			goto done;
-		(*pack)->child_has_delta_outfd = 1;
-	}
-
-	err = got_privsep_send_delta_reuse_req((*pack)->privsep_child->ibuf);
-done:
-	free(path_packfile);
-	return err;
-}
-
 static const struct got_error *
 request_object(struct got_object **obj, struct got_object_id *id,
     struct got_repository *repo, int fd)
@@ -842,7 +718,7 @@ read_packed_commit_privsep(struct got_commit_object **commit,
 	if (pack->privsep_child)
 		return request_packed_commit(commit, pack, idx, id);
 
-	err = start_pack_privsep_child(pack, packidx);
+	err = got_pack_start_privsep_child(pack, packidx);
 	if (err)
 		return err;
 
@@ -1047,7 +923,7 @@ read_packed_tree_privsep(struct got_tree_object **tree,
 	if (pack->privsep_child)
 		return request_packed_tree(tree, pack, idx, id);
 
-	err = start_pack_privsep_child(pack, packidx);
+	err = got_pack_start_privsep_child(pack, packidx);
 	if (err)
 		return err;
 
@@ -1370,7 +1246,7 @@ read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
 	const struct got_error *err = NULL;
 
 	if (pack->privsep_child == NULL) {
-		err = start_pack_privsep_child(pack, packidx);
+		err = got_pack_start_privsep_child(pack, packidx);
 		if (err)
 			return err;
 	}
@@ -1746,7 +1622,7 @@ read_packed_tag_privsep(struct got_tag_object **tag,
 	if (pack->privsep_child)
 		return request_packed_tag(tag, pack, idx, id);
 
-	err = start_pack_privsep_child(pack, packidx);
+	err = got_pack_start_privsep_child(pack, packidx);
 	if (err)
 		return err;
 
@@ -2370,7 +2246,7 @@ got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
 	}
 
 	if (pack->privsep_child == NULL) {
-		err = start_pack_privsep_child(pack, packidx);
+		err = got_pack_start_privsep_child(pack, packidx);
 		if (err)
 			goto done;
 	}
@@ -2426,7 +2302,7 @@ got_object_enumerate(int *found_all_objects,
 	}
 
 	if (pack->privsep_child == NULL) {
-		err = start_pack_privsep_child(pack, packidx);
+		err = got_pack_start_privsep_child(pack, packidx);
 		if (err)
 			goto done;
 	}
diff --git a/lib/pack.c b/lib/pack.c
index bbeafed..5786442 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -19,6 +19,8 @@
 #include <sys/queue.h>
 #include <sys/uio.h>
 #include <sys/mman.h>
+#include <sys/resource.h>
+#include <sys/socket.h>
 
 #include <fcntl.h>
 #include <errno.h>
@@ -712,6 +714,80 @@ got_packidx_match_id_str_prefix(struct got_object_id_queue *matched_ids,
 	return err;
 }
 
+static void
+set_max_datasize(void)
+{
+	struct rlimit rl;
+
+	if (getrlimit(RLIMIT_DATA, &rl) != 0)
+		return;
+
+	rl.rlim_cur = rl.rlim_max;
+	setrlimit(RLIMIT_DATA, &rl);
+}
+
+const struct got_error *
+got_pack_start_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
+{
+	const struct got_error *err = NULL;
+	int imsg_fds[2];
+	pid_t pid;
+	struct imsgbuf *ibuf;
+
+	ibuf = calloc(1, sizeof(*ibuf));
+	if (ibuf == NULL)
+		return got_error_from_errno("calloc");
+
+	pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
+	if (pack->privsep_child == NULL) {
+		err = got_error_from_errno("calloc");
+		free(ibuf);
+		return err;
+	}
+	pack->child_has_tempfiles = 0;
+	pack->child_has_delta_outfd = 0;
+
+	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
+		err = got_error_from_errno("socketpair");
+		goto done;
+	}
+
+	pid = fork();
+	if (pid == -1) {
+		err = got_error_from_errno("fork");
+		goto done;
+	} else if (pid == 0) {
+		set_max_datasize();
+		got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
+		    pack->path_packfile);
+		/* not reached */
+	}
+
+	if (close(imsg_fds[1]) == -1)
+		return got_error_from_errno("close");
+	pack->privsep_child->imsg_fd = imsg_fds[0];
+	pack->privsep_child->pid = pid;
+	imsg_init(ibuf, imsg_fds[0]);
+	pack->privsep_child->ibuf = ibuf;
+
+	err = got_privsep_init_pack_child(ibuf, pack, packidx);
+	if (err) {
+		const struct got_error *child_err;
+		err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
+		child_err = got_privsep_wait_for_child(
+		    pack->privsep_child->pid);
+		if (child_err && err == NULL)
+			err = child_err;
+	}
+done:
+	if (err) {
+		free(ibuf);
+		free(pack->privsep_child);
+		pack->privsep_child = NULL;
+	}
+	return err;
+}
+
 static const struct got_error *
 pack_stop_privsep_child(struct got_pack *pack)
 {
diff --git a/lib/pack_create.c b/lib/pack_create.c
index 2b957c1..16d1a75 100644
--- a/lib/pack_create.c
+++ b/lib/pack_create.c
@@ -595,6 +595,51 @@ recv_reused_delta(struct got_imsg_reused_delta *delta,
 }
 
 static const struct got_error *
+prepare_delta_reuse(struct got_pack **pack, struct got_packidx *packidx,
+    int delta_outfd, struct got_repository *repo)
+{
+	const struct got_error *err = NULL;
+	char *path_packfile = NULL;
+
+	err = got_packidx_get_packfile_path(&path_packfile,
+	    packidx->path_packidx);
+	if (err)
+		return err;
+
+	*pack = got_repo_get_cached_pack(repo, path_packfile);
+	if (*pack == NULL) {
+		err = got_repo_cache_pack(pack, repo, path_packfile, packidx);
+		if (err)
+			goto done;
+	}
+	if ((*pack)->privsep_child == NULL) {
+		err = got_pack_start_privsep_child(*pack, packidx);
+		if (err)
+			goto done;
+	}
+
+	if (!(*pack)->child_has_delta_outfd) {
+		int outfd_child;
+		outfd_child = dup(delta_outfd);
+		if (outfd_child == -1) {
+			err = got_error_from_errno("dup");
+			goto done;
+		}
+		err = got_privsep_send_raw_delta_outfd(
+		    (*pack)->privsep_child->ibuf, outfd_child);
+		if (err)
+			goto done;
+		(*pack)->child_has_delta_outfd = 1;
+	}
+
+	err = got_privsep_send_delta_reuse_req((*pack)->privsep_child->ibuf);
+done:
+	free(path_packfile);
+	return err;
+}
+
+
+static const struct got_error *
 search_deltas(struct got_pack_metavec *v, struct got_object_idset *idset,
     int delta_cache_fd, int ncolored, int nfound, int ntrees, int ncommits,
     struct got_repository *repo,
@@ -615,8 +660,7 @@ search_deltas(struct got_pack_metavec *v, struct got_object_idset *idset,
 	if (packidx == NULL)
 		return NULL;
 
-	err = got_object_prepare_delta_reuse(&pack, packidx,
-	    delta_cache_fd, repo);
+	err = prepare_delta_reuse(&pack, packidx, delta_cache_fd, repo);
 	if (err)
 		return err;