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 f4a881ce 2018-11-17 stsp tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
276 f4a881ce 2018-11-17 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
277 f4a881ce 2018-11-17 stsp {
278 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
279 f4a881ce 2018-11-17 stsp struct got_object *obj = NULL;
280 f4a881ce 2018-11-17 stsp struct got_tag_object *tag = NULL;
281 f4a881ce 2018-11-17 stsp uint8_t *buf;
282 f4a881ce 2018-11-17 stsp size_t len;
283 f4a881ce 2018-11-17 stsp
284 f4a881ce 2018-11-17 stsp err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
285 f4a881ce 2018-11-17 stsp GOT_OBJ_TYPE_TAG);
286 f4a881ce 2018-11-17 stsp if (err)
287 f4a881ce 2018-11-17 stsp return err;
288 f4a881ce 2018-11-17 stsp
289 f4a881ce 2018-11-17 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
290 f4a881ce 2018-11-17 stsp if (err)
291 f4a881ce 2018-11-17 stsp return err;
292 f4a881ce 2018-11-17 stsp
293 f4a881ce 2018-11-17 stsp obj->size = len;
294 f4a881ce 2018-11-17 stsp err = got_object_parse_tag(&tag, buf, len);
295 f4a881ce 2018-11-17 stsp free(buf);
296 f4a881ce 2018-11-17 stsp
297 f4a881ce 2018-11-17 stsp err = got_privsep_send_tag(ibuf, tag);
298 f4a881ce 2018-11-17 stsp if (obj)
299 f4a881ce 2018-11-17 stsp got_object_close(obj);
300 f4a881ce 2018-11-17 stsp got_object_tag_close(tag);
301 f4a881ce 2018-11-17 stsp if (err) {
302 f4a881ce 2018-11-17 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
303 f4a881ce 2018-11-17 stsp err = NULL;
304 f4a881ce 2018-11-17 stsp else
305 f4a881ce 2018-11-17 stsp got_privsep_send_error(ibuf, err);
306 f4a881ce 2018-11-17 stsp }
307 f4a881ce 2018-11-17 stsp
308 f4a881ce 2018-11-17 stsp return err;
309 f4a881ce 2018-11-17 stsp }
310 f4a881ce 2018-11-17 stsp
311 f4a881ce 2018-11-17 stsp static const struct got_error *
312 876c234b 2018-09-10 stsp receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
313 876c234b 2018-09-10 stsp {
314 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
315 876c234b 2018-09-10 stsp struct imsg imsg;
316 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
317 876c234b 2018-09-10 stsp size_t datalen;
318 876c234b 2018-09-10 stsp struct got_packidx *p;
319 876c234b 2018-09-10 stsp
320 876c234b 2018-09-10 stsp *packidx = NULL;
321 876c234b 2018-09-10 stsp
322 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
323 876c234b 2018-09-10 stsp if (err)
324 876c234b 2018-09-10 stsp return err;
325 876c234b 2018-09-10 stsp
326 876c234b 2018-09-10 stsp p = calloc(1, sizeof(*p));
327 876c234b 2018-09-10 stsp if (p == NULL) {
328 876c234b 2018-09-10 stsp err = got_error_from_errno();
329 876c234b 2018-09-10 stsp goto done;
330 876c234b 2018-09-10 stsp }
331 876c234b 2018-09-10 stsp
332 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
333 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
334 876c234b 2018-09-10 stsp goto done;
335 876c234b 2018-09-10 stsp }
336 876c234b 2018-09-10 stsp
337 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
338 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
339 876c234b 2018-09-10 stsp goto done;
340 876c234b 2018-09-10 stsp }
341 876c234b 2018-09-10 stsp
342 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
343 876c234b 2018-09-10 stsp if (datalen != sizeof(ipackidx)) {
344 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
345 876c234b 2018-09-10 stsp goto done;
346 876c234b 2018-09-10 stsp }
347 876c234b 2018-09-10 stsp memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
348 876c234b 2018-09-10 stsp
349 876c234b 2018-09-10 stsp p->len = ipackidx.len;
350 876c234b 2018-09-10 stsp p->fd = dup(imsg.fd);
351 876c234b 2018-09-10 stsp if (p->fd == -1) {
352 56bef47a 2018-09-15 stsp err = got_error_from_errno();
353 56bef47a 2018-09-15 stsp goto done;
354 56bef47a 2018-09-15 stsp }
355 56bef47a 2018-09-15 stsp if (lseek(p->fd, 0, SEEK_SET) == -1) {
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 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
362 876c234b 2018-09-10 stsp if (p->map == MAP_FAILED)
363 876c234b 2018-09-10 stsp p->map = NULL; /* fall back to read(2) */
364 876c234b 2018-09-10 stsp #endif
365 876c234b 2018-09-10 stsp err = got_packidx_init_hdr(p, 1);
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 got_packidx_close(p);
371 876c234b 2018-09-10 stsp } else
372 876c234b 2018-09-10 stsp *packidx = p;
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 static const struct got_error *
378 876c234b 2018-09-10 stsp receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
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 imsg imsg;
382 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
383 876c234b 2018-09-10 stsp size_t datalen;
384 876c234b 2018-09-10 stsp struct got_pack *pack;
385 876c234b 2018-09-10 stsp
386 876c234b 2018-09-10 stsp *packp = NULL;
387 876c234b 2018-09-10 stsp
388 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
389 876c234b 2018-09-10 stsp if (err)
390 876c234b 2018-09-10 stsp return err;
391 876c234b 2018-09-10 stsp
392 876c234b 2018-09-10 stsp pack = calloc(1, sizeof(*pack));
393 876c234b 2018-09-10 stsp if (pack == NULL) {
394 876c234b 2018-09-10 stsp err = got_error_from_errno();
395 876c234b 2018-09-10 stsp goto done;
396 876c234b 2018-09-10 stsp }
397 876c234b 2018-09-10 stsp
398 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACK) {
399 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
400 876c234b 2018-09-10 stsp goto done;
401 876c234b 2018-09-10 stsp }
402 876c234b 2018-09-10 stsp
403 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
404 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
405 876c234b 2018-09-10 stsp goto done;
406 876c234b 2018-09-10 stsp }
407 876c234b 2018-09-10 stsp
408 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
409 876c234b 2018-09-10 stsp if (datalen != sizeof(ipack)) {
410 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
411 876c234b 2018-09-10 stsp goto done;
412 876c234b 2018-09-10 stsp }
413 876c234b 2018-09-10 stsp memcpy(&ipack, imsg.data, sizeof(ipack));
414 876c234b 2018-09-10 stsp
415 876c234b 2018-09-10 stsp pack->filesize = ipack.filesize;
416 876c234b 2018-09-10 stsp pack->fd = dup(imsg.fd);
417 876c234b 2018-09-10 stsp if (pack->fd == -1) {
418 876c234b 2018-09-10 stsp err = got_error_from_errno();
419 876c234b 2018-09-10 stsp goto done;
420 876c234b 2018-09-10 stsp }
421 56bef47a 2018-09-15 stsp if (lseek(pack->fd, 0, SEEK_SET) == -1) {
422 56bef47a 2018-09-15 stsp err = got_error_from_errno();
423 56bef47a 2018-09-15 stsp goto done;
424 56bef47a 2018-09-15 stsp }
425 876c234b 2018-09-10 stsp pack->path_packfile = strdup(ipack.path_packfile);
426 876c234b 2018-09-10 stsp if (pack->path_packfile == NULL) {
427 876c234b 2018-09-10 stsp err = got_error_from_errno();
428 876c234b 2018-09-10 stsp goto done;
429 876c234b 2018-09-10 stsp }
430 876c234b 2018-09-10 stsp
431 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
432 876c234b 2018-09-10 stsp pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
433 876c234b 2018-09-10 stsp pack->fd, 0);
434 876c234b 2018-09-10 stsp if (pack->map == MAP_FAILED)
435 876c234b 2018-09-10 stsp pack->map = NULL; /* fall back to read(2) */
436 876c234b 2018-09-10 stsp #endif
437 876c234b 2018-09-10 stsp done:
438 876c234b 2018-09-10 stsp if (err) {
439 876c234b 2018-09-10 stsp if (imsg.fd != -1)
440 876c234b 2018-09-10 stsp close(imsg.fd);
441 876c234b 2018-09-10 stsp free(pack);
442 876c234b 2018-09-10 stsp } else
443 876c234b 2018-09-10 stsp *packp = pack;
444 876c234b 2018-09-10 stsp imsg_free(&imsg);
445 876c234b 2018-09-10 stsp return err;
446 876c234b 2018-09-10 stsp }
447 876c234b 2018-09-10 stsp
448 876c234b 2018-09-10 stsp int
449 876c234b 2018-09-10 stsp main(int argc, char *argv[])
450 876c234b 2018-09-10 stsp {
451 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
452 876c234b 2018-09-10 stsp struct imsgbuf ibuf;
453 876c234b 2018-09-10 stsp struct imsg imsg;
454 c59b3346 2018-09-11 stsp struct got_packidx *packidx = NULL;
455 c59b3346 2018-09-11 stsp struct got_pack *pack = NULL;
456 c59b3346 2018-09-11 stsp struct got_object_cache objcache;
457 876c234b 2018-09-10 stsp
458 876c234b 2018-09-10 stsp //static int attached;
459 876c234b 2018-09-10 stsp //while (!attached) sleep(1);
460 876c234b 2018-09-10 stsp
461 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
462 99437157 2018-11-11 stsp
463 876c234b 2018-09-10 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
464 876c234b 2018-09-10 stsp
465 c59b3346 2018-09-11 stsp err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
466 c59b3346 2018-09-11 stsp if (err) {
467 c59b3346 2018-09-11 stsp err = got_error_from_errno();
468 c59b3346 2018-09-11 stsp got_privsep_send_error(&ibuf, err);
469 c59b3346 2018-09-11 stsp return 1;
470 c59b3346 2018-09-11 stsp }
471 c59b3346 2018-09-11 stsp
472 2ff12563 2018-09-15 stsp #ifndef PROFILE
473 876c234b 2018-09-10 stsp /* revoke access to most system calls */
474 876c234b 2018-09-10 stsp if (pledge("stdio recvfd", NULL) == -1) {
475 876c234b 2018-09-10 stsp err = got_error_from_errno();
476 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
477 876c234b 2018-09-10 stsp return 1;
478 876c234b 2018-09-10 stsp }
479 2ff12563 2018-09-15 stsp #endif
480 876c234b 2018-09-10 stsp
481 876c234b 2018-09-10 stsp err = receive_packidx(&packidx, &ibuf);
482 876c234b 2018-09-10 stsp if (err) {
483 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
484 876c234b 2018-09-10 stsp return 1;
485 876c234b 2018-09-10 stsp }
486 876c234b 2018-09-10 stsp
487 876c234b 2018-09-10 stsp err = receive_pack(&pack, &ibuf);
488 876c234b 2018-09-10 stsp if (err) {
489 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
490 876c234b 2018-09-10 stsp return 1;
491 876c234b 2018-09-10 stsp }
492 876c234b 2018-09-10 stsp
493 876c234b 2018-09-10 stsp while (1) {
494 876c234b 2018-09-10 stsp imsg.fd = -1;
495 99437157 2018-11-11 stsp
496 99437157 2018-11-11 stsp if (sigint_received) {
497 99437157 2018-11-11 stsp err = got_error(GOT_ERR_CANCELLED);
498 99437157 2018-11-11 stsp break;
499 99437157 2018-11-11 stsp }
500 876c234b 2018-09-10 stsp
501 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
502 876c234b 2018-09-10 stsp if (err) {
503 876c234b 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
504 876c234b 2018-09-10 stsp err = NULL;
505 876c234b 2018-09-10 stsp break;
506 876c234b 2018-09-10 stsp }
507 876c234b 2018-09-10 stsp
508 876c234b 2018-09-10 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
509 876c234b 2018-09-10 stsp break;
510 876c234b 2018-09-10 stsp
511 876c234b 2018-09-10 stsp switch (imsg.hdr.type) {
512 876c234b 2018-09-10 stsp case GOT_IMSG_PACKED_OBJECT_REQUEST:
513 c59b3346 2018-09-11 stsp err = object_request(&imsg, &ibuf, pack, packidx,
514 c59b3346 2018-09-11 stsp &objcache);
515 876c234b 2018-09-10 stsp break;
516 876c234b 2018-09-10 stsp case GOT_IMSG_COMMIT_REQUEST:
517 c59b3346 2018-09-11 stsp err = commit_request(&imsg, &ibuf, pack, packidx,
518 7762fe12 2018-11-05 stsp &objcache);
519 7762fe12 2018-11-05 stsp break;
520 876c234b 2018-09-10 stsp case GOT_IMSG_TREE_REQUEST:
521 c59b3346 2018-09-11 stsp err = tree_request(&imsg, &ibuf, pack, packidx,
522 c59b3346 2018-09-11 stsp &objcache);
523 876c234b 2018-09-10 stsp break;
524 876c234b 2018-09-10 stsp case GOT_IMSG_BLOB_REQUEST:
525 c59b3346 2018-09-11 stsp err = blob_request(&imsg, &ibuf, pack, packidx,
526 c59b3346 2018-09-11 stsp &objcache);
527 876c234b 2018-09-10 stsp break;
528 f4a881ce 2018-11-17 stsp case GOT_IMSG_TAG_REQUEST:
529 f4a881ce 2018-11-17 stsp err = tag_request(&imsg, &ibuf, pack, packidx,
530 f4a881ce 2018-11-17 stsp &objcache);
531 f4a881ce 2018-11-17 stsp break;
532 876c234b 2018-09-10 stsp default:
533 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
534 876c234b 2018-09-10 stsp break;
535 876c234b 2018-09-10 stsp }
536 876c234b 2018-09-10 stsp
537 876c234b 2018-09-10 stsp if (imsg.fd != -1)
538 876c234b 2018-09-10 stsp close(imsg.fd);
539 876c234b 2018-09-10 stsp imsg_free(&imsg);
540 99437157 2018-11-11 stsp if (err)
541 876c234b 2018-09-10 stsp break;
542 876c234b 2018-09-10 stsp }
543 876c234b 2018-09-10 stsp
544 c59b3346 2018-09-11 stsp if (packidx)
545 c59b3346 2018-09-11 stsp got_packidx_close(packidx);
546 c59b3346 2018-09-11 stsp if (pack)
547 c59b3346 2018-09-11 stsp got_pack_close(pack);
548 48d5fe42 2018-09-15 stsp got_object_cache_close(&objcache);
549 876c234b 2018-09-10 stsp imsg_clear(&ibuf);
550 99437157 2018-11-11 stsp if (err) {
551 80d5f134 2018-11-11 stsp if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
552 80d5f134 2018-11-11 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
553 99437157 2018-11-11 stsp got_privsep_send_error(&ibuf, err);
554 80d5f134 2018-11-11 stsp }
555 99437157 2018-11-11 stsp }
556 876c234b 2018-09-10 stsp close(GOT_IMSG_FD_CHILD);
557 876c234b 2018-09-10 stsp return err ? 1 : 0;
558 876c234b 2018-09-10 stsp }