[pve-devel] r5155 - pve-common/trunk

svn-commits at proxmox.com svn-commits at proxmox.com
Wed Sep 15 11:15:09 CEST 2010


Author: dietmar
Date: 2010-09-15 09:15:09 +0000 (Wed, 15 Sep 2010)
New Revision: 5155

Modified:
   pve-common/trunk/RPCEnvironment.pm
Log:
cleanups


Modified: pve-common/trunk/RPCEnvironment.pm
===================================================================
--- pve-common/trunk/RPCEnvironment.pm	2010-09-15 07:49:37 UTC (rev 5154)
+++ pve-common/trunk/RPCEnvironment.pm	2010-09-15 09:15:09 UTC (rev 5155)
@@ -2,9 +2,10 @@
 
 use strict;
 use warnings;
-use Posix;
+use POSIX ":sys_wait_h";
 use IO::File;
-
+use Fcntl qw(:flock);
+use PVE::SafeSyslog;
 use PVE::INotify;
 use PVE::ProcFSTools;
 
@@ -23,13 +24,16 @@
 my $WORKER_PIDS;
 
 my $worker_reaper = sub {
-    local $!; local $?;    
+    local $!; local $?;
+    syslog('info', "start reaper");
     foreach my $pid (keys %$WORKER_PIDS) {
         my $waitpid = waitpid ($pid, WNOHANG);
+	syslog('info', "test $pid $waitpid");  
         if (defined($waitpid) && ($waitpid == $pid)) {
             delete ($WORKER_PIDS->{$pid});
 	}
     }
+    syslog('info', "end reaper"); 
 };
 
 my $register_worker = sub {
@@ -55,7 +59,7 @@
 }
 
 sub init {
-    my ($class, $type) = @_;
+    my ($class, $type, %params) = @_;
 
     $class = ref($class) || $class;
 
@@ -77,6 +81,14 @@
 
     bless $self, $class;
 
+    foreach my $p (%params) {
+	if ($p eq 'atfork') {
+	    $self->{$p} = $params{$p};
+	} else {
+	    die "unknown option '$p'";
+	}
+    }
+
     $pve_env = $self;
 
     return $self;
@@ -127,7 +139,7 @@
 }
 
 sub get_remote_node_ip {
-    my ($self, $node) = @;
+    my ($self, $node) = @_;
 
     return undef if $node eq $self->{hostname};
 
@@ -147,20 +159,20 @@
 
     my $res;
 
-    # "UPID:$pid:$start:$type:$data"
-    if ($upid =~ m/^UPID:(\d+)(-(\d+))?:(\d+):([^:\s]+):(.*)$/) {
-	$res->{pid} = $1;
-	$res->{pstart} = $3 || 0;
-	$res->{starttime} = $4;
-	$res->{type} = $5;
-	$res->{data} = $6;
+    # "UPID:$node:$pid:$start:$type:$data"
+    if ($upid =~ m/^UPID:(\w+):(\d+)(-(\d+))?:(\d+):([^:\s]+):(.*)$/) {
+	$res->{node} = $1;
+	$res->{pid} = $2;
+	$res->{pstart} = $4 || 0;
+	$res->{starttime} = $5;
+	$res->{type} = $6;
+	$res->{data} = $7;
 
 	if ($res->{type} eq 'vmops') {
-	    if ($res->{data} =~ m/^([^:\s]+):(\d+):(\d+):(\S+)$/) {
+	    if ($res->{data} =~ m/^([^:\s]+):(\d+):(\S+)$/) {
 		$res->{command} = $1;
-		$res->{cid} = $2;
-		$res->{veid} = $3;
-		$res->{user} = $4;
+		$res->{veid} = $2;
+		$res->{user} = $3;
 
 		$res->{filename} = "/tmp/vmops-$res->{veid}.out";
 	    } else {
@@ -173,7 +185,7 @@
 		$res->{filename} = "/tmp/apldownload-$res->{user}.out";
 	    } else {
 		return undef;
-	    }		
+	    }	
 	}
     }
 
@@ -185,12 +197,12 @@
 
     my $d = $uip_hash; # shortcut
 
-    return "UPID:$d->{pid}-$d->{pstart}:$d->{starttime}:$d->{type}:$d->{data}";
+    return "UPID:$d->{node}:$d->{pid}-$d->{pstart}:$d->{starttime}:$d->{type}:$d->{data}";
 }
 
 # start long running workers
 # $data append to the returned uniquely identifier, which
-# has the following format: "UPID:$pid-$pstart:$startime:$dtype:$data"
+# has the following format: "UPID:$node:$pid-$pstart:$startime:$dtype:$data"
 # STDIN is redirected to /dev/null
 # STDOUT,STDERR are redirected to the filename returned by upid_decode
 # that file is locked wit flock to make sure only one process 
@@ -199,8 +211,6 @@
 sub fork_worker {
     my ($self, $dtype, $data, $function) = @_;
 
-    my $cpid;
-
     $dtype = 'unknown' if !defined ($dtype);
 
     $data = '' if !defined ($data);
@@ -209,8 +219,10 @@
 
     my @psync = POSIX::pipe();
 
+    my $node = $self->{hostname};
+
     # detect filename with faked PID
-    my $tmp = upid_decode ("UPID:0-0:0:$dtype:$data"); 
+    my $tmp = upid_decode ("UPID:$node:0-0:0:$dtype:$data"); 
     my $filename = $tmp->{filename};
 
     my $lockfh;
@@ -233,8 +245,14 @@
 	}
     }
 
-    if (($cpid = fork()) == 0) {
+    my $cpid = fork();
+    if (!defined($cpid)) {
+	undef $lockfh; # close
+	die "unable to fork worker - $!";
+    }
 
+    if ($cpid == 0) {
+
 	$SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub { die "received interrupt\n"; };
 
 	$SIG{CHLD} = $SIG{PIPE} = 'DEFAULT';
@@ -247,9 +265,9 @@
 
 	PVE::INotify::inotify_close();
 
-	# we close the socket 
-	my $httpd = $pve_config_daemon->{_daemon};
-	$httpd->close();
+	if (my $atfork = $self->{atfork}) {
+	    &$atfork();
+	}
 
 	# same algorythm as used inside SA
 
@@ -298,8 +316,8 @@
 	    die "unable to read process starttime";
 
 	my $upid = upid_encode ({
-	    pid => $$, pstart => $pstart, starttime => $starttime,
-	    type => $dtype, data => $data });
+	    node => $node, pid => $$, pstart => $pstart, 
+	    starttime => $starttime, type => $dtype, data => $data });
 
 	# sync with parent
 	POSIX::write ($psync[1], $upid, length ($upid));
@@ -317,19 +335,18 @@
     POSIX::read($psync[0], $upid, 4096);
     POSIX::close ($psync[0]);
 
-    if ($lockfh) {
-	undef $lockfh; # close
-    }
+    undef $lockfh; # close
 
+    &$register_worker($cpid);
+
     my $uh = upid_decode ($upid);
     if (!$uh || 
-	!($uh->{pid} == $cpid && $uh->{starttime} == $starttime &&
+	!($uh->{node} eq $node && $uh->{pid} == $cpid && 
+	  $uh->{starttime} == $starttime &&
 	  $uh->{type} eq $dtype && $uh->{data} eq $data)) {
-	syslog ('err', "got strange upid - $upid\n");
+	die "got strange worker upid '$upid'\n";
     }
 
-    &$register_worker($cpid);
-    
     return $upid;
 }
 



More information about the pve-devel mailing list