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_parse.h"
41 #include "got_lib_privsep.h"
42 #include "got_lib_pack.h"
44 static const struct got_error *
45 object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
46 struct got_packidx *packidx)
48 const struct got_error *err = NULL;
49 struct got_imsg_packed_object iobj;
50 struct got_object *obj;
53 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
54 if (datalen != sizeof(iobj))
55 return got_error(GOT_ERR_PRIVSEP_LEN);
56 memcpy(&iobj, imsg->data, sizeof(iobj));
58 err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, NULL);
62 err = got_privsep_send_obj(ibuf, obj);
63 got_object_close(obj);
67 static const struct got_error *
68 commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
69 struct got_packidx *packidx)
71 const struct got_error *err = NULL;
72 struct got_object *obj = NULL;
73 struct got_commit_object *commit = NULL;
77 err = got_privsep_get_imsg_obj(&obj, imsg, ibuf);
81 if (obj->type != GOT_OBJ_TYPE_COMMIT)
82 return got_error(GOT_ERR_OBJ_TYPE);
84 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
89 err = got_object_parse_commit(&commit, buf, len);
92 err = got_privsep_send_commit(ibuf, commit);
94 got_object_close(obj);
95 got_object_commit_close(commit);
97 if (err->code == GOT_ERR_PRIVSEP_PIPE)
100 got_privsep_send_error(ibuf, err);
106 static const struct got_error *
107 tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
108 struct got_packidx *packidx)
110 return got_error(GOT_ERR_NOT_IMPL);
113 static const struct got_error *
114 blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
115 struct got_packidx *packidx)
117 return got_error(GOT_ERR_NOT_IMPL);
120 static const struct got_error *
121 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
123 const struct got_error *err = NULL;
125 struct got_imsg_packidx ipackidx;
127 struct got_packidx *p;
131 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
135 p = calloc(1, sizeof(*p));
137 err = got_error_from_errno();
141 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
142 err = got_error(GOT_ERR_PRIVSEP_MSG);
147 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
151 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
152 if (datalen != sizeof(ipackidx)) {
153 err = got_error(GOT_ERR_PRIVSEP_LEN);
156 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
158 p->len = ipackidx.len;
159 p->fd = dup(imsg.fd);
161 err = got_error_from_errno();
165 #ifndef GOT_PACK_NO_MMAP
166 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
167 if (p->map == MAP_FAILED)
168 p->map = NULL; /* fall back to read(2) */
170 err = got_packidx_init_hdr(p, 1);
175 got_packidx_close(p);
182 static const struct got_error *
183 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
185 const struct got_error *err = NULL;
187 struct got_imsg_pack ipack;
189 struct got_pack *pack;
193 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
197 pack = calloc(1, sizeof(*pack));
199 err = got_error_from_errno();
203 if (imsg.hdr.type != GOT_IMSG_PACK) {
204 err = got_error(GOT_ERR_PRIVSEP_MSG);
209 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
213 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
214 if (datalen != sizeof(ipack)) {
215 err = got_error(GOT_ERR_PRIVSEP_LEN);
218 memcpy(&ipack, imsg.data, sizeof(ipack));
220 pack->filesize = ipack.filesize;
221 pack->fd = dup(imsg.fd);
222 if (pack->fd == -1) {
223 err = got_error_from_errno();
226 pack->path_packfile = strdup(ipack.path_packfile);
227 if (pack->path_packfile == NULL) {
228 err = got_error_from_errno();
232 #ifndef GOT_PACK_NO_MMAP
233 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
235 if (pack->map == MAP_FAILED)
236 pack->map = NULL; /* fall back to read(2) */
250 main(int argc, char *argv[])
252 const struct got_error *err = NULL;
255 struct got_packidx *packidx;
256 struct got_pack *pack;
258 //static int attached;
259 //while (!attached) sleep(1);
261 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
263 /* revoke access to most system calls */
264 if (pledge("stdio recvfd", NULL) == -1) {
265 err = got_error_from_errno();
266 got_privsep_send_error(&ibuf, err);
270 err = receive_packidx(&packidx, &ibuf);
272 got_privsep_send_error(&ibuf, err);
276 err = receive_pack(&pack, &ibuf);
278 got_privsep_send_error(&ibuf, err);
285 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
287 if (err->code == GOT_ERR_PRIVSEP_PIPE)
292 if (imsg.hdr.type == GOT_IMSG_STOP)
295 switch (imsg.hdr.type) {
296 case GOT_IMSG_PACKED_OBJECT_REQUEST:
297 err = object_request(&imsg, &ibuf, pack, packidx);
299 case GOT_IMSG_COMMIT_REQUEST:
300 err = commit_request(&imsg, &ibuf, pack, packidx);
302 case GOT_IMSG_TREE_REQUEST:
303 err = tree_request(&imsg, &ibuf, pack, packidx);
305 case GOT_IMSG_BLOB_REQUEST:
306 err = blob_request(&imsg, &ibuf, pack, packidx);
309 err = got_error(GOT_ERR_PRIVSEP_MSG);
317 if (err->code == GOT_ERR_PRIVSEP_PIPE)
320 got_privsep_send_error(&ibuf, err);
325 got_pack_close(pack);
328 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
329 close(GOT_IMSG_FD_CHILD);