[icinga-checkins] icinga.org: icinga2/master: Spawn processes asynchronously.

git at icinga.org git at icinga.org
Wed Mar 6 15:41:54 CET 2013


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

Author: Gunnar Beutner <gunnar.beutner at netways.de>
Date:   Wed Mar  6 15:41:41 2013 +0100

Spawn processes asynchronously.

---

 lib/base/process-unix.cpp |   16 +++++++++-------
 lib/base/process.h        |    4 ++++
 2 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/lib/base/process-unix.cpp b/lib/base/process-unix.cpp
index 2522550..f0bba58 100644
--- a/lib/base/process-unix.cpp
+++ b/lib/base/process-unix.cpp
@@ -128,7 +128,7 @@ void Process::WorkerThreadProc(int taskFd)
 					/* Read one byte for every task we take from the pending tasks list. */
 					char buffer[MaxTasksPerThread];
 
-					assert(want =< sizeof(buffer));
+					assert(want <= sizeof(buffer));
 
 					int have = read(taskFd, &buffer, want);
 
@@ -227,6 +227,12 @@ void Process::InitTask(void)
 	Utility::SetCloExec(fds[1]);
 #endif /* HAVE_PIPE2 */
 
+	Utility::QueueAsyncCallback(boost::bind(&Process::InitTaskHelper, this, fds[0], fds[1]));
+	m_FD = fds[0];
+}
+
+void Process::InitTaskHelper(int rdfd, int wrfd)
+{
 	// build argv
 	char **argv = new char *[m_Arguments.size() + 1];
 
@@ -278,14 +284,11 @@ void Process::InitTask(void)
 	if (m_Pid == 0) {
 		// child process
 
-		if (dup2(fds[1], STDOUT_FILENO) < 0 || dup2(fds[1], STDERR_FILENO) < 0) {
+		if (dup2(wrfd, STDOUT_FILENO) < 0 || dup2(wrfd, STDERR_FILENO) < 0) {
 			perror("dup2() failed.");
 			_exit(128);
 		}
 
-		(void) close(fds[0]);
-		(void) close(fds[1]);
-
 		if (execvpe(argv[0], argv, envp) < 0) {
 			perror("execvpe() failed.");
 			_exit(128);
@@ -308,8 +311,7 @@ void Process::InitTask(void)
 
 	delete [] envp;
 
-	m_FD = fds[0];
-	(void) close(fds[1]);
+	(void) close(wrfd);
 }
 
 bool Process::RunTask(void)
diff --git a/lib/base/process.h b/lib/base/process.h
index 1b0dbd1..517975a 100644
--- a/lib/base/process.h
+++ b/lib/base/process.h
@@ -92,6 +92,10 @@ private:
 	void InitTask(void);
 	bool RunTask(void);
 
+#ifndef _WIN32
+	void InitTaskHelper(int rdfd, int wrfd);
+#endif /* _WIN32 */
+
 	static boost::once_flag m_ThreadOnce;
 	static void Initialize(void);
 };





More information about the icinga-checkins mailing list