]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/Daemon.pm
bump version to 8.2.1
[pve-common.git] / src / PVE / Daemon.pm
index d438d27012effdc546c839b927eeeefee116818d..63fd5eed29195d8f07f8400e2a066481347df448 100644 (file)
@@ -3,7 +3,7 @@ package PVE::Daemon;
 # Abstract class to implement Daemons
 #
 # Features:
-# * lock and write PID file /var/run/$name.pid to make sure onyl
+# * lock and write PID file /var/run/$name.pid to make sure only
 #   one instance is running.
 # * keep lock open during restart
 # * correctly daemonize (redirect STDIN/STDOUT)
@@ -13,7 +13,7 @@ package PVE::Daemon;
 # * allow to restart while workers are still runningl
 #   (option 'leave_children_open_on_reload')
 # * run as different user using setuid/setgid
+
 use strict;
 use warnings;
 use English;
@@ -64,7 +64,7 @@ sub after_fork_cleanup {
 
     for my $sig (qw(CHLD HUP INT TERM QUIT)) {
        $SIG{$sig} = 'DEFAULT'; # restore default handler
-       # AnyEvent signals only works if $SIG{XX} is 
+       # AnyEvent signals only works if $SIG{XX} is
        # undefined (perl event loop)
        delete $SIG{$sig}; # so that we can handle events with AnyEvent
     }
@@ -80,7 +80,7 @@ my $lockpidfile = sub {
     if (my $fd = $self->{env_pve_lock_fd}) {
 
        $self->{daemon_lock_fh} = IO::Handle->new_from_fd($fd, "a");
-       
+
     } else {
 
        $waittime = 5;
@@ -102,9 +102,9 @@ my $lockpidfile = sub {
 
        my ($running, $pid) = $self->running();
        if ($running) {
-           die "can't aquire lock '$lkfn' - daemon already started (pid = $pid)\n";
+           die "can't acquire lock '$lkfn' - daemon already started (pid = $pid)\n";
        } else {
-           die "can't aquire lock '$lkfn' - $err\n";
+           die "can't acquire lock '$lkfn' - $err\n";
        }
     }
 };
@@ -114,10 +114,10 @@ my $writepidfile = sub {
 
     my $pidfile = $self->{pidfile};
 
-    die "can't open pid file '$pidfile' - $!\n" if !open (PIDFH, ">$pidfile");
+    open (my $PID_FH, '>', "$pidfile") or die "can't open pid file '$pidfile' - $!\n";
 
-    print PIDFH "$$\n";
-    close (PIDFH);
+    print $PID_FH "$$\n";
+    close ($PID_FH);
 };
 
 my $server_cleanup = sub {
@@ -146,11 +146,7 @@ my $start_workers = sub {
 
     return if $self->{terminate};
 
-    my $count = 0;
-    foreach my $cpid (keys %{$self->{workers}}) {
-       $count++;
-    }
-
+    my $count = scalar keys %{$self->{workers}};
     my $need = $self->{max_workers} - $count;
 
     return if $need <= 0;
@@ -196,21 +192,16 @@ my $terminate_server = sub {
 
     $self->{terminate} = 1; # set flag to avoid worker restart
 
-    if (!$self->{max_workers}) {
-       eval { $self->shutdown(); };
-       warn $@ if $@;
-       return;
-    }
-
     eval { $self->shutdown(); };
     warn $@ if $@;
 
+    return if !$self->{max_workers}; # if we have no workers we're done here
 
     # if configured, leave children running on HUP
     return if $allow_open_children && $self->{leave_children_open_on_reload};
 
     # else send TERM to all (old and current) child workers
-    kill 15, keys %{$self->@{'workers','old_workers'}};
+    kill 15, (keys %{$self->{workers}}, keys %{$self->{old_workers}});
 
     # nicely shutdown childs (give them max 10 seconds to shut down)
     my $previous_alarm = alarm(10);
@@ -252,8 +243,7 @@ sub setup {
 
     initlog($self->{name});
 
-    my $restart = $ENV{RESTART_PVE_DAEMON};
-    delete $ENV{RESTART_PVE_DAEMON};
+    my $restart = delete $ENV{RESTART_PVE_DAEMON};
     $self->{env_restart_pve_daemon} = $restart;
 
     my $lockfd = $ENV{PVE_DAEMON_LOCK_FD};
@@ -290,6 +280,7 @@ sub setup {
     if ($restart && $self->{max_workers}) {
        if (my $wpids = $ENV{PVE_DAEMON_WORKER_PIDS}) {
            foreach my $pid (split(':', $wpids)) {
+               # check & untaint
                if ($pid =~ m/^(\d+)$/) {
                    $self->{old_workers}->{$1} = 1;
                }
@@ -319,8 +310,8 @@ my $server_run = sub {
     $self->init();
 
     if (!$debug) {
-       open STDIN,  '</dev/null' || die "can't read /dev/null";
-       open STDOUT, '>/dev/null' || die "can't write /dev/null";
+       open STDIN,  '<', '/dev/null' or die "can't read /dev/null - $!";
+       open STDOUT, '>', '/dev/null' or die "can't write /dev/null - $!";
     }
 
     if (!$self->{env_restart_pve_daemon} && !$debug) {
@@ -341,7 +332,7 @@ my $server_run = sub {
        syslog('info' , "starting server");
     }
 
-    POSIX::setsid(); 
+    POSIX::setsid();
 
     open STDERR, '>&STDOUT' || die "can't close STDERR\n";
 
@@ -385,7 +376,7 @@ my $server_run = sub {
        }
     };
 
-    eval { 
+    eval {
        if ($self->{max_workers}) {
            my $old_sig_chld = $SIG{CHLD};
            local $SIG{CHLD} = sub {
@@ -395,7 +386,7 @@ my $server_run = sub {
            };
 
            # now loop forever (until we receive terminate signal)
-           for (;;) { 
+           for (;;) {
                &$start_workers($self);
                sleep(5);
                &$terminate_old_workers($self);
@@ -405,7 +396,7 @@ my $server_run = sub {
 
        } else {
            $self->run();
-       } 
+       }
     };
     my $err = $@;
 
@@ -438,7 +429,7 @@ sub new {
     eval {
        my $class = ref($this) || $this;
 
-       $self = bless { 
+       $self = bless {
            name => $name,
            pidfile => "/var/run/${name}.pid",
            workers => {},
@@ -466,7 +457,7 @@ sub new {
                die "unknown daemon option '$opt'\n";
            }
        }
-       
+
 
        # untaint
        $self->{cmdline} = [map { /^(.*)$/ } @$cmdline];
@@ -503,8 +494,7 @@ sub restart_daemon {
     }
 
     if ($self->{max_workers}) {
-       my @workers = keys %{$self->{workers}};
-       push @workers, keys %{$self->{old_workers}};
+       my @workers = (keys %{$self->{workers}}, keys %{$self->{old_workers}});
        $ENV{PVE_DAEMON_WORKER_PIDS} = join(':', @workers);
     }
 
@@ -574,7 +564,7 @@ my $read_pid = sub {
     return 0 if !$pid_str;
 
     return 0 if $pid_str !~ m/^(\d+)$/; # untaint
+
     my $pid = int($1);
 
     return $pid;
@@ -582,13 +572,12 @@ my $read_pid = sub {
 
 # checks if the process was started by systemd
 my $init_ppid = sub {
-
     if (getppid() == 1) {
        return 1;
     } else {
        return 0;
     }
-}; 
+};
 
 sub running {
     my ($self) = @_;
@@ -673,7 +662,7 @@ sub register_start_command {
             }
 
            return undef;
-       }});  
+       }});
 }
 
 my $reload_daemon = sub {
@@ -682,7 +671,7 @@ my $reload_daemon = sub {
     if ($self->{env_restart_pve_daemon}) {
        $self->start();
     } else {
-       my ($running, $pid) = $self->running(); 
+       my ($running, $pid) = $self->running();
        if (!$running) {
            $self->start();
        } else {
@@ -723,7 +712,7 @@ sub register_restart_command {
            }
 
            return undef;
-       }});               
+       }});
 }
 
 sub register_reload_command {
@@ -748,7 +737,7 @@ sub register_reload_command {
            &$reload_daemon($self, 1);
 
            return undef;
-       }});               
+       }});
 }
 
 sub register_stop_command {
@@ -769,7 +758,7 @@ sub register_stop_command {
 
        code => sub {
            my ($param) = @_;
-           
+
            if (&$init_ppid()) {
                $self->stop();
            } else {
@@ -777,7 +766,7 @@ sub register_stop_command {
            }
 
            return undef;
-       }});               
+       }});
 }
 
 sub register_status_command {
@@ -794,7 +783,7 @@ sub register_status_command {
            additionalProperties => 0,
            properties => {},
        },
-       returns => { 
+       returns => {
            type => 'string',
            enum => ['stopped', 'running'],
        },
@@ -808,7 +797,7 @@ sub register_status_command {
 # some useful helper
 
 sub create_reusable_socket {
-    my ($self, $port, $host, $family) = @_;
+    my ($self, $port, $host) = @_;
 
     die "no port specifed" if !$port;
 
@@ -817,26 +806,34 @@ sub create_reusable_socket {
     if (defined($sockfd = $ENV{"PVE_DAEMON_SOCKET_$port"}) &&
        $self->{env_restart_pve_daemon}) {
 
-       die "unable to parse socket fd '$sockfd'\n" 
+       die "unable to parse socket fd '$sockfd'\n"
            if $sockfd !~ m/^(\d+)$/;
        $sockfd = $1; # untaint
 
        $socket = IO::Socket::IP->new;
-       $socket->fdopen($sockfd, 'w') || 
+       $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(
-           LocalAddr => $host,
+       my %sockargs = (
            LocalPort => $port,
            Listen => SOMAXCONN,
-           Family => $family,
            Proto  => 'tcp',
            GetAddrInfoFlags => 0,
-           ReuseAddr => 1) ||
-           die "unable to create socket - $@\n";
+           ReuseAddr => 1,
+       );
+       if (defined($host)) {
+           $socket = IO::Socket::IP->new( LocalHost => $host, %sockargs) ||
+               die "unable to create socket - $@\n";
+       } else {
+           # disabling AF_INET6 (by adding ipv6.disable=1 to the kernel cmdline)
+           # causes bind on :: to fail, try 0.0.0.0 in that case
+           $socket = IO::Socket::IP->new( LocalHost => '::', %sockargs) //
+               IO::Socket::IP->new( LocalHost => '0.0.0.0', %sockargs);
+           die "unable to create socket - $@\n" if !$socket;
+       }
 
        # we often observe delays when using Nagle algorithm,
        # so we disable that to maximize performance