blocxx

Process.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2005, Quest Software, Inc. All rights reserved.
00003 * Copyright (C) 2006, Novell, Inc. All rights reserved.
00004 * 
00005 * Redistribution and use in source and binary forms, with or without
00006 * modification, are permitted provided that the following conditions are met:
00007 * 
00008 *     * Redistributions of source code must retain the above copyright notice,
00009 *       this list of conditions and the following disclaimer.
00010 *     * Redistributions in binary form must reproduce the above copyright
00011 *       notice, this list of conditions and the following disclaimer in the
00012 *       documentation and/or other materials provided with the distribution.
00013 *     * Neither the name of 
00014 *       Quest Software, Inc., 
00015 *       nor Novell, Inc., 
00016 *       nor the names of its contributors or employees may be used to 
00017 *       endorse or promote products derived from this software without 
00018 *       specific prior written permission.
00019 * 
00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00021 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00022 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00023 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00024 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00025 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00026 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00027 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00028 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00029 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00030 * POSSIBILITY OF SUCH DAMAGE.
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   // For Exec::spawn
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 } // namespace BLOCXX_NAMESPACE
00321 
00322 #endif