You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
113 lines
2.5 KiB
113 lines
2.5 KiB
4 years ago
|
<?php
|
||
3 years ago
|
/*
|
||
|
* @Author: witersen
|
||
|
* @Date: 2021-02-25 14:18:17
|
||
|
* @LastEditors: witersen
|
||
|
* @LastEditTime: 2021-09-16 16:51:20
|
||
|
* @Description: QQ:1801168257
|
||
|
*/
|
||
4 years ago
|
|
||
3 years ago
|
ini_set('display_errors', 1);
|
||
|
error_reporting(E_ALL);
|
||
|
|
||
|
class Daemon
|
||
|
{
|
||
4 years ago
|
|
||
|
private $pidfile;
|
||
|
|
||
3 years ago
|
function __construct()
|
||
|
{
|
||
4 years ago
|
$this->pidfile = dirname(__FILE__) . '/svnadmind.pid';
|
||
|
}
|
||
|
|
||
3 years ago
|
private function init_daemon()
|
||
|
{
|
||
4 years ago
|
$pid = pcntl_fork();
|
||
|
if ($pid < 0) {
|
||
|
exit("pcntl_fork error");
|
||
|
} elseif ($pid > 0) {
|
||
|
exit(0);
|
||
|
}
|
||
|
$sid = posix_setsid();
|
||
|
if (!$sid) {
|
||
|
exit("错误");
|
||
|
}
|
||
|
$pid = pcntl_fork();
|
||
|
if ($pid < 0) {
|
||
|
exit("pcntl_fork error");
|
||
|
} elseif ($pid > 0) {
|
||
|
exit(0);
|
||
|
}
|
||
|
chdir("/");
|
||
|
umask(0);
|
||
|
if (defined('STDIN')) {
|
||
|
fclose(STDIN);
|
||
|
}
|
||
|
if (defined('STDOUT')) {
|
||
|
fclose(STDOUT);
|
||
|
}
|
||
|
if (defined('STDERR')) {
|
||
|
fclose(STDERR);
|
||
|
}
|
||
|
file_put_contents($this->pidfile, getmypid());
|
||
|
return getmypid();
|
||
|
}
|
||
|
|
||
3 years ago
|
private function init_socket()
|
||
|
{
|
||
4 years ago
|
//创建一个新的套接字上下文
|
||
|
$context = new ZMQContext();
|
||
|
//创建一个ZMQ响应套接字
|
||
|
$reply = new ZMQSocket($context, ZMQ::SOCKET_REP);
|
||
|
//绑定端口
|
||
|
$reply->bind("tcp://127.0.0.1:6666");
|
||
|
return $reply;
|
||
|
}
|
||
|
|
||
3 years ago
|
private function start_daemon()
|
||
|
{
|
||
4 years ago
|
if (file_exists($this->pidfile)) {
|
||
|
echo "进程正在运行中 无需启动\n";
|
||
|
exit(0);
|
||
|
}
|
||
|
return $this->init_daemon();
|
||
|
}
|
||
|
|
||
3 years ago
|
private function start()
|
||
|
{
|
||
4 years ago
|
$pid = $this->start_daemon();
|
||
|
$reply = $this->init_socket();
|
||
|
while (true) {
|
||
|
$a = $reply->recv();
|
||
|
$b = urldecode($a);
|
||
|
$result = shell_exec($b);
|
||
|
$reply->send($result);
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
private function stop()
|
||
|
{
|
||
4 years ago
|
if (file_exists($this->pidfile)) {
|
||
|
$pid = file_get_contents($this->pidfile);
|
||
|
posix_kill($pid, 9);
|
||
|
unlink($this->pidfile);
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
public function run($argv)
|
||
|
{
|
||
|
if (isset($argv[1])) {
|
||
|
if ($argv[1] == 'start') {
|
||
|
$this->start();
|
||
|
} else if ($argv[1] == 'stop') {
|
||
|
$this->stop();
|
||
|
}
|
||
4 years ago
|
} else {
|
||
3 years ago
|
echo "Usage: php svnadmind.php start|stop\n";
|
||
4 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
$deamon = new Daemon();
|
||
|
$deamon->run($argv);
|