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_compat.h"
33 #include "got_error.h"
34 #include "got_object.h"
36 #include "got_lib_delta.h"
37 #include "got_lib_delta_cache.h"
38 #include "got_lib_hash.h"
39 #include "got_lib_object.h"
40 #include "got_lib_privsep.h"
41 #include "got_lib_ratelimit.h"
42 #include "got_lib_pack.h"
43 #include "got_lib_pack_index.h"
45 #ifndef nitems
46 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
47 #endif
49 static const struct got_error *
50 send_index_pack_progress(void *arg, uint32_t nobj_total, uint32_t nobj_indexed,
51 uint32_t nobj_loose, uint32_t nobj_resolved)
52 {
53 struct imsgbuf *ibuf = arg;
54 struct got_imsg_index_pack_progress iprogress;
56 iprogress.nobj_total = nobj_total;
57 iprogress.nobj_indexed = nobj_indexed;
58 iprogress.nobj_loose = nobj_loose;
59 iprogress.nobj_resolved = nobj_resolved;
61 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
62 &iprogress, sizeof(iprogress)) == -1)
63 return got_error_from_errno("imsg_compose IDXPACK_PROGRESS");
65 return got_privsep_flush_imsg(ibuf);
66 }
68 static const struct got_error *
69 send_index_pack_done(struct imsgbuf *ibuf)
70 {
71 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
72 return got_error_from_errno("imsg_compose FETCH");
73 return got_privsep_flush_imsg(ibuf);
74 }
77 int
78 main(int argc, char **argv)
79 {
80 const struct got_error *err = NULL, *close_err;
81 struct imsgbuf ibuf;
82 struct imsg imsg;
83 size_t i;
84 int idxfd = -1, tmpfd = -1;
85 FILE *tmpfiles[3];
86 struct got_pack pack;
87 uint8_t pack_hash[SHA1_DIGEST_LENGTH];
88 off_t packfile_size;
89 struct got_ratelimit rl;
90 #if 0
91 static int attached;
92 while (!attached)
93 sleep(1);
94 #endif
96 got_ratelimit_init(&rl, 0, 500);
98 for (i = 0; i < nitems(tmpfiles); i++)
99 tmpfiles[i] = NULL;
101 memset(&pack, 0, sizeof(pack));
102 pack.fd = -1;
103 err = got_delta_cache_alloc(&pack.delta_cache);
104 if (err)
105 goto done;
107 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
108 #ifndef PROFILE
109 /* revoke access to most system calls */
110 if (pledge("stdio recvfd", NULL) == -1) {
111 err = got_error_from_errno("pledge");
112 got_privsep_send_error(&ibuf, err);
113 return 1;
116 /* revoke fs access */
117 if (landlock_no_fs() == -1) {
118 err = got_error_from_errno("landlock_no_fs");
119 got_privsep_send_error(&ibuf, err);
120 return 1;
122 if (cap_enter() == -1) {
123 err = got_error_from_errno("cap_enter");
124 got_privsep_send_error(&ibuf, err);
125 return 1;
127 #endif
128 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
129 if (err)
130 goto done;
131 if (imsg.hdr.type == GOT_IMSG_STOP)
132 goto done;
133 if (imsg.hdr.type != GOT_IMSG_IDXPACK_REQUEST) {
134 err = got_error(GOT_ERR_PRIVSEP_MSG);
135 goto done;
137 if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(pack_hash)) {
138 err = got_error(GOT_ERR_PRIVSEP_LEN);
139 goto done;
141 memcpy(pack_hash, imsg.data, sizeof(pack_hash));
142 pack.fd = imsg.fd;
144 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
145 if (err)
146 goto done;
147 if (imsg.hdr.type == GOT_IMSG_STOP)
148 goto done;
149 if (imsg.hdr.type != GOT_IMSG_IDXPACK_OUTFD) {
150 err = got_error(GOT_ERR_PRIVSEP_MSG);
151 goto done;
153 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
154 err = got_error(GOT_ERR_PRIVSEP_LEN);
155 goto done;
157 idxfd = imsg.fd;
159 for (i = 0; i < nitems(tmpfiles); i++) {
160 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
161 if (err)
162 goto done;
163 if (imsg.hdr.type == GOT_IMSG_STOP)
164 goto done;
165 if (imsg.hdr.type != GOT_IMSG_TMPFD) {
166 err = got_error(GOT_ERR_PRIVSEP_MSG);
167 goto done;
169 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
170 err = got_error(GOT_ERR_PRIVSEP_LEN);
171 goto done;
173 tmpfd = imsg.fd;
174 tmpfiles[i] = fdopen(tmpfd, "w+");
175 if (tmpfiles[i] == NULL) {
176 err = got_error_from_errno("fdopen");
177 goto done;
179 tmpfd = -1;
182 if (lseek(pack.fd, 0, SEEK_END) == -1) {
183 err = got_error_from_errno("lseek");
184 goto done;
186 packfile_size = lseek(pack.fd, 0, SEEK_CUR);
187 if (packfile_size == -1) {
188 err = got_error_from_errno("lseek");
189 goto done;
191 pack.filesize = packfile_size;
193 if (lseek(pack.fd, 0, SEEK_SET) == -1) {
194 err = got_error_from_errno("lseek");
195 goto done;
198 #ifndef GOT_PACK_NO_MMAP
199 if (pack.filesize > 0 && pack.filesize <= SIZE_MAX) {
200 pack.map = mmap(NULL, pack.filesize, PROT_READ, MAP_PRIVATE,
201 pack.fd, 0);
202 if (pack.map == MAP_FAILED)
203 pack.map = NULL; /* fall back to read(2) */
205 #endif
206 err = got_pack_index(&pack, idxfd, tmpfiles[0], tmpfiles[1],
207 tmpfiles[2], pack_hash, send_index_pack_progress, &ibuf, &rl);
208 done:
209 close_err = got_pack_close(&pack);
210 if (close_err && err == NULL)
211 err = close_err;
212 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
213 err = got_error_from_errno("close");
214 if (tmpfd != -1 && close(tmpfd) == -1 && err == NULL)
215 err = got_error_from_errno("close");
216 for (i = 0; i < nitems(tmpfiles); i++) {
217 if (tmpfiles[i] != NULL && fclose(tmpfiles[i]) == EOF &&
218 err == NULL)
219 err = got_error_from_errno("fclose");
222 if (err == NULL)
223 err = send_index_pack_done(&ibuf);
224 if (err) {
225 got_privsep_send_error(&ibuf, err);
226 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
227 got_privsep_send_error(&ibuf, err);
228 exit(1);
231 exit(0);