9   use lib "$FindBin::Bin/..";
 
  11   use SL::System::Process;
 
  12   $exe_dir = SL::System::Process::exe_dir;
 
  14   unshift @INC, "${exe_dir}/modules/override"; # Use our own versions of various modules (e.g. YAML).
 
  15   push    @INC, "${exe_dir}/modules/fallback"; # Only use our own versions of modules if there's no system version.
 
  16   unshift @INC, $exe_dir;
 
  18   chdir($exe_dir) || die "Cannot change directory to ${exe_dir}\n";
 
  21 use CGI qw( -no_xhtml);
 
  26 use English qw(-no_match_vars);
 
  28 use List::Util qw(first);
 
  29 use POSIX qw(setuid setgid);
 
  31 use SL::DB::BackgroundJob;
 
  32 use SL::BackgroundJob::ALL;
 
  34 use SL::Helper::DateTime;
 
  35 use SL::InstanceConfiguration;
 
  39 use SL::System::TaskServer;
 
  44   return if !$lx_office_conf{task_server}->{debug};
 
  45   $::lxdebug->message(0, @_);
 
  49   my $login  = $lx_office_conf{task_server}->{login};
 
  50   my $client = $lx_office_conf{task_server}->{client};
 
  54   $::lxdebug       = LXDebug->new;
 
  55   $::locale        = Locale->new($::lx_office_conf{system}->{language});
 
  57   $::auth          = SL::Auth->new;
 
  58   die "No client configured or no client found with the name/ID '$client'" unless $::auth->set_client($client);
 
  59   $::instance_conf = SL::InstanceConfiguration->new;
 
  60   $::request       = { cgi => CGI->new({}) };
 
  62   die 'cannot reach auth db'               unless $::auth->session_tables_present;
 
  64   $::auth->restore_session;
 
  66   require "bin/mozilla/common.pl";
 
  68   die "cannot find user $login"            unless %::myconfig = $::auth->read_user(login => $login);
 
  69   die "cannot find locale for user $login" unless $::locale   = Locale->new('de');
 
  73   my $user = $lx_office_conf{task_server}->{run_as};
 
  77   while (my @details = getpwent()) {
 
  78     next unless $details[0] eq $user;
 
  79     ($uid, $gid) = @details[2, 3];
 
  85     print "Error: Cannot drop privileges to ${user}: user does not exist\n";
 
  90     print "Error: Cannot drop group privileges to ${user} (group ID $gid): $!\n";
 
  95     print "Error: Cannot drop user privileges to ${user} (user ID $uid): $!\n";
 
 103   SL::LxOfficeConf->read($self->{configfile});
 
 105   die "Missing section [task_server] in config file"                 unless $lx_office_conf{task_server};
 
 106   die "Missing key 'login' in section [task_server] in config file"  unless $lx_office_conf{task_server}->{login};
 
 107   die "Missing key 'client' in section [task_server] in config file" unless $lx_office_conf{task_server}->{client};
 
 118       debug("Retrieving jobs");
 
 120       my $jobs = SL::DB::Manager::BackgroundJob->get_all_need_to_run;
 
 122       debug("  Found: " . join(' ', map { $_->package_name } @{ $jobs })) if @{ $jobs };
 
 124       foreach my $job (@{ $jobs }) {
 
 125         # Provide fresh global variables in case legacy code modifies
 
 127         $::locale = Locale->new($::lx_office_conf{system}->{language});
 
 138     debug("Exception during execution: ${EVAL_ERROR}") if !$ok;
 
 141     my $seconds = 60 - (localtime)[0];
 
 143       local $SIG{'ALRM'} = sub {
 
 144         debug("Got woken up by SIGALRM");
 
 147       sleep($seconds < 30 ? $seconds + 60 : $seconds);
 
 150       die $@ unless $@ eq "Alarm!\n";
 
 157 mkdir SL::System::TaskServer::PID_BASE() if !-d SL::System::TaskServer::PID_BASE();
 
 159 my $file = first { -f } ("${exe_dir}/config/kivitendo.conf", "${exe_dir}/config/lx_office.conf", "${exe_dir}/config/kivitendo.conf.default");
 
 161 die "No configuration file found." unless $file;
 
 163 $file = File::Spec->abs2rel(Cwd::abs_path($file), Cwd::abs_path($exe_dir));
 
 165 newdaemon(configfile => $file,
 
 166           progname   => 'kivitendo-background-jobs',
 
 167           pidbase    => SL::System::TaskServer::PID_BASE() . '/',