[pve-devel] [PATCH ha-manager 08/10] use HA environments after_fork to close all inherited FDs

Thomas Lamprecht t.lamprecht at proxmox.com
Wed Dec 21 16:44:45 CET 2016


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 any of them gets changed by
mistake.

Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---
 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 1835bee..6c943a6 100644
--- a/src/PVE/HA/Sim/RTHardware.pm
+++ b/src/PVE/HA/Sim/RTHardware.pm
@@ -82,7 +82,7 @@ sub read_manager_status {
 }
 
 sub fork_daemon {
-    my ($self, $lockfh, $type, $haenv) = @_;
+    my ($self, $type, $haenv) = @_;
 
     my @psync = POSIX::pipe();
 
@@ -91,8 +91,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];
@@ -193,7 +193,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();
 
@@ -210,8 +209,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





More information about the pve-devel mailing list