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