1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-22 13:12:41 +00:00

More daemon cleanup

This commit is contained in:
J. King 2021-06-15 16:58:54 -04:00
parent 4e1193bab2
commit 59cf27089a
2 changed files with 25 additions and 10 deletions

View file

@ -9,6 +9,7 @@ namespace JKingWeb\Arsse;
use JKingWeb\Arsse\REST\Fever\User as Fever;
use JKingWeb\Arsse\ImportExport\OPML;
use JKingWeb\Arsse\REST\Miniflux\Token as Miniflux;
use JKingWeb\Arsse\Service\Daemon;
class CLI {
public const USAGE = <<<USAGE_TEXT
@ -40,7 +41,7 @@ importing or exporting data.
See the manual page for more details:
man 1 arsse
man arsse
USAGE_TEXT;
protected function usage($prog): string {
@ -94,13 +95,10 @@ USAGE_TEXT;
return 0;
case "daemon":
if ($args['--fork'] !== null) {
$pidfile = Service::resolvePID($args['--fork']);
Service::fork($pidfile);
}
return $this->serviceFork($args['--fork']);
} else {
$this->loadConf();
Arsse::$obj->get(Service::class)->watch(true);
if (isset($pidfile)) {
unlink($pidfile);
}
return 0;
case "feed refresh":
@ -179,6 +177,23 @@ USAGE_TEXT;
fwrite(STDERR, $msg.\PHP_EOL);
}
protected function serviceFork(string $pidfile): int {
// initialize the object factory
Arsse::$obj = Arsse::$obj ?? new Factory;
// create a Daemon object which contains various helper functions
$daemon = Arsse::$obj->get(Daemon::class);
// resolve the PID file to its absolute path; this also checks its readability and writability
$pidfile = $daemon->resolvePID($pidfile);
// daemonize
$daemon->fork($pidfile);
// start the fetching service as normal
$this->loadConf();
Arsse::$obj->get(Service::class)->watch(true);
// after the service has been shut down, delete the PID file and exit cleanly
unlink($pidfile);
return 0;
}
protected function userAddOrSetPassword(string $method, string $user, string $password = null, string $oldpass = null): int {
$passwd = Arsse::$user->$method(...array_slice(func_get_args(), 1));
if (is_null($password)) {

View file

@ -13,7 +13,7 @@ class Daemon {
*/
public function fork(string $pidfile): void {
// check that the PID file is not already used by another process
static::checkPID($pidfile, false);
$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
@ -41,7 +41,7 @@ class Daemon {
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);
$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.