diff --git a/lib/Service/Daemon.php b/lib/Service/Daemon.php index e83a7e2..1367a5e 100644 --- a/lib/Service/Daemon.php +++ b/lib/Service/Daemon.php @@ -43,7 +43,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. - $this->checkPID($pidfile, true); + $this->writePID($pidfile); # 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. @@ -78,13 +78,17 @@ class Daemon { protected function checkPID(string $pidfile) { if (file_exists($pidfile)) { - $pid = (string) @file_get_contents($pidfile); - if (preg_match(static::PID_PATTERN, $pid)) { - if (strlen($pid) && $this->processExists((int) $pid)) { - throw new Exception("pidDuplicate", ['pid' => $pid]); + $pid = @file_get_contents($pidfile); + if ($pid !== false) { + if (preg_match(static::PID_PATTERN, $pid)) { + if (strlen($pid) && $this->processExists((int) $pid)) { + throw new Exception("pidDuplicate", ['pid' => $pid]); + } + } else { + throw new Exception("pidCorrupt", ['pidfile' => $pidfile]); } } else { - throw new Exception("pidCorrupt", ['pidfile' => $pidfile]); + throw new Exception("pidInaccessible", ['pidfile' => $pidfile]); } } } diff --git a/locale/en.php b/locale/en.php index 57ea203..a0de158 100644 --- a/locale/en.php +++ b/locale/en.php @@ -208,12 +208,15 @@ 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/Service/Exception.pidNotFile' => 'Specified PID file location "{pidfile}" must be a regular file', + 'Exception.JKingWeb/Arsse/Service/Exception.pidNotFile' => 'PID file "{pidfile}" must be a regular file', 'Exception.JKingWeb/Arsse/Service/Exception.pidDirMissing' => 'Parent directory "{piddir}" of PID file does not exist', 'Exception.JKingWeb/Arsse/Service/Exception.pidDirUnresolvable' => 'Parent directory "{piddir}" of PID file could not be resolved to its absolute path', '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', + 'Exception.JKingWeb/Arsse/Service/Exception.pidCorrupt' => 'PID file "{pidfile}" does not contain a process identifier', + 'Exception.JKingWeb/Arsse/Service/Exception.pidDuplicate' => 'Service is already running with process identifier {pid}', + 'Exception.JKingWeb/Arsse/Service/Exception.pidLocked' => 'PID file "{pidfile}" is locked', + 'Exception.JKingWeb/Arsse/Service/Exception.pidInaccessible' => 'Unable to open PID file "{pidfile}"', ];