summaryrefslogtreecommitdiff
path: root/perldaemon.pl
blob: 2873c85c2334f9c9ca2625fead2caf659e75cac5 (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
#!/usr/bin/perl

# Minimal Daemon (c) 2011 Paul Buetow

use strict;
use warnings;
use POSIX qw(setsid);

$| = 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 logmsg ($$) {
	my ($config, $msg) = @_;
	my $logfile = $config->{'daemon.logfile'};

	open my $fh, ">>$logfile" or die "Can't write logfile $logfile: $!\n";
	print $fh localtime()." (PID $$): $msg\n";
	close $fh;
}

sub err ($$) {
	my ($config, $msg) = @_;
	logmsg $config => $msg;	
	die "$msg\n";
}

sub checkpid ($) {
	my $config = shift;

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

	trunc $pidfile unless -f $pidfile;

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

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

sub writepid ($) {
	my $config = shift;

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

	open my $fh, ">$pidfile" or err $config => "Can't write pidfile: $!";
	print $fh "$$\n";
	close $fh;
}


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;	
	}

	close $fh;

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

	foreach (qw(wd pidfile logfile truncatelog version)) {
		my $key = "daemon.$_";
		die "$msg $key\n" unless exists $config{$key};
	}

	logmsg \%config => "Reading $configfile complete";
	return \%config;
}

sub daemonize ($) {
	my $config = shift;
	logmsg $config => 'Daemonizing...';

	chdir $config->{'daemon.wd'} or err $config => "Can't chdir to wd: $!";

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

	open STDIN, '>/dev/null' or err $config => "$msg $!";
	open STDOUT, '>/dev/null' or err $config => "$msg $!";
	open STDERR, '>/dev/null' or err $config => "$msg $!";

	defined (my $pid = fork) or err $config => "Can't fork: $!";	
	exit if $pid;
	
	setsid or err $config => "Can't start a new session: $!";

	writepid $config;

	logmsg $config => 'Daemonizing completed';
}

sub prestartup ($) {
	my $config = shift;
	checkpid $config;

	trunc $config->{'daemon.logfile'} if $config->{'daemon.truncatelog'} eq 'yes';
}

sub daemonloop ($) {
	my $config = shift;

	my $loop = shift;
	for (my $i = 1;;++$i) {
		logmsg $config => $config->{'daemon.version'} .  ": Hello $i";
		sleep 3;
	}
}

my $config = readconfig shift;

prestartup $config;
daemonize $config;
daemonloop $config;