From 6e6b529dd7c8964dd795a843250ad6d889b029e2 Mon Sep 17 00:00:00 2001 From: johannes hanika Date: Mon, 24 Apr 2023 10:56:09 +0200 Subject: [PATCH] libvkdt: probe basedir --- Makefile | 1 + src/core/fs.h | 40 ++++++++++++++++++---------------------- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/Makefile b/Makefile index 5c380d5eb..5edac36e9 100644 --- a/Makefile +++ b/Makefile @@ -21,6 +21,7 @@ VKDTDIR?=$(DESTDIR)$(prefix)/lib/vkdt VKDTLIBDIR?=$(DESTDIR)$(prefix)/lib VKDTINCDIR?=$(DESTDIR)$(prefix)/include/vkdt install-bin: all Makefile + mkdir -p $(VKDTDIR)/lib mkdir -p $(DESTDIR)$(prefix)/bin ln -rsf ${VKDTDIR}/vkdt $(DESTDIR)$(prefix)/bin/vkdt ln -rsf ${VKDTDIR}/vkdt-cli $(DESTDIR)$(prefix)/bin/vkdt-cli diff --git a/src/core/fs.h b/src/core/fs.h index 035d4ad50..a5fccbc40 100644 --- a/src/core/fs.h +++ b/src/core/fs.h @@ -1,10 +1,6 @@ #pragma once -#ifdef __linux__ - #ifdef LIBVKDT - #include - #include - #endif -#endif +#include +#include #include #include #include @@ -84,39 +80,39 @@ fs_homedir( snprintf(basedir, maxlen, "%s/.config/vkdt", getenv("HOME")); } -static inline int // returns the directory where the actual binary (not the symlink) resides +static inline void // returns the directory where the actual binary (not the symlink) resides fs_basedir( char *basedir, // output will be copied here size_t maxlen) // allocation size { + basedir[0] = 0; #ifdef __linux__ -#ifdef LIBVKDT - void *handle; - char mod[PATH_MAX]; - handle = dlopen("libvkdt.so", RTLD_LAZY); - if (!handle) { - fprintf(stderr, "%s\n", dlerror()); - return(1); - } - dlinfo(handle, RTLD_DI_ORIGIN, &mod); - snprintf(basedir, maxlen, "%s/", mod); - dlclose(handle); -#else // stupid allocation dance because passing basedir directly // may or may not require PATH_MAX bytes instead of maxlen char *bd = realpath("/proc/self/exe", 0); snprintf(basedir, maxlen, "%s", bd); free(bd); - #endif + fs_dirname(basedir); + char mod[PATH_MAX]; + snprintf(mod, sizeof(mod), "%s/darkroom.ui", basedir); + if(access(mod, F_OK)) + { // no darkroom.ui file and probably also lacking the rest. try dlopen/dso path: + void *handle = dlopen("libvkdt.so", RTLD_LAZY); + if(handle) + { + dlinfo(handle, RTLD_DI_ORIGIN, &mod); + snprintf(basedir, maxlen, "%s/vkdt", mod); + dlclose(handle); + } + } #elif defined(__FreeBSD__) int mib_procpath[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 }; size_t len_procpath = maxlen; sysctl(mib_procpath, 4, basedir, &len_procpath, NULL, 0); + fs_dirname(basedir); #else #error port me #endif - fs_dirname(basedir); - return(0); } static inline int // return the number of devices found