2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
18 #include <sys/queue.h>
21 #include <sys/limits.h>
22 #include <sys/syslimits.h>
34 #include "got_error.h"
35 #include "got_object.h"
37 #include "got_lib_delta.h"
38 #include "got_lib_inflate.h"
39 #include "got_lib_object.h"
40 #include "got_lib_object_cache.h"
41 #include "got_lib_object_parse.h"
42 #include "got_lib_privsep.h"
43 #include "got_lib_pack.h"
45 static const struct got_error *
46 object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
47 struct got_packidx *packidx, struct got_object_cache *objcache)
49 const struct got_error *err = NULL;
50 struct got_imsg_packed_object iobj;
51 struct got_object *obj;
54 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
55 if (datalen != sizeof(iobj))
56 return got_error(GOT_ERR_PRIVSEP_LEN);
57 memcpy(&iobj, imsg->data, sizeof(iobj));
59 err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, NULL);
64 err = got_object_cache_add(objcache, &obj->id, obj);
69 err = got_privsep_send_obj(ibuf, obj);
71 got_object_close(obj);
75 static const struct got_error *
76 get_object(struct got_object **obj, struct imsg *imsg, struct imsgbuf *ibuf,
77 struct got_pack *pack, struct got_packidx *packidx,
78 struct got_object_cache *objcache, int type)
80 const struct got_error *err = NULL;
81 struct got_object *iobj;
83 err = got_privsep_get_imsg_obj(&iobj, imsg, ibuf);
87 if (iobj->type != type) {
88 err = got_error(GOT_ERR_OBJ_TYPE);
92 if ((iobj->flags & GOT_OBJ_FLAG_PACKED) == 0)
93 return got_error(GOT_ERR_OBJ_NOT_PACKED);
95 *obj = got_object_cache_get(objcache, &iobj->id);
97 err = got_packfile_open_object(obj, pack, packidx,
98 iobj->pack_idx, &iobj->id);
104 got_object_close(iobj);
108 static const struct got_error *
109 commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
110 struct got_packidx *packidx, struct got_object_cache *objcache)
112 const struct got_error *err = NULL;
113 struct got_object *obj = NULL;
114 struct got_commit_object *commit = NULL;
118 err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
119 GOT_OBJ_TYPE_COMMIT);
123 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
128 err = got_object_parse_commit(&commit, buf, len);
131 err = got_privsep_send_commit(ibuf, commit);
133 got_object_close(obj);
134 got_object_commit_close(commit);
136 if (err->code == GOT_ERR_PRIVSEP_PIPE)
139 got_privsep_send_error(ibuf, err);
145 static const struct got_error *
146 tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
147 struct got_packidx *packidx, struct got_object_cache *objcache)
149 const struct got_error *err = NULL;
150 struct got_object *obj = NULL;
151 struct got_tree_object *tree = NULL;
155 err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
160 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
165 err = got_object_parse_tree(&tree, buf, len);
168 err = got_privsep_send_tree(ibuf, tree);
170 got_object_close(obj);
171 got_object_tree_close(tree);
173 if (err->code == GOT_ERR_PRIVSEP_PIPE)
176 got_privsep_send_error(ibuf, err);
182 static const struct got_error *
183 blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
184 struct got_packidx *packidx, struct got_object_cache *objcache)
186 const struct got_error *err = NULL;
187 struct got_object *obj = NULL;
189 struct imsg imsg_outfd;
192 memset(&imsg_outfd, 0, sizeof(imsg_outfd));
195 err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
200 err = got_privsep_recv_imsg(&imsg_outfd, ibuf, 0);
204 if (imsg_outfd.hdr.type != GOT_IMSG_BLOB_OUTFD) {
205 err = got_error(GOT_ERR_PRIVSEP_MSG);
209 datalen = imsg_outfd.hdr.len - IMSG_HEADER_SIZE;
211 err = got_error(GOT_ERR_PRIVSEP_LEN);
214 if (imsg_outfd.fd == -1) {
215 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
219 f = fdopen(imsg_outfd.fd, "w+");
221 err = got_error_from_errno();
225 err = got_packfile_extract_object(pack, obj, f);
229 err = got_privsep_send_blob(ibuf, obj->size);
233 else if (imsg_outfd.fd != -1)
234 close(imsg_outfd.fd);
235 imsg_free(&imsg_outfd);
237 got_object_close(obj);
239 if (err->code == GOT_ERR_PRIVSEP_PIPE)
242 got_privsep_send_error(ibuf, err);
248 static const struct got_error *
249 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
251 const struct got_error *err = NULL;
253 struct got_imsg_packidx ipackidx;
255 struct got_packidx *p;
259 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
263 p = calloc(1, sizeof(*p));
265 err = got_error_from_errno();
269 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
270 err = got_error(GOT_ERR_PRIVSEP_MSG);
275 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
279 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
280 if (datalen != sizeof(ipackidx)) {
281 err = got_error(GOT_ERR_PRIVSEP_LEN);
284 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
286 p->len = ipackidx.len;
287 p->fd = dup(imsg.fd);
289 err = got_error_from_errno();
293 #ifndef GOT_PACK_NO_MMAP
294 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
295 if (p->map == MAP_FAILED)
296 p->map = NULL; /* fall back to read(2) */
298 err = got_packidx_init_hdr(p, 1);
303 got_packidx_close(p);
310 static const struct got_error *
311 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
313 const struct got_error *err = NULL;
315 struct got_imsg_pack ipack;
317 struct got_pack *pack;
321 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
325 pack = calloc(1, sizeof(*pack));
327 err = got_error_from_errno();
331 if (imsg.hdr.type != GOT_IMSG_PACK) {
332 err = got_error(GOT_ERR_PRIVSEP_MSG);
337 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
341 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
342 if (datalen != sizeof(ipack)) {
343 err = got_error(GOT_ERR_PRIVSEP_LEN);
346 memcpy(&ipack, imsg.data, sizeof(ipack));
348 pack->filesize = ipack.filesize;
349 pack->fd = dup(imsg.fd);
350 if (pack->fd == -1) {
351 err = got_error_from_errno();
354 pack->path_packfile = strdup(ipack.path_packfile);
355 if (pack->path_packfile == NULL) {
356 err = got_error_from_errno();
360 #ifndef GOT_PACK_NO_MMAP
361 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
363 if (pack->map == MAP_FAILED)
364 pack->map = NULL; /* fall back to read(2) */
378 main(int argc, char *argv[])
380 const struct got_error *err = NULL;
383 struct got_packidx *packidx = NULL;
384 struct got_pack *pack = NULL;
385 struct got_object_cache objcache;
387 //static int attached;
388 //while (!attached) sleep(1);
390 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
392 err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
394 err = got_error_from_errno();
395 got_privsep_send_error(&ibuf, err);
399 /* revoke access to most system calls */
400 if (pledge("stdio recvfd", NULL) == -1) {
401 err = got_error_from_errno();
402 got_privsep_send_error(&ibuf, err);
406 err = receive_packidx(&packidx, &ibuf);
408 got_privsep_send_error(&ibuf, err);
412 err = receive_pack(&pack, &ibuf);
414 got_privsep_send_error(&ibuf, err);
421 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
423 if (err->code == GOT_ERR_PRIVSEP_PIPE)
428 if (imsg.hdr.type == GOT_IMSG_STOP)
431 switch (imsg.hdr.type) {
432 case GOT_IMSG_PACKED_OBJECT_REQUEST:
433 err = object_request(&imsg, &ibuf, pack, packidx,
436 case GOT_IMSG_COMMIT_REQUEST:
437 err = commit_request(&imsg, &ibuf, pack, packidx,
440 case GOT_IMSG_TREE_REQUEST:
441 err = tree_request(&imsg, &ibuf, pack, packidx,
444 case GOT_IMSG_BLOB_REQUEST:
445 err = blob_request(&imsg, &ibuf, pack, packidx,
449 err = got_error(GOT_ERR_PRIVSEP_MSG);
457 if (err->code == GOT_ERR_PRIVSEP_PIPE)
460 got_privsep_send_error(&ibuf, err);
466 got_packidx_close(packidx);
468 got_pack_close(pack);
471 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
472 close(GOT_IMSG_FD_CHILD);