| 1 |
#include "UnixProcess.h" |
| 2 |
#if defined(__WXGTK__) || defined(__WXOSX__) |
| 3 |
#include "file_logger.h" |
| 4 |
|
| 5 |
#include <cl_command_event.h> |
| 6 |
#include <fileutils.h> |
| 7 |
#include <processreaderthread.h> |
| 8 |
#include <signal.h> |
| 9 |
#include <string.h> |
| 10 |
#include <sys/select.h> |
| 11 |
#include <sys/types.h> |
| 12 |
|
| 13 |
UnixProcess::UnixProcess(wxEvtHandler* owner, const wxArrayString& args) |
| 14 |
: m_owner(owner) |
| 15 |
{ |
| 16 |
m_goingDown.store(false); |
| 17 |
|
| 18 |
// Open the pipes |
| 19 |
if(!m_childStdin.Open() || !m_childStderr.Open() || !m_childStdout.Open()) { |
| 20 |
clERROR() << "Could not open redirection pipes." << strerror(errno) << endl; |
| 21 |
return; |
| 22 |
} |
| 23 |
|
| 24 |
child_pid = fork(); |
| 25 |
if(child_pid == -1) { |
| 26 |
clERROR() << _("Failed to start child process") << strerror(errno) << endl; |
| 27 |
} |
| 28 |
if(child_pid == 0) { |
| 29 |
// In child process |
| 30 |
dup2(m_childStdin.GetReadFd(), STDIN_FILENO); |
| 31 |
dup2(m_childStdout.GetWriteFd(), STDOUT_FILENO); |
| 32 |
dup2(m_childStderr.GetWriteFd(), STDERR_FILENO); |
| 33 |
m_childStdin.Close(); |
| 34 |
m_childStdout.Close(); |
| 35 |
m_childStderr.Close(); |
| 36 |
|
| 37 |
char** argv = new char*[args.size() + 1]; |
| 38 |
for(size_t i = 0; i < args.size(); ++i) { |
| 39 |
wxString wx_arg = args[i]; |
| 40 |
wx_arg.Trim(); |
| 41 |
if(wx_arg.StartsWith("\"") && wx_arg.EndsWith("\"") && wx_arg.size() >= 2) { |
| 42 |
wx_arg.Remove(0, 1).RemoveLast(); |
| 43 |
} |
| 44 |
|
| 45 |
std::string cstr_arg = FileUtils::ToStdString(wx_arg); |
| 46 |
argv[i] = new char[cstr_arg.length() + 1]; |
| 47 |
strcpy(argv[i], cstr_arg.c_str()); |
| 48 |
argv[i][cstr_arg.length()] = 0; |
| 49 |
} |
| 50 |
argv[args.size()] = 0; |
| 51 |
int result = execvp(argv[0], const_cast<char* const*>(argv)); |
| 52 |
int errNo = errno; |
| 53 |
if(result == -1) { |
| 54 |
// Note: no point writing to stdout here, it has been redirected |
| 55 |
clERROR() << "Error: Failed to launch program" << args << "." << strerror(errNo) << endl; |
| 56 |
exit(EXIT_FAILURE); |
| 57 |
} |
| 58 |
} else { |
| 59 |
// parent process |
| 60 |
m_childStdin.CloseReadFd(); |
| 61 |
m_childStdout.CloseWriteFd(); |
| 62 |
m_childStderr.CloseWriteFd(); |
| 63 |
|
| 64 |
// Start the reader and writer threads |
| 65 |
StartWriterThread(); |
| 66 |
StartReaderThread(); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
UnixProcess::~UnixProcess() |
| 71 |
{ |
| 72 |
Detach(); |
| 73 |
|
| 74 |
// Kill the child process (if it is still alive) |
| 75 |
Stop(); |
| 76 |
Wait(); |
| 77 |
} |
| 78 |
#define CHUNK_SIZE 1024 |
| 79 |
#define MAX_BUFF_SIZE (1024 * 2048) |
| 80 |
bool UnixProcess::ReadAll(int fd, std::string& content, int timeoutMilliseconds) |
| 81 |
{ |
| 82 |
fd_set rset; |
| 83 |
char buff[CHUNK_SIZE]; |
| 84 |
FD_ZERO(&rset); |
| 85 |
FD_SET(fd, &rset); |
| 86 |
|
| 87 |
int seconds = timeoutMilliseconds / 1000; |
| 88 |
int ms = timeoutMilliseconds % 1000; |
| 89 |
|
| 90 |
struct timeval tv = { seconds, ms * 1000 }; // 10 milliseconds timeout |
| 91 |
while(true) { |
| 92 |
int rc = ::select(fd + 1, &rset, nullptr, nullptr, &tv); |
| 93 |
if(rc > 0) { |
| 94 |
int len = read(fd, buff, (sizeof(buff) - 1)); |
| 95 |
if(len > 0) { |
| 96 |
buff[len] = 0; |
| 97 |
content.append(buff); |
| 98 |
if(content.length() >= MAX_BUFF_SIZE) { |
| 99 |
return true; |
| 100 |
} |
| 101 |
// clear the tv struct so next select() call will return immediately |
| 102 |
tv.tv_usec = 0; |
| 103 |
tv.tv_sec = 0; |
| 104 |
FD_ZERO(&rset); |
| 105 |
FD_SET(fd, &rset); |
| 106 |
continue; |
| 107 |
} |
| 108 |
} else if(rc == 0) { |
| 109 |
// timeout |
| 110 |
return true; |
| 111 |
} |
| 112 |
break; |
| 113 |
} |
| 114 |
// error |
| 115 |
return false; |
| 116 |
} |
| 117 |
|
| 118 |
bool UnixProcess::Write(int fd, const std::string& message, std::atomic_bool& shutdown) |
| 119 |
{ |
| 120 |
int bytes = 0; |
| 121 |
std::string tmp = message; |
| 122 |
const int chunkSize = 4096; |
| 123 |
while(!tmp.empty() && !shutdown.load()) { |
| 124 |
errno = 0; |
| 125 |
bytes = ::write(fd, tmp.c_str(), tmp.length() > chunkSize ? chunkSize : tmp.length()); |
| 126 |
int errCode = errno; |
| 127 |
if(bytes < 0) { |
| 128 |
if((errCode == EWOULDBLOCK) || (errCode == EAGAIN)) { |
| 129 |
std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 130 |
} else if(errCode == EINTR) { |
| 131 |
continue; |
| 132 |
} else { |
| 133 |
break; |
| 134 |
} |
| 135 |
} else if(bytes) { |
| 136 |
tmp.erase(0, bytes); |
| 137 |
} |
| 138 |
} |
| 139 |
LOG_IF_TRACE { clDEBUG1() << "Wrote message of size:" << message.length() << endl; } |
| 140 |
return tmp.empty(); |
| 141 |
} |
| 142 |
|
| 143 |
int UnixProcess::Wait() |
| 144 |
{ |
| 145 |
if(child_pid != wxNOT_FOUND) { |
| 146 |
int status = 0; |
| 147 |
waitpid(child_pid, &status, WNOHANG); |
| 148 |
return WEXITSTATUS(status); |
| 149 |
} else { |
| 150 |
return 0; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
void UnixProcess::Stop() |
| 155 |
{ |
| 156 |
if(child_pid != wxNOT_FOUND) { |
| 157 |
::kill(child_pid, SIGTERM); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
void UnixProcess::Write(const std::string& message) |
| 162 |
{ |
| 163 |
if(!m_writerThread) { |
| 164 |
return; |
| 165 |
} |
| 166 |
m_outgoingQueue.Post(message); |
| 167 |
} |
| 168 |
|
| 169 |
void UnixProcess::StartWriterThread() |
| 170 |
{ |
| 171 |
m_writerThread = new std::thread( |
| 172 |
[](UnixProcess* process, int fd) { |
| 173 |
while(!process->m_goingDown.load()) { |
| 174 |
std::string buffer; |
| 175 |
if(process->m_outgoingQueue.ReceiveTimeout(10, buffer) == wxMSGQUEUE_NO_ERROR) { |
| 176 |
UnixProcess::Write(fd, buffer, std::ref(process->m_goingDown)); |
| 177 |
} |
| 178 |
} |
| 179 |
clDEBUG() << "UnixProcess writer thread: going down" << endl; |
| 180 |
}, |
| 181 |
this, m_childStdin.GetWriteFd()); |
| 182 |
} |
| 183 |
|
| 184 |
void UnixProcess::StartReaderThread() |
| 185 |
{ |
| 186 |
m_readerThread = new std::thread( |
| 187 |
[](UnixProcess* process, int stdoutFd, int stderrFd) { |
| 188 |
while(!process->m_goingDown.load()) { |
| 189 |
std::string content; |
| 190 |
if(!ReadAll(stdoutFd, content, 10)) { |
| 191 |
clProcessEvent evt(wxEVT_ASYNC_PROCESS_TERMINATED); |
| 192 |
wxString error_message; |
| 193 |
int exit_code = process->Wait(); |
| 194 |
error_message << "Process exit code (" << exit_code << "):" << strerror(exit_code); |
| 195 |
evt.SetString(error_message); |
| 196 |
process->m_owner->AddPendingEvent(evt); |
| 197 |
break; |
| 198 |
} else if(!content.empty()) { |
| 199 |
clProcessEvent evt(wxEVT_ASYNC_PROCESS_OUTPUT); |
| 200 |
evt.SetOutput(wxString() << content); |
| 201 |
process->m_owner->AddPendingEvent(evt); |
| 202 |
} |
| 203 |
content.clear(); |
| 204 |
if(!ReadAll(stderrFd, content, 10)) { |
| 205 |
clProcessEvent evt(wxEVT_ASYNC_PROCESS_TERMINATED); |
| 206 |
process->m_owner->AddPendingEvent(evt); |
| 207 |
break; |
| 208 |
} else if(!content.empty()) { |
| 209 |
clProcessEvent evt(wxEVT_ASYNC_PROCESS_STDERR); |
| 210 |
evt.SetOutput(wxString() << content); |
| 211 |
process->m_owner->AddPendingEvent(evt); |
| 212 |
} |
| 213 |
} |
| 214 |
clDEBUG() << "UnixProcess reader thread: going down" << endl; |
| 215 |
}, |
| 216 |
this, m_childStdout.GetReadFd(), m_childStderr.GetReadFd()); |
| 217 |
} |
| 218 |
|
| 219 |
void UnixProcess::Detach() |
| 220 |
{ |
| 221 |
m_goingDown.store(true); |
| 222 |
if(m_writerThread) { |
| 223 |
m_writerThread->join(); |
| 224 |
wxDELETE(m_writerThread); |
| 225 |
} |
| 226 |
if(m_readerThread) { |
| 227 |
m_readerThread->join(); |
| 228 |
wxDELETE(m_readerThread); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
#endif // OSX & GTK |