Browse Source

Handle exceptions from child processes

rpm
J. King 3 years ago
parent
commit
e160189224
  1. 80
      lib/Service/Daemon.php
  2. 2
      tests/cases/CLI/TestCLI.php

80
lib/Service/Daemon.php

@ -6,6 +6,8 @@
declare(strict_types=1); declare(strict_types=1);
namespace JKingWeb\Arsse\Service; namespace JKingWeb\Arsse\Service;
use JKingWeb\Arsse\AbstractException;
class Daemon { class Daemon {
protected const PID_PATTERN = '/^([1-9]\d{0,77})?$/D'; // no more than 78 digits (256-bit unsigned integer), starting with a digit other than zero protected const PID_PATTERN = '/^([1-9]\d{0,77})?$/D'; // no more than 78 digits (256-bit unsigned integer), starting with a digit other than zero
@ -34,42 +36,54 @@ class Daemon {
case 0: case 0:
fclose($pipe[0]); fclose($pipe[0]);
# In the child, call setsid() to detach from any terminal and create an independent session. # In the child, call setsid() to detach from any terminal and create an independent session.
@posix_setsid(); try {
# 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.) if (@posix_setsid() === -1) {
switch (@pcntl_fork()) { throw new Exception("forkFailed", ['instance' => 1]);
case -1: }
// Unable to fork # 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.)
throw new Exception("forkFailed", ['instance' => 2]); switch (@pcntl_fork()) {
case 0: case -1:
// 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 // Unable to fork
# 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. throw new Exception("forkFailed", ['instance' => 2]);
$this->writePID($pidfile); case 0:
# In the daemon process, drop privileges, if possible and applicable. // 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
// already done # 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.
# 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. $this->writePID($pidfile);
fwrite($pipe[1], (string) posix_getpid()); # In the daemon process, drop privileges, if possible and applicable.
fclose($pipe[1]); // already done
// now everything else is done in order # 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.
# In the daemon process, connect /dev/null to standard input, output, and error. fwrite($pipe[1], (string) posix_getpid());
fclose(STDIN); fclose($pipe[1]);
fclose(STDOUT); // now everything else is done in order, but beyond this point any errors cannot be reported back to the original process
fclose(STDERR); # In the daemon process, connect /dev/null to standard input, output, and error.
global $STDIN, $STDOUT, $STDERR; fclose(STDIN);
$STDIN = fopen("/dev/null", "r"); fclose(STDOUT);
$STDOUT = fopen("/dev/null", "w"); fclose(STDERR);
$STDERR = fopen("/dev/null", "w"); global $STDIN, $STDOUT, $STDERR;
# 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. $STDIN = fopen("/dev/null", "r");
umask(0); $STDOUT = fopen("/dev/null", "w");
# 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. $STDERR = fopen("/dev/null", "w");
chdir("/"); # 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.
return; umask(0);
default: # 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.
# 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. chdir("/");
exit; 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;
}
} catch (AbstractException $e) {
// transmit the exception back to the original process, which will re-create the exception if necessary
@fwrite($pipe[1], json_encode([get_class($e), $e->getSymbol(), $e->getParams()]));
exit;
} }
default: default:
fclose($pipe[1]); fclose($pipe[1]);
$result = fread($pipe[0], 100); $result = json_decode(fread($pipe[0], 100), true);
if ($result) {
[$class, $symbol, $params] = $result;
throw new $class($symbol, $params);
}
fclose($pipe[0]); 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. # 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; exit;

2
tests/cases/CLI/TestCLI.php

@ -96,7 +96,7 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest {
$srv = $this->mock(Service::class); $srv = $this->mock(Service::class);
$srv->watch->returns(new \DateTimeImmutable); $srv->watch->returns(new \DateTimeImmutable);
$daemon = $this->mock(Daemon::class); $daemon = $this->mock(Daemon::class);
$daemon->checkPIDFilePath->throws(new Service\Exception("pidDuplicate", ['pid' =>2112])); $daemon->checkPIDFilePath->throws(new Service\Exception("pidDuplicate", ['pid' => 2112]));
$daemon->fork->returns(null); $daemon->fork->returns(null);
$this->objMock->get->with(Service::class)->returns($srv->get()); $this->objMock->get->with(Service::class)->returns($srv->get());
$this->objMock->get->with(Daemon::class)->returns($daemon->get()); $this->objMock->get->with(Daemon::class)->returns($daemon->get());

Loading…
Cancel
Save