Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r62191 - in sandbox/SOC/2010/process/boost/process: . detail
From: fotanus_at_[hidden]
Date: 2010-05-25 01:52:56


Author: fotanus
Date: 2010-05-25 01:52:55 EDT (Tue, 25 May 2010)
New Revision: 62191
URL: http://svn.boost.org/trac/boost/changeset/62191

Log:
Half of work done; Need to solve a nasty bug.
Text files modified:
   sandbox/SOC/2010/process/boost/process/detail/file_handle.hpp | 20 +++-
   sandbox/SOC/2010/process/boost/process/detail/pipe.hpp | 102 +++++++++++-----------
   sandbox/SOC/2010/process/boost/process/detail/posix_helpers.hpp | 4
   sandbox/SOC/2010/process/boost/process/detail/stream_detail.hpp | 12 ++
   sandbox/SOC/2010/process/boost/process/detail/win32_helpers.hpp | 10 +-
   sandbox/SOC/2010/process/boost/process/operations.hpp | 173 ++++++++++++++++++++++-----------------
   6 files changed, 178 insertions(+), 143 deletions(-)

Modified: sandbox/SOC/2010/process/boost/process/detail/file_handle.hpp
==============================================================================
--- sandbox/SOC/2010/process/boost/process/detail/file_handle.hpp (original)
+++ sandbox/SOC/2010/process/boost/process/detail/file_handle.hpp 2010-05-25 01:52:55 EDT (Tue, 25 May 2010)
@@ -33,6 +33,7 @@
 #include <boost/assert.hpp>
 #include <boost/system/system_error.hpp>
 #include <boost/throw_exception.hpp>
+#include <stdio.h>
 
 namespace boost {
 namespace process {
@@ -322,12 +323,14 @@
         static file_handle win32_dup(HANDLE h, bool inheritable){
                 HANDLE h2;
 
- if (!::DuplicateHandle(::GetCurrentProcess(), h,
- ::GetCurrentProcess(), &h2, 0,
- inheritable ? TRUE : FALSE,
- DUPLICATE_SAME_ACCESS))
-
- boost::throw_exception(boost::system::system_error(boost::system::error_code(::GetLastError(), boost::system::get_system_category()), "boost::process::detail::file_handle::win32_dup: DuplicateHandle failed"));
+ if ( ::DuplicateHandle(::GetCurrentProcess(), h,
+ ::GetCurrentProcess(), &h2, 0,
+ false , DUPLICATE_SAME_ACCESS) ==0 )
+ boost::throw_exception(boost::system::system_error(
+ boost::system::error_code(::GetLastError(),
+ boost::system::get_system_category()),
+ "boost::process::detail::file_handle::win32_dup: DuplicateHandle failed"));
+
 
                 return file_handle(h2);
         }
@@ -352,14 +355,17 @@
 
         static file_handle win32_dup_std(DWORD d, bool inheritable){
                 BOOST_ASSERT(d == STD_INPUT_HANDLE || d == STD_OUTPUT_HANDLE || d == STD_ERROR_HANDLE);
+
 
                 HANDLE h = ::GetStdHandle(d);
                 if (h == INVALID_HANDLE_VALUE)
                         boost::throw_exception(boost::system::system_error(boost::system::error_code(::GetLastError(), boost::system::get_system_category()), "boost::process::detail::file_handle::win32_std: GetStdHandle failed"));
 
- return win32_dup(h, inheritable);
+ file_handle dh = win32_dup(h, inheritable);
+ return dh;
         }
 
+
         /**
          * Changes the file handle's inheritable flag.
          *

Modified: sandbox/SOC/2010/process/boost/process/detail/pipe.hpp
==============================================================================
--- sandbox/SOC/2010/process/boost/process/detail/pipe.hpp (original)
+++ sandbox/SOC/2010/process/boost/process/detail/pipe.hpp 2010-05-25 01:52:55 EDT (Tue, 25 May 2010)
@@ -77,58 +77,58 @@
      * \throw boost::system::system_error If the anonymous %pipe
      * creation fails.
      */
- pipe()
- {
- file_handle::handle_type hs[2];
+ pipe(){
+ file_handle::handle_type hs[2];
 
-#if defined(BOOST_POSIX_API)
- if (::pipe(hs) == -1)
- boost::throw_exception(boost::system::system_error(boost::system::error_code(errno, boost::system::get_system_category()), "boost::process::detail::pipe::pipe: pipe(2) failed"));
-#elif defined(BOOST_WINDOWS_API)
- SECURITY_ATTRIBUTES sa;
- ZeroMemory(&sa, sizeof(sa));
- sa.nLength = sizeof(sa);
- sa.lpSecurityDescriptor = NULL;
- sa.bInheritHandle = FALSE;
-
-# if defined(BOOST_PROCESS_WINDOWS_USE_NAMED_PIPE)
- static unsigned int nextid = 0;
- std::string pipe = "\\\\.\\pipe\\boost_process_" + boost::lexical_cast<std::string>(::GetCurrentProcessId()) + "_" + boost::lexical_cast<std::string>(nextid++);
- hs[0] = ::CreateNamedPipeA(pipe.c_str(), PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED, 0, 1, 8192, 8192, 0, &sa);
- if (hs[0] == INVALID_HANDLE_VALUE)
- boost::throw_exception(boost::system::system_error(::GetLastError(), boost::system::system_category, "boost::process::detail::pipe::pipe: CreateNamedPipe failed"));
- hs[1] = ::CreateFileA(pipe.c_str(), GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
- if (hs[1] == INVALID_HANDLE_VALUE)
- boost::throw_exception(boost::system::system_error(::GetLastError(), boost::system::system_category, "boost::process::detail::pipe::pipe: CreateFile failed"));
-
- OVERLAPPED overlapped;
- ZeroMemory(&overlapped, sizeof(overlapped));
- overlapped.hEvent = ::CreateEvent(NULL, TRUE, FALSE, NULL);
- if (!overlapped.hEvent)
- boost::throw_exception(boost::system::system_error(::GetLastError(), boost::system::system_category, "boost::process::detail::pipe::pipe: CreateEvent failed"));
- BOOL b = ::ConnectNamedPipe(hs[0], &overlapped);
- if (!b)
- {
- if (::GetLastError() == ERROR_IO_PENDING)
- {
- if (::WaitForSingleObject(overlapped.hEvent, INFINITE) == WAIT_FAILED)
- {
- ::CloseHandle(overlapped.hEvent);
- boost::throw_exception(boost::system::system_error(::GetLastError(), boost::system::system_category, "boost::process::detail::pipe::pipe: WaitForSingleObject failed"));
- }
- }
- else if (::GetLastError() != ERROR_PIPE_CONNECTED)
- {
- ::CloseHandle(overlapped.hEvent);
- boost::throw_exception(boost::system::system_error(::GetLastError(), boost::system::system_category, "boost::process::detail::pipe::pipe: ConnectNamedPipe failed"));
- }
- }
- ::CloseHandle(overlapped.hEvent);
-# else
- if (!::CreatePipe(&hs[0], &hs[1], &sa, 0))
- boost::throw_exception(boost::system::system_error(::GetLastError(), boost::system::system_category, "boost::process::detail::pipe::pipe: CreatePipe failed"));
-# endif
-#endif
+ #if defined(BOOST_POSIX_API)
+ if (::pipe(hs) == -1)
+ boost::throw_exception(boost::system::system_error(
+ boost::system::error_code(
+ errno, boost::system::get_system_category()),
+ "boost::process::detail::pipe::pipe: pipe(2) failed"));
+ #elif defined(BOOST_WINDOWS_API)
+
+ SECURITY_ATTRIBUTES sa;
+ ZeroMemory(&sa, sizeof(sa));
+ sa.nLength = sizeof(sa);
+
+ sa.lpSecurityDescriptor = NULL;
+ sa.bInheritHandle = TRUE;
+
+ #if defined(BOOST_PROCESS_WINDOWS_USE_NAMED_PIPE)
+ static unsigned int nextid = 0;
+ std::string pipe = "\\\\.\\pipe\\boost_process_" + boost::lexical_cast<std::string>(::GetCurrentProcessId()) + "_" + boost::lexical_cast<std::string>(nextid++);
+ hs[0] = ::CreateNamedPipeA(pipe.c_str(), PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED, 0, 1, 8192, 8192, 0, &sa);
+ if (hs[0] == INVALID_HANDLE_VALUE)
+ boost::throw_exception(boost::system::system_error(::GetLastError(), boost::system::system_category, "boost::process::detail::pipe::pipe: CreateNamedPipe failed"));
+ hs[1] = ::CreateFileA(pipe.c_str(), GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
+ if (hs[1] == INVALID_HANDLE_VALUE)
+ boost::throw_exception(boost::system::system_error(::GetLastError(), boost::system::system_category, "boost::process::detail::pipe::pipe: CreateFile failed"));
+
+ OVERLAPPED overlapped;
+ ZeroMemory(&overlapped, sizeof(overlapped));
+ overlapped.hEvent = ::CreateEvent(NULL, TRUE, FALSE, NULL);
+ if (!overlapped.hEvent)
+ boost::throw_exception(boost::system::system_error(::GetLastError(), boost::system::system_category, "boost::process::detail::pipe::pipe: CreateEvent failed"));
+ BOOL b = ::ConnectNamedPipe(hs[0], &overlapped);
+ if (!b){
+ if (::GetLastError() == ERROR_IO_PENDING){
+ if (::WaitForSingleObject(overlapped.hEvent, INFINITE) == WAIT_FAILED){
+ ::CloseHandle(overlapped.hEvent);
+ boost::throw_exception(boost::system::system_error(::GetLastError(), boost::system::system_category, "boost::process::detail::pipe::pipe: WaitForSingleObject failed"));
+ }
+ }
+ else if (::GetLastError() != ERROR_PIPE_CONNECTED){
+ ::CloseHandle(overlapped.hEvent);
+ boost::throw_exception(boost::system::system_error(::GetLastError(), boost::system::system_category, "boost::process::detail::pipe::pipe: ConnectNamedPipe failed"));
+ }
+ }
+ ::CloseHandle(overlapped.hEvent);
+ #else
+ if (!::CreatePipe(&hs[0], &hs[1], &sa, 0))
+ boost::throw_exception(boost::system::system_error(::GetLastError(), boost::system::system_category, "boost::process::detail::pipe::pipe: CreatePipe failed"));
+ # endif
+ #endif
 
         read_end_ = file_handle(hs[0]);
         write_end_ = file_handle(hs[1]);

Modified: sandbox/SOC/2010/process/boost/process/detail/posix_helpers.hpp
==============================================================================
--- sandbox/SOC/2010/process/boost/process/detail/posix_helpers.hpp (original)
+++ sandbox/SOC/2010/process/boost/process/detail/posix_helpers.hpp 2010-05-25 01:52:55 EDT (Tue, 25 May 2010)
@@ -76,9 +76,7 @@
                            }
 
                 case closed:
- #if defined(BOOST_POSIX_API)
- ::close(s.stream_handle);
- #endif
+ ::close(s.stream_handle);
                         break;
 
                 case inherit:

Modified: sandbox/SOC/2010/process/boost/process/detail/stream_detail.hpp
==============================================================================
--- sandbox/SOC/2010/process/boost/process/detail/stream_detail.hpp (original)
+++ sandbox/SOC/2010/process/boost/process/detail/stream_detail.hpp 2010-05-25 01:52:55 EDT (Tue, 25 May 2010)
@@ -70,7 +70,13 @@
  */
 
 struct stream_detail {
- detail::file_handle::handle_type stream_handle;
+
+ #if defined(BOOST_POSIX_API)
+ int stream_handle;
+ #elif defined(BOOST_WINDOWS_API)
+ DWORD stream_handle;
+ #endif
+
         struct stream_object object;
         std_stream_type stream_type;
         stream_behavior behavior;
@@ -84,7 +90,7 @@
                                 #if defined(BOOST_POSIX_API)
                                         stream_handle = STDIN_FILENO;
                                 #elif defined(BOOST_WINDOWS_API)
- stream_handle = STD_INPUT_HANDLE;
+ stream_handle = STD_INPUT_HANDLE;
                                 #endif
                                 break;
                         }
@@ -109,7 +115,9 @@
                                 BOOST_ASSERT(false);
                         }
 
+
                 }
+ std::cout << "Inicializado com: " << stream_handle << std::endl;
         }
 };
 

Modified: sandbox/SOC/2010/process/boost/process/detail/win32_helpers.hpp
==============================================================================
--- sandbox/SOC/2010/process/boost/process/detail/win32_helpers.hpp (original)
+++ sandbox/SOC/2010/process/boost/process/detail/win32_helpers.hpp 2010-05-25 01:52:55 EDT (Tue, 25 May 2010)
@@ -179,12 +179,12 @@
 
                 case capture:{
                         if(sd.stream_type == stdin_type){
- sd.object.pipe_->rend().win32_set_inheritable(true);
- return_handle = sd.object.pipe_->rend();
+ sd.object.pipe_.rend().win32_set_inheritable(true);
+ return_handle = sd.object.pipe_.rend();
                         }
                         else{
- sd.object.pipe_->wend().win32_set_inheritable(true);
- return_handle = sd.object.pipe_->wend();
+ sd.object.pipe_.wend().win32_set_inheritable(true);
+ return_handle = sd.object.pipe_.wend();
                         }
 
                         break;
@@ -198,7 +198,6 @@
 
 
         file_handle h;
-
         if(return_handle.valid()){
                 switch(sd.stream_type){
                         case stdin_type:{
@@ -207,6 +206,7 @@
                         }
                         case stdout_type:{
                                 (*si).hStdOutput = return_handle.get();
+ WriteFile(si->hStdOutput , "Test1\n", 6, NULL, NULL);
                                 break;
                         }
                         case stderr_type:{

Modified: sandbox/SOC/2010/process/boost/process/operations.hpp
==============================================================================
--- sandbox/SOC/2010/process/boost/process/operations.hpp (original)
+++ sandbox/SOC/2010/process/boost/process/operations.hpp 2010-05-25 01:52:55 EDT (Tue, 25 May 2010)
@@ -219,6 +219,7 @@
 
         detail::file_handle fhstdin, fhstdout, fhstderr;
 
+ //start structures that represents a std stream.
         detail::stream_detail stdin_stream(detail::stdin_type),
                                 stdout_stream(detail::stdout_type),
                                 stderr_stream(detail::stderr_type);
@@ -227,117 +228,139 @@
         stdout_stream.behavior = ctx.stdout_behavior;
         stderr_stream.behavior = ctx.stderr_behavior;
 
+ //adjust process name
         std::string p_name = ctx.process_name.empty() ? executable : ctx.process_name;
         args.insert(args.begin(),p_name);
      
 
-#if defined(BOOST_POSIX_API)
+ #if defined(BOOST_POSIX_API)
 
- static sem_t * sem_stream_config;
+ //TODO: do we need this semaphore?
+ static sem_t * sem_stream_config;
 
- ::sem_unlink("boost_sem_stream_config");
- sem_stream_config = ::sem_open("boost_sem_stream_config",O_CREAT,511,0);
- BOOST_ASSERT( sem_stream_config != SEM_FAILED);
+ ::sem_unlink("boost_sem_stream_config");
+ sem_stream_config = ::sem_open("boost_sem_stream_config",O_CREAT,511,0);
+ BOOST_ASSERT( sem_stream_config != SEM_FAILED);
 
- child::id_type pid = ::fork();
+ child::id_type pid = ::fork();
 
- if (pid == -1){
- boost::throw_exception(
- boost::system::system_error(
- boost::system::error_code(errno, boost::system::get_system_category()),
- "boost::process::detail::posix_start: fork(2) failed"));
- }
+ if (pid == -1){
+ boost::throw_exception(
+ boost::system::system_error(
+ boost::system::error_code(errno, boost::system::get_system_category()),
+ "boost::process::detail::posix_start: fork(2) failed"));
+ }
 
- if (pid == 0){
+ if (pid == 0){
 
- detail::configure_posix_stream(stdin_stream);
- detail::configure_posix_stream(stdout_stream);
- detail::configure_posix_stream(stderr_stream);
- ::sem_post(sem_stream_config);
+ //configure child pipes
+ detail::configure_posix_stream(stdin_stream);
+ detail::configure_posix_stream(stdout_stream);
+ detail::configure_posix_stream(stderr_stream);
+ ::sem_post(sem_stream_config);
 
- std::pair<std::size_t, char**> argcv = detail::collection_to_posix_argv(args);
- char **envp = detail::environment_to_envp(ctx.environment);
+ //execve call
+ std::pair<std::size_t, char**> argcv = detail::collection_to_posix_argv(args);
+ char **envp = detail::environment_to_envp(ctx.environment);
 
- ::execve(executable.c_str(), argcv.second, envp);
+ ::execve(executable.c_str(), argcv.second, envp);
 
- BOOST_ASSERT(false);
+ BOOST_ASSERT(false);
 
- }
+ }
 
- BOOST_ASSERT(pid > 0);
- ::sem_wait(sem_stream_config);
+ BOOST_ASSERT(pid > 0);
+ ::sem_wait(sem_stream_config);
 
- if(ctx.stdin_behavior == capture){
- stdin_stream.object.pipe_.rend().close();
- fhstdin = stdin_stream.object.pipe_.wend().release();
- BOOST_ASSERT(fhstdin.valid());
- }
+ //adjust father pipes
+ if(ctx.stdin_behavior == capture){
+ stdin_stream.object.pipe_.rend().close();
+ fhstdin = stdin_stream.object.pipe_.wend().release();
+ BOOST_ASSERT(fhstdin.valid());
+ }
 
- if(ctx.stdout_behavior == capture){
- stdout_stream.object.pipe_.wend().close();
- fhstdout = stdout_stream.object.pipe_.wend().release();
- BOOST_ASSERT(fhstdout.valid());
- }
+ if(ctx.stdout_behavior == capture){
+ stdout_stream.object.pipe_.wend().close();
+ fhstdout = stdout_stream.object.pipe_.wend().release();
+ BOOST_ASSERT(fhstdout.valid());
+ }
 
- if(ctx.stderr_behavior == capture){
- stderr_stream.object.pipe_.wend().close();
- fhstderr = stderr_stream.object.pipe_.wend().release();
- BOOST_ASSERT(fhstderr.valid());
- }
+ if(ctx.stderr_behavior == capture){
+ stderr_stream.object.pipe_.wend().close();
+ fhstderr = stderr_stream.object.pipe_.wend().release();
+ BOOST_ASSERT(fhstderr.valid());
+ }
 
 
 
- return child(pid, fhstdin, fhstdout, fhstderr);
+ return child(pid, fhstdin, fhstdout, fhstderr);
 
 
-#elif defined(BOOST_WINDOWS_API)
+ #elif defined(BOOST_WINDOWS_API)
+
 
+ //Set up the pipes when needed for the current process.
+ if (stdin_stream.behavior == capture)
+ fhstdin = stdin_stream.object.pipe_.wend();
+ if (stdout_stream.behavior == capture)
+ fhstdout = stdout_stream.object.pipe_.rend();
+ if (stderr_stream.behavior == capture)
+ fhstderr = stderr_stream.object.pipe_.rend();
 
- //Set up the pipes when needed for the current process.
- if (stdin_stream.behavior == capture)
- fhstdin = stdin_stream.object.pipe_->wend();
- if (stdin_stream.behavior == capture)
- fhstdin = stdin_stream.object.pipe_->wend();
- if (stdin_stream.behavior == capture)
- fhstdin = stdin_stream.object.pipe_->wend();
+ //define startup info from the new child
+ STARTUPINFOA startup_info;
+ ::ZeroMemory(&startup_info, sizeof(startup_info));
+ startup_info.cb = sizeof(startup_info);
 
- //define startup info from the new child
- STARTUPINFOA startup_info;
- ::ZeroMemory(&startup_info, sizeof(startup_info));
- startup_info.cb = sizeof(startup_info);
-
- //configure std stream info for the child
- configure_win32_stream(stdin_stream, &startup_info);
- configure_win32_stream(stdout_stream, &startup_info);
- configure_win32_stream(stderr_stream, &startup_info);
-
+ startup_info.dwFlags |= STARTF_USESTDHANDLES;
+
+ //configure std stream info for the child
+ configure_win32_stream(stdin_stream, &startup_info);
+ configure_win32_stream(stdout_stream, &startup_info);
+ configure_win32_stream(stderr_stream, &startup_info);
 
- //define basic info to start the process
- PROCESS_INFORMATION pi;
- ::ZeroMemory(&pi, sizeof(pi));
+ WriteFile(startup_info.hStdOutput , "Test2\n", 6, NULL, NULL);
+
 
- boost::shared_array<char> cmdline = detail::collection_to_win32_cmdline(args);
+ //define process info and create it
+ PROCESS_INFORMATION pi;
+ ::ZeroMemory(&pi, sizeof(pi));
 
- boost::scoped_array<char> exe(new char[executable.size() + 1]);
- ::strcpy_s(exe.get(), executable.size() + 1, executable.c_str());
+ boost::shared_array<char> cmdline = detail::collection_to_win32_cmdline(args);
 
- boost::scoped_array<char> workdir(new char[ctx.work_dir.size() + 1]);
- ::strcpy_s(workdir.get(), ctx.work_dir.size() + 1, ctx.work_dir.c_str());
+ boost::scoped_array<char> exe(new char[executable.size() + 1]);
+ ::strcpy_s(exe.get(), executable.size() + 1, executable.c_str());
 
- boost::shared_array<char> envstrs = detail::environment_to_win32_strings(ctx.environment);
+ boost::scoped_array<char> workdir(new char[ctx.work_dir.size() + 1]);
+ ::strcpy_s(workdir.get(), ctx.work_dir.size() + 1, ctx.work_dir.c_str());
 
+ boost::shared_array<char> envstrs = detail::environment_to_win32_strings(ctx.environment);
 
- if ( ! ::CreateProcessA(exe.get(), cmdline.get(),
- NULL, NULL, TRUE, 0, envstrs.get(), workdir.get(),
- &startup_info, &pi))
- boost::throw_exception(boost::system::system_error(boost::system::error_code(::GetLastError(), boost::system::get_system_category()), "boost::process::detail::win32_start: CreateProcess failed"));
+
 
- if (! ::CloseHandle(pi.hThread))
- boost::throw_exception(boost::system::system_error(boost::system::error_code(::GetLastError(), boost::system::get_system_category()), "boost::process::launch: CloseHandle failed"));
 
- return child(pi.dwProcessId, fhstdin, fhstdout, fhstderr, detail::file_handle(pi.hProcess));
+ //I dare you to understand this code at first look.
+ if ( ::CreateProcessA(exe.get(), cmdline.get(),
+ NULL, NULL, TRUE, 0, envstrs.get(), workdir.get(),
+ &startup_info, &pi) == 0)
 
-#endif
+ boost::throw_exception(boost::system::system_error(
+ boost::system::error_code(
+ ::GetLastError(), boost::system::get_system_category()),
+ "boost::process::detail::win32_start: CreateProcess failed"));
+
+
+ //is this necessary?
+
+ if (! ::CloseHandle(pi.hThread))
+ boost::throw_exception(
+ boost::system::system_error(boost::system::error_code(
+ ::GetLastError(), boost::system::get_system_category()),
+ "boost::process::launch: CloseHandle failed"));
+
+ return child(pi.dwProcessId, fhstdin, fhstdout, fhstderr, detail::file_handle(pi.hProcess));
+
+ #endif
 }
 
 


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk