1 | // |
---|
2 | // Process_WIN32.cpp |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/src/Process_WIN32.cpp#2 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Processes |
---|
8 | // Module: Process |
---|
9 | // |
---|
10 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
11 | // and Contributors. |
---|
12 | // |
---|
13 | // Permission is hereby granted, free of charge, to any person or organization |
---|
14 | // obtaining a copy of the software and accompanying documentation covered by |
---|
15 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
16 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
17 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
18 | // do so, all subject to the following: |
---|
19 | // |
---|
20 | // The copyright notices in the Software and this entire statement, including |
---|
21 | // the above license grant, this restriction and the following disclaimer, |
---|
22 | // must be included in all copies of the Software, in whole or in part, and |
---|
23 | // all derivative works of the Software, unless such copies or derivative |
---|
24 | // works are solely in the form of machine-executable object code generated by |
---|
25 | // a source language processor. |
---|
26 | // |
---|
27 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
28 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
29 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
30 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
31 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
32 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
33 | // DEALINGS IN THE SOFTWARE. |
---|
34 | // |
---|
35 | |
---|
36 | |
---|
37 | #include "Poco/Process_WIN32.h" |
---|
38 | #include "Poco/Exception.h" |
---|
39 | #include "Poco/NumberFormatter.h" |
---|
40 | #include "Poco/NamedEvent.h" |
---|
41 | #include "Poco/Pipe.h" |
---|
42 | |
---|
43 | |
---|
44 | namespace Poco { |
---|
45 | |
---|
46 | |
---|
47 | // |
---|
48 | // ProcessHandleImpl |
---|
49 | // |
---|
50 | ProcessHandleImpl::ProcessHandleImpl(HANDLE hProcess, UInt32 pid): |
---|
51 | _hProcess(hProcess), |
---|
52 | _pid(pid) |
---|
53 | { |
---|
54 | } |
---|
55 | |
---|
56 | |
---|
57 | ProcessHandleImpl::~ProcessHandleImpl() |
---|
58 | { |
---|
59 | CloseHandle(_hProcess); |
---|
60 | } |
---|
61 | |
---|
62 | |
---|
63 | UInt32 ProcessHandleImpl::id() const |
---|
64 | { |
---|
65 | return _pid; |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | int ProcessHandleImpl::wait() const |
---|
70 | { |
---|
71 | DWORD rc = WaitForSingleObject(_hProcess, INFINITE); |
---|
72 | if (rc != WAIT_OBJECT_0) |
---|
73 | throw SystemException("Wait failed for process", NumberFormatter::format(_pid)); |
---|
74 | |
---|
75 | DWORD exitCode; |
---|
76 | if (GetExitCodeProcess(_hProcess, &exitCode) == 0) |
---|
77 | throw SystemException("Cannot get exit code for process", NumberFormatter::format(_pid)); |
---|
78 | |
---|
79 | return exitCode; |
---|
80 | } |
---|
81 | |
---|
82 | |
---|
83 | // |
---|
84 | // ProcessImpl |
---|
85 | // |
---|
86 | ProcessImpl::PIDImpl ProcessImpl::idImpl() |
---|
87 | { |
---|
88 | return GetCurrentProcessId(); |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | void ProcessImpl::timesImpl(long& userTime, long& kernelTime) |
---|
93 | { |
---|
94 | FILETIME ftCreation; |
---|
95 | FILETIME ftExit; |
---|
96 | FILETIME ftKernel; |
---|
97 | FILETIME ftUser; |
---|
98 | |
---|
99 | if (GetProcessTimes(GetCurrentProcess(), &ftCreation, &ftExit, &ftKernel, &ftUser) != 0) |
---|
100 | { |
---|
101 | ULARGE_INTEGER time; |
---|
102 | time.LowPart = ftKernel.dwLowDateTime; |
---|
103 | time.HighPart = ftKernel.dwHighDateTime; |
---|
104 | kernelTime = long(time.QuadPart/10000000L); |
---|
105 | time.LowPart = ftUser.dwLowDateTime; |
---|
106 | time.HighPart = ftUser.dwHighDateTime; |
---|
107 | userTime = long(time.QuadPart/10000000L); |
---|
108 | } |
---|
109 | else |
---|
110 | { |
---|
111 | userTime = kernelTime = -1; |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | |
---|
116 | ProcessHandleImpl* ProcessImpl::launchImpl(const std::string& command, const ArgsImpl& args, Pipe* inPipe, Pipe* outPipe, Pipe* errPipe) |
---|
117 | { |
---|
118 | std::string commandLine = command; |
---|
119 | for (ArgsImpl::const_iterator it = args.begin(); it != args.end(); ++it) |
---|
120 | { |
---|
121 | commandLine.append(" "); |
---|
122 | commandLine.append(*it); |
---|
123 | } |
---|
124 | |
---|
125 | STARTUPINFO startupInfo; |
---|
126 | GetStartupInfo(&startupInfo); // take defaults from current process |
---|
127 | startupInfo.cb = sizeof(STARTUPINFO); |
---|
128 | startupInfo.lpReserved = NULL; |
---|
129 | startupInfo.lpDesktop = NULL; |
---|
130 | startupInfo.lpTitle = NULL; |
---|
131 | startupInfo.dwFlags = STARTF_FORCEOFFFEEDBACK | STARTF_USESTDHANDLES; |
---|
132 | startupInfo.cbReserved2 = 0; |
---|
133 | startupInfo.lpReserved2 = NULL; |
---|
134 | |
---|
135 | HANDLE hProc = GetCurrentProcess(); |
---|
136 | if (inPipe) |
---|
137 | { |
---|
138 | DuplicateHandle(hProc, inPipe->readHandle(), hProc, &startupInfo.hStdInput, 0, TRUE, DUPLICATE_SAME_ACCESS); |
---|
139 | inPipe->close(Pipe::CLOSE_READ); |
---|
140 | } |
---|
141 | else DuplicateHandle(hProc, GetStdHandle(STD_INPUT_HANDLE), hProc, &startupInfo.hStdInput, 0, TRUE, DUPLICATE_SAME_ACCESS); |
---|
142 | // outPipe may be the same as errPipe, so we duplicate first and close later. |
---|
143 | if (outPipe) |
---|
144 | DuplicateHandle(hProc, outPipe->writeHandle(), hProc, &startupInfo.hStdOutput, 0, TRUE, DUPLICATE_SAME_ACCESS); |
---|
145 | else |
---|
146 | DuplicateHandle(hProc, GetStdHandle(STD_OUTPUT_HANDLE), hProc, &startupInfo.hStdOutput, 0, TRUE, DUPLICATE_SAME_ACCESS); |
---|
147 | if (errPipe) |
---|
148 | DuplicateHandle(hProc, errPipe->writeHandle(), hProc, &startupInfo.hStdError, 0, TRUE, DUPLICATE_SAME_ACCESS); |
---|
149 | else |
---|
150 | DuplicateHandle(hProc, GetStdHandle(STD_ERROR_HANDLE), hProc, &startupInfo.hStdError, 0, TRUE, DUPLICATE_SAME_ACCESS); |
---|
151 | if (outPipe) outPipe->close(Pipe::CLOSE_WRITE); |
---|
152 | if (errPipe) errPipe->close(Pipe::CLOSE_WRITE); |
---|
153 | |
---|
154 | PROCESS_INFORMATION processInfo; |
---|
155 | BOOL rc = CreateProcessA( |
---|
156 | NULL, |
---|
157 | const_cast<char*>(commandLine.c_str()), |
---|
158 | NULL, |
---|
159 | NULL, |
---|
160 | TRUE, |
---|
161 | 0, |
---|
162 | NULL, |
---|
163 | NULL, |
---|
164 | &startupInfo, |
---|
165 | &processInfo |
---|
166 | ); |
---|
167 | CloseHandle(startupInfo.hStdInput); |
---|
168 | CloseHandle(startupInfo.hStdOutput); |
---|
169 | CloseHandle(startupInfo.hStdError); |
---|
170 | if (rc) |
---|
171 | { |
---|
172 | CloseHandle(processInfo.hThread); |
---|
173 | return new ProcessHandleImpl(processInfo.hProcess, processInfo.dwProcessId); |
---|
174 | } |
---|
175 | else throw SystemException("Cannot launch process", command); |
---|
176 | } |
---|
177 | |
---|
178 | |
---|
179 | void ProcessImpl::killImpl(PIDImpl pid) |
---|
180 | { |
---|
181 | HANDLE hProc = OpenProcess(PROCESS_TERMINATE, FALSE, pid); |
---|
182 | if (hProc) |
---|
183 | { |
---|
184 | if (TerminateProcess(hProc, 0) == 0) |
---|
185 | { |
---|
186 | CloseHandle(hProc); |
---|
187 | throw SystemException("cannot kill process"); |
---|
188 | } |
---|
189 | CloseHandle(hProc); |
---|
190 | } |
---|
191 | else |
---|
192 | { |
---|
193 | switch (GetLastError()) |
---|
194 | { |
---|
195 | case ERROR_ACCESS_DENIED: |
---|
196 | throw NoPermissionException("cannot kill process"); |
---|
197 | case ERROR_NOT_FOUND: |
---|
198 | throw NotFoundException("cannot kill process"); |
---|
199 | default: |
---|
200 | throw SystemException("cannot kill process"); |
---|
201 | } |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | |
---|
206 | void ProcessImpl::requestTerminationImpl(PIDImpl pid) |
---|
207 | { |
---|
208 | std::string evName("POCOTRM"); |
---|
209 | evName.append(NumberFormatter::formatHex(pid, 8)); |
---|
210 | NamedEvent ev(evName); |
---|
211 | ev.set(); |
---|
212 | } |
---|
213 | |
---|
214 | |
---|
215 | } // namespace Poco |
---|