Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: exec, echo and pipe

by andal (Hermit)
on Apr 01, 2016 at 07:20 UTC ( #1159251=note: print w/replies, xml ) Need Help??

in reply to exec, echo and pipe

I might be wrong, but using "exec" directly in CGI script is not very good idea. The "exec" simply replaces current binary with new binary, it does not terminate the process, so the Web server shall wait for this new binary to finish its work, and that might be undesirable.

The appropriate way would be to do fork then exit the parent and do exec in the child. Ideally, the child should also call POSIX::setsid, to detach itself away from Web server, otherwise it could be killed by the server.

Forking also makes possible to pass your password over the pipe. Something like the following could do the trick

my $chld = fork(); # I wan't the CGI to end quickly die "Can't fork: $!\n" if(!definded $chld); exit(0) if $chld; # CGI ends here POSIX::setsid(); # start new session $chld = open(CHLD, "|-"); # this does another fork internally die "Can't fork: $!\n" if(!definded $chld); if($chld == 0) { # this does the same as ">/dev/null 2>&1" in shell close(STDOUT); open(STDOUT, ">/dev/null"); close(STDERR); open(STDERR, ">&STDOUT"); # avoid parsing of the arguments by shell, especially if $Trigger_ +Job # or $Captured_User_Name contain some dangerous stuff exec './', '-j', $Trigger_Job, '-u', $Captured_User_Na +me; } select(CHLD); # make CHLD my default output $| = 1; # enable autoflushing print $Captured_Password; # output captured password close(CHLD); # wait for child to finish it's work

The code above is not tested. Read more about processes groups and sessions to understand why the switch of session might be good.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1159251]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2022-11-28 18:24 GMT
Find Nodes?
    Voting Booth?