0

Currently, I have an application that does:

execlp(java_exec, java_args, etc, etc);

to launch a jar file.

Is there a way to also have it prepend LD_PRELOAD="mylib.so" ?

I couldn't find a way to tell execlp to do: LD_PRELOAD="mylib.so" java -jar foo.jar

Any ideas how I can achieve this in C or C++ on Linux?

Brandon
  • 22,723
  • 11
  • 93
  • 186

3 Answers3

2

You should probably add LD_PRELOAD=mylib.so to *envp, execlpe()'s fourth arg.

alamar
  • 18,729
  • 4
  • 64
  • 97
0

This command can be used to append the user .so in LD_PRELOAD variable set LD_PRELOAD=$LD_PRELOAD;"mylib.so" System command can be used to set this from C file

Raghuram
  • 3,937
  • 2
  • 19
  • 25
mahendiran.b
  • 1,315
  • 7
  • 13
  • 1
    Just a note: Moving from using `exec*()` to `system()` is probably not a good idea, as there are good reasons to use exec instead of system in a program. – kratenko Aug 01 '14 at 09:40
0

In most cases, you could use putenv(3) (perhaps with snprintf(3) or asprintf(3) ...) before execlp(3). for example:

char buf[64];
memset (buf, 0, sizeof(buf));
snprintf (buf, sizeof(buf), "LD_PRELOAD=%s", yourlibso);
putenv(buf);

and after that your execlp....

However, both putenv and snprintf could fail (or have weird behavior, if yourlibso happens to contain a long string).

You might test before that yourlibso is good enough with access(2) or stat(2).

Handling every failure is difficult.

See syscalls(2) and errno(3).

Things are much simpler when yourlibso is known at compile-time. See a draft C standard such as n2573, or a draft C++ standard such as n3337.

Any ideas how I can achieve this in C or C++ on Linux?

remember that C and C++ are different programming languages.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547