2 // Automated Testing Framework (atf)
4 // Copyright (c) 2008 The NetBSD Foundation, Inc.
5 // All rights reserved.
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
10 // 1. Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // 2. Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
16 // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #if !defined(TOOLS_PROCESS_HPP)
31 #define TOOLS_PROCESS_HPP
34 #include <sys/types.h>
45 #include "auto_array.hpp"
46 #include "exceptions.hpp"
55 // ------------------------------------------------------------------------
56 // The "argv_array" type.
57 // ------------------------------------------------------------------------
60 typedef std::vector
< std::string
> args_vector
;
63 // TODO: This is immutable, so we should be able to use
64 // std::tr1::shared_array instead when it becomes widely available.
65 // The reason would be to remove all copy constructors and assignment
66 // operators from this class.
67 auto_array
< const char* > m_exec_argv
;
68 void ctor_init_exec_argv(void);
71 typedef args_vector::const_iterator const_iterator
;
72 typedef args_vector::size_type size_type
;
75 argv_array(const char*, ...);
76 explicit argv_array(const char* const*);
77 template< class C
> explicit argv_array(const C
&);
78 argv_array(const argv_array
&);
80 const char* const* exec_argv(void) const;
81 size_type
size(void) const;
82 const char* operator[](int) const;
84 const_iterator
begin(void) const;
85 const_iterator
end(void) const;
87 argv_array
& operator=(const argv_array
&);
91 argv_array::argv_array(const C
& c
)
93 for (typename
C::const_iterator iter
= c
.begin(); iter
!= c
.end();
95 m_args
.push_back(*iter
);
96 ctor_init_exec_argv();
99 // ------------------------------------------------------------------------
100 // The "stream" types.
101 // ------------------------------------------------------------------------
103 class stream_capture
{
106 // Allow access to the getters.
107 template< class OutStream
, class ErrStream
> friend
108 child
fork(void (*)(void*), OutStream
, ErrStream
, void*);
109 template< class OutStream
, class ErrStream
> friend
110 status
exec(const tools::fs::path
&, const argv_array
&,
111 const OutStream
&, const ErrStream
&, void (*)(void));
114 int connect_parent(void);
115 void connect_child(const int);
118 stream_capture(void);
119 ~stream_capture(void);
122 class stream_connect
{
126 // Allow access to the getters.
127 template< class OutStream
, class ErrStream
> friend
128 child
fork(void (*)(void*), OutStream
, ErrStream
, void*);
129 template< class OutStream
, class ErrStream
> friend
130 status
exec(const tools::fs::path
&, const argv_array
&,
131 const OutStream
&, const ErrStream
&, void (*)(void));
134 int connect_parent(void);
135 void connect_child(const int);
138 stream_connect(const int, const int);
141 class stream_inherit
{
142 // Allow access to the getters.
143 template< class OutStream
, class ErrStream
> friend
144 child
fork(void (*)(void*), OutStream
, ErrStream
, void*);
145 template< class OutStream
, class ErrStream
> friend
146 status
exec(const tools::fs::path
&, const argv_array
&,
147 const OutStream
&, const ErrStream
&, void (*)(void));
150 int connect_parent(void);
151 void connect_child(const int);
154 stream_inherit(void);
157 class stream_redirect_fd
{
160 // Allow access to the getters.
161 template< class OutStream
, class ErrStream
> friend
162 child
fork(void (*)(void*), OutStream
, ErrStream
, void*);
163 template< class OutStream
, class ErrStream
> friend
164 status
exec(const tools::fs::path
&, const argv_array
&,
165 const OutStream
&, const ErrStream
&, void (*)(void));
168 int connect_parent(void);
169 void connect_child(const int);
172 stream_redirect_fd(const int);
175 class stream_redirect_path
{
176 const tools::fs::path m_path
;
178 // Allow access to the getters.
179 template< class OutStream
, class ErrStream
> friend
180 child
fork(void (*)(void*), OutStream
, ErrStream
, void*);
181 template< class OutStream
, class ErrStream
> friend
182 status
exec(const tools::fs::path
&, const argv_array
&,
183 const OutStream
&, const ErrStream
&, void (*)(void));
186 int connect_parent(void);
187 void connect_child(const int);
190 stream_redirect_path(const tools::fs::path
&);
193 // ------------------------------------------------------------------------
194 // The "status" type.
195 // ------------------------------------------------------------------------
201 template< class OutStream
, class ErrStream
> friend
202 status
exec(const tools::fs::path
&, const argv_array
&,
203 const OutStream
&, const ErrStream
&, void (*)(void));
210 bool exited(void) const;
211 int exitstatus(void) const;
213 bool signaled(void) const;
214 int termsig(void) const;
215 bool coredump(void) const;
218 // ------------------------------------------------------------------------
220 // ------------------------------------------------------------------------
230 template< class OutStream
, class ErrStream
> friend
231 child
fork(void (*)(void*), OutStream
, ErrStream
, void*);
233 child(const pid_t
, const int, const int);
240 pid_t
pid(void) const;
245 // ------------------------------------------------------------------------
247 // ------------------------------------------------------------------------
250 void flush_streams(void);
253 const tools::fs::path m_prog
;
254 const argv_array
& m_argv
;
255 void (*m_prehook
)(void);
258 void do_exec(void *);
259 } // namespace detail
261 // TODO: The void* cookie can probably be templatized, thus also allowing
262 // const data structures.
263 template< class OutStream
, class ErrStream
>
265 fork(void (*start
)(void*), OutStream outsb
, ErrStream errsb
, void* v
)
267 detail::flush_streams();
272 pid_t pid
= ::fork();
274 throw system_error("tools::process::child::fork",
275 "Failed to fork", errno
);
276 } else if (pid
== 0) {
278 outsb
.connect_child(STDOUT_FILENO
);
279 errsb
.connect_child(STDERR_FILENO
);
283 std::cerr
<< "Unhandled error while running subprocess\n";
284 std::exit(EXIT_FAILURE
);
287 const int stdout_fd
= outsb
.connect_parent();
288 const int stderr_fd
= errsb
.connect_parent();
289 return child(pid
, stdout_fd
, stderr_fd
);
293 template< class OutStream
, class ErrStream
>
295 exec(const tools::fs::path
& prog
, const argv_array
& argv
,
296 const OutStream
& outsb
, const ErrStream
& errsb
,
297 void (*prehook
)(void))
299 struct detail::exec_args ea
= { prog
, argv
, prehook
};
300 child c
= fork(detail::do_exec
, outsb
, errsb
, &ea
);
305 } catch (const system_error
& e
) {
306 if (e
.code() == EINTR
)
313 template< class OutStream
, class ErrStream
>
315 exec(const tools::fs::path
& prog
, const argv_array
& argv
,
316 const OutStream
& outsb
, const ErrStream
& errsb
)
318 return exec(prog
, argv
, outsb
, errsb
, NULL
);
321 } // namespace process
324 #endif // !defined(TOOLS_PROCESS_HPP)