blocxx
Process.hpp
Go to the documentation of this file.
1/*******************************************************************************
2* Copyright (C) 2005, Quest Software, Inc. All rights reserved.
3* Copyright (C) 2006, Novell, Inc. All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without
6* modification, are permitted provided that the following conditions are met:
7*
8* * Redistributions of source code must retain the above copyright notice,
9* this list of conditions and the following disclaimer.
10* * Redistributions in binary form must reproduce the above copyright
11* notice, this list of conditions and the following disclaimer in the
12* documentation and/or other materials provided with the distribution.
13* * Neither the name of
14* Quest Software, Inc.,
15* nor Novell, Inc.,
16* nor the names of its contributors or employees may be used to
17* endorse or promote products derived from this software without
18* specific prior written permission.
19*
20* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30* POSSIBILITY OF SUCH DAMAGE.
31*******************************************************************************/
32
33
34#ifndef BLOCXX_PROCESS_HPP_INCLUDE_GUARD_
35#define BLOCXX_PROCESS_HPP_INCLUDE_GUARD_
36
40
41#include "blocxx/BLOCXX_config.h"
42#include "blocxx/Exception.hpp"
43#include "blocxx/Types.hpp"
45#include "blocxx/CommonFwd.hpp"
46#include "blocxx/Cstr.hpp"
47#include "blocxx/Timeout.hpp"
48 // For Exec::spawn
49#include <vector>
50
51namespace BLOCXX_NAMESPACE
52{
53
55
56class ProcessImpl;
58
60//
61class BLOCXX_COMMON_API Process : public IntrusiveCountableBase
62{
63public:
66 //
67 Process(
68 UnnamedPipeRef const & in, UnnamedPipeRef const & out,
70 );
71
72protected:
79 //
80 Process(
81 const ProcessImplRef& impl, UnnamedPipeRef const & in, UnnamedPipeRef const & out,
83 );
84
85public:
89
97 void release();
98
103 virtual ~Process();
104
107 UnnamedPipeRef in() const;
108
111 UnnamedPipeRef out() const;
112
115 UnnamedPipeRef err() const;
116
118 ProcId pid() const;
119
121 //
122 class BLOCXX_COMMON_API Status
123 {
124 public:
125 struct Repr { };
126
129
132 //
133 Status(ProcId wpid, int status);
134
137 //
138 Status(int rep1, int rep2, Repr);
139
141 //
142 Status();
143
145 //
146 bool running() const;
147
149 //
150 bool exitTerminated() const;
151
157 int exitStatus() const;
158
160 //
161 bool terminatedSuccessfully() const;
162
164 //
165 bool signalTerminated() const;
166
168 //
169 bool terminated() const;
170
173 //
174 int termSignal() const;
175
177 //
178 bool stopped() const;
179
182 //
183 int stopSignal() const;
184
188 String toString() const;
189
193 int getPOSIXwaitpidStatus() const;
194
195 void repr(int & rep1, int & rep2) const;
196
197 private:
200 };
201
208
216
238 void waitCloseTerm(
239 const Timeout& wait_initial = Timeout::relative(5.0),
240 const Timeout& wait_close = Timeout::relative(10.0),
241 const Timeout& wait_term = Timeout::relative(15.0),
242 ETerminationSelectionFlag terminationSelectionFlag = E_TERMINATE_PROCESS_GROUP);
243
266 void waitCloseTerm(float wait_initial, float wait_close, float wait_term);
267
268private:
269 bool terminatesWithin(const Timeout& wait_time);
270 ProcId getCurProcessId();
271
272#ifdef BLOCXX_WIN32
273 bool terminateByMessage(const Timeout& waitTime);
274 bool killProcess(const Timeout& waitTime, ETerminationSelectionFlag terminationSelectionFlag);
275#else
276 bool killWait(const Timeout& wait_time, int sig, char const * signame, ETerminationSelectionFlag terminationSelectionFlag);
277#endif
278
280 Process(Process const &);
281
283 void operator=(Process const &);
284
291};
292
296class BLOCXX_COMMON_API ProcessImpl : public IntrusiveCountableBase
297{
298public:
299 virtual ~ProcessImpl();
300
307 virtual int kill(ProcId pid, int sig) = 0;
308
316};
317
318
319
320} // namespace BLOCXX_NAMESPACE
321
322#endif
#define BLOCXX_DECLARE_EXCEPTION(NAME)
Declare a new exception class named <NAME>Exception that derives from Exception This macro is typical...
Portable process status.
Definition Process.hpp:123
String toString() const
Get a string representation of the status suitable for debugging or logging.
Definition Process.cpp:229
Status(ProcId wpid, int status)
Definition Process.cpp:102
int getPOSIXwaitpidStatus() const
Get the result from waitpid()
Definition Process.cpp:191
void repr(int &rep1, int &rep2) const
Definition Process.cpp:218
@ E_TERMINATE_PROCESS_ONLY
The process will be terminated.
Definition Process.hpp:214
@ E_TERMINATE_PROCESS_GROUP
The process and any descendent processes which are in the process group will be terminated.
Definition Process.hpp:212
Process(Process const &)
Copying not allowed (private)
Process(UnnamedPipeRef const &in, UnnamedPipeRef const &out, UnnamedPipeRef const &err, ProcId pid)
Definition Process.cpp:325
UnnamedPipeRef out() const
Stdout for the child process.
Definition Process.cpp:408
ProcessImplRef m_impl
Definition Process.hpp:285
ProcId pid() const
Process ID for the child process.
Definition Process.cpp:418
void operator=(Process const &)
Assignment not allowed (private)
void release()
Releases ownership of the ProcId and UnnamedPipes held by this object.
Definition Process.cpp:395
UnnamedPipeRef in() const
Stdin for the child process.
Definition Process.cpp:403
UnnamedPipeRef err() const
Stderr for the child process.
Definition Process.cpp:413
Abstract interface for abstracting details of dealing with a process.
Definition Process.hpp:297
virtual Process::Status pollStatus(ProcId pid)=0
virtual int kill(ProcId pid, int sig)=0
Sends signal sig to process pid.
This String class is an abstract data type that represents as NULL terminated string of characters.
Definition String.hpp:67
A timeout can be absolute, which means that it will happen at the specified DateTime.
Definition Timeout.hpp:56
static Timeout relative(float seconds)
Definition Timeout.cpp:58
Taken from RFC 1321.
IntrusiveReference< UnnamedPipe > UnnamedPipeRef
IntrusiveReference< ProcessImpl > ProcessImplRef
Definition Process.hpp:57