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>
20 #include <sys/syslimits.h>
34 #include "got_object.h"
35 #include "got_error.h"
37 #include "got_lib_sha1.h"
38 #include "got_lib_delta.h"
39 #include "got_lib_inflate.h"
40 #include "got_lib_object.h"
41 #include "got_lib_object_parse.h"
42 #include "got_lib_privsep.h"
43 #include "got_lib_pack.h"
46 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
49 static const struct got_error *
50 poll_fd(int fd, int events, int timeout)
56 pfd[0].events = events;
58 n = poll(pfd, 1, timeout);
60 return got_error_from_errno();
62 return got_error(GOT_ERR_TIMEOUT);
63 if (pfd[0].revents & (POLLERR | POLLNVAL))
64 return got_error_from_errno();
65 if (pfd[0].revents & (events | POLLHUP))
68 return got_error(GOT_ERR_INTERRUPT);
71 static const struct got_error *
72 read_imsg(struct imsgbuf *ibuf)
74 const struct got_error *err;
77 err = poll_fd(ibuf->fd, POLLIN, INFTIM);
83 if (errno == EAGAIN) /* Could be a file-descriptor leak. */
84 return got_error(GOT_ERR_PRIVSEP_NO_FD);
85 return got_error(GOT_ERR_PRIVSEP_READ);
88 return got_error(GOT_ERR_PRIVSEP_PIPE);
93 const struct got_error *
94 got_privsep_wait_for_child(pid_t pid)
98 waitpid(pid, &child_status, 0);
100 if (!WIFEXITED(child_status))
101 return got_error(GOT_ERR_PRIVSEP_DIED);
103 if (WEXITSTATUS(child_status) != 0)
104 return got_error(GOT_ERR_PRIVSEP_EXIT);
109 const struct got_error *
110 got_privsep_recv_imsg(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
112 const struct got_error *err;
115 n = imsg_get(ibuf, imsg);
117 return got_error_from_errno();
120 err = read_imsg(ibuf);
123 n = imsg_get(ibuf, imsg);
126 if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
127 return got_error(GOT_ERR_PRIVSEP_LEN);
132 static const struct got_error *
133 recv_imsg_error(struct imsg *imsg, size_t datalen)
135 struct got_imsg_error ierr;
137 if (datalen != sizeof(ierr))
138 return got_error(GOT_ERR_PRIVSEP_LEN);
140 memcpy(&ierr, imsg->data, sizeof(ierr));
141 if (ierr.code == GOT_ERR_ERRNO) {
142 static struct got_error serr;
143 serr.code = GOT_ERR_ERRNO;
144 serr.msg = strerror(ierr.errno_code);
148 return got_error(ierr.code);
151 /* Attempt to send an error in an imsg. Complain on stderr as a last resort. */
153 got_privsep_send_error(struct imsgbuf *ibuf, const struct got_error *err)
155 const struct got_error *poll_err;
156 struct got_imsg_error ierr;
159 ierr.code = err->code;
160 if (err->code == GOT_ERR_ERRNO)
161 ierr.errno_code = errno;
164 ret = imsg_compose(ibuf, GOT_IMSG_ERROR, 0, 0, -1, &ierr, sizeof(ierr));
166 fprintf(stderr, "%s: error %d \"%s\": imsg_compose: %s\n",
167 getprogname(), err->code, err->msg, strerror(errno));
171 poll_err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
173 fprintf(stderr, "%s: error %d \"%s\": poll: %s\n",
174 getprogname(), err->code, err->msg, poll_err->msg);
178 ret = imsg_flush(ibuf);
180 fprintf(stderr, "%s: error %d \"%s\": imsg_flush: %s\n",
181 getprogname(), err->code, err->msg, strerror(errno));
186 static const struct got_error *
187 flush_imsg(struct imsgbuf *ibuf)
189 const struct got_error *err;
191 err = poll_fd(ibuf->fd, POLLOUT, INFTIM);
195 if (imsg_flush(ibuf) == -1)
196 return got_error_from_errno();
201 const struct got_error *
202 got_privsep_send_stop(int fd)
204 const struct got_error *err = NULL;
207 imsg_init(&ibuf, fd);
209 if (imsg_compose(&ibuf, GOT_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
210 return got_error_from_errno();
212 err = flush_imsg(&ibuf);
218 init_imsg_object(struct got_imsg_object *iobj, struct got_object *obj)
220 memcpy(iobj->id, obj->id.sha1, sizeof(iobj->id));
221 iobj->type = obj->type;
222 iobj->flags = obj->flags;
223 iobj->hdrlen = obj->hdrlen;
224 iobj->size = obj->size;
225 if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
226 iobj->pack_offset = obj->pack_offset;
227 iobj->pack_idx = obj->pack_idx;
231 const struct got_error *
232 got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd, struct got_object *obj)
234 struct got_imsg_object iobj, *iobjp = NULL;
235 size_t iobj_size = 0;
236 int imsg_code = GOT_IMSG_OBJECT_REQUEST;
240 case GOT_OBJ_TYPE_TREE:
241 imsg_code = GOT_IMSG_TREE_REQUEST;
243 case GOT_OBJ_TYPE_COMMIT:
244 imsg_code = GOT_IMSG_COMMIT_REQUEST;
246 case GOT_OBJ_TYPE_BLOB:
247 imsg_code = GOT_IMSG_BLOB_REQUEST;
250 return got_error(GOT_ERR_OBJ_TYPE);
253 init_imsg_object(&iobj, obj);
256 iobj_size = sizeof(iobj);
259 if (imsg_compose(ibuf, imsg_code, 0, 0, fd, iobjp, iobj_size) == -1)
260 return got_error_from_errno();
262 return flush_imsg(ibuf);
265 const struct got_error *
266 got_privsep_send_mini_commit_req(struct imsgbuf *ibuf, int fd,
267 struct got_object *obj)
269 struct got_imsg_object iobj, *iobjp;
272 if (obj->type != GOT_OBJ_TYPE_COMMIT)
273 return got_error(GOT_ERR_OBJ_TYPE);
275 init_imsg_object(&iobj, obj);
278 iobj_size = sizeof(iobj);
280 if (imsg_compose(ibuf, GOT_IMSG_MINI_COMMIT_REQUEST, 0, 0, fd,
281 iobjp, iobj_size) == -1)
282 return got_error_from_errno();
284 return flush_imsg(ibuf);
287 const struct got_error *
288 got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd)
290 if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, NULL, 0)
292 return got_error_from_errno();
294 return flush_imsg(ibuf);
297 const struct got_error *
298 got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
300 if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
302 return got_error_from_errno();
304 return flush_imsg(ibuf);
307 const struct got_error *
308 got_privsep_send_tmpfd(struct imsgbuf *ibuf, int fd)
310 if (imsg_compose(ibuf, GOT_IMSG_TMPFD, 0, 0, fd, NULL, 0)
312 return got_error_from_errno();
314 return flush_imsg(ibuf);
317 const struct got_error *
318 got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
320 struct got_imsg_object iobj;
322 memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
323 iobj.type = obj->type;
324 iobj.flags = obj->flags;
325 iobj.hdrlen = obj->hdrlen;
326 iobj.size = obj->size;
327 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
328 iobj.pack_offset = obj->pack_offset;
329 iobj.pack_idx = obj->pack_idx;
332 if (imsg_compose(ibuf, GOT_IMSG_OBJECT, 0, 0, -1, &iobj, sizeof(iobj))
334 return got_error_from_errno();
336 return flush_imsg(ibuf);
339 const struct got_error *
340 got_privsep_get_imsg_obj(struct got_object **obj, struct imsg *imsg,
341 struct imsgbuf *ibuf)
343 const struct got_error *err = NULL;
344 struct got_imsg_object iobj;
345 size_t datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
347 if (datalen != sizeof(iobj))
348 return got_error(GOT_ERR_PRIVSEP_LEN);
350 memcpy(&iobj, imsg->data, sizeof(iobj));
352 *obj = calloc(1, sizeof(**obj));
354 return got_error_from_errno();
356 memcpy((*obj)->id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
357 (*obj)->type = iobj.type;
358 (*obj)->flags = iobj.flags;
359 (*obj)->hdrlen = iobj.hdrlen;
360 (*obj)->size = iobj.size;
361 /* path_packfile is handled by caller */
362 if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
363 (*obj)->pack_offset = iobj.pack_offset;
364 (*obj)->pack_idx = iobj.pack_idx;
370 const struct got_error *
371 got_privsep_recv_obj(struct got_object **obj, struct imsgbuf *ibuf)
373 const struct got_error *err = NULL;
376 const size_t min_datalen =
377 MIN(sizeof(struct got_imsg_error), sizeof(struct got_imsg_object));
381 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
385 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
387 switch (imsg.hdr.type) {
389 err = recv_imsg_error(&imsg, datalen);
391 case GOT_IMSG_OBJECT:
392 err = got_privsep_get_imsg_obj(obj, &imsg, ibuf);
395 err = got_error(GOT_ERR_PRIVSEP_MSG);
404 static const struct got_error *
405 send_commit_logmsg(struct imsgbuf *ibuf, struct got_commit_object *commit,
408 const struct got_error *err = NULL;
409 size_t offset, remain;
414 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE, remain);
416 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_LOGMSG, 0, 0, -1,
417 commit->logmsg + offset, n) == -1) {
418 err = got_error_from_errno();
422 err = flush_imsg(ibuf);
433 const struct got_error *
434 got_privsep_send_commit(struct imsgbuf *ibuf, struct got_commit_object *commit)
436 const struct got_error *err = NULL;
437 struct got_imsg_commit_object icommit;
440 struct got_object_qid *qid;
441 size_t logmsg_len = strlen(commit->logmsg);
443 memcpy(icommit.tree_id, commit->tree_id->sha1, sizeof(icommit.tree_id));
444 icommit.author_len = strlen(commit->author);
445 memcpy(&icommit.tm_author, &commit->tm_author,
446 sizeof(icommit.tm_author));
447 icommit.committer_len = strlen(commit->committer);
448 memcpy(&icommit.tm_committer, &commit->tm_committer,
449 sizeof(icommit.tm_committer));
450 icommit.logmsg_len = logmsg_len;
451 icommit.nparents = commit->nparents;
453 total = sizeof(icommit) + icommit.author_len +
454 icommit.committer_len + icommit.nparents * SHA1_DIGEST_LENGTH;
458 return got_error_from_errno();
461 memcpy(buf + len, &icommit, sizeof(icommit));
462 len += sizeof(icommit);
463 memcpy(buf + len, commit->author, icommit.author_len);
464 len += icommit.author_len;
465 memcpy(buf + len, commit->committer, icommit.committer_len);
466 len += icommit.committer_len;
467 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
468 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
469 len += SHA1_DIGEST_LENGTH;
472 if (imsg_compose(ibuf, GOT_IMSG_COMMIT, 0, 0, -1, buf, len) == -1) {
473 err = got_error_from_errno();
477 if (logmsg_len == 0 ||
478 logmsg_len + len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
479 err = flush_imsg(ibuf);
483 err = send_commit_logmsg(ibuf, commit, logmsg_len);
489 const struct got_error *
490 got_privsep_send_mini_commit(struct imsgbuf *ibuf,
491 struct got_commit_object_mini *commit)
493 const struct got_error *err = NULL;
494 struct got_imsg_commit_object_mini icommit;
497 struct got_object_qid *qid;
499 memcpy(icommit.tree_id, commit->tree_id->sha1, sizeof(icommit.tree_id));
500 memcpy(&icommit.tm_committer, &commit->tm_committer,
501 sizeof(icommit.tm_committer));
502 icommit.nparents = commit->nparents;
504 total = sizeof(icommit) + icommit.nparents * SHA1_DIGEST_LENGTH;
508 return got_error_from_errno();
511 memcpy(buf + len, &icommit, sizeof(icommit));
512 len += sizeof(icommit);
513 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
514 memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
515 len += SHA1_DIGEST_LENGTH;
518 if (imsg_compose(ibuf, GOT_IMSG_MINI_COMMIT, 0, 0, -1,
520 err = got_error_from_errno();
526 return flush_imsg(ibuf);
529 const struct got_error *
530 got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
532 const struct got_error *err = NULL;
534 struct got_imsg_commit_object icommit;
537 const size_t min_datalen =
538 MIN(sizeof(struct got_imsg_error),
539 sizeof(struct got_imsg_commit_object));
544 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
549 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
552 switch (imsg.hdr.type) {
554 err = recv_imsg_error(&imsg, datalen);
556 case GOT_IMSG_COMMIT:
557 if (datalen < sizeof(icommit)) {
558 err = got_error(GOT_ERR_PRIVSEP_LEN);
562 memcpy(&icommit, data, sizeof(icommit));
563 if (datalen != sizeof(icommit) + icommit.author_len +
564 icommit.committer_len +
565 icommit.nparents * SHA1_DIGEST_LENGTH) {
566 err = got_error(GOT_ERR_PRIVSEP_LEN);
569 if (icommit.nparents < 0) {
570 err = got_error(GOT_ERR_PRIVSEP_LEN);
573 len += sizeof(icommit);
575 *commit = got_object_commit_alloc_partial();
576 if (*commit == NULL) {
577 err = got_error_from_errno();
581 memcpy((*commit)->tree_id->sha1, icommit.tree_id,
583 memcpy(&(*commit)->tm_author, &icommit.tm_author,
584 sizeof((*commit)->tm_author));
585 memcpy(&(*commit)->tm_committer, &icommit.tm_committer,
586 sizeof((*commit)->tm_committer));
588 if (icommit.author_len == 0) {
589 (*commit)->author = strdup("");
590 if ((*commit)->author == NULL) {
591 err = got_error_from_errno();
595 (*commit)->author = malloc(icommit.author_len + 1);
596 if ((*commit)->author == NULL) {
597 err = got_error_from_errno();
600 memcpy((*commit)->author, data + len,
602 (*commit)->author[icommit.author_len] = '\0';
604 len += icommit.author_len;
606 if (icommit.committer_len == 0) {
607 (*commit)->committer = strdup("");
608 if ((*commit)->committer == NULL) {
609 err = got_error_from_errno();
613 (*commit)->committer =
614 malloc(icommit.committer_len + 1);
615 if ((*commit)->committer == NULL) {
616 err = got_error_from_errno();
619 memcpy((*commit)->committer, data + len,
620 icommit.committer_len);
621 (*commit)->committer[icommit.committer_len] = '\0';
623 len += icommit.committer_len;
625 if (icommit.logmsg_len == 0) {
626 (*commit)->logmsg = strdup("");
627 if ((*commit)->logmsg == NULL) {
628 err = got_error_from_errno();
632 size_t offset = 0, remain = icommit.logmsg_len;
634 (*commit)->logmsg = malloc(icommit.logmsg_len + 1);
635 if ((*commit)->logmsg == NULL) {
636 err = got_error_from_errno();
640 struct imsg imsg_log;
641 size_t n = MIN(MAX_IMSGSIZE - IMSG_HEADER_SIZE,
644 err = got_privsep_recv_imsg(&imsg_log, ibuf, n);
648 if (imsg_log.hdr.type != GOT_IMSG_COMMIT_LOGMSG)
649 return got_error(GOT_ERR_PRIVSEP_MSG);
651 memcpy((*commit)->logmsg + offset,
653 imsg_free(&imsg_log);
657 (*commit)->logmsg[icommit.logmsg_len] = '\0';
660 for (i = 0; i < icommit.nparents; i++) {
661 struct got_object_qid *qid;
663 err = got_object_qid_alloc_partial(&qid);
667 memcpy(qid->id, data + len + i * SHA1_DIGEST_LENGTH,
669 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
670 (*commit)->nparents++;
674 err = got_error(GOT_ERR_PRIVSEP_MSG);
683 const struct got_error *
684 got_privsep_recv_mini_commit(struct got_commit_object_mini **commit,
685 struct imsgbuf *ibuf)
687 const struct got_error *err = NULL;
689 struct got_imsg_commit_object_mini icommit;
692 const size_t min_datalen =
693 MIN(sizeof(struct got_imsg_error),
694 sizeof(struct got_imsg_commit_object_mini));
699 err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
704 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
707 switch (imsg.hdr.type) {
709 err = recv_imsg_error(&imsg, datalen);
711 case GOT_IMSG_MINI_COMMIT:
712 if (datalen < sizeof(icommit)) {
713 err = got_error(GOT_ERR_PRIVSEP_LEN);
717 memcpy(&icommit, data, sizeof(icommit));
718 if (datalen != sizeof(icommit) +
719 icommit.nparents * SHA1_DIGEST_LENGTH) {
720 err = got_error(GOT_ERR_PRIVSEP_LEN);
723 if (icommit.nparents < 0) {
724 err = got_error(GOT_ERR_PRIVSEP_LEN);
727 len += sizeof(icommit);
729 *commit = got_object_mini_commit_alloc_partial();
730 if (*commit == NULL) {
731 err = got_error_from_errno();
735 memcpy((*commit)->tree_id->sha1, icommit.tree_id,
737 memcpy(&(*commit)->tm_committer, &icommit.tm_committer,
738 sizeof((*commit)->tm_committer));
740 for (i = 0; i < icommit.nparents; i++) {
741 struct got_object_qid *qid;
743 err = got_object_qid_alloc_partial(&qid);
747 memcpy(qid->id, data + len + i * SHA1_DIGEST_LENGTH,
749 SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
750 (*commit)->nparents++;
754 err = got_error(GOT_ERR_PRIVSEP_MSG);
763 const struct got_error *
764 got_privsep_send_tree(struct imsgbuf *ibuf, struct got_tree_object *tree)
766 const struct got_error *err = NULL;
767 struct got_imsg_tree_object itree;
768 struct got_tree_entry *te;
770 int nimsg; /* number of imsg queued in ibuf */
772 itree.nentries = tree->entries.nentries;
773 if (imsg_compose(ibuf, GOT_IMSG_TREE, 0, 0, -1, &itree, sizeof(itree))
775 return got_error_from_errno();
777 totlen = sizeof(itree);
779 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
780 struct got_imsg_tree_entry ite;
782 size_t len = sizeof(ite) + strlen(te->name);
784 if (len > MAX_IMSGSIZE)
785 return got_error(GOT_ERR_NO_SPACE);
788 if (totlen + len >= MAX_IMSGSIZE - (IMSG_HEADER_SIZE * nimsg)) {
789 err = flush_imsg(ibuf);
797 return got_error_from_errno();
799 memcpy(ite.id, te->id->sha1, sizeof(ite.id));
801 memcpy(buf, &ite, sizeof(ite));
802 memcpy(buf + sizeof(ite), te->name, strlen(te->name));
804 if (imsg_compose(ibuf, GOT_IMSG_TREE_ENTRY, 0, 0, -1,
806 err = got_error_from_errno();
813 return flush_imsg(ibuf);
816 const struct got_error *
817 got_privsep_recv_tree(struct got_tree_object **tree, struct imsgbuf *ibuf)
819 const struct got_error *err = NULL;
820 const size_t min_datalen =
821 MIN(sizeof(struct got_imsg_error),
822 sizeof(struct got_imsg_tree_object));
823 struct got_imsg_tree_object itree = { 0 };
828 err = read_imsg(ibuf);
836 struct got_imsg_tree_entry ite;
837 struct got_tree_entry *te = NULL;
839 n = imsg_get(ibuf, &imsg);
841 if (*tree && (*tree)->entries.nentries != nentries)
846 if (imsg.hdr.len < IMSG_HEADER_SIZE + min_datalen)
847 return got_error(GOT_ERR_PRIVSEP_LEN);
849 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
851 switch (imsg.hdr.type) {
853 err = recv_imsg_error(&imsg, datalen);
856 /* This message should only appear once. */
858 err = got_error(GOT_ERR_PRIVSEP_MSG);
861 if (datalen != sizeof(itree)) {
862 err = got_error(GOT_ERR_PRIVSEP_LEN);
865 memcpy(&itree, imsg.data, sizeof(itree));
866 *tree = calloc(1, sizeof(**tree));
868 err = got_error_from_errno();
871 (*tree)->entries.nentries = itree.nentries;
872 SIMPLEQ_INIT(&(*tree)->entries.head);
874 case GOT_IMSG_TREE_ENTRY:
875 /* This message should be preceeded by GOT_IMSG_TREE. */
877 err = got_error(GOT_ERR_PRIVSEP_MSG);
880 if (datalen < sizeof(ite) || datalen > MAX_IMSGSIZE) {
881 err = got_error(GOT_ERR_PRIVSEP_LEN);
885 /* Remaining data contains the entry's name. */
886 datalen -= sizeof(ite);
887 memcpy(&ite, imsg.data, sizeof(ite));
888 if (datalen == 0 || datalen > MAX_IMSGSIZE) {
889 err = got_error(GOT_ERR_PRIVSEP_LEN);
893 te = got_alloc_tree_entry_partial();
895 err = got_error_from_errno();
898 te->name = malloc(datalen + 1);
899 if (te->name == NULL) {
901 err = got_error_from_errno();
904 memcpy(te->name, imsg.data + sizeof(ite), datalen);
905 te->name[datalen] = '\0';
907 memcpy(te->id->sha1, ite.id, SHA1_DIGEST_LENGTH);
909 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
913 err = got_error(GOT_ERR_PRIVSEP_MSG);
920 if (*tree && (*tree)->entries.nentries != nentries) {
922 err = got_error(GOT_ERR_PRIVSEP_LEN);
923 got_object_tree_close(*tree);
930 const struct got_error *
931 got_privsep_send_blob(struct imsgbuf *ibuf, size_t size)
933 struct got_imsg_blob iblob;
936 /* Data has already been written to file descriptor. */
938 if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob, sizeof(iblob))
940 return got_error_from_errno();
942 return flush_imsg(ibuf);
945 const struct got_error *
946 got_privsep_recv_blob(size_t *size, struct imsgbuf *ibuf)
948 const struct got_error *err = NULL;
950 struct got_imsg_blob iblob;
953 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
957 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
959 switch (imsg.hdr.type) {
961 err = recv_imsg_error(&imsg, datalen);
964 if (datalen != sizeof(iblob)) {
965 err = got_error(GOT_ERR_PRIVSEP_LEN);
968 memcpy(&iblob, imsg.data, sizeof(iblob));
970 /* Data has been written to file descriptor. */
973 err = got_error(GOT_ERR_PRIVSEP_MSG);
982 const struct got_error *
983 got_privsep_init_pack_child(struct imsgbuf *ibuf, struct got_pack *pack,
984 struct got_packidx *packidx)
986 struct got_imsg_packidx ipackidx;
987 struct got_imsg_pack ipack;
990 ipackidx.len = packidx->len;
991 fd = dup(packidx->fd);
993 return got_error_from_errno();
995 if (imsg_compose(ibuf, GOT_IMSG_PACKIDX, 0, 0, fd, &ipackidx,
996 sizeof(ipackidx)) == -1)
997 return got_error_from_errno();
999 if (strlcpy(ipack.path_packfile, pack->path_packfile,
1000 sizeof(ipack.path_packfile)) >= sizeof(ipack.path_packfile))
1001 return got_error(GOT_ERR_NO_SPACE);
1002 ipack.filesize = pack->filesize;
1006 return got_error_from_errno();
1008 if (imsg_compose(ibuf, GOT_IMSG_PACK, 0, 0, fd, &ipack, sizeof(ipack))
1010 return got_error_from_errno();
1012 return flush_imsg(ibuf);
1015 const struct got_error *
1016 got_privsep_send_packed_obj_req(struct imsgbuf *ibuf, int idx,
1017 struct got_object_id *id)
1019 struct got_imsg_packed_object iobj;
1022 memcpy(iobj.id, id->sha1, sizeof(iobj.id));
1024 if (imsg_compose(ibuf, GOT_IMSG_PACKED_OBJECT_REQUEST, 0, 0, -1,
1025 &iobj, sizeof(iobj)) == -1)
1026 return got_error_from_errno();
1028 return flush_imsg(ibuf);