| 1 | /* |
| 2 | * Copyright (c) 2019-2022 Joris Vink <[email protected]> |
| 3 | * |
| 4 | * Permission to use, copy, modify, and distribute this software for any |
| 5 | * purpose with or without fee is hereby granted, provided that the above |
| 6 | * copyright notice and this permission notice appear in all copies. |
| 7 | * |
| 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 | */ |
| 16 | |
| 17 | #include <sys/param.h> |
| 18 | #include <sys/stat.h> |
| 19 | #include <sys/types.h> |
| 20 | #include <sys/mman.h> |
| 21 | |
| 22 | #include <stdint.h> |
| 23 | |
| 24 | #include "kore.h"cxx . . $ `<file>/include/kore/kore.h`/ |
| 25 | |
| 26 | /* cached filerefs expire after 30 seconds of inactivity. */ |
| 27 | #define FILEREF_EXPIRATIONcxx . . $ `src/fileref.c:27:9`! (1000 * 30) |
| 28 | |
| 29 | static void fileref_timer_primecxx . . $ fileref_timer_prime(49f6e7a06ebc5aa8).(void); |
| 30 | static void fileref_dropcxx . . $ fileref_drop(6512ee26e70dae5b).(struct kore_filerefcxx . . $ kore_fileref# *); |
| 31 | static void fileref_soft_removecxx . . $ fileref_soft_remove(6512ee26e70dae5b).(struct kore_filerefcxx . . $ kore_fileref# *); |
| 32 | static void fileref_expiration_checkcxx . . $ fileref_expiration_check(5500c4293b1bbef0).(void *, u_int64_tcxx . . $ u_int64_t#); |
| 33 | |
| 34 | static TAILQ_HEADcxx . . $ `<external>/2546723208511035544/queue.h:372:9`!(, kore_fileref) refscxx . . $ refs.; |
| 35 | static struct kore_poolcxx . . $ kore_pool# ref_poolcxx . . $ ref_pool.; |
| 36 | static struct kore_timercxx . . $ kore_timer# *ref_timercxx . . $ ref_timer. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 37 | |
| 38 | void |
| 39 | kore_fileref_initcxx . . $ kore_fileref_init(49f6e7a06ebc5aa8).(void) |
| 40 | { |
| 41 | TAILQ_INITcxx . . $ `<external>/2546723208511035544/queue.h:387:9`!(&refs); |
| 42 | kore_pool_initcxx . . $ kore_pool_init(19b74537ff42c712).(&ref_poolcxx . . $ ref_pool., "ref_pool", sizeof(struct kore_filerefcxx . . $ kore_fileref#), 100); |
| 43 | } |
| 44 | |
| 45 | struct kore_filerefcxx . . $ kore_fileref# * |
| 46 | kore_fileref_createcxx . . $ kore_fileref_create(14957474d19f132d).(struct kore_servercxx . . $ kore_server# *srv, const char *path, int fd, |
| 47 | off_tcxx . . $ off_t# size, struct timespeccxx . . $ timespec# *ts) |
| 48 | { |
| 49 | struct kore_filerefcxx . . $ kore_fileref# *ref; |
| 50 | |
| 51 | fileref_timer_primecxx . . $ fileref_timer_prime(49f6e7a06ebc5aa8).(); |
| 52 | |
| 53 | if ((ref = kore_fileref_getcxx . . $ kore_fileref_get(2e3c4ee0a90b8886).(path, srv->tlscxx . . $ kore_server#tls.)) != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 54 | return (ref); |
| 55 | |
| 56 | ref = kore_pool_getcxx . . $ kore_pool_get(f5c578167d321b17).(&ref_poolcxx . . $ ref_pool.); |
| 57 | |
| 58 | ref->cntcxx . . $ kore_fileref#cnt. = 1; |
| 59 | ref->flagscxx . . $ kore_fileref#flags. = 0; |
| 60 | ref->sizecxx . . $ kore_fileref#size. = size; |
| 61 | ref->ontlscxx . . $ kore_fileref#ontls. = srv->tlscxx . . $ kore_server#tls.; |
| 62 | ref->pathcxx . . $ kore_fileref#path. = kore_strdupcxx . . $ kore_strdup(a8b8e32347182212).(path); |
| 63 | ref->mtime_seccxx . . $ kore_fileref#mtime_sec. = ts->tv_seccxx . . $ timespec#tv_sec.; |
| 64 | ref->mtimecxx . . $ kore_fileref#mtime. = ((u_int64_tcxx . . $ u_int64_t#)(ts->tv_seccxx . . $ timespec#tv_sec. * 1000 + (ts->tv_nseccxx . . $ timespec#tv_nsec. / 1000000))); |
| 65 | |
| 66 | #if !defined(KORE_USE_PLATFORM_SENDFILEcxx . . $ `include/kore/kore.h:56:9`!) |
| 67 | if ((uintmax_t)size> SIZE_MAX) { |
| 68 | kore_pool_put(&ref_pool, ref); |
| 69 | return (NULL); |
| 70 | } |
| 71 | |
| 72 | ref->base = mmap(NULL, (size_t)size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 73 | if (ref->base == MAP_FAILED) |
| 74 | fatal("net_send_file: mmap failed: %s", errno_s); |
| 75 | if (madvise(ref->base, (size_t)size, MADV_SEQUENTIAL) == -1) |
| 76 | fatal("net_send_file: madvise: %s", errno_s); |
| 77 | close(fd); |
| 78 | #else |
| 79 | if (srv->tlscxx . . $ kore_server#tls. == 0) { |
| 80 | ref->fdcxx . . $ kore_fileref#fd. = fd; |
| 81 | } else { |
| 82 | if ((uintmax_tcxx . . $ uintmax_t#)size > SIZE_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:216:11`!) { |
| 83 | kore_pool_putcxx . . $ kore_pool_put(d9a47b307a51c9d5).(&ref_poolcxx . . $ ref_pool., ref); |
| 84 | return (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 85 | } |
| 86 | |
| 87 | ref->basecxx . . $ kore_fileref#base. = mmapcxx . . $ mmap(ff06c1c904cdf0e2).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, |
| 88 | (size_t)size, PROT_READcxx . . $ `<external>/17226767460805440860/mman-linux.h:32:9`!, MAP_PRIVATEcxx . . $ `<external>/17226767460805440860/mman-linux.h:43:9`!, fd, 0); |
| 89 | if (ref->basecxx . . $ kore_fileref#base. == MAP_FAILEDcxx . . $ `<external>/6724834311684550606/mman.h:44:9`!) |
| 90 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("net_send_file: mmap failed: %s", errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 91 | if (madvisecxx . . $ madvise(e72f86de37bb8b46).(ref->basecxx . . $ kore_fileref#base., (size_t)size, MADV_SEQUENTIALcxx . . $ `<external>/17226767460805440860/mman-linux.h:90:10`!) == -1) |
| 92 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("net_send_file: madvise: %s", errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 93 | closecxx . . $ close(7864480464b09eea).(fd); |
| 94 | } |
| 95 | #endif |
| 96 | |
| 97 | #if defined(FILEREF_DEBUG) |
| 98 | kore_log(LOG_DEBUG, "ref:%p created", (void *)ref); |
| 99 | #endif |
| 100 | |
| 101 | TAILQ_INSERT_TAILcxx . . $ `<external>/2546723208511035544/queue.h:402:9`!(&refs, ref, list); |
| 102 | |
| 103 | return (ref); |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * Caller must call kore_fileref_release() after kore_fileref_get() even |
| 108 | * if they don't end up using the ref. |
| 109 | */ |
| 110 | struct kore_filerefcxx . . $ kore_fileref# * |
| 111 | kore_fileref_getcxx . . $ kore_fileref_get(2e3c4ee0a90b8886).(const char *path, int ontls) |
| 112 | { |
| 113 | struct statcxx . . $ stat# st; |
| 114 | struct kore_filerefcxx . . $ kore_fileref# *ref; |
| 115 | u_int64_tcxx . . $ u_int64_t# mtime; |
| 116 | |
| 117 | TAILQ_FOREACHcxx . . $ `<external>/2546723208511035544/queue.h:435:9`!(ref, &refs, list) { |
| 118 | if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88).(ref->pathcxx . . $ kore_fileref#path., path) && ref->ontlscxx . . $ kore_fileref#ontls. == ontls) { |
| 119 | if (statcxx . . $ stat(a9cd1e23e96f423c).(ref->pathcxx . . $ kore_fileref#path., &st) == -1) { |
| 120 | if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != ENOENTcxx . . $ `<external>/8987895939074593727/errno-base.h:6:9`!) { |
| 121 | kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_ERRcxx . . $ `<external>/6168640806541382331/syslog.h:54:9`!, "stat(%s): %s", |
| 122 | ref->pathcxx . . $ kore_fileref#path., errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 123 | } |
| 124 | fileref_soft_removecxx . . $ fileref_soft_remove(6512ee26e70dae5b).(ref); |
| 125 | return (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 126 | } |
| 127 | |
| 128 | mtime = ((u_int64_tcxx . . $ u_int64_t#)(st.st_mtimcxx . . $ stat#st_mtim..tv_seccxx . . $ timespec#tv_sec. * 1000 + |
| 129 | (st.st_mtimcxx . . $ stat#st_mtim..tv_nseccxx . . $ timespec#tv_nsec. / 1000000))); |
| 130 | |
| 131 | if (ref->mtimecxx . . $ kore_fileref#mtime. != mtime) { |
| 132 | fileref_soft_removecxx . . $ fileref_soft_remove(6512ee26e70dae5b).(ref); |
| 133 | return (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 134 | } |
| 135 | |
| 136 | ref->cntcxx . . $ kore_fileref#cnt.++; |
| 137 | #if defined(FILEREF_DEBUG) |
| 138 | kore_log(LOG_DEBUG, "ref:%p cnt:%d", |
| 139 | (void *)ref, ref->cnt); |
| 140 | #endif |
| 141 | TAILQ_REMOVEcxx . . $ `<external>/2546723208511035544/queue.h:426:9`!(&refs, ref, list); |
| 142 | TAILQ_INSERT_HEADcxx . . $ `<external>/2546723208511035544/queue.h:392:9`!(&refs, ref, list); |
| 143 | return (ref); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | return (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 148 | } |
| 149 | |
| 150 | void |
| 151 | kore_fileref_releasecxx . . $ kore_fileref_release(6512ee26e70dae5b).(struct kore_filerefcxx . . $ kore_fileref# *ref) |
| 152 | { |
| 153 | ref->cntcxx . . $ kore_fileref#cnt.--; |
| 154 | |
| 155 | #if defined(FILEREF_DEBUG) |
| 156 | kore_log(LOG_DEBUG, "ref:%p released cnt:%d", (void *)ref, ref->cnt); |
| 157 | #endif |
| 158 | |
| 159 | if (ref->cntcxx . . $ kore_fileref#cnt. < 0) { |
| 160 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("kore_fileref_release: cnt < 0 (%p:%d)", |
| 161 | (void *)ref, ref->cntcxx . . $ kore_fileref#cnt.); |
| 162 | } |
| 163 | |
| 164 | if (ref->cntcxx . . $ kore_fileref#cnt. == 0) { |
| 165 | if (ref->flagscxx . . $ kore_fileref#flags. & KORE_FILEREF_SOFT_REMOVEDcxx . . $ `include/kore/kore.h:132:9`!) |
| 166 | fileref_dropcxx . . $ fileref_drop(6512ee26e70dae5b).(ref); |
| 167 | else |
| 168 | ref->expirationcxx . . $ kore_fileref#expiration. = kore_time_mscxx . . $ kore_time_ms(f708dce9a8856c70).() + FILEREF_EXPIRATIONcxx . . $ `src/fileref.c:27:9`!; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | static void |
| 173 | fileref_timer_primecxx . . $ fileref_timer_prime(49f6e7a06ebc5aa8).(void) |
| 174 | { |
| 175 | if (ref_timercxx . . $ ref_timer. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 176 | return; |
| 177 | |
| 178 | ref_timercxx . . $ ref_timer. = kore_timer_addcxx . . $ kore_timer_add(6973730e1cd229bb).(fileref_expiration_checkcxx . . $ fileref_expiration_check(5500c4293b1bbef0)., 10000, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, 0); |
| 179 | } |
| 180 | |
| 181 | static void |
| 182 | fileref_soft_removecxx . . $ fileref_soft_remove(6512ee26e70dae5b).(struct kore_filerefcxx . . $ kore_fileref# *ref) |
| 183 | { |
| 184 | if (ref->flagscxx . . $ kore_fileref#flags. & KORE_FILEREF_SOFT_REMOVEDcxx . . $ `include/kore/kore.h:132:9`!) |
| 185 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("fileref_soft_remove: %p already removed", (void *)ref); |
| 186 | |
| 187 | #if defined(FILEREF_DEBUG) |
| 188 | kore_log(LOG_DEBUG, "ref:%p softremoved", (void *)ref); |
| 189 | #endif |
| 190 | |
| 191 | TAILQ_REMOVEcxx . . $ `<external>/2546723208511035544/queue.h:426:9`!(&refs, ref, list); |
| 192 | ref->flagscxx . . $ kore_fileref#flags. |= KORE_FILEREF_SOFT_REMOVEDcxx . . $ `include/kore/kore.h:132:9`!; |
| 193 | |
| 194 | if (ref->cntcxx . . $ kore_fileref#cnt. == 0) |
| 195 | fileref_dropcxx . . $ fileref_drop(6512ee26e70dae5b).(ref); |
| 196 | } |
| 197 | |
| 198 | static void |
| 199 | fileref_expiration_checkcxx . . $ fileref_expiration_check(5500c4293b1bbef0).(void *arg, u_int64_tcxx . . $ u_int64_t# now) |
| 200 | { |
| 201 | struct kore_filerefcxx . . $ kore_fileref# *ref, *next; |
| 202 | |
| 203 | for (ref = TAILQ_FIRSTcxx . . $ `<external>/2546723208511035544/queue.h:458:9`!(&refs); ref != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; ref = next) { |
| 204 | next = TAILQ_NEXTcxx . . $ `<external>/2546723208511035544/queue.h:459:9`!(ref, list); |
| 205 | |
| 206 | if (ref->cntcxx . . $ kore_fileref#cnt. != 0) |
| 207 | continue; |
| 208 | |
| 209 | if (ref->expirationcxx . . $ kore_fileref#expiration. > now) |
| 210 | continue; |
| 211 | |
| 212 | #if defined(FILEREF_DEBUG) |
| 213 | kore_log(LOG_DEBUG, "ref:%p expired, removing", (void *)ref); |
| 214 | #endif |
| 215 | |
| 216 | fileref_dropcxx . . $ fileref_drop(6512ee26e70dae5b).(ref); |
| 217 | } |
| 218 | |
| 219 | if (TAILQ_EMPTYcxx . . $ `<external>/2546723208511035544/queue.h:457:9`!(&refs)) { |
| 220 | /* remove the timer. */ |
| 221 | ref_timercxx . . $ ref_timer.->flagscxx . . $ kore_timer#flags. |= KORE_TIMER_ONESHOTcxx . . $ `include/kore/kore.h:211:9`!; |
| 222 | ref_timercxx . . $ ref_timer. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | static void |
| 227 | fileref_dropcxx . . $ fileref_drop(6512ee26e70dae5b).(struct kore_filerefcxx . . $ kore_fileref# *ref) |
| 228 | { |
| 229 | #if defined(FILEREF_DEBUG) |
| 230 | kore_log(LOG_DEBUG, "ref:%p dropped", (void *)ref); |
| 231 | #endif |
| 232 | |
| 233 | if (!(ref->flagscxx . . $ kore_fileref#flags. & KORE_FILEREF_SOFT_REMOVEDcxx . . $ `include/kore/kore.h:132:9`!)) |
| 234 | TAILQ_REMOVEcxx . . $ `<external>/2546723208511035544/queue.h:426:9`!(&refs, ref, list); |
| 235 | |
| 236 | kore_freecxx . . $ kore_free(5d22bdacc48458e8).(ref->pathcxx . . $ kore_fileref#path.); |
| 237 | |
| 238 | #if !defined(KORE_USE_PLATFORM_SENDFILEcxx . . $ `include/kore/kore.h:56:9`!) |
| 239 | (void)munmap(ref->base, ref->size); |
| 240 | #else |
| 241 | closecxx . . $ close(7864480464b09eea).(ref->fdcxx . . $ kore_fileref#fd.); |
| 242 | #endif |
| 243 | kore_pool_putcxx . . $ kore_pool_put(d9a47b307a51c9d5).(&ref_poolcxx . . $ ref_pool., ref); |
| 244 | } |