System_Daemon
[ class tree: System_Daemon ] [ index: System_Daemon ] [ all elements ]

Class: System_Daemon

Source Location: /System_Daemon-0.6.0/System/Daemon.php

Class Overview


System_Daemon. Create daemons with practicle functions like $daemon->start()


Author(s):

Version:

  • SVN: Release: $Id: Daemon.php 167 2009-02-08 20:25:07Z kevin $

Copyright:

  • 2008 Kevin van Zonneveld (http://kevin.vanzonneveld.net)

Variables

Methods


Inherited Variables

Inherited Methods


Class Details

[line 39]
System_Daemon. Create daemons with practicle functions like $daemon->start()

Requires PHP build with --enable-cli --with-pcntl. Only runs on *NIX systems, because Windows lacks of the pcntl ext.

PHP version 5



[ Top ]


Class Variables

$_isDying =  false

[line 98]

Wether the our daemon is being killed
  • Access: protected

Type:   boolean


[ Top ]

$_logLevels = array(
        self::LOG_EMERG => "emerg",
        self::LOG_ALERT => "alert",
        self::LOG_CRIT => "crit",
        self::LOG_ERR => "err",
        self::LOG_WARNING => "warning",
        self::LOG_NOTICE => "notice",
        self::LOG_INFO => "info",
        self::LOG_DEBUG => "debug"        
    )

[line 120]

Available log levels
  • Access: protected

Type:   array


[ Top ]

$_optionDefinitions = array(
        "usePEAR" => array(
            "type" => "boolean",
            "default" => true,
            "punch" => "Wether to run this class using PEAR",
            "detail" => "Will run standalone when false",
            "required" => true 
        ),"usePEARLogInstance"=>array("type"=>"boolean|object","default"=>false,"punch"=>"Accepts a PEAR_Log instance to handle all logging","detail"=>"This will replace System_Daemon's own logging facility","required"=>true),"authorName"=>array("type"=>"string/0-50","punch"=>"Author name","example"=>"Kevin van zonneveld","detail"=>"Required for forging init.d script"),"authorEmail"=>array("type"=>"string/email","punch"=>"Author e-mail","example"=>"kevin@vanzonneveld.net","detail"=>"Required for forging init.d script"),"appName"=>array("type"=>"string/unix","punch"=>"The application name","example"=>"logparser","detail"=>"Must be UNIX-proof; Required for running daemon","required"=>true),"appDescription"=>array("type"=>"string","punch"=>"Daemon description","example"=>"Parses logfiles of vsftpd and stores them in MySQL","detail"=>"Required for forging init.d script"),"appDir"=>array("type"=>"string/existing_dirpath","default"=>"@dirname({SERVER.SCRIPT_NAME})","punch"=>"The home directory of the daemon","example"=>"/usr/local/logparser","detail"=>"Highly recommended to set this yourself","required"=>true),"appExecutable"=>array("type"=>"string/existing_filepath","default"=>"@basename({SERVER.SCRIPT_NAME})","punch"=>"The executable daemon file","example"=>"logparser.php","detail"=>"Recommended to set this yourself; Required for init.d","required"=>true),"logVerbosity"=>array("type"=>"number/0-7","default"=>self::LOG_INFO,"punch"=>"Messages below this log level are ignored","example"=>"","detail"=>"Not written to logfile; not displayed on screen","required"=>true),"logLocation"=>array("type"=>"string/creatable_filepath","default"=>"/var/log/{OPTIONS.appName}.log","punch"=>"The log filepath","example"=>"/var/log/logparser_daemon.log","detail"=>"","required"=>true),"appRunAsUID"=>array("type"=>"number/0-65000","default"=>0,"punch"=>"The user id under which to run the process","example"=>"1000","detail"=>"Defaults to root which is insecure!","required"=>true),"appRunAsGID"=>array("type"=>"number/0-65000","default"=>0,"punch"=>"The group id under which to run the process","example"=>"1000","detail"=>"Defaults to root which is insecure!","required"=>true),"appPidLocation"=>array("type"=>"string/creatable_filepath","default"=>"/var/run/{OPTIONS.appName}.pid","punch"=>"The pid filepath","example"=>"/var/run/logparser.pid","detail"=>"","required"=>true),"appDieOnIdentityCrisis"=>array("type"=>"boolean","default"=>true,"punch"=>"Kill daemon if it cannot assume the identity","detail"=>"","required"=>true),"sysMaxExecutionTime"=>array("type"=>"number","default"=>0,"punch"=>"Maximum execution time of each script in seconds","detail"=>"0 is infinite"),"sysMaxInputTime"=>array("type"=>"number","default"=>0,"punch"=>"Maximum time to spend parsing request data","detail"=>"0 is infinite"),"sysMemoryLimit"=>array("type"=>"string","default"=>"128M","punch"=>"Maximum amount of memory a script may consume","detail"=>"0 is infinite"),)

[line 152]

Definitions for all Options

Type:   array


[ Top ]

$_optObj =  false

[line 136]

Holds Option Object
  • Var: object or boolean
  • Access: protected

Type:   mixed


[ Top ]

$_osObj =  false

[line 143]

Holds OS Object
  • Var: object or boolean
  • Access: protected

Type:   mixed


[ Top ]

$_processId =  0

[line 91]

The current process identifier
  • Access: protected

Type:   integer


[ Top ]

$_processIsChild =  false

[line 105]

Wether the current process is a forked child
  • Access: protected

Type:   boolean


[ Top ]

$_safeMode =  false

[line 113]

Wether SAFE_MODE is on or off. This is important for ini_set behavior
  • Access: protected

Type:   boolean


[ Top ]

$_sigHandlers = array(
        SIGCONT => array("System_Daemon", "defaultSigHandler"),SIGALRM=>array("System_Daemon","defaultSigHandler"),SIGINT=>array("System_Daemon","defaultSigHandler"),SIGABRT=>array("System_Daemon","defaultSigHandler"),SIGTERM=>array("System_Daemon","defaultSigHandler"),SIGHUP=>array("System_Daemon","defaultSigHandler"),SIGUSR1=>array("System_Daemon","defaultSigHandler"),SIGCHLD=>array("System_Daemon","defaultSigHandler"))

[line 286]

Available signal handlers setSigHandler can overwrite these values individually.

Type:   array


[ Top ]



Method Detail

__construct (Constructor)   [line 305]

System_Daemon __construct( )

Making the class non-abstract with a protected constructor does a better job of preventing instantiation than just marking the class as abstract.

[ Top ]

autoload   [line 321]

void autoload( string $className)

Autoload static method for loading classes and interfaces.

Code from the PHP_CodeSniffer package by Greg Sherwood and Marc McIntyre

  • Access: public

Parameters:

string   $className   —  The name of the class or interface.

[ Top ]

defaultSigHandler   [line 725]

void defaultSigHandler( integer $signo)

Default signal handler.

You can overrule various signals with the setSigHandler() method


Parameters:

integer   $signo   —  The posix signal received.

[ Top ]

getOption   [line 528]

mixed getOption( string $name)

Gets any option found in $_optionDefinitions Public interface to talk with with protected option methods
  • Access: public

Parameters:

string   $name   —  Name of the Option

[ Top ]

getOptions   [line 542]

array getOptions( )

Gets an array of options found
  • Access: public

[ Top ]

isDying   [line 779]

boolean isDying( )

Wether the our daemon is being killed, you might want to include this in your loop
  • Access: public

[ Top ]

isInBackground   [line 768]

boolean isInBackground( )

Wether the class is already running in the background
  • Access: public

[ Top ]

isRunning   [line 789]

boolean isRunning( )

Check if a previous process with same pidfile was already running
  • Access: public

[ Top ]

log   [line 575]

boolean log( integer $level, string $str, [string $file = false], [string $class = false], [string $function = false], [integer $line = false])

Almost every deamon requires a log file, this function can facilitate that. Also handles class-generated errors, chooses either PEAR handling or PEAR-independant handling, depending on: self::getOption("usePEAR").

Also supports PEAR_Log if you referenc to a valid instance of it in self::getOption("usePEARLogInstance").

It logs a string according to error levels specified in array: self::$_logLevels (0 is fatal and handles daemon's death)


Parameters:

integer   $level   —  What function the log record is from
string   $str   —  The log record
string   $file   —  What code file the log record is from
string   $class   —  What class the log record is from
string   $function   —  What function the log record is from
integer   $line   —  What code line the log record is from

[ Top ]

restart   [line 453]

void restart( )

Restart daemon process.

[ Top ]

setOption   [line 494]

boolean setOption( string $name, mixed $value)

Sets any option found in $_optionDefinitions Public interface to talk with with protected option methods
  • Access: public

Parameters:

string   $name   —  Name of the Option
mixed   $value   —  Value of the Option

[ Top ]

setOptions   [line 511]

boolean setOptions( array $use_options)

Sets an array of options found in $_optionDefinitions Public interface to talk with with protected option methods
  • Access: public

Parameters:

array   $use_options   —  Array with Options

[ Top ]

setSigHandler   [line 470]

boolean setSigHandler( string $signal, mixed $handler)

Overrule or add signal handlers.

Parameters:

string   $signal   —  Signal constant (e.g. SIGHUP)
mixed   $handler   —  Which handler to call on signal

[ Top ]

start   [line 353]

boolean start( )

Spawn daemon process.

[ Top ]

stop   [line 439]

void stop( )

Stop daemon process.

[ Top ]

writeAutoRun   [line 679]

boolean writeAutoRun( [boolean $overwrite = false])

Uses OS class to write an: 'init.d' script on the filesystem
  • Access: public

Parameters:

boolean   $overwrite   —  May the existing init.d file be overwritten?

[ Top ]

_die   [line 966]

void _die( [boolean $restart = false])

Sytem_Daemon::_die() Kill the daemon
  • Access: protected

Parameters:

boolean   $restart   —  Whether to restart after die

[ Top ]

_fork   [line 920]

boolean _fork( )

Fork process and kill parent process, the heart of the 'daemonization'
  • Access: protected

[ Top ]

_optionObjSetup   [line 1016]

boolean _optionObjSetup( )

Sets up Option Object instance
  • Access: protected

[ Top ]

_optionsInit   [line 1041]

mixed _optionsInit( [boolean $premature = false])

Checks if all the required options are set.

Initializes, sanitizes & defaults unset variables

  • Return: integer or boolean
  • Access: protected

Parameters:

boolean   $premature   —  Whether to do a premature option init

[ Top ]

_osObjSetup   [line 995]

boolean _osObjSetup( )

Sets up OS instance
  • Access: protected

[ Top ]

_summon   [line 821]

void _summon( )

Put the running script in background
  • Access: protected

[ Top ]

_whatIAm   [line 953]

string _whatIAm( )

Return what the current process is: child or parent
  • Access: protected

[ Top ]


Documentation generated on Mon, 11 Mar 2019 15:29:03 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.