MadelineProto/src/danog/MadelineProto/Ipc/Runner/entry.php

131 lines
4.7 KiB
PHP
Raw Normal View History

2020-07-11 20:01:54 +02:00
<?php
/**
* IPC server entry module.
*
* This file is part of MadelineProto.
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU General Public License along with MadelineProto.
* If not, see <http://www.gnu.org/licenses/>.
*
* @author Daniil Gentili <daniil@daniil.it>
* @copyright 2016-2020 Daniil Gentili <daniil@daniil.it>
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
*
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/
use danog\MadelineProto\API;
2020-09-24 11:45:20 +02:00
use danog\MadelineProto\Ipc\IpcState;
2020-07-12 00:17:47 +02:00
use danog\MadelineProto\Ipc\Server;
2020-07-11 20:01:54 +02:00
use danog\MadelineProto\Logger;
2020-07-12 00:17:47 +02:00
use danog\MadelineProto\Magic;
2020-07-11 20:01:54 +02:00
use danog\MadelineProto\SessionPaths;
2020-09-26 17:11:41 +02:00
use danog\MadelineProto\Settings\Ipc;
2020-07-11 20:01:54 +02:00
use danog\MadelineProto\Tools;
2020-07-12 14:55:21 +02:00
(static function (): void {
2020-10-04 14:55:05 +02:00
if (\defined('MADELINE_ENTRY')) {
2020-07-12 14:55:21 +02:00
// Already called
return;
}
2020-10-04 14:55:05 +02:00
\define('MADELINE_ENTRY', 1);
if (!\defined('MADELINE_WORKER_TYPE')) {
2020-07-12 14:55:21 +02:00
if (\count(\debug_backtrace(0)) !== 1) {
// We're not being included directly
return;
}
$arguments = [];
2020-07-11 20:01:54 +02:00
if (isset($GLOBALS['argv']) && !empty($GLOBALS['argv'])) {
2020-07-12 14:55:21 +02:00
$arguments = \array_slice($GLOBALS['argv'], 1);
2020-07-11 20:01:54 +02:00
} elseif (isset($_GET['argv']) && !empty($_GET['argv'])) {
$arguments = $_GET['argv'];
}
2020-07-12 14:55:21 +02:00
if (\count($arguments) < 2) {
\trigger_error("Not enough arguments!", E_USER_ERROR);
exit(1);
2020-07-11 20:01:54 +02:00
}
2020-10-04 14:55:05 +02:00
\define('MADELINE_WORKER_TYPE', \array_shift($arguments));
\define('MADELINE_WORKER_ARGS', $arguments);
2020-07-12 14:55:21 +02:00
}
2020-09-24 11:45:20 +02:00
2020-10-04 14:55:05 +02:00
if (\defined('SIGHUP')) {
2020-09-24 11:45:20 +02:00
try {
\pcntl_signal(SIGHUP, fn () => null);
} catch (\Throwable $e) {
}
}
2020-07-12 14:55:21 +02:00
if (!\class_exists(API::class)) {
2020-07-11 20:01:54 +02:00
$paths = [
\dirname(__DIR__, 7)."/autoload.php",
\dirname(__DIR__, 5)."/vendor/autoload.php",
];
foreach ($paths as $path) {
if (\file_exists($path)) {
$autoloadPath = $path;
break;
}
}
if (!isset($autoloadPath)) {
\trigger_error("Could not locate autoload.php in any of the following files: ".\implode(", ", $paths), E_USER_ERROR);
exit(1);
}
include $autoloadPath;
}
2020-07-12 14:55:21 +02:00
if (\MADELINE_WORKER_TYPE === 'madeline-ipc') {
$ipcPath = \MADELINE_WORKER_ARGS[0];
2020-07-11 20:01:54 +02:00
if (!\file_exists($ipcPath)) {
\trigger_error("IPC session $ipcPath does not exist!", E_USER_ERROR);
exit(1);
}
2020-10-04 14:55:05 +02:00
if (\function_exists('cli_set_process_title')) {
2020-07-12 00:17:47 +02:00
@\cli_set_process_title("MadelineProto worker $ipcPath");
}
2020-10-17 01:23:52 +02:00
if (\function_exists('posix_setsid')) {
@\posix_setsid();
}
2020-07-12 00:17:47 +02:00
if (isset($_GET['cwd'])) {
@\chdir($_GET['cwd']);
}
2020-10-04 14:55:05 +02:00
\define('MADELINE_WORKER', 1);
2020-07-11 20:01:54 +02:00
2020-09-24 11:45:20 +02:00
$runnerId = \MADELINE_WORKER_ARGS[1];
$session = new SessionPaths($ipcPath);
2020-07-11 20:01:54 +02:00
try {
2020-07-12 00:17:47 +02:00
Magic::classExists();
Magic::$script_cwd = $_GET['cwd'] ?? Magic::getcwd();
2020-09-28 11:56:14 +02:00
$API = new API($ipcPath, (new Ipc)->setSlow(true));
2020-09-27 19:36:17 +02:00
$API->init();
$API->initSelfRestart();
2020-09-24 11:45:20 +02:00
while (true) {
try {
2020-09-24 21:13:58 +02:00
Tools::wait($session->storeIpcState(new IpcState($runnerId)));
2020-09-24 11:45:20 +02:00
Tools::wait(Server::waitShutdown());
return;
} catch (\Throwable $e) {
Logger::log((string) $e, Logger::FATAL_ERROR);
Tools::wait($API->report("Surfaced: $e"));
}
2020-07-11 20:01:54 +02:00
}
} catch (\Throwable $e) {
2020-09-27 19:36:17 +02:00
Logger::log("$e", Logger::FATAL_ERROR);
Logger::log("Got exception in IPC server, exiting...", Logger::FATAL_ERROR);
2020-09-24 11:45:20 +02:00
$ipc = Tools::wait($session->getIpcState());
if (!($ipc && $ipc->getStartupId() === $runnerId && !$ipc->getException())) {
2020-09-27 19:36:17 +02:00
Logger::log("Reporting error!");
2020-09-24 11:45:20 +02:00
Tools::wait($session->storeIpcState(new IpcState($runnerId, $e)));
2020-09-27 19:36:17 +02:00
Logger::log("Reported error!");
} else {
Logger::log("Not reporting error!");
2020-07-11 20:01:54 +02:00
}
}
}
})();