VICI  0.11.815
Visual Chart Interpreter
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
proc.h
Go to the documentation of this file.
1 /*
2  * proc.h
3  *
4  * Copyright 2001 - 2018 Brenton Ross
5  *
6  * This file is part of VICI.
7  *
8  * VICI is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * VICI is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with VICI. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
27 #ifndef CFI_PROC_H
28 #define CFI_PROC_H
29 
30 #include <string>
31 #include <map>
32 #include <vector>
33 #include <signal.h>
34 #include <mutex>
35 #include <thread>
36 
37 namespace VICI
38 {
39 
40 namespace cfi
41 {
42 
44 
48 {
49 public:
52 
54  virtual int getId() = 0;
55 
57  virtual void kill() = 0;
58 
60  virtual bool done() = 0;
61 
63  virtual void finished( int exit_status ) = 0;
64 };
65 
67 
73 {
74 private:
75  pid_t pid;
76  std::string name;
77  std::vector< std::string> params;
78  int *fdin;
79  int *fdout;
80  int *fderr;
81  int killSignal;
82  void createProcess();
83  void execProcess();
84  int exitSignal, exitStatus;
85  bool ended;
86  bool showExitStatus;
87 
88  ChildProcess( const ChildProcess & ) = delete;
89  void operator = ( const ChildProcess & ) = delete;
90 
91 public:
93 
99  ChildProcess( const std::string &cmnd, int *fdin = nullptr, int *fdout = nullptr, int *fderr = nullptr );
100 
102  ChildProcess();
103 
105  virtual ~ChildProcess();
106 
108 
111  virtual void setArgs( const std::vector< std::string> &args );
112 
114  virtual void run();
115 
117  virtual void kill();
118 
120  virtual int signal( int sig );
121 
123 
128  virtual void finished( int result );
129 
131  bool done() { return ended; }
132 
134  virtual int getId() { return (int)pid; }
135 
137  int getExitSignal() { return exitSignal; }
138 
140  int getExitStatus() { return exitStatus; }
141 
143  void reportExitErrors( bool x) { showExitStatus = x; }
144 };
145 
146 // -----------------------------------------------------------------
147 
149 
154 {
155 public:
157  virtual ~ProcessOwner() {}
158 
160 
163  virtual void processTerminated( AbstractChildProcess *cp ) = 0;
164 };
165 
166 // -----------------------------------------------------------------
167 
169 
177 {
178 private:
179  // these are for insulating the signal handler
180  std::unique_ptr<std::thread> thread;
181  bool terminating;
182  int fds[2];
183 
184  void threadFn();
185  struct EndChild
186  {
187  int stat_val;
188  AbstractChildProcess * kid;
189  bool gotSignal;
190  };
191 
192 private:
194  ChildProcessMgr();
195 
197  ~ChildProcessMgr();
198 
200  void installSignalHandler();
201 
203  static void childDiedSignalHandler( int sig );
204  static void childDiedSignalAction( int sig, siginfo_t *, void *);
205 
207  std::map< pid_t, AbstractChildProcess * > children; // references
208 
210  void (*otherHandler)(int);
211  void (*otherAction)(int, siginfo_t *, void *);
212 
214  std::vector< ProcessOwner * > owners;
215  mutable std::mutex accessMx;
216 
218  void notify( int stat_val, AbstractChildProcess * );
219 
220 public:
222  static ChildProcessMgr & instance();
223 
225  void registerOwner( ProcessOwner * );
226 
228 
233 
235 
241 
243 
246  int numberOfChildren() const { return children.size(); }
247 
249 
252  int numberOfLiveChildren() const;
253 
255  void shutDown();
256 };
257 
258 } // namespace cfi
259 } // namespace vici
260 
261 #endif /* PROC_H_ */
virtual int getId()=0
Returns the pid of the child process.
void shutDown()
terminate all the child processes
Definition: proc.cpp:406
Interface that allows the owner of a child process to be notified.
Definition: proc.h:153
void registerOwner(ProcessOwner *)
register an object that is to be notified of a child&#39;s death
Definition: proc.cpp:379
static ChildProcessMgr & instance()
get reference to the child process manager
Definition: proc.cpp:296
virtual void run()
start the command running
Definition: proc.cpp:75
virtual void setArgs(const std::vector< std::string > &args)
set the args for the process
Definition: proc.cpp:68
virtual void finished(int result)
set the exit status of the process
Definition: proc.cpp:109
int getExitSignal()
get the signal that caused the process to exit.
Definition: proc.h:137
virtual ~ChildProcess()
destructor
Definition: proc.cpp:57
int getExitStatus()
get the exit status
Definition: proc.h:140
int numberOfChildren() const
get the number of child processes
Definition: proc.h:246
virtual void kill()
terminate the process
Definition: proc.cpp:82
Abstract child process interface for ChildProcessMgr.
Definition: proc.h:47
virtual int signal(int sig)
send a signal to the process
Definition: proc.cpp:98
ChildProcess()
constructor for fork
Definition: proc.cpp:49
void deregisterChild(AbstractChildProcess *cp)
forget about a child
Definition: proc.cpp:397
void reportExitErrors(bool x)
turn on or off the reporting of error exits
Definition: proc.h:143
virtual void processTerminated(AbstractChildProcess *cp)=0
Notification that process terminated.
virtual ~AbstractChildProcess()
Destructor.
Definition: proc.h:51
virtual int getId()
get the process id
Definition: proc.h:134
virtual ~ProcessOwner()
Destructor.
Definition: proc.h:157
virtual void kill()=0
Terminates the child process.
virtual void finished(int exit_status)=0
Called by the manager when the child completes.
int numberOfLiveChildren() const
get the number of running children
Definition: proc.cpp:430
void registerChild(AbstractChildProcess *cp)
tell the manager about a child
Definition: proc.cpp:388
virtual bool done()=0
get the running state of the process.
bool done()
get the running status of the program
Definition: proc.h:131
Manage the child processes.
Definition: proc.h:176
Represents the state of a child process.
Definition: proc.h:72