The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

PTools::Proc::Daemonize - Turn a script into a daemon process

VERSION

This document describes version 0.09, released April, 2006.

SYNOPSIS

    use PTools::Proc::Daemonize;

 or use PTools::Proc::Daemonize qw( LoadEtcPath );

    run PTools::Proc::Daemonize();

 or run PTools::Proc::Daemonize( Debug, UID, GID, WorkingDir, Umask, EvListRef, PATH, OutFile, PidFile );

 or $daemon = new PTools::Proc::Daemonize;

    $daemon->verifyProcess( Debug, PidFile );
    die $daemon->err()  if $daemon->stat();     # Exit if a daemon is running

    $daemon->verifyUserGroup( Debug, UID, GID );

    $daemon->changeWorkingDir( Debug, WorkingDir );

    $daemon->setUmask( Debug, Umask );

    $daemon->untaintEnv( Debug, EvListRef );

    $daemon->resetPath( Debug, PATH );

    $daemon->redirectIO( Debug, OutFile );      # Skipped when Debug true

    $daemon->detachSession( Debug );            # Skipped when Debug true

    $daemon->writePidToFile( Debug, PidFile );

The following methods are provided for convenience when running Perl scripts that use the '-T' switch (highly recommended for scripts that will become daemon processes).

    $allow = $daemon->allowedChars();

    $text = $daemon->untaintString( $text, $allow );

DESCRIPTION

This class simplifies all of the tasks necessary to turn a Perl script into a daemon process. Tasks performed can include any or all of the following.

 .  verifying the current Uid and/or Gid
 .  changing the current working directory
 .  setting a new file/dir creation Umask value
 .  cleaning/untainting the runtime environment
 .  resetting the PATH environment variable
 .  redirecting standard IO file descriptors
 .  detaching process from a terminal
 .  writing the new PID to a log file

When used with a 'LoadEtcPath' parameter, the 'untaintEnvironment' method will process the '/etc/PATH' file as done in '/etc/profile'.

Constructor

run ( Debug, UID, GID, WorkingDir, Umask, EvListRef, PATH, OutFile, PidFile )

The run method can be used to accomplish any or all of the necessary tasks described in the Description section, above. As an alternative, each of the specific tasks can be accomplished separately, by making explicit calls to each of the various methods described below. Each of the arguments described here are the same for the various methods.

Note that 'runAs' is a synonymous name for this method, and all arguments to the methods are optional.

Also note that when using the run method there is no default for the WorkingDir argument. However, when explicitly calling the changeWorkingDir method, described below, the / (system root) directory is the default. And, when using a PidFile for a given daemon, the run method will verify the contents of the named file does not match an active process ID (if so, an error condition is set).

Debug

Any non-zero value enables Debug mode. When set, the current standard IO is not redirected, and the session is not detached from a controlling terminal.

UID

A Unix account User Id, expected to be an integer between 0 and 65535.

GID

A Unix account Group Id, expected to be an integer between 0 and 65535.

WorkingDir

A path that will become the script's new Working Directory. Default is '/'.

Umask

A numeric value that will become the script's new file creation Umask value. Default is '0'.

EvListRef

This parameter must be a reference to a list of environment variable names. Any listed EV names will not be removed from the script's environment.

Path

A colon separated string of directory paths that will become the script's new Search Path. Default is '/usr/bin:/usr/sbin'.

Outfile

A path that will become the script's new STDOUT and STDERR output file. Default is '/dev/null'.

PidFile

A path to which the script's new <Process ID> will be written. Default is to not save the new PID.

Methods

verifyProcess ( [ Debug ], PidFile )

Verify that the process identification number contained in the named PidFile does not match a currently active process. If the 'pid' does match, an error condition is set.

Argument values are the same as described for the run method, above.

verifyUserGroup ( [ Debug ] [, UID ] [, GID ] )

Verify that the script is currently running as one or both of the specified UserID or GroupID.

Argument values are the same as described for the run method, above.

changeWorkingDir ( [ Debug [, WorkingDir ] )

Change the script's current working directory to a new WorkingDir path.

Argument values are the same as described for the run method, above.

setUmask ( [ Debug ] [, Umask] )

Set a new file and directory creation umask value

Argument values are the same as described for the run method, above.

untaintEnv ( [ Debug ] [, EvListRef ])

Clean up the script's current environment settings.

An optional EvListRef argument can be passed to retain named Environment Variables. This argument is expected to be a reference to an array of variable names.

Argument values are the same as described for the run method, above.

resetPath ( [ Debug ] [, PATH ] )

Set a new value for the PATH environment variable.

When this class is used with the '/etc/path' parameter, this method will process the 'etc/PATH' file as done in '/etc/profile'.

Argument values are the same as described for the run method, above.

redirectIO ( [ Debug ] [, OutFile ] )

Redirect the script's standard IO, including STDIN, STDOUT and STDERR to a new OutFile. Redirection is skipped when Debug true.

Argument values are the same as described for the run method, above.

detachSession ( [ Debug ] )

The script is not detached from the session when when Debug true.

Argument value is the same as described for the run method, above.

writePidToFile ( [ Debug ] [, PidFile ] )

After the current process is forked into a new sesison, write the new Process ID into the specified PidFile.

Argument values are the same as described for the run method, above.

untaintString ( Text [, AllowedChars ] )
untaintText ( Text [, AllowedChars ] )

This method is provided for convenience when running a Perl script with the '-T' switch. It is highly recommended that a script which will become a daemon process be run in this manner. This method simplifies the process of 'untainting' Perl variables when necessary.

Text

This parameter is a 'tainted' Perl string.

AllowedChars

This optional parameter is a string of allowed characters that are retained in the Text string during the 'untainting' process. This defaults to the value returned by the allowedChars method.

Example:

 $text = $daemon->untaintString( $text );
allowedChars ()
untaintChars ()

This method returns the default string of characters that are allowed (i.e., 'retained') in strings that are 'untainted' using the untaintString method. This list includes those characters identified in The WWW Security FAQ with the addition of the space character (http://www.w3.org/Security/Faq/www-security-faq.html).

 - a-zA-Z0-9_.@

This includes the characters dash, space, alpha-numerics, underscore, dot or period, and the commercial 'at' symbol.

Example:

 $allow = $daemon->allowedChars();

 $text = $daemon->untaintString( $text, $allow );
allChars
dangerousChars

This method returns all typable 'non-control' characters including tab and newline. The result can then be used with the untaintString method, above, to untaint a text (scalar) variable that came from a 'reasonably secure' source. For example, loading configuration or template data from an external file into a 'tainted' variable.

 - a-zA-Z0-9_.@~`'":;?!@#$%^&*()+=,<>{}[]|\\t\\n\\

Warning: Under NO circumstances should this list of characters be used to untaint text entered by the user of a particular script or Web form. Ignoring this warning might result in unauthorized access to your computer system.

Example:

 $danger = $daemon->dangerousChars();

 $text = $daemon->untaintString( $text, $danger );

INHERITANCE

None currently.

SEE ALSO

See POSIX and Fcntl.

AUTHOR

Chris Cobb [no dot spam at ccobb dot net]

COPYRIGHT

Copyright (c) 2004-2007 by Chris Cobb. All rights reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.