]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/Daemon.pm
Add AbstractConfig base class
[pve-common.git] / src / PVE / Daemon.pm
index e051500431bf5a62c0817a3bbba0ea5c9af04fed..f81e589e3aca0433c25bf7a3e567fe9255463277 100644 (file)
@@ -34,6 +34,7 @@ use base qw(PVE::CLIHandler);
 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
 
 my $daemon_initialized = 0; # we only allow one instance
+my $daemon_sockets = [];
 
 my $close_daemon_lock = sub {
     my ($self) = @_;
@@ -500,6 +501,10 @@ sub restart_daemon {
 
     $ENV{RESTART_PVE_DAEMON} = 1;
 
+    foreach my $ds (@$daemon_sockets) {
+       $ds->fcntl(Fcntl::F_SETFD(), 0);
+    }
+
     if ($self->{max_workers}) {
        my @workers = keys %{$self->{workers}};
        push @workers, keys %{$self->{old_workers}};
@@ -578,6 +583,16 @@ my $read_pid = sub {
     return $pid;
 };
 
+# checks if the process was started by systemd
+my $init_ppid = sub {
+
+    if (getppid() == 1) {
+       return 1;
+    } else {
+       return 0;
+    }
+}; 
+
 sub running {
     my ($self) = @_;
 
@@ -654,7 +669,11 @@ sub register_start_command {
        code => sub {
            my ($param) = @_;
 
-           $self->start($param->{debug});
+            if (&$init_ppid()) {
+                $self->start($param->{debug});
+            } else {
+                PVE::Tools::run_command(['systemctl', 'start', $self->{name}]);
+            }
 
            return undef;
        }});  
@@ -700,7 +719,11 @@ sub register_restart_command {
        code => sub {
            my ($param) = @_;
 
-           &$reload_daemon($self, $use_hup);
+           if (&$init_ppid()) {
+               &$reload_daemon($self, $use_hup);
+           } else {
+               PVE::Tools::run_command(['systemctl', $use_hup ? 'reload-or-restart' : 'restart', $self->{name}]);
+           }
 
            return undef;
        }});               
@@ -750,7 +773,11 @@ sub register_stop_command {
        code => sub {
            my ($param) = @_;
            
-           $self->stop();
+           if (&$init_ppid()) {
+               $self->stop();
+           } else {
+               PVE::Tools::run_command(['systemctl', 'stop', $self->{name}]);
+           }
 
            return undef;
        }});               
@@ -801,6 +828,7 @@ sub create_reusable_socket {
        $socket->fdopen($sockfd, 'w') || 
            die "cannot fdopen file descriptor '$sockfd' - $!\n";
 
+       $socket->fcntl(Fcntl::F_SETFD(), Fcntl::FD_CLOEXEC);
     } else {
 
        $socket = IO::Socket::IP->new(
@@ -820,9 +848,8 @@ sub create_reusable_socket {
        $ENV{"PVE_DAEMON_SOCKET_$port"} = $socket->fileno;
     }
 
-    # remove FD_CLOEXEC bit to reuse on exec
-    $socket->fcntl(Fcntl::F_SETFD(), 0);
-    
+    push @$daemon_sockets, $socket;
+
     return $socket;
 }