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_object.h"
38 #include "got_lib_privsep.h"
39 #include "got_lib_ratelimit.h"
40 #include "got_lib_pack.h"
41 #include "got_lib_pack_index.h"
43 #ifndef nitems
44 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
45 #endif
47 static const struct got_error *
48 send_index_pack_progress(void *arg, uint32_t nobj_total, uint32_t nobj_indexed,
49 uint32_t nobj_loose, uint32_t nobj_resolved)
50 {
51 struct imsgbuf *ibuf = arg;
52 struct got_imsg_index_pack_progress iprogress;
54 iprogress.nobj_total = nobj_total;
55 iprogress.nobj_indexed = nobj_indexed;
56 iprogress.nobj_loose = nobj_loose;
57 iprogress.nobj_resolved = nobj_resolved;
59 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
60 &iprogress, sizeof(iprogress)) == -1)
61 return got_error_from_errno("imsg_compose IDXPACK_PROGRESS");
63 return got_privsep_flush_imsg(ibuf);
64 }
66 static const struct got_error *
67 send_index_pack_done(struct imsgbuf *ibuf)
68 {
69 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
70 return got_error_from_errno("imsg_compose FETCH");
71 return got_privsep_flush_imsg(ibuf);
72 }
75 int
76 main(int argc, char **argv)
77 {
78 const struct got_error *err = NULL, *close_err;
79 struct imsgbuf ibuf;
80 struct imsg imsg;
81 size_t i;
82 int idxfd = -1, tmpfd = -1;
83 FILE *tmpfiles[3];
84 struct got_pack pack;
85 uint8_t pack_hash[SHA1_DIGEST_LENGTH];
86 off_t packfile_size;
87 struct got_ratelimit rl;
88 #if 0
89 static int attached;
90 while (!attached)
91 sleep(1);
92 #endif
94 got_ratelimit_init(&rl, 0, 500);
96 for (i = 0; i < nitems(tmpfiles); i++)
97 tmpfiles[i] = NULL;
99 memset(&pack, 0, sizeof(pack));
100 pack.fd = -1;
101 err = got_delta_cache_alloc(&pack.delta_cache);
102 if (err)
103 goto done;
105 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
106 #ifndef PROFILE
107 /* revoke access to most system calls */
108 if (pledge("stdio recvfd", NULL) == -1) {
109 err = got_error_from_errno("pledge");
110 got_privsep_send_error(&ibuf, err);
111 return 1;
114 /* revoke fs access */
115 if (landlock_no_fs() == -1) {
116 err = got_error_from_errno("landlock_no_fs");
117 got_privsep_send_error(&ibuf, err);
118 return 1;
120 if (cap_enter() == -1) {
121 err = got_error_from_errno("cap_enter");
122 got_privsep_send_error(&ibuf, err);
123 return 1;
125 #endif
126 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
127 if (err)
128 goto done;
129 if (imsg.hdr.type == GOT_IMSG_STOP)
130 goto done;
131 if (imsg.hdr.type != GOT_IMSG_IDXPACK_REQUEST) {
132 err = got_error(GOT_ERR_PRIVSEP_MSG);
133 goto done;
135 if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(pack_hash)) {
136 err = got_error(GOT_ERR_PRIVSEP_LEN);
137 goto done;
139 memcpy(pack_hash, imsg.data, sizeof(pack_hash));
140 pack.fd = imsg.fd;
142 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
143 if (err)
144 goto done;
145 if (imsg.hdr.type == GOT_IMSG_STOP)
146 goto done;
147 if (imsg.hdr.type != GOT_IMSG_IDXPACK_OUTFD) {
148 err = got_error(GOT_ERR_PRIVSEP_MSG);
149 goto done;
151 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
152 err = got_error(GOT_ERR_PRIVSEP_LEN);
153 goto done;
155 idxfd = imsg.fd;
157 for (i = 0; i < nitems(tmpfiles); i++) {
158 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
159 if (err)
160 goto done;
161 if (imsg.hdr.type == GOT_IMSG_STOP)
162 goto done;
163 if (imsg.hdr.type != GOT_IMSG_TMPFD) {
164 err = got_error(GOT_ERR_PRIVSEP_MSG);
165 goto done;
167 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
168 err = got_error(GOT_ERR_PRIVSEP_LEN);
169 goto done;
171 tmpfd = imsg.fd;
172 tmpfiles[i] = fdopen(tmpfd, "w+");
173 if (tmpfiles[i] == NULL) {
174 err = got_error_from_errno("fdopen");
175 goto done;
177 tmpfd = -1;
180 if (lseek(pack.fd, 0, SEEK_END) == -1) {
181 err = got_error_from_errno("lseek");
182 goto done;
184 packfile_size = lseek(pack.fd, 0, SEEK_CUR);
185 if (packfile_size == -1) {
186 err = got_error_from_errno("lseek");
187 goto done;
189 pack.filesize = packfile_size;
191 if (lseek(pack.fd, 0, SEEK_SET) == -1) {
192 err = got_error_from_errno("lseek");
193 goto done;
196 #ifndef GOT_PACK_NO_MMAP
197 if (pack.filesize > 0 && pack.filesize <= SIZE_MAX) {
198 pack.map = mmap(NULL, pack.filesize, PROT_READ, MAP_PRIVATE,
199 pack.fd, 0);
200 if (pack.map == MAP_FAILED)
201 pack.map = NULL; /* fall back to read(2) */
203 #endif
204 err = got_pack_index(&pack, idxfd, tmpfiles[0], tmpfiles[1],
205 tmpfiles[2], pack_hash, send_index_pack_progress, &ibuf, &rl);
206 done:
207 close_err = got_pack_close(&pack);
208 if (close_err && err == NULL)
209 err = close_err;
210 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
211 err = got_error_from_errno("close");
212 if (tmpfd != -1 && close(tmpfd) == -1 && err == NULL)
213 err = got_error_from_errno("close");
214 for (i = 0; i < nitems(tmpfiles); i++) {
215 if (tmpfiles[i] != NULL && fclose(tmpfiles[i]) == EOF &&
216 err == NULL)
217 err = got_error_from_errno("fclose");
220 if (err == NULL)
221 err = send_index_pack_done(&ibuf);
222 if (err) {
223 got_privsep_send_error(&ibuf, err);
224 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
225 got_privsep_send_error(&ibuf, err);
226 exit(1);
229 exit(0);