libcommonc++  0.7
Process.h++
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2014 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 */
22 
23 #ifndef __ccxx_Process_hxx
24 #define __ccxx_Process_hxx
25 
26 #include <commonc++/Common.h++>
27 #include <commonc++/Stream.h++>
29 #include <commonc++/String.h++>
30 
31 #include <vector>
32 #include <memory>
33 
34 #ifdef CCXX_OS_POSIX
35 #include <sys/types.h>
36 #include <fcntl.h>
37 #endif
38 
39 namespace ccxx {
40 
47 {
48  friend class Process;
49 
50  public:
51 
53  ExitStatus() { }
54 
57 
59  inline int getExitCode() const
60  { return(_exitCode); }
61 
68  inline int getExitSignal() const
69  { return(_exitSignal); }
70 
72  inline bool isNormal() const
73  { return(_exitedNormally); }
74 
79  inline uint64_t getUserTime() const
80  { return(_userTime); }
81 
86  inline uint64_t getKernelTime() const
87  { return(_kernelTime); }
88 
94  inline uint_t getMaxRSS() const
95  { return(_maxSize); }
96 
98  static const int SigAbort;
99 
104  static const int SigFloatingPointException;
105 
110  static const int SigIllegalInstruction;
111 
116  static const int SigKeyboardInterrupt;
117 
119  static const int SigSegmentationFault;
120 
122  static const int SigTerminationRequest;
123 
125  static const int SUCCESS;
126 
128  static const int FAILURE;
129 
130  private:
131 
132  int _exitCode;
133  int _exitSignal;
134  bool _exitedNormally;
135  uint64_t _userTime;
136  uint64_t _kernelTime;
137  uint_t _maxSize;
138 };
139 
159 {
160  public:
161 
170  Process(const String& path, const StringVec& args,
171  const StringVec& env = StringVec::emptyVec,
172  const String& workingDir = ".");
173 
175  ~Process();
176 
183  void start();
184 
191  void stop();
192 
200  bool kill();
201 
203  void setPriority(Priority priority);
204 
206  Priority getPriority() const;
207 
217  bool waitFor(ExitStatus& status);
218 
220  inline bool isStarted() const
221  { return(_started); }
222 
228  { return(*_ins); }
229 
235  { return(*_outs); }
236 
242  { return(*_errs); }
243 
245  inline time_ms_t getStartTime() const
246  { return(_startTime); }
247 
253  static void exit(int status);
254 
256  inline static String getProgramName()
257  { return(_progname); }
258 
264  static String getExecutablePath();
265 
272  static String getExecutableDir();
273 
275  inline static void setProgramName(const char* name)
276  { _progname = name; }
277 
279  static String getWorkingDir();
280 
287  static bool setWorkingDir(const String& path);
288 
295  static bool hasConsole();
296 
310  static bool getConsoleSize(uint_t* columns, uint_t* rows);
311 
317  static void setConsoleTitle(const String& title);
318 
320  static ProcessID currentProcessID();
321 
322  private:
323 
324  String _path;
325  String _workingDir;
326  bool _started;
327  time_ms_t _startTime;
328  ProcessHandle _handle;
329  StringVec _args;
330  StringVec _env;
331  Stream* _ins;
332  Stream* _outs;
333  Stream* _errs;
334 
335  static String _progname;
336 
337  Process(); // not supported
339 };
340 
341 } // namespace ccxx
342 
343 #endif // __ccxx_Process_hxx
Stream & getOutputStream()
Get the output stream for the process.
Definition: Process.h++:234
static const int SUCCESS
The platform&#39;s default "success" exit status.
Definition: Process.h++:125
static const int SigTerminationRequest
Signal indicating the process received a termination request.
Definition: Process.h++:122
uint64_t getKernelTime() const
Get the total amount of kernel-space time, in milliseconds, that the process consumed.
Definition: Process.h++:86
A String vector.
Definition: String.h++:1159
int64_t time_ms_t
A time expressed in milliseconds since the epoch (00:00:00, UTC, January 1, 1970).
Definition: Integers.h++:98
An unbuffered I/O stream.
Definition: Stream.h++:60
time_ms_t getStartTime() const
Get the time at which the process was started.
Definition: Process.h++:245
~ExitStatus()
Destructor.
Definition: Process.h++:56
static const int SigAbort
Signal indicating the process was aborted.
Definition: Process.h++:98
int getExitSignal() const
Get the exit signal.
Definition: Process.h++:68
pid_t ProcessID
Definition: Common.h++:222
unsigned int uint_t
An alias for unsigned int.
Definition: Integers.h++:74
bool isStarted() const
Determine if the process has been started.
Definition: Process.h++:220
pid_t ProcessHandle
Definition: Common.h++:221
ExitStatus()
Construct a new ExitStatus.
Definition: Process.h++:53
The exit status of a process.
Definition: Process.h++:46
static const int SigIllegalInstruction
Signal indicating the process encountered an illegal machine instruction.
Definition: Process.h++:110
static String getProgramName()
Get the name of the calling program.
Definition: Process.h++:256
static const int FAILURE
The platform&#39;s default "failure" exit status.
Definition: Process.h++:128
uint_t getMaxRSS() const
Get the process&#39;s maximum resident set size, in pages.
Definition: Process.h++:94
#define COMMONCPP_API
Definition: Common.h++:126
int getExitCode() const
Get the exit code from the process.
Definition: Process.h++:59
A flexible, reference counted, copy-on-write, thread-safe, nullable string.
Definition: String.h++:50
static const int SigSegmentationFault
Signal indicating the process attempted an illegal memory access.
Definition: Process.h++:119
bool isNormal() const
Determine if the process exited normally.
Definition: Process.h++:72
A system process.
Definition: Process.h++:158
static const StringVec emptyVec
An empty StringVec.
Definition: String.h++:1171
Priority
Thread and Process priority levels.
Definition: Common.h++:307
#define CCXX_COPY_DECLS(CLASS)
Inlines declarations of a copy constructor and assignment operator for the class CLASS.
Definition: Common.h++:295
uint64_t getUserTime() const
Get the total amount of user-space time, in milliseconds, that the process consumed.
Definition: Process.h++:79
static const int SigKeyboardInterrupt
Signal indicating the process received a keyboard interrupt (Control-C).
Definition: Process.h++:116
static const int SigFloatingPointException
Signal indicating the process encountered a floating-point exception (such as division by zero)...
Definition: Process.h++:104
Definition: AllocationMap.c++:25
Stream & getErrorStream()
Get the error stream for the process.
Definition: Process.h++:241
Stream & getInputStream()
Get the input stream for the process.
Definition: Process.h++:227
static void setProgramName(const char *name)
Set the name of the calling program.
Definition: Process.h++:275