From 5fa61e5a54a2c1e908a9f460bf1fb9e84e9e98c2 Mon Sep 17 00:00:00 2001 From: CcydtN Date: Thu, 16 Jun 2022 01:06:51 +0800 Subject: [PATCH 1/2] Fix generating zombie process --- src/config/ConfigManager.cpp | 42 ++++++++++++++++++++++++++++++--- src/managers/KeybindManager.cpp | 41 ++++++++++++++++++++++++++++++-- 2 files changed, 78 insertions(+), 5 deletions(-) diff --git a/src/config/ConfigManager.cpp b/src/config/ConfigManager.cpp index d12e20cdf..8ee2b55ea 100644 --- a/src/config/ConfigManager.cpp +++ b/src/config/ConfigManager.cpp @@ -169,12 +169,48 @@ void CConfigManager::handleRawExec(const std::string& command, const std::string toExec = std::string("WAYLAND_DISPLAY=") + std::string(g_pCompositor->m_szWLDisplaySocket) + " " + toExec; Debug::log(LOG, "Config executing %s", toExec.c_str()); + + int socket[2]; + if (pipe(socket) != 0) { + Debug::log(LOG, "Unable to create pipe for fork"); + } - if (fork() == 0) { - execl("/bin/sh", "/bin/sh", "-c", toExec.c_str(), nullptr); - + 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]); + (void) write(socket[1], &grandchild, sizeof(grandchild)); + close(socket[1]); + // exit child _exit(0); } + // run in parent + close(socket[1]); + (void) 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) { diff --git a/src/managers/KeybindManager.cpp b/src/managers/KeybindManager.cpp index be87e24af..c60d22a04 100644 --- a/src/managers/KeybindManager.cpp +++ b/src/managers/KeybindManager.cpp @@ -141,11 +141,48 @@ void CKeybindManager::spawn(std::string args) { args = "WAYLAND_DISPLAY=" + std::string(g_pCompositor->m_szWLDisplaySocket) + " " + args; Debug::log(LOG, "Executing %s", args.c_str()); - if (fork() == 0) { - execl("/bin/sh", "/bin/sh", "-c", args.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]); + (void) write(socket[1], &grandchild, sizeof(grandchild)); + close(socket[1]); + // exit child _exit(0); } + // run in parent + close(socket[1]); + (void) 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 CKeybindManager::killActive(std::string args) { From b69375a9183a07e47bf6af89a103686b379df00a Mon Sep 17 00:00:00 2001 From: CcydtN Date: Fri, 17 Jun 2022 03:19:36 +0800 Subject: [PATCH 2/2] Fixing format issue --- src/config/ConfigManager.cpp | 26 +++++++++++++------------- src/managers/KeybindManager.cpp | 22 +++++++++++----------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/config/ConfigManager.cpp b/src/config/ConfigManager.cpp index 8ee2b55ea..d511c05a2 100644 --- a/src/config/ConfigManager.cpp +++ b/src/config/ConfigManager.cpp @@ -169,24 +169,24 @@ void CConfigManager::handleRawExec(const std::string& command, const std::string toExec = std::string("WAYLAND_DISPLAY=") + std::string(g_pCompositor->m_szWLDisplaySocket) + " " + toExec; Debug::log(LOG, "Config executing %s", toExec.c_str()); - - int socket[2]; - if (pipe(socket) != 0) { - Debug::log(LOG, "Unable to create pipe for fork"); - } - pid_t child ,grandchild; + 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){ + if (child < 0) { close(socket[0]); close(socket[1]); Debug::log(LOG, "Fail to create the first fork"); return; } - if (child == 0){ + if (child == 0) { // run in child grandchild = fork(); - if (grandchild==0){ + if (grandchild == 0) { // run in grandchild close(socket[0]); close(socket[1]); @@ -195,22 +195,22 @@ void CConfigManager::handleRawExec(const std::string& command, const std::string _exit(0); } close(socket[0]); - (void) write(socket[1], &grandchild, sizeof(grandchild)); + write(socket[1], &grandchild, sizeof(grandchild)); close(socket[1]); // exit child _exit(0); } // run in parent close(socket[1]); - (void) read(socket[0], &grandchild,sizeof(grandchild)); + read(socket[0], &grandchild, sizeof(grandchild)); close(socket[0]); // clear child and leave child to init waitpid(child, NULL, 0); - if (child<0){ + if (child < 0) { Debug::log(LOG, "Fail to create the second fork"); return; } - Debug::log(LOG, "Process created with pid %d",grandchild); + Debug::log(LOG, "Process created with pid %d", grandchild); } void CConfigManager::handleMonitor(const std::string& command, const std::string& args) { diff --git a/src/managers/KeybindManager.cpp b/src/managers/KeybindManager.cpp index c60d22a04..123e560c8 100644 --- a/src/managers/KeybindManager.cpp +++ b/src/managers/KeybindManager.cpp @@ -143,22 +143,22 @@ void CKeybindManager::spawn(std::string args) { Debug::log(LOG, "Executing %s", args.c_str()); int socket[2]; - if (pipe(socket) != 0) { - Debug::log(LOG, "Unable to create pipe for fork"); - } + if (pipe(socket) != 0) { + Debug::log(LOG, "Unable to create pipe for fork"); + } - pid_t child ,grandchild; + pid_t child, grandchild; child = fork(); - if (child<0){ + if (child < 0) { close(socket[0]); close(socket[1]); Debug::log(LOG, "Fail to create the first fork"); return; } - if (child == 0){ + if (child == 0) { // run in child grandchild = fork(); - if (grandchild==0){ + if (grandchild == 0) { // run in grandchild close(socket[0]); close(socket[1]); @@ -167,22 +167,22 @@ void CKeybindManager::spawn(std::string args) { _exit(0); } close(socket[0]); - (void) write(socket[1], &grandchild, sizeof(grandchild)); + write(socket[1], &grandchild, sizeof(grandchild)); close(socket[1]); // exit child _exit(0); } // run in parent close(socket[1]); - (void) read(socket[0], &grandchild,sizeof(grandchild)); + read(socket[0], &grandchild, sizeof(grandchild)); close(socket[0]); // clear child and leave child to init waitpid(child, NULL, 0); - if (child<0){ + if (child < 0) { Debug::log(LOG, "Fail to create the second fork"); return; } - Debug::log(LOG, "Process Created with pid %d",grandchild); + Debug::log(LOG, "Process Created with pid %d", grandchild); } void CKeybindManager::killActive(std::string args) {