[icinga-checkins] icinga.org: icinga2/master: Process: use read() on *NIX.

git at icinga.org git at icinga.org
Mon Feb 11 00:05:23 CET 2013


Module: icinga2
Branch: master
Commit: a3f03928d750924b7b423f5ebda8e45d5422906b
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=a3f03928d750924b7b423f5ebda8e45d5422906b

Author: Gunnar Beutner <gunnar.beutner at netways.de>
Date:   Mon Feb 11 00:05:11 2013 +0100

Process: use read() on *NIX.

---

 lib/base/process.cpp |   44 ++++++++++++++++++++++++++++++++------------
 lib/base/process.h   |    8 ++++----
 2 files changed, 36 insertions(+), 16 deletions(-)

diff --git a/lib/base/process.cpp b/lib/base/process.cpp
index 0efc585..d22a3bb 100644
--- a/lib/base/process.cpp
+++ b/lib/base/process.cpp
@@ -34,7 +34,12 @@ extern char **environ;
 #endif /* _MSC_VER */
 
 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());
 
@@ -165,7 +170,7 @@ void Process::WorkerThreadProc(int taskFd)
 		int fd;
 		BOOST_FOREACH(tie(fd, tuples::ignore), tasks) {
 			pfds[idx].fd = fd;
-			pfds[idx].events = POLLIN;
+			pfds[idx].events = POLLIN | POLLHUP;
 			idx++;
 		}
 
@@ -183,7 +188,7 @@ void Process::WorkerThreadProc(int taskFd)
 
 #ifndef _MSC_VER
 		for (int i = 0; i < idx; i++) {
-			if ((pfds[i].revents & POLLIN) == 0)
+			if ((pfds[i].revents & (POLLIN|POLLHUP)) == 0)
 				continue;
 
 			if (pfds[i].fd == taskFd) {
@@ -220,7 +225,12 @@ void Process::WorkerThreadProc(int taskFd)
 					try {
 						task->InitTask();
 
+#ifdef _MSC_VER
 						int fd = fileno(task->m_FP);
+#else /* _MSC_VER */
+						int fd = task->m_FD;
+#endif /* _MSC_VER */
+
 						if (fd >= 0)
 							tasks[fd] = task;
 					} catch (...) {
@@ -267,7 +277,11 @@ void Process::InitTask(void)
 {
 	m_Result.ExecutionStart = Utility::GetTime();
 
+#ifdef _MSC_VER
 	assert(m_FP == NULL);
+#else /* _MSC_VER */
+	assert(m_FD == -1);
+#endif /* _MSC_VER */
 
 #ifdef _MSC_VER
 	String cmdLine;
@@ -340,10 +354,7 @@ void Process::InitTask(void)
 
 		(void) close(fds[1]);
 
-		m_FP = fdopen(fds[0], "r");
-
-		if (m_FP == NULL)
-			BOOST_THROW_EXCEPTION(PosixException("fdopen() failed.", errno));
+		m_FD = fds[0];
 	}
 #endif /* _MSC_VER */
 }
@@ -351,13 +362,22 @@ void Process::InitTask(void)
 bool Process::RunTask(void)
 {
 	char buffer[512];
-	size_t read = fread(buffer, 1, sizeof(buffer), m_FP);
-
-	if (read > 0)
-		m_OutputStream.write(buffer, read);
+	int rc;
 
+#ifndef _MSC_VER
+	rc = read(m_FD, buffer, sizeof(buffer));
+#else /* _MSC_VER */
 	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;
+	}
 
 	String output = m_OutputStream.str();
 
@@ -366,7 +386,7 @@ bool Process::RunTask(void)
 #ifdef _MSC_VER
 	status = _pclose(m_FP);
 #else /* _MSC_VER */
-	fclose(m_FP);
+	(void) close(m_FD);
 
 	if (waitpid(m_Pid, &status, 0) != m_Pid)
 		BOOST_THROW_EXCEPTION(PosixException("waitpid() failed.", errno));
diff --git a/lib/base/process.h b/lib/base/process.h
index 9baf98a..c3c6614 100644
--- a/lib/base/process.h
+++ b/lib/base/process.h
@@ -59,12 +59,12 @@ private:
 	char **m_Arguments;
 	char **m_Environment;
 
-#ifndef _WIN32
+#ifndef _MSC_VER
 	pid_t m_Pid;
-#else /* _WIN32 */
-	HANDLE m_Pid;
-#endif /* _WIN32 */
+	int m_FD;
+#else /* _MSC_VER */
 	FILE *m_FP;
+#endif /* _MSC_VER */
 
 	stringstream m_OutputStream;
 





More information about the icinga-checkins mailing list