Browse Source

Move forking and related to Service class

rpm
J. King 3 years ago
parent
commit
32e04e3938
  1. 12
      lib/AbstractException.php
  2. 136
      lib/CLI.php
  3. 130
      lib/Service.php
  4. 2
      lib/Service/Exception.php
  5. 14
      locale/en.php
  6. 1
      tests/bootstrap.php
  7. 41
      tests/cases/CLI/TestCLI.php
  8. 54
      tests/cases/Service/TestPID.php

12
lib/AbstractException.php

@ -102,12 +102,12 @@ abstract class AbstractException extends \Exception {
"ImportExport/Exception.invalidFolderCopy" => 10614,
"ImportExport/Exception.invalidTagName" => 10615,
"Rule/Exception.invalidPattern" => 10701,
"CLI/Exception.pidNotFile" => 10801,
"CLI/Exception.pidDirNotFound" => 10802,
"CLI/Exception.pidUnusable" => 10803,
"CLI/Exception.pidUnreadable" => 10804,
"CLI/Exception.pidUnwritable" => 10805,
"CLI/Exception.pidUncreatable" => 10806,
"Service/Exception.pidNotFile" => 10801,
"Service/Exception.pidDirNotFound" => 10802,
"Service/Exception.pidUnusable" => 10803,
"Service/Exception.pidUnreadable" => 10804,
"Service/Exception.pidUnwritable" => 10805,
"Service/Exception.pidUncreatable" => 10806,
];
protected $symbol;

136
lib/CLI.php

@ -94,8 +94,8 @@ USAGE_TEXT;
return 0;
case "daemon":
if ($args['--fork'] !== null) {
$pidfile = $this->resolvePID($args['--fork']);
$this->fork($pidfile);
$pidfile = Service::resolvePID($args['--fork']);
Service::fork($pidfile);
}
$this->loadConf();
Arsse::$obj->get(Service::class)->watch(true);
@ -228,136 +228,4 @@ USAGE_TEXT;
}
return 0;
}
/** Daemonizes the process via the traditional sysvinit double-fork procedure
*
* @codeCoverageIgnore
*/
protected function fork(string $pidfile): void {
// check that the PID file is not already used by another process
$this->checkPID($pidfile, false);
// We will follow systemd's recommended daemonizing process as much as possible:
# Close all open file descriptors except standard input, output, and error (i.e. the first three file descriptors 0, 1, 2). This ensures that no accidentally passed file descriptor stays around in the daemon process. On Linux, this is best implemented by iterating through /proc/self/fd, with a fallback of iterating from file descriptor 3 to the value returned by getrlimit() for RLIMIT_NOFILE.
// We should have no open file descriptors at this time. Even if we did, I'm not certain how they should be closed from PHP
# Reset all signal handlers to their default. This is best done by iterating through the available signals up to the limit of _NSIG and resetting them to SIG_DFL.
// We have not yet set any signal handlers, so this should be fine
# Reset the signal mask using sigprocmask().
// Not possible to my knowledge
# Sanitize the environment block, removing or resetting environment variables that might negatively impact daemon runtime.
//Not necessary; we don't use the environment
# Call fork(), to create a background process.
$pipe = stream_socket_pair(\STREAM_PF_UNIX, \STREAM_SOCK_STREAM, \STREAM_IPPROTO_IP);
switch (@pcntl_fork()) {
case -1:
// Unable to fork
throw new \Exception("Unable to fork");
case 0:
fclose($pipe[0]);
# In the child, call setsid() to detach from any terminal and create an independent session.
@posix_setsid();
# In the child, call fork() again, to ensure that the daemon can never re-acquire a terminal again. (This relevant if the program — and all its dependencies — does not carefully specify `O_NOCTTY` on each and every single `open()` call that might potentially open a TTY device node.)
switch (@pcntl_fork()) {
case -1:
// Unable to fork
throw new \Exception("Unable to fork");
case 0:
// We do some things out of order because as far as I know there's no way to reconnect stdin, stdout, and stderr without closing the channel to the parent first
# In the daemon process, write the daemon PID (as returned by getpid()) to a PID file, for example /run/foobar.pid (for a hypothetical daemon "foobar") to ensure that the daemon cannot be started more than once. This must be implemented in race-free fashion so that the PID file is only updated when it is verified at the same time that the PID previously stored in the PID file no longer exists or belongs to a foreign process.
$this->checkPID($pidfile, true);
# In the daemon process, drop privileges, if possible and applicable.
// already done
# From the daemon process, notify the original process started that initialization is complete. This can be implemented via an unnamed pipe or similar communication channel that is created before the first fork() and hence available in both the original and the daemon process.
fwrite($pipe[1], (string) posix_getpid());
fclose($pipe[1]);
// now everything else is done in order
# In the daemon process, connect /dev/null to standard input, output, and error.
fclose(STDIN);
fclose(STDOUT);
fclose(STDERR);
global $STDIN, $STDOUT, $STDERR;
$STDIN = fopen("/dev/null", "r");
$STDOUT = fopen("/dev/null", "w");
$STDERR = fopen("/dev/null", "w");
# In the daemon process, reset the umask to 0, so that the file modes passed to open(), mkdir() and suchlike directly control the access mode of the created files and directories.
umask(0);
# In the daemon process, change the current directory to the root directory (/), in order to avoid that the daemon involuntarily blocks mount points from being unmounted.
chdir("/");
return;
default:
# Call exit() in the first child, so that only the second child (the actual daemon process) stays around. This ensures that the daemon process is re-parented to init/PID 1, as all daemons should be.
exit;
}
default:
fclose($pipe[1]);
fread($pipe[0], 100);
fclose($pipe[0]);
# Call exit() in the original process. The process that invoked the daemon must be able to rely on that this exit() happens after initialization is complete and all external communication channels are established and accessible.
exit;
}
}
protected function checkPID(string $pidfile, bool $lock) {
if (!$lock) {
if (file_exists($pidfile)) {
$pid = @file_get_contents($pidfile);
if (preg_match("/^\d+$/s", (string) $pid)) {
if (@posix_kill((int) $pid, 0)) {
throw new \Exception("Process already exists");
}
}
}
} else {
if ($f = @fopen($pidfile, "c+")) {
if (@flock($f, \LOCK_EX | \LOCK_NB)) {
// confirm that some other process didn't get in before us
$pid = fread($f, 100);
if (preg_match("/^\d+$/s", (string) $pid)) {
if (@posix_kill((int) $pid, 0)) {
throw new \Exception("Process already exists");
}
}
// write the PID to the pidfile
rewind($f);
ftruncate($f, 0);
fwrite($f, (string) posix_getpid());
fclose($f);
} else {
throw new \Exception("Process already exists");
}
} else {
throw new Exception("Could not write to PID file");
}
}
}
/** Resolves the PID file path and ensures the file or parent directory is writable */
public function resolvePID(string $pidfile): string {
$dir = dirname($pidfile);
$file = basename($pidfile);
if (!strlen($file)) {
throw new CLI\Exception("pidNotFile", ['pidfile' => $dir]);
} elseif ($base = @realpath($dir)) {
$out = "$base/$file";
if (file_exists($out)) {
if (!is_readable($out) && !is_writable($out)) {
throw new CLI\Exception("pidUnusable", ['pidfile' => $out]);
} elseif (!is_readable($out)) {
throw new CLI\Exception("pidunreadable", ['pidfile' => $out]);
} elseif (!is_writable($out)) {
throw new CLI\Exception("pidUnwritable", ['pidfile' => $out]);
} elseif (!is_file($out)) {
throw new CLI\Exception("pidNotFile", ['pidfile' => $out]);
}
} elseif (!is_writable($base)) {
throw new CLI\Exception("pidUncreatable", ['pidfile' => $out]);
}
} else {
throw new CLI\Exception("pidDirNotFound", ['piddir' => $dir]);
}
return $out;
}
protected function realpath(string $path) {
return @realpath($path);
}
}

130
lib/Service.php

@ -110,5 +110,135 @@ class Service {
$this->loop = false;
}
/** Daemonizes the process via the traditional sysvinit double-fork procedure
*
* @codeCoverageIgnore
*/
public static function fork(string $pidfile): void {
// check that the PID file is not already used by another process
static::checkPID($pidfile, false);
// We will follow systemd's recommended daemonizing process as much as possible:
# Close all open file descriptors except standard input, output, and error (i.e. the first three file descriptors 0, 1, 2). This ensures that no accidentally passed file descriptor stays around in the daemon process. On Linux, this is best implemented by iterating through /proc/self/fd, with a fallback of iterating from file descriptor 3 to the value returned by getrlimit() for RLIMIT_NOFILE.
// We should have no open file descriptors at this time. Even if we did, I'm not certain how they should be closed from PHP
# Reset all signal handlers to their default. This is best done by iterating through the available signals up to the limit of _NSIG and resetting them to SIG_DFL.
// We have not yet set any signal handlers, so this should be fine
# Reset the signal mask using sigprocmask().
// Not possible to my knowledge
# Sanitize the environment block, removing or resetting environment variables that might negatively impact daemon runtime.
//Not necessary; we don't use the environment
# Call fork(), to create a background process.
$pipe = stream_socket_pair(\STREAM_PF_UNIX, \STREAM_SOCK_STREAM, \STREAM_IPPROTO_IP);
switch (@pcntl_fork()) {
case -1:
// Unable to fork
throw new \Exception("Unable to fork");
case 0:
fclose($pipe[0]);
# In the child, call setsid() to detach from any terminal and create an independent session.
@posix_setsid();
# In the child, call fork() again, to ensure that the daemon can never re-acquire a terminal again. (This relevant if the program — and all its dependencies — does not carefully specify `O_NOCTTY` on each and every single `open()` call that might potentially open a TTY device node.)
switch (@pcntl_fork()) {
case -1:
// Unable to fork
throw new \Exception("Unable to fork");
case 0:
// We do some things out of order because as far as I know there's no way to reconnect stdin, stdout, and stderr without closing the channel to the parent first
# In the daemon process, write the daemon PID (as returned by getpid()) to a PID file, for example /run/foobar.pid (for a hypothetical daemon "foobar") to ensure that the daemon cannot be started more than once. This must be implemented in race-free fashion so that the PID file is only updated when it is verified at the same time that the PID previously stored in the PID file no longer exists or belongs to a foreign process.
static::checkPID($pidfile, true);
# In the daemon process, drop privileges, if possible and applicable.
// already done
# From the daemon process, notify the original process started that initialization is complete. This can be implemented via an unnamed pipe or similar communication channel that is created before the first fork() and hence available in both the original and the daemon process.
fwrite($pipe[1], (string) posix_getpid());
fclose($pipe[1]);
// now everything else is done in order
# In the daemon process, connect /dev/null to standard input, output, and error.
fclose(STDIN);
fclose(STDOUT);
fclose(STDERR);
global $STDIN, $STDOUT, $STDERR;
$STDIN = fopen("/dev/null", "r");
$STDOUT = fopen("/dev/null", "w");
$STDERR = fopen("/dev/null", "w");
# In the daemon process, reset the umask to 0, so that the file modes passed to open(), mkdir() and suchlike directly control the access mode of the created files and directories.
umask(0);
# In the daemon process, change the current directory to the root directory (/), in order to avoid that the daemon involuntarily blocks mount points from being unmounted.
chdir("/");
return;
default:
# Call exit() in the first child, so that only the second child (the actual daemon process) stays around. This ensures that the daemon process is re-parented to init/PID 1, as all daemons should be.
exit;
}
default:
fclose($pipe[1]);
fread($pipe[0], 100);
fclose($pipe[0]);
# Call exit() in the original process. The process that invoked the daemon must be able to rely on that this exit() happens after initialization is complete and all external communication channels are established and accessible.
exit;
}
}
protected static function checkPID(string $pidfile, bool $lock) {
if (!$lock) {
if (file_exists($pidfile)) {
$pid = @file_get_contents($pidfile);
if (preg_match("/^\d+$/s", (string) $pid)) {
if (@posix_kill((int) $pid, 0)) {
throw new \Exception("Process already exists");
}
}
}
} else {
if ($f = @fopen($pidfile, "c+")) {
if (@flock($f, \LOCK_EX | \LOCK_NB)) {
// confirm that some other process didn't get in before us
$pid = fread($f, 100);
if (preg_match("/^\d+$/s", (string) $pid)) {
if (@posix_kill((int) $pid, 0)) {
throw new \Exception("Process already exists");
}
}
// write the PID to the pidfile
rewind($f);
ftruncate($f, 0);
fwrite($f, (string) posix_getpid());
fclose($f);
} else {
throw new \Exception("Process already exists");
}
} else {
throw new Exception("Could not write to PID file");
}
}
}
/** Resolves the PID file path and ensures the file or parent directory is writable */
public static function resolvePID(string $pidfile): string {
$dir = dirname($pidfile);
$file = basename($pidfile);
if (!strlen($file)) {
throw new Service\Exception("pidNotFile", ['pidfile' => $dir]);
} elseif ($base = @static::realpath($dir)) {
$out = "$base/$file";
if (file_exists($out)) {
if (!is_readable($out) && !is_writable($out)) {
throw new Service\Exception("pidUnusable", ['pidfile' => $out]);
} elseif (!is_readable($out)) {
throw new Service\Exception("pidunreadable", ['pidfile' => $out]);
} elseif (!is_writable($out)) {
throw new Service\Exception("pidUnwritable", ['pidfile' => $out]);
} elseif (!is_file($out)) {
throw new Service\Exception("pidNotFile", ['pidfile' => $out]);
}
} elseif (!is_writable($base)) {
throw new Service\Exception("pidUncreatable", ['pidfile' => $out]);
}
} else {
throw new Service\Exception("pidDirNotFound", ['piddir' => $dir]);
}
return $out;
}
protected static function realpath(string $path) {
return @realpath($path);
}
}

2
lib/CLI/Exception.php → lib/Service/Exception.php

@ -4,7 +4,7 @@
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
namespace JKingWeb\Arsse\CLI;
namespace JKingWeb\Arsse\Service;
class Exception extends \JKingWeb\Arsse\AbstractException {
}

14
locale/en.php

@ -208,11 +208,11 @@ return [
'Exception.JKingWeb/Arsse/ImportExport/Exception.invalidFolderCopy' => 'Input data contains multiple folders of the same name under the same parent',
'Exception.JKingWeb/Arsse/ImportExport/Exception.invalidTagName' => 'Input data contains an invalid tag name',
'Exception.JKingWeb/Arsse/Rule/Exception.invalidPattern' => 'Specified rule pattern is invalid',
'Exception.JKingWeb/Arsse/CLI/Exception.pidNotFile' => 'Specified PID file location "{pidfile}" must be a regular file',
'Exception.JKingWeb/Arsse/CLI/Exception.pidDirNotFound' => 'Parent directory "{piddir}" of PID file does not exist',
'Exception.JKingWeb/Arsse/CLI/Exception.pidUnreadable' => 'Insufficient permissions to open PID file "{pidfile}" for reading',
'Exception.JKingWeb/Arsse/CLI/Exception.pidUnwritable' => 'Insufficient permissions to open PID file "{pidfile}" for writing',
'Exception.JKingWeb/Arsse/CLI/Exception.pidUnusable' => 'Insufficient permissions to open PID file "{pidfile}" for reading or writing',
'Exception.JKingWeb/Arsse/CLI/Exception.pidUncreatable' => 'Insufficient permissions to create PID file "{pidfile}"',
'Exception.JKingWeb/Arsse/CLI/Exception.pidNotFile' => 'PID file "{pidfile}" must be a regular file',
'Exception.JKingWeb/Arsse/Service/Exception.pidNotFile' => 'Specified PID file location "{pidfile}" must be a regular file',
'Exception.JKingWeb/Arsse/Service/Exception.pidDirNotFound' => 'Parent directory "{piddir}" of PID file does not exist',
'Exception.JKingWeb/Arsse/Service/Exception.pidUnreadable' => 'Insufficient permissions to open PID file "{pidfile}" for reading',
'Exception.JKingWeb/Arsse/Service/Exception.pidUnwritable' => 'Insufficient permissions to open PID file "{pidfile}" for writing',
'Exception.JKingWeb/Arsse/Service/Exception.pidUnusable' => 'Insufficient permissions to open PID file "{pidfile}" for reading or writing',
'Exception.JKingWeb/Arsse/Service/Exception.pidUncreatable' => 'Insufficient permissions to create PID file "{pidfile}"',
'Exception.JKingWeb/Arsse/Service/Exception.pidNotFile' => 'PID file "{pidfile}" must be a regular file',
];

1
tests/bootstrap.php

@ -12,6 +12,7 @@ const DOCROOT = BASE."tests".DIRECTORY_SEPARATOR."docroot".DIRECTORY_SEPARATOR;
ini_set("memory_limit", "-1");
ini_set("zend.assertions", "1");
ini_set("assert.exception", "true");
ini_set("pcre.jit", "0");
// FIXME: This is required by a dependency of Picofeed
error_reporting(\E_ALL & ~\E_DEPRECATED);
require_once BASE."vendor".DIRECTORY_SEPARATOR."autoload.php";

41
tests/cases/CLI/TestCLI.php

@ -17,23 +17,9 @@ use JKingWeb\Arsse\CLI\Exception;
use JKingWeb\Arsse\REST\Fever\User as FeverUser;
use JKingWeb\Arsse\REST\Miniflux\Token as MinifluxToken;
use JKingWeb\Arsse\ImportExport\OPML;
use org\bovigo\vfs\vfsStream;
/** @covers \JKingWeb\Arsse\CLI */
class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest {
protected $pidfiles = [
'errors' => [
'create' => [],
'read' => "",
'write' => "",
'readwrite' => "",
],
'ok' => [
'dir' => [],
'file' => "",
],
];
public function setUp(): void {
parent::setUp();
$this->cli = $this->partialMock(CLI::class);
@ -42,33 +28,6 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest {
$this->dbMock = $this->mock(Database::class);
}
/** @dataProvider providePidResolutions */
public function testResolvePidFiles(string $file, bool $realpath, $exp): void {
$vfs = vfsStream::setup("pidtest", 0777, $this->pidfiles);
$path = $vfs->url()."/";
// set up access blocks
chmod($path."errors/create", 0555);
chmod($path."errors/read", 0333);
chmod($path."errors/write", 0555);
chmod($path."errors/readwrite", 0111);
// set up mock CLI
$this->cli->realPath->returns($realpath ? $path.$file : false);
$cli = $this->cli->get();
// perform the test
if ($exp instanceof \Exception) {
$this->assertException($exp);
$cli->resolvePID($file);
} else {
$this->assertSame($exp, $cli->resolvePID($file));
}
}
public function providePidResolutions(): iterable {
return [
["errors/create", true, new Exception("pidUncreatable")],
];
}
public function assertConsole(string $command, int $exitStatus, string $output = "", bool $pattern = false): void {
Arsse::$obj = $this->objMock->get();
Arsse::$db = $this->dbMock->get();

54
tests/cases/Service/TestPID.php

@ -0,0 +1,54 @@
<?php
/** @license MIT
* Copyright 2017 J. King, Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
namespace JKingWeb\Arsse\TestCase\Service;
use JKingWeb\Arsse\Service;
use JKingWeb\Arsse\Service\Exception;
use org\bovigo\vfs\vfsStream;
/** @covers \JKingWeb\Arsse\Service */
class TestPID extends \JKingWeb\Arsse\Test\AbstractTest {
protected $pidfiles = [
'errors' => [
'create' => [],
'read' => "",
'write' => "",
'readwrite' => "",
],
'ok' => [
'dir' => [],
'file' => "",
],
];
/** @dataProvider providePidResolutions */
public function testResolvePidFiles(string $file, bool $realpath, $exp): void {
$vfs = vfsStream::setup("pidtest", 0777, $this->pidfiles);
$path = $vfs->url()."/";
// set up access blocks
chmod($path."errors/create", 0555);
chmod($path."errors/read", 0333);
chmod($path."errors/write", 0555);
chmod($path."errors/readwrite", 0111);
// set up mock CLI
$this->cli->realPath->returns($realpath ? $path.$file : false);
$cli = $this->cli->get();
// perform the test
if ($exp instanceof \Exception) {
$this->assertException($exp);
$cli->resolvePID($file);
} else {
$this->assertSame($exp, $cli->resolvePID($file));
}
}
public function providePidResolutions(): iterable {
return [
["errors/create", true, new Exception("pidUncreatable")],
];
}
}
Loading…
Cancel
Save