Skip to content

Commit

Permalink
added pidfile
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelog committed Sep 11, 2011
1 parent ef3b7f9 commit 397fdcc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
4 changes: 3 additions & 1 deletion README
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions config.ini.example
@@ -1,5 +1,6 @@
[server]
listen=0.0.0.0:7890
pid=/tmp/fastagi.pid

[php]
display_errors=1
Expand Down
20 changes: 19 additions & 1 deletion fastagi.php
Expand Up @@ -31,6 +31,7 @@ function signalHandler($signal)
{
global $running;
global $children;
global $pidFile;
switch ($signal) {
case SIGINT:
case SIGQUIT:
Expand All @@ -44,6 +45,7 @@ function signalHandler($signal)
pcntl_waitpid($pid, $status);
unset($children[$pid]);
}
@unlink($pidFile);
break;
case SIGCHLD:
$pid = pcntl_waitpid(-1, $status);
Expand All @@ -60,6 +62,7 @@ function signalHandler($signal)
$socket = false;
$retCode = 0;
$children = array();
$pidFile = '';

try
{
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit 397fdcc

Please sign in to comment.