in reply to system() call failure

here's the bare bone of my code which forks the child process. I also added some debug messages.

===

sub fork_job { my $job = shift; my $system_fail = 0; system( "ls . " ) == 0 || print "system failed before forking\n" my $pid = fork(); if( !defined( $pid ) ) { # bad, bad, error return 0; } elsif ($pid != 0) { # parent return $pid; } elsif ($pid == 0) { # $child system( "ls . " ) == 0 || print "system failed after forking(1)\n"; $> = $job->{'uid'}; $< = $job->{'uid'}; system( "ls . " ) == 0 || print "system failed after forking(2)\n"; setpgrp(0, $$); system( "ls . " ) == 0 || print "system failed after forking(3)\n"; ## doesn't exec, but continues on my $runner = Runner->new( $job ); $runner->run(); exit 1; } }

===

It seems that any system() call fails after setpgrp( 0, $$ ). Is setpgrp() an issue on solaris?

Replies are listed 'Best First'.
Re: Re: system() call failure
by lestrrat (Deacon) on May 15, 2001 at 03:11 UTC

    My bad, the above was completely inacurate. I started doing some searching around to the cause of the annoying error, and I reduced it down to this:

    #!/usr/local/bin/perl $SIG{ 'CHLD' } = sub { my $pid = wait(); print "Got $pid\n"; }; system("/bin/ls > /dev/null") == 0 || print "system exited with $?, error is \"$!\"\n";

    If you take out the signal handler, it works. Now I'm completely lost...

    By the way, this is the uname -a output:

    SunOS xxxxxx 5.7 Generic sun4u sparc SUNW,Ultra-5_10

    ANY ideas??

      system() does a wait() for the /bin/ls process to complete. When the process ends, your handler jumps in and grabs it, and system() gets nothing, which isn't what it wanted. If you run a command that has a side effect, you should see that it actually does get executed.

      It's interesting that it worked on Linux but not Solaris. Must be some difference in the way they deliver signals.

      Best solution is probably not to mix $SIG{CHLD} with system(). There are a couple of threads around here that might be useful... try a search or two.

        I ended up overloading the system() function call with my version of it. The truth is *I* don't need system(), but other people using my framework do. So I need account for this

        anyway, all I did was to make sure that the $SIG{'CHLD'} gets masked by local $SIG{'CHLD'} = <em>blah</em>, and the rest is just your typical fork()/exec()

        Boy, that was annoying! Thanks to both clintp and no_slogan for your help :-)

        just letting you guys know what happened... So it turns out yes, you must be correct: when system() forks, for some reason the $SIG{ 'CHLD' } that I defined gets called.

        I don't really need to call system(), but others who use my code do, so I decided on just overloading the system() function call with a version that uses a local $SIG{ 'CHLD' }

        It works now :-)

        Thanks for your help..