Subject: | StartApp() should return $PID; fix process control |
Hello,
StartApp() uses rather poor method for application startup (system() +
background shell syntax). There's a few problems with this approach,
namely: 1) unknown PID of new process (because shell exits); 2) shell
exits and the application is reparented to the "init" process (PID=1).
This basically breaks the possibility of find-grained process control.
The following patch implements (hopefully) more reasonable approach:
1) use fork+exec instead of system() function (which uses fork+exec
pretty much the same way, but also waits for the child to finish);
2) check (in parent process) that exec didn't fail; if exec fails,
it fails quickly, so 1s seems to be enough;
3) return PID instead of simply ture value (PID is then available
for kill, waitpid et al).
--- X11-GUITest-0.20/GUITest.pm- 2004-02-14 15:43:00 +0000
+++ X11-GUITest-0.20/GUITest.pm 2005-07-13 03:04:19 +0000
@@ -174,7 +174,7 @@ require DynaLoader;
Exporter::export_ok_tags(keys %EXPORT_TAGS);
-$VERSION = '0.20';
+$VERSION = '0.201';
# Module Constants
sub DEF_WAIT() { 10; }
@@ -533,19 +533,20 @@ zero is returned on failure, non-zero fo
=cut
sub StartApp {
- my $cmdline = shift;
-
- # Add ampersand if not present to detach program from shell, allowing
- # this function to return before application is finished running.
- # RegExp: [&][zero or more whitespace][anchor, nothing to follow whitespace]
- if ($cmdline !~ /\&\s*$/) {
- $cmdline .= ' &';
+ my @cmd = @_;
+ my $pid = fork;
+ if ($pid) {
+ use POSIX qw(WNOHANG);
+ sleep 1;
+ waitpid($pid, WNOHANG) != $pid
+ and kill(0, $pid) == 1
+ and return $pid;
+ }
+ elsif (defined $pid) {
+ use POSIX qw(_exit);
+ exec @cmd or _exit(1);
}
- local $! = 0;
- system($cmdline);
-
- # Limited to catching specific problems due to detachment from shell
- return( (length($!) == 0) );
+ return;
}
End of patch
Note that POSIX::_exit should be used if exec fails,
in order not to trigger END{} blocks.
--
Alexey Tourbin
ALT Linux Team