summaryrefslogtreecommitdiff
path: root/lib/PerlDaemon/PerlDaemon.pl
blob: ac7e4d26f9e929be206c9904a03f063f84e64664 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/perl

# PerlDaemon (c) 2010, 2011, Dipl.-Inform. (FH) Paul Buetow (http://perldaemon.buetow.org)

use strict;
use warnings;

use Shell qw(mv);
use POSIX qw(setsid strftime);
use Time::HiRes qw(gettimeofday tv_interval);

use PerlDaemon::Logger;
use PerlDaemon::RunModules;

$| = 1;

sub trimstr (@) {
  my @str = 
  @_;

  for (@str) {
    chomp;
    s/^[\t\s]+//;
    s/[\t\s]+$//;
  }

  return @str;
}

sub trunc ($) {
  my $file = shift;
  open my $fh, ">$file" or die "Can't write $file: $!\n";
  print $fh '';
  close $fh;
}

sub checkpid ($) {
  my $conf = shift;
  my $pidfile = $conf->{'daemon.pidfile'};
  my $logger = $conf->{logger};

  trunc $pidfile unless -f $pidfile;

  open my $fh, $pidfile or $logger->err("Can't read pidfile $pidfile: $!");
  my ($pid) = <$fh>;
  close $fh;

  if (defined $pid) {
    chomp $pid;
    $logger->err("Process with pid $pid already running") if 0 < int $pid && kill 0, $pid;
  }
}

sub writepid ($) {
  my $conf = shift;
  my $logger = $conf->{logger};

  my $pidfile = $conf->{'daemon.pidfile'};

  open my $fh, ">$pidfile" or $logger->err("Can't write pidfile: $!");
  print $fh "$$\n";
  close $fh;
}


sub readconf ($%) {
  my ($confile, %opts) = @_;
  my $desc;

  open my $fh, $confile or 
  die "Can't read config file $confile (specify using config=filepath)\n";

  my %conf;
  while (<$fh>) {
    if (/^#(.*)/) {
      $desc = $1;
      next;
    }

    next if /^[\t\w]+#/;
    s/#.*//;

    my ($key, $val) = trimstr split '=', $_, 2;
    next unless defined $val;

    $conf{$key} = $val; 

    if (defined $desc) {
      $conf{"$key.desc"} = $desc; 
      $desc = undef;
    }
  }

  close $fh;

  # Check
  my $msg = 'Missing property:';

  foreach (qw(wd loopinterval alivefile pidfile logfile daemonize)) {
    my $key = "daemon.$_";
    die "$msg $key\n" unless exists $conf{$key};
  }

  @conf{keys %opts} = values %opts;
  return \%conf;
}

sub daemonize ($) {
  my $conf = shift;
  my $logger = $conf->{logger};
  $logger->logmsg('Daemonizing...');

  chdir $conf->{'daemon.wd'} or $logger->err("Can't chdir to wd: $!");

  my $msg = 'Can\'t read /dev/null:';

  open STDIN, '>/dev/null' or $logger->err("$msg $!");
  open STDOUT, '>/dev/null' or $logger->err("$msg $!");
  open STDERR, '>/dev/null' or $logger->err("$msg $!");

  defined (my $pid = fork) or $logger->err("Can't fork: $!"); 
  exit if $pid;

  setsid or $logger->err("Can't start a new session: $!");

  writepid $conf;
  $logger->logmsg('Daemonizing completed');
}

sub sighandlers ($) {
  my $conf = shift;
  my $logger = $conf->{logger};

  $SIG{TERM} = sub {
    # On shutdown
    $logger->logmsg('Received SIGTERM. Shutting down....');
    unlink $conf->{'daemon.pidfile'} if -f $conf->{'daemon.pidfile'};
    exit 0;
  };

  $SIG{HUP} = sub {
    # On logrotate
    $logger->logmsg('Received SIGHUP.');
    $logger->rotatelog();
  };
}

sub prestartup ($) {
  my $conf = shift;
  checkpid $conf;
}

sub alive ($) {
  my $conf = shift;
}

sub daemonloop ($) {
  my $conf = shift;
  my $rmodule = PerlDaemon::RunModules->new($conf);
  my $loopinterval = $conf->{'daemon.loopinterval'};

  my $loop = shift;
  my $lastrun = [0,0];

  for (;;) {
    my $now = [gettimeofday];
    my $timediff = tv_interval($lastrun, $now);

    if ($timediff >= $loopinterval) {
      $lastrun = $now;                                
      $rmodule->do();
      alive $conf;
    }

    sleep $loopinterval / 10;
  }
}

sub showkeys ($) {
  my $conf = shift;
  for my $key (grep !/(^keys$)|(^config$)|(\.desc$)/, keys %$conf) {
    print '#' . (exists $conf->{"$key.desc"}
      ?  $conf->{"$key.desc"} 
      : ' Undocumented property');
    print "\n$key=$conf->{$key}\n\n";
  }
}

sub getopts (@) {
  my %opts;

  for my $opt (@_) {
    next unless $opt =~ /=/;
    my ($key, $val) = split '=', $opt, 2;
    $opts{$key} = $val;
  }

  return %opts;
}

my %opts = getopts @ARGV;

my $conf = readconf $opts{config}, %opts;

if (exists $conf->{keys}) {
  showkeys($conf);
  exit 0;
}

$conf->{logger} = PerlDaemon::Logger->new($conf);

prestartup $conf;

if ($conf->{'daemon.daemonize'} ne 'yes') {
  print "Running in foreground...\n";
} else {
  daemonize $conf;
}

sighandlers $conf;
daemonloop $conf;