1 // Copyright 2010 Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "utils/process/child.ipp"
47 #include "utils/defs.hpp"
48 #include "utils/format/macros.hpp"
49 #include "utils/logging/macros.hpp"
50 #include "utils/process/exceptions.hpp"
51 #include "utils/process/fdstream.hpp"
52 #include "utils/process/system.hpp"
53 #include "utils/process/status.hpp"
54 #include "utils/sanity.hpp"
55 #include "utils/signals/interrupts.hpp"
62 /// Private implementation fields for child objects.
64 /// The process identifier.
67 /// The input stream for the process' stdout and stderr. May be NULL.
68 std::auto_ptr
< process::ifdstream
> _output
;
70 /// Initializes private implementation data.
72 /// \param pid The process identifier.
73 /// \param output The input stream. Grabs ownership of the pointer.
74 impl(const pid_t pid
, process::ifdstream
* output
) :
75 _pid(pid
), _output(output
) {}
79 } // namespace process
83 namespace fs
= utils::fs
;
84 namespace process
= utils::process
;
85 namespace signals
= utils::signals
;
91 /// Exception-based version of dup(2).
93 /// \param old_fd The file descriptor to duplicate.
94 /// \param new_fd The file descriptor to use as the duplicate. This is
95 /// closed if it was open before the copy happens.
97 /// \throw process::system_error If the call to dup2(2) fails.
99 safe_dup(const int old_fd
, const int new_fd
)
101 if (process::detail::syscall_dup2(old_fd
, new_fd
) == -1) {
102 const int original_errno
= errno
;
103 throw process::system_error(F("dup2(%s, %s) failed") % old_fd
% new_fd
,
109 /// Exception-based version of open(2) to open (or create) a file for append.
111 /// \param filename The file to open in append mode.
113 /// \return The file descriptor for the opened or created file.
115 /// \throw process::system_error If the call to open(2) fails.
117 open_for_append(const fs::path
& filename
)
119 const int fd
= process::detail::syscall_open(
120 filename
.c_str(), O_CREAT
| O_WRONLY
| O_APPEND
,
121 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
);
123 const int original_errno
= errno
;
124 throw process::system_error(F("Failed to create %s because open(2) "
125 "failed") % filename
, original_errno
);
131 /// Exception-based, type-improved version of wait(2).
133 /// Because we are waiting for the termination of a process, and because this is
134 /// the canonical way to call wait(2) for this module, we ensure from here that
135 /// any subprocess of the process we are killing is terminated.
137 /// \param pid The identifier of the process to wait for.
139 /// \return The termination status of the process.
141 /// \throw process::system_error If the call to waitpid(2) fails.
142 static process::status
143 safe_wait(const pid_t pid
)
145 LD(F("Waiting for pid=%s") % pid
);
147 if (process::detail::syscall_waitpid(pid
, &stat_loc
, 0) == -1) {
148 const int original_errno
= errno
;
149 throw process::system_error(F("Failed to wait for PID %s") % pid
,
152 return process::status(pid
, stat_loc
);
156 /// Logs the execution of another program.
158 /// \param program The binary to execute.
159 /// \param args The arguments to pass to the binary, without the program name.
161 log_exec(const fs::path
& program
, const process::args_vector
& args
)
163 std::string plain_command
= program
.str();
164 for (process::args_vector::const_iterator iter
= args
.begin();
165 iter
!= args
.end(); ++iter
)
166 plain_command
+= F(" %s") % *iter
;
167 LD(F("Executing %s") % plain_command
);
171 /// Maximum number of arguments supported by cxx_exec.
173 /// We need this limit to avoid having to allocate dynamic memory in the child
174 /// process to construct the arguments list, which would have side-effects in
175 /// the parent's memory if we use vfork().
179 static void cxx_exec(const fs::path
& program
, const process::args_vector
& args
)
180 throw() UTILS_NORETURN
;
183 /// Executes an external binary and replaces the current process.
185 /// This function must not use any of the logging features, so that the output
186 /// of the subprocess is not "polluted" by our own messages.
188 /// This function must also not affect the global state of the current process
189 /// as otherwise we would not be able to use vfork(). Only state stored in the
190 /// stack can be touched.
192 /// \param program The binary to execute.
193 /// \param args The arguments to pass to the binary, without the program name.
195 cxx_exec(const fs::path
& program
, const process::args_vector
& args
) throw()
197 assert(args
.size() < MAX_ARGS
);
199 const char* argv
[MAX_ARGS
+ 1];
201 argv
[0] = program
.c_str();
202 for (process::args_vector::size_type i
= 0; i
< args
.size(); i
++)
203 argv
[1 + i
] = args
[i
].c_str();
204 argv
[1 + args
.size()] = NULL
;
206 #if defined(__minix) && !defined(NDEBUG)
208 #endif /* defined(__minix) && !defined(NDEBUG) */
209 ::execv(program
.c_str(),
210 (char* const*)(unsigned long)(const void*)argv
);
211 const int original_errno
= errno
;
214 std::cerr
<< "Failed to execute " << program
<< ": "
215 << std::strerror(original_errno
) << "\n";
217 } catch (const std::runtime_error
& error
) {
218 std::cerr
<< "Failed to execute " << program
<< ": "
219 << error
.what() << "\n";
222 std::cerr
<< "Failed to execute " << program
<< "; got unexpected "
223 "exception during exec\n";
229 } // anonymous namespace
232 /// Creates a new child.
234 /// \param implptr A dynamically-allocated impl object with the contents of the
236 process::child::child(impl
*implptr
) :
242 /// Destructor for child.
243 process::child::~child(void)
248 /// Helper function for fork().
250 /// Please note: if you update this function to change the return type or to
251 /// raise different errors, do not forget to update fork() accordingly.
253 /// \return In the case of the parent, a new child object returned as a
254 /// dynamically-allocated object because children classes are unique and thus
255 /// noncopyable. In the case of the child, a NULL pointer.
257 /// \throw process::system_error If the calls to pipe(2) or fork(2) fail.
258 std::auto_ptr
< process::child
>
259 process::child::fork_capture_aux(void)
265 if (detail::syscall_pipe(fds
) == -1)
266 throw process::system_error("pipe(2) failed", errno
);
268 std::auto_ptr
< signals::interrupts_inhibiter
> inhibiter(
269 new signals::interrupts_inhibiter
);
270 pid_t pid
= detail::syscall_fork();
272 inhibiter
.reset(NULL
); // Unblock signals.
275 throw process::system_error("fork(2) failed", errno
);
276 } else if (pid
== 0) {
277 inhibiter
.reset(NULL
); // Unblock signals.
278 ::setpgid(::getpid(), ::getpid());
282 safe_dup(fds
[1], STDOUT_FILENO
);
283 safe_dup(fds
[1], STDERR_FILENO
);
285 } catch (const system_error
& e
) {
286 std::cerr
<< F("Failed to set up subprocess: %s\n") % e
.what();
289 return std::auto_ptr
< process::child
>(NULL
);
292 LD(F("Spawned process %s: stdout and stderr inherited") % pid
);
293 signals::add_pid_to_kill(pid
);
294 inhibiter
.reset(NULL
); // Unblock signals.
295 return std::auto_ptr
< process::child
>(
296 new process::child(new impl(pid
, new process::ifdstream(fds
[0]))));
301 /// Helper function for fork().
303 /// Please note: if you update this function to change the return type or to
304 /// raise different errors, do not forget to update fork() accordingly.
306 /// \param stdout_file The name of the file in which to store the stdout.
307 /// If this has the magic value /dev/stdout, then the parent's stdout is
308 /// reused without applying any redirection.
309 /// \param stderr_file The name of the file in which to store the stderr.
310 /// If this has the magic value /dev/stderr, then the parent's stderr is
311 /// reused without applying any redirection.
313 /// \return In the case of the parent, a new child object returned as a
314 /// dynamically-allocated object because children classes are unique and thus
315 /// noncopyable. In the case of the child, a NULL pointer.
317 /// \throw process::system_error If the call to fork(2) fails.
318 std::auto_ptr
< process::child
>
319 process::child::fork_files_aux(const fs::path
& stdout_file
,
320 const fs::path
& stderr_file
)
325 std::auto_ptr
< signals::interrupts_inhibiter
> inhibiter(
326 new signals::interrupts_inhibiter
);
327 pid_t pid
= detail::syscall_fork();
329 inhibiter
.reset(NULL
); // Unblock signals.
330 throw process::system_error("fork(2) failed", errno
);
331 } else if (pid
== 0) {
332 inhibiter
.reset(NULL
); // Unblock signals.
333 ::setpgid(::getpid(), ::getpid());
336 if (stdout_file
!= fs::path("/dev/stdout")) {
337 const int stdout_fd
= open_for_append(stdout_file
);
338 safe_dup(stdout_fd
, STDOUT_FILENO
);
341 if (stderr_file
!= fs::path("/dev/stderr")) {
342 const int stderr_fd
= open_for_append(stderr_file
);
343 safe_dup(stderr_fd
, STDERR_FILENO
);
346 } catch (const system_error
& e
) {
347 std::cerr
<< F("Failed to set up subprocess: %s\n") % e
.what();
350 return std::auto_ptr
< process::child
>(NULL
);
352 LD(F("Spawned process %s: stdout=%s, stderr=%s") % pid
% stdout_file
%
354 signals::add_pid_to_kill(pid
);
355 inhibiter
.reset(NULL
); // Unblock signals.
356 return std::auto_ptr
< process::child
>(
357 new process::child(new impl(pid
, NULL
)));
362 /// Spawns a new binary and multiplexes and captures its stdout and stderr.
364 /// If the subprocess cannot be completely set up for any reason, it attempts to
365 /// dump an error message to its stderr channel and it then calls std::abort().
367 /// \param program The binary to execute.
368 /// \param args The arguments to pass to the binary, without the program name.
370 /// \return A new child object, returned as a dynamically-allocated object
371 /// because children classes are unique and thus noncopyable.
373 /// \throw process::system_error If the process cannot be spawned due to a
374 /// system call error.
375 std::auto_ptr
< process::child
>
376 process::child::spawn_capture(const fs::path
& program
, const args_vector
& args
)
378 std::auto_ptr
< child
> child
= fork_capture_aux();
379 if (child
.get() == NULL
)
380 cxx_exec(program
, args
);
381 log_exec(program
, args
);
386 /// Spawns a new binary and redirects its stdout and stderr to files.
388 /// If the subprocess cannot be completely set up for any reason, it attempts to
389 /// dump an error message to its stderr channel and it then calls std::abort().
391 /// \param program The binary to execute.
392 /// \param args The arguments to pass to the binary, without the program name.
393 /// \param stdout_file The name of the file in which to store the stdout.
394 /// \param stderr_file The name of the file in which to store the stderr.
396 /// \return A new child object, returned as a dynamically-allocated object
397 /// because children classes are unique and thus noncopyable.
399 /// \throw process::system_error If the process cannot be spawned due to a
400 /// system call error.
401 std::auto_ptr
< process::child
>
402 process::child::spawn_files(const fs::path
& program
,
403 const args_vector
& args
,
404 const fs::path
& stdout_file
,
405 const fs::path
& stderr_file
)
407 std::auto_ptr
< child
> child
= fork_files_aux(stdout_file
, stderr_file
);
408 if (child
.get() == NULL
)
409 cxx_exec(program
, args
);
410 log_exec(program
, args
);
415 /// Returns the process identifier of this child.
417 /// \return A process identifier.
419 process::child::pid(void) const
425 /// Gets the input stream corresponding to the stdout and stderr of the child.
427 /// \pre The child must have been started by fork_capture().
429 /// \return A reference to the input stream connected to the output of the test
432 process::child::output(void)
434 PRE(_pimpl
->_output
.get() != NULL
);
435 return *_pimpl
->_output
;
439 /// Blocks to wait for completion.
441 /// \return The termination status of the child process.
443 /// \throw process::system_error If the call to waitpid(2) fails.
445 process::child::wait(void)
447 const process::status status
= safe_wait(_pimpl
->_pid
);
449 signals::interrupts_inhibiter inhibiter
;
450 signals::remove_pid_to_kill(_pimpl
->_pid
);