From 3d26864477b5f36b71e5fd38fd62776e94febe2b Mon Sep 17 00:00:00 2001 From: Ole Tange Date: Sat, 23 Mar 2013 14:33:12 +0100 Subject: [PATCH] src/parallel: family_pids were incompatible with FreeBSD 7 and CentOS 3. --- src/parallel | 56 +++++++++++++++++++++------------------------------- 1 file changed, 22 insertions(+), 34 deletions(-) diff --git a/src/parallel b/src/parallel index 583c36cf..34049aec 100755 --- a/src/parallel +++ b/src/parallel @@ -3519,42 +3519,30 @@ sub family_pids { # TODO test this on different OS as 'ps' is known to be different my $self = shift; my $pid = $self->pid(); - my $script; - if ($^O eq 'linux') { - $script = q{ - family_pids() { - for CHILDPID in `ps --ppid "$@" -o pid --no-headers`; do - family_pids $CHILDPID & - done - echo "$@" - } - } . - "family_pids $pid; wait"; - } elsif ($^O eq 'solaris') { - $script = q{ - family_pids() { - if [ -z "$1" ] ; then return ; fi - family_pids `pgrep -P "$*"` & - for CHILDPID in "$@"; do - echo $CHILDPID - done - } - } . - "family_pids $pid; wait"; + my (@pidtable,%children_of,@pids); + if ($^O eq 'linux' or $^O eq 'solaris') { + # Table with pid parentpid (SysV + GNU) + @pidtable = `ps -ef | awk '{print \$2" "\$3}'`; } else { - # This should cover all System V-derived flavors of 'ps' - $script = q{ - family_pids() { - for CHILDPID in `ps -f | awk '$3 == '"$@"' {print $2}'`; do - family_pids $CHILDPID & - done - echo "$@" - } - } . - "family_pids $pid; wait"; + # Table with pid parentpid (BSD) + @pidtable = `ps -o pid,ppid -ax`; } - my @pids = qx{$script}; - chomp(@pids); + for (@pidtable) { + /(\S+)\s+(\S+)/ or ::die_bug("pidtable format"); + push(@{$children_of{$2}},$1); + } + my @more = ($pid); + while(@more) { + my @m; + push @pids,@more; + for my $parent (@more) { + if($children_of{$parent}) { + push @m, @{$children_of{$parent}}; + } + } + @more = @m; + } + return (@pids); }