Does anyone know how to return errors to the parent process after you've fork()ed, when your exec() fails?
I'm doing this in the clamd tests:
my $pid = fork();
die "Fork failed" unless defined $pid;
if (!$pid) {
exec($clamd);
}
...
But if clamd fails to start for some reason, I don't know how to find out reliably. exec() of course will return an error, but I don't know how to propogate that to the parent.
Exec failure (Score:2)
What do you mean by "exec() fails"? From perldoc -f exec
Under what conditions do you expect the call to fail? Unless the docs are wrong, if the program you are trying to execute exists, you will never get a false value from exec. Perhaps system() would be a better choice, and you can handle terminating the child process manually?
Re:Exec failure (Score:2)
i.e. how can I communicate back to the parent this error condition?
Re:Exec failure (Score:2)
Non-zero exit code from the child. Your wait call's return value can be used to make sure that you get the expected child's death. You can use a time-out, since "success" probably means that the child never effectively exits (or if it does, the time-out is probably not needed). When wait returns the PID of the deceased child, $? will have the status/exit code.
--rjray
Would this work? (Score:2)
Maybe you can use some variant of the following? We use Windows boxes at work (damn it!), so I rarely get a chance to do much nifty stuff with forking. As a result, I could totally be smoking crack with this.
#!/usr/bin/perl -w
use strict;
#!/usr/bin/perl -w
use strict;
use IO::Handle;
pipe( READER, WRITER );
WRITER->autoflush(1);
# Change the following line to a non-existent program to test
my $program = '/usr/bin/echo';
my $pid = fork;
die unless defined $pid;
unless ($pid) {
Re:Would this work? (Score:2)
Plus if you set the pipe to non-blocking reads, then there's timing issues. How long do you wait for something to come out of the pipe?
exit (Score:1)
just exit(1), and make sure the parent calls wait() at some point (or polls with waitpid(WNOHANG)), and checks the exit status.
How IPC::Run does it (Score:1)
pipe()to sync to the child,fork()ing, and marking it as close on exit with afcntl(). Here are some snippets glued together from pieces of IPC::Run:Re:How IPC::Run does it (Score:1)
- Barrie