Reload module in loop

This commit is contained in:
Camden Dixie O'Brien 2024-12-29 14:08:26 +00:00
parent 86a8a6c5f7
commit 519443ed07

23
main.c
View File

@ -13,10 +13,27 @@ typedef const char *(*getmsg_t)(void);
int main(void) int main(void)
{ {
void *mod = dlopen("libmod.so", RTLD_NOW); const char *libfn = "libmod.so";
const getmsg_t getmsg = (getmsg_t)dlsym(mod, "getmsg"); const char *symn = "getmsg";
const struct timespec pause = { .tv_nsec = 16666667 }; void *mod = NULL;
getmsg_t getmsg = NULL;
const struct timespec pause = { .tv_nsec = 100000000 };
while (1) { while (1) {
if (mod)
dlclose(mod);
mod = dlopen(libfn, RTLD_NOW);
if (!mod) {
fprintf(
stderr, "Error: couldn't open %s: %s\n", libfn, dlerror());
return 1;
}
getmsg = (getmsg_t)dlsym(mod, symn);
if (!getmsg) {
fprintf(
stderr, "Error: couldn't find symbol %s: %s\n", symn,
dlerror());
return 1;
}
printf("\33[2K\r%s", getmsg()); printf("\33[2K\r%s", getmsg());
fflush(stdout); fflush(stdout);
nanosleep(&pause, NULL); nanosleep(&pause, NULL);