diff options
| author | Dirk Engling <erdgeist@erdgeist.org> | 2026-10-24 04:56:27 +0200 |
|---|---|---|
| committer | Dirk Engling <erdgeist@erdgeist.org> | 2026-10-24 04:56:27 +0200 |
| commit | 6ab9188d0cec3d887f537894ed669fe803049746 (patch) | |
| tree | f70827d8423ed4d496cb7d20ca12d82a9dc37ef9 | |
| parent | 1aa3411546ec370d600699906b7336eff665c077 (diff) | |
Implement a tiny mmap wrapper ourselves, replacing libowfat's wrapper
| -rw-r--r-- | ot_accesslist.c | 41 | ||||
| -rw-r--r-- | tests/stubs.c | 20 |
2 files changed, 38 insertions, 23 deletions
diff --git a/ot_accesslist.c b/ot_accesslist.c index e20f42c..6315bc2 100644 --- a/ot_accesslist.c +++ b/ot_accesslist.c | |||
| @@ -10,15 +10,19 @@ | |||
| 10 | #include <stdlib.h> | 10 | #include <stdlib.h> |
| 11 | #include <string.h> | 11 | #include <string.h> |
| 12 | #include <unistd.h> | 12 | #include <unistd.h> |
| 13 | #ifdef WANT_DYNAMIC_ACCESSLIST | 13 | #if defined(WANT_ACCESSLIST_BLACK) || defined(WANT_ACCESSLIST_WHITE) |
| 14 | #include <errno.h> | 14 | #include <fcntl.h> |
| 15 | #include <stdint.h> | ||
| 16 | #include <sys/mman.h> | ||
| 15 | #include <sys/stat.h> | 17 | #include <sys/stat.h> |
| 16 | #include <sys/types.h> | 18 | #include <sys/types.h> |
| 17 | #endif | 19 | #endif |
| 20 | #ifdef WANT_DYNAMIC_ACCESSLIST | ||
| 21 | #include <errno.h> | ||
| 22 | #endif | ||
| 18 | 23 | ||
| 19 | /* Libowfat */ | 24 | /* Libowfat */ |
| 20 | #include "ip6.h" | 25 | #include "ip6.h" |
| 21 | #include "mmap.h" | ||
| 22 | 26 | ||
| 23 | /* Opentracker */ | 27 | /* Opentracker */ |
| 24 | #include "ot_accesslist.h" | 28 | #include "ot_accesslist.h" |
| @@ -120,12 +124,37 @@ size_t accesslist_parse_hashes(ot_hash *dest, size_t capacity, const char *buf, | |||
| 120 | return count; | 124 | return count; |
| 121 | } | 125 | } |
| 122 | 126 | ||
| 127 | /* Map a file read only and report its size. Returns NULL if the file cannot | ||
| 128 | be opened, is empty, or is too large to map, leaving maplen untouched. */ | ||
| 129 | static const char *accesslist_map(const char *filename, size_t *maplen) { | ||
| 130 | struct stat st; | ||
| 131 | const char *map; | ||
| 132 | int fd = open(filename, O_RDONLY); | ||
| 133 | |||
| 134 | if (fd < 0) | ||
| 135 | return NULL; | ||
| 136 | |||
| 137 | if (fstat(fd, &st) < 0 || st.st_size <= 0 || (uintmax_t)st.st_size > (uintmax_t)SIZE_MAX) { | ||
| 138 | close(fd); | ||
| 139 | return NULL; | ||
| 140 | } | ||
| 141 | |||
| 142 | map = mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_SHARED, fd, 0); | ||
| 143 | close(fd); | ||
| 144 | |||
| 145 | if (map == MAP_FAILED) | ||
| 146 | return NULL; | ||
| 147 | |||
| 148 | *maplen = (size_t)st.st_size; | ||
| 149 | return map; | ||
| 150 | } | ||
| 151 | |||
| 123 | static void accesslist_readfile(void) { | 152 | static void accesslist_readfile(void) { |
| 124 | ot_accesslist *accesslist_new; | 153 | ot_accesslist *accesslist_new; |
| 125 | const char *map; | 154 | const char *map; |
| 126 | size_t maplen, capacity, found; | 155 | size_t maplen, capacity, found; |
| 127 | 156 | ||
| 128 | if ((map = mmap_read(g_accesslist_filename, &maplen)) == NULL) { | 157 | if ((map = accesslist_map(g_accesslist_filename, &maplen)) == NULL) { |
| 129 | char *wd = getcwd(NULL, 0); | 158 | char *wd = getcwd(NULL, 0); |
| 130 | fprintf(stderr, "Warning: Can't open accesslist file: %s (but will try to create it later, if necessary and possible).\nPWD: %s\n", g_accesslist_filename, wd); | 159 | fprintf(stderr, "Warning: Can't open accesslist file: %s (but will try to create it later, if necessary and possible).\nPWD: %s\n", g_accesslist_filename, wd); |
| 131 | free(wd); | 160 | free(wd); |
| @@ -137,7 +166,7 @@ static void accesslist_readfile(void) { | |||
| 137 | accesslist_new = accesslist_make(g_accesslist, capacity); | 166 | accesslist_new = accesslist_make(g_accesslist, capacity); |
| 138 | if (!accesslist_new) { | 167 | if (!accesslist_new) { |
| 139 | fprintf(stderr, "Warning: Not enough memory to allocate %zd bytes for accesslist buffer. May succeed later.\n", capacity * sizeof(ot_hash)); | 168 | fprintf(stderr, "Warning: Not enough memory to allocate %zd bytes for accesslist buffer. May succeed later.\n", capacity * sizeof(ot_hash)); |
| 140 | mmap_unmap(map, maplen); | 169 | munmap((void *)map, maplen); |
| 141 | return; | 170 | return; |
| 142 | } | 171 | } |
| 143 | 172 | ||
| @@ -146,7 +175,7 @@ static void accesslist_readfile(void) { | |||
| 146 | fprintf(stderr, "Added %zd info_hashes to accesslist\n", found); | 175 | fprintf(stderr, "Added %zd info_hashes to accesslist\n", found); |
| 147 | #endif | 176 | #endif |
| 148 | 177 | ||
| 149 | mmap_unmap(map, maplen); | 178 | munmap((void *)map, maplen); |
| 150 | 179 | ||
| 151 | qsort(accesslist_new->list, found, sizeof(ot_hash), vector_compare_hash); | 180 | qsort(accesslist_new->list, found, sizeof(ot_hash), vector_compare_hash); |
| 152 | accesslist_new->size = found; | 181 | accesslist_new->size = found; |
diff --git a/tests/stubs.c b/tests/stubs.c index febdd7b..ce93071 100644 --- a/tests/stubs.c +++ b/tests/stubs.c | |||
| @@ -14,21 +14,7 @@ | |||
| 14 | void free_peerlist(ot_peerlist *peer_list) { (void)peer_list; } | 14 | void free_peerlist(ot_peerlist *peer_list) { (void)peer_list; } |
| 15 | 15 | ||
| 16 | /* ot_accesslist.c is built with WANT_ACCESSLIST_BLACK so that the hash parser | 16 | /* ot_accesslist.c is built with WANT_ACCESSLIST_BLACK so that the hash parser |
| 17 | is reachable. These four belong to accesslist_readfile and the reload | 17 | is reachable. These two belong to accesslist_readfile and the reload worker, |
| 18 | worker, neither of which any test drives. */ | 18 | neither of which any test drives. */ |
| 19 | time_t g_now_seconds; | 19 | time_t g_now_seconds; |
| 20 | volatile int g_opentracker_running; | 20 | volatile int g_opentracker_running; |
| 21 | |||
| 22 | const char *mmap_read(const char *filename, size_t *filesize); | ||
| 23 | const char *mmap_read(const char *filename, size_t *filesize) { | ||
| 24 | (void)filename; | ||
| 25 | *filesize = 0; | ||
| 26 | return NULL; | ||
| 27 | } | ||
| 28 | |||
| 29 | int mmap_unmap(const char *mapped, size_t maplen); | ||
| 30 | int mmap_unmap(const char *mapped, size_t maplen) { | ||
| 31 | (void)mapped; | ||
| 32 | (void)maplen; | ||
| 33 | return 0; | ||
| 34 | } | ||
