Monday, March 26, 2012

Creating a perl daemon

Source: http://www.perlmonks.org/?node_id=374409

It's a Good Thing to go through the details of exactly what to do, and why, to run as a daemon. A daemon process is one which runs forever without a controlling terminal or parent process. There are some housekeeping issues to be taken care of in launching a daemon. Stevens' Advanced Programming in the UNIX Environment provides a a list of coding rules and a C function for that which is readily translated to perl.

We'll need the POSIX system call setsid() and we'll also import Cwd's chdir to get $ENV{PWD} reset when we change directory. We define a function daemon_init() which does all the daemonic housekeeping.

use POSIX 'setsid';
use Cwd 'chdir';

sub daemon_init {
First, we fork and exit the parent. That makes the launching process think we're done and also makes sure we're not a process group leader - a necessary condition for the next step to succeed.
    my $pid = fork;
    exit 0 if $pid;
    exit 1 if not defined $pid;
Second, we call setsid() which does three things. We become leader of a new session, group leader of a new process group, and become detached from any terminal. To satisfy SVR4, we do the fork dance again to shuck session leadership, which guarantees we never get a controlling terminal.
    setsid();
    $pid = fork;
    exit 0 if $pid;
    exit 1 if not defined $pid;
Third, we change directory to the root of all. That is a courtesy to the system, which without it would be prevented from unmounting the filesystem we started in.     chdir '/' or die $!;
Fourth, we clear the permissions mask for file creation. That frees our daemon to manage its files as it sees fit.     umask 0;
Finally, we close unwanted filehandles we have inherited from the parent process. That is another courtesy to the system, as well as being sparing of our own process resources. I'll take them from arguments passed to daemon_init(), just to be different from Proc::Daemon.
    close $_ for @_;
}
The only reason to do this by hand, as written, instead of through Proc::Daemon is if you do have file handles already open which the daemon will use. Proc::Daemon closes 'em all.
It is conventional to rewire the SIGHUP handler of a daemon to reinitialize and reload configuration files. If you set $SIG{HUP} = \&_init; for some suitably defined _init() before you fork, the initialization will be done automatically. You can repeat initialization at any time ( like after editing configuration files) with kill HUP, pid.

No comments: