111be35a1SLionel Sambuc //
211be35a1SLionel Sambuc // Automated Testing Framework (atf)
311be35a1SLionel Sambuc //
411be35a1SLionel Sambuc // Copyright (c) 2008 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc // All rights reserved.
611be35a1SLionel Sambuc //
711be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
811be35a1SLionel Sambuc // modification, are permitted provided that the following conditions
911be35a1SLionel Sambuc // are met:
1011be35a1SLionel Sambuc // 1. Redistributions of source code must retain the above copyright
1111be35a1SLionel Sambuc // notice, this list of conditions and the following disclaimer.
1211be35a1SLionel Sambuc // 2. Redistributions in binary form must reproduce the above copyright
1311be35a1SLionel Sambuc // notice, this list of conditions and the following disclaimer in the
1411be35a1SLionel Sambuc // documentation and/or other materials provided with the distribution.
1511be35a1SLionel Sambuc //
1611be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
1711be35a1SLionel Sambuc // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
1811be35a1SLionel Sambuc // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1911be35a1SLionel Sambuc // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2011be35a1SLionel Sambuc // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
2111be35a1SLionel Sambuc // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2211be35a1SLionel Sambuc // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2311be35a1SLionel Sambuc // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2411be35a1SLionel Sambuc // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
2511be35a1SLionel Sambuc // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2611be35a1SLionel Sambuc // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
2711be35a1SLionel Sambuc // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2811be35a1SLionel Sambuc //
2911be35a1SLionel Sambuc
3011be35a1SLionel Sambuc extern "C" {
3111be35a1SLionel Sambuc #include <signal.h>
3211be35a1SLionel Sambuc
3311be35a1SLionel Sambuc #include "../../atf-c/error.h"
3411be35a1SLionel Sambuc
3511be35a1SLionel Sambuc #include "../../atf-c/detail/process.h"
3611be35a1SLionel Sambuc }
3711be35a1SLionel Sambuc
3811be35a1SLionel Sambuc #include <iostream>
3911be35a1SLionel Sambuc
4011be35a1SLionel Sambuc #include "exceptions.hpp"
4111be35a1SLionel Sambuc #include "process.hpp"
4211be35a1SLionel Sambuc #include "sanity.hpp"
4311be35a1SLionel Sambuc
4411be35a1SLionel Sambuc namespace detail = atf::process::detail;
4511be35a1SLionel Sambuc namespace impl = atf::process;
4611be35a1SLionel Sambuc #define IMPL_NAME "atf::process"
4711be35a1SLionel Sambuc
4811be35a1SLionel Sambuc // ------------------------------------------------------------------------
4911be35a1SLionel Sambuc // Auxiliary functions.
5011be35a1SLionel Sambuc // ------------------------------------------------------------------------
5111be35a1SLionel Sambuc
5211be35a1SLionel Sambuc template< class C >
5311be35a1SLionel Sambuc atf::auto_array< const char* >
collection_to_argv(const C & c)5411be35a1SLionel Sambuc collection_to_argv(const C& c)
5511be35a1SLionel Sambuc {
5611be35a1SLionel Sambuc atf::auto_array< const char* > argv(new const char*[c.size() + 1]);
5711be35a1SLionel Sambuc
5811be35a1SLionel Sambuc std::size_t pos = 0;
5911be35a1SLionel Sambuc for (typename C::const_iterator iter = c.begin(); iter != c.end();
6011be35a1SLionel Sambuc iter++) {
6111be35a1SLionel Sambuc argv[pos] = (*iter).c_str();
6211be35a1SLionel Sambuc pos++;
6311be35a1SLionel Sambuc }
6411be35a1SLionel Sambuc INV(pos == c.size());
6511be35a1SLionel Sambuc argv[pos] = NULL;
6611be35a1SLionel Sambuc
6711be35a1SLionel Sambuc return argv;
6811be35a1SLionel Sambuc }
6911be35a1SLionel Sambuc
7011be35a1SLionel Sambuc template< class C >
7111be35a1SLionel Sambuc C
argv_to_collection(const char * const * argv)7211be35a1SLionel Sambuc argv_to_collection(const char* const* argv)
7311be35a1SLionel Sambuc {
7411be35a1SLionel Sambuc C c;
7511be35a1SLionel Sambuc
7611be35a1SLionel Sambuc for (const char* const* iter = argv; *iter != NULL; iter++)
7711be35a1SLionel Sambuc c.push_back(std::string(*iter));
7811be35a1SLionel Sambuc
7911be35a1SLionel Sambuc return c;
8011be35a1SLionel Sambuc }
8111be35a1SLionel Sambuc
8211be35a1SLionel Sambuc // ------------------------------------------------------------------------
8311be35a1SLionel Sambuc // The "argv_array" type.
8411be35a1SLionel Sambuc // ------------------------------------------------------------------------
8511be35a1SLionel Sambuc
argv_array(void)8611be35a1SLionel Sambuc impl::argv_array::argv_array(void) :
8711be35a1SLionel Sambuc m_exec_argv(collection_to_argv(m_args))
8811be35a1SLionel Sambuc {
8911be35a1SLionel Sambuc }
9011be35a1SLionel Sambuc
argv_array(const char * arg1,...)9111be35a1SLionel Sambuc impl::argv_array::argv_array(const char* arg1, ...)
9211be35a1SLionel Sambuc {
9311be35a1SLionel Sambuc m_args.push_back(arg1);
9411be35a1SLionel Sambuc
9511be35a1SLionel Sambuc {
9611be35a1SLionel Sambuc va_list ap;
9711be35a1SLionel Sambuc const char* nextarg;
9811be35a1SLionel Sambuc
9911be35a1SLionel Sambuc va_start(ap, arg1);
10011be35a1SLionel Sambuc while ((nextarg = va_arg(ap, const char*)) != NULL)
10111be35a1SLionel Sambuc m_args.push_back(nextarg);
10211be35a1SLionel Sambuc va_end(ap);
10311be35a1SLionel Sambuc }
10411be35a1SLionel Sambuc
10511be35a1SLionel Sambuc ctor_init_exec_argv();
10611be35a1SLionel Sambuc }
10711be35a1SLionel Sambuc
argv_array(const char * const * ca)10811be35a1SLionel Sambuc impl::argv_array::argv_array(const char* const* ca) :
10911be35a1SLionel Sambuc m_args(argv_to_collection< args_vector >(ca)),
11011be35a1SLionel Sambuc m_exec_argv(collection_to_argv(m_args))
11111be35a1SLionel Sambuc {
11211be35a1SLionel Sambuc }
11311be35a1SLionel Sambuc
argv_array(const argv_array & a)11411be35a1SLionel Sambuc impl::argv_array::argv_array(const argv_array& a) :
11511be35a1SLionel Sambuc m_args(a.m_args),
11611be35a1SLionel Sambuc m_exec_argv(collection_to_argv(m_args))
11711be35a1SLionel Sambuc {
11811be35a1SLionel Sambuc }
11911be35a1SLionel Sambuc
12011be35a1SLionel Sambuc void
ctor_init_exec_argv(void)12111be35a1SLionel Sambuc impl::argv_array::ctor_init_exec_argv(void)
12211be35a1SLionel Sambuc {
12311be35a1SLionel Sambuc m_exec_argv = collection_to_argv(m_args);
12411be35a1SLionel Sambuc }
12511be35a1SLionel Sambuc
12611be35a1SLionel Sambuc const char* const*
exec_argv(void) const12711be35a1SLionel Sambuc impl::argv_array::exec_argv(void)
12811be35a1SLionel Sambuc const
12911be35a1SLionel Sambuc {
13011be35a1SLionel Sambuc return m_exec_argv.get();
13111be35a1SLionel Sambuc }
13211be35a1SLionel Sambuc
13311be35a1SLionel Sambuc impl::argv_array::size_type
size(void) const13411be35a1SLionel Sambuc impl::argv_array::size(void)
13511be35a1SLionel Sambuc const
13611be35a1SLionel Sambuc {
13711be35a1SLionel Sambuc return m_args.size();
13811be35a1SLionel Sambuc }
13911be35a1SLionel Sambuc
14011be35a1SLionel Sambuc const char*
operator [](int idx) const14111be35a1SLionel Sambuc impl::argv_array::operator[](int idx)
14211be35a1SLionel Sambuc const
14311be35a1SLionel Sambuc {
14411be35a1SLionel Sambuc return m_args[idx].c_str();
14511be35a1SLionel Sambuc }
14611be35a1SLionel Sambuc
14711be35a1SLionel Sambuc impl::argv_array::const_iterator
begin(void) const14811be35a1SLionel Sambuc impl::argv_array::begin(void)
14911be35a1SLionel Sambuc const
15011be35a1SLionel Sambuc {
15111be35a1SLionel Sambuc return m_args.begin();
15211be35a1SLionel Sambuc }
15311be35a1SLionel Sambuc
15411be35a1SLionel Sambuc impl::argv_array::const_iterator
end(void) const15511be35a1SLionel Sambuc impl::argv_array::end(void)
15611be35a1SLionel Sambuc const
15711be35a1SLionel Sambuc {
15811be35a1SLionel Sambuc return m_args.end();
15911be35a1SLionel Sambuc }
16011be35a1SLionel Sambuc
16111be35a1SLionel Sambuc impl::argv_array&
operator =(const argv_array & a)16211be35a1SLionel Sambuc impl::argv_array::operator=(const argv_array& a)
16311be35a1SLionel Sambuc {
16411be35a1SLionel Sambuc if (this != &a) {
16511be35a1SLionel Sambuc m_args = a.m_args;
16611be35a1SLionel Sambuc m_exec_argv = collection_to_argv(m_args);
16711be35a1SLionel Sambuc }
16811be35a1SLionel Sambuc return *this;
16911be35a1SLionel Sambuc }
17011be35a1SLionel Sambuc
17111be35a1SLionel Sambuc // ------------------------------------------------------------------------
17211be35a1SLionel Sambuc // The "stream" types.
17311be35a1SLionel Sambuc // ------------------------------------------------------------------------
17411be35a1SLionel Sambuc
basic_stream(void)17511be35a1SLionel Sambuc impl::basic_stream::basic_stream(void) :
17611be35a1SLionel Sambuc m_inited(false)
17711be35a1SLionel Sambuc {
17811be35a1SLionel Sambuc }
17911be35a1SLionel Sambuc
~basic_stream(void)18011be35a1SLionel Sambuc impl::basic_stream::~basic_stream(void)
18111be35a1SLionel Sambuc {
18211be35a1SLionel Sambuc if (m_inited)
18311be35a1SLionel Sambuc atf_process_stream_fini(&m_sb);
18411be35a1SLionel Sambuc }
18511be35a1SLionel Sambuc
18611be35a1SLionel Sambuc const atf_process_stream_t*
get_sb(void) const18711be35a1SLionel Sambuc impl::basic_stream::get_sb(void)
18811be35a1SLionel Sambuc const
18911be35a1SLionel Sambuc {
19011be35a1SLionel Sambuc INV(m_inited);
19111be35a1SLionel Sambuc return &m_sb;
19211be35a1SLionel Sambuc }
19311be35a1SLionel Sambuc
stream_capture(void)19411be35a1SLionel Sambuc impl::stream_capture::stream_capture(void)
19511be35a1SLionel Sambuc {
19611be35a1SLionel Sambuc atf_error_t err = atf_process_stream_init_capture(&m_sb);
19711be35a1SLionel Sambuc if (atf_is_error(err))
19811be35a1SLionel Sambuc throw_atf_error(err);
19911be35a1SLionel Sambuc m_inited = true;
20011be35a1SLionel Sambuc }
20111be35a1SLionel Sambuc
stream_connect(const int src_fd,const int tgt_fd)20211be35a1SLionel Sambuc impl::stream_connect::stream_connect(const int src_fd, const int tgt_fd)
20311be35a1SLionel Sambuc {
20411be35a1SLionel Sambuc atf_error_t err = atf_process_stream_init_connect(&m_sb, src_fd, tgt_fd);
20511be35a1SLionel Sambuc if (atf_is_error(err))
20611be35a1SLionel Sambuc throw_atf_error(err);
20711be35a1SLionel Sambuc m_inited = true;
20811be35a1SLionel Sambuc }
20911be35a1SLionel Sambuc
stream_inherit(void)21011be35a1SLionel Sambuc impl::stream_inherit::stream_inherit(void)
21111be35a1SLionel Sambuc {
21211be35a1SLionel Sambuc atf_error_t err = atf_process_stream_init_inherit(&m_sb);
21311be35a1SLionel Sambuc if (atf_is_error(err))
21411be35a1SLionel Sambuc throw_atf_error(err);
21511be35a1SLionel Sambuc m_inited = true;
21611be35a1SLionel Sambuc }
21711be35a1SLionel Sambuc
stream_redirect_fd(const int fd)21811be35a1SLionel Sambuc impl::stream_redirect_fd::stream_redirect_fd(const int fd)
21911be35a1SLionel Sambuc {
22011be35a1SLionel Sambuc atf_error_t err = atf_process_stream_init_redirect_fd(&m_sb, fd);
22111be35a1SLionel Sambuc if (atf_is_error(err))
22211be35a1SLionel Sambuc throw_atf_error(err);
22311be35a1SLionel Sambuc m_inited = true;
22411be35a1SLionel Sambuc }
22511be35a1SLionel Sambuc
stream_redirect_path(const fs::path & p)22611be35a1SLionel Sambuc impl::stream_redirect_path::stream_redirect_path(const fs::path& p)
22711be35a1SLionel Sambuc {
22811be35a1SLionel Sambuc atf_error_t err = atf_process_stream_init_redirect_path(&m_sb, p.c_path());
22911be35a1SLionel Sambuc if (atf_is_error(err))
23011be35a1SLionel Sambuc throw_atf_error(err);
23111be35a1SLionel Sambuc m_inited = true;
23211be35a1SLionel Sambuc }
23311be35a1SLionel Sambuc
23411be35a1SLionel Sambuc // ------------------------------------------------------------------------
23511be35a1SLionel Sambuc // The "status" type.
23611be35a1SLionel Sambuc // ------------------------------------------------------------------------
23711be35a1SLionel Sambuc
status(atf_process_status_t & s)23811be35a1SLionel Sambuc impl::status::status(atf_process_status_t& s) :
23911be35a1SLionel Sambuc m_status(s)
24011be35a1SLionel Sambuc {
24111be35a1SLionel Sambuc }
24211be35a1SLionel Sambuc
~status(void)24311be35a1SLionel Sambuc impl::status::~status(void)
24411be35a1SLionel Sambuc {
24511be35a1SLionel Sambuc atf_process_status_fini(&m_status);
24611be35a1SLionel Sambuc }
24711be35a1SLionel Sambuc
24811be35a1SLionel Sambuc bool
exited(void) const24911be35a1SLionel Sambuc impl::status::exited(void)
25011be35a1SLionel Sambuc const
25111be35a1SLionel Sambuc {
25211be35a1SLionel Sambuc return atf_process_status_exited(&m_status);
25311be35a1SLionel Sambuc }
25411be35a1SLionel Sambuc
25511be35a1SLionel Sambuc int
exitstatus(void) const25611be35a1SLionel Sambuc impl::status::exitstatus(void)
25711be35a1SLionel Sambuc const
25811be35a1SLionel Sambuc {
25911be35a1SLionel Sambuc return atf_process_status_exitstatus(&m_status);
26011be35a1SLionel Sambuc }
26111be35a1SLionel Sambuc
26211be35a1SLionel Sambuc bool
signaled(void) const26311be35a1SLionel Sambuc impl::status::signaled(void)
26411be35a1SLionel Sambuc const
26511be35a1SLionel Sambuc {
26611be35a1SLionel Sambuc return atf_process_status_signaled(&m_status);
26711be35a1SLionel Sambuc }
26811be35a1SLionel Sambuc
26911be35a1SLionel Sambuc int
termsig(void) const27011be35a1SLionel Sambuc impl::status::termsig(void)
27111be35a1SLionel Sambuc const
27211be35a1SLionel Sambuc {
27311be35a1SLionel Sambuc return atf_process_status_termsig(&m_status);
27411be35a1SLionel Sambuc }
27511be35a1SLionel Sambuc
27611be35a1SLionel Sambuc bool
coredump(void) const27711be35a1SLionel Sambuc impl::status::coredump(void)
27811be35a1SLionel Sambuc const
27911be35a1SLionel Sambuc {
28011be35a1SLionel Sambuc return atf_process_status_coredump(&m_status);
28111be35a1SLionel Sambuc }
28211be35a1SLionel Sambuc
28311be35a1SLionel Sambuc // ------------------------------------------------------------------------
28411be35a1SLionel Sambuc // The "child" type.
28511be35a1SLionel Sambuc // ------------------------------------------------------------------------
28611be35a1SLionel Sambuc
child(atf_process_child_t & c)28711be35a1SLionel Sambuc impl::child::child(atf_process_child_t& c) :
28811be35a1SLionel Sambuc m_child(c),
28911be35a1SLionel Sambuc m_waited(false)
29011be35a1SLionel Sambuc {
29111be35a1SLionel Sambuc }
29211be35a1SLionel Sambuc
~child(void)29311be35a1SLionel Sambuc impl::child::~child(void)
29411be35a1SLionel Sambuc {
29511be35a1SLionel Sambuc if (!m_waited) {
29611be35a1SLionel Sambuc ::kill(atf_process_child_pid(&m_child), SIGTERM);
29711be35a1SLionel Sambuc
29811be35a1SLionel Sambuc atf_process_status_t s;
299*3260d16fSLionel Sambuc #if defined(__minix) && !defined(NDEBUG)
300*3260d16fSLionel Sambuc atf_error_t err =
301*3260d16fSLionel Sambuc #endif /* defined(__minix) && !defined(NDEBUG) */
302*3260d16fSLionel Sambuc atf_process_child_wait(&m_child, &s);
30311be35a1SLionel Sambuc INV(!atf_is_error(err));
30411be35a1SLionel Sambuc atf_process_status_fini(&s);
30511be35a1SLionel Sambuc }
30611be35a1SLionel Sambuc }
30711be35a1SLionel Sambuc
30811be35a1SLionel Sambuc impl::status
wait(void)30911be35a1SLionel Sambuc impl::child::wait(void)
31011be35a1SLionel Sambuc {
31111be35a1SLionel Sambuc atf_process_status_t s;
31211be35a1SLionel Sambuc
31311be35a1SLionel Sambuc atf_error_t err = atf_process_child_wait(&m_child, &s);
31411be35a1SLionel Sambuc if (atf_is_error(err))
31511be35a1SLionel Sambuc throw_atf_error(err);
31611be35a1SLionel Sambuc
31711be35a1SLionel Sambuc m_waited = true;
31811be35a1SLionel Sambuc return status(s);
31911be35a1SLionel Sambuc }
32011be35a1SLionel Sambuc
32111be35a1SLionel Sambuc pid_t
pid(void) const32211be35a1SLionel Sambuc impl::child::pid(void)
32311be35a1SLionel Sambuc const
32411be35a1SLionel Sambuc {
32511be35a1SLionel Sambuc return atf_process_child_pid(&m_child);
32611be35a1SLionel Sambuc }
32711be35a1SLionel Sambuc
32811be35a1SLionel Sambuc int
stdout_fd(void)32911be35a1SLionel Sambuc impl::child::stdout_fd(void)
33011be35a1SLionel Sambuc {
33111be35a1SLionel Sambuc return atf_process_child_stdout(&m_child);
33211be35a1SLionel Sambuc }
33311be35a1SLionel Sambuc
33411be35a1SLionel Sambuc int
stderr_fd(void)33511be35a1SLionel Sambuc impl::child::stderr_fd(void)
33611be35a1SLionel Sambuc {
33711be35a1SLionel Sambuc return atf_process_child_stderr(&m_child);
33811be35a1SLionel Sambuc }
33911be35a1SLionel Sambuc
34011be35a1SLionel Sambuc // ------------------------------------------------------------------------
34111be35a1SLionel Sambuc // Free functions.
34211be35a1SLionel Sambuc // ------------------------------------------------------------------------
34311be35a1SLionel Sambuc
34411be35a1SLionel Sambuc void
flush_streams(void)34511be35a1SLionel Sambuc detail::flush_streams(void)
34611be35a1SLionel Sambuc {
34711be35a1SLionel Sambuc // This is a weird hack to ensure that the output of the parent process
34811be35a1SLionel Sambuc // is flushed before executing a child which prevents, for example, the
34911be35a1SLionel Sambuc // output of the atf-run hooks to appear before the output of atf-run
35011be35a1SLionel Sambuc // itself.
35111be35a1SLionel Sambuc //
35211be35a1SLionel Sambuc // TODO: This should only be executed when inheriting the stdout or
35311be35a1SLionel Sambuc // stderr file descriptors. However, the flushing is specific to the
35411be35a1SLionel Sambuc // iostreams, so we cannot do it from the C library where all the process
35511be35a1SLionel Sambuc // logic is performed. Come up with a better design.
35611be35a1SLionel Sambuc std::cout.flush();
35711be35a1SLionel Sambuc std::cerr.flush();
35811be35a1SLionel Sambuc }
359