| 1 | /* |
| 2 | * Copyright (c) 2013-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/types.h> |
| 18 | #include <sys/stat.h> |
| 19 | |
| 20 | #include <dlfcn.h> |
| 21 | |
| 22 | #include "kore.h"cxx . . $ `<file>/include/kore/kore.h`/ |
| 23 | |
| 24 | #if !defined(KORE_NO_HTTP) |
| 25 | #include "http.h"cxx . . $ `<file>/include/kore/http.h`/ |
| 26 | #endif |
| 27 | |
| 28 | #if defined(KORE_USE_PYTHON) |
| 29 | #include "python_api.h" |
| 30 | #endif |
| 31 | |
| 32 | static TAILQ_HEADcxx . . $ `<external>/2546723208511035544/queue.h:372:9`!(, kore_module) modulescxx . . $ modules.; |
| 33 | |
| 34 | static void native_freecxx . . $ native_free(2e695f85d3415088).(struct kore_modulecxx . . $ kore_module# *); |
| 35 | static void native_loadcxx . . $ native_load(2e695f85d3415088).(struct kore_modulecxx . . $ kore_module# *); |
| 36 | static void native_reloadcxx . . $ native_reload(2e695f85d3415088).(struct kore_modulecxx . . $ kore_module# *); |
| 37 | static void *native_getsymcxx . . $ native_getsym(22e576a3c6dd5e12).(struct kore_modulecxx . . $ kore_module# *, const char *); |
| 38 | |
| 39 | struct kore_module_functionscxx . . $ kore_module_functions# kore_native_modulecxx . . $ kore_native_module. = { |
| 40 | .free = native_freecxx . . $ native_free(2e695f85d3415088)., |
| 41 | .load = native_loadcxx . . $ native_load(2e695f85d3415088)., |
| 42 | .getsym = native_getsymcxx . . $ native_getsym(22e576a3c6dd5e12)., |
| 43 | .reload = native_reloadcxx . . $ native_reload(2e695f85d3415088)., |
| 44 | }; |
| 45 | |
| 46 | void |
| 47 | kore_module_initcxx . . $ kore_module_init(49f6e7a06ebc5aa8).(void) |
| 48 | { |
| 49 | TAILQ_INITcxx . . $ `<external>/2546723208511035544/queue.h:387:9`!(&modules); |
| 50 | } |
| 51 | |
| 52 | void |
| 53 | kore_module_cleanupcxx . . $ kore_module_cleanup(49f6e7a06ebc5aa8).(void) |
| 54 | { |
| 55 | struct kore_modulecxx . . $ kore_module# *module, *next; |
| 56 | |
| 57 | for (module = TAILQ_FIRSTcxx . . $ `<external>/2546723208511035544/queue.h:458:9`!(&modules); module != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; module = next) { |
| 58 | next = TAILQ_NEXTcxx . . $ `<external>/2546723208511035544/queue.h:459:9`!(module, list); |
| 59 | TAILQ_REMOVEcxx . . $ `<external>/2546723208511035544/queue.h:426:9`!(&modules, module, list); |
| 60 | module->funcxx . . $ kore_module#fun.->freecxx . . $ kore_module_functions#free.(module); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | struct kore_modulecxx . . $ kore_module# * |
| 65 | kore_module_loadcxx . . $ kore_module_load(4ae48391f6be8edd).(const char *path, const char *onload, int type) |
| 66 | { |
| 67 | struct statcxx . . $ stat# st; |
| 68 | struct kore_modulecxx . . $ kore_module# *module; |
| 69 | |
| 70 | module = kore_malloccxx . . $ kore_malloc(bfcfa4d6b7f7ef64).(sizeof(struct kore_modulecxx . . $ kore_module#)); |
| 71 | module->ocbcxx . . $ kore_module#ocb. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 72 | module->typecxx . . $ kore_module#type. = type; |
| 73 | module->onloadcxx . . $ kore_module#onload. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 74 | module->handlecxx . . $ kore_module#handle. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 75 | |
| 76 | if (path != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 77 | if (statcxx . . $ stat(a9cd1e23e96f423c).(path, &st) == -1) |
| 78 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("stat(%s): %s", path, errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 79 | |
| 80 | module->pathcxx . . $ kore_module#path. = kore_strdupcxx . . $ kore_strdup(a8b8e32347182212).(path); |
| 81 | } else { |
| 82 | module->pathcxx . . $ kore_module#path. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 83 | } |
| 84 | |
| 85 | switch (module->typecxx . . $ kore_module#type.) { |
| 86 | case KORE_MODULE_NATIVEcxx . . $ `include/kore/kore.h:414:9`!: |
| 87 | module->funcxx . . $ kore_module#fun. = &kore_native_modulecxx . . $ kore_native_module.; |
| 88 | module->runtimecxx . . $ kore_module#runtime. = &kore_native_runtimecxx . . $ kore_native_runtime.; |
| 89 | break; |
| 90 | #if defined(KORE_USE_PYTHON) |
| 91 | case KORE_MODULE_PYTHON: |
| 92 | module->fun = &kore_python_module; |
| 93 | module->runtime = &kore_python_runtime; |
| 94 | break; |
| 95 | #endif |
| 96 | default: |
| 97 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("kore_module_load: unknown type %d", type); |
| 98 | } |
| 99 | |
| 100 | module->funcxx . . $ kore_module#fun.->loadcxx . . $ kore_module_functions#load.(module); |
| 101 | TAILQ_INSERT_TAILcxx . . $ `<external>/2546723208511035544/queue.h:402:9`!(&modules, module, list); |
| 102 | |
| 103 | if (onload != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 104 | module->onloadcxx . . $ kore_module#onload. = kore_strdupcxx . . $ kore_strdup(a8b8e32347182212).(onload); |
| 105 | module->ocbcxx . . $ kore_module#ocb. = kore_malloccxx . . $ kore_malloc(bfcfa4d6b7f7ef64).(sizeof(*module->ocbcxx . . $ kore_module#ocb.)); |
| 106 | module->ocbcxx . . $ kore_module#ocb.->runtimecxx . . $ kore_runtime_call#runtime. = module->runtimecxx . . $ kore_module#runtime.; |
| 107 | module->ocbcxx . . $ kore_module#ocb.->addrcxx . . $ kore_runtime_call#addr. = module->funcxx . . $ kore_module#fun.->getsymcxx . . $ kore_module_functions#getsym.(module, onload); |
| 108 | |
| 109 | if (module->ocbcxx . . $ kore_module#ocb.->addrcxx . . $ kore_runtime_call#addr. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 110 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("%s: onload '%s' not present", |
| 111 | module->pathcxx . . $ kore_module#path., onload); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return (module); |
| 116 | } |
| 117 | |
| 118 | void |
| 119 | kore_module_onloadcxx . . $ kore_module_onload(49f6e7a06ebc5aa8).(void) |
| 120 | { |
| 121 | struct kore_modulecxx . . $ kore_module# *module; |
| 122 | |
| 123 | TAILQ_FOREACHcxx . . $ `<external>/2546723208511035544/queue.h:435:9`!(module, &modules, list) { |
| 124 | if (module->pathcxx . . $ kore_module#path. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! || module->ocbcxx . . $ kore_module#ocb. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 125 | continue; |
| 126 | |
| 127 | kore_runtime_onloadcxx . . $ kore_runtime_onload(c29b8bc328b374).(module->ocbcxx . . $ kore_module#ocb., KORE_MODULE_LOADcxx . . $ `include/kore/kore.h:411:9`!); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | void |
| 132 | kore_module_reloadcxx . . $ kore_module_reload(d4f767463ce0a6b3).(int cbs) |
| 133 | { |
| 134 | struct statcxx . . $ stat# st; |
| 135 | int ret; |
| 136 | struct kore_modulecxx . . $ kore_module# *module; |
| 137 | |
| 138 | TAILQ_FOREACHcxx . . $ `<external>/2546723208511035544/queue.h:435:9`!(module, &modules, list) { |
| 139 | if (module->pathcxx . . $ kore_module#path. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 140 | continue; |
| 141 | |
| 142 | if (statcxx . . $ stat(a9cd1e23e96f423c).(module->pathcxx . . $ kore_module#path., &st) == -1) { |
| 143 | kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_NOTICEcxx . . $ `<external>/6168640806541382331/syslog.h:56:9`!, "stat(%s): %s, skipping reload", |
| 144 | module->pathcxx . . $ kore_module#path., errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 145 | continue; |
| 146 | } |
| 147 | |
| 148 | if (module->ocbcxx . . $ kore_module#ocb. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! && cbs == 1) { |
| 149 | ret = kore_runtime_onloadcxx . . $ kore_runtime_onload(c29b8bc328b374).(module->ocbcxx . . $ kore_module#ocb., |
| 150 | KORE_MODULE_UNLOADcxx . . $ `include/kore/kore.h:412:9`!); |
| 151 | if (ret == KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!) { |
| 152 | kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_NOTICEcxx . . $ `<external>/6168640806541382331/syslog.h:56:9`!, |
| 153 | "%s forced no reloaded", module->pathcxx . . $ kore_module#path.); |
| 154 | continue; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | module->funcxx . . $ kore_module#fun.->reloadcxx . . $ kore_module_functions#reload.(module); |
| 159 | |
| 160 | if (module->onloadcxx . . $ kore_module#onload. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 161 | kore_freecxx . . $ kore_free(5d22bdacc48458e8).(module->ocbcxx . . $ kore_module#ocb.); |
| 162 | module->ocbcxx . . $ kore_module#ocb. = kore_malloccxx . . $ kore_malloc(bfcfa4d6b7f7ef64).(sizeof(*module->ocbcxx . . $ kore_module#ocb.)); |
| 163 | module->ocbcxx . . $ kore_module#ocb.->runtimecxx . . $ kore_runtime_call#runtime. = module->runtimecxx . . $ kore_module#runtime.; |
| 164 | module->ocbcxx . . $ kore_module#ocb.->addrcxx . . $ kore_runtime_call#addr. = |
| 165 | module->funcxx . . $ kore_module#fun.->getsymcxx . . $ kore_module_functions#getsym.(module, module->onloadcxx . . $ kore_module#onload.); |
| 166 | if (module->ocbcxx . . $ kore_module#ocb.->addrcxx . . $ kore_runtime_call#addr. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 167 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("%s: onload '%s' not present", |
| 168 | module->pathcxx . . $ kore_module#path., module->onloadcxx . . $ kore_module#onload.); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | if (module->ocbcxx . . $ kore_module#ocb. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! && cbs == 1) |
| 173 | kore_runtime_onloadcxx . . $ kore_runtime_onload(c29b8bc328b374).(module->ocbcxx . . $ kore_module#ocb., KORE_MODULE_LOADcxx . . $ `include/kore/kore.h:411:9`!); |
| 174 | |
| 175 | kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_NOTICEcxx . . $ `<external>/6168640806541382331/syslog.h:56:9`!, "reloaded '%s' module", module->pathcxx . . $ kore_module#path.); |
| 176 | } |
| 177 | |
| 178 | #if !defined(KORE_NO_HTTP) |
| 179 | kore_route_reloadcxx . . $ kore_route_reload(49f6e7a06ebc5aa8).(); |
| 180 | kore_validator_reloadcxx . . $ kore_validator_reload(49f6e7a06ebc5aa8).(); |
| 181 | #endif |
| 182 | } |
| 183 | |
| 184 | int |
| 185 | kore_module_loadedcxx . . $ kore_module_loaded(b126dc7c1de90089).(void) |
| 186 | { |
| 187 | if (TAILQ_EMPTYcxx . . $ `<external>/2546723208511035544/queue.h:457:9`!(&modules)) |
| 188 | return (0); |
| 189 | |
| 190 | return (1); |
| 191 | } |
| 192 | |
| 193 | void * |
| 194 | kore_module_getsymcxx . . $ kore_module_getsym(49a0dbba971d1b15).(const char *symbol, struct kore_runtimecxx . . $ kore_runtime# **runtime) |
| 195 | { |
| 196 | void *ptr; |
| 197 | struct kore_modulecxx . . $ kore_module# *module; |
| 198 | |
| 199 | if (runtime != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 200 | *runtime = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 201 | |
| 202 | TAILQ_FOREACHcxx . . $ `<external>/2546723208511035544/queue.h:435:9`!(module, &modules, list) { |
| 203 | ptr = module->funcxx . . $ kore_module#fun.->getsymcxx . . $ kore_module_functions#getsym.(module, symbol); |
| 204 | if (ptr != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 205 | if (runtime != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 206 | *runtime = module->runtimecxx . . $ kore_module#runtime.; |
| 207 | return (ptr); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | return (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 212 | } |
| 213 | |
| 214 | static void * |
| 215 | native_getsymcxx . . $ native_getsym(22e576a3c6dd5e12).(struct kore_modulecxx . . $ kore_module# *module, const char *symbol) |
| 216 | { |
| 217 | return (dlsymcxx . . $ dlsym(5c3a2d84d269ab4b).(module->handlecxx . . $ kore_module#handle., symbol)); |
| 218 | } |
| 219 | |
| 220 | static void |
| 221 | native_freecxx . . $ native_free(2e695f85d3415088).(struct kore_modulecxx . . $ kore_module# *module) |
| 222 | { |
| 223 | kore_freecxx . . $ kore_free(5d22bdacc48458e8).(module->pathcxx . . $ kore_module#path.); |
| 224 | (void)dlclosecxx . . $ dlclose(dedf1e9b1c614a07).(module->handlecxx . . $ kore_module#handle.); |
| 225 | kore_freecxx . . $ kore_free(5d22bdacc48458e8).(module); |
| 226 | } |
| 227 | |
| 228 | static void |
| 229 | native_reloadcxx . . $ native_reload(2e695f85d3415088).(struct kore_modulecxx . . $ kore_module# *module) |
| 230 | { |
| 231 | if (dlclosecxx . . $ dlclose(dedf1e9b1c614a07).(module->handlecxx . . $ kore_module#handle.)) |
| 232 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("cannot close existing module: %s", dlerrorcxx . . $ dlerror(41208ce6ca65dd58).()); |
| 233 | module->funcxx . . $ kore_module#fun.->loadcxx . . $ kore_module_functions#load.(module); |
| 234 | } |
| 235 | |
| 236 | static void |
| 237 | native_loadcxx . . $ native_load(2e695f85d3415088).(struct kore_modulecxx . . $ kore_module# *module) |
| 238 | { |
| 239 | module->handlecxx . . $ kore_module#handle. = dlopencxx . . $ dlopen(fb9ec0bb113f3da8).(module->pathcxx . . $ kore_module#path., RTLD_NOWcxx . . $ `<external>/10632388298615623587/dlfcn.h:25:9`! | RTLD_GLOBALcxx . . $ `<external>/10632388298615623587/dlfcn.h:33:9`!); |
| 240 | if (module->handlecxx . . $ kore_module#handle. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 241 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("%s: %s", module->pathcxx . . $ kore_module#path., dlerrorcxx . . $ dlerror(41208ce6ca65dd58).()); |
| 242 | } |