00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef BLOCXX_PROCESS_HPP_INCLUDE_GUARD_
00035 #define BLOCXX_PROCESS_HPP_INCLUDE_GUARD_
00036
00041 #include "blocxx/BLOCXX_config.h"
00042 #include "blocxx/Exception.hpp"
00043 #include "blocxx/Types.hpp"
00044 #include "blocxx/IntrusiveCountableBase.hpp"
00045 #include "blocxx/CommonFwd.hpp"
00046 #include "blocxx/Cstr.hpp"
00047 #include "blocxx/Timeout.hpp"
00048
00049 #include <vector>
00050
00051 namespace BLOCXX_NAMESPACE
00052 {
00053
00054 BLOCXX_DECLARE_EXCEPTION(ProcessError);
00055
00056 class ProcessImpl;
00057 typedef IntrusiveReference<ProcessImpl> ProcessImplRef;
00058
00060
00061 class BLOCXX_COMMON_API Process : public IntrusiveCountableBase
00062 {
00063 public:
00066
00067 Process(
00068 UnnamedPipeRef const & in, UnnamedPipeRef const & out,
00069 UnnamedPipeRef const & err, ProcId pid
00070 );
00071
00072 protected:
00079
00080 Process(
00081 const ProcessImplRef& impl, UnnamedPipeRef const & in, UnnamedPipeRef const & out,
00082 UnnamedPipeRef const & err, ProcId pid
00083 );
00084
00085 public:
00088 Process(ProcId pid);
00089
00097 void release();
00098
00103 virtual ~Process();
00104
00107 UnnamedPipeRef in() const;
00108
00111 UnnamedPipeRef out() const;
00112
00115 UnnamedPipeRef err() const;
00116
00118 ProcId pid() const;
00119
00121
00122 class BLOCXX_COMMON_API Status
00123 {
00124 public:
00125 struct Repr { };
00126
00129
00132
00133 Status(ProcId wpid, int status);
00134
00137
00138 Status(int rep1, int rep2, Repr);
00139
00141
00142 Status();
00143
00145
00146 bool running() const;
00147
00149
00150 bool exitTerminated() const;
00151
00157 int exitStatus() const;
00158
00160
00161 bool terminatedSuccessfully() const;
00162
00164
00165 bool signalTerminated() const;
00166
00168
00169 bool terminated() const;
00170
00173
00174 int termSignal() const;
00175
00177
00178 bool stopped() const;
00179
00182
00183 int stopSignal() const;
00184
00188 String toString() const;
00189
00193 int getPOSIXwaitpidStatus() const;
00194
00195 void repr(int & rep1, int & rep2) const;
00196
00197 private:
00198 bool m_status_available;
00199 int m_status;
00200 };
00201
00207 Status processStatus();
00208
00209 enum ETerminationSelectionFlag
00210 {
00212 E_TERMINATE_PROCESS_GROUP,
00214 E_TERMINATE_PROCESS_ONLY
00215 };
00238 void waitCloseTerm(
00239 const Timeout& wait_initial = Timeout::relative(5.0),
00240 const Timeout& wait_close = Timeout::relative(10.0),
00241 const Timeout& wait_term = Timeout::relative(15.0),
00242 ETerminationSelectionFlag terminationSelectionFlag = E_TERMINATE_PROCESS_GROUP);
00243
00266 void waitCloseTerm(float wait_initial, float wait_close, float wait_term);
00267
00268 private:
00269 bool terminatesWithin(const Timeout& wait_time);
00270 ProcId getCurProcessId();
00271
00272 #ifdef BLOCXX_WIN32
00273 bool terminateByMessage(const Timeout& waitTime);
00274 bool killProcess(const Timeout& waitTime, ETerminationSelectionFlag terminationSelectionFlag);
00275 #else
00276 bool killWait(const Timeout& wait_time, int sig, char const * signame, ETerminationSelectionFlag terminationSelectionFlag);
00277 #endif
00278
00280 Process(Process const &);
00281
00283 void operator=(Process const &);
00284
00285 ProcessImplRef m_impl;
00286 UnnamedPipeRef m_in;
00287 UnnamedPipeRef m_out;
00288 UnnamedPipeRef m_err;
00289 ProcId m_pid;
00290 Status m_status;
00291 };
00292
00296 class BLOCXX_COMMON_API ProcessImpl : public IntrusiveCountableBase
00297 {
00298 public:
00299 virtual ~ProcessImpl();
00300
00307 virtual int kill(ProcId pid, int sig) = 0;
00308
00315 virtual Process::Status pollStatus(ProcId pid) = 0;
00316 };
00317
00318
00319
00320 }
00321
00322 #endif