Changeset 11231

Show
Ignore:
Timestamp:
08/18/08 14:56:03 (5 months ago)
Author:
/C=ES/O=Warp Networks S.L./CN=ejhernandez@…
Message:

Using ebox logger capability and daemonise the script refs #1103

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/remote-services/ebox/src/libexec/ebox-runnerd

    r11230 r11231  
    11#!/usr/bin/perl 
    22 
     3use EBox; 
    34use EBox::Config; 
     5use EBox::Exceptions::External; 
    46use Linux::Inotify2; 
    5 use Data::Dumper; 
     7use POSIX qw(SIG_IGN SIGHUP); 
    68 
    79use constant JOBS_DIR      => EBox::Config::conf() . 'remote-services/jobs/'; 
    810use constant INCOMING_DIR  => JOBS_DIR . 'incoming/'; 
    911use constant OUTCOMING_DIR => JOBS_DIR . 'outcoming/'; 
     12 
     13# Procedure: _daemonise 
     14# 
     15#     Daemonise the current script to become a daemon following the 
     16#     Stevens chapter 13 coding rules 
     17# 
     18sub _daemonise 
     19{ 
     20    # Clear the file creation mask 
     21    umask(0); 
     22    # Become a session leader to lose controlling TTY 
     23    my $pid = fork(); 
     24    if ( $pid < 0 ) { 
     25        throw EBox::Exceptions::External( "Cannot fork: $!"); 
     26    } elsif ( $pid != 0 ) { 
     27        # Parent must throw EBox::Exceptions::External( 
     28        exit(0); 
     29    } 
     30    POSIX::setsid(); 
     31 
     32    # Ensure future opens won't allocate controlling TTYs 
     33    POSIX::sigaction(POSIX::SIGHUP, POSIX::SigAction->new( POSIX::SIG_IGN )) 
     34        or throw EBox::Exceptions::External( "Cannot ignore SIGHUP: $!"); 
     35    my $pid = fork(); 
     36    if ( $pid < 0 ) { 
     37        throw EBox::Exceptions::External( "Cannot fork: $!"); 
     38    } elsif ( $pid != 0 ) { 
     39        # Parent must die 
     40        exit(0); 
     41    } 
     42 
     43    # Change current working directory to roo so we won't prevent file 
     44    # systems from being unmounted 
     45    chdir('/') or throw EBox::Exceptions::External( "Cannot change directory to / $!" ); 
     46 
     47    POSIX::close($_) foreach (0 .. 64); 
     48 
     49    open( STDIN, '/dev/null'); 
     50    open( STDOUT, '/dev/null'); 
     51    open( STDERR, '/dev/null'); 
     52 
     53} 
    1054 
    1155# Procedure: _run 
     
    2771    my ($jobDir) = @_; 
    2872 
     73    chomp($jobDir); 
    2974    my ($jobId) = $jobDir =~ m:^.*/(.*?)$:g; 
    3075    system( "xargs -a $jobDir/args $jobDir/script > $jobDir/stdout 2> $jobDir/stderr"); 
    3176    my $retValue = $?; 
    32     open (my $returnedFile, '>', "$jobDir/exitValue") 
    33       or die "Cannot open file $jobDir/exitValue: $!"; 
     77    open (my $returnedFile,  '>', "$jobDir/exitValue") 
     78      or throw EBox::Exceptions::External( "Cannot open file:$jobDir/exitValue: $!"); 
    3479    print $returnedFile $retValue; 
    3580    close($returnedFile); 
     
    4994{ 
    5095    opendir(my $dir, INCOMING_DIR) 
    51       or die "Cannot open directory: $!";  
     96      or throw EBox::Exceptions::External( "Cannot open directory: $!"); 
    5297    while(my $filePath = readdir($dir)) { 
    5398        next if ( $filePath eq '.' or $filePath eq '..' ); 
     
    59104} 
    60105 
     106# MAIN 
     107EBox::init(); 
     108_daemonise(); 
     109 
    61110my $incomingSub = sub { 
    62111    my ($event) = @_; 
    63     print "New stuff: $event->{name}\n"; 
    64     print "$event->{w}{name}\n"; 
     112    EBox::debug("New stuff: $event->{name}"); 
     113    EBox::debug("$event->{w}{name}"); 
    65114    _run($event->{w}{name} . $event->name()); 
    66115}; 
    67116 
    68117my $inotify = new Linux::Inotify2() 
    69   or die "Unable to create a new inotify object: $!"; 
     118  or throw EBox::Exceptions::External("Unable to create a new inotify object: $!"); 
    70119 
    71 $inotify->watch(INCOMING_DIR, IN_CREATE, 
    72                 $incomingSub); 
     120$inotify->watch(INCOMING_DIR, IN_CREATE, $incomingSub); 
    73121# Run those directories which have not run previously 
    74122_runRemainder(); 
     
    76124    my @events = $inotify->read(); 
    77125    unless(@events > 0) { 
    78         print "Read error: $!"; 
     126        EBox::error("Read error: $!"); 
    79127        last; 
    80128    }