summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2011-02-05 15:37:04 +0000
committerPaul Buetow <paul@buetow.org>2011-02-05 15:37:04 +0000
commitc5ddf35d2b0558c1638a81ebc5186942f4377a06 (patch)
tree9f09e9e2503e3f60254ee5a837ec40d1fae9acdc
initial minimal perl daemon
-rw-r--r--perldaemon.conf3
-rw-r--r--perldaemon.pl83
2 files changed, 86 insertions, 0 deletions
diff --git a/perldaemon.conf b/perldaemon.conf
new file mode 100644
index 0000000..e780313
--- /dev/null
+++ b/perldaemon.conf
@@ -0,0 +1,3 @@
+# Minimal Perl Daemon Sample Configuration
+daemon.wd = ./
+daemon.pidfile = ./run/perldaemon.pid
diff --git a/perldaemon.pl b/perldaemon.pl
new file mode 100644
index 0000000..858465a
--- /dev/null
+++ b/perldaemon.pl
@@ -0,0 +1,83 @@
+#!/usr/bin/perl
+
+# Minimal Daemon (c) 2011 Paul Buetow
+
+use strict;
+use warnings;
+use POSIX qw(setsid);
+
+sub trimstr (@) {
+ my @str = @_;
+
+ for (@str) {
+ chomp;
+ s/^[\t\s]+//;
+ s/[\t\s]+$//;
+ }
+
+ return @str;
+}
+
+sub readconfig ($) {
+ my $configfile = shift;
+
+ open my $fh, $configfile or die "Can't read $configfile\n";
+ my %config;
+
+ while (<$fh>) {
+ next if /^[\t\w]+#/;
+ s/#.*//;
+
+ my ($key, $val) = trimstr split '=', $_, 2;
+ next unless defined $val;
+
+ $config{$key} = $val;
+ }
+
+ # Check
+ my $msg = 'Missing property:';
+
+ foreach (qw(wd pidfile)) {
+ my $key = "daemon.$_";
+ die "$msg $key\n" unless exists $config{$key};
+ }
+
+ return \%config;
+}
+
+sub daemonize ($) {
+ my $config = shift;
+
+ chdir $config->{wd} or die "Can't chdir to wd: $!\n";
+
+ my $msg = 'Can\'t read /dev/null:';
+
+ open STDIN, '>/dev/null' or die "$msg $!\n";
+ open STDOUT, '>/dev/null' or die "$msg $!\n";
+ open STDERR, '>/dev/null' or die "$msg $!\n";
+
+ defined (my $pid = fork) or die "Can't fork: $!\n";
+ exit if $pid;
+
+ setsid or die "Can't start a new session: $!\n";
+}
+
+sub signals ($) {
+ my $config = shift;
+}
+
+sub daemonloop ($) {
+ my $config = shift;
+
+ for (;;) {
+ sleep 1;
+ }
+}
+
+my $config = readconfig shift;
+
+#daemonize $config;
+signals $config;
+daemonloop $config;
+
+