Merge branch 'master' of vc.linet-services.de:public/lx-office-erp
[kivitendo-erp.git] / scripts / task_server.pl
1 #!/usr/bin/perl
2
3 use strict;
4
5 BEGIN {
6   require Cwd;
7
8   my $dir =  $0;
9   $dir    =  Cwd::getcwd() . '/' . $dir unless $dir =~ m|^/|;
10   $dir    =~ s|[^/]+$|..|;
11
12   chdir($dir) || die "Cannot change directory to ${dir}\n";
13
14   unshift @INC, "modules/override"; # Use our own versions of various modules (e.g. YAML).
15   push    @INC, "modules/fallback"; # Only use our own versions of modules if there's no system version.
16 }
17
18 use CGI qw( -no_xhtml);
19 use Cwd;
20 use Daemon::Generic;
21 use Data::Dumper;
22 use DateTime;
23 use English qw(-no_match_vars);
24 use POSIX qw(setuid setgid);
25 use SL::Auth;
26 use SL::DB::BackgroundJob;
27 use SL::BackgroundJob::ALL;
28 use SL::Form;
29 use SL::Helper::DateTime;
30 use SL::InstanceConfiguration;
31 use SL::LXDebug;
32 use SL::LxOfficeConf;
33 use SL::Locale;
34
35 our %lx_office_conf;
36
37 sub lxinit {
38   my $login = $lx_office_conf{task_server}->{login};
39
40   package main;
41
42   $::lxdebug       = LXDebug->new;
43   $::locale        = Locale->new($::lx_office_conf{system}->{language});
44   $::form          = Form->new;
45   $::auth          = SL::Auth->new;
46   $::instance_conf = SL::InstanceConfiguration->new;
47   $::request       = { cgi => CGI->new({}) };
48
49   die 'cannot reach auth db'               unless $::auth->session_tables_present;
50
51   $::auth->restore_session;
52
53   require "bin/mozilla/common.pl";
54
55   die "cannot find user $login"            unless %::myconfig = $::auth->read_user(login => $login);
56   die "cannot find locale for user $login" unless $::locale   = Locale->new('de');
57 }
58
59 sub drop_privileges {
60   my $user = $lx_office_conf{task_server}->{run_as};
61   return unless $user;
62
63   my ($uid, $gid);
64   while (my @details = getpwent()) {
65     next unless $details[0] eq $user;
66     ($uid, $gid) = @details[2, 3];
67     last;
68   }
69   endpwent();
70
71   if (!$uid) {
72     print "Error: Cannot drop privileges to ${user}: user does not exist\n";
73     exit 1;
74   }
75
76   if (!setgid($gid)) {
77     print "Error: Cannot drop group privileges to ${user} (group ID $gid): $!\n";
78     exit 1;
79   }
80
81   if (!setuid($uid)) {
82     print "Error: Cannot drop user privileges to ${user} (user ID $uid): $!\n";
83     exit 1;
84   }
85 }
86
87 sub gd_preconfig {
88   my $self = shift;
89
90   SL::LxOfficeConf->read($self->{configfile});
91
92   die "Missing section [task_server] in config file"                unless $lx_office_conf{task_server};
93   die "Missing key 'login' in section [task_server] in config file" unless $lx_office_conf{task_server}->{login};
94
95   drop_privileges();
96   lxinit();
97
98   return ();
99 }
100
101 sub gd_run {
102   while (1) {
103     my $ok = eval {
104       $::lxdebug->message(0, "Retrieving jobs") if $lx_office_conf{task_server}->{debug};
105
106       my $jobs = SL::DB::Manager::BackgroundJob->get_all_need_to_run;
107
108       $::lxdebug->message(0, "  Found: " . join(' ', map { $_->package_name } @{ $jobs })) if $lx_office_conf{task_server}->{debug} && @{ $jobs };
109
110       foreach my $job (@{ $jobs }) {
111         # Provide fresh global variables in case legacy code modifies
112         # them somehow.
113         $::locale = Locale->new($::lx_office_conf{system}->{language});
114         $::form   = Form->new;
115
116         $job->run;
117       }
118
119       1;
120     };
121
122     if ($lx_office_conf{task_server}->{debug}) {
123       $::lxdebug->message(0, "Exception during execution: ${EVAL_ERROR}") if !$ok;
124       $::lxdebug->message(0, "Sleeping");
125     }
126
127     my $seconds = 60 - (localtime)[0];
128     if (!eval {
129       local $SIG{'ALRM'} = sub {
130         $::lxdebug->message(0, "Got woken up by SIGALRM") if $lx_office_conf{task_server}->{debug};
131         die "Alarm!\n"
132       };
133       sleep($seconds < 30 ? $seconds + 60 : $seconds);
134       1;
135     }) {
136       die $@ unless $@ eq "Alarm!\n";
137     }
138   }
139 }
140
141 my $cwd     = getcwd();
142 my $pidbase = "${cwd}/users/pid";
143
144 mkdir($pidbase) if !-d $pidbase;
145
146 my $file = -f "${cwd}/config/lx_office.conf" ? "${cwd}/config/lx_office.conf" : "${cwd}/config/lx_office.conf.default";
147 newdaemon(configfile => $file,
148           progname   => 'kivitendo-task-server',
149           pidbase    => "${pidbase}/",
150           );
151
152 1;