Commit f4defdaa authored by Dietmar Maurer's avatar Dietmar Maurer

code cleanups

parent 3cdd9dc4
...@@ -2,20 +2,12 @@ package PVE::APIDaemon; ...@@ -2,20 +2,12 @@ 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 {
...@@ -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);
...@@ -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 {
...@@ -188,7 +174,7 @@ sub start_server { ...@@ -188,7 +174,7 @@ 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;
...@@ -30,7 +30,6 @@ use CGI; # fixme: remove this! ...@@ -30,7 +30,6 @@ use CGI; # fixme: remove this!
$CGI::DISABLE_UPLOADS = 1; # no uploads $CGI::DISABLE_UPLOADS = 1; # no uploads
$CGI::POST_MAX = 1024 * 10; # max 10K posts $CGI::POST_MAX = 1024 * 10; # max 10K posts
use Scalar::Util qw/weaken/; # fixme: remove?
use Data::Dumper; # fixme: remove use Data::Dumper; # fixme: remove
my $known_methods = { my $known_methods = {
...@@ -92,7 +91,7 @@ sub client_do_disconnect { ...@@ -92,7 +91,7 @@ sub client_do_disconnect {
$hdl->on_eof(undef); $hdl->on_eof(undef);
$self->{conn_count}--; $self->{conn_count}--;
#print "$$: client_do_disconnect $self->{conn_count} $hdl\n"; print "$$: CLOSE FH" . $hdl->{fh}->fileno() . " CONN$self->{conn_count}\n" if $self->{debug};
} }
sub finish_response { sub finish_response {
...@@ -155,12 +154,7 @@ sub response { ...@@ -155,12 +154,7 @@ sub response {
$resp->header('Server' => "pve-api-daemon/3.0"); $resp->header('Server' => "pve-api-daemon/3.0");
my $content_length; my $content_length;
if (ref($content) eq "CODE") { if ($content) {
$reqstate->{keep_alive} = 0;
# fixme:
} elsif ($content) {
$content_length = length($content); $content_length = length($content);
...@@ -172,6 +166,7 @@ sub response { ...@@ -172,6 +166,7 @@ sub response {
} }
$resp->header("Content-Length" => $content_length); $resp->header("Content-Length" => $content_length);
$reqstate->{log}->{content_length} = $content_length; $reqstate->{log}->{content_length} = $content_length;
} else { } else {
$resp->remove_header("Content-Length"); $resp->remove_header("Content-Length");
} }
...@@ -183,7 +178,7 @@ sub response { ...@@ -183,7 +178,7 @@ sub response {
} }
$res .= $resp->headers_as_string("\015\012"); $res .= $resp->headers_as_string("\015\012");
#print "SEND(supress content) $res\n"; #print "SEND(without content) $res\n" if $self->{debug};
$res .= "\015\012"; $res .= "\015\012";
$res .= $content; $res .= $content;
...@@ -283,8 +278,6 @@ sub proxy_request { ...@@ -283,8 +278,6 @@ sub proxy_request {
} }
} }
# fixme: tls_ctx;
my $w; $w = http_request( my $w; $w = http_request(
$method => $target, $method => $target,
headers => $headers, headers => $headers,
...@@ -365,7 +358,7 @@ sub handle_api2_request { ...@@ -365,7 +358,7 @@ sub handle_api2_request {
my $res = PVE::REST::rest_handler($rpcenv, $clientip, $method, $path, $rel_uri, $ticket, $token); my $res = PVE::REST::rest_handler($rpcenv, $clientip, $method, $path, $rel_uri, $ticket, $token);
# fixme: eval { $userid = $rpcenv->get_user(); }; # todo: eval { $userid = $rpcenv->get_user(); };
my $userid = $rpcenv->{user}; # this is faster my $userid = $rpcenv->{user}; # this is faster
$rpcenv->set_user(undef); # clear after request $rpcenv->set_user(undef); # clear after request
...@@ -491,7 +484,7 @@ sub unshift_read_header { ...@@ -491,7 +484,7 @@ sub unshift_read_header {
my $len = $r->header('Content-Length'); my $len = $r->header('Content-Length');
my $pveclientip = $r->header('PVEClientIP'); my $pveclientip = $r->header('PVEClientIP');
# fixme: # fixme: how can we make PVEClientIP header trusted?
if ($self->{trusted_env} && $pveclientip) { if ($self->{trusted_env} && $pveclientip) {
$reqstate->{peer_host} = $pveclientip; $reqstate->{peer_host} = $pveclientip;
} else { } else {
...@@ -613,8 +606,6 @@ sub accept { ...@@ -613,8 +606,6 @@ sub accept {
$self->{conn_count}++; $self->{conn_count}++;
print "$$: ACCEPT OK $self->{conn_count} FH" . $clientfh->fileno() . "\n";
return $clientfh; return $clientfh;
} }
...@@ -633,7 +624,7 @@ sub wait_end_loop { ...@@ -633,7 +624,7 @@ sub wait_end_loop {
# else we need to wait until all open connections gets closed # else we need to wait until all open connections gets closed
my $w; $w = AnyEvent->timer (after => 1, interval => 1, cb => sub { my $w; $w = AnyEvent->timer (after => 1, interval => 1, cb => sub {
eval { eval {
# fixme: test for active connections instead? # todo: test for active connections instead (we can abort idle connections)
if ($self->{conn_count} <= 0) { if ($self->{conn_count} <= 0) {
undef $w; undef $w;
$self->{end_cond}->send(1); $self->{end_cond}->send(1);
...@@ -680,7 +671,7 @@ sub accept_connections { ...@@ -680,7 +671,7 @@ sub accept_connections {
}, },
($self->{tls_ctx} ? (tls => "accept", tls_ctx => $self->{tls_ctx}) : ())); ($self->{tls_ctx} ? (tls => "accept", tls_ctx => $self->{tls_ctx}) : ()));
print "$$: ACCEPT OK $reqstate->{hdl} $self->{conn_count}\n"; print "$$: ACCEPT FH" . $clientfh->fileno() . " CONN$self->{conn_count}\n" if $self->{debug};
$self->push_request_header($reqstate); $self->push_request_header($reqstate);
} }
...@@ -729,12 +720,19 @@ sub new { ...@@ -729,12 +720,19 @@ sub new {
my $class = ref($this) || $this; my $class = ref($this) || $this;
foreach my $req (qw(rpcenv socket lockfh lockfile)) { foreach my $req (qw(socket lockfh lockfile)) {
die "misssing required argument '$req'" if !defined($args{$req}); die "misssing required argument '$req'" if !defined($args{$req});
} }
my $self = bless { %args }, $class; my $self = bless { %args }, $class;
# init inotify
PVE::INotify::inotify_init();
my $atfork = sub { close($self->{socket}); };
$self->{rpcenv} = PVE::RPCEnvironment->init(
$self->{trusted_env} ? 'priv' : 'pub', atfork => $atfork);
fh_nonblocking($self->{socket}, 1); fh_nonblocking($self->{socket}, 1);
$self->{end_loop} = 0; $self->{end_loop} = 0;
...@@ -779,6 +777,10 @@ sub new { ...@@ -779,6 +777,10 @@ sub new {
$self->wait_end_loop(); $self->wait_end_loop();
}); });
$self->{inotify_poll} = AnyEvent->timer(after => 5, interval => 5, cb => sub {
PVE::INotify::poll(); # read inotify events
});
return $self; return $self;
} }
......
...@@ -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