1 //####COPYRIGHTBEGIN####
3 // ----------------------------------------------------------------------------
4 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
6 // This program is part of the eCos host tools.
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)
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
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.
22 // ----------------------------------------------------------------------------
24 //####COPYRIGHTEND####
25 //===========================================================================
26 //#####DESCRIPTIONBEGIN####
33 // Description: Interface of the subprocess clas
40 //####DESCRIPTIONEND####
42 //===========================================================================
46 //===========================================================================
47 // This class spawns subprocesses in a host-independent manner [almost]
48 //===========================================================================
51 #include "eCosThreadUtils.h"
52 #include "Collections.h"
57 void SetPath (LPCTSTR pszPath) { m_strPath=pszPath; }
58 const String ErrorString() const;
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();
65 void SetVerbose (bool b) { m_bVerbose=b; }
66 void SetDirectory (LPCTSTR pszDir) { m_strDir=pszDir; }
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
72 // Run (blocking) sending the output to strOutput.
73 bool Run(String &strOutput,LPCTSTR pszCmd) { return Run(AppendFunc,&strOutput,pszCmd,true); }
75 // Run sending output to callback
76 bool Run(LogFunc *pfnLog,void * pLogparam,LPCTSTR pszCmd,bool bBlock=true);
78 int Pid() const { return m_idProcess; } // returns process id (even when process is terminated)
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;
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; }
91 bool Kill(bool bRecurse=true);
93 // Send some input to the process:
94 void Send (LPCTSTR psz);
95 // Close it (cause EOF to be read)
97 // Is the process running?
100 // Appendfunc can be used to achieve a non-blocking addition to some string
101 static void CALLBACK AppendFunc(void *pParam,LPCTSTR psz) {
103 *((String *)pParam)+=psz;
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;}
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);
119 static const String Name (int pid); // for debugging - only works under NT
121 ContinuationFunc *m_pfnContinue;
122 void *m_pContinuationFuncParam;
123 static bool CALLBACK DefaultContinuationFunc(void *) { return true; }
125 static void CALLBACK NullLogFunc(void *,LPCTSTR) {}
136 bool IsChildOf(int pid) const;
139 typedef std::vector<PInfo> PInfoArray;
141 static bool PSExtract(PInfoArray &arPinfo);
142 static void SetParents(PInfoArray &arPinfo);
145 static DWORD GetPlatform();
148 HANDLE m_hProcess; // This handle is "owned" by the ThreadFunc
149 static HINSTANCE hInstLib1, hInstLib2;
156 static void CALLBACK SThreadFunc(void *pParam) { ((CSubprocess *)pParam)->ThreadFunc(); }
159 bool m_bThreadTerminated;
164 LogFunc *m_pfnLogfunc;
167 static const unsigned int PROCESS_KILL_EXIT_CODE;
168 bool CreateProcess(LPCTSTR pszCmdline);
170 struct CygProcessInfo {
178 void Output(LPCTSTR psz);