Close all file descriptors bigger than 2, that are all open files
from the parent except std{in,out,err}.
With this we can omit the lock_fh parameter which makes unifying the
TestHardware and RTHardwares sim_hardware_cmd easier.

As seeks positions of through forking dup'ed FDs from child and
parent are shared, this also ensures that none of them gets changed
by accident.

Signed-off-by: Thomas Lamprecht <t.lampre...@proxmox.com>
---

The used method could be up to discussion

 src/PVE/HA/Sim/RTEnv.pm      | 13 +++++++++++++
 src/PVE/HA/Sim/RTHardware.pm | 11 +++++------
 2 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/src/PVE/HA/Sim/RTEnv.pm b/src/PVE/HA/Sim/RTEnv.pm
index af2b46d..8f41dd4 100644
--- a/src/PVE/HA/Sim/RTEnv.pm
+++ b/src/PVE/HA/Sim/RTEnv.pm
@@ -71,4 +71,17 @@ sub loop_end_hook {
     die "loop take too long ($delay seconds)\n" if $delay > 30;
 }
 
+sub after_fork {
+    my ($self) = @_;
+
+    # close all fds other than std*
+    for my $path (glob "/proc/self/fd/*") {
+       if ($path =~ m!/(\d)+$!) {
+           POSIX::close($1) if $1 > 2;
+       }
+    }
+    # nothing to clean up in the simulation environment
+}
+
+
 1;
diff --git a/src/PVE/HA/Sim/RTHardware.pm b/src/PVE/HA/Sim/RTHardware.pm
index 145d73b..ffcf593 100644
--- a/src/PVE/HA/Sim/RTHardware.pm
+++ b/src/PVE/HA/Sim/RTHardware.pm
@@ -81,7 +81,7 @@ sub read_manager_status {
 }
 
 sub fork_daemon {
-    my ($self, $lockfh, $type, $haenv) = @_;
+    my ($self, $type, $haenv) = @_;
 
     my @psync = POSIX::pipe();
 
@@ -90,8 +90,8 @@ sub fork_daemon {
 
     if ($pid == 0) { 
 
-       close($lockfh) if defined($lockfh); # unlock global lock
-       
+       $haenv->after_fork();
+
        POSIX::close($psync[0]);
 
        my $outfh = $psync[1];
@@ -189,7 +189,6 @@ sub sim_hardware_cmd {
 
     # note: do not fork when we own the lock!
     my $code = sub {
-       my ($lockfh) = @_;
 
        $cstatus = $self->read_hardware_status_nolock();
 
@@ -206,8 +205,8 @@ sub sim_hardware_cmd {
        if ($cmd eq 'power') {
            if ($cstatus->{$node}->{power} ne $action) {
                if ($action eq 'on') {
-                   $d->{crm} = $self->fork_daemon($lockfh, 'crm', 
$d->{crm_env}) if !$d->{crm};
-                   $d->{lrm} = $self->fork_daemon($lockfh, 'lrm', 
$d->{lrm_env}) if !$d->{lrm};
+                   $d->{crm} = $self->fork_daemon('crm', $d->{crm_env}) if 
!$d->{crm};
+                   $d->{lrm} = $self->fork_daemon('lrm', $d->{lrm_env}) if 
!$d->{lrm};
                } else {
                    if ($d->{crm}) {
                        $self->log('info', "crm on node '$node' killed by 
poweroff");
-- 
2.1.4


_______________________________________________
pve-devel mailing list
pve-devel@pve.proxmox.com
http://pve.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

Reply via email to