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_object *obj = NULL;
125 struct got_commit_object *commit = NULL;
129 err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
130 GOT_OBJ_TYPE_COMMIT);
134 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
139 err = got_object_parse_commit(&commit, buf, len);
142 err = got_privsep_send_commit(ibuf, commit);
144 got_object_close(obj);
145 got_object_commit_close(commit);
147 if (err->code == GOT_ERR_PRIVSEP_PIPE)
150 got_privsep_send_error(ibuf, err);
156 static const struct got_error *
157 tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
158 struct got_packidx *packidx, struct got_object_cache *objcache)
160 const struct got_error *err = NULL;
161 struct got_object *obj = NULL;
162 struct got_tree_object *tree = NULL;
166 err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
171 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
176 err = got_object_parse_tree(&tree, buf, len);
179 err = got_privsep_send_tree(ibuf, tree);
181 got_object_close(obj);
182 got_object_tree_close(tree);
184 if (err->code == GOT_ERR_PRIVSEP_PIPE)
187 got_privsep_send_error(ibuf, err);
193 static const struct got_error *
194 receive_file(FILE **f, struct imsgbuf *ibuf, int imsg_code)
196 const struct got_error *err;
200 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
204 if (imsg.hdr.type != imsg_code) {
205 err = got_error(GOT_ERR_PRIVSEP_MSG);
209 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
211 err = got_error(GOT_ERR_PRIVSEP_LEN);
215 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
219 *f = fdopen(imsg.fd, "w+");
222 err = got_error_from_errno();
230 static const struct got_error *
231 blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
232 struct got_packidx *packidx, struct got_object_cache *objcache)
234 const struct got_error *err = NULL;
235 struct got_object *obj = NULL;
236 FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
238 err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
243 err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
246 err = receive_file(&basefile, ibuf, GOT_IMSG_TMPFD);
249 err = receive_file(&accumfile, ibuf, GOT_IMSG_TMPFD);
253 err = got_packfile_extract_object(pack, obj, outfile, basefile,
258 err = got_privsep_send_blob(ibuf, obj->size);
267 got_object_close(obj);
268 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
269 got_privsep_send_error(ibuf, err);
274 static const struct got_error *
275 tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
276 struct got_packidx *packidx, struct got_object_cache *objcache)
278 const struct got_error *err = NULL;
279 struct got_object *obj = NULL;
280 struct got_tag_object *tag = NULL;
284 err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
289 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
294 err = got_object_parse_tag(&tag, buf, len);
297 err = got_privsep_send_tag(ibuf, tag);
299 got_object_close(obj);
300 got_object_tag_close(tag);
302 if (err->code == GOT_ERR_PRIVSEP_PIPE)
305 got_privsep_send_error(ibuf, err);
311 static const struct got_error *
312 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
314 const struct got_error *err = NULL;
316 struct got_imsg_packidx ipackidx;
318 struct got_packidx *p;
322 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
326 p = calloc(1, sizeof(*p));
328 err = got_error_from_errno();
332 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
333 err = got_error(GOT_ERR_PRIVSEP_MSG);
338 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
342 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
343 if (datalen != sizeof(ipackidx)) {
344 err = got_error(GOT_ERR_PRIVSEP_LEN);
347 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
349 p->len = ipackidx.len;
350 p->fd = dup(imsg.fd);
352 err = got_error_from_errno();
355 if (lseek(p->fd, 0, SEEK_SET) == -1) {
356 err = got_error_from_errno();
360 #ifndef GOT_PACK_NO_MMAP
361 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
362 if (p->map == MAP_FAILED)
363 p->map = NULL; /* fall back to read(2) */
365 err = got_packidx_init_hdr(p, 1);
370 got_packidx_close(p);
377 static const struct got_error *
378 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
380 const struct got_error *err = NULL;
382 struct got_imsg_pack ipack;
384 struct got_pack *pack;
388 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
392 pack = calloc(1, sizeof(*pack));
394 err = got_error_from_errno();
398 if (imsg.hdr.type != GOT_IMSG_PACK) {
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(ipack)) {
410 err = got_error(GOT_ERR_PRIVSEP_LEN);
413 memcpy(&ipack, imsg.data, sizeof(ipack));
415 pack->filesize = ipack.filesize;
416 pack->fd = dup(imsg.fd);
417 if (pack->fd == -1) {
418 err = got_error_from_errno();
421 if (lseek(pack->fd, 0, SEEK_SET) == -1) {
422 err = got_error_from_errno();
425 pack->path_packfile = strdup(ipack.path_packfile);
426 if (pack->path_packfile == NULL) {
427 err = got_error_from_errno();
431 #ifndef GOT_PACK_NO_MMAP
432 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
434 if (pack->map == MAP_FAILED)
435 pack->map = NULL; /* fall back to read(2) */
449 main(int argc, char *argv[])
451 const struct got_error *err = NULL;
454 struct got_packidx *packidx = NULL;
455 struct got_pack *pack = NULL;
456 struct got_object_cache objcache;
458 //static int attached;
459 //while (!attached) sleep(1);
461 signal(SIGINT, catch_sigint);
463 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
465 err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
467 err = got_error_from_errno();
468 got_privsep_send_error(&ibuf, err);
473 /* revoke access to most system calls */
474 if (pledge("stdio recvfd", NULL) == -1) {
475 err = got_error_from_errno();
476 got_privsep_send_error(&ibuf, err);
481 err = receive_packidx(&packidx, &ibuf);
483 got_privsep_send_error(&ibuf, err);
487 err = receive_pack(&pack, &ibuf);
489 got_privsep_send_error(&ibuf, err);
496 if (sigint_received) {
497 err = got_error(GOT_ERR_CANCELLED);
501 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
503 if (err->code == GOT_ERR_PRIVSEP_PIPE)
508 if (imsg.hdr.type == GOT_IMSG_STOP)
511 switch (imsg.hdr.type) {
512 case GOT_IMSG_PACKED_OBJECT_REQUEST:
513 err = object_request(&imsg, &ibuf, pack, packidx,
516 case GOT_IMSG_COMMIT_REQUEST:
517 err = commit_request(&imsg, &ibuf, pack, packidx,
520 case GOT_IMSG_TREE_REQUEST:
521 err = tree_request(&imsg, &ibuf, pack, packidx,
524 case GOT_IMSG_BLOB_REQUEST:
525 err = blob_request(&imsg, &ibuf, pack, packidx,
528 case GOT_IMSG_TAG_REQUEST:
529 err = tag_request(&imsg, &ibuf, pack, packidx,
533 err = got_error(GOT_ERR_PRIVSEP_MSG);
545 got_packidx_close(packidx);
547 got_pack_close(pack);
548 got_object_cache_close(&objcache);
551 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
552 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
553 got_privsep_send_error(&ibuf, err);
556 close(GOT_IMSG_FD_CHILD);