Skip to contentSkip to conversation

setenv and dlopen

DISCUSSION Henry Margies 3 participants 4 messages Oct 25, 2005 – Oct 26, 2005

lore
Share & tools
lore
#0 lore raw

Hi all,

I have a problem using dlopen and setting the LD_LIBRARY_PATH with putenv or setenv.

Here is my test code:

#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <unistd.h>

int main(int argn, char** argv)

{
        void* handle;
        int   ret;

        putenv("LD_LIBRARY_PATH=.");
        putenv("PATH=.");

        handle = dlopen("libmylib.so", RTLD_LAZY);

        if (NULL == handle) {
                printf("error: %s\n", dlerror());
        }

        if (2 == argn)
                return 0;

        ret = execlp("dltest", "dltest", "break", NULL);

        if (ret)
                perror("Error in starting dltest: ");
	return ret;
}

I also tried setenv and absolute path-names for LD_LIBRARY_PATH. Setting LD_LIBRARY_PATH on the console works perfectly. And as you can see, setting the PATH variable using putenv is also working for execlp.

Any suggestions?

Henry

Signature · 2 lines
Hi! I'm a .signature virus! Copy me into your
~/.signature to help me spread!
#1 lore raw
Quoted 1 line · Henry Margies

Hi all,

Hi Henry,

Quoted 2 lines

I have a problem using dlopen and setting the LD_LIBRARY_PATH with putenv or setenv.

I think this is because putenv does not really modify your process'es environment (which is not possible afaik), but rather remembers the given variable until you call some exec* function. libc will then pass the new environment to execve so that the new program will have the modified environment.

Regards,
Christoph

Signature · 4 lines
``There's no dark side of the moon, really
Matter of fact, it's all dark''

--Pink Floyd
#2 lore raw

Hi Christoph,

thank you for you answer.

Quoted 5 lines

I think this is because putenv does not really modify your process'es environment (which is not possible afaik), but rather remembers the given variable until you call some exec* function. libc will then pass the new environment to execve so that the new program will have the modified environment.

Well, depends on the point of view. For the user-space processes side the environment values are all stored in char **environ and I can remove, add and change values. But you are right, the kernel also holds the original values I cannot change (which are shown
in /proc/PID/environ).

So it seems like execlp uses the local values from char **environ (because it reads my PATH value) as dlopen uses the kernel version of the environment variables (and do not know about my changes to LD_LIBARAY_PATH).

Maybe dlopen runs far beyond my local processes scope and has no access to my local environment or it just don't care about :-)

Any solutions to tell dlopen within my c program to search also in my paths?

Greetings,

Henry

Signature · 9 lines
Hi! I'm a .signature virus! Copy me into your
~/.signature to help me spread!




-- 
Hi! I'm a .signature virus! Copy me into your
~/.signature to help me spread!
#3 lore raw
In reply to Henry Margies
Quoted 2 lines · Henry Margies

I have a problem using dlopen and setting the LD_LIBRARY_PATH with putenv or setenv.

ld-linux.so.2 reads the value into a variable at startup, and uses the stored value thereafter.

Signature · 1 line
Glynn Clements <[email protected]>