Init.pm 3.95 KB
Newer Older
1 2
package Lemonldap::NG::Handler::Main::Init;

3
our $VERSION = '2.0.2';
4 5 6

package Lemonldap::NG::Handler::Main;

7 8 9
use strict;
use Lemonldap::NG::Common::Conf;

10 11
our $statusInit = 1;

12 13 14 15 16 17 18 19 20 21 22 23 24
## @imethod void init(hashRef args)
# Read parameters and build the Lemonldap::NG::Common::Conf object.
# @param $args hash containing parameters
sub init($$) {
    my ( $class, $args ) = @_;

    # According to doc, localStorage can be declared in $args root,
    # but it must be in $args->{configStorage}
    foreach (qw(localStorage localStorageOptions)) {
        $args->{configStorage}->{$_} ||= $args->{$_};
    }

    my $tmp = Lemonldap::NG::Common::Conf->new( $args->{configStorage} );
25
    unless ( $class->confAcc($tmp) ) {
26 27 28 29 30 31 32
        die(    "$class : unable to build configuration: "
              . "$Lemonldap::NG::Common::Conf::msg" );
    }

    # Merge local configuration parameters so that params defined in
    # startup parameters have precedence over lemonldap-ng.ini params
    $class->localConfig(
33
        { %{ $class->confAcc->getLocalConf('handler') }, %{$args} } );
34 35 36 37 38

    $class->checkTime( $class->localConfig->{checkTime} || $class->checkTime );

    # Few actions that must be done at server startup:
    # * set log level for Lemonldap::NG logs
39
    $class->logLevelInit();
40 41 42 43 44

    # * set server signature
    $class->serverSignatureInit unless ( $class->localConfig->{hideSignature} );

    # * launch status process
45
    $class->statusInit();
46 47 48 49 50 51
    1;
}

# @method void logLevelInit
# Set log level for Lemonldap::NG logs
sub logLevelInit {
52
    my ($class) = @_;
53
    my $logger = $class->localConfig->{logger} ||= $class->defaultLogger;
54 55
    eval "require $logger";
    die $@ if ($@);
56
    $class->logger( $logger->new( $class->localConfig ) );
57
    $class->logger->debug("Logger $logger loaded");
58 59 60 61
    $logger = $class->localConfig->{userLogger} || $logger;
    eval "require $logger";
    die $@ if ($@);
    $class->userLogger( $logger->new( $class->localConfig ), user => 1 );
62
    $class->logger->debug("User logger $logger loaded");
63 64 65 66 67 68
}

# @method void serverSignatureInit
# adapt server signature
sub serverSignatureInit {
    my $class = shift;
Xavier Guimard's avatar
Xavier Guimard committed
69
    require Lemonldap::NG::Handler::Main;
70 71
    my $version = $Lemonldap::NG::Handler::VERSION;
    $class->setServerSignature("Lemonldap::NG/$version");
72 73 74 75 76 77
}

## @ifn protected void statusInit()
# Launch the status process
sub statusInit {
    my ($class) = @_;
78 79 80 81 82 83 84 85 86 87
    return unless ( $class->localConfig->{status} and $statusInit );
    $statusInit = 0;
    return if ( $class->tsv->{statusPipe} );
    if ( $ENV{LLNGSTATUSHOST} ) {
        require IO::Socket::INET;
        $class->tsv->{statusPipe} = IO::Socket::INET->new(
            Proto    => 'udp',
            PeerAddr => $ENV{LLNGSTATUSHOST}
        );
        $class->tsv->{statusOut} = undef;
88 89
    }
    else {
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
        require IO::Pipe;
        my $statusPipe = IO::Pipe->new;
        my $statusOut  = IO::Pipe->new;
        if ( my $pid = fork() ) {
            $class->logger->debug("Status collector launched ($pid)");
            $statusPipe->writer();
            $statusOut->reader();
            $statusPipe->autoflush(1);
            ( $class->tsv->{statusPipe}, $class->tsv->{statusOut} ) =
              ( $statusPipe, $statusOut );
        }
        else {
            $statusPipe->reader();
            $statusOut->writer();
            my $fdin  = $statusPipe->fileno;
            my $fdout = $statusOut->fileno;
            open STDIN,  "<&$fdin";
            open STDOUT, ">&$fdout";
            my $perl_exec = ( $^X =~ /perl/ ) ? $^X : 'perl';
            exec $perl_exec, '-MLemonldap::NG::Handler::Lib::Status',

              # Insert @INC in Perl path
112
              map( {"-I$_"} @INC ),
113 114 115 116 117 118 119 120 121 122 123

              # Command to launch
              '-e', '&Lemonldap::NG::Handler::Lib::Status::run()',

              # Optional arg: UDP socket to listen to
              (
                $ENV{LLNGSTATUSLISTEN}
                ? ( '--', '--udp', $ENV{LLNGSTATUSLISTEN} )
                : ()
              );
        }
124 125 126 127
    }
}

1;