SASSY  0.0
Software Architecture Support System
 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 - 2017 Brenton Ross
5  *
6  * This file is part of SASSY.
7  *
8  * SASSY 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  * SASSY 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 SASSY. 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 
36 namespace SASSY
37 {
38 
39 namespace cfi
40 {
41 
43 
47 {
48 public:
51 
53  virtual int getId() = 0;
54 
56  virtual void kill() = 0;
57 
59  virtual bool done() = 0;
60 
62  virtual void finished( int exit_status ) = 0;
63 };
64 
66 
72 {
73 private:
74  pid_t pid;
75  std::string name;
76  std::vector< std::string> params;
77  int *fdin;
78  int *fdout;
79  int killSignal;
80  void createProcess();
81  void execProcess();
82  int exitSignal, exitStatus;
83  bool ended;
84 
85  ChildProcess( const ChildProcess & ) = delete;
86  void operator = ( const ChildProcess & ) = delete;
87 
88 public:
90 
95  ChildProcess( const std::string &cmnd, int *fdin = nullptr, int *fdout = nullptr );
96 
98  ChildProcess();
99 
101  virtual ~ChildProcess();
102 
104 
107  virtual void setArgs( const std::vector< std::string> &args );
108 
110  virtual void run();
111 
113  virtual void kill();
114 
116  virtual int signal( int sig );
117 
119 
124  virtual void finished( int result );
125 
127  bool done() { return ended; }
128 
130  virtual int getId() { return (int)pid; }
131 
133  int getExitSignal() { return exitSignal; }
134 };
135 
136 // -----------------------------------------------------------------
137 
139 
144 {
145 public:
147  virtual ~ProcessOwner() {}
148 
150 
153  virtual void processTerminated( AbstractChildProcess *cp ) = 0;
154 };
155 
156 // -----------------------------------------------------------------
157 
159 
167 {
168 private:
170  ChildProcessMgr();
171 
173  void installSignalHandler();
174 
176  static void childDiedSignalHandler( int sig );
177  static void childDiedSignalAction( int sig, siginfo_t *, void *);
178 
180  std::map< pid_t, AbstractChildProcess * > children; // references
181 
183  void (*otherHandler)(int);
184  void (*otherAction)(int, siginfo_t *, void *);
185 
187  std::vector< ProcessOwner * > owners;
188  mutable std::mutex accessMx;
189 
190 public:
192  static ChildProcessMgr & instance();
193 
195  void registerOwner( ProcessOwner * );
196 
198 
203 
205 
211 
213 
216  int numberOfChildren() const { return children.size(); }
217 
219 
222  int numberOfLiveChildren() const;
223 
225  void shutDown();
226 };
227 
228 } // namespace cfi
229 } // namespace SASSY
230 
231 #endif /* PROC_H_ */
int getExitSignal()
get the signal that caused the process to exit.
Definition: proc.h:133
bool done()
get the running status of the program
Definition: proc.h:127
virtual ~ProcessOwner()
Destructor.
Definition: proc.h:147
virtual void kill()=0
Terminates the child process.
virtual bool done()=0
get the running state of the process.
int numberOfLiveChildren() const
get the number of running children
Definition: proc.cpp:349
int numberOfChildren() const
get the number of child processes
Definition: proc.h:216
void registerChild(AbstractChildProcess *cp)
tell the manager about a child
Definition: proc.cpp:307
virtual int getId()=0
Returns the pid of the child process.
static ChildProcessMgr & instance()
get reference to the child process manager
Definition: proc.cpp:280
virtual ~AbstractChildProcess()
Destructor.
Definition: proc.h:50
virtual void kill()
terminate the process
Definition: proc.cpp:82
virtual int signal(int sig)
send a signal to the process
Definition: proc.cpp:98
ChildProcess()
constructor for fork
Definition: proc.cpp:49
void registerOwner(ProcessOwner *)
register an object that is to be notified of a child&#39;s death
Definition: proc.cpp:298
Manage the child processes.
Definition: proc.h:166
virtual void finished(int exit_status)=0
Called by the manager when the child completes.
void shutDown()
terminate all the child processes
Definition: proc.cpp:325
virtual ~ChildProcess()
destructor
Definition: proc.cpp:57
virtual void setArgs(const std::vector< std::string > &args)
set the args for the process
Definition: proc.cpp:68
virtual void processTerminated(AbstractChildProcess *cp)=0
Notification that process terminated.
Abstract child process interface for ChildProcessMgr.
Definition: proc.h:46
virtual void finished(int result)
set the exit status of the process
Definition: proc.cpp:109
virtual int getId()
get the process id
Definition: proc.h:130
Represents the state of a child process.
Definition: proc.h:71
void deregisterChild(AbstractChildProcess *cp)
forget about a child
Definition: proc.cpp:316
virtual void run()
start the command running
Definition: proc.cpp:75
Interface that allows the owner of a child process to be notified.
Definition: proc.h:143