Feature: Share a Linux host to another master

Add COMMAND_SHARE (SHARE_TYPE_YAMA) with the Windows semantics: the
record goes into share_list and a second connection is opened to the
target master while the main link stays where it is. main()'s connection
state is all global, so each record is served by a forked
"--share <entry>" child that reuses the whole login, heartbeat and
command path; the parent only starts, tracks and reaps them.
COMMAND_SHARE_CANCEL clears the list and stops the children right away
(Linux can; Windows does it on the next start).

Transfer now opens its connection immediately too, matching the Windows
fallthrough that spawns a shared instance, and that temporary connection
is dropped once the main link has actually moved to the target or when
the transfer expires.

common/host_endpoint.h holds the ip/port/shine/transport word list the
two commands share; common/host_share.h holds the share payload,
share_list encoding and the per-master dedup.

Config writes now take a cross-process flock and replace the file by
rename. The parent and every child write the same config.conf from their
own snapshot, and a truncating overwrite could drop the other side's
keys. Children ignore the commands that only mean something on the main
link: share, transfer, upgrade, TLS anchor and group.

Verified in WSL against fake masters: share start, cancel and restore,
re-sharing with a different transport, share plus transfer on one master,
and the transfer child disappearing after the main link moved.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-07 23:21:44 +01:00
1 parent 7240879792
commit 2a97471b51
6 files changed
+743 -85

No files matched your search

+40 -12
View File
@@ -3,6 +3,10 @@
#include <map>
#include <fstream>
#include <sys/stat.h>
#include <sys/file.h>
#include <fcntl.h>
#include <unistd.h>
#include <cstdio>
#include <cstdlib>
// ============== 轻量 INI 配置文件读写(Linux)==============
@@ -34,8 +38,7 @@ public:
void SetStr(const std::string& key, const std::string& value)
{
m_data[key] = value;
Save();
SetValue(key, value);
}
int GetInt(const std::string& key, int def = 0) const
@@ -49,8 +52,7 @@ public:
void SetInt(const std::string& key, int value)
{
m_data[key] = std::to_string(value);
Save();
SetValue(key, std::to_string(value));
}
// 重新加载配置
@@ -73,17 +75,43 @@ private:
}
}
void Save()
// 写一个键。主实例和分享子进程写的是同一个文件,各持一份整文件快照、整文件覆盖
// 写,所以必须「拿跨进程锁 -> 重读 -> 改一个键 -> 原子替换」:不重读会把对方刚
// 写的键(比如 transfer)整个抹掉,不原子替换则可能被读到写了一半的文件。
void SetValue(const std::string& key, const std::string& value)
{
// 递归创建目录(mkdir -p 效果)
MkdirRecursive(m_dir);
std::ofstream f(m_path, std::ios::trunc);
if (!f.is_open()) {
return; // 打开失败
}
for (auto& kv : m_data) {
f << kv.first << "=" << kv.second << "\n";
// O_CLOEXEC:锁 fd 不能漏给 fork+exec 出来的分享子进程,否则它会把锁一直
// 持有到自己退出。close() 即释放 flock。
int lockFd = open((m_path + ".lock").c_str(), O_CREAT | O_RDWR | O_CLOEXEC, 0644);
if (lockFd >= 0)
flock(lockFd, LOCK_EX);
Load();
m_data[key] = value;
SaveAtomic();
if (lockFd >= 0)
close(lockFd);
}
// 先写同目录临时文件再 rename:同一文件系统内 rename 是原子的,读者要么看到
// 旧文件、要么看到新文件,不会读到截断到一半的内容。
void SaveAtomic()
{
const std::string tmp = m_path + ".tmp." + std::to_string((long)getpid());
{
std::ofstream f(tmp, std::ios::trunc);
if (!f.is_open())
return; // 打开失败:保留旧文件
for (auto& kv : m_data)
f << kv.first << "=" << kv.second << "\n";
f.flush();
if (!f) {
remove(tmp.c_str());
return;
}
}
if (rename(tmp.c_str(), m_path.c_str()) != 0)
remove(tmp.c_str());
}
// 递归创建目录