2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
18 #include <sys/queue.h>
21 #include <sys/limits.h>
22 #include <sys/syslimits.h>
35 #include "got_error.h"
36 #include "got_object.h"
38 #include "got_lib_delta.h"
39 #include "got_lib_object.h"
40 #include "got_lib_object_cache.h"
41 #include "got_lib_object_parse.h"
42 #include "got_lib_privsep.h"
43 #include "got_lib_pack.h"
45 static volatile sig_atomic_t sigint_received;
48 catch_sigint(int signo)
53 static const struct got_error *
54 open_object(struct got_object **obj, struct got_pack *pack,
55 struct got_packidx *packidx, int idx, struct got_object_id *id,
56 struct got_object_cache *objcache)
58 const struct got_error *err;
60 err = got_packfile_open_object(obj, pack, packidx, idx, id);
65 err = got_object_cache_add(objcache, id, *obj);
67 if (err->code == GOT_ERR_OBJ_EXISTS ||
68 err->code == GOT_ERR_OBJ_TOO_LARGE)
76 static const struct got_error *
77 object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
78 struct got_packidx *packidx, struct got_object_cache *objcache)
80 const struct got_error *err = NULL;
81 struct got_imsg_packed_object iobj;
82 struct got_object *obj;
83 struct got_object_id id;
86 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
87 if (datalen != sizeof(iobj))
88 return got_error(GOT_ERR_PRIVSEP_LEN);
89 memcpy(&iobj, imsg->data, sizeof(iobj));
90 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
92 obj = got_object_cache_get(objcache, &id);
96 err = open_object(&obj, pack, packidx, iobj.idx, &id,
102 err = got_privsep_send_obj(ibuf, obj);
104 got_object_close(obj);
108 static const struct got_error *
109 commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
110 struct got_packidx *packidx, struct got_object_cache *objcache)
112 const struct got_error *err = NULL;
113 struct got_imsg_packed_object iobj;
114 struct got_object *obj = NULL;
115 struct got_commit_object *commit = NULL;
118 struct got_object_id id;
121 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
122 if (datalen != sizeof(iobj))
123 return got_error(GOT_ERR_PRIVSEP_LEN);
124 memcpy(&iobj, imsg->data, sizeof(iobj));
125 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
127 obj = got_object_cache_get(objcache, &id);
131 err = open_object(&obj, pack, packidx, iobj.idx, &id,
137 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
142 err = got_object_parse_commit(&commit, buf, len);
146 err = got_privsep_send_commit(ibuf, commit);
149 got_object_close(obj);
151 got_object_commit_close(commit);
153 if (err->code == GOT_ERR_PRIVSEP_PIPE)
156 got_privsep_send_error(ibuf, err);
162 static const struct got_error *
163 tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
164 struct got_packidx *packidx, struct got_object_cache *objcache)
166 const struct got_error *err = NULL;
167 struct got_imsg_packed_object iobj;
168 struct got_object *obj = NULL;
169 struct got_tree_object *tree = NULL;
172 struct got_object_id id;
175 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
176 if (datalen != sizeof(iobj))
177 return got_error(GOT_ERR_PRIVSEP_LEN);
178 memcpy(&iobj, imsg->data, sizeof(iobj));
179 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
181 obj = got_object_cache_get(objcache, &id);
185 err = open_object(&obj, pack, packidx, iobj.idx, &id,
191 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
196 err = got_object_parse_tree(&tree, buf, len);
200 err = got_privsep_send_tree(ibuf, tree);
203 got_object_close(obj);
205 got_object_tree_close(tree);
207 if (err->code == GOT_ERR_PRIVSEP_PIPE)
210 got_privsep_send_error(ibuf, err);
216 static const struct got_error *
217 receive_file(FILE **f, struct imsgbuf *ibuf, int imsg_code)
219 const struct got_error *err;
223 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
227 if (imsg.hdr.type != imsg_code) {
228 err = got_error(GOT_ERR_PRIVSEP_MSG);
232 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
234 err = got_error(GOT_ERR_PRIVSEP_LEN);
238 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
242 *f = fdopen(imsg.fd, "w+");
244 err = got_error_from_errno("fdopen");
253 static const struct got_error *
254 blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
255 struct got_packidx *packidx, struct got_object_cache *objcache)
257 const struct got_error *err = NULL;
258 struct got_imsg_packed_object iobj;
259 struct got_object *obj = NULL;
260 FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
261 struct got_object_id id;
266 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
267 if (datalen != sizeof(iobj))
268 return got_error(GOT_ERR_PRIVSEP_LEN);
269 memcpy(&iobj, imsg->data, sizeof(iobj));
270 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
272 obj = got_object_cache_get(objcache, &id);
276 err = open_object(&obj, pack, packidx, iobj.idx, &id,
282 err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
285 err = receive_file(&basefile, ibuf, GOT_IMSG_TMPFD);
288 err = receive_file(&accumfile, ibuf, GOT_IMSG_TMPFD);
292 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
293 err = got_pack_get_max_delta_object_size(&blob_size, obj);
297 blob_size = obj->size;
299 if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
300 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
303 err = got_packfile_extract_object(pack, obj, outfile, basefile,
308 err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
311 if (outfile && fclose(outfile) != 0 && err == NULL)
312 err = got_error_from_errno("fclose");
313 if (basefile && fclose(basefile) != 0 && err == NULL)
314 err = got_error_from_errno("fclose");
315 if (accumfile && fclose(accumfile) != 0 && err == NULL)
316 err = got_error_from_errno("fclose");
317 got_object_close(obj);
318 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
319 got_privsep_send_error(ibuf, err);
324 static const struct got_error *
325 tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
326 struct got_packidx *packidx, struct got_object_cache *objcache)
328 const struct got_error *err = NULL;
329 struct got_imsg_packed_object iobj;
330 struct got_object *obj = NULL;
331 struct got_tag_object *tag = NULL;
334 struct got_object_id id;
337 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
338 if (datalen != sizeof(iobj))
339 return got_error(GOT_ERR_PRIVSEP_LEN);
340 memcpy(&iobj, imsg->data, sizeof(iobj));
341 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
343 obj = got_object_cache_get(objcache, &id);
347 err = open_object(&obj, pack, packidx, iobj.idx, &id,
353 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
358 err = got_object_parse_tag(&tag, buf, len);
362 err = got_privsep_send_tag(ibuf, tag);
365 got_object_close(obj);
367 got_object_tag_close(tag);
369 if (err->code == GOT_ERR_PRIVSEP_PIPE)
372 got_privsep_send_error(ibuf, err);
378 static const struct got_error *
379 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
381 const struct got_error *err = NULL;
383 struct got_imsg_packidx ipackidx;
385 struct got_packidx *p;
389 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
393 p = calloc(1, sizeof(*p));
395 err = got_error_from_errno("calloc");
399 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
400 err = got_error(GOT_ERR_PRIVSEP_MSG);
405 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
409 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
410 if (datalen != sizeof(ipackidx)) {
411 err = got_error(GOT_ERR_PRIVSEP_LEN);
414 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
416 p->len = ipackidx.len;
417 p->fd = dup(imsg.fd);
419 err = got_error_from_errno("dup");
422 if (lseek(p->fd, 0, SEEK_SET) == -1) {
423 err = got_error_from_errno("lseek");
427 #ifndef GOT_PACK_NO_MMAP
428 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
429 if (p->map == MAP_FAILED)
430 p->map = NULL; /* fall back to read(2) */
432 err = got_packidx_init_hdr(p, 1);
437 got_packidx_close(p);
444 static const struct got_error *
445 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
447 const struct got_error *err = NULL;
449 struct got_imsg_pack ipack;
451 struct got_pack *pack;
455 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
459 pack = calloc(1, sizeof(*pack));
461 err = got_error_from_errno("calloc");
465 if (imsg.hdr.type != GOT_IMSG_PACK) {
466 err = got_error(GOT_ERR_PRIVSEP_MSG);
471 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
475 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
476 if (datalen != sizeof(ipack)) {
477 err = got_error(GOT_ERR_PRIVSEP_LEN);
480 memcpy(&ipack, imsg.data, sizeof(ipack));
482 pack->filesize = ipack.filesize;
483 pack->fd = dup(imsg.fd);
484 if (pack->fd == -1) {
485 err = got_error_from_errno("dup");
488 if (lseek(pack->fd, 0, SEEK_SET) == -1) {
489 err = got_error_from_errno("lseek");
492 pack->path_packfile = strdup(ipack.path_packfile);
493 if (pack->path_packfile == NULL) {
494 err = got_error_from_errno("strdup");
498 #ifndef GOT_PACK_NO_MMAP
499 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
501 if (pack->map == MAP_FAILED)
502 pack->map = NULL; /* fall back to read(2) */
516 main(int argc, char *argv[])
518 const struct got_error *err = NULL;
521 struct got_packidx *packidx = NULL;
522 struct got_pack *pack = NULL;
523 struct got_object_cache objcache;
525 //static int attached;
526 //while (!attached) sleep(1);
528 signal(SIGINT, catch_sigint);
530 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
532 err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
534 err = got_error_from_errno("got_object_cache_init");
535 got_privsep_send_error(&ibuf, err);
540 /* revoke access to most system calls */
541 if (pledge("stdio recvfd", NULL) == -1) {
542 err = got_error_from_errno("pledge");
543 got_privsep_send_error(&ibuf, err);
548 err = receive_packidx(&packidx, &ibuf);
550 got_privsep_send_error(&ibuf, err);
554 err = receive_pack(&pack, &ibuf);
556 got_privsep_send_error(&ibuf, err);
563 if (sigint_received) {
564 err = got_error(GOT_ERR_CANCELLED);
568 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
570 if (err->code == GOT_ERR_PRIVSEP_PIPE)
575 if (imsg.hdr.type == GOT_IMSG_STOP)
578 switch (imsg.hdr.type) {
579 case GOT_IMSG_PACKED_OBJECT_REQUEST:
580 err = object_request(&imsg, &ibuf, pack, packidx,
583 case GOT_IMSG_COMMIT_REQUEST:
584 err = commit_request(&imsg, &ibuf, pack, packidx,
587 case GOT_IMSG_TREE_REQUEST:
588 err = tree_request(&imsg, &ibuf, pack, packidx,
591 case GOT_IMSG_BLOB_REQUEST:
592 err = blob_request(&imsg, &ibuf, pack, packidx,
595 case GOT_IMSG_TAG_REQUEST:
596 err = tag_request(&imsg, &ibuf, pack, packidx,
600 err = got_error(GOT_ERR_PRIVSEP_MSG);
604 if (imsg.fd != -1 && close(imsg.fd) != 0 && err == NULL)
605 err = got_error_from_errno("close");
612 got_packidx_close(packidx);
614 got_pack_close(pack);
615 got_object_cache_close(&objcache);
618 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
619 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
620 got_privsep_send_error(&ibuf, err);
623 if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
624 err = got_error_from_errno("close");