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 get_object(struct got_object **obj, struct imsg *imsg, struct imsgbuf *ibuf,
88 struct got_pack *pack, struct got_packidx *packidx,
89 struct got_object_cache *objcache, int type)
91 const struct got_error *err = NULL;
92 struct got_object *iobj;
94 err = got_privsep_get_imsg_obj(&iobj, imsg, ibuf);
98 if (iobj->type != type) {
99 err = got_error(GOT_ERR_OBJ_TYPE);
103 if ((iobj->flags & GOT_OBJ_FLAG_PACKED) == 0)
104 return got_error(GOT_ERR_OBJ_NOT_PACKED);
106 *obj = got_object_cache_get(objcache, &iobj->id);
108 err = got_packfile_open_object(obj, pack, packidx,
109 iobj->pack_idx, &iobj->id);
115 got_object_close(iobj);
119 static const struct got_error *
120 commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
121 struct got_packidx *packidx, struct got_object_cache *objcache)
123 const struct got_error *err = NULL;
124 struct got_imsg_packed_object iobj;
125 struct got_object *obj;
126 struct got_commit_object *commit = NULL;
129 struct got_object_id id;
132 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
133 if (datalen != sizeof(iobj))
134 return got_error(GOT_ERR_PRIVSEP_LEN);
135 memcpy(&iobj, imsg->data, sizeof(iobj));
136 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
138 err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, &id);
142 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
147 err = got_object_parse_commit(&commit, buf, len);
150 got_object_close(obj);
154 err = got_privsep_send_commit(ibuf, commit);
155 got_object_commit_close(commit);
157 if (err->code == GOT_ERR_PRIVSEP_PIPE)
160 got_privsep_send_error(ibuf, err);
166 static const struct got_error *
167 tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
168 struct got_packidx *packidx, struct got_object_cache *objcache)
170 const struct got_error *err = NULL;
171 struct got_object *obj = NULL;
172 struct got_tree_object *tree = NULL;
176 err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
181 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
186 err = got_object_parse_tree(&tree, buf, len);
189 err = got_privsep_send_tree(ibuf, tree);
191 got_object_close(obj);
192 got_object_tree_close(tree);
194 if (err->code == GOT_ERR_PRIVSEP_PIPE)
197 got_privsep_send_error(ibuf, err);
203 static const struct got_error *
204 receive_file(FILE **f, struct imsgbuf *ibuf, int imsg_code)
206 const struct got_error *err;
210 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
214 if (imsg.hdr.type != imsg_code) {
215 err = got_error(GOT_ERR_PRIVSEP_MSG);
219 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
221 err = got_error(GOT_ERR_PRIVSEP_LEN);
225 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
229 *f = fdopen(imsg.fd, "w+");
232 err = got_error_from_errno();
240 static const struct got_error *
241 blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
242 struct got_packidx *packidx, struct got_object_cache *objcache)
244 const struct got_error *err = NULL;
245 struct got_object *obj = NULL;
246 FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
248 err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
253 err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
256 err = receive_file(&basefile, ibuf, GOT_IMSG_TMPFD);
259 err = receive_file(&accumfile, ibuf, GOT_IMSG_TMPFD);
263 err = got_packfile_extract_object(pack, obj, outfile, basefile,
268 err = got_privsep_send_blob(ibuf, obj->size);
277 got_object_close(obj);
278 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
279 got_privsep_send_error(ibuf, err);
284 static const struct got_error *
285 tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
286 struct got_packidx *packidx, struct got_object_cache *objcache)
288 const struct got_error *err = NULL;
289 struct got_object *obj = NULL;
290 struct got_tag_object *tag = NULL;
294 err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
299 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
304 err = got_object_parse_tag(&tag, buf, len);
307 err = got_privsep_send_tag(ibuf, tag);
309 got_object_close(obj);
310 got_object_tag_close(tag);
312 if (err->code == GOT_ERR_PRIVSEP_PIPE)
315 got_privsep_send_error(ibuf, err);
321 static const struct got_error *
322 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
324 const struct got_error *err = NULL;
326 struct got_imsg_packidx ipackidx;
328 struct got_packidx *p;
332 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
336 p = calloc(1, sizeof(*p));
338 err = got_error_from_errno();
342 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
343 err = got_error(GOT_ERR_PRIVSEP_MSG);
348 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
352 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
353 if (datalen != sizeof(ipackidx)) {
354 err = got_error(GOT_ERR_PRIVSEP_LEN);
357 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
359 p->len = ipackidx.len;
360 p->fd = dup(imsg.fd);
362 err = got_error_from_errno();
365 if (lseek(p->fd, 0, SEEK_SET) == -1) {
366 err = got_error_from_errno();
370 #ifndef GOT_PACK_NO_MMAP
371 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
372 if (p->map == MAP_FAILED)
373 p->map = NULL; /* fall back to read(2) */
375 err = got_packidx_init_hdr(p, 1);
380 got_packidx_close(p);
387 static const struct got_error *
388 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
390 const struct got_error *err = NULL;
392 struct got_imsg_pack ipack;
394 struct got_pack *pack;
398 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
402 pack = calloc(1, sizeof(*pack));
404 err = got_error_from_errno();
408 if (imsg.hdr.type != GOT_IMSG_PACK) {
409 err = got_error(GOT_ERR_PRIVSEP_MSG);
414 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
418 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
419 if (datalen != sizeof(ipack)) {
420 err = got_error(GOT_ERR_PRIVSEP_LEN);
423 memcpy(&ipack, imsg.data, sizeof(ipack));
425 pack->filesize = ipack.filesize;
426 pack->fd = dup(imsg.fd);
427 if (pack->fd == -1) {
428 err = got_error_from_errno();
431 if (lseek(pack->fd, 0, SEEK_SET) == -1) {
432 err = got_error_from_errno();
435 pack->path_packfile = strdup(ipack.path_packfile);
436 if (pack->path_packfile == NULL) {
437 err = got_error_from_errno();
441 #ifndef GOT_PACK_NO_MMAP
442 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
444 if (pack->map == MAP_FAILED)
445 pack->map = NULL; /* fall back to read(2) */
459 main(int argc, char *argv[])
461 const struct got_error *err = NULL;
464 struct got_packidx *packidx = NULL;
465 struct got_pack *pack = NULL;
466 struct got_object_cache objcache;
468 //static int attached;
469 //while (!attached) sleep(1);
471 signal(SIGINT, catch_sigint);
473 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
475 err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
477 err = got_error_from_errno();
478 got_privsep_send_error(&ibuf, err);
483 /* revoke access to most system calls */
484 if (pledge("stdio recvfd", NULL) == -1) {
485 err = got_error_from_errno();
486 got_privsep_send_error(&ibuf, err);
491 err = receive_packidx(&packidx, &ibuf);
493 got_privsep_send_error(&ibuf, err);
497 err = receive_pack(&pack, &ibuf);
499 got_privsep_send_error(&ibuf, err);
506 if (sigint_received) {
507 err = got_error(GOT_ERR_CANCELLED);
511 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
513 if (err->code == GOT_ERR_PRIVSEP_PIPE)
518 if (imsg.hdr.type == GOT_IMSG_STOP)
521 switch (imsg.hdr.type) {
522 case GOT_IMSG_PACKED_OBJECT_REQUEST:
523 err = object_request(&imsg, &ibuf, pack, packidx,
526 case GOT_IMSG_COMMIT_REQUEST:
527 err = commit_request(&imsg, &ibuf, pack, packidx,
530 case GOT_IMSG_TREE_REQUEST:
531 err = tree_request(&imsg, &ibuf, pack, packidx,
534 case GOT_IMSG_BLOB_REQUEST:
535 err = blob_request(&imsg, &ibuf, pack, packidx,
538 case GOT_IMSG_TAG_REQUEST:
539 err = tag_request(&imsg, &ibuf, pack, packidx,
543 err = got_error(GOT_ERR_PRIVSEP_MSG);
555 got_packidx_close(packidx);
557 got_pack_close(pack);
558 got_object_cache_close(&objcache);
561 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
562 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
563 got_privsep_send_error(&ibuf, err);
566 close(GOT_IMSG_FD_CHILD);