process-cpp: /build/process-cpp/src/process-cpp-3.0.1/src/core/testing/fork_and_run.cpp Source File

process-cpp 3.0.0 A simple convenience library for handling processes in C++11.

fork_and_run.cpp
Go to the documentation of this file.
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 }
fork_and_run.h
fork.h
exit.h
core::testing::operator&
CORE_POSIX_DLL_PUBLIC ForkAndRunResult operator&(ForkAndRunResult lhs, ForkAndRunResult rhs)
Definition: fork_and_run.cpp:33
core::testing::ForkAndRunResult
ForkAndRunResult
The ForkAndRunResult enum models the different failure modes of fork_and_run.
Definition: fork_and_run.h:35
core::posix::exit::Status
Status
The Status enum wrap's the posix exit status.
Definition: exit.h:34
wait.h
core::posix::wait::Result::Status::exited
@ exited
The process exited normally.
core::testing::operator|
CORE_POSIX_DLL_PUBLIC ForkAndRunResult operator|(ForkAndRunResult lhs, ForkAndRunResult rhs)
Definition: fork_and_run.cpp:25
core::posix::fork
CORE_POSIX_DLL_PUBLIC ChildProcess fork(const std::function< posix::exit::Status()> &main, const StandardStream &flags)
fork forks a new process and executes the provided main function in the newly forked process.
Definition: fork.cpp:57
core::posix::exit::Status::success
@ success
core::testing::ForkAndRunResult::empty
@ empty
Special value indicating no bit being set.
core::testing::fork_and_run
CORE_POSIX_DLL_PUBLIC ForkAndRunResult fork_and_run(const std::function< core::posix::exit::Status()> &service, const std::function< core::posix::exit::Status()> &client)
Forks two processes for both the service and the client.
Definition: fork_and_run.cpp:41