Changeset 11231
- Timestamp:
- 08/18/08 14:56:03 (5 months ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
branches/remote-services/ebox/src/libexec/ebox-runnerd
r11230 r11231 1 1 #!/usr/bin/perl 2 2 3 use EBox; 3 4 use EBox::Config; 5 use EBox::Exceptions::External; 4 6 use Linux::Inotify2; 5 use Data::Dumper;7 use POSIX qw(SIG_IGN SIGHUP); 6 8 7 9 use constant JOBS_DIR => EBox::Config::conf() . 'remote-services/jobs/'; 8 10 use constant INCOMING_DIR => JOBS_DIR . 'incoming/'; 9 11 use 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 # 18 sub _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 } 10 54 11 55 # Procedure: _run … … 27 71 my ($jobDir) = @_; 28 72 73 chomp($jobDir); 29 74 my ($jobId) = $jobDir =~ m:^.*/(.*?)$:g; 30 75 system( "xargs -a $jobDir/args $jobDir/script > $jobDir/stdout 2> $jobDir/stderr"); 31 76 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: $!"); 34 79 print $returnedFile $retValue; 35 80 close($returnedFile); … … 49 94 { 50 95 opendir(my $dir, INCOMING_DIR) 51 or die "Cannot open directory: $!";96 or throw EBox::Exceptions::External( "Cannot open directory: $!"); 52 97 while(my $filePath = readdir($dir)) { 53 98 next if ( $filePath eq '.' or $filePath eq '..' ); … … 59 104 } 60 105 106 # MAIN 107 EBox::init(); 108 _daemonise(); 109 61 110 my $incomingSub = sub { 62 111 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}"); 65 114 _run($event->{w}{name} . $event->name()); 66 115 }; 67 116 68 117 my $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: $!"); 70 119 71 $inotify->watch(INCOMING_DIR, IN_CREATE, 72 $incomingSub); 120 $inotify->watch(INCOMING_DIR, IN_CREATE, $incomingSub); 73 121 # Run those directories which have not run previously 74 122 _runRemainder(); … … 76 124 my @events = $inotify->read(); 77 125 unless(@events > 0) { 78 print "Read error: $!";126 EBox::error("Read error: $!"); 79 127 last; 80 128 }
