Blame


1 876c234b 2018-09-10 stsp /*
2 876c234b 2018-09-10 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 876c234b 2018-09-10 stsp *
4 876c234b 2018-09-10 stsp * Permission to use, copy, modify, and distribute this software for any
5 876c234b 2018-09-10 stsp * purpose with or without fee is hereby granted, provided that the above
6 876c234b 2018-09-10 stsp * copyright notice and this permission notice appear in all copies.
7 876c234b 2018-09-10 stsp *
8 876c234b 2018-09-10 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 876c234b 2018-09-10 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 876c234b 2018-09-10 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 876c234b 2018-09-10 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 876c234b 2018-09-10 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 876c234b 2018-09-10 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 876c234b 2018-09-10 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 876c234b 2018-09-10 stsp */
16 876c234b 2018-09-10 stsp
17 876c234b 2018-09-10 stsp #include <sys/types.h>
18 876c234b 2018-09-10 stsp #include <sys/queue.h>
19 876c234b 2018-09-10 stsp #include <sys/uio.h>
20 876c234b 2018-09-10 stsp #include <sys/time.h>
21 876c234b 2018-09-10 stsp #include <sys/limits.h>
22 876c234b 2018-09-10 stsp #include <sys/syslimits.h>
23 876c234b 2018-09-10 stsp #include <sys/mman.h>
24 876c234b 2018-09-10 stsp
25 876c234b 2018-09-10 stsp #include <limits.h>
26 876c234b 2018-09-10 stsp #include <stdint.h>
27 876c234b 2018-09-10 stsp #include <imsg.h>
28 876c234b 2018-09-10 stsp #include <stdio.h>
29 876c234b 2018-09-10 stsp #include <stdlib.h>
30 876c234b 2018-09-10 stsp #include <string.h>
31 876c234b 2018-09-10 stsp #include <sha1.h>
32 876c234b 2018-09-10 stsp #include <zlib.h>
33 876c234b 2018-09-10 stsp
34 876c234b 2018-09-10 stsp #include "got_error.h"
35 876c234b 2018-09-10 stsp #include "got_object.h"
36 876c234b 2018-09-10 stsp
37 876c234b 2018-09-10 stsp #include "got_lib_delta.h"
38 876c234b 2018-09-10 stsp #include "got_lib_inflate.h"
39 876c234b 2018-09-10 stsp #include "got_lib_object.h"
40 c59b3346 2018-09-11 stsp #include "got_lib_object_cache.h"
41 876c234b 2018-09-10 stsp #include "got_lib_object_parse.h"
42 876c234b 2018-09-10 stsp #include "got_lib_privsep.h"
43 876c234b 2018-09-10 stsp #include "got_lib_pack.h"
44 876c234b 2018-09-10 stsp
45 876c234b 2018-09-10 stsp static const struct got_error *
46 876c234b 2018-09-10 stsp object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
47 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
48 876c234b 2018-09-10 stsp {
49 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
50 876c234b 2018-09-10 stsp struct got_imsg_packed_object iobj;
51 876c234b 2018-09-10 stsp struct got_object *obj;
52 876c234b 2018-09-10 stsp size_t datalen;
53 876c234b 2018-09-10 stsp
54 876c234b 2018-09-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
55 876c234b 2018-09-10 stsp if (datalen != sizeof(iobj))
56 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
57 876c234b 2018-09-10 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
58 876c234b 2018-09-10 stsp
59 876c234b 2018-09-10 stsp err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, NULL);
60 876c234b 2018-09-10 stsp if (err)
61 876c234b 2018-09-10 stsp return err;
62 c59b3346 2018-09-11 stsp obj->refcnt++;
63 876c234b 2018-09-10 stsp
64 c59b3346 2018-09-11 stsp err = got_object_cache_add(objcache, &obj->id, obj);
65 c59b3346 2018-09-11 stsp if (err)
66 c59b3346 2018-09-11 stsp goto done;
67 c59b3346 2018-09-11 stsp obj->refcnt++;
68 c59b3346 2018-09-11 stsp
69 876c234b 2018-09-10 stsp err = got_privsep_send_obj(ibuf, obj);
70 c59b3346 2018-09-11 stsp done:
71 876c234b 2018-09-10 stsp got_object_close(obj);
72 876c234b 2018-09-10 stsp return err;
73 876c234b 2018-09-10 stsp }
74 876c234b 2018-09-10 stsp
75 876c234b 2018-09-10 stsp static const struct got_error *
76 c59b3346 2018-09-11 stsp get_object(struct got_object **obj, struct imsg *imsg, struct imsgbuf *ibuf,
77 c59b3346 2018-09-11 stsp struct got_pack *pack, struct got_packidx *packidx,
78 c59b3346 2018-09-11 stsp struct got_object_cache *objcache, int type)
79 c59b3346 2018-09-11 stsp {
80 c59b3346 2018-09-11 stsp const struct got_error *err = NULL;
81 c59b3346 2018-09-11 stsp struct got_object *iobj;
82 c59b3346 2018-09-11 stsp
83 c59b3346 2018-09-11 stsp err = got_privsep_get_imsg_obj(&iobj, imsg, ibuf);
84 c59b3346 2018-09-11 stsp if (err)
85 c59b3346 2018-09-11 stsp return err;
86 c59b3346 2018-09-11 stsp
87 c59b3346 2018-09-11 stsp if (iobj->type != type) {
88 c59b3346 2018-09-11 stsp err = got_error(GOT_ERR_OBJ_TYPE);
89 c59b3346 2018-09-11 stsp goto done;
90 c59b3346 2018-09-11 stsp }
91 c59b3346 2018-09-11 stsp
92 c59b3346 2018-09-11 stsp if ((iobj->flags & GOT_OBJ_FLAG_PACKED) == 0)
93 c59b3346 2018-09-11 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
94 c59b3346 2018-09-11 stsp
95 c59b3346 2018-09-11 stsp *obj = got_object_cache_get(objcache, &iobj->id);
96 c59b3346 2018-09-11 stsp if (*obj == NULL) {
97 c59b3346 2018-09-11 stsp err = got_packfile_open_object(obj, pack, packidx,
98 c59b3346 2018-09-11 stsp iobj->pack_idx, &iobj->id);
99 c59b3346 2018-09-11 stsp if (err)
100 c59b3346 2018-09-11 stsp goto done;
101 c59b3346 2018-09-11 stsp }
102 c59b3346 2018-09-11 stsp (*obj)->refcnt++;
103 c59b3346 2018-09-11 stsp done:
104 c59b3346 2018-09-11 stsp got_object_close(iobj);
105 c59b3346 2018-09-11 stsp return err;
106 c59b3346 2018-09-11 stsp }
107 c59b3346 2018-09-11 stsp
108 c59b3346 2018-09-11 stsp static const struct got_error *
109 876c234b 2018-09-10 stsp commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
110 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
111 876c234b 2018-09-10 stsp {
112 cfd633c2 2018-09-10 stsp const struct got_error *err = NULL;
113 cfd633c2 2018-09-10 stsp struct got_object *obj = NULL;
114 cfd633c2 2018-09-10 stsp struct got_commit_object *commit = NULL;
115 cfd633c2 2018-09-10 stsp uint8_t *buf;
116 cfd633c2 2018-09-10 stsp size_t len;
117 cfd633c2 2018-09-10 stsp
118 c59b3346 2018-09-11 stsp err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
119 c59b3346 2018-09-11 stsp GOT_OBJ_TYPE_COMMIT);
120 cfd633c2 2018-09-10 stsp if (err)
121 cfd633c2 2018-09-10 stsp return err;
122 cfd633c2 2018-09-10 stsp
123 cfd633c2 2018-09-10 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
124 cfd633c2 2018-09-10 stsp if (err)
125 cfd633c2 2018-09-10 stsp return err;
126 cfd633c2 2018-09-10 stsp
127 cfd633c2 2018-09-10 stsp obj->size = len;
128 cfd633c2 2018-09-10 stsp err = got_object_parse_commit(&commit, buf, len);
129 cfd633c2 2018-09-10 stsp free(buf);
130 cfd633c2 2018-09-10 stsp
131 cfd633c2 2018-09-10 stsp err = got_privsep_send_commit(ibuf, commit);
132 cfd633c2 2018-09-10 stsp if (obj)
133 cfd633c2 2018-09-10 stsp got_object_close(obj);
134 cfd633c2 2018-09-10 stsp got_object_commit_close(commit);
135 cfd633c2 2018-09-10 stsp if (err) {
136 cfd633c2 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
137 cfd633c2 2018-09-10 stsp err = NULL;
138 cfd633c2 2018-09-10 stsp else
139 cfd633c2 2018-09-10 stsp got_privsep_send_error(ibuf, err);
140 cfd633c2 2018-09-10 stsp }
141 cfd633c2 2018-09-10 stsp
142 cfd633c2 2018-09-10 stsp return err;
143 876c234b 2018-09-10 stsp }
144 876c234b 2018-09-10 stsp
145 876c234b 2018-09-10 stsp static const struct got_error *
146 876c234b 2018-09-10 stsp tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
147 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
148 876c234b 2018-09-10 stsp {
149 e7885405 2018-09-10 stsp const struct got_error *err = NULL;
150 e7885405 2018-09-10 stsp struct got_object *obj = NULL;
151 e7885405 2018-09-10 stsp struct got_tree_object *tree = NULL;
152 e7885405 2018-09-10 stsp uint8_t *buf;
153 e7885405 2018-09-10 stsp size_t len;
154 e7885405 2018-09-10 stsp
155 c59b3346 2018-09-11 stsp err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
156 c59b3346 2018-09-11 stsp GOT_OBJ_TYPE_TREE);
157 e7885405 2018-09-10 stsp if (err)
158 e7885405 2018-09-10 stsp return err;
159 e7885405 2018-09-10 stsp
160 e7885405 2018-09-10 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
161 e7885405 2018-09-10 stsp if (err)
162 e7885405 2018-09-10 stsp return err;
163 e7885405 2018-09-10 stsp
164 e7885405 2018-09-10 stsp obj->size = len;
165 e7885405 2018-09-10 stsp err = got_object_parse_tree(&tree, buf, len);
166 e7885405 2018-09-10 stsp free(buf);
167 e7885405 2018-09-10 stsp
168 e7885405 2018-09-10 stsp err = got_privsep_send_tree(ibuf, tree);
169 e7885405 2018-09-10 stsp if (obj)
170 e7885405 2018-09-10 stsp got_object_close(obj);
171 e7885405 2018-09-10 stsp got_object_tree_close(tree);
172 e7885405 2018-09-10 stsp if (err) {
173 e7885405 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
174 e7885405 2018-09-10 stsp err = NULL;
175 e7885405 2018-09-10 stsp else
176 e7885405 2018-09-10 stsp got_privsep_send_error(ibuf, err);
177 e7885405 2018-09-10 stsp }
178 e7885405 2018-09-10 stsp
179 e7885405 2018-09-10 stsp return err;
180 876c234b 2018-09-10 stsp }
181 876c234b 2018-09-10 stsp
182 876c234b 2018-09-10 stsp static const struct got_error *
183 3840f4c9 2018-09-12 stsp receive_file(FILE **f, struct imsgbuf *ibuf, int imsg_code)
184 876c234b 2018-09-10 stsp {
185 3840f4c9 2018-09-12 stsp const struct got_error *err;
186 3840f4c9 2018-09-12 stsp struct imsg imsg;
187 55da3778 2018-09-10 stsp size_t datalen;
188 55da3778 2018-09-10 stsp
189 3840f4c9 2018-09-12 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
190 55da3778 2018-09-10 stsp if (err)
191 55da3778 2018-09-10 stsp return err;
192 55da3778 2018-09-10 stsp
193 3840f4c9 2018-09-12 stsp if (imsg.hdr.type != imsg_code) {
194 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
195 55da3778 2018-09-10 stsp goto done;
196 55da3778 2018-09-10 stsp }
197 55da3778 2018-09-10 stsp
198 3840f4c9 2018-09-12 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
199 55da3778 2018-09-10 stsp if (datalen != 0) {
200 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
201 55da3778 2018-09-10 stsp goto done;
202 55da3778 2018-09-10 stsp }
203 3840f4c9 2018-09-12 stsp if (imsg.fd == -1) {
204 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
205 55da3778 2018-09-10 stsp goto done;
206 55da3778 2018-09-10 stsp }
207 55da3778 2018-09-10 stsp
208 3840f4c9 2018-09-12 stsp *f = fdopen(imsg.fd, "w+");
209 3840f4c9 2018-09-12 stsp if (*f == NULL) {
210 3840f4c9 2018-09-12 stsp close(imsg.fd);
211 55da3778 2018-09-10 stsp err = got_error_from_errno();
212 55da3778 2018-09-10 stsp goto done;
213 55da3778 2018-09-10 stsp }
214 3840f4c9 2018-09-12 stsp done:
215 3840f4c9 2018-09-12 stsp imsg_free(&imsg);
216 3840f4c9 2018-09-12 stsp return err;
217 3840f4c9 2018-09-12 stsp }
218 55da3778 2018-09-10 stsp
219 3840f4c9 2018-09-12 stsp static const struct got_error *
220 3840f4c9 2018-09-12 stsp blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
221 3840f4c9 2018-09-12 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
222 3840f4c9 2018-09-12 stsp {
223 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
224 3840f4c9 2018-09-12 stsp struct got_object *obj = NULL;
225 3840f4c9 2018-09-12 stsp FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
226 3840f4c9 2018-09-12 stsp
227 3840f4c9 2018-09-12 stsp err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
228 3840f4c9 2018-09-12 stsp GOT_OBJ_TYPE_BLOB);
229 55da3778 2018-09-10 stsp if (err)
230 3840f4c9 2018-09-12 stsp return err;
231 3840f4c9 2018-09-12 stsp
232 3840f4c9 2018-09-12 stsp err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
233 3840f4c9 2018-09-12 stsp if (err)
234 3840f4c9 2018-09-12 stsp return err;
235 3840f4c9 2018-09-12 stsp err = receive_file(&basefile, ibuf, GOT_IMSG_TMPFD);
236 3840f4c9 2018-09-12 stsp if (err)
237 3840f4c9 2018-09-12 stsp return err;
238 3840f4c9 2018-09-12 stsp err = receive_file(&accumfile, ibuf, GOT_IMSG_TMPFD);
239 3840f4c9 2018-09-12 stsp if (err)
240 3840f4c9 2018-09-12 stsp return err;
241 3840f4c9 2018-09-12 stsp
242 3840f4c9 2018-09-12 stsp err = got_packfile_extract_object(pack, obj, outfile, basefile,
243 3840f4c9 2018-09-12 stsp accumfile);
244 3840f4c9 2018-09-12 stsp if (err)
245 55da3778 2018-09-10 stsp goto done;
246 55da3778 2018-09-10 stsp
247 55da3778 2018-09-10 stsp err = got_privsep_send_blob(ibuf, obj->size);
248 55da3778 2018-09-10 stsp done:
249 3840f4c9 2018-09-12 stsp if (outfile)
250 3840f4c9 2018-09-12 stsp fclose(outfile);
251 3840f4c9 2018-09-12 stsp if (basefile)
252 3840f4c9 2018-09-12 stsp fclose(basefile);
253 3840f4c9 2018-09-12 stsp if (accumfile)
254 3840f4c9 2018-09-12 stsp fclose(accumfile);
255 c59b3346 2018-09-11 stsp if (obj)
256 c59b3346 2018-09-11 stsp got_object_close(obj);
257 3840f4c9 2018-09-12 stsp if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
258 3840f4c9 2018-09-12 stsp got_privsep_send_error(ibuf, err);
259 55da3778 2018-09-10 stsp
260 55da3778 2018-09-10 stsp return err;
261 876c234b 2018-09-10 stsp }
262 876c234b 2018-09-10 stsp
263 876c234b 2018-09-10 stsp static const struct got_error *
264 876c234b 2018-09-10 stsp receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
265 876c234b 2018-09-10 stsp {
266 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
267 876c234b 2018-09-10 stsp struct imsg imsg;
268 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
269 876c234b 2018-09-10 stsp size_t datalen;
270 876c234b 2018-09-10 stsp struct got_packidx *p;
271 876c234b 2018-09-10 stsp
272 876c234b 2018-09-10 stsp *packidx = NULL;
273 876c234b 2018-09-10 stsp
274 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
275 876c234b 2018-09-10 stsp if (err)
276 876c234b 2018-09-10 stsp return err;
277 876c234b 2018-09-10 stsp
278 876c234b 2018-09-10 stsp p = calloc(1, sizeof(*p));
279 876c234b 2018-09-10 stsp if (p == NULL) {
280 876c234b 2018-09-10 stsp err = got_error_from_errno();
281 876c234b 2018-09-10 stsp goto done;
282 876c234b 2018-09-10 stsp }
283 876c234b 2018-09-10 stsp
284 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
285 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
286 876c234b 2018-09-10 stsp goto done;
287 876c234b 2018-09-10 stsp }
288 876c234b 2018-09-10 stsp
289 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
290 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
291 876c234b 2018-09-10 stsp goto done;
292 876c234b 2018-09-10 stsp }
293 876c234b 2018-09-10 stsp
294 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
295 876c234b 2018-09-10 stsp if (datalen != sizeof(ipackidx)) {
296 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
297 876c234b 2018-09-10 stsp goto done;
298 876c234b 2018-09-10 stsp }
299 876c234b 2018-09-10 stsp memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
300 876c234b 2018-09-10 stsp
301 876c234b 2018-09-10 stsp p->len = ipackidx.len;
302 876c234b 2018-09-10 stsp p->fd = dup(imsg.fd);
303 876c234b 2018-09-10 stsp if (p->fd == -1) {
304 876c234b 2018-09-10 stsp err = got_error_from_errno();
305 876c234b 2018-09-10 stsp goto done;
306 876c234b 2018-09-10 stsp }
307 876c234b 2018-09-10 stsp
308 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
309 876c234b 2018-09-10 stsp p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
310 876c234b 2018-09-10 stsp if (p->map == MAP_FAILED)
311 876c234b 2018-09-10 stsp p->map = NULL; /* fall back to read(2) */
312 876c234b 2018-09-10 stsp #endif
313 876c234b 2018-09-10 stsp err = got_packidx_init_hdr(p, 1);
314 876c234b 2018-09-10 stsp done:
315 876c234b 2018-09-10 stsp if (err) {
316 876c234b 2018-09-10 stsp if (imsg.fd != -1)
317 876c234b 2018-09-10 stsp close(imsg.fd);
318 876c234b 2018-09-10 stsp got_packidx_close(p);
319 876c234b 2018-09-10 stsp } else
320 876c234b 2018-09-10 stsp *packidx = p;
321 876c234b 2018-09-10 stsp imsg_free(&imsg);
322 876c234b 2018-09-10 stsp return err;
323 876c234b 2018-09-10 stsp }
324 876c234b 2018-09-10 stsp
325 876c234b 2018-09-10 stsp static const struct got_error *
326 876c234b 2018-09-10 stsp receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
327 876c234b 2018-09-10 stsp {
328 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
329 876c234b 2018-09-10 stsp struct imsg imsg;
330 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
331 876c234b 2018-09-10 stsp size_t datalen;
332 876c234b 2018-09-10 stsp struct got_pack *pack;
333 876c234b 2018-09-10 stsp
334 876c234b 2018-09-10 stsp *packp = NULL;
335 876c234b 2018-09-10 stsp
336 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
337 876c234b 2018-09-10 stsp if (err)
338 876c234b 2018-09-10 stsp return err;
339 876c234b 2018-09-10 stsp
340 876c234b 2018-09-10 stsp pack = calloc(1, sizeof(*pack));
341 876c234b 2018-09-10 stsp if (pack == NULL) {
342 876c234b 2018-09-10 stsp err = got_error_from_errno();
343 876c234b 2018-09-10 stsp goto done;
344 876c234b 2018-09-10 stsp }
345 876c234b 2018-09-10 stsp
346 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACK) {
347 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
348 876c234b 2018-09-10 stsp goto done;
349 876c234b 2018-09-10 stsp }
350 876c234b 2018-09-10 stsp
351 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
352 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
353 876c234b 2018-09-10 stsp goto done;
354 876c234b 2018-09-10 stsp }
355 876c234b 2018-09-10 stsp
356 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
357 876c234b 2018-09-10 stsp if (datalen != sizeof(ipack)) {
358 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
359 876c234b 2018-09-10 stsp goto done;
360 876c234b 2018-09-10 stsp }
361 876c234b 2018-09-10 stsp memcpy(&ipack, imsg.data, sizeof(ipack));
362 876c234b 2018-09-10 stsp
363 876c234b 2018-09-10 stsp pack->filesize = ipack.filesize;
364 876c234b 2018-09-10 stsp pack->fd = dup(imsg.fd);
365 876c234b 2018-09-10 stsp if (pack->fd == -1) {
366 876c234b 2018-09-10 stsp err = got_error_from_errno();
367 876c234b 2018-09-10 stsp goto done;
368 876c234b 2018-09-10 stsp }
369 876c234b 2018-09-10 stsp pack->path_packfile = strdup(ipack.path_packfile);
370 876c234b 2018-09-10 stsp if (pack->path_packfile == NULL) {
371 876c234b 2018-09-10 stsp err = got_error_from_errno();
372 876c234b 2018-09-10 stsp goto done;
373 876c234b 2018-09-10 stsp }
374 876c234b 2018-09-10 stsp
375 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
376 876c234b 2018-09-10 stsp pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
377 876c234b 2018-09-10 stsp pack->fd, 0);
378 876c234b 2018-09-10 stsp if (pack->map == MAP_FAILED)
379 876c234b 2018-09-10 stsp pack->map = NULL; /* fall back to read(2) */
380 876c234b 2018-09-10 stsp #endif
381 876c234b 2018-09-10 stsp done:
382 876c234b 2018-09-10 stsp if (err) {
383 876c234b 2018-09-10 stsp if (imsg.fd != -1)
384 876c234b 2018-09-10 stsp close(imsg.fd);
385 876c234b 2018-09-10 stsp free(pack);
386 876c234b 2018-09-10 stsp } else
387 876c234b 2018-09-10 stsp *packp = pack;
388 876c234b 2018-09-10 stsp imsg_free(&imsg);
389 876c234b 2018-09-10 stsp return err;
390 876c234b 2018-09-10 stsp }
391 876c234b 2018-09-10 stsp
392 876c234b 2018-09-10 stsp int
393 876c234b 2018-09-10 stsp main(int argc, char *argv[])
394 876c234b 2018-09-10 stsp {
395 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
396 876c234b 2018-09-10 stsp struct imsgbuf ibuf;
397 876c234b 2018-09-10 stsp struct imsg imsg;
398 c59b3346 2018-09-11 stsp struct got_packidx *packidx = NULL;
399 c59b3346 2018-09-11 stsp struct got_pack *pack = NULL;
400 c59b3346 2018-09-11 stsp struct got_object_cache objcache;
401 876c234b 2018-09-10 stsp
402 876c234b 2018-09-10 stsp //static int attached;
403 876c234b 2018-09-10 stsp //while (!attached) sleep(1);
404 876c234b 2018-09-10 stsp
405 876c234b 2018-09-10 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
406 876c234b 2018-09-10 stsp
407 c59b3346 2018-09-11 stsp err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
408 c59b3346 2018-09-11 stsp if (err) {
409 c59b3346 2018-09-11 stsp err = got_error_from_errno();
410 c59b3346 2018-09-11 stsp got_privsep_send_error(&ibuf, err);
411 c59b3346 2018-09-11 stsp return 1;
412 c59b3346 2018-09-11 stsp }
413 c59b3346 2018-09-11 stsp
414 2ff12563 2018-09-15 stsp #ifndef PROFILE
415 876c234b 2018-09-10 stsp /* revoke access to most system calls */
416 876c234b 2018-09-10 stsp if (pledge("stdio recvfd", NULL) == -1) {
417 876c234b 2018-09-10 stsp err = got_error_from_errno();
418 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
419 876c234b 2018-09-10 stsp return 1;
420 876c234b 2018-09-10 stsp }
421 2ff12563 2018-09-15 stsp #endif
422 876c234b 2018-09-10 stsp
423 876c234b 2018-09-10 stsp err = receive_packidx(&packidx, &ibuf);
424 876c234b 2018-09-10 stsp if (err) {
425 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
426 876c234b 2018-09-10 stsp return 1;
427 876c234b 2018-09-10 stsp }
428 876c234b 2018-09-10 stsp
429 876c234b 2018-09-10 stsp err = receive_pack(&pack, &ibuf);
430 876c234b 2018-09-10 stsp if (err) {
431 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
432 876c234b 2018-09-10 stsp return 1;
433 876c234b 2018-09-10 stsp }
434 876c234b 2018-09-10 stsp
435 876c234b 2018-09-10 stsp while (1) {
436 876c234b 2018-09-10 stsp imsg.fd = -1;
437 876c234b 2018-09-10 stsp
438 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
439 876c234b 2018-09-10 stsp if (err) {
440 876c234b 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
441 876c234b 2018-09-10 stsp err = NULL;
442 876c234b 2018-09-10 stsp break;
443 876c234b 2018-09-10 stsp }
444 876c234b 2018-09-10 stsp
445 876c234b 2018-09-10 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
446 876c234b 2018-09-10 stsp break;
447 876c234b 2018-09-10 stsp
448 876c234b 2018-09-10 stsp switch (imsg.hdr.type) {
449 876c234b 2018-09-10 stsp case GOT_IMSG_PACKED_OBJECT_REQUEST:
450 c59b3346 2018-09-11 stsp err = object_request(&imsg, &ibuf, pack, packidx,
451 c59b3346 2018-09-11 stsp &objcache);
452 876c234b 2018-09-10 stsp break;
453 876c234b 2018-09-10 stsp case GOT_IMSG_COMMIT_REQUEST:
454 c59b3346 2018-09-11 stsp err = commit_request(&imsg, &ibuf, pack, packidx,
455 c59b3346 2018-09-11 stsp &objcache);
456 876c234b 2018-09-10 stsp break;
457 876c234b 2018-09-10 stsp case GOT_IMSG_TREE_REQUEST:
458 c59b3346 2018-09-11 stsp err = tree_request(&imsg, &ibuf, pack, packidx,
459 c59b3346 2018-09-11 stsp &objcache);
460 876c234b 2018-09-10 stsp break;
461 876c234b 2018-09-10 stsp case GOT_IMSG_BLOB_REQUEST:
462 c59b3346 2018-09-11 stsp err = blob_request(&imsg, &ibuf, pack, packidx,
463 c59b3346 2018-09-11 stsp &objcache);
464 876c234b 2018-09-10 stsp break;
465 876c234b 2018-09-10 stsp default:
466 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
467 876c234b 2018-09-10 stsp break;
468 876c234b 2018-09-10 stsp }
469 876c234b 2018-09-10 stsp
470 876c234b 2018-09-10 stsp if (imsg.fd != -1)
471 876c234b 2018-09-10 stsp close(imsg.fd);
472 876c234b 2018-09-10 stsp imsg_free(&imsg);
473 876c234b 2018-09-10 stsp if (err) {
474 876c234b 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
475 876c234b 2018-09-10 stsp err = NULL;
476 876c234b 2018-09-10 stsp else
477 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
478 876c234b 2018-09-10 stsp break;
479 876c234b 2018-09-10 stsp }
480 876c234b 2018-09-10 stsp }
481 876c234b 2018-09-10 stsp
482 c59b3346 2018-09-11 stsp if (packidx)
483 c59b3346 2018-09-11 stsp got_packidx_close(packidx);
484 c59b3346 2018-09-11 stsp if (pack)
485 c59b3346 2018-09-11 stsp got_pack_close(pack);
486 876c234b 2018-09-10 stsp imsg_clear(&ibuf);
487 876c234b 2018-09-10 stsp if (err)
488 876c234b 2018-09-10 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
489 876c234b 2018-09-10 stsp close(GOT_IMSG_FD_CHILD);
490 876c234b 2018-09-10 stsp return err ? 1 : 0;
491 876c234b 2018-09-10 stsp }