Setting LD_LIBRARY_PATH for a forked process

Setting LD_LIBRARY_PATH for a forked process

am 09.01.2009 10:08:16 von Raymond Wan

Hi all,

I'm developing a web server which forks a process and then the child process goes off and does some processing and I do not want to wait for it to return. The child process runs a C++ program (i.e., not a Perl script). Thanks to replies here a while back, I got things working by performing a "fork" followed by a "system". So all was well...

Now, things have changed slightly and the C++ program needs to make use of shared libraries and right now, it cannot find them. Does anyone have any suggestions on how I would do that? Obviously, I need to update $LD_LIBRARY_PATH; but how would I do it? Currently, I'm doing something like this:

-----
use Env qw(LD_LIBRARY_PATH);

my $cmd = "...";
my @args = ("...");

$ENV{LD_LIBRARY_PATH} = ""; ## Or prepend to it

my $kid = fork;
if (!defined $kid) {
## Some error happened
}
elsif ($kid == 0) {
## Open/close filehandles, etc.
## Run the command
system ($cmd, @args);

CORE::exit (0);
}

## Parent process continues here...
-----

It is possible I'm doing something wrong, but so far, this isn't working. And if I replace the $cmd with a Perl script and try to print out $ENV{LD_LIBRARY_PATH}, there is nothing.

Am I close? I've googled a bit and I lost the page that I saw it (went to too many pages), but one person put $cmd and the updated environment variable into a bash script and then ran the bash script. Any other thoughts?

Thank you!

Ray

Re: Setting LD_LIBRARY_PATH for a forked process

am 09.01.2009 10:25:22 von torsten.foertsch

On Fri 09 Jan 2009, Raymond Wan wrote:
> It is possible I'm doing something wrong, but so far, this isn't
> working. =A0And if I replace the $cmd with a Perl script and try to
> print out $ENV{LD_LIBRARY_PATH}, there is nothing. =A0

I think you need this one:
http://search.cpan.org/~stas/Env-C-0.08/C.pm

Torsten

=2D-=20
Need professional mod_perl support?
Just hire me: torsten.foertsch@gmx.net

Re: Setting LD_LIBRARY_PATH for a forked process

am 09.01.2009 10:40:51 von Heiko Jansen

Am Freitag, den 09.01.2009, 10:25 +0100 schrieb Torsten Foertsch:
> On Fri 09 Jan 2009, Raymond Wan wrote:
> > It is possible I'm doing something wrong, but so far, this isn't
> > working. And if I replace the $cmd with a Perl script and try to
> > print out $ENV{LD_LIBRARY_PATH}, there is nothing.
>
> I think you need this one:
> http://search.cpan.org/~stas/Env-C-0.08/C.pm

Perhaps also possible: replace the program that gets executed with a
shell script that sets the env param and then does "exec realcmd $@".
Or even more simple: change the command string in your perl script to
something like 'LD_LIBRARY_PATH="..." realcmd'.
Of course that's far less elegant than using Env::C but I believe it
should work.

Heiko

Re: Setting LD_LIBRARY_PATH for a forked process

am 10.01.2009 10:06:40 von Raymond Wan

Hi Torsten and Heiko,


Heiko Jansen wrote:
> Am Freitag, den 09.01.2009, 10:25 +0100 schrieb Torsten Foertsch:
>> On Fri 09 Jan 2009, Raymond Wan wrote:
>>> It is possible I'm doing something wrong, but so far, this isn't
>>> working. And if I replace the $cmd with a Perl script and try to
>>> print out $ENV{LD_LIBRARY_PATH}, there is nothing.
>> I think you need this one:
>> http://search.cpan.org/~stas/Env-C-0.08/C.pm
>
> Perhaps also possible: replace the program that gets executed with a
> shell script that sets the env param and then does "exec realcmd $@".
> Or even more simple: change the command string in your perl script to
> something like 'LD_LIBRARY_PATH="..." realcmd'.
> Of course that's far less elegant than using Env::C but I believe it
> should work.


Thank you both for your replies! I thought I'd give Env::C.pm a try first and yes, worked like a dream! Thank you for giving me the alternatives, Heiko.

Is there an easy explanation why I needed Env::C? Part of the documentation for it says, "if these variables are set in Perl and the glue code doesn't worry to set them on the C level, these variables might not be seen by the C level". The way this is phrased makes it seem very generic. Does "C level" just mean anything that a Perl script calls? It could be a library in C or an entirely separate program; but basically environment variables that aren't being passed from the script to something else (that may or may not be related to C -- could be a java program).

Thanks both of you for your help!

Ray