Lines 37-44
Link Here
|
37 |
#include <stdint.h> |
37 |
#include <stdint.h> |
38 |
#include <sys/mman.h> |
38 |
#include <sys/mman.h> |
39 |
#include <sys/param.h> |
39 |
#include <sys/param.h> |
|
|
40 |
#include <sys/shm.h> |
40 |
#include <sys/stat.h> |
41 |
#include <sys/stat.h> |
41 |
|
42 |
|
|
|
43 |
#include <libc-mmap.h> |
42 |
#include "../../crypt/md5.h" |
44 |
#include "../../crypt/md5.h" |
43 |
#include "../localeinfo.h" |
45 |
#include "../localeinfo.h" |
44 |
#include "../locarchive.h" |
46 |
#include "../locarchive.h" |
Lines 79-99
static const char *locnames[] =
Link Here
|
79 |
mapping affects the address selection. So do this mapping from the |
81 |
mapping affects the address selection. So do this mapping from the |
80 |
actual file, even though it's only a dummy to reserve address space. */ |
82 |
actual file, even though it's only a dummy to reserve address space. */ |
81 |
static void * |
83 |
static void * |
82 |
prepare_address_space (int fd, size_t total, size_t *reserved, int *xflags) |
84 |
prepare_address_space (int fd, size_t total, size_t *reserved, int *xflags, |
|
|
85 |
void **mmap_base, size_t *mmap_len) |
83 |
{ |
86 |
{ |
84 |
if (total < RESERVE_MMAP_SIZE) |
87 |
if (total < RESERVE_MMAP_SIZE) |
85 |
{ |
88 |
{ |
86 |
void *p = mmap64 (NULL, RESERVE_MMAP_SIZE, PROT_NONE, MAP_SHARED, fd, 0); |
89 |
void *p = mmap64 (NULL, RESERVE_MMAP_SIZE, PROT_NONE, MAP_SHARED, fd, 0); |
87 |
if (p != MAP_FAILED) |
90 |
if (p != MAP_FAILED) |
88 |
{ |
91 |
{ |
89 |
*reserved = RESERVE_MMAP_SIZE; |
92 |
void *aligned_p = PTR_ALIGN_UP (p, MAP_FIXED_ALIGNMENT); |
90 |
*xflags = MAP_FIXED; |
93 |
size_t align_adjust = aligned_p - p; |
91 |
return p; |
94 |
*mmap_base = p; |
92 |
} |
95 |
*mmap_len = RESERVE_MMAP_SIZE; |
|
|
96 |
assert (align_adjust < RESERVE_MMAP_SIZE); |
97 |
*reserved = RESERVE_MMAP_SIZE - align_adjust; |
98 |
*xflags = MAP_FIXED; |
99 |
return aligned_p; |
100 |
} |
93 |
} |
101 |
} |
94 |
|
102 |
|
95 |
*reserved = total; |
103 |
*reserved = total; |
96 |
*xflags = 0; |
104 |
*xflags = 0; |
|
|
105 |
*mmap_base = NULL; |
106 |
*mmap_len = 0; |
97 |
return NULL; |
107 |
return NULL; |
98 |
} |
108 |
} |
99 |
|
109 |
|
Lines 151-159
create_archive (const char *archivefname, struct locarhandle *ah)
Link Here
|
151 |
error (EXIT_FAILURE, errval, _("cannot resize archive file")); |
161 |
error (EXIT_FAILURE, errval, _("cannot resize archive file")); |
152 |
} |
162 |
} |
153 |
|
163 |
|
154 |
size_t reserved; |
164 |
size_t reserved, mmap_len; |
155 |
int xflags; |
165 |
int xflags; |
156 |
void *p = prepare_address_space (fd, total, &reserved, &xflags); |
166 |
void *mmap_base; |
|
|
167 |
void *p = prepare_address_space (fd, total, &reserved, &xflags, &mmap_base, |
168 |
&mmap_len); |
157 |
|
169 |
|
158 |
/* Map the header and all the administration data structures. */ |
170 |
/* Map the header and all the administration data structures. */ |
159 |
p = mmap64 (p, total, PROT_READ | PROT_WRITE, MAP_SHARED | xflags, fd, 0); |
171 |
p = mmap64 (p, total, PROT_READ | PROT_WRITE, MAP_SHARED | xflags, fd, 0); |
Lines 199-204
create_archive (const char *archivefname, struct locarhandle *ah)
Link Here
|
199 |
} |
211 |
} |
200 |
|
212 |
|
201 |
ah->fd = fd; |
213 |
ah->fd = fd; |
|
|
214 |
ah->mmap_base = mmap_base; |
215 |
ah->mmap_len = mmap_len; |
202 |
ah->addr = p; |
216 |
ah->addr = p; |
203 |
ah->mmaped = total; |
217 |
ah->mmaped = total; |
204 |
ah->reserved = reserved; |
218 |
ah->reserved = reserved; |
Lines 271-278
file_data_available_p (struct locarhandle *ah, uint32_t offset, uint32_t size)
Link Here
|
271 |
if (st.st_size > ah->reserved) |
285 |
if (st.st_size > ah->reserved) |
272 |
return false; |
286 |
return false; |
273 |
|
287 |
|
274 |
const size_t pagesz = getpagesize (); |
288 |
size_t start = ALIGN_DOWN (ah->mmaped, MAP_FIXED_ALIGNMENT); |
275 |
size_t start = ah->mmaped & ~(pagesz - 1); |
|
|
276 |
void *p = mmap64 (ah->addr + start, st.st_size - start, |
289 |
void *p = mmap64 (ah->addr + start, st.st_size - start, |
277 |
PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, |
290 |
PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, |
278 |
ah->fd, start); |
291 |
ah->fd, start); |
Lines 332-341
enlarge_archive (struct locarhandle *ah, const struct locarhead *head)
Link Here
|
332 |
MAP_SHARED | MAP_FIXED, ah->fd, 0); |
345 |
MAP_SHARED | MAP_FIXED, ah->fd, 0); |
333 |
else |
346 |
else |
334 |
{ |
347 |
{ |
335 |
munmap (ah->addr, ah->reserved); |
348 |
if (ah->mmap_base) |
|
|
349 |
munmap (ah->mmap_base, ah->mmap_len); |
350 |
else |
351 |
munmap (ah->addr, ah->reserved); |
336 |
ah->addr = mmap64 (NULL, st.st_size, PROT_READ | PROT_WRITE, |
352 |
ah->addr = mmap64 (NULL, st.st_size, PROT_READ | PROT_WRITE, |
337 |
MAP_SHARED, ah->fd, 0); |
353 |
MAP_SHARED, ah->fd, 0); |
338 |
ah->reserved = st.st_size; |
354 |
ah->reserved = st.st_size; |
|
|
355 |
ah->mmap_base = NULL; |
356 |
ah->mmap_len = 0; |
339 |
head = ah->addr; |
357 |
head = ah->addr; |
340 |
} |
358 |
} |
341 |
if (ah->addr == MAP_FAILED) |
359 |
if (ah->addr == MAP_FAILED) |
Lines 401-409
enlarge_archive (struct locarhandle *ah, const struct locarhead *head)
Link Here
|
401 |
error (EXIT_FAILURE, errval, _("cannot resize archive file")); |
419 |
error (EXIT_FAILURE, errval, _("cannot resize archive file")); |
402 |
} |
420 |
} |
403 |
|
421 |
|
404 |
size_t reserved; |
422 |
size_t reserved, mmap_len; |
405 |
int xflags; |
423 |
int xflags; |
406 |
void *p = prepare_address_space (fd, total, &reserved, &xflags); |
424 |
void *mmap_base; |
|
|
425 |
void *p = prepare_address_space (fd, total, &reserved, &xflags, &mmap_base, |
426 |
&mmap_len); |
407 |
|
427 |
|
408 |
/* Map the header and all the administration data structures. */ |
428 |
/* Map the header and all the administration data structures. */ |
409 |
p = mmap64 (p, total, PROT_READ | PROT_WRITE, MAP_SHARED | xflags, fd, 0); |
429 |
p = mmap64 (p, total, PROT_READ | PROT_WRITE, MAP_SHARED | xflags, fd, 0); |
Lines 423-428
enlarge_archive (struct locarhandle *ah, const struct locarhead *head)
Link Here
|
423 |
} |
443 |
} |
424 |
|
444 |
|
425 |
new_ah.mmaped = total; |
445 |
new_ah.mmaped = total; |
|
|
446 |
new_ah.mmap_base = mmap_base; |
447 |
new_ah.mmap_len = mmap_len; |
426 |
new_ah.addr = p; |
448 |
new_ah.addr = p; |
427 |
new_ah.fd = fd; |
449 |
new_ah.fd = fd; |
428 |
new_ah.reserved = reserved; |
450 |
new_ah.reserved = reserved; |
Lines 606-614
open_archive (struct locarhandle *ah, bool readonly)
Link Here
|
606 |
ah->fd = fd; |
628 |
ah->fd = fd; |
607 |
ah->mmaped = st.st_size; |
629 |
ah->mmaped = st.st_size; |
608 |
|
630 |
|
609 |
size_t reserved; |
631 |
size_t reserved, mmap_len; |
610 |
int xflags; |
632 |
int xflags; |
611 |
void *p = prepare_address_space (fd, st.st_size, &reserved, &xflags); |
633 |
void *mmap_base; |
|
|
634 |
void *p = prepare_address_space (fd, st.st_size, &reserved, &xflags, |
635 |
&mmap_base, &mmap_len); |
612 |
|
636 |
|
613 |
/* Map the entire file. We might need to compare the category data |
637 |
/* Map the entire file. We might need to compare the category data |
614 |
in the file with the newly added data. */ |
638 |
in the file with the newly added data. */ |
Lines 620-625
open_archive (struct locarhandle *ah, bool readonly)
Link Here
|
620 |
error (EXIT_FAILURE, errno, _("cannot map archive header")); |
644 |
error (EXIT_FAILURE, errno, _("cannot map archive header")); |
621 |
} |
645 |
} |
622 |
ah->reserved = reserved; |
646 |
ah->reserved = reserved; |
|
|
647 |
ah->mmap_base = mmap_base; |
648 |
ah->mmap_len = mmap_len; |
623 |
} |
649 |
} |
624 |
|
650 |
|
625 |
|
651 |
|
Lines 628-634
close_archive (struct locarhandle *ah)
Link Here
|
628 |
{ |
654 |
{ |
629 |
if (ah->fd != -1) |
655 |
if (ah->fd != -1) |
630 |
{ |
656 |
{ |
631 |
munmap (ah->addr, ah->reserved); |
657 |
if (ah->mmap_base) |
|
|
658 |
munmap (ah->mmap_base, ah->mmap_len); |
659 |
else |
660 |
munmap (ah->addr, ah->reserved); |
632 |
close (ah->fd); |
661 |
close (ah->fd); |
633 |
} |
662 |
} |
634 |
} |
663 |
} |