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 c59b3346 2018-09-11 stsp get_object(struct got_object **obj, struct imsg *imsg, struct imsgbuf *ibuf,
88 c59b3346 2018-09-11 stsp struct got_pack *pack, struct got_packidx *packidx,
89 c59b3346 2018-09-11 stsp struct got_object_cache *objcache, int type)
91 c59b3346 2018-09-11 stsp const struct got_error *err = NULL;
92 c59b3346 2018-09-11 stsp struct got_object *iobj;
94 c59b3346 2018-09-11 stsp err = got_privsep_get_imsg_obj(&iobj, imsg, ibuf);
98 c59b3346 2018-09-11 stsp if (iobj->type != type) {
99 c59b3346 2018-09-11 stsp err = got_error(GOT_ERR_OBJ_TYPE);
103 c59b3346 2018-09-11 stsp if ((iobj->flags & GOT_OBJ_FLAG_PACKED) == 0)
104 c59b3346 2018-09-11 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
106 c59b3346 2018-09-11 stsp *obj = got_object_cache_get(objcache, &iobj->id);
107 c59b3346 2018-09-11 stsp if (*obj == NULL) {
108 c59b3346 2018-09-11 stsp err = got_packfile_open_object(obj, pack, packidx,
109 c59b3346 2018-09-11 stsp iobj->pack_idx, &iobj->id);
113 c59b3346 2018-09-11 stsp (*obj)->refcnt++;
115 c59b3346 2018-09-11 stsp got_object_close(iobj);
116 c59b3346 2018-09-11 stsp return err;
119 c59b3346 2018-09-11 stsp static const struct got_error *
120 876c234b 2018-09-10 stsp commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
121 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
123 cfd633c2 2018-09-10 stsp const struct got_error *err = NULL;
124 1785f84a 2018-12-23 stsp struct got_imsg_packed_object iobj;
125 1785f84a 2018-12-23 stsp struct got_object *obj;
126 cfd633c2 2018-09-10 stsp struct got_commit_object *commit = NULL;
127 cfd633c2 2018-09-10 stsp uint8_t *buf;
128 cfd633c2 2018-09-10 stsp size_t len;
129 1785f84a 2018-12-23 stsp struct got_object_id id;
130 1785f84a 2018-12-23 stsp size_t datalen;
132 1785f84a 2018-12-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
133 1785f84a 2018-12-23 stsp if (datalen != sizeof(iobj))
134 1785f84a 2018-12-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
135 1785f84a 2018-12-23 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
136 1785f84a 2018-12-23 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
138 1785f84a 2018-12-23 stsp err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, &id);
140 cfd633c2 2018-09-10 stsp return err;
142 cfd633c2 2018-09-10 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
144 cfd633c2 2018-09-10 stsp return err;
146 cfd633c2 2018-09-10 stsp obj->size = len;
147 cfd633c2 2018-09-10 stsp err = got_object_parse_commit(&commit, buf, len);
150 1785f84a 2018-12-23 stsp got_object_close(obj);
151 1785f84a 2018-12-23 stsp return err;
154 cfd633c2 2018-09-10 stsp err = got_privsep_send_commit(ibuf, commit);
155 cfd633c2 2018-09-10 stsp got_object_commit_close(commit);
157 7762fe12 2018-11-05 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
158 7762fe12 2018-11-05 stsp err = NULL;
160 7762fe12 2018-11-05 stsp got_privsep_send_error(ibuf, err);
163 7762fe12 2018-11-05 stsp return err;
166 7762fe12 2018-11-05 stsp static const struct got_error *
167 876c234b 2018-09-10 stsp tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
168 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
170 e7885405 2018-09-10 stsp const struct got_error *err = NULL;
171 13c729f7 2018-12-24 stsp struct got_imsg_packed_object iobj;
172 e7885405 2018-09-10 stsp struct got_object *obj = NULL;
173 e7885405 2018-09-10 stsp struct got_tree_object *tree = NULL;
174 e7885405 2018-09-10 stsp uint8_t *buf;
175 e7885405 2018-09-10 stsp size_t len;
176 13c729f7 2018-12-24 stsp struct got_object_id id;
177 13c729f7 2018-12-24 stsp size_t datalen;
179 13c729f7 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
180 13c729f7 2018-12-24 stsp if (datalen != sizeof(iobj))
181 13c729f7 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
182 13c729f7 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
183 13c729f7 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
185 13c729f7 2018-12-24 stsp err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, &id);
187 e7885405 2018-09-10 stsp return err;
189 e7885405 2018-09-10 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
191 e7885405 2018-09-10 stsp return err;
193 e7885405 2018-09-10 stsp obj->size = len;
194 e7885405 2018-09-10 stsp err = got_object_parse_tree(&tree, buf, len);
197 e7885405 2018-09-10 stsp err = got_privsep_send_tree(ibuf, tree);
199 e7885405 2018-09-10 stsp got_object_close(obj);
200 e7885405 2018-09-10 stsp got_object_tree_close(tree);
202 e7885405 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
203 e7885405 2018-09-10 stsp err = NULL;
205 e7885405 2018-09-10 stsp got_privsep_send_error(ibuf, err);
208 e7885405 2018-09-10 stsp return err;
211 876c234b 2018-09-10 stsp static const struct got_error *
212 3840f4c9 2018-09-12 stsp receive_file(FILE **f, struct imsgbuf *ibuf, int imsg_code)
214 3840f4c9 2018-09-12 stsp const struct got_error *err;
215 3840f4c9 2018-09-12 stsp struct imsg imsg;
216 55da3778 2018-09-10 stsp size_t datalen;
218 3840f4c9 2018-09-12 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
220 55da3778 2018-09-10 stsp return err;
222 3840f4c9 2018-09-12 stsp if (imsg.hdr.type != imsg_code) {
223 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
227 3840f4c9 2018-09-12 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
228 55da3778 2018-09-10 stsp if (datalen != 0) {
229 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
232 3840f4c9 2018-09-12 stsp if (imsg.fd == -1) {
233 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
237 3840f4c9 2018-09-12 stsp *f = fdopen(imsg.fd, "w+");
238 3840f4c9 2018-09-12 stsp if (*f == NULL) {
239 3840f4c9 2018-09-12 stsp close(imsg.fd);
240 55da3778 2018-09-10 stsp err = got_error_from_errno();
244 3840f4c9 2018-09-12 stsp imsg_free(&imsg);
245 3840f4c9 2018-09-12 stsp return err;
248 3840f4c9 2018-09-12 stsp static const struct got_error *
249 3840f4c9 2018-09-12 stsp blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
250 3840f4c9 2018-09-12 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
252 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
253 3840f4c9 2018-09-12 stsp struct got_object *obj = NULL;
254 3840f4c9 2018-09-12 stsp FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
256 3840f4c9 2018-09-12 stsp err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
257 3840f4c9 2018-09-12 stsp GOT_OBJ_TYPE_BLOB);
259 3840f4c9 2018-09-12 stsp return err;
261 3840f4c9 2018-09-12 stsp err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
263 3840f4c9 2018-09-12 stsp return err;
264 3840f4c9 2018-09-12 stsp err = receive_file(&basefile, ibuf, GOT_IMSG_TMPFD);
266 3840f4c9 2018-09-12 stsp return err;
267 3840f4c9 2018-09-12 stsp err = receive_file(&accumfile, ibuf, GOT_IMSG_TMPFD);
269 3840f4c9 2018-09-12 stsp return err;
271 3840f4c9 2018-09-12 stsp err = got_packfile_extract_object(pack, obj, outfile, basefile,
272 3840f4c9 2018-09-12 stsp accumfile);
276 55da3778 2018-09-10 stsp err = got_privsep_send_blob(ibuf, obj->size);
278 3840f4c9 2018-09-12 stsp if (outfile)
279 3840f4c9 2018-09-12 stsp fclose(outfile);
280 3840f4c9 2018-09-12 stsp if (basefile)
281 3840f4c9 2018-09-12 stsp fclose(basefile);
282 3840f4c9 2018-09-12 stsp if (accumfile)
283 3840f4c9 2018-09-12 stsp fclose(accumfile);
285 c59b3346 2018-09-11 stsp got_object_close(obj);
286 3840f4c9 2018-09-12 stsp if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
287 3840f4c9 2018-09-12 stsp got_privsep_send_error(ibuf, err);
289 55da3778 2018-09-10 stsp return err;
292 876c234b 2018-09-10 stsp static const struct got_error *
293 f4a881ce 2018-11-17 stsp tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
294 f4a881ce 2018-11-17 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
296 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
297 f4a881ce 2018-11-17 stsp struct got_object *obj = NULL;
298 f4a881ce 2018-11-17 stsp struct got_tag_object *tag = NULL;
299 f4a881ce 2018-11-17 stsp uint8_t *buf;
300 f4a881ce 2018-11-17 stsp size_t len;
302 f4a881ce 2018-11-17 stsp err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
303 f4a881ce 2018-11-17 stsp GOT_OBJ_TYPE_TAG);
305 f4a881ce 2018-11-17 stsp return err;
307 f4a881ce 2018-11-17 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
309 f4a881ce 2018-11-17 stsp return err;
311 f4a881ce 2018-11-17 stsp obj->size = len;
312 f4a881ce 2018-11-17 stsp err = got_object_parse_tag(&tag, buf, len);
315 f4a881ce 2018-11-17 stsp err = got_privsep_send_tag(ibuf, tag);
317 f4a881ce 2018-11-17 stsp got_object_close(obj);
318 f4a881ce 2018-11-17 stsp got_object_tag_close(tag);
320 f4a881ce 2018-11-17 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
321 f4a881ce 2018-11-17 stsp err = NULL;
323 f4a881ce 2018-11-17 stsp got_privsep_send_error(ibuf, err);
326 f4a881ce 2018-11-17 stsp return err;
329 f4a881ce 2018-11-17 stsp static const struct got_error *
330 876c234b 2018-09-10 stsp receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
332 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
333 876c234b 2018-09-10 stsp struct imsg imsg;
334 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
335 876c234b 2018-09-10 stsp size_t datalen;
336 876c234b 2018-09-10 stsp struct got_packidx *p;
338 876c234b 2018-09-10 stsp *packidx = NULL;
340 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
342 876c234b 2018-09-10 stsp return err;
344 876c234b 2018-09-10 stsp p = calloc(1, sizeof(*p));
345 876c234b 2018-09-10 stsp if (p == NULL) {
346 876c234b 2018-09-10 stsp err = got_error_from_errno();
350 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
351 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
355 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
356 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
360 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
361 876c234b 2018-09-10 stsp if (datalen != sizeof(ipackidx)) {
362 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
365 876c234b 2018-09-10 stsp memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
367 876c234b 2018-09-10 stsp p->len = ipackidx.len;
368 876c234b 2018-09-10 stsp p->fd = dup(imsg.fd);
369 876c234b 2018-09-10 stsp if (p->fd == -1) {
370 56bef47a 2018-09-15 stsp err = got_error_from_errno();
373 56bef47a 2018-09-15 stsp if (lseek(p->fd, 0, SEEK_SET) == -1) {
374 876c234b 2018-09-10 stsp err = got_error_from_errno();
378 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
379 876c234b 2018-09-10 stsp p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
380 876c234b 2018-09-10 stsp if (p->map == MAP_FAILED)
381 876c234b 2018-09-10 stsp p->map = NULL; /* fall back to read(2) */
383 876c234b 2018-09-10 stsp err = got_packidx_init_hdr(p, 1);
386 876c234b 2018-09-10 stsp if (imsg.fd != -1)
387 876c234b 2018-09-10 stsp close(imsg.fd);
388 876c234b 2018-09-10 stsp got_packidx_close(p);
390 876c234b 2018-09-10 stsp *packidx = p;
391 876c234b 2018-09-10 stsp imsg_free(&imsg);
392 876c234b 2018-09-10 stsp return err;
395 876c234b 2018-09-10 stsp static const struct got_error *
396 876c234b 2018-09-10 stsp receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
398 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
399 876c234b 2018-09-10 stsp struct imsg imsg;
400 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
401 876c234b 2018-09-10 stsp size_t datalen;
402 876c234b 2018-09-10 stsp struct got_pack *pack;
404 876c234b 2018-09-10 stsp *packp = NULL;
406 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
408 876c234b 2018-09-10 stsp return err;
410 876c234b 2018-09-10 stsp pack = calloc(1, sizeof(*pack));
411 876c234b 2018-09-10 stsp if (pack == NULL) {
412 876c234b 2018-09-10 stsp err = got_error_from_errno();
416 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACK) {
417 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
421 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
422 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
426 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
427 876c234b 2018-09-10 stsp if (datalen != sizeof(ipack)) {
428 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
431 876c234b 2018-09-10 stsp memcpy(&ipack, imsg.data, sizeof(ipack));
433 876c234b 2018-09-10 stsp pack->filesize = ipack.filesize;
434 876c234b 2018-09-10 stsp pack->fd = dup(imsg.fd);
435 876c234b 2018-09-10 stsp if (pack->fd == -1) {
436 876c234b 2018-09-10 stsp err = got_error_from_errno();
439 56bef47a 2018-09-15 stsp if (lseek(pack->fd, 0, SEEK_SET) == -1) {
440 56bef47a 2018-09-15 stsp err = got_error_from_errno();
443 876c234b 2018-09-10 stsp pack->path_packfile = strdup(ipack.path_packfile);
444 876c234b 2018-09-10 stsp if (pack->path_packfile == NULL) {
445 876c234b 2018-09-10 stsp err = got_error_from_errno();
449 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
450 876c234b 2018-09-10 stsp pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
451 876c234b 2018-09-10 stsp pack->fd, 0);
452 876c234b 2018-09-10 stsp if (pack->map == MAP_FAILED)
453 876c234b 2018-09-10 stsp pack->map = NULL; /* fall back to read(2) */
457 876c234b 2018-09-10 stsp if (imsg.fd != -1)
458 876c234b 2018-09-10 stsp close(imsg.fd);
459 876c234b 2018-09-10 stsp free(pack);
461 876c234b 2018-09-10 stsp *packp = pack;
462 876c234b 2018-09-10 stsp imsg_free(&imsg);
463 876c234b 2018-09-10 stsp return err;
467 876c234b 2018-09-10 stsp main(int argc, char *argv[])
469 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
470 876c234b 2018-09-10 stsp struct imsgbuf ibuf;
471 876c234b 2018-09-10 stsp struct imsg imsg;
472 c59b3346 2018-09-11 stsp struct got_packidx *packidx = NULL;
473 c59b3346 2018-09-11 stsp struct got_pack *pack = NULL;
474 c59b3346 2018-09-11 stsp struct got_object_cache objcache;
476 876c234b 2018-09-10 stsp //static int attached;
477 876c234b 2018-09-10 stsp //while (!attached) sleep(1);
479 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
481 876c234b 2018-09-10 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
483 c59b3346 2018-09-11 stsp err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
485 c59b3346 2018-09-11 stsp err = got_error_from_errno();
486 c59b3346 2018-09-11 stsp got_privsep_send_error(&ibuf, err);
490 2ff12563 2018-09-15 stsp #ifndef PROFILE
491 876c234b 2018-09-10 stsp /* revoke access to most system calls */
492 876c234b 2018-09-10 stsp if (pledge("stdio recvfd", NULL) == -1) {
493 876c234b 2018-09-10 stsp err = got_error_from_errno();
494 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
499 876c234b 2018-09-10 stsp err = receive_packidx(&packidx, &ibuf);
501 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
505 876c234b 2018-09-10 stsp err = receive_pack(&pack, &ibuf);
507 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
511 876c234b 2018-09-10 stsp while (1) {
512 876c234b 2018-09-10 stsp imsg.fd = -1;
514 99437157 2018-11-11 stsp if (sigint_received) {
515 99437157 2018-11-11 stsp err = got_error(GOT_ERR_CANCELLED);
519 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
521 876c234b 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
522 876c234b 2018-09-10 stsp err = NULL;
526 876c234b 2018-09-10 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
529 876c234b 2018-09-10 stsp switch (imsg.hdr.type) {
530 876c234b 2018-09-10 stsp case GOT_IMSG_PACKED_OBJECT_REQUEST:
531 c59b3346 2018-09-11 stsp err = object_request(&imsg, &ibuf, pack, packidx,
532 c59b3346 2018-09-11 stsp &objcache);
534 876c234b 2018-09-10 stsp case GOT_IMSG_COMMIT_REQUEST:
535 c59b3346 2018-09-11 stsp err = commit_request(&imsg, &ibuf, pack, packidx,
536 7762fe12 2018-11-05 stsp &objcache);
538 876c234b 2018-09-10 stsp case GOT_IMSG_TREE_REQUEST:
539 c59b3346 2018-09-11 stsp err = tree_request(&imsg, &ibuf, pack, packidx,
540 c59b3346 2018-09-11 stsp &objcache);
542 876c234b 2018-09-10 stsp case GOT_IMSG_BLOB_REQUEST:
543 c59b3346 2018-09-11 stsp err = blob_request(&imsg, &ibuf, pack, packidx,
544 c59b3346 2018-09-11 stsp &objcache);
546 f4a881ce 2018-11-17 stsp case GOT_IMSG_TAG_REQUEST:
547 f4a881ce 2018-11-17 stsp err = tag_request(&imsg, &ibuf, pack, packidx,
548 f4a881ce 2018-11-17 stsp &objcache);
551 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
555 876c234b 2018-09-10 stsp if (imsg.fd != -1)
556 876c234b 2018-09-10 stsp close(imsg.fd);
557 876c234b 2018-09-10 stsp imsg_free(&imsg);
562 c59b3346 2018-09-11 stsp if (packidx)
563 c59b3346 2018-09-11 stsp got_packidx_close(packidx);
565 c59b3346 2018-09-11 stsp got_pack_close(pack);
566 48d5fe42 2018-09-15 stsp got_object_cache_close(&objcache);
567 876c234b 2018-09-10 stsp imsg_clear(&ibuf);
569 80d5f134 2018-11-11 stsp if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
570 80d5f134 2018-11-11 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
571 99437157 2018-11-11 stsp got_privsep_send_error(&ibuf, err);
574 876c234b 2018-09-10 stsp close(GOT_IMSG_FD_CHILD);
575 876c234b 2018-09-10 stsp return err ? 1 : 0;