#!/usr/bin/perl -T

$ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';

delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};  

use strict;
use warnings;
use Getopt::Long;
use POSIX ":sys_wait_h";
use Socket;
use PVE::SafeSyslog;
use PVE::APIDaemon;

my $pidfile = "/var/run/pvedaemon.pid";
my $lockfile = "/var/lock/pvedaemon.lck";

my $opt_debug;

initlog ('pvedaemon');

if (!GetOptions ('debug' => \$opt_debug)) {
    die "usage: $0 [--debug]\n";
}

$SIG{'__WARN__'} = sub {
    my $err = $@;
    my $t = $_[0];
    chomp $t;
    syslog('warning', "WARNING: %s", $t);
    $@ = $err;
};

$0 = "pvedaemon";

# create dir for dtach sockets
mkdir "/var/run/dtach";

my $cpid;
my $daemon;
eval {
    $daemon = PVE::APIDaemon->new(
	host => "127.0.0.1", 
	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);
};

my $err = $@;

if ($err) {
    syslog ('err' , "unable to start server: $err");
    print STDERR $err;
    exit (-1);
}

if ($opt_debug || !($cpid = fork ())) {

    $SIG{PIPE} = 'IGNORE';
    $SIG{INT} = 'IGNORE' if !$opt_debug;

    $SIG{TERM} = $SIG{QUIT} = sub { 
	syslog ('info' , "server closing");

	$SIG{INT} = 'DEFAULT';

	unlink "$pidfile";

	exit (0);
    };

    syslog ('info' , "starting server");

    if (!$opt_debug) {
	# redirect STDIN/STDOUT/SDTERR to /dev/null
	open STDIN,  '</dev/null' || die "can't read /dev/null  [$!]";
	open STDOUT, '>/dev/null' || die "can't write /dev/null [$!]";
	open STDERR, '>&STDOUT' || die "can't open STDERR to STDOUT [$!]";
    }

    POSIX::setsid(); 

    system ("echo > /var/lib/pve-manager/vmops"); # init vmops file

    eval {
	$daemon->start_server();
    };
    my $err = $@;

    if ($err) {
	syslog ('err' , "unexpected server error: $err");
	print STDERR $err if $opt_debug;
	exit (-1);
    }

} else {

    open (PIDFILE, ">$pidfile") || 
	die "cant write '$pidfile' - $! :ERROR";
    print PIDFILE "$cpid\n";
    close (PIDFILE) || 
	die "cant write '$pidfile' - $! :ERROR";
}

exit (0);

__END__

=head1 NAME
                                          
pvedaemon - the PVE configuration server

=head1 SYNOPSIS

pvedaemon [--debug]

=head1 DESCRIPTION

All configuration is done using this Server. The Server only
listens to a local address 127.0.0.1 port 85 for security
reasons.

=head1 COPYRIGHT AND DISCLAIMER

 Copyright (C) 2007-2013 Proxmox Server Solutions GmbH

 This program is free software: you can redistribute it and/or modify it
 under the terms of the GNU Affero General Public License as published
 by the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public
 License along with this program.  If not, see
 <http://www.gnu.org/licenses/>.

