ian@0
|
1 //
|
ian@0
|
2 // Boost.Process
|
ian@0
|
3 // ~~~~~~~~~~~~~
|
ian@0
|
4 //
|
ian@0
|
5 // Copyright (c) 2006, 2007 Julio M. Merino Vidal
|
ian@0
|
6 // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
|
ian@0
|
7 // Copyright (c) 2009 Boris Schaeling
|
ian@0
|
8 // Copyright (c) 2010 Felipe Tanus, Boris Schaeling
|
ian@0
|
9 //
|
ian@0
|
10 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
ian@0
|
11 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
ian@0
|
12 //
|
ian@0
|
13
|
ian@0
|
14 /**
|
ian@0
|
15 * \file boost/process/context.hpp
|
ian@0
|
16 *
|
ian@0
|
17 * Includes the declaration of the context class.
|
ian@0
|
18 */
|
ian@0
|
19
|
ian@0
|
20 #ifndef BOOST_PROCESS_CONTEXT_HPP
|
ian@0
|
21 #define BOOST_PROCESS_CONTEXT_HPP
|
ian@0
|
22
|
ian@0
|
23 #include <boost/process/config.hpp>
|
ian@0
|
24
|
ian@0
|
25 #if defined(BOOST_POSIX_API)
|
ian@0
|
26 # include <unistd.h>
|
ian@0
|
27 #elif defined(BOOST_WINDOWS_API)
|
ian@0
|
28 # include <windows.h>
|
ian@0
|
29 #endif
|
ian@0
|
30
|
ian@0
|
31 #include <boost/process/stream_id.hpp>
|
ian@0
|
32 #include <boost/process/stream_ends.hpp>
|
ian@0
|
33 #include <boost/process/stream_type.hpp>
|
ian@0
|
34 #include <boost/process/environment.hpp>
|
ian@0
|
35 #include <boost/process/self.hpp>
|
ian@0
|
36 #include <boost/process/stream_behavior.hpp>
|
ian@0
|
37 #include <boost/function.hpp>
|
ian@0
|
38 #include <string>
|
ian@0
|
39 #include <map>
|
ian@0
|
40
|
ian@0
|
41 namespace boost {
|
ian@0
|
42 namespace process {
|
ian@0
|
43
|
ian@0
|
44 /**
|
ian@0
|
45 * Context class to define how a child process is created.
|
ian@0
|
46 *
|
ian@0
|
47 * The context class is used to configure streams, to set the work directory
|
ian@0
|
48 * and define environment variables. It is also used to change a process
|
ian@0
|
49 * name (the variable commonly known as argv[0]).
|
ian@0
|
50 */
|
ian@0
|
51 struct context
|
ian@0
|
52 {
|
ian@0
|
53 typedef std::map<stream_id, boost::function<stream_ends (stream_type)> >
|
ian@0
|
54 streams_t;
|
ian@0
|
55
|
ian@0
|
56 /**
|
ian@0
|
57 * Streams.
|
ian@0
|
58 *
|
ian@0
|
59 * Streams of a child process can be configured through factory functions
|
ian@0
|
60 * which return a pair of handles - one handle to use as a stream end
|
ian@0
|
61 * in the child process and possibly another handle to use as a stream end
|
ian@0
|
62 * in the parent process (if a pipe is setup both processes can communicate
|
ian@0
|
63 * with each other).
|
ian@0
|
64 */
|
ian@0
|
65 streams_t streams;
|
ian@0
|
66
|
ian@0
|
67 /**
|
ian@0
|
68 * Process name.
|
ian@0
|
69 *
|
ian@0
|
70 * The child process can access the process name via a variable
|
ian@0
|
71 * commonly known as argv[0].
|
ian@0
|
72 */
|
ian@0
|
73 std::string process_name;
|
ian@0
|
74
|
ian@0
|
75 /**
|
ian@0
|
76 * Work directory.
|
ian@0
|
77 */
|
ian@0
|
78 std::string work_dir;
|
ian@0
|
79
|
ian@0
|
80 /**
|
ian@0
|
81 * Environment variables.
|
ian@0
|
82 */
|
ian@0
|
83 environment env;
|
ian@0
|
84
|
ian@0
|
85 /**
|
ian@0
|
86 * Constructs a process context.
|
ian@0
|
87 *
|
ian@0
|
88 * The default behavior of standard streams is to inherit them. The current
|
ian@0
|
89 * work directory is also the work directory of the child process. The child
|
ian@0
|
90 * process also inherits all environment variables.
|
ian@0
|
91 */
|
ian@0
|
92 context()
|
ian@0
|
93 : work_dir(self::get_work_dir()),
|
ian@0
|
94 env(self::get_environment())
|
ian@0
|
95 {
|
ian@0
|
96 #if defined(BOOST_POSIX_API)
|
ian@0
|
97 streams[stdin_id] = behavior::inherit(STDIN_FILENO);
|
ian@0
|
98 streams[stdout_id] = behavior::inherit(STDOUT_FILENO);
|
ian@0
|
99 streams[stderr_id] = behavior::inherit(STDERR_FILENO);
|
ian@0
|
100 #elif defined(BOOST_WINDOWS_API)
|
ian@0
|
101 streams[stdin_id] = behavior::inherit(GetStdHandle(STD_INPUT_HANDLE));
|
ian@0
|
102 streams[stdout_id] = behavior::inherit(GetStdHandle(STD_OUTPUT_HANDLE));
|
ian@0
|
103 streams[stderr_id] = behavior::inherit(GetStdHandle(STD_ERROR_HANDLE));
|
ian@0
|
104 #endif
|
ian@0
|
105 }
|
ian@0
|
106
|
ian@0
|
107 #if defined(BOOST_POSIX_API) || defined(BOOST_PROCESS_DOXYGEN)
|
ian@0
|
108 /**
|
ian@0
|
109 * Setups a child process.
|
ian@0
|
110 *
|
ian@0
|
111 * This is an extension point to support more configuration options for
|
ian@0
|
112 * child processes. You can initialize \a setup with a user-defined function
|
ian@0
|
113 * which is called when a child process is created.
|
ian@0
|
114 *
|
ian@0
|
115 * On POSIX platforms setup() is called in the child process. That's why in
|
ian@0
|
116 * a multithreaded application only async-signal-safe functions must be
|
ian@0
|
117 * called in the function \a setup is bound to.
|
ian@0
|
118 *
|
ian@0
|
119 * On Windows platforms setup() is called in the parent process. A
|
ian@0
|
120 * reference to a STARTUPINFOA structure is passed as parameter.
|
ian@0
|
121 */
|
ian@0
|
122 boost::function<void ()> setup;
|
ian@0
|
123 #elif defined(BOOST_WINDOWS_API)
|
ian@0
|
124 boost::function<void (STARTUPINFOA&)> setup;
|
ian@0
|
125 #endif
|
ian@0
|
126 };
|
ian@0
|
127
|
ian@0
|
128 }
|
ian@0
|
129 }
|
ian@0
|
130
|
ian@0
|
131 #endif
|