Commit f4defdaa authored by Dietmar Maurer's avatar Dietmar Maurer

code cleanups

parent 3cdd9dc4
...@@ -2,25 +2,17 @@ package PVE::APIDaemon; ...@@ -2,25 +2,17 @@ package PVE::APIDaemon;
use strict; use strict;
use warnings; use warnings;
use vars qw(@ISA); use POSIX ":sys_wait_h";
use IO::Socket::INET; use IO::Socket::INET;
use PVE::SafeSyslog; use PVE::SafeSyslog;
use PVE::INotify;
use PVE::RPCEnvironment;
use PVE::HTTPServer; use PVE::HTTPServer;
use POSIX qw(EINTR);
use POSIX ":sys_wait_h";
use IO::Handle;
use IO::Select;
use JSON;
my $workers = {}; my $workers = {};
sub new { sub new {
my ($this, %args) = @_; my ($this, %args) = @_;
my $class = ref($this) || $this; my $class = ref($this) || $this;
die "no lockfile" if !$args{lockfile}; die "no lockfile" if !$args{lockfile};
...@@ -43,7 +35,7 @@ sub new { ...@@ -43,7 +35,7 @@ sub new {
$cfg->{lockfh} = $lockfh; $cfg->{lockfh} = $lockfh;
$cfg->{max_workers} = 3 if !$cfg->{max_workers}; $cfg->{max_workers} = 3 if !$cfg->{max_workers};
$cfg->{trusted_env} = 0 if !defined($cfg->{trusted_env}); $cfg->{trusted_env} = 0 if !defined($cfg->{trusted_env});
return $self; return $self;
} }
...@@ -52,9 +44,9 @@ sub worker_finished { ...@@ -52,9 +44,9 @@ sub worker_finished {
syslog('info', "worker $cpid finished"); syslog('info', "worker $cpid finished");
} }
sub finish_workers { sub finish_workers {
local $!; local $?; local $!; local $?;
foreach my $cpid (keys %$workers) { foreach my $cpid (keys %$workers) {
my $waitpid = waitpid ($cpid, WNOHANG); my $waitpid = waitpid ($cpid, WNOHANG);
if (defined($waitpid) && ($waitpid == $cpid)) { if (defined($waitpid) && ($waitpid == $cpid)) {
...@@ -75,7 +67,7 @@ sub test_workers { ...@@ -75,7 +67,7 @@ sub test_workers {
} }
sub start_workers { sub start_workers {
my ($self, $rpcenv) = @_; my ($self) = @_;
my $count = 0; my $count = 0;
foreach my $cpid (keys %$workers) { foreach my $cpid (keys %$workers) {
...@@ -104,10 +96,8 @@ sub start_workers { ...@@ -104,10 +96,8 @@ sub start_workers {
$SIG{TERM} = $SIG{QUIT} = 'DEFAULT'; # we handle that with AnyEvent $SIG{TERM} = $SIG{QUIT} = 'DEFAULT'; # we handle that with AnyEvent
eval { eval {
# try to init inotify my $server = PVE::HTTPServer->new(%{$self->{cfg}});
# fixme: poll $server->run();
PVE::INotify::inotify_init();
$self->handle_connections($rpcenv);
}; };
if (my $err = $@) { if (my $err = $@) {
syslog('err', $err); syslog('err', $err);
...@@ -130,7 +120,7 @@ sub terminate_server { ...@@ -130,7 +120,7 @@ sub terminate_server {
my $previous_alarm = alarm (10); my $previous_alarm = alarm (10);
eval { eval {
local $SIG{ALRM} = sub { die "timeout\n" }; local $SIG{ALRM} = sub { die "timeout\n" };
while ((my $pid = waitpid (-1, 0)) > 0) { while ((my $pid = waitpid (-1, 0)) > 0) {
if (defined($workers->{$pid})) { if (defined($workers->{$pid})) {
delete ($workers->{$pid}); delete ($workers->{$pid});
...@@ -140,11 +130,11 @@ sub terminate_server { ...@@ -140,11 +130,11 @@ sub terminate_server {
alarm(0); # avoid race condition alarm(0); # avoid race condition
}; };
my $err = $@; my $err = $@;
alarm ($previous_alarm); alarm ($previous_alarm);
if ($err) { if ($err) {
syslog('err', "error stopping workers (will kill them now) - $err"); syslog('err', "error stopping workers (will kill them now) - $err");
foreach my $cpid (keys %$workers) { foreach my $cpid (keys %$workers) {
# KILL childs still alive! # KILL childs still alive!
if (kill (0, $cpid)) { if (kill (0, $cpid)) {
...@@ -159,10 +149,6 @@ sub terminate_server { ...@@ -159,10 +149,6 @@ sub terminate_server {
sub start_server { sub start_server {
my $self = shift; my $self = shift;
my $atfork = sub { close($self->{cfg}->{socket}); };
my $rpcenv = PVE::RPCEnvironment->init(
$self->{cfg}->{trusted_env} ? 'priv' : 'pub', atfork => $atfork);
eval { eval {
my $old_sig_chld = $SIG{CHLD}; my $old_sig_chld = $SIG{CHLD};
local $SIG{CHLD} = sub { local $SIG{CHLD} = sub {
...@@ -171,11 +157,11 @@ sub start_server { ...@@ -171,11 +157,11 @@ sub start_server {
}; };
my $old_sig_term = $SIG{TERM}; my $old_sig_term = $SIG{TERM};
local $SIG{TERM} = sub { local $SIG{TERM} = sub {
terminate_server (); terminate_server ();
&$old_sig_term(@_) if $old_sig_term; &$old_sig_term(@_) if $old_sig_term;
}; };
local $SIG{QUIT} = sub { local $SIG{QUIT} = sub {
terminate_server(); terminate_server();
&$old_sig_term(@_) if $old_sig_term; &$old_sig_term(@_) if $old_sig_term;
}; };
...@@ -188,8 +174,8 @@ sub start_server { ...@@ -188,8 +174,8 @@ sub start_server {
}; };
for (;;) { # forever for (;;) { # forever
$self->start_workers($rpcenv); $self->start_workers();
sleep (5); sleep (5);
$self->test_workers(); $self->test_workers();
} }
}; };
...@@ -200,18 +186,4 @@ sub start_server { ...@@ -200,18 +186,4 @@ sub start_server {
} }
} }
sub send_error {
my ($c, $code, $msg) = @_;
$c->send_response(HTTP::Response->new($code, $msg));
}
sub handle_connections {
my ($self, $rpcenv) = @_;
my $server = PVE::HTTPServer->new(%{$self->{cfg}}, rpcenv => $rpcenv);
$server->run();
}
1; 1;
This diff is collapsed.
...@@ -34,8 +34,6 @@ $SIG{'__WARN__'} = sub { ...@@ -34,8 +34,6 @@ $SIG{'__WARN__'} = sub {
$0 = "pvedaemon"; $0 = "pvedaemon";
PVE::APIDaemon::enable_debug() if $opt_debug;
# create dir for dtach sockets # create dir for dtach sockets
mkdir "/var/run/dtach"; mkdir "/var/run/dtach";
...@@ -47,6 +45,7 @@ eval { ...@@ -47,6 +45,7 @@ eval {
port => 85, port => 85,
trusted_env => 1, # partly trusted, because only local programs can connect trusted_env => 1, # partly trusted, because only local programs can connect
lockfile => $lockfile, lockfile => $lockfile,
debug => $opt_debug,
keep_alive => 100, keep_alive => 100,
max_conn => 500, max_conn => 500,
max_requests => 1000); max_requests => 1000);
......
...@@ -52,8 +52,6 @@ POSIX::setuid($uid) || die "setuid $uid failed - $!\n"; ...@@ -52,8 +52,6 @@ POSIX::setuid($uid) || die "setuid $uid failed - $!\n";
# just to be sure # just to be sure
die "detected strange uid/gid\n" if !($UID == $uid && $EUID == $uid && $GID eq "$gid $gid" && $EGID eq "$gid $gid"); die "detected strange uid/gid\n" if !($UID == $uid && $EUID == $uid && $GID eq "$gid $gid" && $EGID eq "$gid $gid");
PVE::APIDaemon::enable_debug() if $opt_debug;
sub add_dirs { sub add_dirs {
my ($result_hash, $alias, $subdir) = @_; my ($result_hash, $alias, $subdir) = @_;
...@@ -86,6 +84,7 @@ eval { ...@@ -86,6 +84,7 @@ eval {
keep_alive => 100, keep_alive => 100,
max_conn => 500, max_conn => 500,
max_requests => 1000, max_requests => 1000,
debug => $opt_debug,
trusted_env => 0, # not trusted, anyone can connect trusted_env => 0, # not trusted, anyone can connect
logfile => '/var/log/pveproxy/access.log', logfile => '/var/log/pveproxy/access.log',
lockfile => $lockfile, lockfile => $lockfile,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment