diff --git a/src/shim/shim.c b/src/shim/shim.c index a812e85..5c01469 100644 --- a/src/shim/shim.c +++ b/src/shim/shim.c @@ -12,6 +12,7 @@ #define _GNU_SOURCE #include +#include #include #include #include @@ -40,6 +41,29 @@ */ #define REDIRECT_PATH "/usr/\$LIB/liblsi-redirect.so" +/** + * Set up LD_PRELOAD, respecting an existing LD_PRELOAD and forcing ourselves + * to be first in the list. + */ +static void shim_set_ld_preload(void) +{ + const char *preload = NULL; + static char tgt[PATH_MAX] = { 0 }; + + preload = getenv("LD_PRELOAD"); + if (!preload) { + setenv("LD_PRELOAD", REDIRECT_PATH, 1); + return; + } + + if (snprintf(tgt, sizeof(tgt), "%s:%s", REDIRECT_PATH, preload) < 0) { + setenv("LD_PRELOAD", REDIRECT_PATH, 1); + return; + } + + setenv("LD_PRELOAD", tgt, 1); +} + int main(int argc, char **argv) { LsiConfig config = { 0 }; @@ -77,11 +101,9 @@ int main(int argc, char **argv) } #endif #ifdef HAVE_LIBREDIRECT - /* Only use libredirect in combination with native runtime! - * TODO: Respect existing preload libraries? - */ + /* Only use libredirect in combination with native runtime! */ if (config.use_libredirect) { - setenv("LD_PRELOAD", REDIRECT_PATH, 1); + shim_set_ld_preload(); } #endif } else {