2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 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 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
207 3a6ce05a 2019-02-11 stsp close(imsg.fd);
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 fb43ecf1 2019-02-11 stsp if (outfile && fclose(outfile) != 0 && err == NULL)
268 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
269 638f9024 2019-05-13 stsp if (basefile && fclose(basefile) != 0 && err == NULL)
270 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
271 fb43ecf1 2019-02-11 stsp if (accumfile && fclose(accumfile) != 0 && err == NULL)
272 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
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 0ae4af15 2019-02-01 stsp return err;
314 f4a881ce 2018-11-17 stsp err = got_privsep_send_tag(ibuf, tag);
316 f4a881ce 2018-11-17 stsp got_object_close(obj);
317 f4a881ce 2018-11-17 stsp got_object_tag_close(tag);
319 f4a881ce 2018-11-17 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
320 f4a881ce 2018-11-17 stsp err = NULL;
322 f4a881ce 2018-11-17 stsp got_privsep_send_error(ibuf, err);
325 f4a881ce 2018-11-17 stsp return err;
328 f4a881ce 2018-11-17 stsp static const struct got_error *
329 876c234b 2018-09-10 stsp receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
331 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
332 876c234b 2018-09-10 stsp struct imsg imsg;
333 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
334 876c234b 2018-09-10 stsp size_t datalen;
335 876c234b 2018-09-10 stsp struct got_packidx *p;
337 876c234b 2018-09-10 stsp *packidx = NULL;
339 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
341 876c234b 2018-09-10 stsp return err;
343 876c234b 2018-09-10 stsp p = calloc(1, sizeof(*p));
344 876c234b 2018-09-10 stsp if (p == NULL) {
345 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
349 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
350 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
354 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
355 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
359 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
360 876c234b 2018-09-10 stsp if (datalen != sizeof(ipackidx)) {
361 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
364 876c234b 2018-09-10 stsp memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
366 876c234b 2018-09-10 stsp p->len = ipackidx.len;
367 876c234b 2018-09-10 stsp p->fd = dup(imsg.fd);
368 876c234b 2018-09-10 stsp if (p->fd == -1) {
369 638f9024 2019-05-13 stsp err = got_error_from_errno("dup");
372 56bef47a 2018-09-15 stsp if (lseek(p->fd, 0, SEEK_SET) == -1) {
373 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
377 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
378 876c234b 2018-09-10 stsp p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
379 876c234b 2018-09-10 stsp if (p->map == MAP_FAILED)
380 876c234b 2018-09-10 stsp p->map = NULL; /* fall back to read(2) */
382 876c234b 2018-09-10 stsp err = got_packidx_init_hdr(p, 1);
385 876c234b 2018-09-10 stsp if (imsg.fd != -1)
386 876c234b 2018-09-10 stsp close(imsg.fd);
387 876c234b 2018-09-10 stsp got_packidx_close(p);
389 876c234b 2018-09-10 stsp *packidx = p;
390 876c234b 2018-09-10 stsp imsg_free(&imsg);
391 876c234b 2018-09-10 stsp return err;
394 876c234b 2018-09-10 stsp static const struct got_error *
395 876c234b 2018-09-10 stsp receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
397 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
398 876c234b 2018-09-10 stsp struct imsg imsg;
399 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
400 876c234b 2018-09-10 stsp size_t datalen;
401 876c234b 2018-09-10 stsp struct got_pack *pack;
403 876c234b 2018-09-10 stsp *packp = NULL;
405 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
407 876c234b 2018-09-10 stsp return err;
409 876c234b 2018-09-10 stsp pack = calloc(1, sizeof(*pack));
410 876c234b 2018-09-10 stsp if (pack == NULL) {
411 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
415 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACK) {
416 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
420 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
421 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
425 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
426 876c234b 2018-09-10 stsp if (datalen != sizeof(ipack)) {
427 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
430 876c234b 2018-09-10 stsp memcpy(&ipack, imsg.data, sizeof(ipack));
432 876c234b 2018-09-10 stsp pack->filesize = ipack.filesize;
433 876c234b 2018-09-10 stsp pack->fd = dup(imsg.fd);
434 876c234b 2018-09-10 stsp if (pack->fd == -1) {
435 638f9024 2019-05-13 stsp err = got_error_from_errno("dup");
438 56bef47a 2018-09-15 stsp if (lseek(pack->fd, 0, SEEK_SET) == -1) {
439 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
442 876c234b 2018-09-10 stsp pack->path_packfile = strdup(ipack.path_packfile);
443 876c234b 2018-09-10 stsp if (pack->path_packfile == NULL) {
444 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
448 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
449 876c234b 2018-09-10 stsp pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
450 876c234b 2018-09-10 stsp pack->fd, 0);
451 876c234b 2018-09-10 stsp if (pack->map == MAP_FAILED)
452 876c234b 2018-09-10 stsp pack->map = NULL; /* fall back to read(2) */
456 876c234b 2018-09-10 stsp if (imsg.fd != -1)
457 876c234b 2018-09-10 stsp close(imsg.fd);
458 876c234b 2018-09-10 stsp free(pack);
460 876c234b 2018-09-10 stsp *packp = pack;
461 876c234b 2018-09-10 stsp imsg_free(&imsg);
462 876c234b 2018-09-10 stsp return err;
466 876c234b 2018-09-10 stsp main(int argc, char *argv[])
468 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
469 876c234b 2018-09-10 stsp struct imsgbuf ibuf;
470 876c234b 2018-09-10 stsp struct imsg imsg;
471 c59b3346 2018-09-11 stsp struct got_packidx *packidx = NULL;
472 c59b3346 2018-09-11 stsp struct got_pack *pack = NULL;
473 c59b3346 2018-09-11 stsp struct got_object_cache objcache;
475 876c234b 2018-09-10 stsp //static int attached;
476 876c234b 2018-09-10 stsp //while (!attached) sleep(1);
478 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
480 876c234b 2018-09-10 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
482 c59b3346 2018-09-11 stsp err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
484 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_cache_init");
485 c59b3346 2018-09-11 stsp got_privsep_send_error(&ibuf, err);
489 2ff12563 2018-09-15 stsp #ifndef PROFILE
490 876c234b 2018-09-10 stsp /* revoke access to most system calls */
491 876c234b 2018-09-10 stsp if (pledge("stdio recvfd", NULL) == -1) {
492 638f9024 2019-05-13 stsp err = got_error_from_errno("pledge");
493 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
498 876c234b 2018-09-10 stsp err = receive_packidx(&packidx, &ibuf);
500 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
504 876c234b 2018-09-10 stsp err = receive_pack(&pack, &ibuf);
506 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
511 876c234b 2018-09-10 stsp imsg.fd = -1;
513 99437157 2018-11-11 stsp if (sigint_received) {
514 99437157 2018-11-11 stsp err = got_error(GOT_ERR_CANCELLED);
518 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
520 876c234b 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
521 876c234b 2018-09-10 stsp err = NULL;
525 876c234b 2018-09-10 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
528 876c234b 2018-09-10 stsp switch (imsg.hdr.type) {
529 876c234b 2018-09-10 stsp case GOT_IMSG_PACKED_OBJECT_REQUEST:
530 c59b3346 2018-09-11 stsp err = object_request(&imsg, &ibuf, pack, packidx,
531 c59b3346 2018-09-11 stsp &objcache);
533 876c234b 2018-09-10 stsp case GOT_IMSG_COMMIT_REQUEST:
534 c59b3346 2018-09-11 stsp err = commit_request(&imsg, &ibuf, pack, packidx,
535 7762fe12 2018-11-05 stsp &objcache);
537 876c234b 2018-09-10 stsp case GOT_IMSG_TREE_REQUEST:
538 c59b3346 2018-09-11 stsp err = tree_request(&imsg, &ibuf, pack, packidx,
539 c59b3346 2018-09-11 stsp &objcache);
541 876c234b 2018-09-10 stsp case GOT_IMSG_BLOB_REQUEST:
542 c59b3346 2018-09-11 stsp err = blob_request(&imsg, &ibuf, pack, packidx,
543 c59b3346 2018-09-11 stsp &objcache);
545 f4a881ce 2018-11-17 stsp case GOT_IMSG_TAG_REQUEST:
546 f4a881ce 2018-11-17 stsp err = tag_request(&imsg, &ibuf, pack, packidx,
547 f4a881ce 2018-11-17 stsp &objcache);
550 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
554 3a6ce05a 2019-02-11 stsp if (imsg.fd != -1 && close(imsg.fd) != 0 && err == NULL)
555 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
556 876c234b 2018-09-10 stsp imsg_free(&imsg);
561 c59b3346 2018-09-11 stsp if (packidx)
562 c59b3346 2018-09-11 stsp got_packidx_close(packidx);
564 c59b3346 2018-09-11 stsp got_pack_close(pack);
565 48d5fe42 2018-09-15 stsp got_object_cache_close(&objcache);
566 876c234b 2018-09-10 stsp imsg_clear(&ibuf);
568 80d5f134 2018-11-11 stsp if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
569 80d5f134 2018-11-11 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
570 99437157 2018-11-11 stsp got_privsep_send_error(&ibuf, err);
573 3a6ce05a 2019-02-11 stsp if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
574 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
575 876c234b 2018-09-10 stsp return err ? 1 : 0;