1 |
26 |
unneback |
//####COPYRIGHTBEGIN####
|
2 |
|
|
//
|
3 |
|
|
// ----------------------------------------------------------------------------
|
4 |
|
|
// Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
|
5 |
|
|
//
|
6 |
|
|
// This program is part of the eCos host tools.
|
7 |
|
|
//
|
8 |
|
|
// This program is free software; you can redistribute it and/or modify it
|
9 |
|
|
// under the terms of the GNU General Public License as published by the Free
|
10 |
|
|
// Software Foundation; either version 2 of the License, or (at your option)
|
11 |
|
|
// any later version.
|
12 |
|
|
//
|
13 |
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
14 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
15 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
16 |
|
|
// more details.
|
17 |
|
|
//
|
18 |
|
|
// You should have received a copy of the GNU General Public License along with
|
19 |
|
|
// this program; if not, write to the Free Software Foundation, Inc.,
|
20 |
|
|
// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
21 |
|
|
//
|
22 |
|
|
// ----------------------------------------------------------------------------
|
23 |
|
|
//
|
24 |
|
|
//####COPYRIGHTEND####
|
25 |
|
|
//===========================================================================
|
26 |
|
|
//#####DESCRIPTIONBEGIN####
|
27 |
|
|
//
|
28 |
|
|
// Author(s): sdf
|
29 |
|
|
// Contact(s): sdf
|
30 |
|
|
// Date: 1998/08/11
|
31 |
|
|
// Version: 0.01
|
32 |
|
|
// Purpose:
|
33 |
|
|
// Description: Interface of the subprocess clas
|
34 |
|
|
// Requires:
|
35 |
|
|
// Provides:
|
36 |
|
|
// See also:
|
37 |
|
|
// Known bugs:
|
38 |
|
|
// Usage:
|
39 |
|
|
//
|
40 |
|
|
//####DESCRIPTIONEND####
|
41 |
|
|
//
|
42 |
|
|
//===========================================================================
|
43 |
|
|
#ifndef _SUBPROCESS_H
|
44 |
|
|
#define _SUBPROCESS_H
|
45 |
|
|
|
46 |
|
|
//===========================================================================
|
47 |
|
|
// This class spawns subprocesses in a host-independent manner [almost]
|
48 |
|
|
//===========================================================================
|
49 |
|
|
|
50 |
|
|
#include "eCosStd.h"
|
51 |
|
|
#include "eCosThreadUtils.h"
|
52 |
|
|
#include "Collections.h"
|
53 |
|
|
|
54 |
|
|
class CSubprocess {
|
55 |
|
|
|
56 |
|
|
public:
|
57 |
|
|
void SetPath (LPCTSTR pszPath) { m_strPath=pszPath; }
|
58 |
|
|
const String ErrorString() const;
|
59 |
|
|
|
60 |
|
|
// If bAutodelete is set, the class object will delete itself when the process is finished.
|
61 |
|
|
// This must only be used if the class object is allocated on the heap.
|
62 |
|
|
CSubprocess(bool bAutodelete=false);
|
63 |
|
|
virtual ~CSubprocess();
|
64 |
|
|
|
65 |
|
|
void SetVerbose (bool b) { m_bVerbose=b; }
|
66 |
|
|
void SetDirectory (LPCTSTR pszDir) { m_strDir=pszDir; }
|
67 |
|
|
|
68 |
|
|
// Various forms of the Run function. In each case under UNIX the Run results will always return true
|
69 |
|
|
// (because we can't determine the result of the exec after fork). Under NT the result correctly represents
|
70 |
|
|
// whether the process creation was successful
|
71 |
|
|
|
72 |
|
|
// Run (blocking) sending the output to strOutput.
|
73 |
|
|
bool Run(String &strOutput,LPCTSTR pszCmd) { return Run(AppendFunc,&strOutput,pszCmd,true); }
|
74 |
|
|
|
75 |
|
|
// Run sending output to callback
|
76 |
|
|
bool Run(LogFunc *pfnLog,void * pLogparam,LPCTSTR pszCmd,bool bBlock=true);
|
77 |
|
|
|
78 |
|
|
int Pid() const { return m_idProcess; } // returns process id (even when process is terminated)
|
79 |
|
|
|
80 |
|
|
// Get the CPU time of the process (and, optionally, its children)
|
81 |
|
|
// Note that under UNIX this involves running ps and so may not be that cheap.
|
82 |
|
|
Time CpuTime(bool bRecurse=true) const;
|
83 |
|
|
|
84 |
|
|
// Get the process exit code. This can be:
|
85 |
|
|
// exit code of process (if terminated)
|
86 |
|
|
// 0xffffffff (if process not yet run)
|
87 |
|
|
// GetLastError result (if process could not be run)
|
88 |
|
|
int GetExitCode() { return m_nExitCode; }
|
89 |
|
|
|
90 |
|
|
// Kill the process:
|
91 |
|
|
bool Kill(bool bRecurse=true);
|
92 |
|
|
|
93 |
|
|
// Send some input to the process:
|
94 |
|
|
void Send (LPCTSTR psz);
|
95 |
|
|
// Close it (cause EOF to be read)
|
96 |
|
|
void CloseInput();
|
97 |
|
|
// Is the process running?
|
98 |
|
|
bool ProcessAlive();
|
99 |
|
|
|
100 |
|
|
// Appendfunc can be used to achieve a non-blocking addition to some string
|
101 |
|
|
static void CALLBACK AppendFunc(void *pParam,LPCTSTR psz) {
|
102 |
|
|
ENTERCRITICAL;
|
103 |
|
|
*((String *)pParam)+=psz;
|
104 |
|
|
LEAVECRITICAL;
|
105 |
|
|
}
|
106 |
|
|
|
107 |
|
|
// This function may be used to stop a process given some condition evaluated externally,
|
108 |
|
|
// As long as the function returns true the process will be allowed to continue
|
109 |
|
|
typedef bool (CALLBACK ContinuationFunc)(void *);
|
110 |
|
|
void SetContinuationFunc(ContinuationFunc *pfnContinue,void *pParam){m_pfnContinue=pfnContinue;m_pContinuationFuncParam=pParam;}
|
111 |
|
|
|
112 |
|
|
// Wait for completion of the process, with optional timeout. If the timeout occurs without the process
|
113 |
|
|
// having terminated, the result will be false.
|
114 |
|
|
bool Wait(Duration dTimeout=0x7fffffff);
|
115 |
|
|
|
116 |
|
|
protected:
|
117 |
|
|
String m_strPath;
|
118 |
|
|
|
119 |
|
|
static const String Name (int pid); // for debugging - only works under NT
|
120 |
|
|
|
121 |
|
|
ContinuationFunc *m_pfnContinue;
|
122 |
|
|
void *m_pContinuationFuncParam;
|
123 |
|
|
static bool CALLBACK DefaultContinuationFunc(void *) { return true; }
|
124 |
|
|
|
125 |
|
|
static void CALLBACK NullLogFunc(void *,LPCTSTR) {}
|
126 |
|
|
|
127 |
|
|
struct PInfo;
|
128 |
|
|
struct PInfo {
|
129 |
|
|
PInfo *pParent;
|
130 |
|
|
#ifdef _WIN32
|
131 |
|
|
__int64 tCreation;
|
132 |
|
|
#endif
|
133 |
|
|
Time tCpu;
|
134 |
|
|
int PID;
|
135 |
|
|
int PPID;
|
136 |
|
|
bool IsChildOf(int pid) const;
|
137 |
|
|
};
|
138 |
|
|
|
139 |
|
|
typedef std::vector<PInfo> PInfoArray;
|
140 |
|
|
|
141 |
|
|
static bool PSExtract(PInfoArray &arPinfo);
|
142 |
|
|
static void SetParents(PInfoArray &arPinfo);
|
143 |
|
|
|
144 |
|
|
#ifdef _WIN32
|
145 |
|
|
static DWORD GetPlatform();
|
146 |
|
|
HANDLE m_hrPipe;
|
147 |
|
|
HANDLE m_hwPipe;
|
148 |
|
|
HANDLE m_hProcess; // This handle is "owned" by the ThreadFunc
|
149 |
|
|
static HINSTANCE hInstLib1, hInstLib2;
|
150 |
|
|
int m_nErr;
|
151 |
|
|
#else
|
152 |
|
|
int m_tty;
|
153 |
|
|
String m_strCmd;
|
154 |
|
|
#endif
|
155 |
|
|
|
156 |
|
|
static void CALLBACK SThreadFunc(void *pParam) { ((CSubprocess *)pParam)->ThreadFunc(); }
|
157 |
|
|
void ThreadFunc();
|
158 |
|
|
bool m_bAutoDelete;
|
159 |
|
|
bool m_bThreadTerminated;
|
160 |
|
|
bool m_bVerbose;
|
161 |
|
|
int m_nExitCode;
|
162 |
|
|
int m_idProcess;
|
163 |
|
|
void *m_pLogparam;
|
164 |
|
|
LogFunc *m_pfnLogfunc;
|
165 |
|
|
bool m_bKillThread;
|
166 |
|
|
|
167 |
|
|
static const unsigned int PROCESS_KILL_EXIT_CODE;
|
168 |
|
|
bool CreateProcess(LPCTSTR pszCmdline);
|
169 |
|
|
|
170 |
|
|
struct CygProcessInfo {
|
171 |
|
|
int nPid;
|
172 |
|
|
int nPpid;
|
173 |
|
|
int nPgid;
|
174 |
|
|
int nWinpid;
|
175 |
|
|
};
|
176 |
|
|
|
177 |
|
|
|
178 |
|
|
void Output(LPCTSTR psz);
|
179 |
|
|
String m_strDir;
|
180 |
|
|
};
|
181 |
|
|
|
182 |
|
|
#endif
|