2 * Copyright (c) 2018 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_inflate.h"
40 #include "got_lib_object.h"
41 #include "got_lib_object_cache.h"
42 #include "got_lib_object_parse.h"
43 #include "got_lib_privsep.h"
44 #include "got_lib_pack.h"
46 static volatile sig_atomic_t sigint_received;
49 catch_sigint(int signo)
54 static const struct got_error *
55 object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
56 struct got_packidx *packidx, struct got_object_cache *objcache)
58 const struct got_error *err = NULL;
59 struct got_imsg_packed_object iobj;
60 struct got_object *obj;
61 struct got_object_id id;
64 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
65 if (datalen != sizeof(iobj))
66 return got_error(GOT_ERR_PRIVSEP_LEN);
67 memcpy(&iobj, imsg->data, sizeof(iobj));
68 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
70 err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, &id);
75 err = got_object_cache_add(objcache, &obj->id, obj);
80 err = got_privsep_send_obj(ibuf, obj);
82 got_object_close(obj);
86 static const struct got_error *
87 commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
88 struct got_packidx *packidx, struct got_object_cache *objcache)
90 const struct got_error *err = NULL;
91 struct got_imsg_packed_object iobj;
92 struct got_object *obj;
93 struct got_commit_object *commit = NULL;
96 struct got_object_id id;
99 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
100 if (datalen != sizeof(iobj))
101 return got_error(GOT_ERR_PRIVSEP_LEN);
102 memcpy(&iobj, imsg->data, sizeof(iobj));
103 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
105 err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, &id);
109 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
114 err = got_object_parse_commit(&commit, buf, len);
117 got_object_close(obj);
121 err = got_privsep_send_commit(ibuf, commit);
122 got_object_commit_close(commit);
124 if (err->code == GOT_ERR_PRIVSEP_PIPE)
127 got_privsep_send_error(ibuf, err);
133 static const struct got_error *
134 tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
135 struct got_packidx *packidx, struct got_object_cache *objcache)
137 const struct got_error *err = NULL;
138 struct got_imsg_packed_object iobj;
139 struct got_object *obj = NULL;
140 struct got_tree_object *tree = NULL;
143 struct got_object_id id;
146 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
147 if (datalen != sizeof(iobj))
148 return got_error(GOT_ERR_PRIVSEP_LEN);
149 memcpy(&iobj, imsg->data, sizeof(iobj));
150 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
152 err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, &id);
156 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
161 err = got_object_parse_tree(&tree, buf, len);
164 err = got_privsep_send_tree(ibuf, tree);
166 got_object_close(obj);
167 got_object_tree_close(tree);
169 if (err->code == GOT_ERR_PRIVSEP_PIPE)
172 got_privsep_send_error(ibuf, err);
178 static const struct got_error *
179 receive_file(FILE **f, struct imsgbuf *ibuf, int imsg_code)
181 const struct got_error *err;
185 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
189 if (imsg.hdr.type != imsg_code) {
190 err = got_error(GOT_ERR_PRIVSEP_MSG);
194 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
196 err = got_error(GOT_ERR_PRIVSEP_LEN);
200 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
204 *f = fdopen(imsg.fd, "w+");
207 err = got_error_from_errno();
215 static const struct got_error *
216 blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
217 struct got_packidx *packidx, struct got_object_cache *objcache)
219 const struct got_error *err = NULL;
220 struct got_imsg_packed_object iobj;
221 struct got_object *obj = NULL;
222 FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
223 struct got_object_id id;
228 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
229 if (datalen != sizeof(iobj))
230 return got_error(GOT_ERR_PRIVSEP_LEN);
231 memcpy(&iobj, imsg->data, sizeof(iobj));
232 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
234 err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, &id);
238 err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
241 err = receive_file(&basefile, ibuf, GOT_IMSG_TMPFD);
244 err = receive_file(&accumfile, ibuf, GOT_IMSG_TMPFD);
248 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
249 err = got_pack_get_max_delta_object_size(&blob_size, obj);
253 blob_size = obj->size;
255 if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
256 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
259 err = got_packfile_extract_object(pack, obj, outfile, basefile,
264 err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
274 got_object_close(obj);
275 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
276 got_privsep_send_error(ibuf, err);
281 static const struct got_error *
282 tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
283 struct got_packidx *packidx, struct got_object_cache *objcache)
285 const struct got_error *err = NULL;
286 struct got_imsg_packed_object iobj;
287 struct got_object *obj = NULL;
288 struct got_tag_object *tag = NULL;
291 struct got_object_id id;
294 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
295 if (datalen != sizeof(iobj))
296 return got_error(GOT_ERR_PRIVSEP_LEN);
297 memcpy(&iobj, imsg->data, sizeof(iobj));
298 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
300 err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, &id);
304 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
309 err = got_object_parse_tag(&tag, buf, len);
312 err = got_privsep_send_tag(ibuf, tag);
314 got_object_close(obj);
315 got_object_tag_close(tag);
317 if (err->code == GOT_ERR_PRIVSEP_PIPE)
320 got_privsep_send_error(ibuf, err);
326 static const struct got_error *
327 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
329 const struct got_error *err = NULL;
331 struct got_imsg_packidx ipackidx;
333 struct got_packidx *p;
337 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
341 p = calloc(1, sizeof(*p));
343 err = got_error_from_errno();
347 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
348 err = got_error(GOT_ERR_PRIVSEP_MSG);
353 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
357 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
358 if (datalen != sizeof(ipackidx)) {
359 err = got_error(GOT_ERR_PRIVSEP_LEN);
362 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
364 p->len = ipackidx.len;
365 p->fd = dup(imsg.fd);
367 err = got_error_from_errno();
370 if (lseek(p->fd, 0, SEEK_SET) == -1) {
371 err = got_error_from_errno();
375 #ifndef GOT_PACK_NO_MMAP
376 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
377 if (p->map == MAP_FAILED)
378 p->map = NULL; /* fall back to read(2) */
380 err = got_packidx_init_hdr(p, 1);
385 got_packidx_close(p);
392 static const struct got_error *
393 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
395 const struct got_error *err = NULL;
397 struct got_imsg_pack ipack;
399 struct got_pack *pack;
403 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
407 pack = calloc(1, sizeof(*pack));
409 err = got_error_from_errno();
413 if (imsg.hdr.type != GOT_IMSG_PACK) {
414 err = got_error(GOT_ERR_PRIVSEP_MSG);
419 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
423 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
424 if (datalen != sizeof(ipack)) {
425 err = got_error(GOT_ERR_PRIVSEP_LEN);
428 memcpy(&ipack, imsg.data, sizeof(ipack));
430 pack->filesize = ipack.filesize;
431 pack->fd = dup(imsg.fd);
432 if (pack->fd == -1) {
433 err = got_error_from_errno();
436 if (lseek(pack->fd, 0, SEEK_SET) == -1) {
437 err = got_error_from_errno();
440 pack->path_packfile = strdup(ipack.path_packfile);
441 if (pack->path_packfile == NULL) {
442 err = got_error_from_errno();
446 #ifndef GOT_PACK_NO_MMAP
447 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
449 if (pack->map == MAP_FAILED)
450 pack->map = NULL; /* fall back to read(2) */
464 main(int argc, char *argv[])
466 const struct got_error *err = NULL;
469 struct got_packidx *packidx = NULL;
470 struct got_pack *pack = NULL;
471 struct got_object_cache objcache;
473 //static int attached;
474 //while (!attached) sleep(1);
476 signal(SIGINT, catch_sigint);
478 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
480 err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
482 err = got_error_from_errno();
483 got_privsep_send_error(&ibuf, err);
488 /* revoke access to most system calls */
489 if (pledge("stdio recvfd", NULL) == -1) {
490 err = got_error_from_errno();
491 got_privsep_send_error(&ibuf, err);
496 err = receive_packidx(&packidx, &ibuf);
498 got_privsep_send_error(&ibuf, err);
502 err = receive_pack(&pack, &ibuf);
504 got_privsep_send_error(&ibuf, err);
511 if (sigint_received) {
512 err = got_error(GOT_ERR_CANCELLED);
516 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
518 if (err->code == GOT_ERR_PRIVSEP_PIPE)
523 if (imsg.hdr.type == GOT_IMSG_STOP)
526 switch (imsg.hdr.type) {
527 case GOT_IMSG_PACKED_OBJECT_REQUEST:
528 err = object_request(&imsg, &ibuf, pack, packidx,
531 case GOT_IMSG_COMMIT_REQUEST:
532 err = commit_request(&imsg, &ibuf, pack, packidx,
535 case GOT_IMSG_TREE_REQUEST:
536 err = tree_request(&imsg, &ibuf, pack, packidx,
539 case GOT_IMSG_BLOB_REQUEST:
540 err = blob_request(&imsg, &ibuf, pack, packidx,
543 case GOT_IMSG_TAG_REQUEST:
544 err = tag_request(&imsg, &ibuf, pack, packidx,
548 err = got_error(GOT_ERR_PRIVSEP_MSG);
560 got_packidx_close(packidx);
562 got_pack_close(pack);
563 got_object_cache_close(&objcache);
566 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
567 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
568 got_privsep_send_error(&ibuf, err);
571 close(GOT_IMSG_FD_CHILD);