[pve-devel] [PATCH 1/2] move trivial preparations into prepare()

Wolfgang Bumiller w.bumiller at proxmox.com
Fri May 29 13:44:49 CEST 2015


Moving some preparation code found at the top level of some binaries
into a prepare() sub.
---
 bin/pvedaemon  | 10 ++++++----
 bin/pveproxy   | 18 ++++++++++--------
 bin/pvestatd   | 18 +++++++++++-------
 bin/spiceproxy | 18 ++++++++++--------
 4 files changed, 37 insertions(+), 27 deletions(-)

diff --git a/bin/pvedaemon b/bin/pvedaemon
index 75fe20d..5440a0c 100755
--- a/bin/pvedaemon
+++ b/bin/pvedaemon
@@ -34,11 +34,13 @@ my %daemon_options = (
     leave_children_open_on_reload => 1,
 );
 
-# create dir for dtach sockets
-mkdir "/var/run/dtach";
-
 my $daemon = __PACKAGE__->new('pvedaemon', $cmdline, %daemon_options); 
 
+sub prepare {
+    # create dir for dtach sockets
+    mkdir "/var/run/dtach";
+}
+
 sub init {
     my ($self) = @_;
 
@@ -83,7 +85,7 @@ my $cmddef = {
 
 my $cmd = shift;
 
-PVE::CLIHandler::handle_cmd($cmddef, $0, $cmd, \@ARGV, undef, $0);
+PVE::CLIHandler::handle_cmd($cmddef, $0, $cmd, \@ARGV, undef, $0, \&prepare);
 
 exit (0);
 
diff --git a/bin/pveproxy b/bin/pveproxy
index 3864444..df7eb10 100755
--- a/bin/pveproxy
+++ b/bin/pveproxy
@@ -51,15 +51,17 @@ my %daemon_options = (
     pidfile => '/var/run/pveproxy/pveproxy.pid',
 );
 
-my $rundir="/var/run/pveproxy";
-if (mkdir($rundir, 0700)) { # only works at first start if we are root)
-    my $gid = getgrnam('www-data') || die "getgrnam failed - $!\n";
-    my $uid = getpwnam('www-data') || die "getpwnam failed - $!\n";
-    chown($uid, $gid, $rundir);
+my $daemon = __PACKAGE__->new('pveproxy', $cmdline, %daemon_options);
+
+sub prepare {
+    my $rundir="/var/run/pveproxy";
+    if (mkdir($rundir, 0700)) { # only works at first start if we are root)
+    	my $gid = getgrnam('www-data') || die "getgrnam failed - $!\n";
+    	my $uid = getpwnam('www-data') || die "getpwnam failed - $!\n";
+    	chown($uid, $gid, $rundir);
+    }
 }
 
-my $daemon = __PACKAGE__->new('pveproxy', $cmdline, %daemon_options); 
-
 sub add_dirs {
     my ($result_hash, $alias, $subdir) = @_;
 
@@ -160,7 +162,7 @@ my $cmddef = {
 
 my $cmd = shift;
 
-PVE::CLIHandler::handle_cmd($cmddef, $0, $cmd, \@ARGV, undef, $0);
+PVE::CLIHandler::handle_cmd($cmddef, $0, $cmd, \@ARGV, undef, $0, \&prepare);
 
 exit (0);
 
diff --git a/bin/pvestatd b/bin/pvestatd
index 3a4e8f2..c8b3b98 100755
--- a/bin/pvestatd
+++ b/bin/pvestatd
@@ -27,15 +27,19 @@ my $cmdline = [$0, @ARGV];
 my %daemon_options = (restart_on_error => 5, stop_wait_time => 5);
 
 my $daemon = __PACKAGE__->new('pvestatd', $cmdline, %daemon_options);
+my $nodename;
+my $restart_request;
 
-my $rpcenv = PVE::RPCEnvironment->init('cli');
+sub prepare {
+    my $rpcenv = PVE::RPCEnvironment->init('cli');
 
-$rpcenv->init_request();
-$rpcenv->set_language($ENV{LANG});
-$rpcenv->set_user('root at pam');
+    $rpcenv->init_request();
+    $rpcenv->set_language($ENV{LANG});
+    $rpcenv->set_user('root at pam');
 
-my $nodename = PVE::INotify::nodename();
-my $restart_request = 0;
+    $nodename = PVE::INotify::nodename();
+    $restart_request = 0;
+}
 
 sub init {
     my ($self) = @_;
@@ -354,7 +358,7 @@ my $cmddef = {
 
 my $cmd = shift;
 
-PVE::CLIHandler::handle_cmd($cmddef, $0, $cmd, \@ARGV, undef, $0);
+PVE::CLIHandler::handle_cmd($cmddef, $0, $cmd, \@ARGV, undef, $0, \&prepare);
 
 exit (0);
 
diff --git a/bin/spiceproxy b/bin/spiceproxy
index 07acf32..7065ebd 100755
--- a/bin/spiceproxy
+++ b/bin/spiceproxy
@@ -40,15 +40,17 @@ my %daemon_options = (
     pidfile => '/var/run/pveproxy/spiceproxy.pid',
     );
 
-my $rundir="/var/run/pveproxy";
-if (mkdir($rundir, 0700)) { # only works at first start if we are root)
-    my $gid = getgrnam('www-data') || die "getgrnam failed - $!\n";
-    my $uid = getpwnam('www-data') || die "getpwnam failed - $!\n";
-    chown($uid, $gid, $rundir);
-}
-
 my $daemon = __PACKAGE__->new('spiceproxy', $cmdline, %daemon_options); 
 
+sub prepare {
+    my $rundir="/var/run/pveproxy";
+    if (mkdir($rundir, 0700)) { # only works at first start if we are root)
+	my $gid = getgrnam('www-data') || die "getgrnam failed - $!\n";
+	my $uid = getpwnam('www-data') || die "getpwnam failed - $!\n";
+	chown($uid, $gid, $rundir);
+    }
+}
+
 sub init {
     my ($self) = @_;
 
@@ -101,7 +103,7 @@ my $cmddef = {
 
 my $cmd = shift;
 
-PVE::CLIHandler::handle_cmd($cmddef, $0, $cmd, \@ARGV, undef, $0);
+PVE::CLIHandler::handle_cmd($cmddef, $0, $cmd, \@ARGV, undef, $0, \&prepare);
 
 exit (0);
 
-- 
2.1.4




More information about the pve-devel mailing list