Process: use read() on *NIX.

This commit is contained in:
Gunnar Beutner 2013-02-11 00:05:11 +01:00
parent bddd9ebf0b
commit a3f03928d7
2 changed files with 36 additions and 16 deletions

View File

@ -34,7 +34,12 @@ extern char **environ;
#endif /* _MSC_VER */ #endif /* _MSC_VER */
Process::Process(const vector<String>& arguments, const Dictionary::Ptr& extraEnvironment) Process::Process(const vector<String>& arguments, const Dictionary::Ptr& extraEnvironment)
: AsyncTask<Process, ProcessResult>(), m_FP(NULL) : AsyncTask<Process, ProcessResult>(),
#ifndef _MSC_VER
m_FD(-1)
#else /* _MSC_VER */
m_FP(NULL)
#endif /* _MSC_VER */
{ {
assert(Application::IsMainThread()); assert(Application::IsMainThread());
@ -165,7 +170,7 @@ void Process::WorkerThreadProc(int taskFd)
int fd; int fd;
BOOST_FOREACH(tie(fd, tuples::ignore), tasks) { BOOST_FOREACH(tie(fd, tuples::ignore), tasks) {
pfds[idx].fd = fd; pfds[idx].fd = fd;
pfds[idx].events = POLLIN; pfds[idx].events = POLLIN | POLLHUP;
idx++; idx++;
} }
@ -183,7 +188,7 @@ void Process::WorkerThreadProc(int taskFd)
#ifndef _MSC_VER #ifndef _MSC_VER
for (int i = 0; i < idx; i++) { for (int i = 0; i < idx; i++) {
if ((pfds[i].revents & POLLIN) == 0) if ((pfds[i].revents & (POLLIN|POLLHUP)) == 0)
continue; continue;
if (pfds[i].fd == taskFd) { if (pfds[i].fd == taskFd) {
@ -220,7 +225,12 @@ void Process::WorkerThreadProc(int taskFd)
try { try {
task->InitTask(); task->InitTask();
#ifdef _MSC_VER
int fd = fileno(task->m_FP); int fd = fileno(task->m_FP);
#else /* _MSC_VER */
int fd = task->m_FD;
#endif /* _MSC_VER */
if (fd >= 0) if (fd >= 0)
tasks[fd] = task; tasks[fd] = task;
} catch (...) { } catch (...) {
@ -267,7 +277,11 @@ void Process::InitTask(void)
{ {
m_Result.ExecutionStart = Utility::GetTime(); m_Result.ExecutionStart = Utility::GetTime();
#ifdef _MSC_VER
assert(m_FP == NULL); assert(m_FP == NULL);
#else /* _MSC_VER */
assert(m_FD == -1);
#endif /* _MSC_VER */
#ifdef _MSC_VER #ifdef _MSC_VER
String cmdLine; String cmdLine;
@ -340,10 +354,7 @@ void Process::InitTask(void)
(void) close(fds[1]); (void) close(fds[1]);
m_FP = fdopen(fds[0], "r"); m_FD = fds[0];
if (m_FP == NULL)
BOOST_THROW_EXCEPTION(PosixException("fdopen() failed.", errno));
} }
#endif /* _MSC_VER */ #endif /* _MSC_VER */
} }
@ -351,13 +362,22 @@ void Process::InitTask(void)
bool Process::RunTask(void) bool Process::RunTask(void)
{ {
char buffer[512]; char buffer[512];
size_t read = fread(buffer, 1, sizeof(buffer), m_FP); int rc;
if (read > 0)
m_OutputStream.write(buffer, read);
#ifndef _MSC_VER
rc = read(m_FD, buffer, sizeof(buffer));
#else /* _MSC_VER */
if (!feof(m_FP)) if (!feof(m_FP))
rc = fread(buffer, 1, sizeof(buffer), m_FP);
else
rc = 0;
#endif /* _MSC_VER */
if (rc > 0) {
m_OutputStream.write(buffer, rc);
return true; return true;
}
String output = m_OutputStream.str(); String output = m_OutputStream.str();
@ -366,7 +386,7 @@ bool Process::RunTask(void)
#ifdef _MSC_VER #ifdef _MSC_VER
status = _pclose(m_FP); status = _pclose(m_FP);
#else /* _MSC_VER */ #else /* _MSC_VER */
fclose(m_FP); (void) close(m_FD);
if (waitpid(m_Pid, &status, 0) != m_Pid) if (waitpid(m_Pid, &status, 0) != m_Pid)
BOOST_THROW_EXCEPTION(PosixException("waitpid() failed.", errno)); BOOST_THROW_EXCEPTION(PosixException("waitpid() failed.", errno));

View File

@ -59,12 +59,12 @@ private:
char **m_Arguments; char **m_Arguments;
char **m_Environment; char **m_Environment;
#ifndef _WIN32 #ifndef _MSC_VER
pid_t m_Pid; pid_t m_Pid;
#else /* _WIN32 */ int m_FD;
HANDLE m_Pid; #else /* _MSC_VER */
#endif /* _WIN32 */
FILE *m_FP; FILE *m_FP;
#endif /* _MSC_VER */
stringstream m_OutputStream; stringstream m_OutputStream;