Commit f4defdaa authored by Dietmar Maurer's avatar Dietmar Maurer

code cleanups

parent 3cdd9dc4
......@@ -2,20 +2,12 @@ package PVE::APIDaemon;
use strict;
use warnings;
use vars qw(@ISA);
use POSIX ":sys_wait_h";
use IO::Socket::INET;
use PVE::SafeSyslog;
use PVE::INotify;
use PVE::RPCEnvironment;
use PVE::HTTPServer;
use POSIX qw(EINTR);
use POSIX ":sys_wait_h";
use IO::Handle;
use IO::Select;
use JSON;
my $workers = {};
sub new {
......@@ -75,7 +67,7 @@ sub test_workers {
}
sub start_workers {
my ($self, $rpcenv) = @_;
my ($self) = @_;
my $count = 0;
foreach my $cpid (keys %$workers) {
......@@ -104,10 +96,8 @@ sub start_workers {
$SIG{TERM} = $SIG{QUIT} = 'DEFAULT'; # we handle that with AnyEvent
eval {
# try to init inotify
# fixme: poll
PVE::INotify::inotify_init();
$self->handle_connections($rpcenv);
my $server = PVE::HTTPServer->new(%{$self->{cfg}});
$server->run();
};
if (my $err = $@) {
syslog('err', $err);
......@@ -159,10 +149,6 @@ sub terminate_server {
sub start_server {
my $self = shift;
my $atfork = sub { close($self->{cfg}->{socket}); };
my $rpcenv = PVE::RPCEnvironment->init(
$self->{cfg}->{trusted_env} ? 'priv' : 'pub', atfork => $atfork);
eval {
my $old_sig_chld = $SIG{CHLD};
local $SIG{CHLD} = sub {
......@@ -188,7 +174,7 @@ sub start_server {
};
for (;;) { # forever
$self->start_workers($rpcenv);
$self->start_workers();
sleep (5);
$self->test_workers();
}
......@@ -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;
......@@ -30,7 +30,6 @@ use CGI; # fixme: remove this!
$CGI::DISABLE_UPLOADS = 1; # no uploads
$CGI::POST_MAX = 1024 * 10; # max 10K posts
use Scalar::Util qw/weaken/; # fixme: remove?
use Data::Dumper; # fixme: remove
my $known_methods = {
......@@ -92,7 +91,7 @@ sub client_do_disconnect {
$hdl->on_eof(undef);
$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 {
......@@ -155,12 +154,7 @@ sub response {
$resp->header('Server' => "pve-api-daemon/3.0");
my $content_length;
if (ref($content) eq "CODE") {
$reqstate->{keep_alive} = 0;
# fixme:
} elsif ($content) {
if ($content) {
$content_length = length($content);
......@@ -172,6 +166,7 @@ sub response {
}
$resp->header("Content-Length" => $content_length);
$reqstate->{log}->{content_length} = $content_length;
} else {
$resp->remove_header("Content-Length");
}
......@@ -183,7 +178,7 @@ sub response {
}
$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 .= $content;
......@@ -283,8 +278,6 @@ sub proxy_request {
}
}
# fixme: tls_ctx;
my $w; $w = http_request(
$method => $target,
headers => $headers,
......@@ -365,7 +358,7 @@ sub handle_api2_request {
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
$rpcenv->set_user(undef); # clear after request
......@@ -491,7 +484,7 @@ sub unshift_read_header {
my $len = $r->header('Content-Length');
my $pveclientip = $r->header('PVEClientIP');
# fixme:
# fixme: how can we make PVEClientIP header trusted?
if ($self->{trusted_env} && $pveclientip) {
$reqstate->{peer_host} = $pveclientip;
} else {
......@@ -613,8 +606,6 @@ sub accept {
$self->{conn_count}++;
print "$$: ACCEPT OK $self->{conn_count} FH" . $clientfh->fileno() . "\n";
return $clientfh;
}
......@@ -633,7 +624,7 @@ sub wait_end_loop {
# else we need to wait until all open connections gets closed
my $w; $w = AnyEvent->timer (after => 1, interval => 1, cb => sub {
eval {
# fixme: test for active connections instead?
# todo: test for active connections instead (we can abort idle connections)
if ($self->{conn_count} <= 0) {
undef $w;
$self->{end_cond}->send(1);
......@@ -680,7 +671,7 @@ sub accept_connections {
},
($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);
}
......@@ -729,12 +720,19 @@ sub new {
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});
}
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);
$self->{end_loop} = 0;
......@@ -779,6 +777,10 @@ sub new {
$self->wait_end_loop();
});
$self->{inotify_poll} = AnyEvent->timer(after => 5, interval => 5, cb => sub {
PVE::INotify::poll(); # read inotify events
});
return $self;
}
......
......@@ -34,8 +34,6 @@ $SIG{'__WARN__'} = sub {
$0 = "pvedaemon";
PVE::APIDaemon::enable_debug() if $opt_debug;
# create dir for dtach sockets
mkdir "/var/run/dtach";
......@@ -47,6 +45,7 @@ eval {
port => 85,
trusted_env => 1, # partly trusted, because only local programs can connect
lockfile => $lockfile,
debug => $opt_debug,
keep_alive => 100,
max_conn => 500,
max_requests => 1000);
......
......@@ -52,8 +52,6 @@ POSIX::setuid($uid) || die "setuid $uid failed - $!\n";
# just to be sure
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 {
my ($result_hash, $alias, $subdir) = @_;
......@@ -86,6 +84,7 @@ eval {
keep_alive => 100,
max_conn => 500,
max_requests => 1000,
debug => $opt_debug,
trusted_env => 0, # not trusted, anyone can connect
logfile => '/var/log/pveproxy/access.log',
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