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/syslimits.h>
34 #include "got_error.h"
35 #include "got_object.h"
37 #include "got_lib_delta.h"
38 #include "got_lib_object.h"
39 #include "got_lib_object_cache.h"
40 #include "got_lib_object_parse.h"
41 #include "got_lib_privsep.h"
42 #include "got_lib_pack.h"
44 static volatile sig_atomic_t sigint_received;
47 catch_sigint(int signo)
52 static const struct got_error *
53 open_object(struct got_object **obj, struct got_pack *pack,
54 struct got_packidx *packidx, int idx, struct got_object_id *id,
55 struct got_object_cache *objcache)
57 const struct got_error *err;
59 err = got_packfile_open_object(obj, pack, packidx, idx, id);
64 err = got_object_cache_add(objcache, id, *obj);
66 if (err->code == GOT_ERR_OBJ_EXISTS ||
67 err->code == GOT_ERR_OBJ_TOO_LARGE)
75 static const struct got_error *
76 object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
77 struct got_packidx *packidx, struct got_object_cache *objcache)
79 const struct got_error *err = NULL;
80 struct got_imsg_packed_object iobj;
81 struct got_object *obj;
82 struct got_object_id id;
85 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
86 if (datalen != sizeof(iobj))
87 return got_error(GOT_ERR_PRIVSEP_LEN);
88 memcpy(&iobj, imsg->data, sizeof(iobj));
89 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
91 obj = got_object_cache_get(objcache, &id);
95 err = open_object(&obj, pack, packidx, iobj.idx, &id,
101 err = got_privsep_send_obj(ibuf, obj);
103 got_object_close(obj);
107 static const struct got_error *
108 commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
109 struct got_packidx *packidx, struct got_object_cache *objcache)
111 const struct got_error *err = NULL;
112 struct got_imsg_packed_object iobj;
113 struct got_object *obj = NULL;
114 struct got_commit_object *commit = NULL;
117 struct got_object_id id;
120 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
121 if (datalen != sizeof(iobj))
122 return got_error(GOT_ERR_PRIVSEP_LEN);
123 memcpy(&iobj, imsg->data, sizeof(iobj));
124 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
126 obj = got_object_cache_get(objcache, &id);
130 err = open_object(&obj, pack, packidx, iobj.idx, &id,
136 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
141 err = got_object_parse_commit(&commit, buf, len);
145 err = got_privsep_send_commit(ibuf, commit);
148 got_object_close(obj);
150 got_object_commit_close(commit);
152 if (err->code == GOT_ERR_PRIVSEP_PIPE)
155 got_privsep_send_error(ibuf, err);
161 static const struct got_error *
162 tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
163 struct got_packidx *packidx, struct got_object_cache *objcache)
165 const struct got_error *err = NULL;
166 struct got_imsg_packed_object iobj;
167 struct got_object *obj = NULL;
168 struct got_tree_object *tree = NULL;
171 struct got_object_id id;
174 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
175 if (datalen != sizeof(iobj))
176 return got_error(GOT_ERR_PRIVSEP_LEN);
177 memcpy(&iobj, imsg->data, sizeof(iobj));
178 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
180 obj = got_object_cache_get(objcache, &id);
184 err = open_object(&obj, pack, packidx, iobj.idx, &id,
190 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
195 err = got_object_parse_tree(&tree, buf, len);
199 err = got_privsep_send_tree(ibuf, tree);
202 got_object_close(obj);
204 got_object_tree_close(tree);
206 if (err->code == GOT_ERR_PRIVSEP_PIPE)
209 got_privsep_send_error(ibuf, err);
215 static const struct got_error *
216 receive_file(FILE **f, struct imsgbuf *ibuf, int imsg_code)
218 const struct got_error *err;
222 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
226 if (imsg.hdr.type != imsg_code) {
227 err = got_error(GOT_ERR_PRIVSEP_MSG);
231 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
233 err = got_error(GOT_ERR_PRIVSEP_LEN);
237 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
241 *f = fdopen(imsg.fd, "w+");
243 err = got_error_from_errno("fdopen");
252 static const struct got_error *
253 blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
254 struct got_packidx *packidx, struct got_object_cache *objcache)
256 const struct got_error *err = NULL;
257 struct got_imsg_packed_object iobj;
258 struct got_object *obj = NULL;
259 FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
260 struct got_object_id id;
265 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
266 if (datalen != sizeof(iobj))
267 return got_error(GOT_ERR_PRIVSEP_LEN);
268 memcpy(&iobj, imsg->data, sizeof(iobj));
269 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
271 obj = got_object_cache_get(objcache, &id);
275 err = open_object(&obj, pack, packidx, iobj.idx, &id,
281 err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
284 err = receive_file(&basefile, ibuf, GOT_IMSG_TMPFD);
287 err = receive_file(&accumfile, ibuf, GOT_IMSG_TMPFD);
291 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
292 err = got_pack_get_max_delta_object_size(&blob_size, obj);
296 blob_size = obj->size;
298 if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
299 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
302 err = got_packfile_extract_object(pack, obj, outfile, basefile,
307 err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
310 if (outfile && fclose(outfile) != 0 && err == NULL)
311 err = got_error_from_errno("fclose");
312 if (basefile && fclose(basefile) != 0 && err == NULL)
313 err = got_error_from_errno("fclose");
314 if (accumfile && fclose(accumfile) != 0 && err == NULL)
315 err = got_error_from_errno("fclose");
316 got_object_close(obj);
317 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
318 got_privsep_send_error(ibuf, err);
323 static const struct got_error *
324 tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
325 struct got_packidx *packidx, struct got_object_cache *objcache)
327 const struct got_error *err = NULL;
328 struct got_imsg_packed_object iobj;
329 struct got_object *obj = NULL;
330 struct got_tag_object *tag = NULL;
333 struct got_object_id id;
336 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
337 if (datalen != sizeof(iobj))
338 return got_error(GOT_ERR_PRIVSEP_LEN);
339 memcpy(&iobj, imsg->data, sizeof(iobj));
340 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
342 obj = got_object_cache_get(objcache, &id);
346 err = open_object(&obj, pack, packidx, iobj.idx, &id,
352 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
357 err = got_object_parse_tag(&tag, buf, len);
361 err = got_privsep_send_tag(ibuf, tag);
364 got_object_close(obj);
366 got_object_tag_close(tag);
368 if (err->code == GOT_ERR_PRIVSEP_PIPE)
371 got_privsep_send_error(ibuf, err);
377 static const struct got_error *
378 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
380 const struct got_error *err = NULL;
382 struct got_imsg_packidx ipackidx;
384 struct got_packidx *p;
388 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
392 p = calloc(1, sizeof(*p));
394 err = got_error_from_errno("calloc");
398 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
399 err = got_error(GOT_ERR_PRIVSEP_MSG);
404 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
408 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
409 if (datalen != sizeof(ipackidx)) {
410 err = got_error(GOT_ERR_PRIVSEP_LEN);
413 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
415 p->len = ipackidx.len;
416 p->fd = dup(imsg.fd);
418 err = got_error_from_errno("dup");
421 if (lseek(p->fd, 0, SEEK_SET) == -1) {
422 err = got_error_from_errno("lseek");
426 #ifndef GOT_PACK_NO_MMAP
427 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
428 if (p->map == MAP_FAILED)
429 p->map = NULL; /* fall back to read(2) */
431 err = got_packidx_init_hdr(p, 1);
436 got_packidx_close(p);
443 static const struct got_error *
444 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
446 const struct got_error *err = NULL;
448 struct got_imsg_pack ipack;
450 struct got_pack *pack;
454 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
458 pack = calloc(1, sizeof(*pack));
460 err = got_error_from_errno("calloc");
464 if (imsg.hdr.type != GOT_IMSG_PACK) {
465 err = got_error(GOT_ERR_PRIVSEP_MSG);
470 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
474 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
475 if (datalen != sizeof(ipack)) {
476 err = got_error(GOT_ERR_PRIVSEP_LEN);
479 memcpy(&ipack, imsg.data, sizeof(ipack));
481 pack->filesize = ipack.filesize;
482 pack->fd = dup(imsg.fd);
483 if (pack->fd == -1) {
484 err = got_error_from_errno("dup");
487 if (lseek(pack->fd, 0, SEEK_SET) == -1) {
488 err = got_error_from_errno("lseek");
491 pack->path_packfile = strdup(ipack.path_packfile);
492 if (pack->path_packfile == NULL) {
493 err = got_error_from_errno("strdup");
497 #ifndef GOT_PACK_NO_MMAP
498 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
500 if (pack->map == MAP_FAILED)
501 pack->map = NULL; /* fall back to read(2) */
515 main(int argc, char *argv[])
517 const struct got_error *err = NULL;
520 struct got_packidx *packidx = NULL;
521 struct got_pack *pack = NULL;
522 struct got_object_cache objcache;
524 //static int attached;
525 //while (!attached) sleep(1);
527 signal(SIGINT, catch_sigint);
529 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
531 err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
533 err = got_error_from_errno("got_object_cache_init");
534 got_privsep_send_error(&ibuf, err);
539 /* revoke access to most system calls */
540 if (pledge("stdio recvfd", NULL) == -1) {
541 err = got_error_from_errno("pledge");
542 got_privsep_send_error(&ibuf, err);
547 err = receive_packidx(&packidx, &ibuf);
549 got_privsep_send_error(&ibuf, err);
553 err = receive_pack(&pack, &ibuf);
555 got_privsep_send_error(&ibuf, err);
562 if (sigint_received) {
563 err = got_error(GOT_ERR_CANCELLED);
567 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
569 if (err->code == GOT_ERR_PRIVSEP_PIPE)
574 if (imsg.hdr.type == GOT_IMSG_STOP)
577 switch (imsg.hdr.type) {
578 case GOT_IMSG_PACKED_OBJECT_REQUEST:
579 err = object_request(&imsg, &ibuf, pack, packidx,
582 case GOT_IMSG_COMMIT_REQUEST:
583 err = commit_request(&imsg, &ibuf, pack, packidx,
586 case GOT_IMSG_TREE_REQUEST:
587 err = tree_request(&imsg, &ibuf, pack, packidx,
590 case GOT_IMSG_BLOB_REQUEST:
591 err = blob_request(&imsg, &ibuf, pack, packidx,
594 case GOT_IMSG_TAG_REQUEST:
595 err = tag_request(&imsg, &ibuf, pack, packidx,
599 err = got_error(GOT_ERR_PRIVSEP_MSG);
603 if (imsg.fd != -1 && close(imsg.fd) != 0 && err == NULL)
604 err = got_error_from_errno("close");
611 got_packidx_close(packidx);
613 got_pack_close(pack);
614 got_object_cache_close(&objcache);
617 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
618 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
619 got_privsep_send_error(&ibuf, err);
622 if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
623 err = got_error_from_errno("close");