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 
20 
21 #include <core/posix/exit.h>
22 #include <core/posix/fork.h>
23 #include <core/posix/wait.h>
24 
28 {
29  return static_cast<core::testing::ForkAndRunResult>(
30  static_cast<unsigned int> (lhs) | static_cast<unsigned int>(rhs));
31 }
32 
36 {
37  return static_cast<core::testing::ForkAndRunResult>(
38  static_cast<unsigned int> (lhs) & static_cast<unsigned int>(rhs));
39 }
40 
42  const std::function<core::posix::exit::Status()>& service,
43  const std::function<core::posix::exit::Status()>& client)
44 {
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  {
55  if (client_result.detail.if_exited.status == core::posix::exit::Status::failure)
57  break;
58  default:
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  {
69  if (service_result.detail.if_exited.status == core::posix::exit::Status::failure)
71  break;
72  default:
74  break;
75  }
76 
77  return result;
78 }
ForkAndRunResult
The ForkAndRunResult enum models the different failure modes of fork_and_run.
Definition: fork_and_run.h:34
Status
The Status enum wrap&#39;s the posix exit status.
Definition: exit.h:33
The process exited normally.
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.
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
Special value indicating no bit being set.
CORE_POSIX_DLL_PUBLIC ForkAndRunResult operator&(ForkAndRunResult lhs, ForkAndRunResult rhs)
CORE_POSIX_DLL_PUBLIC ForkAndRunResult operator|(ForkAndRunResult lhs, ForkAndRunResult rhs)
Also wait for state changes in untraced children.