Remove building with NOCRYPTO option
[minix3.git] / external / bsd / atf / dist / tools / process.hpp
blob2ebd865a8d3f7df311a4aa78a88c421eaa517fb0
1 //
2 // Automated Testing Framework (atf)
3 //
4 // Copyright (c) 2008 The NetBSD Foundation, Inc.
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
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
33 extern "C" {
34 #include <sys/types.h>
36 #include <unistd.h>
39 #include <cerrno>
40 #include <cstdlib>
41 #include <iostream>
42 #include <string>
43 #include <vector>
45 #include "auto_array.hpp"
46 #include "exceptions.hpp"
47 #include "fs.hpp"
49 namespace tools {
50 namespace process {
52 class child;
53 class status;
55 // ------------------------------------------------------------------------
56 // The "argv_array" type.
57 // ------------------------------------------------------------------------
59 class argv_array {
60 typedef std::vector< std::string > args_vector;
61 args_vector m_args;
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);
70 public:
71 typedef args_vector::const_iterator const_iterator;
72 typedef args_vector::size_type size_type;
74 argv_array(void);
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&);
90 template< class C >
91 argv_array::argv_array(const C& c)
93 for (typename C::const_iterator iter = c.begin(); iter != c.end();
94 iter++)
95 m_args.push_back(*iter);
96 ctor_init_exec_argv();
99 // ------------------------------------------------------------------------
100 // The "stream" types.
101 // ------------------------------------------------------------------------
103 class stream_capture {
104 int m_pipefds[2];
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));
113 void prepare(void);
114 int connect_parent(void);
115 void connect_child(const int);
117 public:
118 stream_capture(void);
119 ~stream_capture(void);
122 class stream_connect {
123 int m_src_fd;
124 int m_tgt_fd;
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));
133 void prepare(void);
134 int connect_parent(void);
135 void connect_child(const int);
137 public:
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));
149 void prepare(void);
150 int connect_parent(void);
151 void connect_child(const int);
153 public:
154 stream_inherit(void);
157 class stream_redirect_fd {
158 int m_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));
167 void prepare(void);
168 int connect_parent(void);
169 void connect_child(const int);
171 public:
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));
185 void prepare(void);
186 int connect_parent(void);
187 void connect_child(const int);
189 public:
190 stream_redirect_path(const tools::fs::path&);
193 // ------------------------------------------------------------------------
194 // The "status" type.
195 // ------------------------------------------------------------------------
197 class status {
198 int m_status;
200 friend class child;
201 template< class OutStream, class ErrStream > friend
202 status exec(const tools::fs::path&, const argv_array&,
203 const OutStream&, const ErrStream&, void (*)(void));
205 status(int);
207 public:
208 ~status(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 // ------------------------------------------------------------------------
219 // The "child" type.
220 // ------------------------------------------------------------------------
222 class child {
223 pid_t m_pid;
225 int m_stdout;
226 int m_stderr;
228 bool m_waited;
230 template< class OutStream, class ErrStream > friend
231 child fork(void (*)(void*), OutStream, ErrStream, void*);
233 child(const pid_t, const int, const int);
235 public:
236 ~child(void);
238 status wait(void);
240 pid_t pid(void) const;
241 int stdout_fd(void);
242 int stderr_fd(void);
245 // ------------------------------------------------------------------------
246 // Free functions.
247 // ------------------------------------------------------------------------
249 namespace detail {
250 void flush_streams(void);
252 struct exec_args {
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 >
264 child
265 fork(void (*start)(void*), OutStream outsb, ErrStream errsb, void* v)
267 detail::flush_streams();
269 outsb.prepare();
270 errsb.prepare();
272 pid_t pid = ::fork();
273 if (pid == -1) {
274 throw system_error("tools::process::child::fork",
275 "Failed to fork", errno);
276 } else if (pid == 0) {
277 try {
278 outsb.connect_child(STDOUT_FILENO);
279 errsb.connect_child(STDERR_FILENO);
280 start(v);
281 std::abort();
282 } catch (...) {
283 std::cerr << "Unhandled error while running subprocess\n";
284 std::exit(EXIT_FAILURE);
286 } else {
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 >
294 status
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);
302 again:
303 try {
304 return c.wait();
305 } catch (const system_error& e) {
306 if (e.code() == EINTR)
307 goto again;
308 else
309 throw e;
313 template< class OutStream, class ErrStream >
314 status
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
322 } // namespace tools
324 #endif // !defined(TOOLS_PROCESS_HPP)