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 99437157 2018-11-11 stsp #include <signal.h>
27 876c234b 2018-09-10 stsp #include <stdint.h>
28 876c234b 2018-09-10 stsp #include <imsg.h>
29 876c234b 2018-09-10 stsp #include <stdio.h>
30 876c234b 2018-09-10 stsp #include <stdlib.h>
31 876c234b 2018-09-10 stsp #include <string.h>
32 876c234b 2018-09-10 stsp #include <sha1.h>
33 876c234b 2018-09-10 stsp #include <zlib.h>
34 876c234b 2018-09-10 stsp
35 876c234b 2018-09-10 stsp #include "got_error.h"
36 876c234b 2018-09-10 stsp #include "got_object.h"
37 876c234b 2018-09-10 stsp
38 876c234b 2018-09-10 stsp #include "got_lib_delta.h"
39 876c234b 2018-09-10 stsp #include "got_lib_inflate.h"
40 876c234b 2018-09-10 stsp #include "got_lib_object.h"
41 c59b3346 2018-09-11 stsp #include "got_lib_object_cache.h"
42 876c234b 2018-09-10 stsp #include "got_lib_object_parse.h"
43 876c234b 2018-09-10 stsp #include "got_lib_privsep.h"
44 876c234b 2018-09-10 stsp #include "got_lib_pack.h"
45 876c234b 2018-09-10 stsp
46 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
47 99437157 2018-11-11 stsp
48 99437157 2018-11-11 stsp static void
49 99437157 2018-11-11 stsp catch_sigint(int signo)
50 99437157 2018-11-11 stsp {
51 99437157 2018-11-11 stsp sigint_received = 1;
52 99437157 2018-11-11 stsp }
53 99437157 2018-11-11 stsp
54 876c234b 2018-09-10 stsp static const struct got_error *
55 876c234b 2018-09-10 stsp object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
56 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
57 876c234b 2018-09-10 stsp {
58 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
59 876c234b 2018-09-10 stsp struct got_imsg_packed_object iobj;
60 876c234b 2018-09-10 stsp struct got_object *obj;
61 106807b4 2018-09-15 stsp struct got_object_id id;
62 876c234b 2018-09-10 stsp size_t datalen;
63 876c234b 2018-09-10 stsp
64 876c234b 2018-09-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
65 876c234b 2018-09-10 stsp if (datalen != sizeof(iobj))
66 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
67 876c234b 2018-09-10 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
68 106807b4 2018-09-15 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
69 876c234b 2018-09-10 stsp
70 106807b4 2018-09-15 stsp err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, &id);
71 876c234b 2018-09-10 stsp if (err)
72 876c234b 2018-09-10 stsp return err;
73 c59b3346 2018-09-11 stsp obj->refcnt++;
74 876c234b 2018-09-10 stsp
75 c59b3346 2018-09-11 stsp err = got_object_cache_add(objcache, &obj->id, obj);
76 c59b3346 2018-09-11 stsp if (err)
77 c59b3346 2018-09-11 stsp goto done;
78 c59b3346 2018-09-11 stsp obj->refcnt++;
79 c59b3346 2018-09-11 stsp
80 876c234b 2018-09-10 stsp err = got_privsep_send_obj(ibuf, obj);
81 c59b3346 2018-09-11 stsp done:
82 876c234b 2018-09-10 stsp got_object_close(obj);
83 876c234b 2018-09-10 stsp return err;
84 876c234b 2018-09-10 stsp }
85 876c234b 2018-09-10 stsp
86 876c234b 2018-09-10 stsp static const struct got_error *
87 c59b3346 2018-09-11 stsp get_object(struct got_object **obj, struct imsg *imsg, struct imsgbuf *ibuf,
88 c59b3346 2018-09-11 stsp struct got_pack *pack, struct got_packidx *packidx,
89 c59b3346 2018-09-11 stsp struct got_object_cache *objcache, int type)
90 c59b3346 2018-09-11 stsp {
91 c59b3346 2018-09-11 stsp const struct got_error *err = NULL;
92 c59b3346 2018-09-11 stsp struct got_object *iobj;
93 c59b3346 2018-09-11 stsp
94 c59b3346 2018-09-11 stsp err = got_privsep_get_imsg_obj(&iobj, imsg, ibuf);
95 c59b3346 2018-09-11 stsp if (err)
96 c59b3346 2018-09-11 stsp return err;
97 c59b3346 2018-09-11 stsp
98 c59b3346 2018-09-11 stsp if (iobj->type != type) {
99 c59b3346 2018-09-11 stsp err = got_error(GOT_ERR_OBJ_TYPE);
100 c59b3346 2018-09-11 stsp goto done;
101 c59b3346 2018-09-11 stsp }
102 c59b3346 2018-09-11 stsp
103 c59b3346 2018-09-11 stsp if ((iobj->flags & GOT_OBJ_FLAG_PACKED) == 0)
104 c59b3346 2018-09-11 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
105 c59b3346 2018-09-11 stsp
106 c59b3346 2018-09-11 stsp *obj = got_object_cache_get(objcache, &iobj->id);
107 c59b3346 2018-09-11 stsp if (*obj == NULL) {
108 c59b3346 2018-09-11 stsp err = got_packfile_open_object(obj, pack, packidx,
109 c59b3346 2018-09-11 stsp iobj->pack_idx, &iobj->id);
110 c59b3346 2018-09-11 stsp if (err)
111 c59b3346 2018-09-11 stsp goto done;
112 c59b3346 2018-09-11 stsp }
113 c59b3346 2018-09-11 stsp (*obj)->refcnt++;
114 c59b3346 2018-09-11 stsp done:
115 c59b3346 2018-09-11 stsp got_object_close(iobj);
116 c59b3346 2018-09-11 stsp return err;
117 c59b3346 2018-09-11 stsp }
118 c59b3346 2018-09-11 stsp
119 c59b3346 2018-09-11 stsp static const struct got_error *
120 876c234b 2018-09-10 stsp commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
121 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
122 876c234b 2018-09-10 stsp {
123 cfd633c2 2018-09-10 stsp const struct got_error *err = NULL;
124 cfd633c2 2018-09-10 stsp struct got_object *obj = NULL;
125 cfd633c2 2018-09-10 stsp struct got_commit_object *commit = NULL;
126 cfd633c2 2018-09-10 stsp uint8_t *buf;
127 cfd633c2 2018-09-10 stsp size_t len;
128 cfd633c2 2018-09-10 stsp
129 c59b3346 2018-09-11 stsp err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
130 c59b3346 2018-09-11 stsp GOT_OBJ_TYPE_COMMIT);
131 cfd633c2 2018-09-10 stsp if (err)
132 cfd633c2 2018-09-10 stsp return err;
133 cfd633c2 2018-09-10 stsp
134 cfd633c2 2018-09-10 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
135 cfd633c2 2018-09-10 stsp if (err)
136 cfd633c2 2018-09-10 stsp return err;
137 cfd633c2 2018-09-10 stsp
138 cfd633c2 2018-09-10 stsp obj->size = len;
139 cfd633c2 2018-09-10 stsp err = got_object_parse_commit(&commit, buf, len);
140 cfd633c2 2018-09-10 stsp free(buf);
141 cfd633c2 2018-09-10 stsp
142 cfd633c2 2018-09-10 stsp err = got_privsep_send_commit(ibuf, commit);
143 cfd633c2 2018-09-10 stsp if (obj)
144 cfd633c2 2018-09-10 stsp got_object_close(obj);
145 cfd633c2 2018-09-10 stsp got_object_commit_close(commit);
146 7762fe12 2018-11-05 stsp if (err) {
147 7762fe12 2018-11-05 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
148 7762fe12 2018-11-05 stsp err = NULL;
149 7762fe12 2018-11-05 stsp else
150 7762fe12 2018-11-05 stsp got_privsep_send_error(ibuf, err);
151 7762fe12 2018-11-05 stsp }
152 7762fe12 2018-11-05 stsp
153 7762fe12 2018-11-05 stsp return err;
154 7762fe12 2018-11-05 stsp }
155 7762fe12 2018-11-05 stsp
156 7762fe12 2018-11-05 stsp static const struct got_error *
157 876c234b 2018-09-10 stsp tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
158 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
159 876c234b 2018-09-10 stsp {
160 e7885405 2018-09-10 stsp const struct got_error *err = NULL;
161 e7885405 2018-09-10 stsp struct got_object *obj = NULL;
162 e7885405 2018-09-10 stsp struct got_tree_object *tree = NULL;
163 e7885405 2018-09-10 stsp uint8_t *buf;
164 e7885405 2018-09-10 stsp size_t len;
165 e7885405 2018-09-10 stsp
166 c59b3346 2018-09-11 stsp err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
167 c59b3346 2018-09-11 stsp GOT_OBJ_TYPE_TREE);
168 e7885405 2018-09-10 stsp if (err)
169 e7885405 2018-09-10 stsp return err;
170 e7885405 2018-09-10 stsp
171 e7885405 2018-09-10 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
172 e7885405 2018-09-10 stsp if (err)
173 e7885405 2018-09-10 stsp return err;
174 e7885405 2018-09-10 stsp
175 e7885405 2018-09-10 stsp obj->size = len;
176 e7885405 2018-09-10 stsp err = got_object_parse_tree(&tree, buf, len);
177 e7885405 2018-09-10 stsp free(buf);
178 e7885405 2018-09-10 stsp
179 e7885405 2018-09-10 stsp err = got_privsep_send_tree(ibuf, tree);
180 e7885405 2018-09-10 stsp if (obj)
181 e7885405 2018-09-10 stsp got_object_close(obj);
182 e7885405 2018-09-10 stsp got_object_tree_close(tree);
183 e7885405 2018-09-10 stsp if (err) {
184 e7885405 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
185 e7885405 2018-09-10 stsp err = NULL;
186 e7885405 2018-09-10 stsp else
187 e7885405 2018-09-10 stsp got_privsep_send_error(ibuf, err);
188 e7885405 2018-09-10 stsp }
189 e7885405 2018-09-10 stsp
190 e7885405 2018-09-10 stsp return err;
191 876c234b 2018-09-10 stsp }
192 876c234b 2018-09-10 stsp
193 876c234b 2018-09-10 stsp static const struct got_error *
194 3840f4c9 2018-09-12 stsp receive_file(FILE **f, struct imsgbuf *ibuf, int imsg_code)
195 876c234b 2018-09-10 stsp {
196 3840f4c9 2018-09-12 stsp const struct got_error *err;
197 3840f4c9 2018-09-12 stsp struct imsg imsg;
198 55da3778 2018-09-10 stsp size_t datalen;
199 55da3778 2018-09-10 stsp
200 3840f4c9 2018-09-12 stsp err = got_privsep_recv_imsg(&imsg, 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 3840f4c9 2018-09-12 stsp if (imsg.hdr.type != imsg_code) {
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 3840f4c9 2018-09-12 stsp datalen = imsg.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 3840f4c9 2018-09-12 stsp if (imsg.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 3840f4c9 2018-09-12 stsp *f = fdopen(imsg.fd, "w+");
220 3840f4c9 2018-09-12 stsp if (*f == NULL) {
221 3840f4c9 2018-09-12 stsp close(imsg.fd);
222 55da3778 2018-09-10 stsp err = got_error_from_errno();
223 55da3778 2018-09-10 stsp goto done;
224 55da3778 2018-09-10 stsp }
225 3840f4c9 2018-09-12 stsp done:
226 3840f4c9 2018-09-12 stsp imsg_free(&imsg);
227 3840f4c9 2018-09-12 stsp return err;
228 3840f4c9 2018-09-12 stsp }
229 55da3778 2018-09-10 stsp
230 3840f4c9 2018-09-12 stsp static const struct got_error *
231 3840f4c9 2018-09-12 stsp blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
232 3840f4c9 2018-09-12 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
233 3840f4c9 2018-09-12 stsp {
234 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
235 3840f4c9 2018-09-12 stsp struct got_object *obj = NULL;
236 3840f4c9 2018-09-12 stsp FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
237 3840f4c9 2018-09-12 stsp
238 3840f4c9 2018-09-12 stsp err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
239 3840f4c9 2018-09-12 stsp GOT_OBJ_TYPE_BLOB);
240 55da3778 2018-09-10 stsp if (err)
241 3840f4c9 2018-09-12 stsp return err;
242 3840f4c9 2018-09-12 stsp
243 3840f4c9 2018-09-12 stsp err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
244 3840f4c9 2018-09-12 stsp if (err)
245 3840f4c9 2018-09-12 stsp return err;
246 3840f4c9 2018-09-12 stsp err = receive_file(&basefile, ibuf, GOT_IMSG_TMPFD);
247 3840f4c9 2018-09-12 stsp if (err)
248 3840f4c9 2018-09-12 stsp return err;
249 3840f4c9 2018-09-12 stsp err = receive_file(&accumfile, ibuf, GOT_IMSG_TMPFD);
250 3840f4c9 2018-09-12 stsp if (err)
251 3840f4c9 2018-09-12 stsp return err;
252 3840f4c9 2018-09-12 stsp
253 3840f4c9 2018-09-12 stsp err = got_packfile_extract_object(pack, obj, outfile, basefile,
254 3840f4c9 2018-09-12 stsp accumfile);
255 3840f4c9 2018-09-12 stsp if (err)
256 55da3778 2018-09-10 stsp goto done;
257 55da3778 2018-09-10 stsp
258 55da3778 2018-09-10 stsp err = got_privsep_send_blob(ibuf, obj->size);
259 55da3778 2018-09-10 stsp done:
260 3840f4c9 2018-09-12 stsp if (outfile)
261 3840f4c9 2018-09-12 stsp fclose(outfile);
262 3840f4c9 2018-09-12 stsp if (basefile)
263 3840f4c9 2018-09-12 stsp fclose(basefile);
264 3840f4c9 2018-09-12 stsp if (accumfile)
265 3840f4c9 2018-09-12 stsp fclose(accumfile);
266 c59b3346 2018-09-11 stsp if (obj)
267 c59b3346 2018-09-11 stsp got_object_close(obj);
268 3840f4c9 2018-09-12 stsp if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
269 3840f4c9 2018-09-12 stsp got_privsep_send_error(ibuf, err);
270 55da3778 2018-09-10 stsp
271 55da3778 2018-09-10 stsp return err;
272 876c234b 2018-09-10 stsp }
273 876c234b 2018-09-10 stsp
274 876c234b 2018-09-10 stsp static const struct got_error *
275 876c234b 2018-09-10 stsp receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
276 876c234b 2018-09-10 stsp {
277 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
278 876c234b 2018-09-10 stsp struct imsg imsg;
279 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
280 876c234b 2018-09-10 stsp size_t datalen;
281 876c234b 2018-09-10 stsp struct got_packidx *p;
282 876c234b 2018-09-10 stsp
283 876c234b 2018-09-10 stsp *packidx = NULL;
284 876c234b 2018-09-10 stsp
285 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
286 876c234b 2018-09-10 stsp if (err)
287 876c234b 2018-09-10 stsp return err;
288 876c234b 2018-09-10 stsp
289 876c234b 2018-09-10 stsp p = calloc(1, sizeof(*p));
290 876c234b 2018-09-10 stsp if (p == NULL) {
291 876c234b 2018-09-10 stsp err = got_error_from_errno();
292 876c234b 2018-09-10 stsp goto done;
293 876c234b 2018-09-10 stsp }
294 876c234b 2018-09-10 stsp
295 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
296 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
297 876c234b 2018-09-10 stsp goto done;
298 876c234b 2018-09-10 stsp }
299 876c234b 2018-09-10 stsp
300 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
301 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
302 876c234b 2018-09-10 stsp goto done;
303 876c234b 2018-09-10 stsp }
304 876c234b 2018-09-10 stsp
305 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
306 876c234b 2018-09-10 stsp if (datalen != sizeof(ipackidx)) {
307 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
308 876c234b 2018-09-10 stsp goto done;
309 876c234b 2018-09-10 stsp }
310 876c234b 2018-09-10 stsp memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
311 876c234b 2018-09-10 stsp
312 876c234b 2018-09-10 stsp p->len = ipackidx.len;
313 876c234b 2018-09-10 stsp p->fd = dup(imsg.fd);
314 876c234b 2018-09-10 stsp if (p->fd == -1) {
315 56bef47a 2018-09-15 stsp err = got_error_from_errno();
316 56bef47a 2018-09-15 stsp goto done;
317 56bef47a 2018-09-15 stsp }
318 56bef47a 2018-09-15 stsp if (lseek(p->fd, 0, SEEK_SET) == -1) {
319 876c234b 2018-09-10 stsp err = got_error_from_errno();
320 876c234b 2018-09-10 stsp goto done;
321 876c234b 2018-09-10 stsp }
322 876c234b 2018-09-10 stsp
323 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
324 876c234b 2018-09-10 stsp p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
325 876c234b 2018-09-10 stsp if (p->map == MAP_FAILED)
326 876c234b 2018-09-10 stsp p->map = NULL; /* fall back to read(2) */
327 876c234b 2018-09-10 stsp #endif
328 876c234b 2018-09-10 stsp err = got_packidx_init_hdr(p, 1);
329 876c234b 2018-09-10 stsp done:
330 876c234b 2018-09-10 stsp if (err) {
331 876c234b 2018-09-10 stsp if (imsg.fd != -1)
332 876c234b 2018-09-10 stsp close(imsg.fd);
333 876c234b 2018-09-10 stsp got_packidx_close(p);
334 876c234b 2018-09-10 stsp } else
335 876c234b 2018-09-10 stsp *packidx = p;
336 876c234b 2018-09-10 stsp imsg_free(&imsg);
337 876c234b 2018-09-10 stsp return err;
338 876c234b 2018-09-10 stsp }
339 876c234b 2018-09-10 stsp
340 876c234b 2018-09-10 stsp static const struct got_error *
341 876c234b 2018-09-10 stsp receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
342 876c234b 2018-09-10 stsp {
343 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
344 876c234b 2018-09-10 stsp struct imsg imsg;
345 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
346 876c234b 2018-09-10 stsp size_t datalen;
347 876c234b 2018-09-10 stsp struct got_pack *pack;
348 876c234b 2018-09-10 stsp
349 876c234b 2018-09-10 stsp *packp = NULL;
350 876c234b 2018-09-10 stsp
351 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
352 876c234b 2018-09-10 stsp if (err)
353 876c234b 2018-09-10 stsp return err;
354 876c234b 2018-09-10 stsp
355 876c234b 2018-09-10 stsp pack = calloc(1, sizeof(*pack));
356 876c234b 2018-09-10 stsp if (pack == NULL) {
357 876c234b 2018-09-10 stsp err = got_error_from_errno();
358 876c234b 2018-09-10 stsp goto done;
359 876c234b 2018-09-10 stsp }
360 876c234b 2018-09-10 stsp
361 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACK) {
362 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
363 876c234b 2018-09-10 stsp goto done;
364 876c234b 2018-09-10 stsp }
365 876c234b 2018-09-10 stsp
366 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
367 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
368 876c234b 2018-09-10 stsp goto done;
369 876c234b 2018-09-10 stsp }
370 876c234b 2018-09-10 stsp
371 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
372 876c234b 2018-09-10 stsp if (datalen != sizeof(ipack)) {
373 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
374 876c234b 2018-09-10 stsp goto done;
375 876c234b 2018-09-10 stsp }
376 876c234b 2018-09-10 stsp memcpy(&ipack, imsg.data, sizeof(ipack));
377 876c234b 2018-09-10 stsp
378 876c234b 2018-09-10 stsp pack->filesize = ipack.filesize;
379 876c234b 2018-09-10 stsp pack->fd = dup(imsg.fd);
380 876c234b 2018-09-10 stsp if (pack->fd == -1) {
381 876c234b 2018-09-10 stsp err = got_error_from_errno();
382 876c234b 2018-09-10 stsp goto done;
383 876c234b 2018-09-10 stsp }
384 56bef47a 2018-09-15 stsp if (lseek(pack->fd, 0, SEEK_SET) == -1) {
385 56bef47a 2018-09-15 stsp err = got_error_from_errno();
386 56bef47a 2018-09-15 stsp goto done;
387 56bef47a 2018-09-15 stsp }
388 876c234b 2018-09-10 stsp pack->path_packfile = strdup(ipack.path_packfile);
389 876c234b 2018-09-10 stsp if (pack->path_packfile == NULL) {
390 876c234b 2018-09-10 stsp err = got_error_from_errno();
391 876c234b 2018-09-10 stsp goto done;
392 876c234b 2018-09-10 stsp }
393 876c234b 2018-09-10 stsp
394 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
395 876c234b 2018-09-10 stsp pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
396 876c234b 2018-09-10 stsp pack->fd, 0);
397 876c234b 2018-09-10 stsp if (pack->map == MAP_FAILED)
398 876c234b 2018-09-10 stsp pack->map = NULL; /* fall back to read(2) */
399 876c234b 2018-09-10 stsp #endif
400 876c234b 2018-09-10 stsp done:
401 876c234b 2018-09-10 stsp if (err) {
402 876c234b 2018-09-10 stsp if (imsg.fd != -1)
403 876c234b 2018-09-10 stsp close(imsg.fd);
404 876c234b 2018-09-10 stsp free(pack);
405 876c234b 2018-09-10 stsp } else
406 876c234b 2018-09-10 stsp *packp = pack;
407 876c234b 2018-09-10 stsp imsg_free(&imsg);
408 876c234b 2018-09-10 stsp return err;
409 876c234b 2018-09-10 stsp }
410 876c234b 2018-09-10 stsp
411 876c234b 2018-09-10 stsp int
412 876c234b 2018-09-10 stsp main(int argc, char *argv[])
413 876c234b 2018-09-10 stsp {
414 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
415 876c234b 2018-09-10 stsp struct imsgbuf ibuf;
416 876c234b 2018-09-10 stsp struct imsg imsg;
417 c59b3346 2018-09-11 stsp struct got_packidx *packidx = NULL;
418 c59b3346 2018-09-11 stsp struct got_pack *pack = NULL;
419 c59b3346 2018-09-11 stsp struct got_object_cache objcache;
420 876c234b 2018-09-10 stsp
421 876c234b 2018-09-10 stsp //static int attached;
422 876c234b 2018-09-10 stsp //while (!attached) sleep(1);
423 876c234b 2018-09-10 stsp
424 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
425 99437157 2018-11-11 stsp
426 876c234b 2018-09-10 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
427 876c234b 2018-09-10 stsp
428 c59b3346 2018-09-11 stsp err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
429 c59b3346 2018-09-11 stsp if (err) {
430 c59b3346 2018-09-11 stsp err = got_error_from_errno();
431 c59b3346 2018-09-11 stsp got_privsep_send_error(&ibuf, err);
432 c59b3346 2018-09-11 stsp return 1;
433 c59b3346 2018-09-11 stsp }
434 c59b3346 2018-09-11 stsp
435 2ff12563 2018-09-15 stsp #ifndef PROFILE
436 876c234b 2018-09-10 stsp /* revoke access to most system calls */
437 876c234b 2018-09-10 stsp if (pledge("stdio recvfd", NULL) == -1) {
438 876c234b 2018-09-10 stsp err = got_error_from_errno();
439 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
440 876c234b 2018-09-10 stsp return 1;
441 876c234b 2018-09-10 stsp }
442 2ff12563 2018-09-15 stsp #endif
443 876c234b 2018-09-10 stsp
444 876c234b 2018-09-10 stsp err = receive_packidx(&packidx, &ibuf);
445 876c234b 2018-09-10 stsp if (err) {
446 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
447 876c234b 2018-09-10 stsp return 1;
448 876c234b 2018-09-10 stsp }
449 876c234b 2018-09-10 stsp
450 876c234b 2018-09-10 stsp err = receive_pack(&pack, &ibuf);
451 876c234b 2018-09-10 stsp if (err) {
452 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
453 876c234b 2018-09-10 stsp return 1;
454 876c234b 2018-09-10 stsp }
455 876c234b 2018-09-10 stsp
456 876c234b 2018-09-10 stsp while (1) {
457 876c234b 2018-09-10 stsp imsg.fd = -1;
458 99437157 2018-11-11 stsp
459 99437157 2018-11-11 stsp if (sigint_received) {
460 99437157 2018-11-11 stsp err = got_error(GOT_ERR_CANCELLED);
461 99437157 2018-11-11 stsp break;
462 99437157 2018-11-11 stsp }
463 876c234b 2018-09-10 stsp
464 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
465 876c234b 2018-09-10 stsp if (err) {
466 876c234b 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
467 876c234b 2018-09-10 stsp err = NULL;
468 876c234b 2018-09-10 stsp break;
469 876c234b 2018-09-10 stsp }
470 876c234b 2018-09-10 stsp
471 876c234b 2018-09-10 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
472 876c234b 2018-09-10 stsp break;
473 876c234b 2018-09-10 stsp
474 876c234b 2018-09-10 stsp switch (imsg.hdr.type) {
475 876c234b 2018-09-10 stsp case GOT_IMSG_PACKED_OBJECT_REQUEST:
476 c59b3346 2018-09-11 stsp err = object_request(&imsg, &ibuf, pack, packidx,
477 c59b3346 2018-09-11 stsp &objcache);
478 876c234b 2018-09-10 stsp break;
479 876c234b 2018-09-10 stsp case GOT_IMSG_COMMIT_REQUEST:
480 c59b3346 2018-09-11 stsp err = commit_request(&imsg, &ibuf, pack, packidx,
481 7762fe12 2018-11-05 stsp &objcache);
482 7762fe12 2018-11-05 stsp break;
483 876c234b 2018-09-10 stsp case GOT_IMSG_TREE_REQUEST:
484 c59b3346 2018-09-11 stsp err = tree_request(&imsg, &ibuf, pack, packidx,
485 c59b3346 2018-09-11 stsp &objcache);
486 876c234b 2018-09-10 stsp break;
487 876c234b 2018-09-10 stsp case GOT_IMSG_BLOB_REQUEST:
488 c59b3346 2018-09-11 stsp err = blob_request(&imsg, &ibuf, pack, packidx,
489 c59b3346 2018-09-11 stsp &objcache);
490 876c234b 2018-09-10 stsp break;
491 876c234b 2018-09-10 stsp default:
492 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
493 876c234b 2018-09-10 stsp break;
494 876c234b 2018-09-10 stsp }
495 876c234b 2018-09-10 stsp
496 876c234b 2018-09-10 stsp if (imsg.fd != -1)
497 876c234b 2018-09-10 stsp close(imsg.fd);
498 876c234b 2018-09-10 stsp imsg_free(&imsg);
499 99437157 2018-11-11 stsp if (err)
500 876c234b 2018-09-10 stsp break;
501 876c234b 2018-09-10 stsp }
502 876c234b 2018-09-10 stsp
503 c59b3346 2018-09-11 stsp if (packidx)
504 c59b3346 2018-09-11 stsp got_packidx_close(packidx);
505 c59b3346 2018-09-11 stsp if (pack)
506 c59b3346 2018-09-11 stsp got_pack_close(pack);
507 48d5fe42 2018-09-15 stsp got_object_cache_close(&objcache);
508 876c234b 2018-09-10 stsp imsg_clear(&ibuf);
509 99437157 2018-11-11 stsp if (err) {
510 80d5f134 2018-11-11 stsp if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
511 80d5f134 2018-11-11 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
512 99437157 2018-11-11 stsp got_privsep_send_error(&ibuf, err);
513 80d5f134 2018-11-11 stsp }
514 99437157 2018-11-11 stsp }
515 876c234b 2018-09-10 stsp close(GOT_IMSG_FD_CHILD);
516 876c234b 2018-09-10 stsp return err ? 1 : 0;
517 876c234b 2018-09-10 stsp }