Mir
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 2 or 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 
57  // Was the process terminated normally?
58  bool exited_normally() const;
59 
61  int exit_code;
62  int signal;
63 };
64 
65 // Posix process control class.
66 class Process
67 {
68 public:
69  // Construct a process with the supplied pid
70  Process(pid_t pid);
71 
72  // Destroy the process cleanly, by terminating it and waiting for
73  // the pid.
74  ~Process();
75 
76  // Wait for the process to terminate, and return the results.
77  Result wait_for_termination(const std::chrono::milliseconds& timeout = std::chrono::milliseconds(60 * 1000));
78 
79  void kill();
80  void terminate();
81  void stop();
82  void cont();
83  void detach();
84 
85 protected:
86  Process() = delete;
87  Process(const Process&) = delete;
88  Process& operator=(const Process&) = delete;
89 
90 private:
91  pid_t pid;
92  bool terminated;
93  bool detached;
94 };
95 
96 // Stream print helper
97 std::ostream& operator<<(std::ostream& out, const Result& result);
98 
99 // Fork a child process to run the supplied main function, calling
100 // the exit function when done.
101 // Returns the parent process.
102 template<typename Callable>
103 std::shared_ptr<Process> fork_and_run_in_a_different_process(
104  Callable&& main_fn, std::function<int()> exit_fn)
105 {
106  pid_t pid = fork();
107 
108  if (pid < 0)
109  {
110  throw std::runtime_error("Failed to fork process");
111  }
112 
113  if (pid == 0)
114  {
115  main_fn();
116  exit(exit_fn());
117  }
118 
119  return std::shared_ptr<Process>(new Process(pid));
120 }
121 }
122 
123 #endif // MIR_TEST_FRAMEWORK_PROCESS_H_
void kill(Application const &application, int sig)
std::ostream & operator<<(std::ostream &out, const Result &result)
int signal
Definition: process.h:62
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:103
TerminationReason reason
Definition: process.h:60
Definition: any_surface.h:25
Definition: process.h:47
Definition: process.h:66
int exit_code
Definition: process.h:61

Copyright © 2012-2018 Canonical Ltd.
Generated on Thu Apr 5 15:26:05 UTC 2018