diff options
| author | vaxerski <43317083+vaxerski@users.noreply.github.com> | 2022-06-17 17:53:47 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-06-17 17:53:47 +0100 |
| commit | 837d4af8f6b282ecac42a8228aaa527bc19fb989 (patch) | |
| tree | 763dfb0211940d396cf0d1cbbd3be05dd4934999 /src/config/ConfigManager.cpp | |
| parent | 354e2651289861bf7027fd02be41088e45fc1c99 (diff) | |
| parent | b69375a9183a07e47bf6af89a103686b379df00a (diff) | |
Merge pull request #231 from CcydtN/main
Fix zombie process problem
Diffstat (limited to 'src/config/ConfigManager.cpp')
| -rw-r--r-- | src/config/ConfigManager.cpp | 40 |
1 files changed, 38 insertions, 2 deletions
diff --git a/src/config/ConfigManager.cpp b/src/config/ConfigManager.cpp index d12e20cd..d511c05a 100644 --- a/src/config/ConfigManager.cpp +++ b/src/config/ConfigManager.cpp @@ -170,11 +170,47 @@ void CConfigManager::handleRawExec(const std::string& command, const std::string Debug::log(LOG, "Config executing %s", toExec.c_str()); - if (fork() == 0) { - execl("/bin/sh", "/bin/sh", "-c", toExec.c_str(), nullptr); + int socket[2]; + if (pipe(socket) != 0) { + Debug::log(LOG, "Unable to create pipe for fork"); + } + pid_t child, grandchild; + child = fork(); + if (child < 0) { + close(socket[0]); + close(socket[1]); + Debug::log(LOG, "Fail to create the first fork"); + return; + } + if (child == 0) { + // run in child + grandchild = fork(); + if (grandchild == 0) { + // run in grandchild + close(socket[0]); + close(socket[1]); + execl("/bin/sh", "/bin/sh", "-c", args.c_str(), nullptr); + // exit grandchild + _exit(0); + } + close(socket[0]); + write(socket[1], &grandchild, sizeof(grandchild)); + close(socket[1]); + // exit child _exit(0); } + // run in parent + close(socket[1]); + read(socket[0], &grandchild, sizeof(grandchild)); + close(socket[0]); + // clear child and leave child to init + waitpid(child, NULL, 0); + if (child < 0) { + Debug::log(LOG, "Fail to create the second fork"); + return; + } + Debug::log(LOG, "Process created with pid %d", grandchild); } void CConfigManager::handleMonitor(const std::string& command, const std::string& args) { |
