Blob


1 /*
2 * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
3 * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/mman.h>
21 #include <sys/uio.h>
23 #include <stdint.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <imsg.h>
28 #include <limits.h>
29 #include <time.h>
30 #include <unistd.h>
32 #include "got_error.h"
33 #include "got_object.h"
35 #include "got_lib_delta.h"
36 #include "got_lib_delta_cache.h"
37 #include "got_lib_hash.h"
38 #include "got_lib_object.h"
39 #include "got_lib_privsep.h"
40 #include "got_lib_ratelimit.h"
41 #include "got_lib_pack.h"
42 #include "got_lib_pack_index.h"
44 #ifndef nitems
45 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
46 #endif
48 static const struct got_error *
49 send_index_pack_progress(void *arg, uint32_t nobj_total, uint32_t nobj_indexed,
50 uint32_t nobj_loose, uint32_t nobj_resolved)
51 {
52 struct imsgbuf *ibuf = arg;
53 struct got_imsg_index_pack_progress iprogress;
55 iprogress.nobj_total = nobj_total;
56 iprogress.nobj_indexed = nobj_indexed;
57 iprogress.nobj_loose = nobj_loose;
58 iprogress.nobj_resolved = nobj_resolved;
60 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
61 &iprogress, sizeof(iprogress)) == -1)
62 return got_error_from_errno("imsg_compose IDXPACK_PROGRESS");
64 return got_privsep_flush_imsg(ibuf);
65 }
67 static const struct got_error *
68 send_index_pack_done(struct imsgbuf *ibuf)
69 {
70 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
71 return got_error_from_errno("imsg_compose FETCH");
72 return got_privsep_flush_imsg(ibuf);
73 }
76 int
77 main(int argc, char **argv)
78 {
79 const struct got_error *err = NULL, *close_err;
80 struct imsgbuf ibuf;
81 struct imsg imsg;
82 size_t i;
83 int idxfd = -1, tmpfd = -1;
84 FILE *tmpfiles[3];
85 struct got_pack pack;
86 uint8_t pack_hash[SHA1_DIGEST_LENGTH];
87 off_t packfile_size;
88 struct got_ratelimit rl;
89 #if 0
90 static int attached;
91 while (!attached)
92 sleep(1);
93 #endif
95 got_ratelimit_init(&rl, 0, 500);
97 for (i = 0; i < nitems(tmpfiles); i++)
98 tmpfiles[i] = NULL;
100 memset(&pack, 0, sizeof(pack));
101 pack.fd = -1;
102 err = got_delta_cache_alloc(&pack.delta_cache);
103 if (err)
104 goto done;
106 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
107 #ifndef PROFILE
108 /* revoke access to most system calls */
109 if (pledge("stdio recvfd", NULL) == -1) {
110 err = got_error_from_errno("pledge");
111 got_privsep_send_error(&ibuf, err);
112 return 1;
115 /* revoke fs access */
116 if (landlock_no_fs() == -1) {
117 err = got_error_from_errno("landlock_no_fs");
118 got_privsep_send_error(&ibuf, err);
119 return 1;
121 if (cap_enter() == -1) {
122 err = got_error_from_errno("cap_enter");
123 got_privsep_send_error(&ibuf, err);
124 return 1;
126 #endif
127 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
128 if (err)
129 goto done;
130 if (imsg.hdr.type == GOT_IMSG_STOP)
131 goto done;
132 if (imsg.hdr.type != GOT_IMSG_IDXPACK_REQUEST) {
133 err = got_error(GOT_ERR_PRIVSEP_MSG);
134 goto done;
136 if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(pack_hash)) {
137 err = got_error(GOT_ERR_PRIVSEP_LEN);
138 goto done;
140 memcpy(pack_hash, imsg.data, sizeof(pack_hash));
141 pack.fd = imsg.fd;
143 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
144 if (err)
145 goto done;
146 if (imsg.hdr.type == GOT_IMSG_STOP)
147 goto done;
148 if (imsg.hdr.type != GOT_IMSG_IDXPACK_OUTFD) {
149 err = got_error(GOT_ERR_PRIVSEP_MSG);
150 goto done;
152 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
153 err = got_error(GOT_ERR_PRIVSEP_LEN);
154 goto done;
156 idxfd = imsg.fd;
158 for (i = 0; i < nitems(tmpfiles); i++) {
159 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
160 if (err)
161 goto done;
162 if (imsg.hdr.type == GOT_IMSG_STOP)
163 goto done;
164 if (imsg.hdr.type != GOT_IMSG_TMPFD) {
165 err = got_error(GOT_ERR_PRIVSEP_MSG);
166 goto done;
168 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
169 err = got_error(GOT_ERR_PRIVSEP_LEN);
170 goto done;
172 tmpfd = imsg.fd;
173 tmpfiles[i] = fdopen(tmpfd, "w+");
174 if (tmpfiles[i] == NULL) {
175 err = got_error_from_errno("fdopen");
176 goto done;
178 tmpfd = -1;
181 if (lseek(pack.fd, 0, SEEK_END) == -1) {
182 err = got_error_from_errno("lseek");
183 goto done;
185 packfile_size = lseek(pack.fd, 0, SEEK_CUR);
186 if (packfile_size == -1) {
187 err = got_error_from_errno("lseek");
188 goto done;
190 pack.filesize = packfile_size;
192 if (lseek(pack.fd, 0, SEEK_SET) == -1) {
193 err = got_error_from_errno("lseek");
194 goto done;
197 #ifndef GOT_PACK_NO_MMAP
198 if (pack.filesize > 0 && pack.filesize <= SIZE_MAX) {
199 pack.map = mmap(NULL, pack.filesize, PROT_READ, MAP_PRIVATE,
200 pack.fd, 0);
201 if (pack.map == MAP_FAILED)
202 pack.map = NULL; /* fall back to read(2) */
204 #endif
205 err = got_pack_index(&pack, idxfd, tmpfiles[0], tmpfiles[1],
206 tmpfiles[2], pack_hash, send_index_pack_progress, &ibuf, &rl);
207 done:
208 close_err = got_pack_close(&pack);
209 if (close_err && err == NULL)
210 err = close_err;
211 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
212 err = got_error_from_errno("close");
213 if (tmpfd != -1 && close(tmpfd) == -1 && err == NULL)
214 err = got_error_from_errno("close");
215 for (i = 0; i < nitems(tmpfiles); i++) {
216 if (tmpfiles[i] != NULL && fclose(tmpfiles[i]) == EOF &&
217 err == NULL)
218 err = got_error_from_errno("fclose");
221 if (err == NULL)
222 err = send_index_pack_done(&ibuf);
223 if (err) {
224 got_privsep_send_error(&ibuf, err);
225 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
226 got_privsep_send_error(&ibuf, err);
227 exit(1);
230 exit(0);