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 876c234b 2018-09-10 stsp blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
184 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
185 876c234b 2018-09-10 stsp {
186 55da3778 2018-09-10 stsp const struct got_error *err = NULL;
187 55da3778 2018-09-10 stsp struct got_object *obj = NULL;
188 55da3778 2018-09-10 stsp FILE *f = NULL;
189 55da3778 2018-09-10 stsp struct imsg imsg_outfd;
190 55da3778 2018-09-10 stsp size_t datalen;
191 55da3778 2018-09-10 stsp
192 55da3778 2018-09-10 stsp memset(&imsg_outfd, 0, sizeof(imsg_outfd));
193 55da3778 2018-09-10 stsp imsg_outfd.fd = -1;
194 55da3778 2018-09-10 stsp
195 c59b3346 2018-09-11 stsp err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
196 c59b3346 2018-09-11 stsp GOT_OBJ_TYPE_BLOB);
197 55da3778 2018-09-10 stsp if (err)
198 55da3778 2018-09-10 stsp return err;
199 55da3778 2018-09-10 stsp
200 55da3778 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg_outfd, ibuf, 0);
201 55da3778 2018-09-10 stsp if (err)
202 55da3778 2018-09-10 stsp return err;
203 55da3778 2018-09-10 stsp
204 55da3778 2018-09-10 stsp if (imsg_outfd.hdr.type != GOT_IMSG_BLOB_OUTFD) {
205 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
206 55da3778 2018-09-10 stsp goto done;
207 55da3778 2018-09-10 stsp }
208 55da3778 2018-09-10 stsp
209 55da3778 2018-09-10 stsp datalen = imsg_outfd.hdr.len - IMSG_HEADER_SIZE;
210 55da3778 2018-09-10 stsp if (datalen != 0) {
211 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
212 55da3778 2018-09-10 stsp goto done;
213 55da3778 2018-09-10 stsp }
214 55da3778 2018-09-10 stsp if (imsg_outfd.fd == -1) {
215 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
216 55da3778 2018-09-10 stsp goto done;
217 55da3778 2018-09-10 stsp }
218 55da3778 2018-09-10 stsp
219 55da3778 2018-09-10 stsp f = fdopen(imsg_outfd.fd, "w+");
220 55da3778 2018-09-10 stsp if (f == NULL) {
221 55da3778 2018-09-10 stsp err = got_error_from_errno();
222 55da3778 2018-09-10 stsp goto done;
223 55da3778 2018-09-10 stsp }
224 55da3778 2018-09-10 stsp
225 55da3778 2018-09-10 stsp err = got_packfile_extract_object(pack, obj, f);
226 55da3778 2018-09-10 stsp if (err)
227 55da3778 2018-09-10 stsp goto done;
228 55da3778 2018-09-10 stsp
229 55da3778 2018-09-10 stsp err = got_privsep_send_blob(ibuf, obj->size);
230 55da3778 2018-09-10 stsp done:
231 55da3778 2018-09-10 stsp if (f)
232 55da3778 2018-09-10 stsp fclose(f);
233 55da3778 2018-09-10 stsp else if (imsg_outfd.fd != -1)
234 55da3778 2018-09-10 stsp close(imsg_outfd.fd);
235 55da3778 2018-09-10 stsp imsg_free(&imsg_outfd);
236 c59b3346 2018-09-11 stsp if (obj)
237 c59b3346 2018-09-11 stsp got_object_close(obj);
238 55da3778 2018-09-10 stsp if (err) {
239 55da3778 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
240 55da3778 2018-09-10 stsp err = NULL;
241 55da3778 2018-09-10 stsp else
242 55da3778 2018-09-10 stsp got_privsep_send_error(ibuf, err);
243 55da3778 2018-09-10 stsp }
244 55da3778 2018-09-10 stsp
245 55da3778 2018-09-10 stsp return err;
246 876c234b 2018-09-10 stsp }
247 876c234b 2018-09-10 stsp
248 876c234b 2018-09-10 stsp static const struct got_error *
249 876c234b 2018-09-10 stsp receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
250 876c234b 2018-09-10 stsp {
251 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
252 876c234b 2018-09-10 stsp struct imsg imsg;
253 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
254 876c234b 2018-09-10 stsp size_t datalen;
255 876c234b 2018-09-10 stsp struct got_packidx *p;
256 876c234b 2018-09-10 stsp
257 876c234b 2018-09-10 stsp *packidx = NULL;
258 876c234b 2018-09-10 stsp
259 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
260 876c234b 2018-09-10 stsp if (err)
261 876c234b 2018-09-10 stsp return err;
262 876c234b 2018-09-10 stsp
263 876c234b 2018-09-10 stsp p = calloc(1, sizeof(*p));
264 876c234b 2018-09-10 stsp if (p == NULL) {
265 876c234b 2018-09-10 stsp err = got_error_from_errno();
266 876c234b 2018-09-10 stsp goto done;
267 876c234b 2018-09-10 stsp }
268 876c234b 2018-09-10 stsp
269 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
270 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
271 876c234b 2018-09-10 stsp goto done;
272 876c234b 2018-09-10 stsp }
273 876c234b 2018-09-10 stsp
274 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
275 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
276 876c234b 2018-09-10 stsp goto done;
277 876c234b 2018-09-10 stsp }
278 876c234b 2018-09-10 stsp
279 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
280 876c234b 2018-09-10 stsp if (datalen != sizeof(ipackidx)) {
281 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
282 876c234b 2018-09-10 stsp goto done;
283 876c234b 2018-09-10 stsp }
284 876c234b 2018-09-10 stsp memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
285 876c234b 2018-09-10 stsp
286 876c234b 2018-09-10 stsp p->len = ipackidx.len;
287 876c234b 2018-09-10 stsp p->fd = dup(imsg.fd);
288 876c234b 2018-09-10 stsp if (p->fd == -1) {
289 876c234b 2018-09-10 stsp err = got_error_from_errno();
290 876c234b 2018-09-10 stsp goto done;
291 876c234b 2018-09-10 stsp }
292 876c234b 2018-09-10 stsp
293 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
294 876c234b 2018-09-10 stsp p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
295 876c234b 2018-09-10 stsp if (p->map == MAP_FAILED)
296 876c234b 2018-09-10 stsp p->map = NULL; /* fall back to read(2) */
297 876c234b 2018-09-10 stsp #endif
298 876c234b 2018-09-10 stsp err = got_packidx_init_hdr(p, 1);
299 876c234b 2018-09-10 stsp done:
300 876c234b 2018-09-10 stsp if (err) {
301 876c234b 2018-09-10 stsp if (imsg.fd != -1)
302 876c234b 2018-09-10 stsp close(imsg.fd);
303 876c234b 2018-09-10 stsp got_packidx_close(p);
304 876c234b 2018-09-10 stsp } else
305 876c234b 2018-09-10 stsp *packidx = p;
306 876c234b 2018-09-10 stsp imsg_free(&imsg);
307 876c234b 2018-09-10 stsp return err;
308 876c234b 2018-09-10 stsp }
309 876c234b 2018-09-10 stsp
310 876c234b 2018-09-10 stsp static const struct got_error *
311 876c234b 2018-09-10 stsp receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
312 876c234b 2018-09-10 stsp {
313 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
314 876c234b 2018-09-10 stsp struct imsg imsg;
315 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
316 876c234b 2018-09-10 stsp size_t datalen;
317 876c234b 2018-09-10 stsp struct got_pack *pack;
318 876c234b 2018-09-10 stsp
319 876c234b 2018-09-10 stsp *packp = NULL;
320 876c234b 2018-09-10 stsp
321 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
322 876c234b 2018-09-10 stsp if (err)
323 876c234b 2018-09-10 stsp return err;
324 876c234b 2018-09-10 stsp
325 876c234b 2018-09-10 stsp pack = calloc(1, sizeof(*pack));
326 876c234b 2018-09-10 stsp if (pack == NULL) {
327 876c234b 2018-09-10 stsp err = got_error_from_errno();
328 876c234b 2018-09-10 stsp goto done;
329 876c234b 2018-09-10 stsp }
330 876c234b 2018-09-10 stsp
331 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACK) {
332 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
333 876c234b 2018-09-10 stsp goto done;
334 876c234b 2018-09-10 stsp }
335 876c234b 2018-09-10 stsp
336 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
337 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
338 876c234b 2018-09-10 stsp goto done;
339 876c234b 2018-09-10 stsp }
340 876c234b 2018-09-10 stsp
341 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
342 876c234b 2018-09-10 stsp if (datalen != sizeof(ipack)) {
343 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
344 876c234b 2018-09-10 stsp goto done;
345 876c234b 2018-09-10 stsp }
346 876c234b 2018-09-10 stsp memcpy(&ipack, imsg.data, sizeof(ipack));
347 876c234b 2018-09-10 stsp
348 876c234b 2018-09-10 stsp pack->filesize = ipack.filesize;
349 876c234b 2018-09-10 stsp pack->fd = dup(imsg.fd);
350 876c234b 2018-09-10 stsp if (pack->fd == -1) {
351 876c234b 2018-09-10 stsp err = got_error_from_errno();
352 876c234b 2018-09-10 stsp goto done;
353 876c234b 2018-09-10 stsp }
354 876c234b 2018-09-10 stsp pack->path_packfile = strdup(ipack.path_packfile);
355 876c234b 2018-09-10 stsp if (pack->path_packfile == NULL) {
356 876c234b 2018-09-10 stsp err = got_error_from_errno();
357 876c234b 2018-09-10 stsp goto done;
358 876c234b 2018-09-10 stsp }
359 876c234b 2018-09-10 stsp
360 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
361 876c234b 2018-09-10 stsp pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
362 876c234b 2018-09-10 stsp pack->fd, 0);
363 876c234b 2018-09-10 stsp if (pack->map == MAP_FAILED)
364 876c234b 2018-09-10 stsp pack->map = NULL; /* fall back to read(2) */
365 876c234b 2018-09-10 stsp #endif
366 876c234b 2018-09-10 stsp done:
367 876c234b 2018-09-10 stsp if (err) {
368 876c234b 2018-09-10 stsp if (imsg.fd != -1)
369 876c234b 2018-09-10 stsp close(imsg.fd);
370 876c234b 2018-09-10 stsp free(pack);
371 876c234b 2018-09-10 stsp } else
372 876c234b 2018-09-10 stsp *packp = pack;
373 876c234b 2018-09-10 stsp imsg_free(&imsg);
374 876c234b 2018-09-10 stsp return err;
375 876c234b 2018-09-10 stsp }
376 876c234b 2018-09-10 stsp
377 876c234b 2018-09-10 stsp int
378 876c234b 2018-09-10 stsp main(int argc, char *argv[])
379 876c234b 2018-09-10 stsp {
380 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
381 876c234b 2018-09-10 stsp struct imsgbuf ibuf;
382 876c234b 2018-09-10 stsp struct imsg imsg;
383 c59b3346 2018-09-11 stsp struct got_packidx *packidx = NULL;
384 c59b3346 2018-09-11 stsp struct got_pack *pack = NULL;
385 c59b3346 2018-09-11 stsp struct got_object_cache objcache;
386 876c234b 2018-09-10 stsp
387 876c234b 2018-09-10 stsp //static int attached;
388 876c234b 2018-09-10 stsp //while (!attached) sleep(1);
389 876c234b 2018-09-10 stsp
390 876c234b 2018-09-10 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
391 876c234b 2018-09-10 stsp
392 c59b3346 2018-09-11 stsp err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
393 c59b3346 2018-09-11 stsp if (err) {
394 c59b3346 2018-09-11 stsp err = got_error_from_errno();
395 c59b3346 2018-09-11 stsp got_privsep_send_error(&ibuf, err);
396 c59b3346 2018-09-11 stsp return 1;
397 c59b3346 2018-09-11 stsp }
398 c59b3346 2018-09-11 stsp
399 876c234b 2018-09-10 stsp /* revoke access to most system calls */
400 876c234b 2018-09-10 stsp if (pledge("stdio recvfd", NULL) == -1) {
401 876c234b 2018-09-10 stsp err = got_error_from_errno();
402 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
403 876c234b 2018-09-10 stsp return 1;
404 876c234b 2018-09-10 stsp }
405 876c234b 2018-09-10 stsp
406 876c234b 2018-09-10 stsp err = receive_packidx(&packidx, &ibuf);
407 876c234b 2018-09-10 stsp if (err) {
408 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
409 876c234b 2018-09-10 stsp return 1;
410 876c234b 2018-09-10 stsp }
411 876c234b 2018-09-10 stsp
412 876c234b 2018-09-10 stsp err = receive_pack(&pack, &ibuf);
413 876c234b 2018-09-10 stsp if (err) {
414 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
415 876c234b 2018-09-10 stsp return 1;
416 876c234b 2018-09-10 stsp }
417 876c234b 2018-09-10 stsp
418 876c234b 2018-09-10 stsp while (1) {
419 876c234b 2018-09-10 stsp imsg.fd = -1;
420 876c234b 2018-09-10 stsp
421 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
422 876c234b 2018-09-10 stsp if (err) {
423 876c234b 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
424 876c234b 2018-09-10 stsp err = NULL;
425 876c234b 2018-09-10 stsp break;
426 876c234b 2018-09-10 stsp }
427 876c234b 2018-09-10 stsp
428 876c234b 2018-09-10 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
429 876c234b 2018-09-10 stsp break;
430 876c234b 2018-09-10 stsp
431 876c234b 2018-09-10 stsp switch (imsg.hdr.type) {
432 876c234b 2018-09-10 stsp case GOT_IMSG_PACKED_OBJECT_REQUEST:
433 c59b3346 2018-09-11 stsp err = object_request(&imsg, &ibuf, pack, packidx,
434 c59b3346 2018-09-11 stsp &objcache);
435 876c234b 2018-09-10 stsp break;
436 876c234b 2018-09-10 stsp case GOT_IMSG_COMMIT_REQUEST:
437 c59b3346 2018-09-11 stsp err = commit_request(&imsg, &ibuf, pack, packidx,
438 c59b3346 2018-09-11 stsp &objcache);
439 876c234b 2018-09-10 stsp break;
440 876c234b 2018-09-10 stsp case GOT_IMSG_TREE_REQUEST:
441 c59b3346 2018-09-11 stsp err = tree_request(&imsg, &ibuf, pack, packidx,
442 c59b3346 2018-09-11 stsp &objcache);
443 876c234b 2018-09-10 stsp break;
444 876c234b 2018-09-10 stsp case GOT_IMSG_BLOB_REQUEST:
445 c59b3346 2018-09-11 stsp err = blob_request(&imsg, &ibuf, pack, packidx,
446 c59b3346 2018-09-11 stsp &objcache);
447 876c234b 2018-09-10 stsp break;
448 876c234b 2018-09-10 stsp default:
449 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
450 876c234b 2018-09-10 stsp break;
451 876c234b 2018-09-10 stsp }
452 876c234b 2018-09-10 stsp
453 876c234b 2018-09-10 stsp if (imsg.fd != -1)
454 876c234b 2018-09-10 stsp close(imsg.fd);
455 876c234b 2018-09-10 stsp imsg_free(&imsg);
456 876c234b 2018-09-10 stsp if (err) {
457 876c234b 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
458 876c234b 2018-09-10 stsp err = NULL;
459 876c234b 2018-09-10 stsp else
460 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
461 876c234b 2018-09-10 stsp break;
462 876c234b 2018-09-10 stsp }
463 876c234b 2018-09-10 stsp }
464 876c234b 2018-09-10 stsp
465 c59b3346 2018-09-11 stsp if (packidx)
466 c59b3346 2018-09-11 stsp got_packidx_close(packidx);
467 c59b3346 2018-09-11 stsp if (pack)
468 c59b3346 2018-09-11 stsp got_pack_close(pack);
469 876c234b 2018-09-10 stsp imsg_clear(&ibuf);
470 876c234b 2018-09-10 stsp if (err)
471 876c234b 2018-09-10 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
472 876c234b 2018-09-10 stsp close(GOT_IMSG_FD_CHILD);
473 876c234b 2018-09-10 stsp return err ? 1 : 0;
474 876c234b 2018-09-10 stsp }