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

Class: System_Daemon

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

Class Overview


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


Author(s):

Version:

  • SVN: Release: $Id: Daemon.php 219 2009-04-25 10:38:23Z 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
    )

[line 136]

Available PHP error levels and their meaning in POSIX loglevel terms
  • 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"),)

[line 172]

Definitions for all Options

Type:   array


[ Top ]

$_optObj =  false

[line 156]

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

Type:   mixed


[ Top ]

$_osObj =  false

[line 163]

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

Available signal handlers setSigHandler can overwrite these values individually.

Type:   array


[ Top ]



Method Detail

autoload   [line 368]

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

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

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

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

array getOptions( )

Gets an array of options found
  • Access: public

[ Top ]

isDying   [line 887]

boolean isDying( )

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

[ Top ]

isInBackground   [line 876]

boolean isInBackground( )

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

[ Top ]

isRunning   [line 940]

boolean isRunning( )

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

[ Top ]

iterate   [line 494]

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

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

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

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

void restart( )

Restart daemon process.

[ Top ]

setOption   [line 565]

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

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

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

boolean start( )

Spawn daemon process.

[ Top ]

stop   [line 510]

void stop( )

Stop daemon process.

[ Top ]

writeAutoRun   [line 786]

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

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

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

boolean _fork( )

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

[ Top ]

_isValidPidLocation   [line 1055]

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

boolean _mkdirr( string $dirPath, integer $mode)

Recursive alternative to mkdir
  • Access: protected

Parameters:

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

[ Top ]

_optionObjSetup   [line 1324]

boolean _optionObjSetup( )

Sets up Option Object instance
  • Access: protected

[ Top ]

_optionsInit   [line 1349]

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

boolean _osObjSetup( )

Sets up OS instance
  • Access: protected

[ Top ]

_summon   [line 972]

void _summon( )

Put the running script in background
  • Access: protected

[ Top ]

_whatIAm   [line 1254]

string _whatIAm( )

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

[ Top ]

_writePid   [line 1089]

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:31:26 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.