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 #ifndef CEYLAN_PROCESS_H_
00028 #define CEYLAN_PROCESS_H_
00029
00030
00031 #include "CeylanRunnable.h"
00032 #include "CeylanSystem.h"
00033 #include "CeylanTypes.h"
00034 #include "CeylanFeatures.h"
00035
00036
00037
00038 #include <string>
00039 #include <list>
00040
00041
00042
00043
00044 namespace Ceylan
00045 {
00046
00047
00048 namespace System
00049 {
00050
00051
00052
00053 class InputStream ;
00054
00055
00056
00057 class OutputStream ;
00058
00059
00060
00062 class CEYLAN_DLL ProcessException: public RunnableException
00063 {
00064
00065 public:
00066
00067 explicit ProcessException( const std::string message ) ;
00068 virtual ~ProcessException() throw() ;
00069
00070 } ;
00071
00072
00073
00075 typedef Ceylan::Uint32 Pid ;
00076
00077
00078
00079
00103 class CEYLAN_DLL Process: public Runnable
00104 {
00105
00106
00107 public:
00108
00109
00110
00112 enum ExitReason
00113 {
00114
00116 ExitedNormally,
00117
00119 Signaled,
00120
00122 Stopped,
00123
00125 BadChildPid
00126
00127 } ;
00128
00129
00130
00132 Process() ;
00133
00134
00135
00137 explicit Process( const std::string & name ) ;
00138
00139
00140
00142 virtual ~Process() throw() ;
00143
00144
00145
00153 virtual void run() ;
00154
00155
00156
00158 virtual void start() = 0 ;
00159
00160
00161
00169 virtual void kill() ;
00170
00171
00172
00180 bool isRunning() const ;
00181
00182
00183
00185 inline ErrorCode getError() const ;
00186
00187
00188
00194 inline Pid getPID() const ;
00195
00196
00197
00210 virtual const std::string toString(
00211 Ceylan::VerbosityLevels level = Ceylan::high ) const ;
00212
00213
00214
00215
00216
00217
00218
00229 static Pid GetHostingPID() ;
00230
00231
00232
00241 static Pid GetParentID() ;
00242
00243
00244
00261 static ExitReason WaitChildProcess(
00262 const Process & childProcess,
00263 ErrorCode * executionInfo = 0 ) ;
00264
00265
00266
00273 static std::string GetOwner() ;
00274
00275
00276
00296 static void RunExecutable(
00297 const std::string & filename,
00298 const std::list<std::string> & arguments,
00299 const std::string & stdoutFilename = 0,
00300 const std::string & stderrFilename = 0,
00301 const std::string & stdinFilename = 0 ) ;
00302
00303
00304
00313 static bool RedirectStdout( const std::string & filename ) ;
00314
00315
00316
00327 static bool RedirectStdout( OutputStream & os ) ;
00328
00329
00330
00339 static bool RedirectStderr( const std::string & filename ) ;
00340
00341
00342
00353 static bool RedirectStderr( OutputStream & os ) ;
00354
00355
00356
00365 static bool RedirectStdin( const std::string & filename ) ;
00366
00367
00368
00379 static bool RedirectStdin( InputStream & is ) ;
00380
00381
00382
00390 static Ceylan::Uint32 GetTime() ;
00391
00392
00393
00401 static void SaveState( int argc, char ** argv ) ;
00402
00403
00404
00414 static void Restart() ;
00415
00416
00417
00418 protected:
00419
00420
00427 virtual void processCreationFailed() ;
00428
00429
00430
00431 private:
00432
00433
00434
00442 static bool DuplicateStream( FileDescriptor FDOld,
00443 FileDescriptor FDNew ) ;
00444
00445
00446
00454 Process( const Process & source ) ;
00455
00456
00464 Process & operator = ( const Process & source ) ;
00465
00466
00467
00469 Pid _id ;
00470
00471
00473 ErrorCode _error ;
00474
00475
00476
00477
00478
00479
00480
00482 static std::string _Owner ;
00483
00484
00486 static bool _Saved ;
00487
00488
00490 static std::string _Path ;
00491
00492
00494 static std::string _Executable ;
00495
00496
00497
00498
00499
00500
00501
00502
00503 #pragma warning( push )
00504 #pragma warning( disable: 4251 )
00505
00507 static std::list<std::string> _ArgumentList ;
00508
00509 #pragma warning( pop )
00510
00511
00512 } ;
00513
00514
00515
00516 ErrorCode Process::getError() const
00517 {
00518
00519 return _error ;
00520
00521 }
00522
00523
00524
00525 Pid Process::getPID() const
00526 {
00527
00528 return _id ;
00529
00530 }
00531
00532
00533 }
00534
00535 }
00536
00537
00538
00539 #endif // CEYLAN_PROCESS_H_
00540