Mir
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
process.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2012 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU 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 General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored by: Thomas Voss <thomas.voss@canonical.com>
17  * Thomas Guest <thomas.guest@canonical.com>
18  */
19 
20 #ifndef MIR_TEST_FRAMEWORK_PROCESS_H_
21 #define MIR_TEST_FRAMEWORK_PROCESS_H_
22 
23 #include <chrono>
24 #include <cstdlib>
25 #include <functional>
26 #include <iosfwd>
27 #include <memory>
28 #include <stdexcept>
29 
30 #include <unistd.h>
31 
32 namespace mir_test_framework
33 {
34 
36 {
37  unknown,
43 };
44 
45 
46 // Aggregated results of running a process to completion
47 struct Result
48 {
49  Result();
50 
51  // Did the process exit without error?
52  bool succeeded() const;
53 
54  // Was the process terminated by a signal?
55  bool signalled() const;
56 
58  int exit_code;
59  int signal;
60 };
61 
62 // Posix process control class.
63 class Process
64 {
65 public:
66  // Construct a process with the supplied pid
67  Process(pid_t pid);
68 
69  // Destroy the process cleanly, by terminating it and waiting for
70  // the pid.
71  ~Process();
72 
73  // Wait for the process to terminate, and return the results.
74  Result wait_for_termination(const std::chrono::milliseconds& timeout = std::chrono::milliseconds(60 * 1000));
75 
76  void kill();
77  void terminate();
78  void stop();
79  void cont();
80  void detach();
81 
82 protected:
83  Process() = delete;
84  Process(const Process&) = delete;
85  Process& operator=(const Process&) = delete;
86 
87 private:
88  pid_t pid;
89  bool terminated;
90  bool detached;
91 };
92 
93 // Stream print helper
94 std::ostream& operator<<(std::ostream& out, const Result& result);
95 
96 // Fork a child process to run the supplied main function, calling
97 // the exit function when done.
98 // Returns the parent process.
99 template<typename Callable>
100 std::shared_ptr<Process> fork_and_run_in_a_different_process(
101  Callable&& main_fn, std::function<int()> exit_fn)
102 {
103  pid_t pid = fork();
104 
105  if (pid < 0)
106  {
107  throw std::runtime_error("Failed to fork process");
108  }
109 
110  if (pid == 0)
111  {
112  main_fn();
113  exit(exit_fn());
114  }
115 
116  return std::shared_ptr<Process>(new Process(pid));
117 }
118 }
119 
120 #endif // MIR_TEST_FRAMEWORK_PROCESS_H_
std::ostream & operator<<(std::ostream &out, const Result &result)
Process & operator=(const Process &)=delete
int signal
Definition: process.h:59
TerminationReason
Definition: process.h:35
std::shared_ptr< Process > fork_and_run_in_a_different_process(Callable &&main_fn, std::function< int()> exit_fn)
Definition: process.h:100
Result wait_for_termination(const std::chrono::milliseconds &timeout=std::chrono::milliseconds(60 *1000))
TerminationReason reason
Definition: process.h:57
Definition: process.h:47
Definition: process.h:63
int exit_code
Definition: process.h:58

Copyright © 2012,2013 Canonical Ltd.
Generated on Fri Apr 11 21:14:53 UTC 2014