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

Class: System_Daemon

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

Class Overview


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


Author(s):

Version:

  • SVN: Release: $Id: Daemon.php 200 2009-03-24 10:18:18Z 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 ]

$_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),"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"))

[line 326]

Available signal handlers setSigHandler can overwrite these values individually.

Type:   array


[ Top ]



Method Detail

autoload   [line 361]

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

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

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   — 

[ Top ]

getOption   [line 571]

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

array getOptions( )

Gets an array of options found
  • Access: public

[ Top ]

isDying   [line 840]

boolean isDying( )

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

[ Top ]

isInBackground   [line 829]

boolean isInBackground( )

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

[ Top ]

isRunning   [line 892]

boolean isRunning( )

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

[ Top ]

log   [line 630]

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

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

void phpErrors( $errno, $errstr, $errfile, $errline)

  • Access: public

Parameters:

   $errno   — 
   $errstr   — 
   $errfile   — 
   $errline   — 

[ Top ]

restart   [line 496]

void restart( )

Restart daemon process.

[ Top ]

setOption   [line 537]

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

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

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

boolean start( )

Spawn daemon process.

[ Top ]

stop   [line 482]

void stop( )

Stop daemon process.

[ Top ]

writeAutoRun   [line 739]

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

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

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

boolean _fork( )

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

[ Top ]

_isValidPidLocation   [line 1004]

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

void _mkdirr( $dirPath, $mode)

  • Access: protected

Parameters:

   $dirPath   — 
   $mode   — 

[ Top ]

_optionObjSetup   [line 1253]

boolean _optionObjSetup( )

Sets up Option Object instance
  • Access: protected

[ Top ]

_optionsInit   [line 1278]

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

boolean _osObjSetup( )

Sets up OS instance
  • Access: protected

[ Top ]

_summon   [line 924]

void _summon( )

Put the running script in background
  • Access: protected

[ Top ]

_whatIAm   [line 1184]

string _whatIAm( )

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

[ Top ]

_writePid   [line 1029]

void _writePid( [ $pidFilePath = null], [ $pid = null])

  • Access: protected

Parameters:

   $pidFilePath   — 
   $pid   — 

[ Top ]


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