Browse Source

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.

merge-requests/365/head
eidheim 10 years ago
parent
commit
5cd2e4909c
  1. 19
      src/terminal.cc

19
src/terminal.cc

@ -57,7 +57,7 @@ pid_t popen3(const char *command, int *input_descriptor, int *output_descriptor,
close(p_stderr[0]); close(p_stderr[0]);
dup2(p_stderr[1], 2); 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); execl("/bin/sh", "sh", "-c", command, NULL);
perror("execl"); perror("execl");
exit(1); exit(1);
@ -194,7 +194,7 @@ void Terminal::async_execute(const std::string &command, const std::string &path
boost_path=boost::filesystem::path(path); boost_path=boost::filesystem::path(path);
//TODO: Windows... //TODO: Windows...
cd_path_and_command="cd "+boost_path.string()+" && exec "+command; cd_path_and_command="cd "+boost_path.string()+" && "+command;
} }
else else
cd_path_and_command=command; 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); 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_execute_descriptors[pid]={input_descriptor, output_descriptor, error_descriptor};
async_and_sync_execute_mutex.unlock(); async_and_sync_execute_mutex.unlock();
if (pid<=0) if (pid<=0) {
async_print("Error: Failed to run command" + command + "\n"); async_print("Error: Failed to run command: " + command + "\n");
if(callback)
callback(-1);
}
else { else {
std::thread error_thread([this, error_descriptor](){ std::thread error_thread([this, error_descriptor](){
char buffer[1024]; 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)[0]);
close(async_execute_descriptors.at(pid.first)[1]); close(async_execute_descriptors.at(pid.first)[1]);
close(async_execute_descriptors.at(pid.first)[2]); close(async_execute_descriptors.at(pid.first)[2]);
kill(pid.first, SIGTERM); //signal_execl_exit is not always called after kill! kill(-pid.first, SIGINT); //signal_execl_exit is not always called after kill!
while(waitpid(pid.first, &status, WNOHANG) > 0) {} while(waitpid(-pid.first, &status, WNOHANG) > 0) {}
async_execute_status[pid.first]=status; 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(); async_and_sync_execute_mutex.unlock();
} }

Loading…
Cancel
Save