author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 12 Mar 2010 15:46:37 +0200 | |
branch | RCL_3 |
changeset 5 | d3bac044e0f0 |
parent 4 | 3b1da2848fc7 |
child 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtCore module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "qprocess.h" |
|
43 |
#include "qprocess_p.h" |
|
44 |
#include "qwindowspipewriter_p.h" |
|
45 |
||
46 |
#include <qdatetime.h> |
|
47 |
#include <qdir.h> |
|
48 |
#include <qfileinfo.h> |
|
49 |
#include <qtimer.h> |
|
50 |
#include <qthread.h> |
|
51 |
#include <qmutex.h> |
|
52 |
#include <qwaitcondition.h> |
|
53 |
#include <private/qwineventnotifier_p.h> |
|
54 |
#include <private/qthread_p.h> |
|
55 |
#include <qdebug.h> |
|
56 |
||
57 |
#include "private/qfsfileengine_p.h" // for longFileName |
|
58 |
||
59 |
||
60 |
#ifndef QT_NO_PROCESS |
|
61 |
||
62 |
QT_BEGIN_NAMESPACE |
|
63 |
||
64 |
//#define QPROCESS_DEBUG |
|
65 |
||
66 |
#define NOTIFYTIMEOUT 100 |
|
67 |
||
68 |
static void qt_create_pipe(Q_PIPE *pipe, bool in) |
|
69 |
{ |
|
70 |
// Open the pipes. Make non-inheritable copies of input write and output |
|
71 |
// read handles to avoid non-closable handles (this is done by the |
|
72 |
// DuplicateHandle() call). |
|
73 |
||
74 |
#if !defined(Q_OS_WINCE) |
|
75 |
SECURITY_ATTRIBUTES secAtt = { sizeof( SECURITY_ATTRIBUTES ), NULL, TRUE }; |
|
76 |
||
77 |
HANDLE tmpHandle; |
|
78 |
if (in) { // stdin |
|
79 |
if (!CreatePipe(&pipe[0], &tmpHandle, &secAtt, 1024 * 1024)) |
|
80 |
return; |
|
81 |
if (!DuplicateHandle(GetCurrentProcess(), tmpHandle, GetCurrentProcess(), |
|
82 |
&pipe[1], 0, FALSE, DUPLICATE_SAME_ACCESS)) |
|
83 |
return; |
|
84 |
} else { // stdout or stderr |
|
85 |
if (!CreatePipe(&tmpHandle, &pipe[1], &secAtt, 1024 * 1024)) |
|
86 |
return; |
|
87 |
if (!DuplicateHandle(GetCurrentProcess(), tmpHandle, GetCurrentProcess(), |
|
88 |
&pipe[0], 0, FALSE, DUPLICATE_SAME_ACCESS)) |
|
89 |
return; |
|
90 |
} |
|
91 |
||
92 |
CloseHandle(tmpHandle); |
|
93 |
#else |
|
94 |
Q_UNUSED(pipe); |
|
95 |
Q_UNUSED(in); |
|
96 |
#endif |
|
97 |
} |
|
98 |
||
99 |
/* |
|
100 |
Create the pipes to a QProcessPrivate::Channel. |
|
101 |
||
102 |
This function must be called in order: stdin, stdout, stderr |
|
103 |
*/ |
|
104 |
bool QProcessPrivate::createChannel(Channel &channel) |
|
105 |
{ |
|
106 |
Q_Q(QProcess); |
|
107 |
||
108 |
if (&channel == &stderrChannel && processChannelMode == QProcess::MergedChannels) { |
|
109 |
return DuplicateHandle(GetCurrentProcess(), stdoutChannel.pipe[1], GetCurrentProcess(), |
|
110 |
&stderrChannel.pipe[1], 0, TRUE, DUPLICATE_SAME_ACCESS); |
|
111 |
} |
|
112 |
||
113 |
if (channel.type == Channel::Normal) { |
|
114 |
// we're piping this channel to our own process |
|
115 |
qt_create_pipe(channel.pipe, &channel == &stdinChannel); |
|
116 |
||
117 |
return true; |
|
118 |
} else if (channel.type == Channel::Redirect) { |
|
119 |
// we're redirecting the channel to/from a file |
|
120 |
SECURITY_ATTRIBUTES secAtt = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE }; |
|
121 |
||
122 |
if (&channel == &stdinChannel) { |
|
123 |
// try to open in read-only mode |
|
124 |
channel.pipe[1] = INVALID_Q_PIPE; |
|
125 |
channel.pipe[0] = |
|
126 |
CreateFile((const wchar_t*)QFSFileEnginePrivate::longFileName(channel.file).utf16(), |
|
127 |
GENERIC_READ, |
|
128 |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
|
129 |
&secAtt, |
|
130 |
OPEN_EXISTING, |
|
131 |
FILE_ATTRIBUTE_NORMAL, |
|
132 |
NULL); |
|
133 |
||
134 |
if (channel.pipe[0] != INVALID_Q_PIPE) |
|
135 |
return true; |
|
136 |
||
137 |
q->setErrorString(QProcess::tr("Could not open input redirection for reading")); |
|
138 |
} else { |
|
139 |
// open in write mode |
|
140 |
channel.pipe[0] = INVALID_Q_PIPE; |
|
141 |
channel.pipe[1] = |
|
142 |
CreateFile((const wchar_t *)QFSFileEnginePrivate::longFileName(channel.file).utf16(), |
|
143 |
GENERIC_WRITE, |
|
144 |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
|
145 |
&secAtt, |
|
146 |
channel.append ? OPEN_ALWAYS : CREATE_ALWAYS, |
|
147 |
FILE_ATTRIBUTE_NORMAL, |
|
148 |
NULL); |
|
149 |
||
150 |
if (channel.pipe[1] != INVALID_Q_PIPE) { |
|
151 |
if (channel.append) { |
|
152 |
SetFilePointer(channel.pipe[1], 0, NULL, FILE_END); |
|
153 |
} |
|
154 |
return true; |
|
155 |
} |
|
156 |
||
157 |
q->setErrorString(QProcess::tr("Could not open output redirection for writing")); |
|
158 |
} |
|
159 |
||
160 |
// could not open file |
|
161 |
processError = QProcess::FailedToStart; |
|
162 |
emit q->error(processError); |
|
163 |
cleanup(); |
|
164 |
return false; |
|
165 |
} else { |
|
166 |
Q_ASSERT_X(channel.process, "QProcess::start", "Internal error"); |
|
167 |
||
168 |
Channel *source; |
|
169 |
Channel *sink; |
|
170 |
||
171 |
if (channel.type == Channel::PipeSource) { |
|
172 |
// we are the source |
|
173 |
source = &channel; |
|
174 |
sink = &channel.process->stdinChannel; |
|
175 |
||
176 |
if (source->pipe[1] != INVALID_Q_PIPE) { |
|
177 |
// already constructed by the sink |
|
178 |
// make it inheritable |
|
179 |
HANDLE tmpHandle = source->pipe[1]; |
|
180 |
if (!DuplicateHandle(GetCurrentProcess(), tmpHandle, |
|
181 |
GetCurrentProcess(), &source->pipe[1], |
|
182 |
0, TRUE, DUPLICATE_SAME_ACCESS)) |
|
183 |
return false; |
|
184 |
||
185 |
CloseHandle(tmpHandle); |
|
186 |
return true; |
|
187 |
} |
|
188 |
||
189 |
Q_ASSERT(source == &stdoutChannel); |
|
190 |
Q_ASSERT(sink->process == this && sink->type == Channel::PipeSink); |
|
191 |
||
192 |
qt_create_pipe(source->pipe, /* in = */ false); // source is stdout |
|
193 |
sink->pipe[0] = source->pipe[0]; |
|
194 |
source->pipe[0] = INVALID_Q_PIPE; |
|
195 |
||
196 |
return true; |
|
197 |
} else { |
|
198 |
// we are the sink; |
|
199 |
source = &channel.process->stdoutChannel; |
|
200 |
sink = &channel; |
|
201 |
||
202 |
if (sink->pipe[0] != INVALID_Q_PIPE) { |
|
203 |
// already constructed by the source |
|
204 |
// make it inheritable |
|
205 |
HANDLE tmpHandle = sink->pipe[0]; |
|
206 |
if (!DuplicateHandle(GetCurrentProcess(), tmpHandle, |
|
207 |
GetCurrentProcess(), &sink->pipe[0], |
|
208 |
0, TRUE, DUPLICATE_SAME_ACCESS)) |
|
209 |
return false; |
|
210 |
||
211 |
CloseHandle(tmpHandle); |
|
212 |
return true; |
|
213 |
} |
|
214 |
Q_ASSERT(sink == &stdinChannel); |
|
215 |
Q_ASSERT(source->process == this && source->type == Channel::PipeSource); |
|
216 |
||
217 |
qt_create_pipe(sink->pipe, /* in = */ true); // sink is stdin |
|
218 |
source->pipe[1] = sink->pipe[1]; |
|
219 |
sink->pipe[1] = INVALID_Q_PIPE; |
|
220 |
||
221 |
return true; |
|
222 |
} |
|
223 |
} |
|
224 |
} |
|
225 |
||
226 |
void QProcessPrivate::destroyPipe(Q_PIPE pipe[2]) |
|
227 |
{ |
|
228 |
if (pipe[0] == stdinChannel.pipe[0] && pipe[1] == stdinChannel.pipe[1] && pipeWriter) { |
|
229 |
delete pipeWriter; |
|
230 |
pipeWriter = 0; |
|
231 |
} |
|
232 |
||
233 |
if (pipe[0] != INVALID_Q_PIPE) { |
|
234 |
CloseHandle(pipe[0]); |
|
235 |
pipe[0] = INVALID_Q_PIPE; |
|
236 |
} |
|
237 |
if (pipe[1] != INVALID_Q_PIPE) { |
|
238 |
CloseHandle(pipe[1]); |
|
239 |
pipe[1] = INVALID_Q_PIPE; |
|
240 |
} |
|
241 |
} |
|
242 |
||
243 |
||
244 |
static QString qt_create_commandline(const QString &program, const QStringList &arguments) |
|
245 |
{ |
|
246 |
QString args; |
|
247 |
if (!program.isEmpty()) { |
|
248 |
QString programName = program; |
|
249 |
if (!programName.startsWith(QLatin1Char('\"')) && !programName.endsWith(QLatin1Char('\"')) && programName.contains(QLatin1Char(' '))) |
|
250 |
programName = QLatin1Char('\"') + programName + QLatin1Char('\"'); |
|
251 |
programName.replace(QLatin1Char('/'), QLatin1Char('\\')); |
|
252 |
||
253 |
// add the prgram as the first arg ... it works better |
|
254 |
args = programName + QLatin1Char(' '); |
|
255 |
} |
|
256 |
||
257 |
for (int i=0; i<arguments.size(); ++i) { |
|
258 |
QString tmp = arguments.at(i); |
|
259 |
// in the case of \" already being in the string the \ must also be escaped |
|
260 |
tmp.replace( QLatin1String("\\\""), QLatin1String("\\\\\"") ); |
|
261 |
// escape a single " because the arguments will be parsed |
|
262 |
tmp.replace( QLatin1Char('\"'), QLatin1String("\\\"") ); |
|
263 |
if (tmp.isEmpty() || tmp.contains(QLatin1Char(' ')) || tmp.contains(QLatin1Char('\t'))) { |
|
264 |
// The argument must not end with a \ since this would be interpreted |
|
265 |
// as escaping the quote -- rather put the \ behind the quote: e.g. |
|
266 |
// rather use "foo"\ than "foo\" |
|
267 |
QString endQuote(QLatin1Char('\"')); |
|
268 |
int i = tmp.length(); |
|
269 |
while (i>0 && tmp.at(i-1) == QLatin1Char('\\')) { |
|
270 |
--i; |
|
271 |
endQuote += QLatin1Char('\\'); |
|
272 |
} |
|
273 |
args += QLatin1String(" \"") + tmp.left(i) + endQuote; |
|
274 |
} else { |
|
275 |
args += QLatin1Char(' ') + tmp; |
|
276 |
} |
|
277 |
} |
|
278 |
return args; |
|
279 |
} |
|
280 |
||
281 |
static QByteArray qt_create_environment(const QHash<QString, QString> &environment) |
|
282 |
{ |
|
283 |
QByteArray envlist; |
|
284 |
if (!environment.isEmpty()) { |
|
285 |
QHash<QString, QString> copy = environment; |
|
286 |
||
287 |
// add PATH if necessary (for DLL loading) |
|
288 |
if (!copy.contains(QLatin1String("PATH"))) { |
|
289 |
QByteArray path = qgetenv("PATH"); |
|
290 |
if (!path.isEmpty()) |
|
291 |
copy.insert(QLatin1String("PATH"), QString::fromLocal8Bit(path)); |
|
292 |
} |
|
293 |
||
294 |
// add systemroot if needed |
|
295 |
if (!copy.contains(QLatin1String("SYSTEMROOT"))) { |
|
296 |
QByteArray systemRoot = qgetenv("SYSTEMROOT"); |
|
297 |
if (!systemRoot.isEmpty()) |
|
298 |
copy.insert(QLatin1String("SYSTEMROOT"), QString::fromLocal8Bit(systemRoot)); |
|
299 |
} |
|
300 |
||
301 |
int pos = 0; |
|
302 |
QHash<QString, QString>::ConstIterator it = copy.constBegin(), |
|
303 |
end = copy.constEnd(); |
|
304 |
||
305 |
static const wchar_t equal = L'='; |
|
306 |
static const wchar_t nul = L'\0'; |
|
307 |
||
308 |
for ( ; it != end; ++it) { |
|
309 |
uint tmpSize = sizeof(wchar_t) * (it.key().length() + it.value().length() + 2); |
|
310 |
// ignore empty strings |
|
311 |
if (tmpSize == sizeof(wchar_t) * 2) |
|
312 |
continue; |
|
313 |
envlist.resize(envlist.size() + tmpSize); |
|
314 |
||
315 |
tmpSize = it.key().length() * sizeof(wchar_t); |
|
316 |
memcpy(envlist.data()+pos, it.key().utf16(), tmpSize); |
|
317 |
pos += tmpSize; |
|
318 |
||
319 |
memcpy(envlist.data()+pos, &equal, sizeof(wchar_t)); |
|
320 |
pos += sizeof(wchar_t); |
|
321 |
||
322 |
tmpSize = it.value().length() * sizeof(wchar_t); |
|
323 |
memcpy(envlist.data()+pos, it.value().utf16(), tmpSize); |
|
324 |
pos += tmpSize; |
|
325 |
||
326 |
memcpy(envlist.data()+pos, &nul, sizeof(wchar_t)); |
|
327 |
pos += sizeof(wchar_t); |
|
328 |
} |
|
329 |
// add the 2 terminating 0 (actually 4, just to be on the safe side) |
|
330 |
envlist.resize( envlist.size()+4 ); |
|
331 |
envlist[pos++] = 0; |
|
332 |
envlist[pos++] = 0; |
|
333 |
envlist[pos++] = 0; |
|
334 |
envlist[pos++] = 0; |
|
335 |
} |
|
336 |
return envlist; |
|
337 |
} |
|
338 |
||
339 |
void QProcessPrivate::startProcess() |
|
340 |
{ |
|
341 |
Q_Q(QProcess); |
|
342 |
||
343 |
bool success = false; |
|
344 |
||
345 |
if (pid) { |
|
346 |
CloseHandle(pid->hThread); |
|
347 |
CloseHandle(pid->hProcess); |
|
348 |
delete pid; |
|
349 |
pid = 0; |
|
350 |
} |
|
351 |
pid = new PROCESS_INFORMATION; |
|
352 |
memset(pid, 0, sizeof(PROCESS_INFORMATION)); |
|
353 |
||
354 |
q->setProcessState(QProcess::Starting); |
|
355 |
||
356 |
if (!createChannel(stdinChannel) || |
|
357 |
!createChannel(stdoutChannel) || |
|
358 |
!createChannel(stderrChannel)) |
|
359 |
return; |
|
360 |
||
361 |
#if defined(Q_OS_WINCE) |
|
362 |
QString args = qt_create_commandline(QString(), arguments); |
|
363 |
#else |
|
364 |
QString args = qt_create_commandline(program, arguments); |
|
365 |
QByteArray envlist; |
|
366 |
if (environment.d.constData()) |
|
367 |
envlist = qt_create_environment(environment.d.constData()->hash); |
|
368 |
#endif |
|
369 |
||
370 |
#if defined QPROCESS_DEBUG |
|
371 |
qDebug("Creating process"); |
|
372 |
qDebug(" program : [%s]", program.toLatin1().constData()); |
|
373 |
qDebug(" args : %s", args.toLatin1().constData()); |
|
374 |
qDebug(" pass environment : %s", environment.isEmpty() ? "no" : "yes"); |
|
375 |
#endif |
|
376 |
||
377 |
#if defined(Q_OS_WINCE) |
|
378 |
QString fullPathProgram = program; |
|
379 |
if (!QDir::isAbsolutePath(fullPathProgram)) |
|
380 |
fullPathProgram = QFileInfo(fullPathProgram).absoluteFilePath(); |
|
381 |
fullPathProgram.replace(QLatin1Char('/'), QLatin1Char('\\')); |
|
382 |
success = CreateProcess((wchar_t*)fullPathProgram.utf16(), |
|
383 |
(wchar_t*)args.utf16(), |
|
384 |
0, 0, false, 0, 0, 0, 0, pid); |
|
385 |
#else |
|
386 |
DWORD dwCreationFlags = CREATE_NO_WINDOW; |
|
387 |
dwCreationFlags |= CREATE_UNICODE_ENVIRONMENT; |
|
388 |
STARTUPINFOW startupInfo = { sizeof( STARTUPINFO ), 0, 0, 0, |
|
389 |
(ulong)CW_USEDEFAULT, (ulong)CW_USEDEFAULT, |
|
390 |
(ulong)CW_USEDEFAULT, (ulong)CW_USEDEFAULT, |
|
391 |
0, 0, 0, |
|
392 |
STARTF_USESTDHANDLES, |
|
393 |
0, 0, 0, |
|
394 |
stdinChannel.pipe[0], stdoutChannel.pipe[1], stderrChannel.pipe[1] |
|
395 |
}; |
|
396 |
success = CreateProcess(0, (wchar_t*)args.utf16(), |
|
397 |
0, 0, TRUE, dwCreationFlags, |
|
398 |
environment.isEmpty() ? 0 : envlist.data(), |
|
399 |
workingDirectory.isEmpty() ? 0 : (wchar_t*)QDir::toNativeSeparators(workingDirectory).utf16(), |
|
400 |
&startupInfo, pid); |
|
401 |
if (!success) { |
|
402 |
// Capture the error string before we do CloseHandle below |
|
403 |
q->setErrorString(QProcess::tr("Process failed to start: %1").arg(qt_error_string())); |
|
404 |
} |
|
405 |
||
406 |
if (stdinChannel.pipe[0] != INVALID_Q_PIPE) { |
|
407 |
CloseHandle(stdinChannel.pipe[0]); |
|
408 |
stdinChannel.pipe[0] = INVALID_Q_PIPE; |
|
409 |
} |
|
410 |
if (stdoutChannel.pipe[1] != INVALID_Q_PIPE) { |
|
411 |
CloseHandle(stdoutChannel.pipe[1]); |
|
412 |
stdoutChannel.pipe[1] = INVALID_Q_PIPE; |
|
413 |
} |
|
414 |
if (stderrChannel.pipe[1] != INVALID_Q_PIPE) { |
|
415 |
CloseHandle(stderrChannel.pipe[1]); |
|
416 |
stderrChannel.pipe[1] = INVALID_Q_PIPE; |
|
417 |
} |
|
418 |
#endif // Q_OS_WINCE |
|
419 |
||
420 |
if (!success) { |
|
421 |
cleanup(); |
|
422 |
processError = QProcess::FailedToStart; |
|
423 |
emit q->error(processError); |
|
424 |
q->setProcessState(QProcess::NotRunning); |
|
425 |
return; |
|
426 |
} |
|
427 |
||
428 |
q->setProcessState(QProcess::Running); |
|
429 |
// User can call kill()/terminate() from the stateChanged() slot |
|
430 |
// so check before proceeding |
|
431 |
if (!pid) |
|
432 |
return; |
|
433 |
||
434 |
if (threadData->eventDispatcher) { |
|
435 |
processFinishedNotifier = new QWinEventNotifier(pid->hProcess, q); |
|
436 |
QObject::connect(processFinishedNotifier, SIGNAL(activated(HANDLE)), q, SLOT(_q_processDied())); |
|
437 |
processFinishedNotifier->setEnabled(true); |
|
438 |
notifier = new QTimer(q); |
|
439 |
QObject::connect(notifier, SIGNAL(timeout()), q, SLOT(_q_notified())); |
|
440 |
notifier->start(NOTIFYTIMEOUT); |
|
441 |
} |
|
442 |
||
443 |
// give the process a chance to start ... |
|
444 |
Sleep(SLEEPMIN * 2); |
|
445 |
_q_startupNotification(); |
|
446 |
} |
|
447 |
||
448 |
bool QProcessPrivate::processStarted() |
|
449 |
{ |
|
450 |
return processState == QProcess::Running; |
|
451 |
} |
|
452 |
||
453 |
qint64 QProcessPrivate::bytesAvailableFromStdout() const |
|
454 |
{ |
|
455 |
if (stdoutChannel.pipe[0] == INVALID_Q_PIPE) |
|
456 |
return 0; |
|
457 |
||
458 |
DWORD bytesAvail = 0; |
|
459 |
#if !defined(Q_OS_WINCE) |
|
460 |
PeekNamedPipe(stdoutChannel.pipe[0], 0, 0, 0, &bytesAvail, 0); |
|
461 |
#if defined QPROCESS_DEBUG |
|
462 |
qDebug("QProcessPrivate::bytesAvailableFromStdout() == %d", bytesAvail); |
|
463 |
#endif |
|
464 |
if (processChannelMode == QProcess::ForwardedChannels && bytesAvail > 0) { |
|
465 |
QByteArray buf(bytesAvail, 0); |
|
466 |
DWORD bytesRead = 0; |
|
467 |
if (ReadFile(stdoutChannel.pipe[0], buf.data(), buf.size(), &bytesRead, 0) && bytesRead > 0) { |
|
468 |
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); |
|
469 |
if (hStdout) { |
|
470 |
DWORD bytesWritten = 0; |
|
471 |
WriteFile(hStdout, buf.data(), bytesRead, &bytesWritten, 0); |
|
472 |
} |
|
473 |
} |
|
474 |
bytesAvail = 0; |
|
475 |
} |
|
476 |
#endif |
|
477 |
return bytesAvail; |
|
478 |
} |
|
479 |
||
480 |
qint64 QProcessPrivate::bytesAvailableFromStderr() const |
|
481 |
{ |
|
482 |
if (stderrChannel.pipe[0] == INVALID_Q_PIPE) |
|
483 |
return 0; |
|
484 |
||
485 |
DWORD bytesAvail = 0; |
|
486 |
#if !defined(Q_OS_WINCE) |
|
487 |
PeekNamedPipe(stderrChannel.pipe[0], 0, 0, 0, &bytesAvail, 0); |
|
488 |
#if defined QPROCESS_DEBUG |
|
489 |
qDebug("QProcessPrivate::bytesAvailableFromStderr() == %d", bytesAvail); |
|
490 |
#endif |
|
491 |
if (processChannelMode == QProcess::ForwardedChannels && bytesAvail > 0) { |
|
492 |
QByteArray buf(bytesAvail, 0); |
|
493 |
DWORD bytesRead = 0; |
|
494 |
if (ReadFile(stderrChannel.pipe[0], buf.data(), buf.size(), &bytesRead, 0) && bytesRead > 0) { |
|
495 |
HANDLE hStderr = GetStdHandle(STD_ERROR_HANDLE); |
|
496 |
if (hStderr) { |
|
497 |
DWORD bytesWritten = 0; |
|
498 |
WriteFile(hStderr, buf.data(), bytesRead, &bytesWritten, 0); |
|
499 |
} |
|
500 |
} |
|
501 |
bytesAvail = 0; |
|
502 |
} |
|
503 |
#endif |
|
504 |
return bytesAvail; |
|
505 |
} |
|
506 |
||
507 |
qint64 QProcessPrivate::readFromStdout(char *data, qint64 maxlen) |
|
508 |
{ |
|
509 |
DWORD read = qMin(maxlen, bytesAvailableFromStdout()); |
|
510 |
DWORD bytesRead = 0; |
|
511 |
||
512 |
if (read > 0 && !ReadFile(stdoutChannel.pipe[0], data, read, &bytesRead, 0)) |
|
513 |
return -1; |
|
514 |
return bytesRead; |
|
515 |
} |
|
516 |
||
517 |
qint64 QProcessPrivate::readFromStderr(char *data, qint64 maxlen) |
|
518 |
{ |
|
519 |
DWORD read = qMin(maxlen, bytesAvailableFromStderr()); |
|
520 |
DWORD bytesRead = 0; |
|
521 |
||
522 |
if (read > 0 && !ReadFile(stderrChannel.pipe[0], data, read, &bytesRead, 0)) |
|
523 |
return -1; |
|
524 |
return bytesRead; |
|
525 |
} |
|
526 |
||
527 |
||
528 |
static BOOL CALLBACK qt_terminateApp(HWND hwnd, LPARAM procId) |
|
529 |
{ |
|
530 |
DWORD currentProcId = 0; |
|
531 |
GetWindowThreadProcessId(hwnd, ¤tProcId); |
|
532 |
if (currentProcId == (DWORD)procId) |
|
533 |
PostMessage(hwnd, WM_CLOSE, 0, 0); |
|
534 |
||
535 |
return TRUE; |
|
536 |
} |
|
537 |
||
538 |
void QProcessPrivate::terminateProcess() |
|
539 |
{ |
|
540 |
if (pid) { |
|
541 |
EnumWindows(qt_terminateApp, (LPARAM)pid->dwProcessId); |
|
542 |
PostThreadMessage(pid->dwThreadId, WM_CLOSE, 0, 0); |
|
543 |
} |
|
544 |
} |
|
545 |
||
546 |
void QProcessPrivate::killProcess() |
|
547 |
{ |
|
548 |
if (pid) |
|
549 |
TerminateProcess(pid->hProcess, 0xf291); |
|
550 |
} |
|
551 |
||
552 |
bool QProcessPrivate::waitForStarted(int) |
|
553 |
{ |
|
554 |
Q_Q(QProcess); |
|
555 |
||
556 |
if (processStarted()) |
|
557 |
return true; |
|
558 |
||
559 |
if (processError == QProcess::FailedToStart) |
|
560 |
return false; |
|
561 |
||
562 |
processError = QProcess::Timedout; |
|
563 |
q->setErrorString(QProcess::tr("Process operation timed out")); |
|
564 |
return false; |
|
565 |
} |
|
566 |
||
567 |
bool QProcessPrivate::waitForReadyRead(int msecs) |
|
568 |
{ |
|
569 |
Q_Q(QProcess); |
|
570 |
||
571 |
#if defined(Q_OS_WINCE) |
|
572 |
processError = QProcess::ReadError; |
|
573 |
q->setErrorString(QProcess::tr("Error reading from process")); |
|
574 |
emit q->error(processError); |
|
575 |
return false; |
|
576 |
#endif |
|
577 |
||
578 |
QIncrementalSleepTimer timer(msecs); |
|
579 |
||
580 |
forever { |
|
581 |
if (!writeBuffer.isEmpty() && !_q_canWrite()) |
|
582 |
return false; |
|
583 |
if (pipeWriter && pipeWriter->waitForWrite(0)) |
|
584 |
timer.resetIncrements(); |
|
585 |
bool readyReadEmitted = false; |
|
586 |
if (bytesAvailableFromStdout() != 0) { |
|
587 |
readyReadEmitted = _q_canReadStandardOutput() ? true : readyReadEmitted; |
|
588 |
timer.resetIncrements(); |
|
589 |
} |
|
590 |
||
591 |
if (bytesAvailableFromStderr() != 0) { |
|
592 |
readyReadEmitted = _q_canReadStandardError() ? true : readyReadEmitted; |
|
593 |
timer.resetIncrements(); |
|
594 |
} |
|
595 |
||
596 |
if (readyReadEmitted) |
|
597 |
return true; |
|
598 |
||
599 |
if (!pid) |
|
600 |
return false; |
|
601 |
if (WaitForSingleObject(pid->hProcess, 0) == WAIT_OBJECT_0) { |
|
602 |
// find the return value if there is noew data to read |
|
603 |
_q_processDied(); |
|
604 |
return false; |
|
605 |
} |
|
606 |
||
607 |
Sleep(timer.nextSleepTime()); |
|
608 |
if (timer.hasTimedOut()) |
|
609 |
break; |
|
610 |
} |
|
611 |
||
612 |
processError = QProcess::Timedout; |
|
613 |
q->setErrorString(QProcess::tr("Process operation timed out")); |
|
614 |
return false; |
|
615 |
} |
|
616 |
||
617 |
bool QProcessPrivate::waitForBytesWritten(int msecs) |
|
618 |
{ |
|
619 |
Q_Q(QProcess); |
|
620 |
||
621 |
#if defined(Q_OS_WINCE) |
|
622 |
processError = QProcess::ReadError; |
|
623 |
q->setErrorString(QProcess::tr("Error reading from process")); |
|
624 |
emit q->error(processError); |
|
625 |
return false; |
|
626 |
#endif |
|
627 |
||
628 |
QIncrementalSleepTimer timer(msecs); |
|
629 |
||
630 |
forever { |
|
631 |
// Check if we have any data pending: the pipe writer has |
|
632 |
// bytes waiting to written, or it has written data since the |
|
633 |
// last time we called pipeWriter->waitForWrite(). |
|
634 |
bool pendingDataInPipe = pipeWriter && (pipeWriter->bytesToWrite() || pipeWriter->hadWritten()); |
|
635 |
||
636 |
// If we don't have pending data, and our write buffer is |
|
637 |
// empty, we fail. |
|
638 |
if (!pendingDataInPipe && writeBuffer.isEmpty()) |
|
639 |
return false; |
|
640 |
||
641 |
// If we don't have pending data and we do have data in our |
|
642 |
// write buffer, try to flush that data over to the pipe |
|
643 |
// writer. Fail on error. |
|
644 |
if (!pendingDataInPipe) { |
|
645 |
if (!_q_canWrite()) |
|
646 |
return false; |
|
647 |
} |
|
648 |
||
649 |
// Wait for the pipe writer to acknowledge that it has |
|
650 |
// written. This will succeed if either the pipe writer has |
|
651 |
// already written the data, or if it manages to write data |
|
652 |
// within the given timeout. If the write buffer was non-empty |
|
653 |
// and the pipeWriter is now dead, that means _q_canWrite() |
|
654 |
// destroyed the writer after it successfully wrote the last |
|
655 |
// batch. |
|
656 |
if (!pipeWriter || pipeWriter->waitForWrite(0)) |
|
657 |
return true; |
|
658 |
||
659 |
// If we wouldn't write anything, check if we can read stdout. |
|
660 |
if (bytesAvailableFromStdout() != 0) { |
|
661 |
_q_canReadStandardOutput(); |
|
662 |
timer.resetIncrements(); |
|
663 |
} |
|
664 |
||
665 |
// Check if we can read stderr. |
|
666 |
if (bytesAvailableFromStderr() != 0) { |
|
667 |
_q_canReadStandardError(); |
|
668 |
timer.resetIncrements(); |
|
669 |
} |
|
670 |
||
671 |
// Check if the process died while reading. |
|
672 |
if (!pid) |
|
673 |
return false; |
|
674 |
||
675 |
// Wait for the process to signal any change in its state, |
|
676 |
// such as incoming data, or if the process died. |
|
677 |
if (WaitForSingleObject(pid->hProcess, 0) == WAIT_OBJECT_0) { |
|
678 |
_q_processDied(); |
|
679 |
return false; |
|
680 |
} |
|
681 |
||
682 |
// Only wait for as long as we've been asked. |
|
683 |
if (timer.hasTimedOut()) |
|
684 |
break; |
|
685 |
} |
|
686 |
||
687 |
processError = QProcess::Timedout; |
|
688 |
q->setErrorString(QProcess::tr("Process operation timed out")); |
|
689 |
return false; |
|
690 |
} |
|
691 |
||
692 |
||
693 |
bool QProcessPrivate::waitForFinished(int msecs) |
|
694 |
{ |
|
695 |
Q_Q(QProcess); |
|
696 |
#if defined QPROCESS_DEBUG |
|
697 |
qDebug("QProcessPrivate::waitForFinished(%d)", msecs); |
|
698 |
#endif |
|
699 |
||
700 |
QIncrementalSleepTimer timer(msecs); |
|
701 |
||
702 |
forever { |
|
703 |
if (!writeBuffer.isEmpty() && !_q_canWrite()) |
|
704 |
return false; |
|
705 |
if (pipeWriter && pipeWriter->waitForWrite(0)) |
|
706 |
timer.resetIncrements(); |
|
707 |
||
708 |
if (bytesAvailableFromStdout() != 0) { |
|
709 |
_q_canReadStandardOutput(); |
|
710 |
timer.resetIncrements(); |
|
711 |
} |
|
712 |
||
713 |
if (bytesAvailableFromStderr() != 0) { |
|
714 |
_q_canReadStandardError(); |
|
715 |
timer.resetIncrements(); |
|
716 |
} |
|
717 |
||
718 |
if (!pid) |
|
719 |
return true; |
|
720 |
||
721 |
if (WaitForSingleObject(pid->hProcess, timer.nextSleepTime()) == WAIT_OBJECT_0) { |
|
722 |
_q_processDied(); |
|
723 |
return true; |
|
724 |
} |
|
725 |
||
726 |
if (timer.hasTimedOut()) |
|
727 |
break; |
|
728 |
} |
|
729 |
processError = QProcess::Timedout; |
|
730 |
q->setErrorString(QProcess::tr("Process operation timed out")); |
|
731 |
return false; |
|
732 |
} |
|
733 |
||
734 |
||
735 |
void QProcessPrivate::findExitCode() |
|
736 |
{ |
|
737 |
DWORD theExitCode; |
|
738 |
if (GetExitCodeProcess(pid->hProcess, &theExitCode)) { |
|
739 |
exitCode = theExitCode; |
|
740 |
//### for now we assume a crash if exit code is less than -1 or the magic number |
|
741 |
crashed = (exitCode == 0xf291 || (int)exitCode < 0); |
|
742 |
} |
|
743 |
} |
|
744 |
||
745 |
void QProcessPrivate::flushPipeWriter() |
|
746 |
{ |
|
747 |
if (pipeWriter && pipeWriter->bytesToWrite() > 0) { |
|
748 |
pipeWriter->waitForWrite(ULONG_MAX); |
|
749 |
} |
|
750 |
} |
|
751 |
||
752 |
qint64 QProcessPrivate::pipeWriterBytesToWrite() const |
|
753 |
{ |
|
754 |
return pipeWriter ? pipeWriter->bytesToWrite() : qint64(0); |
|
755 |
} |
|
756 |
||
757 |
qint64 QProcessPrivate::writeToStdin(const char *data, qint64 maxlen) |
|
758 |
{ |
|
759 |
Q_Q(QProcess); |
|
760 |
||
761 |
#if defined(Q_OS_WINCE) |
|
762 |
processError = QProcess::WriteError; |
|
763 |
q->setErrorString(QProcess::tr("Error writing to process")); |
|
764 |
emit q->error(processError); |
|
765 |
return -1; |
|
766 |
#endif |
|
767 |
||
768 |
if (!pipeWriter) { |
|
769 |
pipeWriter = new QWindowsPipeWriter(stdinChannel.pipe[1], q); |
|
770 |
pipeWriter->start(); |
|
771 |
} |
|
772 |
||
773 |
return pipeWriter->write(data, maxlen); |
|
774 |
} |
|
775 |
||
776 |
bool QProcessPrivate::waitForWrite(int msecs) |
|
777 |
{ |
|
778 |
Q_Q(QProcess); |
|
779 |
||
780 |
if (!pipeWriter || pipeWriter->waitForWrite(msecs)) |
|
781 |
return true; |
|
782 |
||
783 |
processError = QProcess::Timedout; |
|
784 |
q->setErrorString(QProcess::tr("Process operation timed out")); |
|
785 |
return false; |
|
786 |
} |
|
787 |
||
788 |
void QProcessPrivate::_q_notified() |
|
789 |
{ |
|
790 |
notifier->stop(); |
|
791 |
||
792 |
if (!writeBuffer.isEmpty() && (!pipeWriter || pipeWriter->waitForWrite(0))) |
|
793 |
_q_canWrite(); |
|
794 |
||
795 |
if (bytesAvailableFromStdout()) |
|
796 |
_q_canReadStandardOutput(); |
|
797 |
||
798 |
if (bytesAvailableFromStderr()) |
|
799 |
_q_canReadStandardError(); |
|
800 |
||
801 |
if (processState != QProcess::NotRunning) |
|
802 |
notifier->start(NOTIFYTIMEOUT); |
|
803 |
} |
|
804 |
||
805 |
bool QProcessPrivate::startDetached(const QString &program, const QStringList &arguments, const QString &workingDir, qint64 *pid) |
|
806 |
{ |
|
807 |
#if defined(Q_OS_WINCE) |
|
808 |
Q_UNUSED(workingDir); |
|
809 |
QString args = qt_create_commandline(QString(), arguments); |
|
810 |
#else |
|
811 |
QString args = qt_create_commandline(program, arguments); |
|
812 |
#endif |
|
813 |
||
814 |
bool success = false; |
|
815 |
||
816 |
PROCESS_INFORMATION pinfo; |
|
817 |
||
818 |
#if defined(Q_OS_WINCE) |
|
819 |
QString fullPathProgram = program; |
|
820 |
if (!QDir::isAbsolutePath(fullPathProgram)) |
|
821 |
fullPathProgram.prepend(QDir::currentPath().append(QLatin1Char('/'))); |
|
822 |
fullPathProgram.replace(QLatin1Char('/'), QLatin1Char('\\')); |
|
823 |
success = CreateProcess((wchar_t*)fullPathProgram.utf16(), |
|
824 |
(wchar_t*)args.utf16(), |
|
825 |
0, 0, false, CREATE_NEW_CONSOLE, 0, 0, 0, &pinfo); |
|
826 |
#else |
|
827 |
STARTUPINFOW startupInfo = { sizeof( STARTUPINFO ), 0, 0, 0, |
|
828 |
(ulong)CW_USEDEFAULT, (ulong)CW_USEDEFAULT, |
|
829 |
(ulong)CW_USEDEFAULT, (ulong)CW_USEDEFAULT, |
|
830 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
|
831 |
}; |
|
832 |
success = CreateProcess(0, (wchar_t*)args.utf16(), |
|
833 |
0, 0, FALSE, CREATE_UNICODE_ENVIRONMENT | CREATE_NEW_CONSOLE, 0, |
|
834 |
workingDir.isEmpty() ? 0 : (wchar_t*)workingDir.utf16(), |
|
835 |
&startupInfo, &pinfo); |
|
836 |
#endif // Q_OS_WINCE |
|
837 |
||
838 |
if (success) { |
|
839 |
CloseHandle(pinfo.hThread); |
|
840 |
CloseHandle(pinfo.hProcess); |
|
841 |
if (pid) |
|
842 |
*pid = pinfo.dwProcessId; |
|
843 |
} |
|
844 |
||
845 |
return success; |
|
846 |
} |
|
847 |
||
848 |
QT_END_NAMESPACE |
|
849 |
||
850 |
#endif // QT_NO_PROCESS |