From 5cd2e4909c7237b1fc278dfc003f61342815b270 Mon Sep 17 00:00:00 2001 From: eidheim Date: Fri, 7 Aug 2015 17:31:17 +0200 Subject: [PATCH] Solved the whole zombie/crash issue. Though, for some reason, files/pipes/something are kept open, after a while one gets the message that too many files are open. --- src/terminal.cc | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/terminal.cc b/src/terminal.cc index 7bbf668..8103af4 100644 --- a/src/terminal.cc +++ b/src/terminal.cc @@ -57,7 +57,7 @@ pid_t popen3(const char *command, int *input_descriptor, int *output_descriptor, close(p_stderr[0]); dup2(p_stderr[1], 2); - //setpgid(0, 0); //TODO: get this working so we can execute without calling exec from sh -c (in that way we can call more complex commands) + setpgid(0, 0); execl("/bin/sh", "sh", "-c", command, NULL); perror("execl"); exit(1); @@ -194,7 +194,7 @@ void Terminal::async_execute(const std::string &command, const std::string &path boost_path=boost::filesystem::path(path); //TODO: Windows... - cd_path_and_command="cd "+boost_path.string()+" && exec "+command; + cd_path_and_command="cd "+boost_path.string()+" && "+command; } else cd_path_and_command=command; @@ -204,8 +204,11 @@ void Terminal::async_execute(const std::string &command, const std::string &path auto pid=popen3(cd_path_and_command.c_str(), &input_descriptor, &output_descriptor, &error_descriptor); async_execute_descriptors[pid]={input_descriptor, output_descriptor, error_descriptor}; async_and_sync_execute_mutex.unlock(); - if (pid<=0) - async_print("Error: Failed to run command" + command + "\n"); + if (pid<=0) { + async_print("Error: Failed to run command: " + command + "\n"); + if(callback) + callback(-1); + } else { std::thread error_thread([this, error_descriptor](){ char buffer[1024]; @@ -246,14 +249,10 @@ void Terminal::kill_executing() { close(async_execute_descriptors.at(pid.first)[0]); close(async_execute_descriptors.at(pid.first)[1]); close(async_execute_descriptors.at(pid.first)[2]); - kill(pid.first, SIGTERM); //signal_execl_exit is not always called after kill! - while(waitpid(pid.first, &status, WNOHANG) > 0) {} + kill(-pid.first, SIGINT); //signal_execl_exit is not always called after kill! + while(waitpid(-pid.first, &status, WNOHANG) > 0) {} async_execute_status[pid.first]=status; } - /*for(auto &pid: async_execute_descriptors) { - kill(-pid.first, SIGTERM); - while(waitpid(-pid.first, &status, WNOHANG) > 0) {} - }*/ async_and_sync_execute_mutex.unlock(); }