Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/uio.h>
20 #include <sys/time.h>
21 #include <sys/syslimits.h>
23 #include <stdint.h>
24 #include <imsg.h>
25 #include <limits.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sha1.h>
31 #include <zlib.h>
33 #include "got_error.h"
34 #include "got_object.h"
36 #include "got_lib_delta.h"
37 #include "got_lib_inflate.h"
38 #include "got_lib_object.h"
39 #include "got_lib_object_parse.h"
40 #include "got_lib_privsep.h"
42 #ifndef nitems
43 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
44 #endif
46 #define GOT_OBJ_TAG_COMMIT "commit"
47 #define GOT_OBJ_TAG_TREE "tree"
48 #define GOT_OBJ_TAG_BLOB "blob"
49 #define GOT_OBJ_TAG_TAG "tag"
51 static volatile sig_atomic_t sigint_received;
53 static void
54 catch_sigint(int signo)
55 {
56 sigint_received = 1;
57 }
59 int
60 main(int argc, char *argv[])
61 {
62 const struct got_error *err = NULL;
63 struct got_object *obj = NULL;
64 struct imsg imsg;
65 struct imsgbuf ibuf;
66 size_t datalen;
68 signal(SIGINT, catch_sigint);
70 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
72 #ifndef PROFILE
73 /* revoke access to most system calls */
74 if (pledge("stdio recvfd", NULL) == -1) {
75 err = got_error_from_errno("pledge");
76 got_privsep_send_error(&ibuf, err);
77 return 1;
78 }
79 #endif
81 for (;;) {
82 if (sigint_received) {
83 err = got_error(GOT_ERR_CANCELLED);
84 break;
85 }
87 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
88 if (err) {
89 if (err->code == GOT_ERR_PRIVSEP_PIPE)
90 err = NULL;
91 break;
92 }
94 if (imsg.hdr.type == GOT_IMSG_STOP)
95 break;
97 if (imsg.hdr.type != GOT_IMSG_OBJECT_REQUEST) {
98 err = got_error(GOT_ERR_PRIVSEP_MSG);
99 goto done;
102 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
103 if (datalen != 0) {
104 err = got_error(GOT_ERR_PRIVSEP_LEN);
105 goto done;
108 err = got_object_read_header(&obj, imsg.fd);
109 if (err)
110 goto done;
112 err = got_privsep_send_obj(&ibuf, obj);
113 done:
114 if (close(imsg.fd) != 0 && err == NULL)
115 err = got_error_from_errno("close");
116 imsg_free(&imsg);
117 if (obj)
118 got_object_close(obj);
119 if (err)
120 break;
123 imsg_clear(&ibuf);
124 if (err) {
125 if(!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
126 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
127 got_privsep_send_error(&ibuf, err);
130 if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
131 err = got_error_from_errno("close");
132 return err ? 1 : 0;