#!/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 PVE::SafeSyslog;
use PVE::Service::pvedaemon;

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

my $prepare = sub {
    # create dir for dtach sockets
    mkdir "/var/run/dtach";

    # Pre-create the accept-lock file as root, so PVE::Service::pvedaemon::init()
    # (running after PVE::Daemon::setup, which on some distributions can drop
    # privileges depending on configuration) can always open it. Required on
    # systems where /run/lock is not world-writable (e.g. openEuler / RHEL),
    # where Debian's 1777 default is not present.
    my $lock = "/var/lock/pvedaemon.lck";
    if (open(my $fh, '>>', $lock)) {
        close($fh);
        chmod(0644, $lock);
    }
};

PVE::Service::pvedaemon->run_cli_handler(prepare => $prepare);
