Skip to content

Commit 260ef5e

Browse files
authored
add options to leave LD_PRELOAD and LD_LIBRARY_PATH unchanged (#160)
1 parent d054e6d commit 260ef5e

File tree

1 file changed

+61
-34
lines changed

1 file changed

+61
-34
lines changed

cliloader/cliloader.cpp

Lines changed: 61 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ static bool getEnvVars(
150150

151151
#define SETENV( _name, _value ) setenv( _name, _value, 1 );
152152

153+
bool set_LD_LIBRARY_PATH = true;
154+
bool set_LD_PRELOAD = true;
155+
153156
#endif
154157

155158
bool debug = false;
@@ -243,6 +246,16 @@ static bool parseArguments(int argc, char *argv[])
243246
{
244247
debug = true;
245248
}
249+
#if !defined(_WIN32)
250+
else if( !strcmp(argv[i], "--no-LD_PRELOAD") )
251+
{
252+
set_LD_PRELOAD = false;
253+
}
254+
else if( !strcmp(argv[i], "--no-LD_LIBRARY_PATH") )
255+
{
256+
set_LD_LIBRARY_PATH = false;
257+
}
258+
#endif
246259
else if( !strcmp(argv[i], "-q") || !strcmp(argv[i], "--quiet") )
247260
{
248261
SETENV("CLI_SuppressLogging", "1");
@@ -350,6 +363,11 @@ static bool parseArguments(int argc, char *argv[])
350363
"\n"
351364
"Options:\n"
352365
" --debug Enable cliloader Debug Messages\n"
366+
#if !defined(_WIN32)
367+
" --no-LD_PRELOAD Do not set LD_PRELOAD\n"
368+
" --no-LD_LIBRARY_PATH Do not set LD_LIBRARY_PATH\n"
369+
#endif
370+
"\n"
353371
" --quiet [-q] Disable Logging\n"
354372
" --call-logging [-c] Trace Host API Calls\n"
355373
" --dump-source [-dsrc] Dump Input Program Source\n"
@@ -562,42 +580,51 @@ int main(int argc, char *argv[])
562580

563581
#else // not Windows
564582

565-
std::string ld_preload;
566-
std::string ld_library_path;
567-
568-
// Look for the CLIntercept shared library.
569-
// First, check the current directory.
570-
bool found = getEnvVars( path, ld_preload, ld_library_path );
571-
if( found == false )
583+
if( set_LD_PRELOAD || set_LD_LIBRARY_PATH )
572584
{
573-
// Next, check the parent directory.
574-
std::string libPath = path + "/..";
575-
found = getEnvVars( libPath, ld_preload, ld_library_path );
576-
}
577-
if( found == false )
578-
{
579-
// Next, check a lib directory.
580-
std::string libPath = path + "/../" + CLILOADER_LIB_DIR;
581-
found = getEnvVars( libPath, ld_preload, ld_library_path );
582-
}
583-
if( found == false )
584-
{
585-
// Next, check for an intercept directory.
586-
// This is for running cliloader straight from a CMake directory.
587-
std::string libPath = path + "/../intercept";
588-
found = getEnvVars( libPath, ld_preload, ld_library_path );
589-
}
590-
if( found )
591-
{
592-
DEBUG("New %s is %s\n", LD_PRELOAD_ENV, ld_preload.c_str());
593-
DEBUG("New %s is %s\n", LD_LIBRARY_PATH_ENV, ld_library_path.c_str());
585+
std::string ld_preload;
586+
std::string ld_library_path;
594587

595-
SETENV(LD_PRELOAD_ENV, ld_preload.c_str());
596-
SETENV(LD_LIBRARY_PATH_ENV, ld_library_path.c_str());
597-
}
598-
else
599-
{
600-
DEBUG("Couldn't find CLIntercept shared library!\n");
588+
// Look for the CLIntercept shared library.
589+
// First, check the current directory.
590+
bool found = getEnvVars( path, ld_preload, ld_library_path );
591+
if( found == false )
592+
{
593+
// Next, check the parent directory.
594+
std::string libPath = path + "/..";
595+
found = getEnvVars( libPath, ld_preload, ld_library_path );
596+
}
597+
if( found == false )
598+
{
599+
// Next, check a lib directory.
600+
std::string libPath = path + "/../" + CLILOADER_LIB_DIR;
601+
found = getEnvVars( libPath, ld_preload, ld_library_path );
602+
}
603+
if( found == false )
604+
{
605+
// Next, check for an intercept directory.
606+
// This is for running cliloader straight from a CMake directory.
607+
std::string libPath = path + "/../intercept";
608+
found = getEnvVars( libPath, ld_preload, ld_library_path );
609+
}
610+
if( found )
611+
{
612+
if( set_LD_PRELOAD )
613+
{
614+
DEBUG("New %s is %s\n", LD_PRELOAD_ENV, ld_preload.c_str());
615+
SETENV(LD_PRELOAD_ENV, ld_preload.c_str());
616+
}
617+
618+
if( set_LD_LIBRARY_PATH )
619+
{
620+
DEBUG("New %s is %s\n", LD_LIBRARY_PATH_ENV, ld_library_path.c_str());
621+
SETENV(LD_LIBRARY_PATH_ENV, ld_library_path.c_str());
622+
}
623+
}
624+
else
625+
{
626+
DEBUG("Couldn't find CLIntercept shared library!\n");
627+
}
601628
}
602629

603630
#ifdef __APPLE__

0 commit comments

Comments
 (0)