various updates
This commit is contained in:
parent
161ce54cb8
commit
73f94bcebe
34 changed files with 3449 additions and 4 deletions
|
|
@ -93,7 +93,7 @@ set( sources
|
||||||
# src/program_options.cpp
|
# src/program_options.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
#add_subdirectory(vendor/libssh2-1.4.2)
|
add_subdirectory(vendor) #/libssh2-1.4.2)
|
||||||
|
|
||||||
setup_library( fc SOURCES ${sources} )
|
setup_library( fc SOURCES ${sources} )
|
||||||
|
|
||||||
|
|
|
||||||
25
include/boost/process.hpp
Normal file
25
include/boost/process.hpp
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
//
|
||||||
|
// Boost.Process
|
||||||
|
// ~~~~~~~~~~~~~
|
||||||
|
//
|
||||||
|
// Copyright (c) 2006, 2007 Julio M. Merino Vidal
|
||||||
|
// Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
|
||||||
|
// Copyright (c) 2009 Boris Schaeling
|
||||||
|
// Copyright (c) 2010 Felipe Tanus, Boris Schaeling
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \file boost/process.hpp
|
||||||
|
*
|
||||||
|
* Convenience header that includes all public Boost.Process header files.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BOOST_PROCESS_HPP
|
||||||
|
#define BOOST_PROCESS_HPP
|
||||||
|
|
||||||
|
#include <boost/process/all.hpp>
|
||||||
|
|
||||||
|
#endif
|
||||||
40
include/boost/process/all.hpp
Normal file
40
include/boost/process/all.hpp
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
//
|
||||||
|
// Boost.Process
|
||||||
|
// ~~~~~~~~~~~~~
|
||||||
|
//
|
||||||
|
// Copyright (c) 2006, 2007 Julio M. Merino Vidal
|
||||||
|
// Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
|
||||||
|
// Copyright (c) 2009 Boris Schaeling
|
||||||
|
// Copyright (c) 2010 Felipe Tanus, Boris Schaeling
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \file boost/process/all.hpp
|
||||||
|
*
|
||||||
|
* Convenience header that includes all public Boost.Process header files.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BOOST_PROCESS_ALL_HPP
|
||||||
|
#define BOOST_PROCESS_ALL_HPP
|
||||||
|
|
||||||
|
#include <boost/process/child.hpp>
|
||||||
|
#include <boost/process/context.hpp>
|
||||||
|
#include <boost/process/environment.hpp>
|
||||||
|
#include <boost/process/handle.hpp>
|
||||||
|
#include <boost/process/operations.hpp>
|
||||||
|
#include <boost/process/pid_type.hpp>
|
||||||
|
#include <boost/process/pipe.hpp>
|
||||||
|
#include <boost/process/pistream.hpp>
|
||||||
|
#include <boost/process/postream.hpp>
|
||||||
|
#include <boost/process/process.hpp>
|
||||||
|
#include <boost/process/self.hpp>
|
||||||
|
#include <boost/process/status.hpp>
|
||||||
|
#include <boost/process/stream_behavior.hpp>
|
||||||
|
#include <boost/process/stream_ends.hpp>
|
||||||
|
#include <boost/process/stream_id.hpp>
|
||||||
|
#include <boost/process/stream_type.hpp>
|
||||||
|
|
||||||
|
#endif
|
||||||
92
include/boost/process/child.hpp
Normal file
92
include/boost/process/child.hpp
Normal file
|
|
@ -0,0 +1,92 @@
|
||||||
|
//
|
||||||
|
// Boost.Process
|
||||||
|
// ~~~~~~~~~~~~~
|
||||||
|
//
|
||||||
|
// Copyright (c) 2006, 2007 Julio M. Merino Vidal
|
||||||
|
// Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
|
||||||
|
// Copyright (c) 2009 Boris Schaeling
|
||||||
|
// Copyright (c) 2010 Felipe Tanus, Boris Schaeling
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \file boost/process/child.hpp
|
||||||
|
*
|
||||||
|
* Includes the declaration of the child class.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BOOST_PROCESS_CHILD_HPP
|
||||||
|
#define BOOST_PROCESS_CHILD_HPP
|
||||||
|
|
||||||
|
#include <boost/process/config.hpp>
|
||||||
|
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
# include <windows.h>
|
||||||
|
#else
|
||||||
|
# error "Unsupported platform."
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <boost/process/process.hpp>
|
||||||
|
#include <boost/process/pid_type.hpp>
|
||||||
|
#include <boost/process/stream_id.hpp>
|
||||||
|
#include <boost/process/handle.hpp>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
namespace process {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The child class provides access to a child process.
|
||||||
|
*/
|
||||||
|
class child : public process
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* Creates a new child object that represents the just spawned child
|
||||||
|
* process \a id.
|
||||||
|
*/
|
||||||
|
child(pid_type id, std::map<stream_id, handle> handles)
|
||||||
|
: process(id),
|
||||||
|
handles_(handles)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(BOOST_WINDOWS_API)
|
||||||
|
/**
|
||||||
|
* Creates a new child object that represents the just spawned child
|
||||||
|
* process \a id.
|
||||||
|
*
|
||||||
|
* This operation is only available on Windows systems.
|
||||||
|
*/
|
||||||
|
child(handle hprocess, std::map<stream_id, handle> handles)
|
||||||
|
: process(hprocess),
|
||||||
|
handles_(handles)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a handle to a stream attached to the child.
|
||||||
|
*
|
||||||
|
* If the handle doesn't exist an invalid handle is returned.
|
||||||
|
*/
|
||||||
|
handle get_handle(stream_id id) const
|
||||||
|
{
|
||||||
|
std::map<stream_id, handle>::const_iterator it = handles_.find(id);
|
||||||
|
return (it != handles_.end()) ? it->second : handle();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
/**
|
||||||
|
* Handles providing access to streams attached to the child process.
|
||||||
|
*/
|
||||||
|
std::map<stream_id, handle> handles_;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
74
include/boost/process/config.hpp
Normal file
74
include/boost/process/config.hpp
Normal file
|
|
@ -0,0 +1,74 @@
|
||||||
|
//
|
||||||
|
// Boost.Process
|
||||||
|
// ~~~~~~~~~~~~~
|
||||||
|
//
|
||||||
|
// Copyright (c) 2006, 2007 Julio M. Merino Vidal
|
||||||
|
// Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
|
||||||
|
// Copyright (c) 2009 Boris Schaeling
|
||||||
|
// Copyright (c) 2010 Felipe Tanus, Boris Schaeling
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \file boost/process/config.hpp
|
||||||
|
*
|
||||||
|
* Defines macros that are used by the library to determine the operating
|
||||||
|
* system it is running under and the features it supports.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BOOST_PROCESS_CONFIG_HPP
|
||||||
|
#define BOOST_PROCESS_CONFIG_HPP
|
||||||
|
|
||||||
|
#include <boost/config.hpp>
|
||||||
|
#include <boost/system/config.hpp>
|
||||||
|
#include <boost/system/system_error.hpp>
|
||||||
|
#include <boost/throw_exception.hpp>
|
||||||
|
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
# include <errno.h>
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
# include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(BOOST_POSIX_API) || defined(BOOST_PROCESS_DOXYGEN)
|
||||||
|
# if !defined(BOOST_PROCESS_POSIX_PATH_MAX) || defined(BOOST_PROCESS_DOXYGEN)
|
||||||
|
/**
|
||||||
|
* Specifies the system's maximal supported path length.
|
||||||
|
*
|
||||||
|
* The macro BOOST_PROCESS_POSIX_PATH_MAX is set to a positive integer
|
||||||
|
* value which specifies the system's maximal supported path length. It is
|
||||||
|
* only used if neither PATH_MAX nor _PC_PATH_MAX and HAVE_PATHCONF are defined.
|
||||||
|
* The maximal supported path length is required by
|
||||||
|
* boost::process::self::get_work_dir(). Please note that this function is
|
||||||
|
* also called by the constructor of boost::process::context.
|
||||||
|
*/
|
||||||
|
# define BOOST_PROCESS_POSIX_PATH_MAX 259
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/** \cond */
|
||||||
|
#define BOOST_PROCESS_SOURCE_LOCATION \
|
||||||
|
"in file '" __FILE__ "', line " BOOST_STRINGIZE(__LINE__) ": "
|
||||||
|
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
# define BOOST_PROCESS_LAST_ERROR errno
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
# define BOOST_PROCESS_LAST_ERROR GetLastError()
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR(what) \
|
||||||
|
boost::throw_exception(boost::system::system_error( \
|
||||||
|
boost::system::error_code(BOOST_PROCESS_LAST_ERROR, \
|
||||||
|
boost::system::get_system_category()), \
|
||||||
|
BOOST_PROCESS_SOURCE_LOCATION what))
|
||||||
|
|
||||||
|
#define BOOST_PROCESS_THROW_ERROR(error, what) \
|
||||||
|
boost::throw_exception(boost::system::system_error( \
|
||||||
|
boost::system::error_code(error, \
|
||||||
|
boost::system::get_system_category()), \
|
||||||
|
BOOST_PROCESS_SOURCE_LOCATION what))
|
||||||
|
/** \endcond */
|
||||||
|
|
||||||
|
#endif
|
||||||
133
include/boost/process/context.hpp
Normal file
133
include/boost/process/context.hpp
Normal file
|
|
@ -0,0 +1,133 @@
|
||||||
|
//
|
||||||
|
// Boost.Process
|
||||||
|
// ~~~~~~~~~~~~~
|
||||||
|
//
|
||||||
|
// Copyright (c) 2006, 2007 Julio M. Merino Vidal
|
||||||
|
// Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
|
||||||
|
// Copyright (c) 2009 Boris Schaeling
|
||||||
|
// Copyright (c) 2010 Felipe Tanus, Boris Schaeling
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \file boost/process/context.hpp
|
||||||
|
*
|
||||||
|
* Includes the declaration of the context class.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BOOST_PROCESS_CONTEXT_HPP
|
||||||
|
#define BOOST_PROCESS_CONTEXT_HPP
|
||||||
|
|
||||||
|
#include <boost/process/config.hpp>
|
||||||
|
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
# include <unistd.h>
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
# include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <boost/process/stream_id.hpp>
|
||||||
|
#include <boost/process/stream_ends.hpp>
|
||||||
|
#include <boost/process/stream_type.hpp>
|
||||||
|
#include <boost/process/environment.hpp>
|
||||||
|
#include <boost/process/self.hpp>
|
||||||
|
#include <boost/process/stream_behavior.hpp>
|
||||||
|
#include <boost/function.hpp>
|
||||||
|
#include <string>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
namespace process {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Context class to define how a child process is created.
|
||||||
|
*
|
||||||
|
* The context class is used to configure streams, to set the work directory
|
||||||
|
* and define environment variables. It is also used to change a process
|
||||||
|
* name (the variable commonly known as argv[0]).
|
||||||
|
*/
|
||||||
|
struct context
|
||||||
|
{
|
||||||
|
typedef std::map<stream_id, boost::function<stream_ends (stream_type)> >
|
||||||
|
streams_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Streams.
|
||||||
|
*
|
||||||
|
* Streams of a child process can be configured through factory functions
|
||||||
|
* which return a pair of handles - one handle to use as a stream end
|
||||||
|
* in the child process and possibly another handle to use as a stream end
|
||||||
|
* in the parent process (if a pipe is setup both processes can communicate
|
||||||
|
* with each other).
|
||||||
|
*/
|
||||||
|
streams_t streams;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Process name.
|
||||||
|
*
|
||||||
|
* The child process can access the process name via a variable
|
||||||
|
* commonly known as argv[0].
|
||||||
|
*/
|
||||||
|
std::string process_name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Work directory.
|
||||||
|
*/
|
||||||
|
std::string work_dir;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Environment variables.
|
||||||
|
*/
|
||||||
|
environment env;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a process context.
|
||||||
|
*
|
||||||
|
* The default behavior of standard streams is to inherit them. The current
|
||||||
|
* work directory is also the work directory of the child process. The child
|
||||||
|
* process also inherits all environment variables.
|
||||||
|
*/
|
||||||
|
context()
|
||||||
|
: work_dir(self::get_work_dir()),
|
||||||
|
env(self::get_environment())
|
||||||
|
{
|
||||||
|
#if 0 // this default behavior will throw in non-console apps
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
streams[stdin_id] = behavior::inherit(STDIN_FILENO);
|
||||||
|
streams[stdout_id] = behavior::inherit(STDOUT_FILENO);
|
||||||
|
streams[stderr_id] = behavior::inherit(STDERR_FILENO);
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
streams[stdin_id] = behavior::inherit(GetStdHandle(STD_INPUT_HANDLE));
|
||||||
|
streams[stdout_id] = behavior::inherit(GetStdHandle(STD_OUTPUT_HANDLE));
|
||||||
|
streams[stderr_id] = behavior::inherit(GetStdHandle(STD_ERROR_HANDLE));
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(BOOST_POSIX_API) || defined(BOOST_PROCESS_DOXYGEN)
|
||||||
|
/**
|
||||||
|
* Setups a child process.
|
||||||
|
*
|
||||||
|
* This is an extension point to support more configuration options for
|
||||||
|
* child processes. You can initialize \a setup with a user-defined function
|
||||||
|
* which is called when a child process is created.
|
||||||
|
*
|
||||||
|
* On POSIX platforms setup() is called in the child process. That's why in
|
||||||
|
* a multithreaded application only async-signal-safe functions must be
|
||||||
|
* called in the function \a setup is bound to.
|
||||||
|
*
|
||||||
|
* On Windows platforms setup() is called in the parent process. A
|
||||||
|
* reference to a STARTUPINFOA structure is passed as parameter.
|
||||||
|
*/
|
||||||
|
boost::function<void ()> setup;
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
boost::function<void (STARTUPINFOA&)> setup;
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
69
include/boost/process/detail/basic_status.hpp
Normal file
69
include/boost/process/detail/basic_status.hpp
Normal file
|
|
@ -0,0 +1,69 @@
|
||||||
|
//
|
||||||
|
// Boost.Process
|
||||||
|
// ~~~~~~~~~~~~~
|
||||||
|
//
|
||||||
|
// Copyright (c) 2006, 2007 Julio M. Merino Vidal
|
||||||
|
// Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
|
||||||
|
// Copyright (c) 2009 Boris Schaeling
|
||||||
|
// Copyright (c) 2010 Felipe Tanus, Boris Schaeling
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \file boost/process/detail/basic_status.hpp
|
||||||
|
*
|
||||||
|
* Includes the declaration of the basic status class.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BOOST_PROCESS_DETAIL_BASIC_STATUS_HPP
|
||||||
|
#define BOOST_PROCESS_DETAIL_BASIC_STATUS_HPP
|
||||||
|
|
||||||
|
#include <boost/process/config.hpp>
|
||||||
|
#include <boost/process/pid_type.hpp>
|
||||||
|
#include <boost/asio.hpp>
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
namespace process {
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The basic_status class to wait for processes to exit.
|
||||||
|
*
|
||||||
|
* The basic_status class is a Boost.Asio I/O object and supports synchronous
|
||||||
|
* and asynchronous wait operations. It must be instantiated with a Service.
|
||||||
|
*/
|
||||||
|
template <typename Service>
|
||||||
|
class basic_status
|
||||||
|
: public boost::asio::basic_io_object<Service>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit basic_status(boost::asio::io_service &io_service)
|
||||||
|
: boost::asio::basic_io_object<Service>(io_service)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Waits synchronously for a process to exit.
|
||||||
|
*/
|
||||||
|
int wait(pid_type pid)
|
||||||
|
{
|
||||||
|
return this->service.wait(this->implementation, pid);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Waits asynchronously for a process to exit.
|
||||||
|
*/
|
||||||
|
template <typename Handler>
|
||||||
|
void async_wait(pid_type pid, Handler handler)
|
||||||
|
{
|
||||||
|
this->service.async_wait(this->implementation, pid, handler);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
318
include/boost/process/detail/basic_status_service.hpp
Normal file
318
include/boost/process/detail/basic_status_service.hpp
Normal file
|
|
@ -0,0 +1,318 @@
|
||||||
|
//
|
||||||
|
// Boost.Process
|
||||||
|
// ~~~~~~~~~~~~~
|
||||||
|
//
|
||||||
|
// Copyright (c) 2006, 2007 Julio M. Merino Vidal
|
||||||
|
// Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
|
||||||
|
// Copyright (c) 2009 Boris Schaeling
|
||||||
|
// Copyright (c) 2010 Felipe Tanus, Boris Schaeling
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \file boost/process/detail/basic_status_service.hpp
|
||||||
|
*
|
||||||
|
* Includes the declaration of the basic status service class.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BOOST_PROCESS_DETAIL_BASIC_STATUS_SERVICE_HPP
|
||||||
|
#define BOOST_PROCESS_DETAIL_BASIC_STATUS_SERVICE_HPP
|
||||||
|
|
||||||
|
#include <boost/process/config.hpp>
|
||||||
|
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
# include <boost/process/operations.hpp>
|
||||||
|
# include <string>
|
||||||
|
# include <sys/types.h>
|
||||||
|
# include <sys/wait.h>
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
# include <windows.h>
|
||||||
|
#else
|
||||||
|
# error "Unsupported platform."
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <boost/process/pid_type.hpp>
|
||||||
|
#include <boost/process/detail/status_impl.hpp>
|
||||||
|
#include <boost/asio.hpp>
|
||||||
|
#include <boost/thread.hpp>
|
||||||
|
#include <boost/shared_ptr.hpp>
|
||||||
|
#include <boost/make_shared.hpp>
|
||||||
|
#include <boost/scoped_ptr.hpp>
|
||||||
|
#include <boost/system/error_code.hpp>
|
||||||
|
#include <boost/unordered_map.hpp>
|
||||||
|
#include <vector>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
namespace process {
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The basic_status_service class provides the service to wait for processes
|
||||||
|
* synchronously and asynchronously.
|
||||||
|
*/
|
||||||
|
template <typename StatusImplementation = status_impl>
|
||||||
|
class basic_status_service
|
||||||
|
: public boost::asio::detail::service_base<StatusImplementation>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit basic_status_service(boost::asio::io_service &io_service)
|
||||||
|
: boost::asio::detail::service_base<StatusImplementation>(io_service),
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
interrupt_pid_(-1),
|
||||||
|
pids_(0)
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
run_(true)
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
#if defined(BOOST_WINDOWS_API)
|
||||||
|
handles_.push_back(CreateEvent(NULL, FALSE, FALSE, NULL));
|
||||||
|
if (handles_[0] == NULL)
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("CreateEvent() failed");
|
||||||
|
work_thread_ = boost::thread(
|
||||||
|
&basic_status_service<StatusImplementation>::work_thread, this);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
~basic_status_service()
|
||||||
|
{
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
boost::unique_lock<boost::mutex> lock(work_thread_mutex_);
|
||||||
|
bool worker_thread_active = (pids_ != 0);
|
||||||
|
lock.unlock();
|
||||||
|
if (worker_thread_active)
|
||||||
|
{
|
||||||
|
stop_work_thread();
|
||||||
|
work_thread_.join();
|
||||||
|
}
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
stop_work_thread();
|
||||||
|
work_thread_.join();
|
||||||
|
CloseHandle(handles_[0]);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef boost::shared_ptr<StatusImplementation> implementation_type;
|
||||||
|
|
||||||
|
void construct(implementation_type &impl)
|
||||||
|
{
|
||||||
|
impl = boost::make_shared<StatusImplementation>();
|
||||||
|
boost::unique_lock<boost::mutex> lock(work_thread_mutex_);
|
||||||
|
impls_.push_back(impl);
|
||||||
|
}
|
||||||
|
|
||||||
|
void destroy(implementation_type &impl)
|
||||||
|
{
|
||||||
|
boost::unique_lock<boost::mutex> lock(work_thread_mutex_);
|
||||||
|
typename std::vector<implementation_type>::iterator it =
|
||||||
|
std::find(impls_.begin(), impls_.end(), impl);
|
||||||
|
if (it != impls_.end())
|
||||||
|
impls_.erase(it);
|
||||||
|
#if defined(BOOST_WINDOWS_API)
|
||||||
|
interrupt_work_thread();
|
||||||
|
work_thread_cond_.wait(work_thread_mutex_);
|
||||||
|
impl->clear(handles_);
|
||||||
|
work_thread_cond_.notify_all();
|
||||||
|
#endif
|
||||||
|
impl.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
int wait(implementation_type &impl, pid_type pid)
|
||||||
|
{
|
||||||
|
boost::system::error_code ec;
|
||||||
|
int status = impl->wait(pid, ec);
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
if (ec.value() == ECHILD)
|
||||||
|
{
|
||||||
|
boost::unique_lock<boost::mutex> lock(work_thread_mutex_);
|
||||||
|
boost::unordered_map<pid_t, int>::iterator it = statuses_.find(pid);
|
||||||
|
if (it == statuses_.end())
|
||||||
|
{
|
||||||
|
work_thread_cond_.wait(work_thread_mutex_);
|
||||||
|
it = statuses_.find(pid);
|
||||||
|
}
|
||||||
|
if (it != statuses_.end())
|
||||||
|
{
|
||||||
|
status = it->second;
|
||||||
|
statuses_.erase(it);
|
||||||
|
ec.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
boost::asio::detail::throw_error(ec);
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Handler>
|
||||||
|
void async_wait(implementation_type &impl, pid_type pid, Handler handler)
|
||||||
|
{
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
boost::unique_lock<boost::mutex> lock(work_thread_mutex_);
|
||||||
|
if (++pids_ == 1)
|
||||||
|
{
|
||||||
|
work_.reset(new boost::asio::io_service::work(
|
||||||
|
this->get_io_service()));
|
||||||
|
work_thread_ = boost::thread(
|
||||||
|
&basic_status_service<StatusImplementation>::work_thread,
|
||||||
|
this);
|
||||||
|
}
|
||||||
|
impl->async_wait(pid, this->get_io_service().wrap(handler));
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
HANDLE handle = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION,
|
||||||
|
FALSE, pid);
|
||||||
|
if (handle == NULL)
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("OpenProcess() failed");
|
||||||
|
boost::unique_lock<boost::mutex> lock(work_thread_mutex_);
|
||||||
|
if (!work_)
|
||||||
|
work_.reset(new boost::asio::io_service::work(
|
||||||
|
this->get_io_service()));
|
||||||
|
interrupt_work_thread();
|
||||||
|
work_thread_cond_.wait(work_thread_mutex_);
|
||||||
|
handles_.push_back(handle);
|
||||||
|
impl->async_wait(handle, this->get_io_service().wrap(handler));
|
||||||
|
work_thread_cond_.notify_all();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
void shutdown_service()
|
||||||
|
{
|
||||||
|
#if defined(BOOST_WINDOWS_API)
|
||||||
|
boost::unique_lock<boost::mutex> lock(work_thread_mutex_);
|
||||||
|
work_.reset();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void work_thread()
|
||||||
|
{
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
int status;
|
||||||
|
pid_t pid = ::wait(&status);
|
||||||
|
if (pid == -1)
|
||||||
|
{
|
||||||
|
if (errno != EINTR)
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("wait(2) failed");
|
||||||
|
}
|
||||||
|
else if (interrupted(pid))
|
||||||
|
{
|
||||||
|
// On POSIX the only reason to interrupt is to break out.
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
boost::unique_lock<boost::mutex> lock(work_thread_mutex_);
|
||||||
|
bool regchild = false;
|
||||||
|
for (typename std::vector<implementation_type>::iterator it =
|
||||||
|
impls_.begin(); it != impls_.end(); ++it)
|
||||||
|
regchild |= (*it)->complete(pid, status);
|
||||||
|
if (regchild && --pids_ == 0)
|
||||||
|
{
|
||||||
|
work_.reset();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else if (!regchild)
|
||||||
|
{
|
||||||
|
statuses_.insert(boost::unordered_map<pid_t, int>::
|
||||||
|
value_type(pid, status));
|
||||||
|
work_thread_cond_.notify_all();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
DWORD res = WaitForMultipleObjects(handles_.size(), &handles_[0],
|
||||||
|
FALSE, INFINITE);
|
||||||
|
if (res == WAIT_FAILED)
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR(
|
||||||
|
"WaitForMultipleObjects() failed");
|
||||||
|
else if (res - WAIT_OBJECT_0 == 0)
|
||||||
|
{
|
||||||
|
boost::unique_lock<boost::mutex> lock(work_thread_mutex_);
|
||||||
|
if (!run_)
|
||||||
|
break;
|
||||||
|
work_thread_cond_.notify_all();
|
||||||
|
work_thread_cond_.wait(work_thread_mutex_);
|
||||||
|
}
|
||||||
|
else if (res - WAIT_OBJECT_0 > 0)
|
||||||
|
{
|
||||||
|
HANDLE handle = handles_[res - WAIT_OBJECT_0];
|
||||||
|
DWORD exit_code;
|
||||||
|
if (!GetExitCodeProcess(handle, &exit_code))
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR(
|
||||||
|
"GetExitCodeProcess() failed");
|
||||||
|
boost::unique_lock<boost::mutex> lock(work_thread_mutex_);
|
||||||
|
for (typename std::vector<implementation_type>::iterator it =
|
||||||
|
impls_.begin(); it != impls_.end(); ++it)
|
||||||
|
(*it)->complete(handle, exit_code);
|
||||||
|
std::vector<HANDLE>::iterator it = handles_.begin();
|
||||||
|
std::advance(it, res - WAIT_OBJECT_0);
|
||||||
|
handles_.erase(it);
|
||||||
|
if (handles_.size() == 1)
|
||||||
|
work_.reset();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void interrupt_work_thread()
|
||||||
|
{
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
// By creating a child process which immediately exits
|
||||||
|
// we interrupt wait().
|
||||||
|
std::vector<std::string> args;
|
||||||
|
args.push_back("-c");
|
||||||
|
args.push_back("'exit'");
|
||||||
|
interrupt_pid_ = create_child("/bin/sh", args).get_id();
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
// By signaling the event in the first slot WaitForMultipleObjects()
|
||||||
|
// will return. The work thread won't do anything except checking if
|
||||||
|
// it should continue to run.
|
||||||
|
if (!SetEvent(handles_[0]))
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("SetEvent() failed");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
bool interrupted(pid_t pid)
|
||||||
|
{
|
||||||
|
boost::mutex::scoped_lock lock(work_thread_mutex_);
|
||||||
|
return interrupt_pid_ == pid;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void stop_work_thread()
|
||||||
|
{
|
||||||
|
boost::mutex::scoped_lock lock(work_thread_mutex_);
|
||||||
|
#if defined(BOOST_WINDOWS_API)
|
||||||
|
// Access to run_ must be sychronized with running().
|
||||||
|
run_ = false;
|
||||||
|
#endif
|
||||||
|
// Access to interrupt_pid_ must be sychronized with interrupted().
|
||||||
|
interrupt_work_thread();
|
||||||
|
}
|
||||||
|
|
||||||
|
boost::scoped_ptr<boost::asio::io_service::work> work_;
|
||||||
|
std::vector<implementation_type> impls_;
|
||||||
|
boost::mutex work_thread_mutex_;
|
||||||
|
boost::thread work_thread_;
|
||||||
|
boost::condition_variable_any work_thread_cond_;
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
pid_t interrupt_pid_;
|
||||||
|
int pids_;
|
||||||
|
boost::unordered_map<pid_t, int> statuses_;
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
bool run_;
|
||||||
|
std::vector<HANDLE> handles_;
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
106
include/boost/process/detail/posix_helpers.hpp
Normal file
106
include/boost/process/detail/posix_helpers.hpp
Normal file
|
|
@ -0,0 +1,106 @@
|
||||||
|
//
|
||||||
|
// Boost.Process
|
||||||
|
// ~~~~~~~~~~~~~
|
||||||
|
//
|
||||||
|
// Copyright (c) 2006, 2007 Julio M. Merino Vidal
|
||||||
|
// Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
|
||||||
|
// Copyright (c) 2009 Boris Schaeling
|
||||||
|
// Copyright (c) 2010 Felipe Tanus, Boris Schaeling
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \file boost/process/detail/posix_helpers.hpp
|
||||||
|
*
|
||||||
|
* Includes the declaration of helper functions for POSIX systems.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BOOST_PROCESS_POSIX_HELPERS_HPP
|
||||||
|
#define BOOST_PROCESS_POSIX_HELPERS_HPP
|
||||||
|
|
||||||
|
#include <boost/process/config.hpp>
|
||||||
|
#include <boost/process/environment.hpp>
|
||||||
|
#include <string>
|
||||||
|
#include <utility>
|
||||||
|
#include <cstring>
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
namespace process {
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts an environment to a char** table as used by execve().
|
||||||
|
*
|
||||||
|
* Converts the environment's contents to the format used by the
|
||||||
|
* execve() system call. The returned char** array is allocated
|
||||||
|
* in dynamic memory; the caller must free it when not used any
|
||||||
|
* more. Each entry is also allocated in dynamic memory and is a
|
||||||
|
* NULL-terminated string of the form var=value; these must also be
|
||||||
|
* released by the caller.
|
||||||
|
*
|
||||||
|
* This operation is only available on POSIX systems.
|
||||||
|
*
|
||||||
|
* \return The first argument of the pair is an integer that indicates
|
||||||
|
* how many strings are stored in the second argument. The
|
||||||
|
* second argument is a NULL-terminated, dynamically allocated
|
||||||
|
* array of dynamically allocated strings representing the
|
||||||
|
* enviroment's content. Each array entry is a NULL-terminated
|
||||||
|
* string of the form var=value. The caller is responsible for
|
||||||
|
* freeing them.
|
||||||
|
*/
|
||||||
|
inline std::pair<std::size_t, char**> environment_to_envp(const environment
|
||||||
|
&env)
|
||||||
|
{
|
||||||
|
std::size_t nargs = env.size();
|
||||||
|
char **envp = new char*[nargs + 1];
|
||||||
|
environment::size_type i = 0;
|
||||||
|
for (environment::const_iterator it = env.begin(); it != env.end(); ++it)
|
||||||
|
{
|
||||||
|
std::string s = it->first + "=" + it->second;
|
||||||
|
envp[i] = new char[s.size() + 1];
|
||||||
|
std::strncpy(envp[i], s.c_str(), s.size() + 1);
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
envp[i] = 0;
|
||||||
|
return std::pair<std::size_t, char**>(nargs, envp);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts the command line to an array of C strings.
|
||||||
|
*
|
||||||
|
* Converts the command line's list of arguments to the format expected
|
||||||
|
* by the \a argv parameter in the POSIX execve() system call.
|
||||||
|
*
|
||||||
|
* This operation is only available on POSIX systems.
|
||||||
|
*
|
||||||
|
* \return The first argument of the pair is an integer that indicates
|
||||||
|
* how many strings are stored in the second argument. The
|
||||||
|
* second argument is a NULL-terminated, dynamically allocated
|
||||||
|
* array of dynamically allocated strings holding the arguments
|
||||||
|
* to the executable. The caller is responsible for freeing them.
|
||||||
|
*/
|
||||||
|
template <class Arguments>
|
||||||
|
inline std::pair<std::size_t, char**> collection_to_argv(const Arguments &args)
|
||||||
|
{
|
||||||
|
std::size_t nargs = args.size();
|
||||||
|
char **argv = new char*[nargs + 1];
|
||||||
|
typename Arguments::size_type i = 0;
|
||||||
|
for (typename Arguments::const_iterator it = args.begin(); it != args.end();
|
||||||
|
++it)
|
||||||
|
{
|
||||||
|
argv[i] = new char[it->size() + 1];
|
||||||
|
std::strncpy(argv[i], it->c_str(), it->size() + 1);
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
argv[nargs] = 0;
|
||||||
|
return std::pair<std::size_t, char**>(nargs, argv);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
190
include/boost/process/detail/status_impl.hpp
Normal file
190
include/boost/process/detail/status_impl.hpp
Normal file
|
|
@ -0,0 +1,190 @@
|
||||||
|
//
|
||||||
|
// Boost.Process
|
||||||
|
// ~~~~~~~~~~~~~
|
||||||
|
//
|
||||||
|
// Copyright (c) 2006, 2007 Julio M. Merino Vidal
|
||||||
|
// Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
|
||||||
|
// Copyright (c) 2009 Boris Schaeling
|
||||||
|
// Copyright (c) 2010 Felipe Tanus, Boris Schaeling
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \file boost/process/detail/status_impl.hpp
|
||||||
|
*
|
||||||
|
* Includes the declaration of the status implementation class.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BOOST_PROCESS_DETAIL_STATUS_IMPL_HPP
|
||||||
|
#define BOOST_PROCESS_DETAIL_STATUS_IMPL_HPP
|
||||||
|
|
||||||
|
#include <boost/process/config.hpp>
|
||||||
|
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
# include <sys/types.h>
|
||||||
|
# include <signal.h>
|
||||||
|
# include <sys/wait.h>
|
||||||
|
# include <errno.h>
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
# include <windows.h>
|
||||||
|
#else
|
||||||
|
# error "Unsupported platform."
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <boost/process/pid_type.hpp>
|
||||||
|
#include <boost/system/error_code.hpp>
|
||||||
|
#include <boost/ptr_container/ptr_unordered_map.hpp>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
namespace process {
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
typedef pid_t phandle;
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
typedef HANDLE phandle;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct operation
|
||||||
|
{
|
||||||
|
virtual ~operation(){}
|
||||||
|
virtual void operator()(int exit_code)
|
||||||
|
{
|
||||||
|
#if defined(BOOST_MSVC)
|
||||||
|
exit_code;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename Handler>
|
||||||
|
class wrapped_handler : public operation
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
wrapped_handler(Handler handler)
|
||||||
|
: handler_(handler)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void operator()(int exit_code)
|
||||||
|
{
|
||||||
|
handler_(boost::system::error_code(), exit_code);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
Handler handler_;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The status_impl class saves internal data of every status I/O object.
|
||||||
|
*/
|
||||||
|
class status_impl
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
#if defined(BOOST_WINDOWS_API)
|
||||||
|
template <typename Container>
|
||||||
|
void clear(Container &handles)
|
||||||
|
{
|
||||||
|
for (operations_type::iterator it = ops_.begin(); it != ops_.end();
|
||||||
|
++it)
|
||||||
|
{
|
||||||
|
for (typename Container::iterator it2 = handles.begin(); it2 !=
|
||||||
|
handles.end(); ++it2)
|
||||||
|
{
|
||||||
|
if (*it2 == it->first)
|
||||||
|
{
|
||||||
|
handles.erase(it2);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
CloseHandle(it->first);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int wait(pid_type pid, boost::system::error_code &ec)
|
||||||
|
{
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
pid_t p;
|
||||||
|
int status;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
p = waitpid(pid, &status, 0);
|
||||||
|
} while (p == -1 && errno == EINTR);
|
||||||
|
if (p == -1)
|
||||||
|
{
|
||||||
|
ec = boost::system::error_code(errno,
|
||||||
|
boost::system::get_system_category());
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION | SYNCHRONIZE, FALSE,
|
||||||
|
pid);
|
||||||
|
if (h == NULL)
|
||||||
|
{
|
||||||
|
ec = boost::system::error_code(GetLastError(),
|
||||||
|
boost::system::get_system_category());
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (WaitForSingleObject(h, INFINITE) == WAIT_FAILED)
|
||||||
|
{
|
||||||
|
CloseHandle(h);
|
||||||
|
ec = boost::system::error_code(GetLastError(),
|
||||||
|
boost::system::get_system_category());
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
DWORD exit_code;
|
||||||
|
if (!GetExitCodeProcess(h, &exit_code))
|
||||||
|
{
|
||||||
|
CloseHandle(h);
|
||||||
|
ec = boost::system::error_code(GetLastError(),
|
||||||
|
boost::system::get_system_category());
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (!CloseHandle(h))
|
||||||
|
{
|
||||||
|
ec = boost::system::error_code(GetLastError(),
|
||||||
|
boost::system::get_system_category());
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return exit_code;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Handler>
|
||||||
|
void async_wait(phandle ph, Handler handler)
|
||||||
|
{
|
||||||
|
ops_.insert(ph, new wrapped_handler<Handler>(handler));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool complete(phandle ph, int exit_code)
|
||||||
|
{
|
||||||
|
boost::iterator_range<operations_type::iterator> r =
|
||||||
|
ops_.equal_range(ph);
|
||||||
|
if (r.empty())
|
||||||
|
return false;
|
||||||
|
for (operations_type::iterator it = r.begin(); it != r.end(); ++it)
|
||||||
|
(*it->second)(exit_code);
|
||||||
|
ops_.erase(r.begin(), r.end());
|
||||||
|
#if defined(BOOST_WINDOWS_API)
|
||||||
|
if (!CloseHandle(ph))
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("CloseHandle() failed");
|
||||||
|
#endif
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
typedef boost::ptr_unordered_multimap<phandle, operation> operations_type;
|
||||||
|
operations_type ops_;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
228
include/boost/process/detail/systembuf.hpp
Normal file
228
include/boost/process/detail/systembuf.hpp
Normal file
|
|
@ -0,0 +1,228 @@
|
||||||
|
//
|
||||||
|
// Boost.Process
|
||||||
|
// ~~~~~~~~~~~~~
|
||||||
|
//
|
||||||
|
// Copyright (c) 2006, 2007 Julio M. Merino Vidal
|
||||||
|
// Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
|
||||||
|
// Copyright (c) 2009 Boris Schaeling
|
||||||
|
// Copyright (c) 2010 Felipe Tanus, Boris Schaeling
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \file boost/process/detail/systembuf.hpp
|
||||||
|
*
|
||||||
|
* Includes the declaration of the systembuf class.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BOOST_PROCESS_DETAIL_SYSTEMBUF_HPP
|
||||||
|
#define BOOST_PROCESS_DETAIL_SYSTEMBUF_HPP
|
||||||
|
|
||||||
|
#include <boost/process/config.hpp>
|
||||||
|
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
# include <sys/types.h>
|
||||||
|
# include <unistd.h>
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
# include <windows.h>
|
||||||
|
#else
|
||||||
|
# error "Unsupported platform."
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <boost/noncopyable.hpp>
|
||||||
|
#include <boost/scoped_array.hpp>
|
||||||
|
#include <boost/assert.hpp>
|
||||||
|
#include <streambuf>
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
namespace process {
|
||||||
|
|
||||||
|
class postream;
|
||||||
|
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* std::streambuf implementation for handles.
|
||||||
|
*
|
||||||
|
* systembuf provides a std::streambuf implementation for handles.
|
||||||
|
* Contrarywise to the handle class, this class does \b not take
|
||||||
|
* ownership of the native handle; this should be taken care of
|
||||||
|
* somewhere else.
|
||||||
|
*
|
||||||
|
* This class follows the expected semantics of a std::streambuf object.
|
||||||
|
* However, it is not copyable to avoid introducing inconsistences with
|
||||||
|
* the on-disk file and the in-memory buffers.
|
||||||
|
*/
|
||||||
|
class systembuf : public std::streambuf, public boost::noncopyable
|
||||||
|
{
|
||||||
|
friend class boost::process::postream;
|
||||||
|
|
||||||
|
public:
|
||||||
|
#if defined(BOOST_PROCESS_DOXYGEN)
|
||||||
|
/**
|
||||||
|
* Opaque name for the native handle type.
|
||||||
|
*/
|
||||||
|
typedef NativeHandleType handle_type;
|
||||||
|
#elif defined(BOOST_POSIX_API)
|
||||||
|
typedef int handle_type;
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
typedef HANDLE handle_type;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new systembuf for the given handle.
|
||||||
|
*
|
||||||
|
* This constructor creates a new systembuf object that reads or
|
||||||
|
* writes data from/to the \a h native handle. This handle
|
||||||
|
* is \b not owned by the created systembuf object; the code
|
||||||
|
* should take care of it externally.
|
||||||
|
*
|
||||||
|
* This class buffers input and output; the buffer size may be
|
||||||
|
* tuned through the \a bufsize parameter, which defaults to 8192
|
||||||
|
* bytes.
|
||||||
|
*
|
||||||
|
* \see pistream and postream
|
||||||
|
*/
|
||||||
|
explicit systembuf(handle_type h, std::size_t bufsize = 8192)
|
||||||
|
: handle_(h),
|
||||||
|
bufsize_(bufsize),
|
||||||
|
read_buf_(new char[bufsize]),
|
||||||
|
write_buf_(new char[bufsize])
|
||||||
|
{
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
BOOST_ASSERT(handle_ >= 0);
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
BOOST_ASSERT(handle_ != INVALID_HANDLE_VALUE);
|
||||||
|
#endif
|
||||||
|
BOOST_ASSERT(bufsize_ > 0);
|
||||||
|
setp(write_buf_.get(), write_buf_.get() + bufsize_);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
/**
|
||||||
|
* Reads new data from the native handle.
|
||||||
|
*
|
||||||
|
* This operation is called by input methods when there is no more
|
||||||
|
* data in the input buffer. The function fills the buffer with new
|
||||||
|
* data, if available.
|
||||||
|
*
|
||||||
|
* \pre All input positions are exhausted (gptr() >= egptr()).
|
||||||
|
* \post The input buffer has new data, if available.
|
||||||
|
* \returns traits_type::eof() if a read error occurrs or there are
|
||||||
|
* no more data to be read. Otherwise returns
|
||||||
|
* traits_type::to_int_type(*gptr()).
|
||||||
|
*/
|
||||||
|
virtual int_type underflow()
|
||||||
|
{
|
||||||
|
BOOST_ASSERT(gptr() >= egptr());
|
||||||
|
|
||||||
|
bool ok;
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
ssize_t cnt = read(handle_, read_buf_.get(), bufsize_);
|
||||||
|
ok = (cnt != -1 && cnt != 0);
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
DWORD cnt;
|
||||||
|
BOOL res = ReadFile(handle_, read_buf_.get(), bufsize_, &cnt, NULL);
|
||||||
|
ok = (res && cnt > 0);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (!ok)
|
||||||
|
return traits_type::eof();
|
||||||
|
else
|
||||||
|
{
|
||||||
|
setg(read_buf_.get(), read_buf_.get(), read_buf_.get() + cnt);
|
||||||
|
return traits_type::to_int_type(*gptr());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Makes room in the write buffer for additional data.
|
||||||
|
*
|
||||||
|
* This operation is called by output methods when there is no more
|
||||||
|
* space in the output buffer to hold a new element. The function
|
||||||
|
* first flushes the buffer's contents to disk and then clears it to
|
||||||
|
* leave room for more characters. The given \a c character is
|
||||||
|
* stored at the beginning of the new space.
|
||||||
|
*
|
||||||
|
* \pre All output positions are exhausted (pptr() >= epptr()).
|
||||||
|
* \post The output buffer has more space if no errors occurred
|
||||||
|
* during the write to disk.
|
||||||
|
* \post *(pptr() - 1) is \a c.
|
||||||
|
* \returns traits_type::eof() if a write error occurrs. Otherwise
|
||||||
|
* returns traits_type::not_eof(c).
|
||||||
|
*/
|
||||||
|
virtual int_type overflow(int c)
|
||||||
|
{
|
||||||
|
BOOST_ASSERT(pptr() >= epptr());
|
||||||
|
|
||||||
|
if (sync() == -1)
|
||||||
|
return traits_type::eof();
|
||||||
|
|
||||||
|
if (!traits_type::eq_int_type(c, traits_type::eof()))
|
||||||
|
{
|
||||||
|
traits_type::assign(*pptr(), static_cast<traits_type::char_type>(
|
||||||
|
c));
|
||||||
|
pbump(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return traits_type::not_eof(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Flushes the output buffer to disk.
|
||||||
|
*
|
||||||
|
* Synchronizes the systembuf buffers with the contents of the file
|
||||||
|
* associated to this object through the native handle. The output buffer
|
||||||
|
* is flushed to disk and cleared to leave new room for more data.
|
||||||
|
*
|
||||||
|
* \returns 0 on success, -1 if an error occurred.
|
||||||
|
*/
|
||||||
|
virtual int sync()
|
||||||
|
{
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
ssize_t cnt = pptr() - pbase();
|
||||||
|
bool ok = (write(handle_, pbase(), cnt) == cnt);
|
||||||
|
if (ok)
|
||||||
|
pbump(-cnt);
|
||||||
|
return ok ? 0 : -1;
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
long cnt = pptr() - pbase();
|
||||||
|
DWORD rcnt;
|
||||||
|
BOOL res = WriteFile(handle_, pbase(), cnt, &rcnt, NULL);
|
||||||
|
bool ok = (res && static_cast<long>(rcnt) == cnt);
|
||||||
|
if (ok)
|
||||||
|
pbump(-cnt);
|
||||||
|
return ok ? 0 : -1;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
/**
|
||||||
|
* Native handle used by the systembuf object.
|
||||||
|
*/
|
||||||
|
handle_type handle_;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Internal buffer size used during read and write operations.
|
||||||
|
*/
|
||||||
|
std::size_t bufsize_;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Internal buffer used during read operations.
|
||||||
|
*/
|
||||||
|
boost::scoped_array<char> read_buf_;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Internal buffer used during write operations.
|
||||||
|
*/
|
||||||
|
boost::scoped_array<char> write_buf_;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
138
include/boost/process/detail/windows_helpers.hpp
Normal file
138
include/boost/process/detail/windows_helpers.hpp
Normal file
|
|
@ -0,0 +1,138 @@
|
||||||
|
//
|
||||||
|
// Boost.Process
|
||||||
|
// ~~~~~~~~~~~~~
|
||||||
|
//
|
||||||
|
// Copyright (c) 2006, 2007 Julio M. Merino Vidal
|
||||||
|
// Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
|
||||||
|
// Copyright (c) 2009 Boris Schaeling
|
||||||
|
// Copyright (c) 2010 Felipe Tanus, Boris Schaeling
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \file boost/process/detail/windows_helpers.hpp
|
||||||
|
*
|
||||||
|
* Includes the declaration of helper functions for Windows systems.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BOOST_PROCESS_WINDOWS_HELPERS_HPP
|
||||||
|
#define BOOST_PROCESS_WINDOWS_HELPERS_HPP
|
||||||
|
|
||||||
|
#include <boost/process/config.hpp>
|
||||||
|
#include <boost/process/environment.hpp>
|
||||||
|
#include <boost/shared_array.hpp>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <string.h>
|
||||||
|
#include <windows.h>
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
namespace process {
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts an environment to a string used by CreateProcess().
|
||||||
|
*
|
||||||
|
* Converts the environment's contents to the format used by the
|
||||||
|
* CreateProcess() system call. The returned char* string is
|
||||||
|
* allocated in dynamic memory; the caller must free it when not
|
||||||
|
* used any more. This is enforced by the use of a shared pointer.
|
||||||
|
*
|
||||||
|
* This operation is only available on Windows systems.
|
||||||
|
*
|
||||||
|
* \return A dynamically allocated char* string that represents
|
||||||
|
* the environment's content. This string is of the form
|
||||||
|
* var1=value1\\0var2=value2\\0\\0.
|
||||||
|
*/
|
||||||
|
inline boost::shared_array<char> environment_to_windows_strings(environment
|
||||||
|
&env)
|
||||||
|
{
|
||||||
|
boost::shared_array<char> envp;
|
||||||
|
|
||||||
|
if (env.empty())
|
||||||
|
{
|
||||||
|
envp.reset(new char[2]);
|
||||||
|
ZeroMemory(envp.get(), 2);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::string s;
|
||||||
|
for (environment::const_iterator it = env.begin(); it != env.end();
|
||||||
|
++it)
|
||||||
|
{
|
||||||
|
s += it->first + "=" + it->second;
|
||||||
|
s.push_back(0);
|
||||||
|
}
|
||||||
|
envp.reset(new char[s.size() + 1]);
|
||||||
|
#if (BOOST_MSVC >= 1400)
|
||||||
|
memcpy_s(envp.get(), s.size() + 1, s.c_str(), s.size() + 1);
|
||||||
|
#else
|
||||||
|
memcpy(envp.get(), s.c_str(), s.size() + 1);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
return envp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts the command line to a plain string.
|
||||||
|
*
|
||||||
|
* Converts the command line's list of arguments to the format expected by the
|
||||||
|
* \a lpCommandLine parameter in the CreateProcess() system call.
|
||||||
|
*
|
||||||
|
* This operation is only available on Windows systems.
|
||||||
|
*
|
||||||
|
* \return A dynamically allocated string holding the command line
|
||||||
|
* to be passed to the executable. It is returned in a
|
||||||
|
* shared_array object to ensure its release at some point.
|
||||||
|
*/
|
||||||
|
template <class Arguments>
|
||||||
|
inline boost::shared_array<char> collection_to_windows_cmdline(const Arguments
|
||||||
|
&args)
|
||||||
|
{
|
||||||
|
typedef std::vector<std::string> arguments_t;
|
||||||
|
arguments_t args2;
|
||||||
|
typename Arguments::size_type i = 0;
|
||||||
|
std::size_t size = 0;
|
||||||
|
for (typename Arguments::const_iterator it = args.begin(); it != args.end();
|
||||||
|
++it)
|
||||||
|
{
|
||||||
|
std::string arg = *it;
|
||||||
|
|
||||||
|
std::string::size_type pos = 0;
|
||||||
|
while ( (pos = arg.find('"', pos)) != std::string::npos)
|
||||||
|
{
|
||||||
|
arg.replace(pos, 1, "\\\"");
|
||||||
|
pos += 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (arg.find(' ') != std::string::npos)
|
||||||
|
arg = '\"' + arg + '\"';
|
||||||
|
|
||||||
|
if (i++ != args.size() - 1)
|
||||||
|
arg += ' ';
|
||||||
|
|
||||||
|
args2.push_back(arg);
|
||||||
|
size += arg.size() + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
boost::shared_array<char> cmdline(new char[size]);
|
||||||
|
cmdline.get()[0] = '\0';
|
||||||
|
for (arguments_t::size_type i = 0; i < args.size(); ++i)
|
||||||
|
#if (BOOST_MSVC >= 1400)
|
||||||
|
strcat_s(cmdline.get(), size, args2[i].c_str());
|
||||||
|
#else
|
||||||
|
strncat(cmdline.get(), args2[i].c_str(), args2[i].size());
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return cmdline;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
52
include/boost/process/environment.hpp
Normal file
52
include/boost/process/environment.hpp
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
//
|
||||||
|
// Boost.Process
|
||||||
|
// ~~~~~~~~~~~~~
|
||||||
|
//
|
||||||
|
// Copyright (c) 2006, 2007 Julio M. Merino Vidal
|
||||||
|
// Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
|
||||||
|
// Copyright (c) 2009 Boris Schaeling
|
||||||
|
// Copyright (c) 2010 Felipe Tanus, Boris Schaeling
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \file boost/process/environment.hpp
|
||||||
|
*
|
||||||
|
* Includes the declaration of the environment class.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BOOST_PROCESS_ENVIRONMENT_HPP
|
||||||
|
#define BOOST_PROCESS_ENVIRONMENT_HPP
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
namespace process {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Representation of a process' environment variables.
|
||||||
|
*
|
||||||
|
* The environment is a map that establishes an unidirectional
|
||||||
|
* association between variable names and their values and is
|
||||||
|
* represented by a string to string map.
|
||||||
|
*
|
||||||
|
* Variables may be defined to the empty string. Be aware that doing so
|
||||||
|
* is not portable: POSIX systems will treat such variables as being
|
||||||
|
* defined to the empty value, but Windows systems are not able to
|
||||||
|
* distinguish them from undefined variables.
|
||||||
|
*
|
||||||
|
* Neither POSIX nor Windows systems support a variable with no name.
|
||||||
|
*
|
||||||
|
* It is worthy to note that the environment is sorted alphabetically.
|
||||||
|
* This is provided for-free by the map container used to implement this
|
||||||
|
* type, and this behavior is required by Windows systems.
|
||||||
|
*/
|
||||||
|
typedef std::map<std::string, std::string> environment;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
231
include/boost/process/handle.hpp
Normal file
231
include/boost/process/handle.hpp
Normal file
|
|
@ -0,0 +1,231 @@
|
||||||
|
//
|
||||||
|
// Boost.Process
|
||||||
|
// ~~~~~~~~~~~~~
|
||||||
|
//
|
||||||
|
// Copyright (c) 2006, 2007 Julio M. Merino Vidal
|
||||||
|
// Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
|
||||||
|
// Copyright (c) 2009 Boris Schaeling
|
||||||
|
// Copyright (c) 2010 Felipe Tanus, Boris Schaeling
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \file boost/process/handle.hpp
|
||||||
|
*
|
||||||
|
* Includes the declaration of the handle class.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BOOST_PROCESS_HANDLE_HPP
|
||||||
|
#define BOOST_PROCESS_HANDLE_HPP
|
||||||
|
|
||||||
|
#include <boost/process/config.hpp>
|
||||||
|
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
# include <unistd.h>
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
# include <windows.h>
|
||||||
|
#else
|
||||||
|
# error "Unsupported platform."
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <boost/shared_ptr.hpp>
|
||||||
|
#include <boost/make_shared.hpp>
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
namespace process {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RAII model for handles.
|
||||||
|
*
|
||||||
|
* The \a handle class is a RAII model for native handles. This class wraps
|
||||||
|
* one of such handles grabbing its ownership, and automaticaly closes it
|
||||||
|
* upon destruction. It is used to avoid leaking open handles, shall an
|
||||||
|
* unexpected execution trace occur.
|
||||||
|
*/
|
||||||
|
class handle
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
#if defined(BOOST_PROCESS_DOXYGEN)
|
||||||
|
/**
|
||||||
|
* Opaque name for the native handle type.
|
||||||
|
*
|
||||||
|
* On POSIX systems \a NativeSystemHandle is an integer type while it is
|
||||||
|
* a \a HANDLE on Windows systems.
|
||||||
|
*/
|
||||||
|
typedef NativeSystemHandle native_type;
|
||||||
|
#elif defined(BOOST_POSIX_API)
|
||||||
|
typedef int native_type;
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
typedef HANDLE native_type;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs an invalid handle.
|
||||||
|
*
|
||||||
|
* \see valid()
|
||||||
|
*/
|
||||||
|
handle()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RAII settings to specify if handle should be automatically closed.
|
||||||
|
*/
|
||||||
|
enum close_type { do_close, dont_close };
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a handle from a native handle.
|
||||||
|
*
|
||||||
|
* This constructor creates a new \a handle object that takes
|
||||||
|
* ownership of the given \a native handle. If \a close is set to
|
||||||
|
* handle::dont_close the \a native handle is not closed upon destruction.
|
||||||
|
* The user must not close \a native if it is owned by a \a handle object.
|
||||||
|
* Ownership can be reclaimed using release().
|
||||||
|
*
|
||||||
|
* \see release()
|
||||||
|
*/
|
||||||
|
handle(native_type native, close_type close = handle::do_close)
|
||||||
|
: impl_(boost::make_shared<impl>(native, close))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks whether the handle is valid or not.
|
||||||
|
*
|
||||||
|
* \return true if the handle is valid; false otherwise.
|
||||||
|
*/
|
||||||
|
bool valid() const
|
||||||
|
{
|
||||||
|
return impl_ && impl_->valid();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Closes the handle.
|
||||||
|
*
|
||||||
|
* \post The handle is invalid.
|
||||||
|
* \post The native handle is closed.
|
||||||
|
*/
|
||||||
|
void close()
|
||||||
|
{
|
||||||
|
if (impl_)
|
||||||
|
impl_->close();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the native handle.
|
||||||
|
*
|
||||||
|
* The caller can issue any operation on it except closing it.
|
||||||
|
* If closing is required, release() shall be used.
|
||||||
|
*
|
||||||
|
* \return The native handle.
|
||||||
|
*/
|
||||||
|
native_type native() const
|
||||||
|
{
|
||||||
|
return impl_ ? impl_->native() : invalid_handle();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reclaims ownership of the native handle.
|
||||||
|
*
|
||||||
|
* The caller is responsible of closing the native handle.
|
||||||
|
*
|
||||||
|
* \post The handle is invalid.
|
||||||
|
* \return The native handle.
|
||||||
|
*/
|
||||||
|
native_type release()
|
||||||
|
{
|
||||||
|
return impl_ ? impl_->release() : invalid_handle();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
class impl
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
typedef handle::native_type native_type;
|
||||||
|
|
||||||
|
impl(native_type native, close_type close)
|
||||||
|
: native_(native),
|
||||||
|
close_(close)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
~impl()
|
||||||
|
{
|
||||||
|
if (valid() && close_ == handle::do_close)
|
||||||
|
{
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
::close(native_);
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
CloseHandle(native_);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool valid() const
|
||||||
|
{
|
||||||
|
return native_ != handle::invalid_handle();
|
||||||
|
}
|
||||||
|
|
||||||
|
void close()
|
||||||
|
{
|
||||||
|
if (valid())
|
||||||
|
{
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
::close(native_);
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
CloseHandle(native_);
|
||||||
|
#endif
|
||||||
|
native_ = handle::invalid_handle();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
native_type native() const
|
||||||
|
{
|
||||||
|
return native_;
|
||||||
|
}
|
||||||
|
|
||||||
|
native_type release()
|
||||||
|
{
|
||||||
|
native_type native = native_;
|
||||||
|
native_ = handle::invalid_handle();
|
||||||
|
return native;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
native_type native_;
|
||||||
|
close_type close_;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implementation of handle to store native handle value.
|
||||||
|
*
|
||||||
|
* A shared pointer is used as handles represent system resources. If a
|
||||||
|
* handle is closed and becomes invalid the state of copies of the handle
|
||||||
|
* object will be updated as they all share the handle implementation.
|
||||||
|
*/
|
||||||
|
boost::shared_ptr<impl> impl_;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constant function representing an invalid handle value.
|
||||||
|
*
|
||||||
|
* Returns the platform-specific handle value that represents an
|
||||||
|
* invalid handle. This is a constant function rather than a regular
|
||||||
|
* constant because, in the latter case, we cannot define it under
|
||||||
|
* Windows due to the value being of a complex type.
|
||||||
|
*/
|
||||||
|
static const native_type invalid_handle()
|
||||||
|
{
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
return -1;
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
return INVALID_HANDLE_VALUE;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
433
include/boost/process/operations.hpp
Normal file
433
include/boost/process/operations.hpp
Normal file
|
|
@ -0,0 +1,433 @@
|
||||||
|
//
|
||||||
|
// Boost.Process
|
||||||
|
// ~~~~~~~~~~~~~
|
||||||
|
//
|
||||||
|
// Copyright (c) 2006, 2007 Julio M. Merino Vidal
|
||||||
|
// Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
|
||||||
|
// Copyright (c) 2009 Boris Schaeling
|
||||||
|
// Copyright (c) 2010 Felipe Tanus, Boris Schaeling
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \file boost/process/operations.hpp
|
||||||
|
*
|
||||||
|
* Provides miscellaneous free functions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BOOST_PROCESS_OPERATIONS_HPP
|
||||||
|
#define BOOST_PROCESS_OPERATIONS_HPP
|
||||||
|
|
||||||
|
#include <boost/process/config.hpp>
|
||||||
|
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
# include <boost/process/detail/posix_helpers.hpp>
|
||||||
|
# include <utility>
|
||||||
|
# include <cstddef>
|
||||||
|
# include <stdlib.h>
|
||||||
|
# include <unistd.h>
|
||||||
|
# include <fcntl.h>
|
||||||
|
# if defined(__CYGWIN__)
|
||||||
|
# include <boost/scoped_array.hpp>
|
||||||
|
# include <sys/cygwin.h>
|
||||||
|
# endif
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
# include <boost/process/detail/windows_helpers.hpp>
|
||||||
|
# include <boost/scoped_array.hpp>
|
||||||
|
# include <boost/shared_array.hpp>
|
||||||
|
# include <windows.h>
|
||||||
|
#else
|
||||||
|
# error "Unsupported platform."
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <boost/process/child.hpp>
|
||||||
|
#include <boost/process/context.hpp>
|
||||||
|
#include <boost/process/stream_id.hpp>
|
||||||
|
#include <boost/process/stream_ends.hpp>
|
||||||
|
#include <boost/process/handle.hpp>
|
||||||
|
#include <boost/filesystem/path.hpp>
|
||||||
|
#include <boost/filesystem.hpp>
|
||||||
|
#include <boost/algorithm/string/predicate.hpp>
|
||||||
|
#include <boost/system/system_error.hpp>
|
||||||
|
#include <boost/throw_exception.hpp>
|
||||||
|
#include <boost/assert.hpp>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <map>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
namespace process {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Locates the executable program \a file in all the directory components
|
||||||
|
* specified in \a path. If \a path is empty, the value of the PATH
|
||||||
|
* environment variable is used.
|
||||||
|
*
|
||||||
|
* The path variable is interpreted following the same conventions used
|
||||||
|
* to parse the PATH environment variable in the underlying platform.
|
||||||
|
*
|
||||||
|
* \throw boost::filesystem::filesystem_error If the file cannot be found
|
||||||
|
* in the path.
|
||||||
|
*/
|
||||||
|
inline std::string find_executable_in_path(const std::string &file,
|
||||||
|
std::string path = "")
|
||||||
|
{
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
BOOST_ASSERT(file.find('/') == std::string::npos);
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
BOOST_ASSERT(file.find_first_of("\\/") == std::string::npos);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
std::string result;
|
||||||
|
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
if (path.empty())
|
||||||
|
{
|
||||||
|
const char *envpath = getenv("PATH");
|
||||||
|
if (!envpath)
|
||||||
|
boost::throw_exception(boost::filesystem::filesystem_error(
|
||||||
|
BOOST_PROCESS_SOURCE_LOCATION "file not found", file,
|
||||||
|
boost::system::errc::make_error_code(
|
||||||
|
boost::system::errc::no_such_file_or_directory)));
|
||||||
|
path = envpath;
|
||||||
|
}
|
||||||
|
BOOST_ASSERT(!path.empty());
|
||||||
|
|
||||||
|
#if defined(__CYGWIN__)
|
||||||
|
if (!cygwin_posix_path_list_p(path.c_str()))
|
||||||
|
{
|
||||||
|
int size = cygwin_win32_to_posix_path_list_buf_size(path.c_str());
|
||||||
|
boost::scoped_array<char> cygpath(new char[size]);
|
||||||
|
cygwin_win32_to_posix_path_list(path.c_str(), cygpath.get());
|
||||||
|
path = cygpath.get();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
std::string::size_type pos1 = 0, pos2;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
pos2 = path.find(':', pos1);
|
||||||
|
std::string dir = (pos2 != std::string::npos) ?
|
||||||
|
path.substr(pos1, pos2 - pos1) : path.substr(pos1);
|
||||||
|
std::string f = dir +
|
||||||
|
(boost::algorithm::ends_with(dir, "/") ? "" : "/") + file;
|
||||||
|
if (!access(f.c_str(), X_OK))
|
||||||
|
result = f;
|
||||||
|
pos1 = pos2 + 1;
|
||||||
|
} while (pos2 != std::string::npos && result.empty());
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
const char *exts[] = { "", ".exe", ".com", ".bat", NULL };
|
||||||
|
const char **ext = exts;
|
||||||
|
while (*ext)
|
||||||
|
{
|
||||||
|
char buf[MAX_PATH];
|
||||||
|
char *dummy;
|
||||||
|
DWORD size = SearchPathA(path.empty() ? NULL : path.c_str(),
|
||||||
|
file.c_str(), *ext, MAX_PATH, buf, &dummy);
|
||||||
|
BOOST_ASSERT(size < MAX_PATH);
|
||||||
|
if (size > 0)
|
||||||
|
{
|
||||||
|
result = buf;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
++ext;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (result.empty())
|
||||||
|
boost::throw_exception(boost::filesystem::filesystem_error(
|
||||||
|
BOOST_PROCESS_SOURCE_LOCATION "file not found", file,
|
||||||
|
boost::system::errc::make_error_code(
|
||||||
|
boost::system::errc::no_such_file_or_directory)));
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extracts the program name from a given executable.
|
||||||
|
*
|
||||||
|
* \return The program name. On Windows the program name
|
||||||
|
* is returned without a file extension.
|
||||||
|
*/
|
||||||
|
inline std::string executable_to_progname(const std::string &exe)
|
||||||
|
{
|
||||||
|
std::string::size_type begin = 0;
|
||||||
|
std::string::size_type end = std::string::npos;
|
||||||
|
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
std::string::size_type slash = exe.rfind('/');
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
std::string::size_type slash = exe.find_last_of("/\\");
|
||||||
|
#endif
|
||||||
|
if (slash != std::string::npos)
|
||||||
|
begin = slash + 1;
|
||||||
|
|
||||||
|
#if defined(BOOST_WINDOWS_API)
|
||||||
|
if (exe.size() > 4 && (boost::algorithm::iends_with(exe, ".exe") ||
|
||||||
|
boost::algorithm::iends_with(exe, ".com") ||
|
||||||
|
boost::algorithm::iends_with(exe, ".bat")))
|
||||||
|
end = exe.size() - 4;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return exe.substr(begin, end - begin);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starts a new child process.
|
||||||
|
*
|
||||||
|
* Launches a new process based on the binary image specified by the
|
||||||
|
* executable, the set of arguments passed to it and the execution context.
|
||||||
|
*
|
||||||
|
* \remark Blocking remarks: This function may block if the device holding the
|
||||||
|
* executable blocks when loading the image. This might happen if, e.g.,
|
||||||
|
* the binary is being loaded from a network share.
|
||||||
|
*
|
||||||
|
* \return A handle to the new child process.
|
||||||
|
*/
|
||||||
|
template <typename Arguments, typename Context>
|
||||||
|
inline child create_child(const std::string &executable, Arguments args,
|
||||||
|
Context ctx)
|
||||||
|
{
|
||||||
|
typedef std::map<stream_id, stream_ends> handles_t;
|
||||||
|
handles_t handles;
|
||||||
|
typename Context::streams_t::iterator it = ctx.streams.begin();
|
||||||
|
for (; it != ctx.streams.end(); ++it)
|
||||||
|
{
|
||||||
|
if (it->first == stdin_id)
|
||||||
|
handles[it->first] = it->second(input_stream);
|
||||||
|
else if (it->first == stdout_id)
|
||||||
|
handles[it->first] = it->second(output_stream);
|
||||||
|
else if (it->first == stderr_id)
|
||||||
|
handles[it->first] = it->second(output_stream);
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
else
|
||||||
|
handles[it->first] = it->second(unknown_stream);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string p_name = ctx.process_name.empty() ?
|
||||||
|
executable_to_progname(executable) : ctx.process_name;
|
||||||
|
args.insert(args.begin(), p_name);
|
||||||
|
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
// Between fork() and execve() only async-signal-safe functions
|
||||||
|
// must be called if multithreaded applications should be supported.
|
||||||
|
// That's why the following code is executed before fork() is called.
|
||||||
|
#if defined(F_MAXFD)
|
||||||
|
int maxdescs = fcntl(-1, F_MAXFD, 0);
|
||||||
|
if (maxdescs == -1)
|
||||||
|
maxdescs = sysconf(_SC_OPEN_MAX);
|
||||||
|
#else
|
||||||
|
int maxdescs = sysconf(_SC_OPEN_MAX);
|
||||||
|
#endif
|
||||||
|
if (maxdescs == -1)
|
||||||
|
maxdescs = 1024;
|
||||||
|
std::vector<bool> closeflags(maxdescs, true);
|
||||||
|
std::pair<std::size_t, char**> argv = detail::collection_to_argv(args);
|
||||||
|
std::pair<std::size_t, char**> envp =
|
||||||
|
detail::environment_to_envp(ctx.env);
|
||||||
|
|
||||||
|
const char *work_dir = ctx.work_dir.c_str();
|
||||||
|
|
||||||
|
pid_t pid = fork();
|
||||||
|
if (pid == -1)
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("fork(2) failed");
|
||||||
|
else if (pid == 0)
|
||||||
|
{
|
||||||
|
if (chdir(work_dir) == -1)
|
||||||
|
{
|
||||||
|
write(STDERR_FILENO, "chdir() failed\n", 15);
|
||||||
|
_exit(127);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (handles_t::iterator it = handles.begin(); it != handles.end();
|
||||||
|
++it)
|
||||||
|
{
|
||||||
|
if (it->second.child.valid())
|
||||||
|
{
|
||||||
|
handles_t::iterator it2 = it;
|
||||||
|
++it2;
|
||||||
|
for (; it2 != handles.end(); ++it2)
|
||||||
|
{
|
||||||
|
if (it2->second.child.native() == it->first)
|
||||||
|
{
|
||||||
|
int fd = fcntl(it2->second.child.native(), F_DUPFD,
|
||||||
|
it->first + 1);
|
||||||
|
if (fd == -1)
|
||||||
|
{
|
||||||
|
write(STDERR_FILENO, "fcntl() failed\n", 15);
|
||||||
|
_exit(127);
|
||||||
|
}
|
||||||
|
it2->second.child = fd;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dup2(it->second.child.native(), it->first) == -1)
|
||||||
|
{
|
||||||
|
write(STDERR_FILENO, "dup2() failed\n", 14);
|
||||||
|
_exit(127);
|
||||||
|
}
|
||||||
|
closeflags[it->first] = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ctx.setup)
|
||||||
|
ctx.setup();
|
||||||
|
|
||||||
|
for (std::size_t i = 0; i < closeflags.size(); ++i)
|
||||||
|
{
|
||||||
|
if (closeflags[i])
|
||||||
|
close(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
execve(executable.c_str(), argv.second, envp.second);
|
||||||
|
|
||||||
|
// Actually we should delete argv and envp data. As we must not
|
||||||
|
// call any non-async-signal-safe functions though we simply exit.
|
||||||
|
write(STDERR_FILENO, "execve() failed\n", 16);
|
||||||
|
_exit(127);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
BOOST_ASSERT(pid > 0);
|
||||||
|
|
||||||
|
for (std::size_t i = 0; i < argv.first; ++i)
|
||||||
|
delete[] argv.second[i];
|
||||||
|
delete[] argv.second;
|
||||||
|
|
||||||
|
for (std::size_t i = 0; i < envp.first; ++i)
|
||||||
|
delete[] envp.second[i];
|
||||||
|
delete[] envp.second;
|
||||||
|
|
||||||
|
std::map<stream_id, handle> parent_ends;
|
||||||
|
for (handles_t::iterator it = handles.begin(); it != handles.end();
|
||||||
|
++it)
|
||||||
|
parent_ends[it->first] = it->second.parent;
|
||||||
|
|
||||||
|
return child(pid, parent_ends);
|
||||||
|
}
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
STARTUPINFOA startup_info;
|
||||||
|
ZeroMemory(&startup_info, sizeof(startup_info));
|
||||||
|
startup_info.cb = sizeof(startup_info);
|
||||||
|
startup_info.dwFlags |= STARTF_USESTDHANDLES;
|
||||||
|
startup_info.hStdInput = handles[stdin_id].child.native();
|
||||||
|
startup_info.hStdOutput = handles[stdout_id].child.native();
|
||||||
|
startup_info.hStdError = handles[stderr_id].child.native();
|
||||||
|
|
||||||
|
if (ctx.setup)
|
||||||
|
ctx.setup(startup_info);
|
||||||
|
|
||||||
|
PROCESS_INFORMATION pi;
|
||||||
|
ZeroMemory(&pi, sizeof(pi));
|
||||||
|
|
||||||
|
boost::shared_array<char> cmdline =
|
||||||
|
detail::collection_to_windows_cmdline(args);
|
||||||
|
|
||||||
|
boost::scoped_array<char> exe(new char[executable.size() + 1]);
|
||||||
|
#if (BOOST_MSVC >= 1400)
|
||||||
|
strcpy_s(exe.get(), executable.size() + 1, executable.c_str());
|
||||||
|
#else
|
||||||
|
strcpy(exe.get(), executable.c_str());
|
||||||
|
#endif
|
||||||
|
|
||||||
|
boost::scoped_array<char> workdir(new char[ctx.work_dir.size() + 1]);
|
||||||
|
#if (BOOST_MSVC >= 1400)
|
||||||
|
strcpy_s(workdir.get(), ctx.work_dir.size() + 1, ctx.work_dir.c_str());
|
||||||
|
#else
|
||||||
|
strcpy(workdir.get(), ctx.work_dir.c_str());
|
||||||
|
#endif
|
||||||
|
|
||||||
|
boost::shared_array<char> envstrs =
|
||||||
|
detail::environment_to_windows_strings(ctx.env);
|
||||||
|
|
||||||
|
if (CreateProcessA(exe.get(), cmdline.get(), NULL, NULL, TRUE, 0,
|
||||||
|
envstrs.get(), workdir.get(), &startup_info, &pi) == 0)
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("CreateProcess() failed");
|
||||||
|
|
||||||
|
handle hprocess(pi.hProcess);
|
||||||
|
|
||||||
|
if (!CloseHandle(pi.hThread))
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("CloseHandle() failed");
|
||||||
|
|
||||||
|
std::map<stream_id, handle> parent_ends;
|
||||||
|
parent_ends[stdin_id] = handles[stdin_id].parent;
|
||||||
|
parent_ends[stdout_id] = handles[stdout_id].parent;
|
||||||
|
parent_ends[stderr_id] = handles[stderr_id].parent;
|
||||||
|
|
||||||
|
return child(hprocess, parent_ends);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \overload
|
||||||
|
*/
|
||||||
|
inline child create_child(const std::string &executable)
|
||||||
|
{
|
||||||
|
return create_child(executable, std::vector<std::string>(), context());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \overload
|
||||||
|
*/
|
||||||
|
template <typename Arguments>
|
||||||
|
inline child create_child(const std::string &executable, Arguments args)
|
||||||
|
{
|
||||||
|
return create_child(executable, args, context());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starts a shell-based command.
|
||||||
|
*
|
||||||
|
* Executes the given command through the default system shell. The
|
||||||
|
* command is subject to pattern expansion, redirection and pipelining.
|
||||||
|
* The shell is launched as described by the parameters in the context.
|
||||||
|
*
|
||||||
|
* This function behaves similarly to the system(3) system call. In a
|
||||||
|
* POSIX system, the command is fed to /bin/sh whereas under a Windows
|
||||||
|
* system, it is fed to cmd.exe. It is difficult to write portable
|
||||||
|
* commands, but this function comes in handy in multiple situations.
|
||||||
|
*
|
||||||
|
* \remark Blocking remarks: This function may block if the device holding the
|
||||||
|
* executable blocks when loading the image. This might happen if, e.g.,
|
||||||
|
* the binary is being loaded from a network share.
|
||||||
|
*
|
||||||
|
* \return A handle to the new child process.
|
||||||
|
*/
|
||||||
|
template <typename Context>
|
||||||
|
inline child shell(const std::string &command, Context ctx)
|
||||||
|
{
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
std::string executable = "/bin/sh";
|
||||||
|
std::vector<std::string> args;
|
||||||
|
args.push_back("-c");
|
||||||
|
args.push_back(command);
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
char sysdir[MAX_PATH];
|
||||||
|
UINT size = GetSystemDirectoryA(sysdir, sizeof(sysdir));
|
||||||
|
if (!size)
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("GetSystemDirectory() failed");
|
||||||
|
std::string executable = std::string(sysdir) +
|
||||||
|
(sysdir[size - 1] != '\\' ? "\\cmd.exe" : "cmd.exe");
|
||||||
|
std::vector<std::string> args;
|
||||||
|
args.push_back("/c");
|
||||||
|
args.push_back(command);
|
||||||
|
#endif
|
||||||
|
return create_child(executable, args, ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \overload
|
||||||
|
*/
|
||||||
|
inline child shell(const std::string &command)
|
||||||
|
{
|
||||||
|
return shell(command, context());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
56
include/boost/process/pid_type.hpp
Normal file
56
include/boost/process/pid_type.hpp
Normal file
|
|
@ -0,0 +1,56 @@
|
||||||
|
//
|
||||||
|
// Boost.Process
|
||||||
|
// ~~~~~~~~~~~~~
|
||||||
|
//
|
||||||
|
// Copyright (c) 2006, 2007 Julio M. Merino Vidal
|
||||||
|
// Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
|
||||||
|
// Copyright (c) 2009 Boris Schaeling
|
||||||
|
// Copyright (c) 2010 Felipe Tanus, Boris Schaeling
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \file boost/process/pid_type.hpp
|
||||||
|
*
|
||||||
|
* Includes the declaration of the pid type.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BOOST_PROCESS_PID_TYPE_HPP
|
||||||
|
#define BOOST_PROCESS_PID_TYPE_HPP
|
||||||
|
|
||||||
|
#include <boost/process/config.hpp>
|
||||||
|
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
# include <sys/types.h>
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
# include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
namespace process {
|
||||||
|
|
||||||
|
#if defined(BOOST_PROCESS_DOXYGEN)
|
||||||
|
/**
|
||||||
|
* Opaque name for the process identifier type.
|
||||||
|
*
|
||||||
|
* Each operating system identifies processes using a specific type.
|
||||||
|
* The \a pid_type type is used to transparently refer to a process
|
||||||
|
* regardless of the operating system.
|
||||||
|
*
|
||||||
|
* This type is guaranteed to be an integral type on all supported
|
||||||
|
* platforms. On POSIX systems it is defined as pid_t, on Windows systems as
|
||||||
|
* DWORD.
|
||||||
|
*/
|
||||||
|
typedef NativeProcessId pid_type;
|
||||||
|
#elif defined(BOOST_POSIX_API)
|
||||||
|
typedef pid_t pid_type;
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
typedef DWORD pid_type;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
49
include/boost/process/pipe.hpp
Normal file
49
include/boost/process/pipe.hpp
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
//
|
||||||
|
// Boost.Process
|
||||||
|
// ~~~~~~~~~~~~~
|
||||||
|
//
|
||||||
|
// Copyright (c) 2006, 2007 Julio M. Merino Vidal
|
||||||
|
// Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
|
||||||
|
// Copyright (c) 2009 Boris Schaeling
|
||||||
|
// Copyright (c) 2010 Felipe Tanus, Boris Schaeling
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \file boost/process/pipe.hpp
|
||||||
|
*
|
||||||
|
* Includes the declaration of the pipe class.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BOOST_PROCESS_PIPE_HPP
|
||||||
|
#define BOOST_PROCESS_PIPE_HPP
|
||||||
|
|
||||||
|
#include <boost/process/config.hpp>
|
||||||
|
#include <boost/asio.hpp>
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
namespace process {
|
||||||
|
|
||||||
|
#if defined(BOOST_PROCESS_DOXYGEN)
|
||||||
|
/**
|
||||||
|
* The pipe class is a type definition for stream-based classes defined by
|
||||||
|
* Boost.Asio.
|
||||||
|
*
|
||||||
|
* The type definition is provided for convenience. You can also use Boost.Asio
|
||||||
|
* classes directly for asynchronous I/O operations.
|
||||||
|
*/
|
||||||
|
typedef BoostAsioPipe pipe;
|
||||||
|
#elif defined(BOOST_POSIX_API)
|
||||||
|
typedef boost::asio::posix::stream_descriptor pipe;
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
typedef boost::asio::windows::stream_handle pipe;
|
||||||
|
#else
|
||||||
|
# error "Unsupported platform."
|
||||||
|
#endif
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
114
include/boost/process/pistream.hpp
Normal file
114
include/boost/process/pistream.hpp
Normal file
|
|
@ -0,0 +1,114 @@
|
||||||
|
//
|
||||||
|
// Boost.Process
|
||||||
|
// ~~~~~~~~~~~~~
|
||||||
|
//
|
||||||
|
// Copyright (c) 2006, 2007 Julio M. Merino Vidal
|
||||||
|
// Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
|
||||||
|
// Copyright (c) 2009 Boris Schaeling
|
||||||
|
// Copyright (c) 2010 Felipe Tanus, Boris Schaeling
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \file boost/process/pistream.hpp
|
||||||
|
*
|
||||||
|
* Includes the declaration of the pistream class.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BOOST_PROCESS_PISTREAM_HPP
|
||||||
|
#define BOOST_PROCESS_PISTREAM_HPP
|
||||||
|
|
||||||
|
#include <boost/process/handle.hpp>
|
||||||
|
#include <boost/process/detail/systembuf.hpp>
|
||||||
|
#include <boost/noncopyable.hpp>
|
||||||
|
#include <istream>
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
namespace process {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Child process' output stream.
|
||||||
|
*
|
||||||
|
* The pistream class represents an output communication channel with the
|
||||||
|
* child process. The child process writes data to this stream and the
|
||||||
|
* parent process can read it through the pistream object. In other
|
||||||
|
* words, from the child's point of view, the communication channel is an
|
||||||
|
* output one, but from the parent's point of view it is an input one;
|
||||||
|
* hence the confusing pistream name.
|
||||||
|
*
|
||||||
|
* pistream objects cannot be copied because they buffer data
|
||||||
|
* that flows through the communication channel.
|
||||||
|
*
|
||||||
|
* A pistream object behaves as a std::istream stream in all senses.
|
||||||
|
* The class is only provided because it must provide a method to let
|
||||||
|
* the caller explicitly close the communication channel.
|
||||||
|
*
|
||||||
|
* \remark Blocking remarks: Functions that read data from this
|
||||||
|
* stream can block if the associated handle blocks during
|
||||||
|
* the read. As this class is used to communicate with child
|
||||||
|
* processes through anonymous pipes, the most typical blocking
|
||||||
|
* condition happens when the child has no more data to send to
|
||||||
|
* the pipe's system buffer. When this happens, the buffer
|
||||||
|
* eventually empties and the system blocks until the writer
|
||||||
|
* generates some data.
|
||||||
|
*/
|
||||||
|
class pistream : public std::istream, public boost::noncopyable
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* Creates a new process' output stream.
|
||||||
|
*/
|
||||||
|
explicit pistream(boost::process::handle h)
|
||||||
|
: std::istream(0),
|
||||||
|
handle_(h),
|
||||||
|
systembuf_(handle_.native())
|
||||||
|
{
|
||||||
|
rdbuf(&systembuf_);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the handle managed by this stream.
|
||||||
|
*/
|
||||||
|
const boost::process::handle &handle() const
|
||||||
|
{
|
||||||
|
return handle_;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the handle managed by this stream.
|
||||||
|
*/
|
||||||
|
boost::process::handle &handle()
|
||||||
|
{
|
||||||
|
return handle_;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Closes the handle managed by this stream.
|
||||||
|
*
|
||||||
|
* Explicitly closes the handle managed by this stream. This
|
||||||
|
* function can be used by the user to tell the child process it's
|
||||||
|
* not willing to receive more data.
|
||||||
|
*/
|
||||||
|
void close()
|
||||||
|
{
|
||||||
|
handle_.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
/**
|
||||||
|
* The handle managed by this stream.
|
||||||
|
*/
|
||||||
|
boost::process::handle handle_;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The systembuf object used to manage this stream's data.
|
||||||
|
*/
|
||||||
|
detail::systembuf systembuf_;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
115
include/boost/process/postream.hpp
Normal file
115
include/boost/process/postream.hpp
Normal file
|
|
@ -0,0 +1,115 @@
|
||||||
|
//
|
||||||
|
// Boost.Process
|
||||||
|
// ~~~~~~~~~~~~~
|
||||||
|
//
|
||||||
|
// Copyright (c) 2006, 2007 Julio M. Merino Vidal
|
||||||
|
// Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
|
||||||
|
// Copyright (c) 2009 Boris Schaeling
|
||||||
|
// Copyright (c) 2010 Felipe Tanus, Boris Schaeling
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \file boost/process/postream.hpp
|
||||||
|
*
|
||||||
|
* Includes the declaration of the postream class.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BOOST_PROCESS_POSTREAM_HPP
|
||||||
|
#define BOOST_PROCESS_POSTREAM_HPP
|
||||||
|
|
||||||
|
#include <boost/process/handle.hpp>
|
||||||
|
#include <boost/process/detail/systembuf.hpp>
|
||||||
|
#include <boost/noncopyable.hpp>
|
||||||
|
#include <ostream>
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
namespace process {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Child process' input stream.
|
||||||
|
*
|
||||||
|
* The postream class represents an input communication channel with the
|
||||||
|
* child process. The child process reads data from this stream and the
|
||||||
|
* parent process can write to it through the postream object. In other
|
||||||
|
* words, from the child's point of view, the communication channel is an
|
||||||
|
* input one, but from the parent's point of view it is an output one;
|
||||||
|
* hence the confusing postream name.
|
||||||
|
*
|
||||||
|
* postream objects cannot be copied because they buffer data that flows
|
||||||
|
* through the communication channel.
|
||||||
|
*
|
||||||
|
* A postream object behaves as a std::ostream stream in all senses.
|
||||||
|
* The class is only provided because it must provide a method to let
|
||||||
|
* the caller explicitly close the communication channel.
|
||||||
|
*
|
||||||
|
* \remark Blocking remarks: Functions that write data to this
|
||||||
|
* stream can block if the associated handle blocks during
|
||||||
|
* the write. As this class is used to communicate with child
|
||||||
|
* processes through anonymous pipes, the most typical blocking
|
||||||
|
* condition happens when the child is not processing the data
|
||||||
|
* in the pipe's system buffer. When this happens, the buffer
|
||||||
|
* eventually fills up and the system blocks until the reader
|
||||||
|
* consumes some data, leaving some new room.
|
||||||
|
*/
|
||||||
|
class postream : public std::ostream, public boost::noncopyable
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* Creates a new process' input stream.
|
||||||
|
*/
|
||||||
|
explicit postream(boost::process::handle h)
|
||||||
|
: std::ostream(0),
|
||||||
|
handle_(h),
|
||||||
|
systembuf_(handle_.native())
|
||||||
|
{
|
||||||
|
rdbuf(&systembuf_);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the handle managed by this stream.
|
||||||
|
*/
|
||||||
|
const boost::process::handle &handle() const
|
||||||
|
{
|
||||||
|
return handle_;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the handle managed by this stream.
|
||||||
|
*/
|
||||||
|
boost::process::handle &handle()
|
||||||
|
{
|
||||||
|
return handle_;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Closes the handle managed by this stream.
|
||||||
|
*
|
||||||
|
* Explicitly closes the handle managed by this stream. This
|
||||||
|
* function can be used by the user to tell the child process there
|
||||||
|
* is no more data to send.
|
||||||
|
*/
|
||||||
|
void close()
|
||||||
|
{
|
||||||
|
systembuf_.sync();
|
||||||
|
handle_.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
/**
|
||||||
|
* The handle managed by this stream.
|
||||||
|
*/
|
||||||
|
boost::process::handle handle_;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The systembuf object used to manage this stream's data.
|
||||||
|
*/
|
||||||
|
detail::systembuf systembuf_;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
212
include/boost/process/process.hpp
Normal file
212
include/boost/process/process.hpp
Normal file
|
|
@ -0,0 +1,212 @@
|
||||||
|
//
|
||||||
|
// Boost.Process
|
||||||
|
// ~~~~~~~~~~~~~
|
||||||
|
//
|
||||||
|
// Copyright (c) 2006, 2007 Julio M. Merino Vidal
|
||||||
|
// Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
|
||||||
|
// Copyright (c) 2009 Boris Schaeling
|
||||||
|
// Copyright (c) 2010 Felipe Tanus, Boris Schaeling
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \file boost/process/process.hpp
|
||||||
|
*
|
||||||
|
* Includes the declaration of the process class.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BOOST_PROCESS_PROCESS_HPP
|
||||||
|
#define BOOST_PROCESS_PROCESS_HPP
|
||||||
|
|
||||||
|
#include <boost/process/config.hpp>
|
||||||
|
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
# include <unistd.h>
|
||||||
|
# include <sys/types.h>
|
||||||
|
# include <signal.h>
|
||||||
|
# include <sys/wait.h>
|
||||||
|
# include <errno.h>
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
# include <boost/process/handle.hpp>
|
||||||
|
# include <cstdlib>
|
||||||
|
# include <windows.h>
|
||||||
|
#else
|
||||||
|
# error "Unsupported platform."
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <boost/process/pid_type.hpp>
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
namespace process {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Process class to represent any running process.
|
||||||
|
*/
|
||||||
|
class process
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* Constructs a new process object.
|
||||||
|
*
|
||||||
|
* Creates a new process object that represents a running process
|
||||||
|
* within the system.
|
||||||
|
*
|
||||||
|
* On Windows the process is opened and a handle saved. This is required
|
||||||
|
* to avoid the operating system removing process resources when the
|
||||||
|
* process exits. The handle is closed when the process instance (and all
|
||||||
|
* of its copies) is destroyed.
|
||||||
|
*/
|
||||||
|
process(pid_type id)
|
||||||
|
: id_(id)
|
||||||
|
#if defined(BOOST_WINDOWS_API)
|
||||||
|
, handle_(open_process(id))
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(BOOST_WINDOWS_API) || defined(BOOST_PROCESS_DOXYGEN)
|
||||||
|
/**
|
||||||
|
* Constructs a new process object.
|
||||||
|
*
|
||||||
|
* Creates a new process object that represents a running process
|
||||||
|
* within the system.
|
||||||
|
*
|
||||||
|
* This operation is only available on Windows systems. The handle is
|
||||||
|
* closed when the process instance (and all of its copies) is destroyed.
|
||||||
|
*/
|
||||||
|
process(handle h)
|
||||||
|
: id_(GetProcessId(h.native())),
|
||||||
|
handle_(h)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the process identifier.
|
||||||
|
*/
|
||||||
|
pid_type get_id() const
|
||||||
|
{
|
||||||
|
return id_;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Terminates the process execution.
|
||||||
|
*
|
||||||
|
* Forces the termination of the process execution. Some platforms
|
||||||
|
* allow processes to ignore some external termination notifications
|
||||||
|
* or to capture them for a proper exit cleanup. You can set the
|
||||||
|
* \a force flag to true to force their termination regardless
|
||||||
|
* of any exit handler.
|
||||||
|
*
|
||||||
|
* After this call, accessing this object can be dangerous because the
|
||||||
|
* process identifier may have been reused by a different process. It
|
||||||
|
* might still be valid, though, if the process has refused to die.
|
||||||
|
*
|
||||||
|
* \throw boost::system::system_error If system calls used to terminate the
|
||||||
|
* process fail.
|
||||||
|
*/
|
||||||
|
void terminate(bool force = false) const
|
||||||
|
{
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
if (kill(id_, force ? SIGKILL : SIGTERM) == -1)
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("kill(2) failed");
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
#if defined(BOOST_MSVC)
|
||||||
|
force;
|
||||||
|
#endif
|
||||||
|
HANDLE h = OpenProcess(PROCESS_TERMINATE, FALSE, id_);
|
||||||
|
if (h == NULL)
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("OpenProcess() failed");
|
||||||
|
if (!TerminateProcess(h, EXIT_FAILURE))
|
||||||
|
{
|
||||||
|
CloseHandle(h);
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("TerminateProcess() failed");
|
||||||
|
}
|
||||||
|
if (!CloseHandle(h))
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("CloseHandle() failed");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Blocks and waits for the process to terminate.
|
||||||
|
*
|
||||||
|
* Returns an exit code. The process object ceases to be valid after this
|
||||||
|
* call.
|
||||||
|
*
|
||||||
|
* \remark Blocking remarks: This call blocks if the process has not
|
||||||
|
* finalized execution and waits until it terminates.
|
||||||
|
*
|
||||||
|
* \throw boost::system::system_error If system calls used to wait for the
|
||||||
|
* process fail.
|
||||||
|
*/
|
||||||
|
int wait() const
|
||||||
|
{
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
pid_t p;
|
||||||
|
int status;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
p = waitpid(id_, &status, 0);
|
||||||
|
} while (p == -1 && errno == EINTR);
|
||||||
|
if (p == -1)
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("waitpid(2) failed");
|
||||||
|
return status;
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION | SYNCHRONIZE, FALSE,
|
||||||
|
id_);
|
||||||
|
if (h == NULL)
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("OpenProcess() failed");
|
||||||
|
if (WaitForSingleObject(h, INFINITE) == WAIT_FAILED)
|
||||||
|
{
|
||||||
|
CloseHandle(h);
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR(
|
||||||
|
"WaitForSingleObject() failed");
|
||||||
|
}
|
||||||
|
DWORD exit_code;
|
||||||
|
if (!GetExitCodeProcess(h, &exit_code))
|
||||||
|
{
|
||||||
|
CloseHandle(h);
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR(
|
||||||
|
"GetExitCodeProcess() failed");
|
||||||
|
}
|
||||||
|
if (!CloseHandle(h))
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("CloseHandle() failed");
|
||||||
|
return exit_code;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
/**
|
||||||
|
* The process identifier.
|
||||||
|
*/
|
||||||
|
pid_type id_;
|
||||||
|
|
||||||
|
#if defined(BOOST_WINDOWS_API)
|
||||||
|
/**
|
||||||
|
* Opens a process and returns a handle.
|
||||||
|
*
|
||||||
|
* OpenProcess() returns NULL and not INVALID_HANDLE_VALUE on failure.
|
||||||
|
* That's why the return value is manually checked in this helper function
|
||||||
|
* instead of simply passing it to the constructor of the handle class.
|
||||||
|
*/
|
||||||
|
HANDLE open_process(pid_type id)
|
||||||
|
{
|
||||||
|
HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, id);
|
||||||
|
if (h == NULL)
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("OpenProcess() failed");
|
||||||
|
return h;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The process handle.
|
||||||
|
*/
|
||||||
|
handle handle_;
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
188
include/boost/process/self.hpp
Normal file
188
include/boost/process/self.hpp
Normal file
|
|
@ -0,0 +1,188 @@
|
||||||
|
//
|
||||||
|
// Boost.Process
|
||||||
|
// ~~~~~~~~~~~~~
|
||||||
|
//
|
||||||
|
// Copyright (c) 2006, 2007 Julio M. Merino Vidal
|
||||||
|
// Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
|
||||||
|
// Copyright (c) 2009 Boris Schaeling
|
||||||
|
// Copyright (c) 2010 Felipe Tanus, Boris Schaeling
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \file boost/process/self.hpp
|
||||||
|
*
|
||||||
|
* Includes the declaration of the self class.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BOOST_PROCESS_SELF_HPP
|
||||||
|
#define BOOST_PROCESS_SELF_HPP
|
||||||
|
|
||||||
|
#include <boost/process/config.hpp>
|
||||||
|
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
# include <boost/scoped_array.hpp>
|
||||||
|
# include <errno.h>
|
||||||
|
# include <unistd.h>
|
||||||
|
# include <limits.h>
|
||||||
|
# if defined(__APPLE__)
|
||||||
|
# include <crt_externs.h>
|
||||||
|
# endif
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
# include <windows.h>
|
||||||
|
#else
|
||||||
|
# error "Unsupported platform."
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <boost/process/process.hpp>
|
||||||
|
#include <boost/process/environment.hpp>
|
||||||
|
#include <boost/noncopyable.hpp>
|
||||||
|
#include <boost/assert.hpp>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
extern char **environ;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
namespace process {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The self class provides access to the process itself.
|
||||||
|
*/
|
||||||
|
class self : public process, public boost::noncopyable
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* Returns the self instance representing the caller's process.
|
||||||
|
*/
|
||||||
|
static self &get_instance()
|
||||||
|
{
|
||||||
|
static self *instance = 0;
|
||||||
|
if (!instance)
|
||||||
|
instance = new self;
|
||||||
|
return *instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the current environment.
|
||||||
|
*
|
||||||
|
* Returns the current process environment variables. Modifying the
|
||||||
|
* returned object has no effect on the current environment.
|
||||||
|
*/
|
||||||
|
static environment get_environment()
|
||||||
|
{
|
||||||
|
environment e;
|
||||||
|
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
# if defined(__APPLE__)
|
||||||
|
char **env = *_NSGetEnviron();
|
||||||
|
# else
|
||||||
|
char **env = environ;
|
||||||
|
# endif
|
||||||
|
|
||||||
|
while (*env)
|
||||||
|
{
|
||||||
|
std::string s = *env;
|
||||||
|
std::string::size_type pos = s.find('=');
|
||||||
|
e.insert(environment::value_type(s.substr(0, pos),
|
||||||
|
s.substr(pos + 1)));
|
||||||
|
++env;
|
||||||
|
}
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
# ifdef GetEnvironmentStrings
|
||||||
|
# undef GetEnvironmentStrings
|
||||||
|
# endif
|
||||||
|
|
||||||
|
char *ms_environ = GetEnvironmentStrings();
|
||||||
|
if (!ms_environ)
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR(
|
||||||
|
"GetEnvironmentStrings() failed");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
char *env = ms_environ;
|
||||||
|
while (*env)
|
||||||
|
{
|
||||||
|
std::string s = env;
|
||||||
|
std::string::size_type pos = s.find('=');
|
||||||
|
e.insert(environment::value_type(s.substr(0, pos),
|
||||||
|
s.substr(pos + 1)));
|
||||||
|
env += s.size() + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
FreeEnvironmentStringsA(ms_environ);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
FreeEnvironmentStringsA(ms_environ);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the current work directory.
|
||||||
|
*/
|
||||||
|
static std::string get_work_dir()
|
||||||
|
{
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
#if defined(PATH_MAX)
|
||||||
|
char buffer[PATH_MAX];
|
||||||
|
char *cwd = buffer;
|
||||||
|
long size = PATH_MAX;
|
||||||
|
#elif defined(_PC_PATH_MAX)
|
||||||
|
errno = 0;
|
||||||
|
long size = pathconf("/", _PC_PATH_MAX);
|
||||||
|
if (size == -1 && errno)
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("pathconf(2) failed");
|
||||||
|
else if (size == -1)
|
||||||
|
size = BOOST_PROCESS_POSIX_PATH_MAX;
|
||||||
|
BOOST_ASSERT(size > 0);
|
||||||
|
boost::scoped_array<char> buffer(new char[size]);
|
||||||
|
char *cwd = buffer.get();
|
||||||
|
#else
|
||||||
|
char buffer[BOOST_PROCESS_POSIX_PATH_MAX];
|
||||||
|
char *cwd = buffer;
|
||||||
|
long size = BOOST_PROCESS_POSIX_PATH_MAX;
|
||||||
|
#endif
|
||||||
|
if (!getcwd(cwd, size))
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("getcwd(2) failed");
|
||||||
|
BOOST_ASSERT(cwd[0] != '\0');
|
||||||
|
return cwd;
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
BOOST_ASSERT(MAX_PATH > 0);
|
||||||
|
char cwd[MAX_PATH];
|
||||||
|
if (!GetCurrentDirectoryA(sizeof(cwd), cwd))
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR(
|
||||||
|
"GetCurrentDirectory() failed");
|
||||||
|
BOOST_ASSERT(cwd[0] != '\0');
|
||||||
|
return cwd;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
/**
|
||||||
|
* Constructs a new self object.
|
||||||
|
*
|
||||||
|
* Creates a new self object that represents the current process.
|
||||||
|
*/
|
||||||
|
self() :
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
process(getpid())
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
process(GetCurrentProcessId())
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
41
include/boost/process/status.hpp
Normal file
41
include/boost/process/status.hpp
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
//
|
||||||
|
// Boost.Process
|
||||||
|
// ~~~~~~~~~~~~~
|
||||||
|
//
|
||||||
|
// Copyright (c) 2006, 2007 Julio M. Merino Vidal
|
||||||
|
// Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
|
||||||
|
// Copyright (c) 2009 Boris Schaeling
|
||||||
|
// Copyright (c) 2010 Felipe Tanus, Boris Schaeling
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \file boost/process/status.hpp
|
||||||
|
*
|
||||||
|
* Includes the declaration of the status class.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BOOST_PROCESS_STATUS_HPP
|
||||||
|
#define BOOST_PROCESS_STATUS_HPP
|
||||||
|
|
||||||
|
#include <boost/process/config.hpp>
|
||||||
|
#include <boost/process/detail/basic_status.hpp>
|
||||||
|
#include <boost/process/detail/basic_status_service.hpp>
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
namespace process {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The status class to wait for processes to exit.
|
||||||
|
*
|
||||||
|
* The status class is a Boost.Asio I/O object and supports synchronous
|
||||||
|
* and asynchronous wait operations.
|
||||||
|
*/
|
||||||
|
typedef detail::basic_status<detail::basic_status_service<> > status;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
326
include/boost/process/stream_behavior.hpp
Normal file
326
include/boost/process/stream_behavior.hpp
Normal file
|
|
@ -0,0 +1,326 @@
|
||||||
|
//
|
||||||
|
// Boost.Process
|
||||||
|
// ~~~~~~~~~~~~~
|
||||||
|
//
|
||||||
|
// Copyright (c) 2006, 2007 Julio M. Merino Vidal
|
||||||
|
// Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
|
||||||
|
// Copyright (c) 2009 Boris Schaeling
|
||||||
|
// Copyright (c) 2010 Felipe Tanus, Boris Schaeling
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \file boost/process/stream_behavior.hpp
|
||||||
|
*
|
||||||
|
* Includes the declaration of stream behavior classes.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BOOST_PROCESS_STREAM_BEHAVIOR_HPP
|
||||||
|
#define BOOST_PROCESS_STREAM_BEHAVIOR_HPP
|
||||||
|
|
||||||
|
#include <boost/process/config.hpp>
|
||||||
|
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
# include <sys/stat.h>
|
||||||
|
# include <fcntl.h>
|
||||||
|
# include <unistd.h>
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
# include <windows.h>
|
||||||
|
# include <rpc.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <boost/process/stream_ends.hpp>
|
||||||
|
#include <boost/process/stream_type.hpp>
|
||||||
|
#include <boost/process/handle.hpp>
|
||||||
|
#include <string>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
namespace process {
|
||||||
|
namespace behavior {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stream behavior to close streams of a child process.
|
||||||
|
*
|
||||||
|
* A child process will not be able to use the stream.
|
||||||
|
*/
|
||||||
|
class close
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
stream_ends operator()(stream_type) const
|
||||||
|
{
|
||||||
|
return stream_ends();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stream behavior to make a child process inherit streams.
|
||||||
|
*
|
||||||
|
* A child process will use the very same stream of its parent process.
|
||||||
|
*/
|
||||||
|
class inherit
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
inherit(handle::native_type h)
|
||||||
|
: h_(h, handle::dont_close)
|
||||||
|
{
|
||||||
|
#if defined(BOOST_WINDOWS_API)
|
||||||
|
if (h != INVALID_HANDLE_VALUE)
|
||||||
|
{
|
||||||
|
if (!SetHandleInformation(h_.native(), HANDLE_FLAG_INHERIT,
|
||||||
|
HANDLE_FLAG_INHERIT))
|
||||||
|
{
|
||||||
|
HANDLE proc = GetCurrentProcess();
|
||||||
|
HANDLE dup;
|
||||||
|
if (!DuplicateHandle(proc, h_.native(), proc, &dup, 0,
|
||||||
|
TRUE, DUPLICATE_SAME_ACCESS))
|
||||||
|
{
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR(
|
||||||
|
"DuplicateHandle() failed");
|
||||||
|
}
|
||||||
|
h_ = dup;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
stream_ends operator()(stream_type) const
|
||||||
|
{
|
||||||
|
return stream_ends(h_, handle());
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
handle h_;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stream behavior to redirect streams with a pipe.
|
||||||
|
*
|
||||||
|
* A child process will be able to communicate with its parent process.
|
||||||
|
*/
|
||||||
|
class pipe
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
pipe()
|
||||||
|
: stype_(unknown_stream)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
pipe(stream_type stype)
|
||||||
|
: stype_(stype)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
stream_ends operator()(stream_type stype) const
|
||||||
|
{
|
||||||
|
handle::native_type ends[2];
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
if (::pipe(ends) == -1)
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("pipe(2) failed");
|
||||||
|
if (stype_ != unknown_stream)
|
||||||
|
stype = stype_;
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
SECURITY_ATTRIBUTES sa;
|
||||||
|
ZeroMemory(&sa, sizeof(sa));
|
||||||
|
sa.nLength = sizeof(sa);
|
||||||
|
sa.lpSecurityDescriptor = NULL;
|
||||||
|
sa.bInheritHandle = FALSE;
|
||||||
|
if (!CreatePipe(&ends[0], &ends[1], &sa, 0))
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("CreatePipe() failed");
|
||||||
|
#endif
|
||||||
|
handle child_end = ends[stype == input_stream ? 0 : 1];
|
||||||
|
handle parent_end = ends[stype == input_stream ? 1 : 0];
|
||||||
|
#if defined(BOOST_WINDOWS_API)
|
||||||
|
if (!SetHandleInformation(child_end.native(), HANDLE_FLAG_INHERIT,
|
||||||
|
HANDLE_FLAG_INHERIT))
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR(
|
||||||
|
"SetHandleInformation() failed");
|
||||||
|
#endif
|
||||||
|
return stream_ends(child_end, parent_end);
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
private:
|
||||||
|
stream_type stype_;
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stream behavior to redirect streams with a named pipe.
|
||||||
|
*
|
||||||
|
* A child process will be able to communicate with its parent process.
|
||||||
|
*/
|
||||||
|
class named_pipe
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
named_pipe(const std::string &name)
|
||||||
|
: name_(name)
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
, stype_(unknown_stream)
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
named_pipe(const std::string &name, stream_type stype)
|
||||||
|
: name_(name),
|
||||||
|
stype_(stype)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
stream_ends operator()(stream_type stype) const
|
||||||
|
{
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
if (mkfifo(name_.c_str(), S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1)
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("mkfifo(3) failed");
|
||||||
|
handle child_end = open(name_.c_str(), O_RDONLY | O_NONBLOCK);
|
||||||
|
if (!child_end.valid())
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("open(2) failed");
|
||||||
|
int opts = fcntl(child_end.native(), F_GETFL);
|
||||||
|
if (opts == -1)
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("fcntl(2) failed");
|
||||||
|
opts ^= O_NONBLOCK;
|
||||||
|
if (fcntl(child_end.native(), F_SETFL, opts) == -1)
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("fcntl(2) failed");
|
||||||
|
handle parent_end = open(name_.c_str(), O_WRONLY);
|
||||||
|
if (!parent_end.valid())
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("open(2) failed");
|
||||||
|
if (stype_ != unknown_stream)
|
||||||
|
stype = stype_;
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
SECURITY_ATTRIBUTES sa;
|
||||||
|
ZeroMemory(&sa, sizeof(sa));
|
||||||
|
sa.nLength = sizeof(sa);
|
||||||
|
sa.lpSecurityDescriptor = NULL;
|
||||||
|
sa.bInheritHandle = TRUE;
|
||||||
|
handle child_end = CreateNamedPipeA(name_.c_str(), PIPE_ACCESS_INBOUND |
|
||||||
|
FILE_FLAG_OVERLAPPED, 0, 1, 8192, 8192, 0, &sa);
|
||||||
|
if (!child_end.valid())
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("CreateNamedPipe() failed");
|
||||||
|
handle parent_end = CreateFileA(name_.c_str(), GENERIC_WRITE, 0, NULL,
|
||||||
|
OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
|
||||||
|
if (!parent_end.valid())
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("CreateFile() failed");
|
||||||
|
#endif
|
||||||
|
if (stype == output_stream)
|
||||||
|
std::swap(child_end, parent_end);
|
||||||
|
#if defined(BOOST_WINDOWS_API)
|
||||||
|
if (!SetHandleInformation(child_end.native(), HANDLE_FLAG_INHERIT,
|
||||||
|
HANDLE_FLAG_INHERIT))
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR(
|
||||||
|
"SetHandleInformation() failed");
|
||||||
|
#endif
|
||||||
|
return stream_ends(child_end, parent_end);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string name_;
|
||||||
|
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
stream_type stype_;
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stream behavior to redirect streams with a pipe which supports asynchronous
|
||||||
|
* I/O.
|
||||||
|
*
|
||||||
|
* As platforms require different types of pipes for asynchronous I/O this
|
||||||
|
* stream behavior is provided for convenience. It uses the minimum required
|
||||||
|
* pipe type on a platform in order to be able to use asynchronous I/O.
|
||||||
|
*/
|
||||||
|
#if defined(BOOST_POSIX_API) || defined(BOOST_PROCESS_DOXYGEN)
|
||||||
|
typedef pipe async_pipe;
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
class async_pipe
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
stream_ends operator()(stream_type stype) const
|
||||||
|
{
|
||||||
|
UUID uuid;
|
||||||
|
RPC_STATUS s = UuidCreateSequential(&uuid);
|
||||||
|
if (s != RPC_S_OK && s != RPC_S_UUID_LOCAL_ONLY)
|
||||||
|
BOOST_PROCESS_THROW_ERROR(s, "UuidCreateSequential() failed");
|
||||||
|
unsigned char *c;
|
||||||
|
s = UuidToStringA(&uuid, &c);
|
||||||
|
if (s != RPC_S_OK)
|
||||||
|
BOOST_PROCESS_THROW_ERROR(s, "UuidToString() failed");
|
||||||
|
std::string name;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
name = reinterpret_cast<char*>(c);
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
RpcStringFreeA(&c);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
RpcStringFreeA(&c);
|
||||||
|
named_pipe p("\\\\.\\pipe\\boost_process_" + name);
|
||||||
|
return p(stype);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stream behavior to mute streams.
|
||||||
|
*
|
||||||
|
* A child process will be able to use streams. But data written to an
|
||||||
|
* output stream is discarded and data read from an input stream is 0.
|
||||||
|
*/
|
||||||
|
class null
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
null()
|
||||||
|
: stype_(unknown_stream)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
null(stream_type stype)
|
||||||
|
: stype_(stype)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
stream_ends operator()(stream_type stype) const
|
||||||
|
{
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
if (stype_ != unknown_stream)
|
||||||
|
stype = stype_;
|
||||||
|
std::string filename = (stype == input_stream) ? "/dev/zero" :
|
||||||
|
"/dev/null";
|
||||||
|
int flag = (stype == input_stream) ? O_RDONLY : O_WRONLY;
|
||||||
|
handle child_end = open(filename.c_str(), flag);
|
||||||
|
if (!child_end.valid())
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("open(2) failed");
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
DWORD access = (stype == input_stream) ? GENERIC_READ : GENERIC_WRITE;
|
||||||
|
handle child_end = CreateFileA("NUL", access, 0, NULL, OPEN_EXISTING,
|
||||||
|
FILE_ATTRIBUTE_NORMAL, NULL);
|
||||||
|
if (!child_end.valid())
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("CreateFile() failed");
|
||||||
|
if (!SetHandleInformation(child_end.native(), HANDLE_FLAG_INHERIT,
|
||||||
|
HANDLE_FLAG_INHERIT))
|
||||||
|
BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR(
|
||||||
|
"SetHandleInformation() failed");
|
||||||
|
#endif
|
||||||
|
return stream_ends(child_end, handle());
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
private:
|
||||||
|
stream_type stype_;
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
68
include/boost/process/stream_ends.hpp
Normal file
68
include/boost/process/stream_ends.hpp
Normal file
|
|
@ -0,0 +1,68 @@
|
||||||
|
//
|
||||||
|
// Boost.Process
|
||||||
|
// ~~~~~~~~~~~~~
|
||||||
|
//
|
||||||
|
// Copyright (c) 2006, 2007 Julio M. Merino Vidal
|
||||||
|
// Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
|
||||||
|
// Copyright (c) 2009 Boris Schaeling
|
||||||
|
// Copyright (c) 2010 Felipe Tanus, Boris Schaeling
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \file boost/process/stream_ends.hpp
|
||||||
|
*
|
||||||
|
* Includes the declaration of the stream_ends class.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BOOST_PROCESS_STREAM_ENDS_HPP
|
||||||
|
#define BOOST_PROCESS_STREAM_ENDS_HPP
|
||||||
|
|
||||||
|
#include <boost/process/config.hpp>
|
||||||
|
#include <boost/process/handle.hpp>
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
namespace process {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A pair of handles to configure streams.
|
||||||
|
*
|
||||||
|
* Stream behaviors return a pair of handles to specify how a child's stream
|
||||||
|
* should be configured and possibly the opposite end of a child's end. This
|
||||||
|
* is the end remaining in the parent process and which can be used for example
|
||||||
|
* to communicate with a child process through its standard streams.
|
||||||
|
*/
|
||||||
|
struct stream_ends {
|
||||||
|
/**
|
||||||
|
* The child's end.
|
||||||
|
*/
|
||||||
|
handle child;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The parent's end.
|
||||||
|
*/
|
||||||
|
handle parent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Standard constructor creating two invalid handles.
|
||||||
|
*/
|
||||||
|
stream_ends()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper constructor to easily initialize handles.
|
||||||
|
*/
|
||||||
|
stream_ends(handle c, handle p)
|
||||||
|
: child(c),
|
||||||
|
parent(p)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
50
include/boost/process/stream_id.hpp
Normal file
50
include/boost/process/stream_id.hpp
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
//
|
||||||
|
// Boost.Process
|
||||||
|
// ~~~~~~~~~~~~~
|
||||||
|
//
|
||||||
|
// Copyright (c) 2006, 2007 Julio M. Merino Vidal
|
||||||
|
// Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
|
||||||
|
// Copyright (c) 2009 Boris Schaeling
|
||||||
|
// Copyright (c) 2010 Felipe Tanus, Boris Schaeling
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \file boost/process/stream_id.hpp
|
||||||
|
*
|
||||||
|
* Includes the declaration of the stream_id type.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BOOST_PROCESS_STREAM_ID_HPP
|
||||||
|
#define BOOST_PROCESS_STREAM_ID_HPP
|
||||||
|
|
||||||
|
#include <boost/process/config.hpp>
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
namespace process {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Standard stream id to refer to standard streams in a cross-platform manner.
|
||||||
|
*/
|
||||||
|
enum std_stream_id { stdin_id, stdout_id, stderr_id };
|
||||||
|
|
||||||
|
#if defined(BOOST_PROCESS_DOXYGEN)
|
||||||
|
/**
|
||||||
|
* Stream id type.
|
||||||
|
*
|
||||||
|
* Depending on the platform the stream id type is defined to refer to standard
|
||||||
|
* streams only or to support more streams.
|
||||||
|
*/
|
||||||
|
typedef NativeStreamId stream_id;
|
||||||
|
#elif defined(BOOST_POSIX_API)
|
||||||
|
typedef int stream_id;
|
||||||
|
#elif defined(BOOST_WINDOWS_API)
|
||||||
|
typedef std_stream_id stream_id;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
45
include/boost/process/stream_type.hpp
Normal file
45
include/boost/process/stream_type.hpp
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
//
|
||||||
|
// Boost.Process
|
||||||
|
// ~~~~~~~~~~~~~
|
||||||
|
//
|
||||||
|
// Copyright (c) 2006, 2007 Julio M. Merino Vidal
|
||||||
|
// Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
|
||||||
|
// Copyright (c) 2009 Boris Schaeling
|
||||||
|
// Copyright (c) 2010 Felipe Tanus, Boris Schaeling
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \file boost/process/stream_type.hpp
|
||||||
|
*
|
||||||
|
* Includes the declaration of the stream_type enumeration.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BOOST_PROCESS_STREAM_TYPE_HPP
|
||||||
|
#define BOOST_PROCESS_STREAM_TYPE_HPP
|
||||||
|
|
||||||
|
#include <boost/process/config.hpp>
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
namespace process {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stream type to differentiate between input and output streams.
|
||||||
|
*
|
||||||
|
* On POSIX systems another value unknown_stream is defined. It is passed
|
||||||
|
* to stream behaviors for file descriptors greater than 2.
|
||||||
|
*/
|
||||||
|
enum stream_type {
|
||||||
|
input_stream,
|
||||||
|
output_stream
|
||||||
|
#if defined(BOOST_POSIX_API)
|
||||||
|
, unknown_stream
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
@ -6,6 +6,7 @@ namespace boost {
|
||||||
namespace filesystem {
|
namespace filesystem {
|
||||||
class path;
|
class path;
|
||||||
class directory_iterator;
|
class directory_iterator;
|
||||||
|
class recursive_directory_iterator;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -31,6 +32,7 @@ namespace fc {
|
||||||
operator boost::filesystem::path& ();
|
operator boost::filesystem::path& ();
|
||||||
operator const boost::filesystem::path& ()const;
|
operator const boost::filesystem::path& ()const;
|
||||||
|
|
||||||
|
void replace_extension( const fc::path& e );
|
||||||
fc::path stem()const;
|
fc::path stem()const;
|
||||||
fc::path extension()const;
|
fc::path extension()const;
|
||||||
fc::path filename()const;
|
fc::path filename()const;
|
||||||
|
|
@ -56,6 +58,21 @@ namespace fc {
|
||||||
private:
|
private:
|
||||||
fwd<boost::filesystem::directory_iterator,16> _p;
|
fwd<boost::filesystem::directory_iterator,16> _p;
|
||||||
};
|
};
|
||||||
|
class recursive_directory_iterator {
|
||||||
|
public:
|
||||||
|
recursive_directory_iterator( const fc::path& p );
|
||||||
|
recursive_directory_iterator();
|
||||||
|
~recursive_directory_iterator();
|
||||||
|
|
||||||
|
fc::path operator*()const;
|
||||||
|
recursive_directory_iterator& operator++(int);
|
||||||
|
recursive_directory_iterator& operator++();
|
||||||
|
|
||||||
|
friend bool operator==( const recursive_directory_iterator&, const recursive_directory_iterator& );
|
||||||
|
friend bool operator!=( const recursive_directory_iterator&, const recursive_directory_iterator& );
|
||||||
|
private:
|
||||||
|
fwd<boost::filesystem::recursive_directory_iterator,16> _p;
|
||||||
|
};
|
||||||
|
|
||||||
bool exists( const path& p );
|
bool exists( const path& p );
|
||||||
bool is_directory( const path& p );
|
bool is_directory( const path& p );
|
||||||
|
|
@ -66,6 +83,7 @@ namespace fc {
|
||||||
size_t file_size( const path& p );
|
size_t file_size( const path& p );
|
||||||
bool remove( const path& p );
|
bool remove( const path& p );
|
||||||
void copy( const path& from, const path& to );
|
void copy( const path& from, const path& to );
|
||||||
|
void create_hard_link( const path& from, const path& to );
|
||||||
|
|
||||||
path unique_path();
|
path unique_path();
|
||||||
path temp_directory_path();
|
path temp_directory_path();
|
||||||
|
|
|
||||||
|
|
@ -65,6 +65,8 @@ namespace fc {
|
||||||
void reserve( size_t );
|
void reserve( size_t );
|
||||||
size_t size()const;
|
size_t size()const;
|
||||||
size_t find( char c, size_t pos = 0 )const;
|
size_t find( char c, size_t pos = 0 )const;
|
||||||
|
size_t rfind( char c, size_t pos = 0 )const;
|
||||||
|
size_t rfind( const fc::string& c, size_t pos = 0 )const;
|
||||||
|
|
||||||
void resize( size_t s );
|
void resize( size_t s );
|
||||||
void clear();
|
void clear();
|
||||||
|
|
|
||||||
|
|
@ -100,6 +100,9 @@ namespace fc {
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
value& operator=( const T& v );
|
value& operator=( const T& v );
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T cast()const;
|
||||||
/*
|
/*
|
||||||
{
|
{
|
||||||
slog("operator= %p", this);
|
slog("operator= %p", this);
|
||||||
|
|
|
||||||
|
|
@ -312,4 +312,8 @@ namespace fc {
|
||||||
*this = fc::move(tmp);
|
*this = fc::move(tmp);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
template<typename T>
|
||||||
|
T value::cast()const {
|
||||||
|
return value_cast<T>(*this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -68,6 +68,9 @@ namespace fc {
|
||||||
fc::path path::filename()const {
|
fc::path path::filename()const {
|
||||||
return _p->filename();
|
return _p->filename();
|
||||||
}
|
}
|
||||||
|
void path::replace_extension( const fc::path& e ) {
|
||||||
|
_p->replace_extension(e);
|
||||||
|
}
|
||||||
fc::path path::extension()const {
|
fc::path path::extension()const {
|
||||||
return _p->extension();
|
return _p->extension();
|
||||||
}
|
}
|
||||||
|
|
@ -95,12 +98,32 @@ namespace fc {
|
||||||
return *r._p != *l._p;
|
return *r._p != *l._p;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
recursive_directory_iterator::recursive_directory_iterator( const fc::path& p )
|
||||||
|
:_p(p){}
|
||||||
|
|
||||||
|
recursive_directory_iterator::recursive_directory_iterator(){}
|
||||||
|
recursive_directory_iterator::~recursive_directory_iterator(){}
|
||||||
|
|
||||||
|
fc::path recursive_directory_iterator::operator*()const { return boost::filesystem::path(*(*_p)); }
|
||||||
|
recursive_directory_iterator& recursive_directory_iterator::operator++(int) { (*_p)++; return *this; }
|
||||||
|
recursive_directory_iterator& recursive_directory_iterator::operator++() { (*_p)++; return *this; }
|
||||||
|
|
||||||
|
bool operator==( const recursive_directory_iterator& r, const recursive_directory_iterator& l) {
|
||||||
|
return *r._p == *l._p;
|
||||||
|
}
|
||||||
|
bool operator!=( const recursive_directory_iterator& r, const recursive_directory_iterator& l) {
|
||||||
|
return *r._p != *l._p;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
bool exists( const path& p ) { return boost::filesystem::exists(p); }
|
bool exists( const path& p ) { return boost::filesystem::exists(p); }
|
||||||
void create_directories( const path& p ) { boost::filesystem::create_directories(p); }
|
void create_directories( const path& p ) { boost::filesystem::create_directories(p); }
|
||||||
bool is_directory( const path& p ) { return boost::filesystem::is_directory(p); }
|
bool is_directory( const path& p ) { return boost::filesystem::is_directory(p); }
|
||||||
bool is_regular_file( const path& p ) { return boost::filesystem::is_regular_file(p); }
|
bool is_regular_file( const path& p ) { return boost::filesystem::is_regular_file(p); }
|
||||||
size_t file_size( const path& p ) { return boost::filesystem::file_size(p); }
|
size_t file_size( const path& p ) { return boost::filesystem::file_size(p); }
|
||||||
void copy( const path& f, const path& t ) { boost::filesystem::copy( boost::filesystem::path(f), boost::filesystem::path(t) ); }
|
void copy( const path& f, const path& t ) { boost::filesystem::copy( boost::filesystem::path(f), boost::filesystem::path(t) ); }
|
||||||
|
void create_hard_link( const path& f, const path& t ) { boost::filesystem::create_hard_link( f, t ); }
|
||||||
bool remove( const path& f ) { return boost::filesystem::remove( f ); }
|
bool remove( const path& f ) { return boost::filesystem::remove( f ); }
|
||||||
fc::path canonical( const fc::path& p ) { return boost::filesystem::canonical(p); }
|
fc::path canonical( const fc::path& p ) { return boost::filesystem::canonical(p); }
|
||||||
fc::path absolute( const fc::path& p ) { return boost::filesystem::absolute(p); }
|
fc::path absolute( const fc::path& p ) { return boost::filesystem::absolute(p); }
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,8 @@ namespace fc {
|
||||||
void string::reserve(size_t r) { my->reserve(r); }
|
void string::reserve(size_t r) { my->reserve(r); }
|
||||||
size_t string::size()const { return my->size(); }
|
size_t string::size()const { return my->size(); }
|
||||||
size_t string::find(char c, size_t p)const { return my->find(c,p); }
|
size_t string::find(char c, size_t p)const { return my->find(c,p); }
|
||||||
|
size_t string::rfind(char c, size_t p)const { return my->rfind(c,p); }
|
||||||
|
size_t string::rfind(const fc::string& c, size_t p)const { return my->rfind(c,p); }
|
||||||
void string::clear() { my->clear(); }
|
void string::clear() { my->clear(); }
|
||||||
void string::resize( size_t s ) { my->resize(s); }
|
void string::resize( size_t s ) { my->resize(s); }
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
#include <fc/url.hpp>
|
#include <fc/url.hpp>
|
||||||
#include <fc/Value.hpp>
|
#include <fc/value.hpp>
|
||||||
#include <fc/error_report.hpp>
|
#include <fc/error_report.hpp>
|
||||||
#include <fc/lexical_cast.hpp>
|
#include <fc/lexical_cast.hpp>
|
||||||
#include <fc/value_cast.hpp>
|
#include <fc/value_cast.hpp>
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ FC_STUB( test, (add)(namep_test)(sub)(sub1)(sub2)(sub3)(sub4)(sub5)(sub6)(sub7)(
|
||||||
int main( int argc, char** argv ) {
|
int main( int argc, char** argv ) {
|
||||||
try {
|
try {
|
||||||
slog( "Hello World\n" );
|
slog( "Hello World\n" );
|
||||||
fc::value v = std::string("goodbye");
|
fc::value v = fc::string("goodbye");
|
||||||
slog(".");
|
slog(".");
|
||||||
fc::value v2;
|
fc::value v2;
|
||||||
slog("..");
|
slog("..");
|
||||||
|
|
@ -44,7 +44,7 @@ int main( int argc, char** argv ) {
|
||||||
slog("...");
|
slog("...");
|
||||||
fc::value& b = v2["b"];
|
fc::value& b = v2["b"];
|
||||||
slog( "....");
|
slog( "....");
|
||||||
b = std::string("hello");
|
b = fc::string("hello");
|
||||||
slog(".....");
|
slog(".....");
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue