2 876c234b 2018-09-10 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 876c234b 2018-09-10 stsp * Permission to use, copy, modify, and distribute this software for any
5 876c234b 2018-09-10 stsp * purpose with or without fee is hereby granted, provided that the above
6 876c234b 2018-09-10 stsp * copyright notice and this permission notice appear in all copies.
8 876c234b 2018-09-10 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 876c234b 2018-09-10 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 876c234b 2018-09-10 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 876c234b 2018-09-10 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 876c234b 2018-09-10 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 876c234b 2018-09-10 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 876c234b 2018-09-10 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 876c234b 2018-09-10 stsp #include <sys/types.h>
18 876c234b 2018-09-10 stsp #include <sys/queue.h>
19 876c234b 2018-09-10 stsp #include <sys/uio.h>
20 876c234b 2018-09-10 stsp #include <sys/time.h>
21 876c234b 2018-09-10 stsp #include <sys/limits.h>
22 876c234b 2018-09-10 stsp #include <sys/syslimits.h>
23 876c234b 2018-09-10 stsp #include <sys/mman.h>
25 876c234b 2018-09-10 stsp #include <limits.h>
26 99437157 2018-11-11 stsp #include <signal.h>
27 876c234b 2018-09-10 stsp #include <stdint.h>
28 876c234b 2018-09-10 stsp #include <imsg.h>
29 876c234b 2018-09-10 stsp #include <stdio.h>
30 876c234b 2018-09-10 stsp #include <stdlib.h>
31 876c234b 2018-09-10 stsp #include <string.h>
32 876c234b 2018-09-10 stsp #include <sha1.h>
33 876c234b 2018-09-10 stsp #include <zlib.h>
35 876c234b 2018-09-10 stsp #include "got_error.h"
36 876c234b 2018-09-10 stsp #include "got_object.h"
38 876c234b 2018-09-10 stsp #include "got_lib_delta.h"
39 876c234b 2018-09-10 stsp #include "got_lib_inflate.h"
40 876c234b 2018-09-10 stsp #include "got_lib_object.h"
41 c59b3346 2018-09-11 stsp #include "got_lib_object_cache.h"
42 876c234b 2018-09-10 stsp #include "got_lib_object_parse.h"
43 876c234b 2018-09-10 stsp #include "got_lib_privsep.h"
44 876c234b 2018-09-10 stsp #include "got_lib_pack.h"
46 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
49 99437157 2018-11-11 stsp catch_sigint(int signo)
51 99437157 2018-11-11 stsp sigint_received = 1;
54 876c234b 2018-09-10 stsp static const struct got_error *
55 876c234b 2018-09-10 stsp object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
56 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
58 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
59 876c234b 2018-09-10 stsp struct got_imsg_packed_object iobj;
60 876c234b 2018-09-10 stsp struct got_object *obj;
61 106807b4 2018-09-15 stsp struct got_object_id id;
62 876c234b 2018-09-10 stsp size_t datalen;
64 876c234b 2018-09-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
65 876c234b 2018-09-10 stsp if (datalen != sizeof(iobj))
66 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
67 876c234b 2018-09-10 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
68 106807b4 2018-09-15 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
70 106807b4 2018-09-15 stsp err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, &id);
73 c59b3346 2018-09-11 stsp obj->refcnt++;
75 c59b3346 2018-09-11 stsp err = got_object_cache_add(objcache, &obj->id, obj);
78 c59b3346 2018-09-11 stsp obj->refcnt++;
80 876c234b 2018-09-10 stsp err = got_privsep_send_obj(ibuf, obj);
82 876c234b 2018-09-10 stsp got_object_close(obj);
86 876c234b 2018-09-10 stsp static const struct got_error *
87 876c234b 2018-09-10 stsp commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
88 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
90 cfd633c2 2018-09-10 stsp const struct got_error *err = NULL;
91 1785f84a 2018-12-23 stsp struct got_imsg_packed_object iobj;
92 1785f84a 2018-12-23 stsp struct got_object *obj;
93 cfd633c2 2018-09-10 stsp struct got_commit_object *commit = NULL;
94 cfd633c2 2018-09-10 stsp uint8_t *buf;
96 1785f84a 2018-12-23 stsp struct got_object_id id;
97 1785f84a 2018-12-23 stsp size_t datalen;
99 1785f84a 2018-12-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
100 1785f84a 2018-12-23 stsp if (datalen != sizeof(iobj))
101 1785f84a 2018-12-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
102 1785f84a 2018-12-23 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
103 1785f84a 2018-12-23 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
105 1785f84a 2018-12-23 stsp err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, &id);
107 cfd633c2 2018-09-10 stsp return err;
109 cfd633c2 2018-09-10 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
111 cfd633c2 2018-09-10 stsp return err;
113 cfd633c2 2018-09-10 stsp obj->size = len;
114 cfd633c2 2018-09-10 stsp err = got_object_parse_commit(&commit, buf, len);
117 1785f84a 2018-12-23 stsp got_object_close(obj);
118 1785f84a 2018-12-23 stsp return err;
121 cfd633c2 2018-09-10 stsp err = got_privsep_send_commit(ibuf, commit);
122 cfd633c2 2018-09-10 stsp got_object_commit_close(commit);
124 7762fe12 2018-11-05 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
125 7762fe12 2018-11-05 stsp err = NULL;
127 7762fe12 2018-11-05 stsp got_privsep_send_error(ibuf, err);
130 7762fe12 2018-11-05 stsp return err;
133 7762fe12 2018-11-05 stsp static const struct got_error *
134 876c234b 2018-09-10 stsp tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
135 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
137 e7885405 2018-09-10 stsp const struct got_error *err = NULL;
138 13c729f7 2018-12-24 stsp struct got_imsg_packed_object iobj;
139 e7885405 2018-09-10 stsp struct got_object *obj = NULL;
140 e7885405 2018-09-10 stsp struct got_tree_object *tree = NULL;
141 e7885405 2018-09-10 stsp uint8_t *buf;
142 e7885405 2018-09-10 stsp size_t len;
143 13c729f7 2018-12-24 stsp struct got_object_id id;
144 13c729f7 2018-12-24 stsp size_t datalen;
146 13c729f7 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
147 13c729f7 2018-12-24 stsp if (datalen != sizeof(iobj))
148 13c729f7 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
149 13c729f7 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
150 13c729f7 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
152 13c729f7 2018-12-24 stsp err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, &id);
154 e7885405 2018-09-10 stsp return err;
156 e7885405 2018-09-10 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
158 e7885405 2018-09-10 stsp return err;
160 e7885405 2018-09-10 stsp obj->size = len;
161 e7885405 2018-09-10 stsp err = got_object_parse_tree(&tree, buf, len);
164 e7885405 2018-09-10 stsp err = got_privsep_send_tree(ibuf, tree);
166 e7885405 2018-09-10 stsp got_object_close(obj);
167 e7885405 2018-09-10 stsp got_object_tree_close(tree);
169 e7885405 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
170 e7885405 2018-09-10 stsp err = NULL;
172 e7885405 2018-09-10 stsp got_privsep_send_error(ibuf, err);
175 e7885405 2018-09-10 stsp return err;
178 876c234b 2018-09-10 stsp static const struct got_error *
179 3840f4c9 2018-09-12 stsp receive_file(FILE **f, struct imsgbuf *ibuf, int imsg_code)
181 3840f4c9 2018-09-12 stsp const struct got_error *err;
182 3840f4c9 2018-09-12 stsp struct imsg imsg;
183 55da3778 2018-09-10 stsp size_t datalen;
185 3840f4c9 2018-09-12 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
187 55da3778 2018-09-10 stsp return err;
189 3840f4c9 2018-09-12 stsp if (imsg.hdr.type != imsg_code) {
190 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
194 3840f4c9 2018-09-12 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
195 55da3778 2018-09-10 stsp if (datalen != 0) {
196 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
199 3840f4c9 2018-09-12 stsp if (imsg.fd == -1) {
200 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
204 3840f4c9 2018-09-12 stsp *f = fdopen(imsg.fd, "w+");
205 3840f4c9 2018-09-12 stsp if (*f == NULL) {
206 3840f4c9 2018-09-12 stsp close(imsg.fd);
207 55da3778 2018-09-10 stsp err = got_error_from_errno();
211 3840f4c9 2018-09-12 stsp imsg_free(&imsg);
212 3840f4c9 2018-09-12 stsp return err;
215 3840f4c9 2018-09-12 stsp static const struct got_error *
216 3840f4c9 2018-09-12 stsp blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
217 3840f4c9 2018-09-12 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
219 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
220 ebc55e2d 2018-12-24 stsp struct got_imsg_packed_object iobj;
221 3840f4c9 2018-09-12 stsp struct got_object *obj = NULL;
222 3840f4c9 2018-09-12 stsp FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
223 ebc55e2d 2018-12-24 stsp struct got_object_id id;
224 ebc55e2d 2018-12-24 stsp size_t datalen;
225 ac544f8c 2019-01-13 stsp uint64_t blob_size;
226 ac544f8c 2019-01-13 stsp uint8_t *buf = NULL;
228 ebc55e2d 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
229 ebc55e2d 2018-12-24 stsp if (datalen != sizeof(iobj))
230 ebc55e2d 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
231 ebc55e2d 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
232 ebc55e2d 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
234 ebc55e2d 2018-12-24 stsp err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, &id);
236 3840f4c9 2018-09-12 stsp return err;
238 3840f4c9 2018-09-12 stsp err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
241 3840f4c9 2018-09-12 stsp err = receive_file(&basefile, ibuf, GOT_IMSG_TMPFD);
244 3840f4c9 2018-09-12 stsp err = receive_file(&accumfile, ibuf, GOT_IMSG_TMPFD);
248 ac544f8c 2019-01-13 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
249 85a703fa 2019-01-13 stsp err = got_pack_get_max_delta_object_size(&blob_size, obj);
253 ac544f8c 2019-01-13 stsp blob_size = obj->size;
255 ac544f8c 2019-01-13 stsp if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
256 ac544f8c 2019-01-13 stsp err = got_packfile_extract_object_to_mem(&buf, &obj->size,
257 ac544f8c 2019-01-13 stsp obj, pack);
259 ac544f8c 2019-01-13 stsp err = got_packfile_extract_object(pack, obj, outfile, basefile,
260 ac544f8c 2019-01-13 stsp accumfile);
264 ac544f8c 2019-01-13 stsp err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
267 3840f4c9 2018-09-12 stsp if (outfile)
268 3840f4c9 2018-09-12 stsp fclose(outfile);
269 3840f4c9 2018-09-12 stsp if (basefile)
270 3840f4c9 2018-09-12 stsp fclose(basefile);
271 3840f4c9 2018-09-12 stsp if (accumfile)
272 3840f4c9 2018-09-12 stsp fclose(accumfile);
274 c59b3346 2018-09-11 stsp got_object_close(obj);
275 3840f4c9 2018-09-12 stsp if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
276 3840f4c9 2018-09-12 stsp got_privsep_send_error(ibuf, err);
278 55da3778 2018-09-10 stsp return err;
281 876c234b 2018-09-10 stsp static const struct got_error *
282 f4a881ce 2018-11-17 stsp tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
283 f4a881ce 2018-11-17 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
285 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
286 268f7291 2018-12-24 stsp struct got_imsg_packed_object iobj;
287 f4a881ce 2018-11-17 stsp struct got_object *obj = NULL;
288 f4a881ce 2018-11-17 stsp struct got_tag_object *tag = NULL;
289 f4a881ce 2018-11-17 stsp uint8_t *buf;
290 f4a881ce 2018-11-17 stsp size_t len;
291 268f7291 2018-12-24 stsp struct got_object_id id;
292 268f7291 2018-12-24 stsp size_t datalen;
294 268f7291 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
295 268f7291 2018-12-24 stsp if (datalen != sizeof(iobj))
296 268f7291 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
297 268f7291 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
298 268f7291 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
300 268f7291 2018-12-24 stsp err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, &id);
302 f4a881ce 2018-11-17 stsp return err;
304 f4a881ce 2018-11-17 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
306 f4a881ce 2018-11-17 stsp return err;
308 f4a881ce 2018-11-17 stsp obj->size = len;
309 f4a881ce 2018-11-17 stsp err = got_object_parse_tag(&tag, buf, len);
312 f4a881ce 2018-11-17 stsp err = got_privsep_send_tag(ibuf, tag);
314 f4a881ce 2018-11-17 stsp got_object_close(obj);
315 f4a881ce 2018-11-17 stsp got_object_tag_close(tag);
317 f4a881ce 2018-11-17 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
318 f4a881ce 2018-11-17 stsp err = NULL;
320 f4a881ce 2018-11-17 stsp got_privsep_send_error(ibuf, err);
323 f4a881ce 2018-11-17 stsp return err;
326 f4a881ce 2018-11-17 stsp static const struct got_error *
327 876c234b 2018-09-10 stsp receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
329 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
330 876c234b 2018-09-10 stsp struct imsg imsg;
331 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
332 876c234b 2018-09-10 stsp size_t datalen;
333 876c234b 2018-09-10 stsp struct got_packidx *p;
335 876c234b 2018-09-10 stsp *packidx = NULL;
337 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
339 876c234b 2018-09-10 stsp return err;
341 876c234b 2018-09-10 stsp p = calloc(1, sizeof(*p));
342 876c234b 2018-09-10 stsp if (p == NULL) {
343 876c234b 2018-09-10 stsp err = got_error_from_errno();
347 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
348 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
352 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
353 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
357 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
358 876c234b 2018-09-10 stsp if (datalen != sizeof(ipackidx)) {
359 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
362 876c234b 2018-09-10 stsp memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
364 876c234b 2018-09-10 stsp p->len = ipackidx.len;
365 876c234b 2018-09-10 stsp p->fd = dup(imsg.fd);
366 876c234b 2018-09-10 stsp if (p->fd == -1) {
367 56bef47a 2018-09-15 stsp err = got_error_from_errno();
370 56bef47a 2018-09-15 stsp if (lseek(p->fd, 0, SEEK_SET) == -1) {
371 876c234b 2018-09-10 stsp err = got_error_from_errno();
375 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
376 876c234b 2018-09-10 stsp p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
377 876c234b 2018-09-10 stsp if (p->map == MAP_FAILED)
378 876c234b 2018-09-10 stsp p->map = NULL; /* fall back to read(2) */
380 876c234b 2018-09-10 stsp err = got_packidx_init_hdr(p, 1);
383 876c234b 2018-09-10 stsp if (imsg.fd != -1)
384 876c234b 2018-09-10 stsp close(imsg.fd);
385 876c234b 2018-09-10 stsp got_packidx_close(p);
387 876c234b 2018-09-10 stsp *packidx = p;
388 876c234b 2018-09-10 stsp imsg_free(&imsg);
389 876c234b 2018-09-10 stsp return err;
392 876c234b 2018-09-10 stsp static const struct got_error *
393 876c234b 2018-09-10 stsp receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
395 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
396 876c234b 2018-09-10 stsp struct imsg imsg;
397 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
398 876c234b 2018-09-10 stsp size_t datalen;
399 876c234b 2018-09-10 stsp struct got_pack *pack;
401 876c234b 2018-09-10 stsp *packp = NULL;
403 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
405 876c234b 2018-09-10 stsp return err;
407 876c234b 2018-09-10 stsp pack = calloc(1, sizeof(*pack));
408 876c234b 2018-09-10 stsp if (pack == NULL) {
409 876c234b 2018-09-10 stsp err = got_error_from_errno();
413 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACK) {
414 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
418 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
419 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
423 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
424 876c234b 2018-09-10 stsp if (datalen != sizeof(ipack)) {
425 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
428 876c234b 2018-09-10 stsp memcpy(&ipack, imsg.data, sizeof(ipack));
430 876c234b 2018-09-10 stsp pack->filesize = ipack.filesize;
431 876c234b 2018-09-10 stsp pack->fd = dup(imsg.fd);
432 876c234b 2018-09-10 stsp if (pack->fd == -1) {
433 876c234b 2018-09-10 stsp err = got_error_from_errno();
436 56bef47a 2018-09-15 stsp if (lseek(pack->fd, 0, SEEK_SET) == -1) {
437 56bef47a 2018-09-15 stsp err = got_error_from_errno();
440 876c234b 2018-09-10 stsp pack->path_packfile = strdup(ipack.path_packfile);
441 876c234b 2018-09-10 stsp if (pack->path_packfile == NULL) {
442 876c234b 2018-09-10 stsp err = got_error_from_errno();
446 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
447 876c234b 2018-09-10 stsp pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
448 876c234b 2018-09-10 stsp pack->fd, 0);
449 876c234b 2018-09-10 stsp if (pack->map == MAP_FAILED)
450 876c234b 2018-09-10 stsp pack->map = NULL; /* fall back to read(2) */
454 876c234b 2018-09-10 stsp if (imsg.fd != -1)
455 876c234b 2018-09-10 stsp close(imsg.fd);
456 876c234b 2018-09-10 stsp free(pack);
458 876c234b 2018-09-10 stsp *packp = pack;
459 876c234b 2018-09-10 stsp imsg_free(&imsg);
460 876c234b 2018-09-10 stsp return err;
464 876c234b 2018-09-10 stsp main(int argc, char *argv[])
466 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
467 876c234b 2018-09-10 stsp struct imsgbuf ibuf;
468 876c234b 2018-09-10 stsp struct imsg imsg;
469 c59b3346 2018-09-11 stsp struct got_packidx *packidx = NULL;
470 c59b3346 2018-09-11 stsp struct got_pack *pack = NULL;
471 c59b3346 2018-09-11 stsp struct got_object_cache objcache;
473 876c234b 2018-09-10 stsp //static int attached;
474 876c234b 2018-09-10 stsp //while (!attached) sleep(1);
476 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
478 876c234b 2018-09-10 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
480 c59b3346 2018-09-11 stsp err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
482 c59b3346 2018-09-11 stsp err = got_error_from_errno();
483 c59b3346 2018-09-11 stsp got_privsep_send_error(&ibuf, err);
487 2ff12563 2018-09-15 stsp #ifndef PROFILE
488 876c234b 2018-09-10 stsp /* revoke access to most system calls */
489 876c234b 2018-09-10 stsp if (pledge("stdio recvfd", NULL) == -1) {
490 876c234b 2018-09-10 stsp err = got_error_from_errno();
491 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
496 876c234b 2018-09-10 stsp err = receive_packidx(&packidx, &ibuf);
498 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
502 876c234b 2018-09-10 stsp err = receive_pack(&pack, &ibuf);
504 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
508 876c234b 2018-09-10 stsp while (1) {
509 876c234b 2018-09-10 stsp imsg.fd = -1;
511 99437157 2018-11-11 stsp if (sigint_received) {
512 99437157 2018-11-11 stsp err = got_error(GOT_ERR_CANCELLED);
516 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
518 876c234b 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
519 876c234b 2018-09-10 stsp err = NULL;
523 876c234b 2018-09-10 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
526 876c234b 2018-09-10 stsp switch (imsg.hdr.type) {
527 876c234b 2018-09-10 stsp case GOT_IMSG_PACKED_OBJECT_REQUEST:
528 c59b3346 2018-09-11 stsp err = object_request(&imsg, &ibuf, pack, packidx,
529 c59b3346 2018-09-11 stsp &objcache);
531 876c234b 2018-09-10 stsp case GOT_IMSG_COMMIT_REQUEST:
532 c59b3346 2018-09-11 stsp err = commit_request(&imsg, &ibuf, pack, packidx,
533 7762fe12 2018-11-05 stsp &objcache);
535 876c234b 2018-09-10 stsp case GOT_IMSG_TREE_REQUEST:
536 c59b3346 2018-09-11 stsp err = tree_request(&imsg, &ibuf, pack, packidx,
537 c59b3346 2018-09-11 stsp &objcache);
539 876c234b 2018-09-10 stsp case GOT_IMSG_BLOB_REQUEST:
540 c59b3346 2018-09-11 stsp err = blob_request(&imsg, &ibuf, pack, packidx,
541 c59b3346 2018-09-11 stsp &objcache);
543 f4a881ce 2018-11-17 stsp case GOT_IMSG_TAG_REQUEST:
544 f4a881ce 2018-11-17 stsp err = tag_request(&imsg, &ibuf, pack, packidx,
545 f4a881ce 2018-11-17 stsp &objcache);
548 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
552 876c234b 2018-09-10 stsp if (imsg.fd != -1)
553 876c234b 2018-09-10 stsp close(imsg.fd);
554 876c234b 2018-09-10 stsp imsg_free(&imsg);
559 c59b3346 2018-09-11 stsp if (packidx)
560 c59b3346 2018-09-11 stsp got_packidx_close(packidx);
562 c59b3346 2018-09-11 stsp got_pack_close(pack);
563 48d5fe42 2018-09-15 stsp got_object_cache_close(&objcache);
564 876c234b 2018-09-10 stsp imsg_clear(&ibuf);
566 80d5f134 2018-11-11 stsp if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
567 80d5f134 2018-11-11 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
568 99437157 2018-11-11 stsp got_privsep_send_error(&ibuf, err);
571 876c234b 2018-09-10 stsp close(GOT_IMSG_FD_CHILD);
572 876c234b 2018-09-10 stsp return err ? 1 : 0;