ba2efd630302d5bf05692d447ed425e03e5492c7
[iec.git] / src / type3_AndroidCloud / anbox-master / external / process-cpp-minimal / src / core / testing / fork_and_run.cpp
1 /*
2  * Copyright © 2012-2013 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License version 3,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored by: Thomas Voß <thomas.voss@canonical.com>
17  */
18
19 #include <core/testing/fork_and_run.h>
20
21 #include <core/posix/exit.h>
22 #include <core/posix/fork.h>
23 #include <core/posix/wait.h>
24
25 core::testing::ForkAndRunResult core::testing::operator|(
26         core::testing::ForkAndRunResult lhs,
27         core::testing::ForkAndRunResult rhs)
28 {
29     return static_cast<core::testing::ForkAndRunResult>(
30                 static_cast<unsigned int> (lhs) | static_cast<unsigned int>(rhs));
31 }
32
33 core::testing::ForkAndRunResult core::testing::operator&(
34         core::testing::ForkAndRunResult lhs,
35         core::testing::ForkAndRunResult rhs)
36 {
37     return static_cast<core::testing::ForkAndRunResult>(
38                 static_cast<unsigned int> (lhs) & static_cast<unsigned int>(rhs));
39 }
40
41 core::testing::ForkAndRunResult core::testing::fork_and_run(
42         const std::function<core::posix::exit::Status()>& service,
43         const std::function<core::posix::exit::Status()>& client)
44 {
45     core::testing::ForkAndRunResult result = core::testing::ForkAndRunResult::empty;
46
47     auto service_process = core::posix::fork(service, core::posix::StandardStream::empty);
48     auto client_process = core::posix::fork(client, core::posix::StandardStream::empty);
49
50     auto client_result = client_process.wait_for(core::posix::wait::Flags::untraced);
51
52     switch (client_result.status)
53     {
54     case core::posix::wait::Result::Status::exited:
55         if (client_result.detail.if_exited.status == core::posix::exit::Status::failure)
56             result = result | core::testing::ForkAndRunResult::client_failed;
57         break;
58     default:
59         result = result | core::testing::ForkAndRunResult::client_failed;
60         break;
61     }
62
63     service_process.send_signal_or_throw(core::posix::Signal::sig_term);
64     auto service_result = service_process.wait_for(core::posix::wait::Flags::untraced);
65
66     switch (service_result.status)
67     {
68     case core::posix::wait::Result::Status::exited:
69         if (service_result.detail.if_exited.status == core::posix::exit::Status::failure)
70             result = result | core::testing::ForkAndRunResult::service_failed;
71         break;
72     default:
73         result = result | core::testing::ForkAndRunResult::service_failed;
74         break;
75     }
76
77     return result;
78 }