mirror of
https://github.com/hyprwm/Hyprland
synced 2024-12-23 09:09:48 +01:00
Fix generating zombie process
This commit is contained in:
parent
36ea12b315
commit
5fa61e5a54
2 changed files with 78 additions and 5 deletions
|
@ -170,11 +170,47 @@ void CConfigManager::handleRawExec(const std::string& command, const std::string
|
||||||
|
|
||||||
Debug::log(LOG, "Config executing %s", toExec.c_str());
|
Debug::log(LOG, "Config executing %s", toExec.c_str());
|
||||||
|
|
||||||
if (fork() == 0) {
|
int socket[2];
|
||||||
execl("/bin/sh", "/bin/sh", "-c", toExec.c_str(), nullptr);
|
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);
|
_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) {
|
void CConfigManager::handleMonitor(const std::string& command, const std::string& args) {
|
||||||
|
|
|
@ -141,11 +141,48 @@ void CKeybindManager::spawn(std::string args) {
|
||||||
args = "WAYLAND_DISPLAY=" + std::string(g_pCompositor->m_szWLDisplaySocket) + " " + args;
|
args = "WAYLAND_DISPLAY=" + std::string(g_pCompositor->m_szWLDisplaySocket) + " " + args;
|
||||||
|
|
||||||
Debug::log(LOG, "Executing %s", args.c_str());
|
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);
|
_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) {
|
void CKeybindManager::killActive(std::string args) {
|
||||||
|
|
Loading…
Reference in a new issue