Skip to content

Commit 954de93

Browse files
committed
* 使得cluster_controller.py支持远程启动进程
已知的问题:由于machine的startserver消息増加了cid和gus参数,但guiconsole并未做相应的调整,因此guiconsole可能无法正常启动进程。 Merge branch 'master' of https://github.com/acatadog/kbengine
1 parent 69ab8c0 commit 954de93

5 files changed

Lines changed: 288 additions & 162 deletions

File tree

‎kbe/src/lib/helper/debug_helper.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ along with KBEngine. If not, see <http://www.gnu.org/licenses/>.
4141

4242
#ifndef NO_USE_LOG4CXX
4343
#include "log4cxx/logger.h"
44+
#include "log4cxx/logmanager.h"
4445
#include "log4cxx/net/socketappender.h"
4546
#include "log4cxx/fileappender.h"
4647
#include "log4cxx/helpers/inetaddress.h"
@@ -830,6 +831,13 @@ void DebugHelper::backtrace_msg()
830831
#endif
831832

832833
//-------------------------------------------------------------------------------------
834+
void DebugHelper::closeLogger()
835+
{
836+
// close logger for fork + execv
837+
g_logger = (const int)NULL;
838+
log4cxx::LogManager::shutdown();
839+
}
840+
833841

834842
}
835843

‎kbe/src/lib/helper/debug_helper.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ class DebugHelper : public Singleton<DebugHelper>
163163
void unregisterLogger(Network::MessageID msgID, Network::Address* pAddr);
164164

165165
void changeLogger(const std::string& name);
166+
void closeLogger(); // close logger for fork + execv
166167

167168
void clearBufferedLog(bool destroy = false);
168169

‎kbe/src/server/machine/machine.cpp‎

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,8 @@ void Machine::startserver(Network::Channel* pChannel, KBEngine::MemoryStream& s)
635635
{
636636
int32 uid = 0;
637637
COMPONENT_TYPE componentType;
638+
uint64 cid = 0;
639+
int16 gus = 0;
638640

639641
Network::Bundle* pBundle = Network::Bundle::ObjPool().createObject();
640642
bool success = true;
@@ -643,14 +645,16 @@ void Machine::startserver(Network::Channel* pChannel, KBEngine::MemoryStream& s)
643645

644646
s >> uid;
645647
s >> componentType;
648+
s >> cid;
649+
s >> gus;
646650

647651
if(s.length() > 0)
648652
{
649653
s >> finderRecvPort;
650654
}
651655

652-
INFO_MSG(fmt::format("Machine::startserver: uid={}, [{}], addr={}\n",
653-
uid, COMPONENT_NAME_EX(componentType), pChannel->c_str()));
656+
INFO_MSG(fmt::format("Machine::startserver: uid={}, [{}], addr={}, cid={}, gus={}\n",
657+
uid, COMPONENT_NAME_EX(componentType), pChannel->c_str(), cid, gus));
654658

655659
if(ComponentName2ComponentType(COMPONENT_NAME_EX(componentType)) == UNKNOWN_COMPONENT_TYPE)
656660
return;
@@ -691,6 +695,10 @@ void Machine::startserver(Network::Channel* pChannel, KBEngine::MemoryStream& s)
691695
free(szCmdline);
692696
free(currdir);
693697
#else
698+
if (startLinuxProcess(uid, componentType, cid, gus) <= 0)
699+
{
700+
success = false;
701+
}
694702
#endif
695703

696704
(*pBundle) << success;
@@ -892,5 +900,59 @@ void Machine::stopserver(Network::Channel* pChannel, KBEngine::MemoryStream& s)
892900

893901

894902
//-------------------------------------------------------------------------------------
903+
#if KBE_PLATFORM != PLATFORM_WIN32
904+
uint16 Machine::startLinuxProcess(int32 uid, COMPONENT_TYPE componentType, uint64 cid, int16 gus)
905+
{
906+
uint16 childpid;
907+
908+
if ((childpid = fork()) == 0)
909+
{
910+
if (setuid(uid) == -1)
911+
{
912+
ERROR_MSG(fmt::format("Machine::startLinuxProcess: Failed to setuid to {}, aborting exec for '{}'\n",
913+
uid, COMPONENT_NAME_EX(componentType)));
914+
915+
exit(1);
916+
}
917+
918+
std::string bin_path = Resmgr::getSingleton().getEnv().bin_path;
919+
std::string cmdLine = bin_path + COMPONENT_NAME_EX(componentType);
920+
921+
// 改变当前目录,以让出问题的时候core能在此处生成
922+
//chdir(bin_path.c_str());
923+
924+
const char *argv[6];
925+
std::string scid = fmt::format("{}", cid);
926+
std::string sgus = fmt::format("{}", gus);
927+
928+
argv[0] = cmdLine.c_str();
929+
argv[1] = "--cid";
930+
argv[2] = scid.c_str();
931+
argv[3] = "--gus";
932+
argv[4] = sgus.c_str();
933+
argv[5] = NULL;
934+
935+
// 关闭父类的socket
936+
ep_.close();
937+
epBroadcast_.close();
938+
epLocal_.close();
939+
940+
INFO_MSG(fmt::format("Machine::startLinuxProcess: UID {} execing '{}', cid = {}, gus = {}\n", uid, cmdLine, cid, gus));
941+
942+
DebugHelper::getSingleton().closeLogger();
943+
int result = execv(cmdLine.c_str(), (char * const *)argv);
944+
945+
if (result == -1)
946+
{
947+
ERROR_MSG(fmt::format("Machine::startLinuxProcess: Failed to exec '{}'\n", cmdLine));
948+
}
949+
950+
exit(1);
951+
return 0;
952+
}
953+
else
954+
return childpid;
955+
}
956+
#endif
895957

896958
}

‎kbe/src/server/machine/machine.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ class Machine: public ServerApp,
101101
*/
102102
void startserver(Network::Channel* pChannel, KBEngine::MemoryStream& s);
103103

104+
#if KBE_PLATFORM != PLATFORM_WIN32
105+
/**
106+
* 在linux下启动一个新进程
107+
*/
108+
uint16 startLinuxProcess(int32 uid, COMPONENT_TYPE componentType, uint64 cid, int16 gus);
109+
#endif
110+
104111
/** 网络接口
105112
关闭服务器
106113
@uid: 提供启动的uid参数

0 commit comments

Comments
 (0)