diff --git a/README b/README index c878d38..2a78673 100644 --- a/README +++ b/README @@ -10,7 +10,9 @@ Copy config.ini.example to config.ini. Edit config.ini The [php] section can be filled with options to be set by ini_set automagically. -The [server] section customizes things like address to listen for connections. +The [server] section customizes things like address to listen for connections: + * listen: address:port + * pid: Absolute path to where to write the pid file of the server The [application] section contains some very specific options related to PAGI: * log4php: Absolute path to log4php.properties file to be used by your PAGI app. * bootstrap: Absolute path to be required prior to executing your PAGI app (typically diff --git a/config.ini.example b/config.ini.example index c1e1004..2fe768e 100644 --- a/config.ini.example +++ b/config.ini.example @@ -1,5 +1,6 @@ [server] listen=0.0.0.0:7890 +pid=/tmp/fastagi.pid [php] display_errors=1 diff --git a/fastagi.php b/fastagi.php index 5c566f5..9038e05 100644 --- a/fastagi.php +++ b/fastagi.php @@ -31,6 +31,7 @@ function signalHandler($signal) { global $running; global $children; + global $pidFile; switch ($signal) { case SIGINT: case SIGQUIT: @@ -44,6 +45,7 @@ function signalHandler($signal) pcntl_waitpid($pid, $status); unset($children[$pid]); } + @unlink($pidFile); break; case SIGCHLD: $pid = pcntl_waitpid(-1, $status); @@ -60,6 +62,7 @@ function signalHandler($signal) $socket = false; $retCode = 0; $children = array(); +$pidFile = ''; try { @@ -91,6 +94,21 @@ function signalHandler($signal) throw new \Exception("Could not parse config file: $configFile"); } + // Check if pidfile already exists. + if (!isset($config['server']['pid'])) { + throw new \Exception('Missing server.pid configuration'); + } + $pidFile = $config['server']['pid']; + if (file_exists($pidFile)) { + $pid = @file_get_contents($pidFile); + throw new \Exception("$pidFile already exists for pid: $pid"); + } + + $pid = posix_getpid(); + if (file_put_contents($pidFile, $pid) === false) { + throw new \Exception("Could not write $pidFile"); + } + // Setup php options, defined in the php section of the config file if (isset($config['php']) && is_array($config['php'])) { foreach ($config['php'] as $key => $value) { @@ -118,7 +136,7 @@ function signalHandler($signal) $ex = null; $result = @stream_select($read, $write, $ex, 0, 1); if ($result === false) { - throw new \Exception('Error selecting from socket: ' . socket_strerror(socket_last_error($this->_socket))); + throw new \Exception('Error selecting from socket: ' . socket_strerror(socket_last_error($socket))); } if ($result > 0) { if (in_array($socket, $read)) {