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

Class: System_Daemon

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

Class Overview


System_Daemon. Create daemons with practicle functions like System_Daemon::start()


Author(s):

Version:

  • SVN: Release: $Id: Daemon.php 235 2009-09-02 16:03:27Z 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 System_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 ]

$_logPhpMapping = array(
        E_ERROR => self::LOG_ERR,
        E_WARNING => self::LOG_WARNING,
        E_PARSE => self::LOG_EMERG,
        E_NOTICE => self::LOG_DEBUG,
        E_CORE_ERROR => self::LOG_EMERG,
        E_CORE_WARNING => self::LOG_WARNING,
        E_COMPILE_ERROR => self::LOG_EMERG,
        E_COMPILE_WARNING => self::LOG_WARNING,
        E_USER_ERROR => self::LOG_ERR,
        E_USER_WARNING => self::LOG_WARNING,
        E_USER_NOTICE => self::LOG_DEBUG,
        E_RECOVERABLE_ERROR => self::LOG_CRIT,
        'E_DEPRECATED' => self::LOG_NOTICE,
        'E_USER_DEPRECATED' => self::LOG_NOTICE,
    )

[line 139]

Available PHP error levels and their meaning in POSIX loglevel terms Some ERROR constants are not supported in all PHP versions and will conditionally be translated from strings to constants, or else: removed from this mapping at start().
  • 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),"logPhpErrors"=>array("type"=>"boolean","default"=>true,"punch"=>"Reroute PHP errors to log function","detail"=>"","required"=>true),"logFilePosition"=>array("type"=>"boolean","default"=>false,"punch"=>"Show file in which the log message was generated","detail"=>"","required"=>true),"logLinePosition"=>array("type"=>"boolean","default"=>true,"punch"=>"Show the line number in which the log message was generated","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/unix_filepath","default"=>"/var/run/{OPTIONS.appName}/{OPTIONS.appName}.pid","punch"=>"The pid filepath","example"=>"/var/run/logparser/logparser.pid","detail"=>"","required"=>true),"appChkConfig"=>array("type"=>"string","default"=>"- 99 0","punch"=>"chkconfig parameters for init.d","detail"=>"runlevel startpriority stoppriority"),"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"),"runTemplateLocation"=>array("type"=>"string/existing_filepath","default"=>false,"punch"=>"The filepath to a custom autorun Template","example"=>"/etc/init.d/skeleton","detail"=>"Sometimes it's better to stick with the OS default,
                and use something like /etc/default/<name> for customization",),)

[line 177]

Definitions for all Options

Type:   array


[ Top ]

$_optObj =  false

[line 161]

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

Type:   mixed


[ Top ]

$_osObj =  false

[line 168]

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"),SIGPIPE=>SIG_IGN,)

[line 347]

Available signal handlers setSigHandler can overwrite these values individually.

Type:   array


[ Top ]



Method Detail

autoload   [line 383]

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 859]

void defaultSigHandler( integer $signo)

Default signal handler.

You can overrule various signals with the setSigHandler() method


Parameters:

integer   $signo   —  The posix signal received.

[ Top ]

fileExistsInPath   [line 929]

boolean fileExistsInPath( string $file)

file_exists does not check the include paths. This function does.

It was not written by me, I don't know where it's from exactly. Let me know if you do.

From kvzlib.net

  • Access: public

Parameters:

string   $file   —  Filename to test

[ Top ]

getOption   [line 625]

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 639]

array getOptions( )

Gets an array of options found
  • Access: public

[ Top ]

isDying   [line 913]

boolean isDying( )

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

[ Top ]

isInBackground   [line 902]

boolean isInBackground( )

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

[ Top ]

isRunning   [line 966]

boolean isRunning( )

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

[ Top ]

iterate   [line 519]

void iterate( [integer $sleepSeconds = 0])

Protects your daemon by e.g. clearing statcache. Can optionally be used as a replacement for sleep as well.

Parameters:

integer   $sleepSeconds   —  Optionally put your daemon to rest for X s.

[ Top ]

log   [line 704]

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 ]

__construct (Constructor)   [line 367]

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 ]

phpErrors   [line 658]

boolean phpErrors( integer $errno, string $errstr, string $errfile, integer $errline)

Catches PHP Errors and forwards them to log function
  • Access: public

Parameters:

integer   $errno   —  Level
string   $errstr   —  Error
string   $errfile   —  File
integer   $errline   —  Line

[ Top ]

restart   [line 550]

void restart( )

Restart daemon process.

[ Top ]

setOption   [line 591]

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 608]

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 567]

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 416]

boolean start( )

Spawn daemon process.

[ Top ]

stop   [line 536]

void stop( )

Stop daemon process.

[ Top ]

writeAutoRun   [line 813]

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 ]

_changeIdentity   [line 1186]

boolean _changeIdentity( [integer $gid = 0], [integer $uid = 0])

Change identity of process & resources if needed.
  • Access: protected

Parameters:

integer   $gid   —  Group identifier (number)
integer   $uid   —  User identifier (number)

[ Top ]

_die   [line 1300]

void _die( [boolean $restart = false])

Sytem_Daemon::_die() Kill the daemon Keep this function as independent from complex logic as possible
  • Access: protected

Parameters:

boolean   $restart   —  Whether to restart after die

[ Top ]

_fork   [line 1253]

boolean _fork( )

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

[ Top ]

_isValidPidLocation   [line 1087]

boolean _isValidPidLocation( string $pidFilePath, [boolean $log = true])

Determine whether pidfilelocation is valid
  • Access: protected

Parameters:

string   $pidFilePath   —  Pid location
boolean   $log   —  Allow this function to log directly on error

[ Top ]

_mkdirr   [line 1172]

boolean _mkdirr( string $dirPath, integer $mode)

Recursive alternative to mkdir
  • Access: protected

Parameters:

string   $dirPath   —  Directory to create
integer   $mode   —  Umask

[ Top ]

_optionObjSetup   [line 1356]

boolean _optionObjSetup( )

Sets up Option Object instance
  • Access: protected

[ Top ]

_optionsInit   [line 1381]

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 1335]

boolean _osObjSetup( )

Sets up OS instance
  • Access: protected

[ Top ]

_summon   [line 998]

void _summon( )

Put the running script in background
  • Access: protected

[ Top ]

_whatIAm   [line 1286]

string _whatIAm( )

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

[ Top ]

_writePid   [line 1121]

boolean _writePid( [string $pidFilePath = null], [integer $pid = null])

Creates pid dir and writes process id to pid file
  • Access: protected

Parameters:

string   $pidFilePath   —  PID File path
integer   $pid   —  PID

[ Top ]


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