Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/io/qprocess_unix.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||
2 | ** | - | ||||||||||||
3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||
4 | ** Copyright (C) 2016 Intel Corporation. | - | ||||||||||||
5 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||
6 | ** | - | ||||||||||||
7 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||||||||
8 | ** | - | ||||||||||||
9 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||||||||
10 | ** Commercial License Usage | - | ||||||||||||
11 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
12 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
13 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
14 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
15 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||
16 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||||||||
17 | ** | - | ||||||||||||
18 | ** GNU Lesser General Public License Usage | - | ||||||||||||
19 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
20 | ** General Public License version 3 as published by the Free Software | - | ||||||||||||
21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||
22 | ** packaging of this file. Please review the following information to | - | ||||||||||||
23 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||
24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||
25 | ** | - | ||||||||||||
26 | ** GNU General Public License Usage | - | ||||||||||||
27 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||
28 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||
29 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||
30 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||
31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||
32 | ** included in the packaging of this file. Please review the following | - | ||||||||||||
33 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||
34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||
35 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||
36 | ** | - | ||||||||||||
37 | ** $QT_END_LICENSE$ | - | ||||||||||||
38 | ** | - | ||||||||||||
39 | ****************************************************************************/ | - | ||||||||||||
40 | - | |||||||||||||
41 | //#define QPROCESS_DEBUG | - | ||||||||||||
42 | #include "qdebug.h" | - | ||||||||||||
43 | - | |||||||||||||
44 | #ifndef QT_NO_PROCESS | - | ||||||||||||
45 | - | |||||||||||||
46 | #if defined QPROCESS_DEBUG | - | ||||||||||||
47 | #include "private/qtools_p.h" | - | ||||||||||||
48 | #include <ctype.h> | - | ||||||||||||
49 | - | |||||||||||||
50 | /* | - | ||||||||||||
51 | Returns a human readable representation of the first \a len | - | ||||||||||||
52 | characters in \a data. | - | ||||||||||||
53 | */ | - | ||||||||||||
54 | QT_BEGIN_NAMESPACE | - | ||||||||||||
55 | static QByteArray qt_prettyDebug(const char *data, int len, int maxSize) | - | ||||||||||||
56 | { | - | ||||||||||||
57 | if (!data) return "(null)"; | - | ||||||||||||
58 | QByteArray out; | - | ||||||||||||
59 | for (int i = 0; i < len; ++i) { | - | ||||||||||||
60 | char c = data[i]; | - | ||||||||||||
61 | if (isprint(c)) { | - | ||||||||||||
62 | out += c; | - | ||||||||||||
63 | } else switch (c) { | - | ||||||||||||
64 | case '\n': out += "\\n"; break; | - | ||||||||||||
65 | case '\r': out += "\\r"; break; | - | ||||||||||||
66 | case '\t': out += "\\t"; break; | - | ||||||||||||
67 | default: { | - | ||||||||||||
68 | const char buf[] = { | - | ||||||||||||
69 | '\\', | - | ||||||||||||
70 | QtMiscUtils::toOct(uchar(c) / 64), | - | ||||||||||||
71 | QtMiscUtils::toOct(uchar(c) % 64 / 8), | - | ||||||||||||
72 | QtMiscUtils::toOct(uchar(c) % 8), | - | ||||||||||||
73 | 0 | - | ||||||||||||
74 | }; | - | ||||||||||||
75 | out += buf; | - | ||||||||||||
76 | } | - | ||||||||||||
77 | } | - | ||||||||||||
78 | } | - | ||||||||||||
79 | - | |||||||||||||
80 | if (len < maxSize) | - | ||||||||||||
81 | out += "..."; | - | ||||||||||||
82 | - | |||||||||||||
83 | return out; | - | ||||||||||||
84 | } | - | ||||||||||||
85 | QT_END_NAMESPACE | - | ||||||||||||
86 | #endif | - | ||||||||||||
87 | - | |||||||||||||
88 | #include "qplatformdefs.h" | - | ||||||||||||
89 | - | |||||||||||||
90 | #include "qprocess.h" | - | ||||||||||||
91 | #include "qprocess_p.h" | - | ||||||||||||
92 | #include "private/qcore_unix_p.h" | - | ||||||||||||
93 | - | |||||||||||||
94 | #ifdef Q_OS_MAC | - | ||||||||||||
95 | #include <private/qcore_mac_p.h> | - | ||||||||||||
96 | #endif | - | ||||||||||||
97 | - | |||||||||||||
98 | #include <private/qcoreapplication_p.h> | - | ||||||||||||
99 | #include <private/qthread_p.h> | - | ||||||||||||
100 | #include <qfile.h> | - | ||||||||||||
101 | #include <qfileinfo.h> | - | ||||||||||||
102 | #include <qdir.h> | - | ||||||||||||
103 | #include <qlist.h> | - | ||||||||||||
104 | #include <qmutex.h> | - | ||||||||||||
105 | #include <qsemaphore.h> | - | ||||||||||||
106 | #include <qsocketnotifier.h> | - | ||||||||||||
107 | #include <qthread.h> | - | ||||||||||||
108 | #include <qelapsedtimer.h> | - | ||||||||||||
109 | - | |||||||||||||
110 | #ifdef Q_OS_QNX | - | ||||||||||||
111 | # include <sys/neutrino.h> | - | ||||||||||||
112 | #endif | - | ||||||||||||
113 | - | |||||||||||||
114 | #include <errno.h> | - | ||||||||||||
115 | #include <stdlib.h> | - | ||||||||||||
116 | #include <string.h> | - | ||||||||||||
117 | #include <forkfd.h> | - | ||||||||||||
118 | - | |||||||||||||
119 | QT_BEGIN_NAMESPACE | - | ||||||||||||
120 | - | |||||||||||||
121 | // POSIX requires PIPE_BUF to be 512 or larger | - | ||||||||||||
122 | // so we will use 512 | - | ||||||||||||
123 | static const int errorBufferMax = 512; | - | ||||||||||||
124 | - | |||||||||||||
static inline void add_fdnamespace { | ||||||||||||||
126 | struct QProcessPoller | - | ||||||||||||
127 | { | - | ||||||||||||
128 | QProcessPoller(const QProcessPrivate &proc); | - | ||||||||||||
129 | - | |||||||||||||
130 | int &nfds,poll(int fd, fd_set *fdsettimeout); | - | ||||||||||||
131 | - | |||||||||||||
132 | pollfd &stdinPipe() { return pfds[0]; } executed 48656 times by 30 tests: return pfds[0]; Executed by:
| 48656 | ||||||||||||
133 | pollfd &stdoutPipe() { return pfds[1]; } executed 81622 times by 30 tests: return pfds[1]; Executed by:
| 81622 | ||||||||||||
134 | pollfd &stderrPipe() { return pfds[2]; } executed 81622 times by 30 tests: return pfds[2]; Executed by:
| 81622 | ||||||||||||
135 | pollfd &forkfd() { return pfds[3]; } executed 81031 times by 30 tests: return pfds[3]; Executed by:
| 81031 | ||||||||||||
136 | pollfd &childStartedPipe() { return pfds[4]; } executed 85000 times by 30 tests: return pfds[4]; Executed by:
| 85000 | ||||||||||||
137 | - | |||||||||||||
138 | enum { n_pfds = 5 }; | - | ||||||||||||
139 | pollfd pfds[n_pfds]; | - | ||||||||||||
140 | }; | - | ||||||||||||
141 | - | |||||||||||||
142 | QProcessPoller::QProcessPoller(const QProcessPrivate &proc) | - | ||||||||||||
143 | { | - | ||||||||||||
144 | FD_SETfor (fdint i = 0; i < n_pfds; i++)
| 42448-212240 | ||||||||||||
145 | pfds[i] = qt_make_pollfd(-1 executed 212240 times by 30 tests: , fdsetPOLLIN);pfds[i] = qt_make_pollfd(-1, 0x001); Executed by:
executed 212240 times by 30 tests: pfds[i] = qt_make_pollfd(-1, 0x001); Executed by:
| 212240 | ||||||||||||
146 | - | |||||||||||||
147 | stdoutPipe().fd = proc.stdoutChannel.pipe[0]; | - | ||||||||||||
148 | stderrPipe().fd = proc.stderrChannel.pipe[0]; | - | ||||||||||||
149 | - | |||||||||||||
150 | if (((!proc.writeBuffer.isEmpty()) {
| 3456-38992 | ||||||||||||
151 | stdinPipe().fd = proc.stdinChannel.pipe[1]; | - | ||||||||||||
152 | stdinPipe().events = POLLOUT; | - | ||||||||||||
153 | } executed 3456 times by 5 tests: end of block Executed by:
| 3456 | ||||||||||||
154 | - | |||||||||||||
155 | forkfd().fd = proc.forkfd; | - | ||||||||||||
156 | - | |||||||||||||
157 | if (proc.processState == QProcess::Starting
| 115-42333 | ||||||||||||
158 | > nfdschildStartedPipe().fd = proc.childStartedPipe[0]; executed 115 times by 4 tests: childStartedPipe().fd = proc.childStartedPipe[0]; Executed by:
| 115 | ||||||||||||
159 | } executed 42448 times by 30 tests: end of block Executed by:
| 42448 | ||||||||||||
160 | - | |||||||||||||
161 | int QProcessPoller::poll(int timeout) | - | ||||||||||||
162 | { | - | ||||||||||||
163 | const nfds_t
| 115-42333 | ||||||||||||
164 | return qt_poll_msecs(pfds, nfds, timeout); executed 42448 times by 30 tests: return qt_poll_msecs(pfds, nfds, timeout); Executed by:
| 42448 | ||||||||||||
165 | } | - | ||||||||||||
166 | } // anonymous namespace | - | ||||||||||||
167 | - | |||||||||||||
168 | static bool qt_pollfd_check(const pollfd &pfd, short revents) | - | ||||||||||||
169 | { | - | ||||||||||||
170 | return pfd. executed 201112 times by 30 tests: fd >= 0 && (pfd.revents & (revents | POLLHUP | POLLERR | POLLNVAL)) != 0;return pfd.fd >= 0 && (pfd.revents & (revents | 0x010 | 0x008 | 0x020)) != 0; Executed by:
executed 201112 times by 30 tests: return pfd.fd >= 0 && (pfd.revents & (revents | 0x010 | 0x008 | 0x020)) != 0; Executed by:
| 201112 | ||||||||||||
171 | } | - | ||||||||||||
172 | - | |||||||||||||
173 | static int qt_create_pipe(int *pipe) | - | ||||||||||||
174 | { | - | ||||||||||||
175 | if (pipe[0] != -1) | - | ||||||||||||
176 | qt_safe_close(pipe[0]); | - | ||||||||||||
177 | if (pipe[1] != -1) | - | ||||||||||||
178 | qt_safe_close(pipe[1]); | - | ||||||||||||
179 | int pipe_ret = qt_safe_pipe(pipe); | - | ||||||||||||
180 | if (pipe_ret != 0) { | - | ||||||||||||
181 | qWarning("QProcessPrivate::createPipe: Cannot create pipe %p: %s", | - | ||||||||||||
182 | pipe, qPrintable(qt_error_string(errno))); | - | ||||||||||||
183 | } | - | ||||||||||||
184 | return pipe_ret; | - | ||||||||||||
185 | } | - | ||||||||||||
186 | - | |||||||||||||
187 | void QProcessPrivate::destroyPipe(int *pipe) | - | ||||||||||||
188 | { | - | ||||||||||||
189 | if (pipe[1] != -1) { | - | ||||||||||||
190 | qt_safe_close(pipe[1]); | - | ||||||||||||
191 | pipe[1] = -1; | - | ||||||||||||
192 | } | - | ||||||||||||
193 | if (pipe[0] != -1) { | - | ||||||||||||
194 | qt_safe_close(pipe[0]); | - | ||||||||||||
195 | pipe[0] = -1; | - | ||||||||||||
196 | } | - | ||||||||||||
197 | } | - | ||||||||||||
198 | - | |||||||||||||
199 | void QProcessPrivate::closeChannel(Channel *channel) | - | ||||||||||||
200 | { | - | ||||||||||||
201 | destroyPipe(channel->pipe); | - | ||||||||||||
202 | } | - | ||||||||||||
203 | - | |||||||||||||
204 | /* | - | ||||||||||||
205 | Create the pipes to a QProcessPrivate::Channel. | - | ||||||||||||
206 | - | |||||||||||||
207 | This function must be called in order: stdin, stdout, stderr | - | ||||||||||||
208 | */ | - | ||||||||||||
209 | bool QProcessPrivate::openChannel(Channel &channel) | - | ||||||||||||
210 | { | - | ||||||||||||
211 | Q_Q(QProcess); | - | ||||||||||||
212 | - | |||||||||||||
213 | if (&channel == &stderrChannel && processChannelMode == QProcess::MergedChannels) { | - | ||||||||||||
214 | channel.pipe[0] = -1; | - | ||||||||||||
215 | channel.pipe[1] = -1; | - | ||||||||||||
216 | return true; | - | ||||||||||||
217 | } | - | ||||||||||||
218 | - | |||||||||||||
219 | if (channel.type == Channel::Normal) { | - | ||||||||||||
220 | // we're piping this channel to our own process | - | ||||||||||||
221 | if (qt_create_pipe(channel.pipe) != 0) | - | ||||||||||||
222 | return false; | - | ||||||||||||
223 | - | |||||||||||||
224 | // create the socket notifiers | - | ||||||||||||
225 | if (threadData->hasEventDispatcher()) { | - | ||||||||||||
226 | if (&channel == &stdinChannel) { | - | ||||||||||||
227 | channel.notifier = new QSocketNotifier(channel.pipe[1], | - | ||||||||||||
228 | QSocketNotifier::Write, q); | - | ||||||||||||
229 | channel.notifier->setEnabled(false); | - | ||||||||||||
230 | QObject::connect(channel.notifier, SIGNAL(activated(int)), | - | ||||||||||||
231 | q, SLOT(_q_canWrite())); | - | ||||||||||||
232 | } else { | - | ||||||||||||
233 | channel.notifier = new QSocketNotifier(channel.pipe[0], | - | ||||||||||||
234 | QSocketNotifier::Read, q); | - | ||||||||||||
235 | const char *receiver; | - | ||||||||||||
236 | if (&channel == &stdoutChannel) | - | ||||||||||||
237 | receiver = SLOT(_q_canReadStandardOutput()); | - | ||||||||||||
238 | else | - | ||||||||||||
239 | receiver = SLOT(_q_canReadStandardError()); | - | ||||||||||||
240 | QObject::connect(channel.notifier, SIGNAL(activated(int)), | - | ||||||||||||
241 | q, receiver); | - | ||||||||||||
242 | } | - | ||||||||||||
243 | } | - | ||||||||||||
244 | - | |||||||||||||
245 | return true; | - | ||||||||||||
246 | } else if (channel.type == Channel::Redirect) { | - | ||||||||||||
247 | // we're redirecting the channel to/from a file | - | ||||||||||||
248 | QByteArray fname = QFile::encodeName(channel.file); | - | ||||||||||||
249 | - | |||||||||||||
250 | if (&channel == &stdinChannel) { | - | ||||||||||||
251 | // try to open in read-only mode | - | ||||||||||||
252 | channel.pipe[1] = -1; | - | ||||||||||||
253 | if ( (channel.pipe[0] = qt_safe_open(fname, O_RDONLY)) != -1) | - | ||||||||||||
254 | return true; // success | - | ||||||||||||
255 | setErrorAndEmit(QProcess::FailedToStart, | - | ||||||||||||
256 | QProcess::tr("Could not open input redirection for reading")); | - | ||||||||||||
257 | } else { | - | ||||||||||||
258 | int mode = O_WRONLY | O_CREAT; | - | ||||||||||||
259 | if (channel.append) | - | ||||||||||||
260 | mode |= O_APPEND; | - | ||||||||||||
261 | else | - | ||||||||||||
262 | mode |= O_TRUNC; | - | ||||||||||||
263 | - | |||||||||||||
264 | channel.pipe[0] = -1; | - | ||||||||||||
265 | if ( (channel.pipe[1] = qt_safe_open(fname, mode, 0666)) != -1) | - | ||||||||||||
266 | return true; // success | - | ||||||||||||
267 | - | |||||||||||||
268 | setErrorAndEmit(QProcess::FailedToStart, | - | ||||||||||||
269 | QProcess::tr("Could not open input redirection for reading")); | - | ||||||||||||
270 | } | - | ||||||||||||
271 | cleanup(); | - | ||||||||||||
272 | return false; | - | ||||||||||||
273 | } else { | - | ||||||||||||
274 | Q_ASSERT_X(channel.process, "QProcess::start", "Internal error"); | - | ||||||||||||
275 | - | |||||||||||||
276 | Channel *source; | - | ||||||||||||
277 | Channel *sink; | - | ||||||||||||
278 | - | |||||||||||||
279 | if (channel.type == Channel::PipeSource) { | - | ||||||||||||
280 | // we are the source | - | ||||||||||||
281 | source = &channel; | - | ||||||||||||
282 | sink = &channel.process->stdinChannel; | - | ||||||||||||
283 | - | |||||||||||||
284 | Q_ASSERT(source == &stdoutChannel); | - | ||||||||||||
285 | Q_ASSERT(sink->process == this && sink->type == Channel::PipeSink); | - | ||||||||||||
286 | } else { | - | ||||||||||||
287 | // we are the sink; | - | ||||||||||||
288 | source = &channel.process->stdoutChannel; | - | ||||||||||||
289 | sink = &channel; | - | ||||||||||||
290 | - | |||||||||||||
291 | Q_ASSERT(sink == &stdinChannel); | - | ||||||||||||
292 | Q_ASSERT(source->process == this && source->type == Channel::PipeSource); | - | ||||||||||||
293 | } | - | ||||||||||||
294 | - | |||||||||||||
295 | if (source->pipe[1] != INVALID_Q_PIPE || sink->pipe[0] != INVALID_Q_PIPE) { | - | ||||||||||||
296 | // already created, do nothing | - | ||||||||||||
297 | return true; | - | ||||||||||||
298 | } else { | - | ||||||||||||
299 | Q_ASSERT(source->pipe[0] == INVALID_Q_PIPE && source->pipe[1] == INVALID_Q_PIPE); | - | ||||||||||||
300 | Q_ASSERT(sink->pipe[0] == INVALID_Q_PIPE && sink->pipe[1] == INVALID_Q_PIPE); | - | ||||||||||||
301 | - | |||||||||||||
302 | Q_PIPE pipe[2] = { -1, -1 }; | - | ||||||||||||
303 | if (qt_create_pipe(pipe) != 0) | - | ||||||||||||
304 | return false; | - | ||||||||||||
305 | sink->pipe[0] = pipe[0]; | - | ||||||||||||
306 | source->pipe[1] = pipe[1]; | - | ||||||||||||
307 | - | |||||||||||||
308 | return true; | - | ||||||||||||
309 | } | - | ||||||||||||
310 | } | - | ||||||||||||
311 | } | - | ||||||||||||
312 | - | |||||||||||||
313 | QT_BEGIN_INCLUDE_NAMESPACE | - | ||||||||||||
314 | #if defined(Q_OS_MACX) | - | ||||||||||||
315 | # include <crt_externs.h> | - | ||||||||||||
316 | # define environ (*_NSGetEnviron()) | - | ||||||||||||
317 | #else | - | ||||||||||||
318 | extern char **environ; | - | ||||||||||||
319 | #endif | - | ||||||||||||
320 | QT_END_INCLUDE_NAMESPACE | - | ||||||||||||
321 | - | |||||||||||||
322 | QProcessEnvironment QProcessEnvironment::systemEnvironment() | - | ||||||||||||
323 | { | - | ||||||||||||
324 | QProcessEnvironment env; | - | ||||||||||||
325 | #if !defined(Q_OS_IOS) | - | ||||||||||||
326 | const char *entry; | - | ||||||||||||
327 | for (int count = 0; (entry = environ[count]); ++count) { | - | ||||||||||||
328 | const char *equal = strchr(entry, '='); | - | ||||||||||||
329 | if (!equal) | - | ||||||||||||
330 | continue; | - | ||||||||||||
331 | - | |||||||||||||
332 | QByteArray name(entry, equal - entry); | - | ||||||||||||
333 | QByteArray value(equal + 1); | - | ||||||||||||
334 | env.d->hash.insert(QProcessEnvironmentPrivate::Key(name), | - | ||||||||||||
335 | QProcessEnvironmentPrivate::Value(value)); | - | ||||||||||||
336 | } | - | ||||||||||||
337 | #endif | - | ||||||||||||
338 | return env; | - | ||||||||||||
339 | } | - | ||||||||||||
340 | - | |||||||||||||
341 | static char **_q_dupEnvironment(const QProcessEnvironmentPrivate::Hash &environment, int *envc) | - | ||||||||||||
342 | { | - | ||||||||||||
343 | *envc = 0; | - | ||||||||||||
344 | if (environment.isEmpty()) | - | ||||||||||||
345 | return 0; | - | ||||||||||||
346 | - | |||||||||||||
347 | char **envp = new char *[environment.count() + 2]; | - | ||||||||||||
348 | envp[environment.count()] = 0; | - | ||||||||||||
349 | envp[environment.count() + 1] = 0; | - | ||||||||||||
350 | - | |||||||||||||
351 | QProcessEnvironmentPrivate::Hash::ConstIterator it = environment.constBegin(); | - | ||||||||||||
352 | const QProcessEnvironmentPrivate::Hash::ConstIterator end = environment.constEnd(); | - | ||||||||||||
353 | for ( ; it != end; ++it) { | - | ||||||||||||
354 | QByteArray key = it.key().key; | - | ||||||||||||
355 | QByteArray value = it.value().bytes(); | - | ||||||||||||
356 | key.reserve(key.length() + 1 + value.length()); | - | ||||||||||||
357 | key.append('='); | - | ||||||||||||
358 | key.append(value); | - | ||||||||||||
359 | - | |||||||||||||
360 | envp[(*envc)++] = ::strdup(key.constData()); | - | ||||||||||||
361 | } | - | ||||||||||||
362 | - | |||||||||||||
363 | return envp; | - | ||||||||||||
364 | } | - | ||||||||||||
365 | - | |||||||||||||
366 | void QProcessPrivate::startProcess() | - | ||||||||||||
367 | { | - | ||||||||||||
368 | Q_Q(QProcess); | - | ||||||||||||
369 | - | |||||||||||||
370 | #if defined (QPROCESS_DEBUG) | - | ||||||||||||
371 | qDebug("QProcessPrivate::startProcess()"); | - | ||||||||||||
372 | #endif | - | ||||||||||||
373 | - | |||||||||||||
374 | // Initialize pipes | - | ||||||||||||
375 | if (!openChannel(stdinChannel) || | - | ||||||||||||
376 | !openChannel(stdoutChannel) || | - | ||||||||||||
377 | !openChannel(stderrChannel) || | - | ||||||||||||
378 | qt_create_pipe(childStartedPipe) != 0) { | - | ||||||||||||
379 | setErrorAndEmit(QProcess::FailedToStart, qt_error_string(errno)); | - | ||||||||||||
380 | cleanup(); | - | ||||||||||||
381 | return; | - | ||||||||||||
382 | } | - | ||||||||||||
383 | - | |||||||||||||
384 | if (threadData->hasEventDispatcher()) { | - | ||||||||||||
385 | startupSocketNotifier = new QSocketNotifier(childStartedPipe[0], | - | ||||||||||||
386 | QSocketNotifier::Read, q); | - | ||||||||||||
387 | QObject::connect(startupSocketNotifier, SIGNAL(activated(int)), | - | ||||||||||||
388 | q, SLOT(_q_startupNotification())); | - | ||||||||||||
389 | } | - | ||||||||||||
390 | - | |||||||||||||
391 | // Start the process (platform dependent) | - | ||||||||||||
392 | q->setProcessState(QProcess::Starting); | - | ||||||||||||
393 | - | |||||||||||||
394 | // Create argument list with right number of elements, and set the final | - | ||||||||||||
395 | // one to 0. | - | ||||||||||||
396 | char **argv = new char *[arguments.count() + 2]; | - | ||||||||||||
397 | argv[arguments.count() + 1] = 0; | - | ||||||||||||
398 | - | |||||||||||||
399 | // Encode the program name. | - | ||||||||||||
400 | QByteArray encodedProgramName = QFile::encodeName(program); | - | ||||||||||||
401 | #ifdef Q_OS_MAC | - | ||||||||||||
402 | // allow invoking of .app bundles on the Mac. | - | ||||||||||||
403 | QFileInfo fileInfo(program); | - | ||||||||||||
404 | if (encodedProgramName.endsWith(".app") && fileInfo.isDir()) { | - | ||||||||||||
405 | QCFType<CFURLRef> url = CFURLCreateWithFileSystemPath(0, | - | ||||||||||||
406 | QCFString(fileInfo.absoluteFilePath()), | - | ||||||||||||
407 | kCFURLPOSIXPathStyle, true); | - | ||||||||||||
408 | { | - | ||||||||||||
409 | // CFBundle is not reentrant, since CFBundleCreate might return a reference | - | ||||||||||||
410 | // to a cached bundle object. Protect the bundle calls with a mutex lock. | - | ||||||||||||
411 | static QBasicMutex cfbundleMutex; | - | ||||||||||||
412 | QMutexLocker lock(&cfbundleMutex); | - | ||||||||||||
413 | QCFType<CFBundleRef> bundle = CFBundleCreate(0, url); | - | ||||||||||||
414 | // 'executableURL' can be either relative or absolute ... | - | ||||||||||||
415 | QCFType<CFURLRef> executableURL = CFBundleCopyExecutableURL(bundle); | - | ||||||||||||
416 | // not to depend on caching - make sure it's always absolute. | - | ||||||||||||
417 | url = CFURLCopyAbsoluteURL(executableURL); | - | ||||||||||||
418 | } | - | ||||||||||||
419 | if (url) { | - | ||||||||||||
420 | const QCFString str = CFURLCopyFileSystemPath(url, kCFURLPOSIXPathStyle); | - | ||||||||||||
421 | encodedProgramName += (QDir::separator() + QDir(program).relativeFilePath(QCFString::toQString(str))).toUtf8(); | - | ||||||||||||
422 | } | - | ||||||||||||
423 | } | - | ||||||||||||
424 | #endif | - | ||||||||||||
425 | - | |||||||||||||
426 | // Add the program name to the argument list. | - | ||||||||||||
427 | char *dupProgramName = ::strdup(encodedProgramName.constData()); | - | ||||||||||||
428 | argv[0] = dupProgramName; | - | ||||||||||||
429 | - | |||||||||||||
430 | // Add every argument to the list | - | ||||||||||||
431 | for (int i = 0; i < arguments.count(); ++i) | - | ||||||||||||
432 | argv[i + 1] = ::strdup(QFile::encodeName(arguments.at(i)).constData()); | - | ||||||||||||
433 | - | |||||||||||||
434 | // Duplicate the environment. | - | ||||||||||||
435 | int envc = 0; | - | ||||||||||||
436 | char **envp = 0; | - | ||||||||||||
437 | if (environment.d.constData()) { | - | ||||||||||||
438 | QProcessEnvironmentPrivate::MutexLocker locker(environment.d); | - | ||||||||||||
439 | envp = _q_dupEnvironment(environment.d.constData()->hash, &envc); | - | ||||||||||||
440 | } | - | ||||||||||||
441 | - | |||||||||||||
442 | // Encode the working directory if it's non-empty, otherwise just pass 0. | - | ||||||||||||
443 | const char *workingDirPtr = 0; | - | ||||||||||||
444 | QByteArray encodedWorkingDirectory; | - | ||||||||||||
445 | if (!workingDirectory.isEmpty()) { | - | ||||||||||||
446 | encodedWorkingDirectory = QFile::encodeName(workingDirectory); | - | ||||||||||||
447 | workingDirPtr = encodedWorkingDirectory.constData(); | - | ||||||||||||
448 | } | - | ||||||||||||
449 | - | |||||||||||||
450 | // If the program does not specify a path, generate a list of possible | - | ||||||||||||
451 | // locations for the binary using the PATH environment variable. | - | ||||||||||||
452 | char **path = 0; | - | ||||||||||||
453 | int pathc = 0; | - | ||||||||||||
454 | if (!program.contains(QLatin1Char('/'))) { | - | ||||||||||||
455 | const QString pathEnv = QString::fromLocal8Bit(qgetenv("PATH")); | - | ||||||||||||
456 | if (!pathEnv.isEmpty()) { | - | ||||||||||||
457 | QStringList pathEntries = pathEnv.split(QLatin1Char(':'), QString::SkipEmptyParts); | - | ||||||||||||
458 | if (!pathEntries.isEmpty()) { | - | ||||||||||||
459 | pathc = pathEntries.size(); | - | ||||||||||||
460 | path = new char *[pathc + 1]; | - | ||||||||||||
461 | path[pathc] = 0; | - | ||||||||||||
462 | - | |||||||||||||
463 | for (int k = 0; k < pathEntries.size(); ++k) { | - | ||||||||||||
464 | QByteArray tmp = QFile::encodeName(pathEntries.at(k)); | - | ||||||||||||
465 | if (!tmp.endsWith('/')) tmp += '/'; | - | ||||||||||||
466 | tmp += encodedProgramName; | - | ||||||||||||
467 | path[k] = ::strdup(tmp.constData()); | - | ||||||||||||
468 | } | - | ||||||||||||
469 | } | - | ||||||||||||
470 | } | - | ||||||||||||
471 | } | - | ||||||||||||
472 | - | |||||||||||||
473 | // Start the process manager, and fork off the child process. | - | ||||||||||||
474 | pid_t childPid; | - | ||||||||||||
475 | forkfd = ::forkfd(FFD_CLOEXEC, &childPid); | - | ||||||||||||
476 | int lastForkErrno = errno; | - | ||||||||||||
477 | if (forkfd != FFD_CHILD_PROCESS) { | - | ||||||||||||
478 | // Parent process. | - | ||||||||||||
479 | // Clean up duplicated memory. | - | ||||||||||||
480 | free(dupProgramName); | - | ||||||||||||
481 | for (int i = 1; i <= arguments.count(); ++i) | - | ||||||||||||
482 | free(argv[i]); | - | ||||||||||||
483 | for (int i = 0; i < envc; ++i) | - | ||||||||||||
484 | free(envp[i]); | - | ||||||||||||
485 | for (int i = 0; i < pathc; ++i) | - | ||||||||||||
486 | free(path[i]); | - | ||||||||||||
487 | delete [] argv; | - | ||||||||||||
488 | delete [] envp; | - | ||||||||||||
489 | delete [] path; | - | ||||||||||||
490 | } | - | ||||||||||||
491 | - | |||||||||||||
492 | // On QNX, if spawnChild failed, childPid will be -1 but forkfd is still 0. | - | ||||||||||||
493 | // This is intentional because we only want to handle failure to fork() | - | ||||||||||||
494 | // here, which is a rare occurrence. Handling of the failure to start is | - | ||||||||||||
495 | // done elsewhere. | - | ||||||||||||
496 | if (forkfd == -1) { | - | ||||||||||||
497 | // Cleanup, report error and return | - | ||||||||||||
498 | #if defined (QPROCESS_DEBUG) | - | ||||||||||||
499 | qDebug("fork failed: %s", qPrintable(qt_error_string(lastForkErrno))); | - | ||||||||||||
500 | #endif | - | ||||||||||||
501 | q->setProcessState(QProcess::NotRunning); | - | ||||||||||||
502 | setErrorAndEmit(QProcess::FailedToStart, | - | ||||||||||||
503 | QProcess::tr("Resource error (fork failure): %1").arg(qt_error_string(lastForkErrno))); | - | ||||||||||||
504 | cleanup(); | - | ||||||||||||
505 | return; | - | ||||||||||||
506 | } | - | ||||||||||||
507 | - | |||||||||||||
508 | // Start the child. | - | ||||||||||||
509 | if (forkfd == FFD_CHILD_PROCESS) { | - | ||||||||||||
510 | execChild(workingDirPtr, path, argv, envp); | - | ||||||||||||
511 | ::_exit(-1); | - | ||||||||||||
512 | } | - | ||||||||||||
513 | - | |||||||||||||
514 | pid = Q_PID(childPid); | - | ||||||||||||
515 | - | |||||||||||||
516 | // parent | - | ||||||||||||
517 | // close the ends we don't use and make all pipes non-blocking | - | ||||||||||||
518 | qt_safe_close(childStartedPipe[1]); | - | ||||||||||||
519 | childStartedPipe[1] = -1; | - | ||||||||||||
520 | - | |||||||||||||
521 | if (stdinChannel.pipe[0] != -1) { | - | ||||||||||||
522 | qt_safe_close(stdinChannel.pipe[0]); | - | ||||||||||||
523 | stdinChannel.pipe[0] = -1; | - | ||||||||||||
524 | } | - | ||||||||||||
525 | - | |||||||||||||
526 | if (stdinChannel.pipe[1] != -1) | - | ||||||||||||
527 | ::fcntl(stdinChannel.pipe[1], F_SETFL, ::fcntl(stdinChannel.pipe[1], F_GETFL) | O_NONBLOCK); | - | ||||||||||||
528 | - | |||||||||||||
529 | if (stdoutChannel.pipe[1] != -1) { | - | ||||||||||||
530 | qt_safe_close(stdoutChannel.pipe[1]); | - | ||||||||||||
531 | stdoutChannel.pipe[1] = -1; | - | ||||||||||||
532 | } | - | ||||||||||||
533 | - | |||||||||||||
534 | if (stdoutChannel.pipe[0] != -1) | - | ||||||||||||
535 | ::fcntl(stdoutChannel.pipe[0], F_SETFL, ::fcntl(stdoutChannel.pipe[0], F_GETFL) | O_NONBLOCK); | - | ||||||||||||
536 | - | |||||||||||||
537 | if (stderrChannel.pipe[1] != -1) { | - | ||||||||||||
538 | qt_safe_close(stderrChannel.pipe[1]); | - | ||||||||||||
539 | stderrChannel.pipe[1] = -1; | - | ||||||||||||
540 | } | - | ||||||||||||
541 | if (stderrChannel.pipe[0] != -1) | - | ||||||||||||
542 | ::fcntl(stderrChannel.pipe[0], F_SETFL, ::fcntl(stderrChannel.pipe[0], F_GETFL) | O_NONBLOCK); | - | ||||||||||||
543 | - | |||||||||||||
544 | if (threadData->eventDispatcher) { | - | ||||||||||||
545 | deathNotifier = new QSocketNotifier(forkfd, QSocketNotifier::Read, q); | - | ||||||||||||
546 | QObject::connect(deathNotifier, SIGNAL(activated(int)), | - | ||||||||||||
547 | q, SLOT(_q_processDied())); | - | ||||||||||||
548 | } | - | ||||||||||||
549 | } | - | ||||||||||||
550 | - | |||||||||||||
551 | void QProcessPrivate::execChild(const char *workingDir, char **path, char **argv, char **envp) | - | ||||||||||||
552 | { | - | ||||||||||||
553 | ::signal(SIGPIPE, SIG_DFL); // reset the signal that we ignored | - | ||||||||||||
554 | - | |||||||||||||
555 | Q_Q(QProcess); | - | ||||||||||||
556 | - | |||||||||||||
557 | // copy the stdin socket if asked to (without closing on exec) | - | ||||||||||||
558 | if (inputChannelMode != QProcess::ForwardedInputChannel)
| 0 | ||||||||||||
559 | qt_safe_dup2(stdinChannel.pipe[0], STDIN_FILENO, 0); never executed: qt_safe_dup2(stdinChannel.pipe[0], 0, 0); | 0 | ||||||||||||
560 | - | |||||||||||||
561 | // copy the stdout and stderr if asked to | - | ||||||||||||
562 | if (processChannelMode != QProcess::ForwardedChannels) {
| 0 | ||||||||||||
563 | if (processChannelMode != QProcess::ForwardedOutputChannel)
| 0 | ||||||||||||
564 | qt_safe_dup2(stdoutChannel.pipe[1], STDOUT_FILENO, 0); never executed: qt_safe_dup2(stdoutChannel.pipe[1], 1, 0); | 0 | ||||||||||||
565 | - | |||||||||||||
566 | // merge stdout and stderr if asked to | - | ||||||||||||
567 | if (processChannelMode == QProcess::MergedChannels) {
| 0 | ||||||||||||
568 | qt_safe_dup2(STDOUT_FILENO, STDERR_FILENO, 0); | - | ||||||||||||
569 | } else if (processChannelMode != QProcess::ForwardedErrorChannel) { never executed: end of block
| 0 | ||||||||||||
570 | qt_safe_dup2(stderrChannel.pipe[1], STDERR_FILENO, 0); | - | ||||||||||||
571 | } never executed: end of block | 0 | ||||||||||||
572 | } never executed: end of block | 0 | ||||||||||||
573 | - | |||||||||||||
574 | // make sure this fd is closed if execvp() succeeds | - | ||||||||||||
575 | qt_safe_close(childStartedPipe[0]); | - | ||||||||||||
576 | - | |||||||||||||
577 | // enter the working directory | - | ||||||||||||
578 | const char *callthatfailed = "chdir: "; | - | ||||||||||||
579 | if (workingDir && QT_CHDIR(workingDir) == -1) {
| 0 | ||||||||||||
580 | // failed, stop the process | - | ||||||||||||
581 | goto report_errno; never executed: goto report_errno; | 0 | ||||||||||||
582 | } | - | ||||||||||||
583 | - | |||||||||||||
584 | // this is a virtual call, and it base behavior is to do nothing. | - | ||||||||||||
585 | q->setupChildProcess(); | - | ||||||||||||
586 | - | |||||||||||||
587 | // execute the process | - | ||||||||||||
588 | if (!envp) {
| 0 | ||||||||||||
589 | qt_safe_execvp(argv[0], argv); | - | ||||||||||||
590 | callthatfailed = "execvp: "; | - | ||||||||||||
591 | } else { never executed: end of block | 0 | ||||||||||||
592 | if (path) {
| 0 | ||||||||||||
593 | char **arg = path; | - | ||||||||||||
594 | while (*arg) {
| 0 | ||||||||||||
595 | argv[0] = *arg; | - | ||||||||||||
596 | #if defined (QPROCESS_DEBUG) | - | ||||||||||||
597 | fprintf(stderr, "QProcessPrivate::execChild() searching / starting %s\n", argv[0]); | - | ||||||||||||
598 | #endif | - | ||||||||||||
599 | qt_safe_execve(argv[0], argv, envp); | - | ||||||||||||
600 | ++arg; | - | ||||||||||||
601 | } never executed: end of block | 0 | ||||||||||||
602 | } else { never executed: end of block | 0 | ||||||||||||
603 | #if defined (QPROCESS_DEBUG) | - | ||||||||||||
604 | fprintf(stderr, "QProcessPrivate::execChild() starting %s\n", argv[0]); | - | ||||||||||||
605 | #endif | - | ||||||||||||
606 | qt_safe_execve(argv[0], argv, envp); | - | ||||||||||||
607 | } never executed: end of block | 0 | ||||||||||||
608 | callthatfailed = "execve: "; | - | ||||||||||||
609 | } never executed: end of block | 0 | ||||||||||||
610 | - | |||||||||||||
611 | // notify failure | - | ||||||||||||
612 | // we're running in the child process, so we don't need to be thread-safe; | - | ||||||||||||
613 | // we can use strerror | - | ||||||||||||
614 | report_errno: code before this statement never executed: report_errno: | 0 | ||||||||||||
615 | QString errorconst char *msg = qt_error_stringstrerror(errno); | - | ||||||||||||
616 | #if defined (QPROCESS_DEBUG) | - | ||||||||||||
617 | fprintf(stderr, "QProcessPrivate::execChild() failed (%s), notifying parent process\n", qPrintablemsg); | - | ||||||||||||
618 | #endif | - | ||||||||||||
619 | qt_safe_write(errorchildStartedPipe[1], callthatfailed, strlen(callthatfailed)); | - | ||||||||||||
620 | #endifqt_safe_write(childStartedPipe[1], error.data(), error.length() * sizeofmsg, strlen(QCharmsg)); | - | ||||||||||||
621 | qt_safe_close(childStartedPipe[1]); | - | ||||||||||||
622 | childStartedPipe[1] = -1; | - | ||||||||||||
623 | } never executed: end of block | 0 | ||||||||||||
624 | - | |||||||||||||
625 | bool QProcessPrivate::processStarted(QString *errorMessage) | - | ||||||||||||
626 | { | - | ||||||||||||
627 | ushortchar buf[errorBufferMax]; | - | ||||||||||||
628 | int i = 0; | - | ||||||||||||
629 | int ret; | - | ||||||||||||
630 | do { | - | ||||||||||||
631 | ret = qt_safe_read(childStartedPipe[0], &bufbuf + i, sizeof buf - i); | - | ||||||||||||
632 | if (ret > 0)
| 1408-3023 | ||||||||||||
633 | i += ret; executed 1408 times by 11 tests: i += ret; Executed by:
| 1408 | ||||||||||||
634 | } while (ret > 0 && i < int(sizeof buf)); executed 4431 times by 39 tests: end of block Executed by:
| 0-4431 | ||||||||||||
635 | - | |||||||||||||
636 | if (startupSocketNotifier) {
| 81-2942 | ||||||||||||
637 | startupSocketNotifier->setEnabled(false); | - | ||||||||||||
638 | startupSocketNotifier->deleteLater(); | - | ||||||||||||
639 | startupSocketNotifier = 0; | - | ||||||||||||
640 | } executed 2942 times by 37 tests: end of block Executed by:
| 2942 | ||||||||||||
641 | qt_safe_close(childStartedPipe[0]); | - | ||||||||||||
642 | childStartedPipe[0] = -1; | - | ||||||||||||
643 | - | |||||||||||||
644 | #if defined (QPROCESS_DEBUG) | - | ||||||||||||
645 | qDebug("QProcessPrivate::processStarted() == %s", i <= 0 ? "true" : "false"); | - | ||||||||||||
646 | #endif | - | ||||||||||||
647 | - | |||||||||||||
648 | // did we read an error message? | - | ||||||||||||
649 | if ((i > 0) && errorMessage)
| 0-2319 | ||||||||||||
650 | *errorMessage = QString((const QChar *)::fromLocal8Bit(buf, i/ sizeof(QChar));); executed 704 times by 11 tests: *errorMessage = QString::fromLocal8Bit(buf, i); Executed by:
| 704 | ||||||||||||
651 | - | |||||||||||||
652 | return i <= 0; executed 3023 times by 39 tests: return i <= 0; Executed by:
| 3023 | ||||||||||||
653 | } | - | ||||||||||||
654 | - | |||||||||||||
655 | qint64 QProcessPrivate::bytesAvailableInChannel(const Channel *channel) const | - | ||||||||||||
656 | { | - | ||||||||||||
657 | Q_ASSERT(channel->pipe[0] != INVALID_Q_PIPE); | - | ||||||||||||
658 | int nbytes = 0; | - | ||||||||||||
659 | qint64 available = 0; | - | ||||||||||||
660 | if (::ioctl(channel->pipe[0], FIONREAD, (char *) &nbytes) >= 0) | - | ||||||||||||
661 | available = (qint64) nbytes; | - | ||||||||||||
662 | #if defined (QPROCESS_DEBUG) | - | ||||||||||||
663 | qDebug("QProcessPrivate::bytesAvailableInChannel(%d) == %lld", int(channel - &stdinChannel), available); | - | ||||||||||||
664 | #endif | - | ||||||||||||
665 | return available; | - | ||||||||||||
666 | } | - | ||||||||||||
667 | - | |||||||||||||
668 | qint64 QProcessPrivate::readFromChannel(const Channel *channel, char *data, qint64 maxlen) | - | ||||||||||||
669 | { | - | ||||||||||||
670 | Q_ASSERT(channel->pipe[0] != INVALID_Q_PIPE); | - | ||||||||||||
671 | qint64 bytesRead = qt_safe_read(channel->pipe[0], data, maxlen); | - | ||||||||||||
672 | #if defined QPROCESS_DEBUG | - | ||||||||||||
673 | int save_errno = errno; | - | ||||||||||||
674 | qDebug("QProcessPrivate::readFromChannel(%d, %p \"%s\", %lld) == %lld", | - | ||||||||||||
675 | int(channel - &stdinChannel), | - | ||||||||||||
676 | data, qt_prettyDebug(data, bytesRead, 16).constData(), maxlen, bytesRead); | - | ||||||||||||
677 | errno = save_errno; | - | ||||||||||||
678 | #endif | - | ||||||||||||
679 | if (bytesRead == -1 && errno == EWOULDBLOCK) | - | ||||||||||||
680 | return -2; | - | ||||||||||||
681 | return bytesRead; | - | ||||||||||||
682 | } | - | ||||||||||||
683 | - | |||||||||||||
684 | bool QProcessPrivate::writeToStdin() | - | ||||||||||||
685 | { | - | ||||||||||||
686 | const char *data = stdinChannel.bufferwriteBuffer.readPointer(); | - | ||||||||||||
687 | const qint64 bytesToWrite = stdinChannel.bufferwriteBuffer.nextDataBlockSize(); | - | ||||||||||||
688 | - | |||||||||||||
689 | qint64 written = qt_safe_write_nosignal(stdinChannel.pipe[1], data, bytesToWrite); | - | ||||||||||||
690 | #if defined QPROCESS_DEBUG | - | ||||||||||||
691 | qDebug("QProcessPrivate::writeToStdin(), write(%p \"%s\", %lld) == %lld", | - | ||||||||||||
692 | data, qt_prettyDebug(data, bytesToWrite, 16).constData(), bytesToWrite, written); | - | ||||||||||||
693 | if (written == -1) | - | ||||||||||||
694 | qDebug("QProcessPrivate::writeToStdin(), failed to write (%s)", qPrintable(qt_error_string(errno))); | - | ||||||||||||
695 | #endif | - | ||||||||||||
696 | if (written == -1) {
| 5-3625 | ||||||||||||
697 | // If the O_NONBLOCK flag is set and If some data can be written without blocking | - | ||||||||||||
698 | // the process, write() will transfer what it can and return the number of bytes written. | - | ||||||||||||
699 | // Otherwise, it will return -1 and set errno to EAGAIN | - | ||||||||||||
700 | if (errno == EAGAIN)
| 0-5 | ||||||||||||
701 | return true; never executed: return true; | 0 | ||||||||||||
702 | - | |||||||||||||
703 | closeChannel(&stdinChannel); | - | ||||||||||||
704 | setErrorAndEmit(QProcess::WriteError); | - | ||||||||||||
705 | return false; executed 5 times by 1 test: return false; Executed by:
| 5 | ||||||||||||
706 | } | - | ||||||||||||
707 | stdinChannel.bufferwriteBuffer.free(written); | - | ||||||||||||
708 | if (!emittedBytesWritten && written != 0) {
| 0-3624 | ||||||||||||
709 | emittedBytesWritten = true; | - | ||||||||||||
710 | emit q_func()->bytesWritten(written); | - | ||||||||||||
711 | emittedBytesWritten = false; | - | ||||||||||||
712 | } executed 3624 times by 6 tests: end of block Executed by:
| 3624 | ||||||||||||
713 | return true; executed 3625 times by 6 tests: return true; Executed by:
| 3625 | ||||||||||||
714 | } | - | ||||||||||||
715 | - | |||||||||||||
716 | void QProcessPrivate::terminateProcess() | - | ||||||||||||
717 | { | - | ||||||||||||
718 | #if defined (QPROCESS_DEBUG) | - | ||||||||||||
719 | qDebug("QProcessPrivate::terminateProcess()"); | - | ||||||||||||
720 | #endif | - | ||||||||||||
721 | if (pid) | - | ||||||||||||
722 | ::kill(pid_t(pid), SIGTERM); | - | ||||||||||||
723 | } | - | ||||||||||||
724 | - | |||||||||||||
725 | void QProcessPrivate::killProcess() | - | ||||||||||||
726 | { | - | ||||||||||||
727 | #if defined (QPROCESS_DEBUG) | - | ||||||||||||
728 | qDebug("QProcessPrivate::killProcess()"); | - | ||||||||||||
729 | #endif | - | ||||||||||||
730 | if (pid) | - | ||||||||||||
731 | ::kill(pid_t(pid), SIGKILL); | - | ||||||||||||
732 | } | - | ||||||||||||
733 | - | |||||||||||||
734 | bool QProcessPrivate::waitForStarted(int msecs) | - | ||||||||||||
735 | { | - | ||||||||||||
736 | #if defined (QPROCESS_DEBUG) | - | ||||||||||||
737 | qDebug("QProcessPrivate::waitForStarted(%d) waiting for child to start (fd = %d)", msecs, | - | ||||||||||||
738 | childStartedPipe[0]); | - | ||||||||||||
739 | #endif | - | ||||||||||||
740 | - | |||||||||||||
741 | fd_set fds; | - | ||||||||||||
FD_ZERO(&fds); | ||||||||||||||
FD_SETpollfd pfd = qt_make_pollfd(childStartedPipe[0], &fdsPOLLIN); | ||||||||||||||
742 | - | |||||||||||||
743 | if (qt_select_msecsqt_poll_msecs(childStartedPipe[0] + 1, &fds&pfd, 01, msecs) == 0) {
| 0-2796 | ||||||||||||
744 | setError(QProcess::Timedout); | - | ||||||||||||
745 | #if defined (QPROCESS_DEBUG) | - | ||||||||||||
746 | qDebug("QProcessPrivate::waitForStarted(%d) == false (timed out)", msecs); | - | ||||||||||||
747 | #endif | - | ||||||||||||
748 | return false; never executed: return false; | 0 | ||||||||||||
749 | } | - | ||||||||||||
750 | - | |||||||||||||
751 | bool startedEmitted = _q_startupNotification(); | - | ||||||||||||
752 | #if defined (QPROCESS_DEBUG) | - | ||||||||||||
753 | qDebug("QProcessPrivate::waitForStarted() == %s", startedEmitted ? "true" : "false"); | - | ||||||||||||
754 | #endif | - | ||||||||||||
755 | return startedEmitted; executed 2796 times by 37 tests: return startedEmitted; Executed by:
| 2796 | ||||||||||||
} executed 2796 times by 37 tests: return startedEmitted; Executed by:
| ||||||||||||||
#ifdef Q_OS_BLACKBERRY executed 2796 times by 37 tests: return startedEmitted; Executed by:
| ||||||||||||||
QList<QSocketNotifier *> QProcessPrivate::defaultNotifiers() const executed 2796 times by 37 tests: return startedEmitted; Executed by:
| ||||||||||||||
{ executed 2796 times by 37 tests: return startedEmitted; Executed by:
| ||||||||||||||
QList<QSocketNotifier *> notifiers; executed 2796 times by 37 tests: return startedEmitted; Executed by:
| ||||||||||||||
notifiers << stdoutChannel.notifier executed 2796 times by 37 tests: return startedEmitted; Executed by:
| ||||||||||||||
<< stderrChannel.notifier executed 2796 times by 37 tests: return startedEmitted; Executed by:
| ||||||||||||||
<< stdinChannel.notifier; executed 2796 times by 37 tests: return startedEmitted; Executed by:
| ||||||||||||||
return notifiers executed 2796 times by 37 tests: ;return startedEmitted; Executed by:
executed 2796 times by 37 tests: return startedEmitted; Executed by:
| ||||||||||||||
756 | } | - | ||||||||||||
757 | - | |||||||||||||
#endif bool QProcessPrivate::waitForReadyRead(int msecs) | ||||||||||||||
759 | { | - | ||||||||||||
760 | #if defined (QPROCESS_DEBUG) | - | ||||||||||||
761 | qDebug("QProcessPrivate::waitForReadyRead(%d)", msecs); | - | ||||||||||||
762 | #endif | - | ||||||||||||
763 | - | |||||||||||||
764 | QElapsedTimer stopWatch; | - | ||||||||||||
765 | stopWatch.start(); | - | ||||||||||||
#ifdef Q_OS_BLACKBERRY | ||||||||||||||
QList<QSocketNotifier *> notifiers = defaultNotifiers(); | ||||||||||||||
766 | - | |||||||||||||
767 | #endifforever { | - | ||||||||||||
768 | fd_set fdread; | - | ||||||||||||
fd_set fdwrite; | ||||||||||||||
FD_ZERO(&fdread); | ||||||||||||||
FD_ZERO(&fdwrite); | ||||||||||||||
int nfds = forkfd; | ||||||||||||||
FD_SET(forkfd, &fdread); | ||||||||||||||
if (processState == QProcess::Starting) | ||||||||||||||
add_fd(nfds, childStartedPipe[0], &fdread); | ||||||||||||||
if (stdoutChannel.pipe[0] != -1) | ||||||||||||||
add_fd(nfds, stdoutChannel.pipe[0], &fdread); | ||||||||||||||
if (stderrChannel.pipe[0] != -1) | ||||||||||||||
add_fd(nfds, stderrChannel.pipe[0], &fdread); | ||||||||||||||
if (!stdinChannel.buffer.isEmpty() && stdinChannel.pipe[1] != -1) | ||||||||||||||
add_fd(nfds, stdinChannel.pipe[1], &fdwriteQProcessPoller poller(*this); | ||||||||||||||
769 | - | |||||||||||||
770 | int timeout = qt_subtract_from_timeout(msecs, stopWatch.elapsed()); | - | ||||||||||||
771 | #ifdef Q_OS_BLACKBERRY | - | ||||||||||||
int ret = bb_select(notifiers, nfds + 1, &fdread, &fdwrite, timeout); | ||||||||||||||
#elseint ret = qt_select_msecspoller.poll(nfds + 1, &fdread, &fdwrite,timeout); | ||||||||||||||
772 | - | |||||||||||||
773 | #endifif (ret < 0) {
| 0-972 | ||||||||||||
774 | break; never executed: break; | 0 | ||||||||||||
775 | } | - | ||||||||||||
776 | if (ret == 0) {
| 5-967 | ||||||||||||
777 | setError(QProcess::Timedout); | - | ||||||||||||
778 | return false; executed 5 times by 1 test: return false; Executed by:
| 5 | ||||||||||||
779 | } | - | ||||||||||||
780 | - | |||||||||||||
781 | if (childStartedPipe[0] != -1 && FD_ISSETqt_pollfd_check(poller.childStartedPipe[0], &fdread(), POLLIN)) {
| 113-854 | ||||||||||||
782 | if (!_q_startupNotification())
| 11-102 | ||||||||||||
783 | return false; executed 102 times by 2 tests: return false; Executed by:
| 102 | ||||||||||||
784 | } executed 11 times by 3 tests: end of block Executed by:
| 11 | ||||||||||||
785 | - | |||||||||||||
786 | bool readyReadEmitted = false; | - | ||||||||||||
787 | if (stdoutChannel.pipe[0] != -1 && FD_ISSETqt_pollfd_check(stdoutChannelpoller.pipe[0], &fdreadstdoutPipe(), POLLIN)) {
| 280-585 | ||||||||||||
788 | bool canRead = _q_canReadStandardOutput(); | - | ||||||||||||
789 | if (processChannelcurrentReadChannel == QProcess::StandardOutput && canRead)
| 1-570 | ||||||||||||
790 | readyReadEmitted = true; executed 569 times by 8 tests: readyReadEmitted = true; Executed by:
| 569 | ||||||||||||
791 | } executed 585 times by 8 tests: end of block Executed by:
| 585 | ||||||||||||
792 | if (stderrChannel.pipe[0] != -1 && FD_ISSETqt_pollfd_check(stderrChannelpoller.pipe[0], &fdreadstderrPipe(), POLLIN)) {
| 51-814 | ||||||||||||
793 | bool canRead = _q_canReadStandardError(); | - | ||||||||||||
794 | if (processChannelcurrentReadChannel == QProcess::StandardError && canRead)
| 0-29 | ||||||||||||
795 | readyReadEmitted = true; executed 22 times by 1 test: readyReadEmitted = true; Executed by:
| 22 | ||||||||||||
796 | } executed 51 times by 5 tests: end of block Executed by:
| 51 | ||||||||||||
797 | if (readyReadEmitted)
| 274-591 | ||||||||||||
798 | return true; executed 591 times by 8 tests: return true; Executed by:
| 591 | ||||||||||||
799 | - | |||||||||||||
800 | if (stdinChannel.pipe[1] != -1 && FD_ISSETqt_pollfd_check(stdinChannelpoller.pipe[1], &fdwritestdinPipe(), POLLOUT))
| 57-217 | ||||||||||||
801 | _q_canWrite(); executed 217 times by 2 tests: _q_canWrite(); Executed by:
| 217 | ||||||||||||
802 | - | |||||||||||||
803 | if (forkfd == -1 || FD_ISSETqt_pollfd_check(poller.forkfd, &fdread(), POLLIN)) {
| 1-273 | ||||||||||||
804 | if (_q_processDied())
| 0-1 | ||||||||||||
805 | return false; executed 1 time by 1 test: return false; Executed by:
| 1 | ||||||||||||
806 | } never executed: end of block | 0 | ||||||||||||
807 | } executed 273 times by 8 tests: end of block Executed by:
| 273 | ||||||||||||
808 | return false; never executed: return false; | 0 | ||||||||||||
809 | } | - | ||||||||||||
810 | - | |||||||||||||
811 | bool QProcessPrivate::waitForBytesWritten(int msecs) | - | ||||||||||||
812 | { | - | ||||||||||||
813 | #if defined (QPROCESS_DEBUG) | - | ||||||||||||
814 | qDebug("QProcessPrivate::waitForBytesWritten(%d)", msecs); | - | ||||||||||||
815 | #endif | - | ||||||||||||
816 | - | |||||||||||||
817 | QElapsedTimer stopWatch; | - | ||||||||||||
818 | stopWatch.start(); | - | ||||||||||||
#ifdef Q_OS_BLACKBERRY | ||||||||||||||
QList<QSocketNotifier *> notifiers = defaultNotifiers(); | ||||||||||||||
819 | - | |||||||||||||
820 | #endifwhile (!stdinChannel.bufferwriteBuffer.isEmpty()) {
| 1-3161 | ||||||||||||
821 | fd_set fdread; | - | ||||||||||||
fd_set fdwrite; | ||||||||||||||
FD_ZERO(&fdread); | ||||||||||||||
FD_ZERO(&fdwrite); | ||||||||||||||
int nfds = forkfd; | ||||||||||||||
FD_SET(forkfd, &fdread); | ||||||||||||||
if (processState == QProcess::Starting) | ||||||||||||||
add_fd(nfds, childStartedPipe[0], &fdread); | ||||||||||||||
if (stdoutChannel.pipe[0] != -1) | ||||||||||||||
add_fd(nfds, stdoutChannel.pipe[0], &fdread); | ||||||||||||||
if (stderrChannel.pipe[0] != -1) | ||||||||||||||
add_fd(nfds, stderrChannel.pipe[0], &fdread); | ||||||||||||||
if (!stdinChannel.buffer.isEmpty() && stdinChannel.pipe[1] != -1) | ||||||||||||||
add_fd(nfds, stdinChannel.pipe[1], &fdwriteQProcessPoller poller(*this); | ||||||||||||||
822 | - | |||||||||||||
823 | int timeout = qt_subtract_from_timeout(msecs, stopWatch.elapsed()); | - | ||||||||||||
824 | #ifdef Q_OS_BLACKBERRY | - | ||||||||||||
int ret = bb_select(notifiers, nfds + 1, &fdread, &fdwrite, timeout); | ||||||||||||||
#elseint ret = qt_select_msecspoller.poll(nfds + 1, &fdread, &fdwrite,timeout); | ||||||||||||||
825 | - | |||||||||||||
826 | #endifif (ret < 0) {
| 0-3161 | ||||||||||||
827 | break; never executed: break; | 0 | ||||||||||||
828 | } | - | ||||||||||||
829 | - | |||||||||||||
830 | if (ret == 0) {
| 0-3161 | ||||||||||||
831 | setError(QProcess::Timedout); | - | ||||||||||||
832 | return false; never executed: return false; | 0 | ||||||||||||
833 | } | - | ||||||||||||
834 | - | |||||||||||||
835 | if (childStartedPipe[0] != -1 && FD_ISSETqt_pollfd_check(poller.childStartedPipe[0], &fdread(), POLLIN)) {
| 0-3161 | ||||||||||||
836 | if (!_q_startupNotification())
| 0 | ||||||||||||
837 | return false; never executed: return false; | 0 | ||||||||||||
838 | } never executed: end of block | 0 | ||||||||||||
839 | - | |||||||||||||
840 | if (stdinChannel.pipe[1] != -1 && FD_ISSETqt_pollfd_check(stdinChannelpoller.pipe[1], &fdwritestdinPipe(), POLLOUT))
| 0-3161 | ||||||||||||
841 | return _q_canWrite(); executed 3161 times by 2 tests: return _q_canWrite(); Executed by:
| 3161 | ||||||||||||
842 | - | |||||||||||||
843 | if (stdoutChannel.pipe[0] != -1 && FD_ISSETqt_pollfd_check(stdoutChannelpoller.pipe[0], &fdreadstdoutPipe(), POLLIN))
| 0 | ||||||||||||
844 | _q_canReadStandardOutput(); never executed: _q_canReadStandardOutput(); | 0 | ||||||||||||
845 | - | |||||||||||||
846 | if (stderrChannel.pipe[0] != -1 && FD_ISSETqt_pollfd_check(stderrChannelpoller.pipe[0], &fdreadstderrPipe(), POLLIN))
| 0 | ||||||||||||
847 | _q_canReadStandardError(); never executed: _q_canReadStandardError(); | 0 | ||||||||||||
848 | - | |||||||||||||
849 | if (forkfd == -1 || FD_ISSETqt_pollfd_check(poller.forkfd, &fdread(), POLLIN)) {
| 0 | ||||||||||||
850 | if (_q_processDied())
| 0 | ||||||||||||
851 | return false; never executed: return false; | 0 | ||||||||||||
852 | } never executed: end of block | 0 | ||||||||||||
853 | } never executed: end of block | 0 | ||||||||||||
854 | - | |||||||||||||
855 | return false; executed 1 time by 1 test: return false; Executed by:
| 1 | ||||||||||||
856 | } | - | ||||||||||||
857 | - | |||||||||||||
858 | bool QProcessPrivate::waitForFinished(int msecs) | - | ||||||||||||
859 | { | - | ||||||||||||
860 | #if defined (QPROCESS_DEBUG) | - | ||||||||||||
861 | qDebug("QProcessPrivate::waitForFinished(%d)", msecs); | - | ||||||||||||
862 | #endif | - | ||||||||||||
863 | - | |||||||||||||
864 | QElapsedTimer stopWatch; | - | ||||||||||||
865 | stopWatch.start(); | - | ||||||||||||
#ifdef Q_OS_BLACKBERRY | ||||||||||||||
QList<QSocketNotifier *> notifiers = defaultNotifiers(); | ||||||||||||||
866 | - | |||||||||||||
867 | #endifforever { | - | ||||||||||||
868 | fd_set fdread; | - | ||||||||||||
fd_set fdwrite; | ||||||||||||||
int nfds = -1; | ||||||||||||||
FD_ZERO(&fdread); | ||||||||||||||
FD_ZERO(&fdwrite); | ||||||||||||||
if (processState == QProcess::Starting) | ||||||||||||||
add_fd(nfds, childStartedPipe[0], &fdread); | ||||||||||||||
if (stdoutChannel.pipe[0] != -1) | ||||||||||||||
add_fd(nfds, stdoutChannel.pipe[0], &fdread); | ||||||||||||||
if (stderrChannel.pipe[0] != -1) | ||||||||||||||
add_fd(nfds, stderrChannel.pipe[0], &fdread); | ||||||||||||||
if (processState == QProcess::Running && forkfd != -1) | ||||||||||||||
add_fd(nfds, forkfd, &fdread); | ||||||||||||||
if (!stdinChannel.buffer.isEmpty() && stdinChannel.pipe[1] != -1) | ||||||||||||||
add_fd(nfds, stdinChannel.pipe[1], &fdwriteQProcessPoller poller(*this); | ||||||||||||||
869 | - | |||||||||||||
870 | int timeout = qt_subtract_from_timeout(msecs, stopWatch.elapsed()); | - | ||||||||||||
871 | #ifdef Q_OS_BLACKBERRY | - | ||||||||||||
int ret = bb_select(notifiers, nfds + 1, &fdread, &fdwrite, timeout); | ||||||||||||||
#elseint ret = qt_select_msecspoller.poll(nfds + 1, &fdread, &fdwrite,timeout); | ||||||||||||||
872 | - | |||||||||||||
873 | #endifif (ret < 0) {
| 0-38315 | ||||||||||||
874 | break; never executed: break; | 0 | ||||||||||||
875 | } | - | ||||||||||||
876 | if (ret == 0) {
| 6-38309 | ||||||||||||
877 | setError(QProcess::Timedout); | - | ||||||||||||
878 | return false; executed 6 times by 3 tests: return false; Executed by:
| 6 | ||||||||||||
879 | } | - | ||||||||||||
880 | - | |||||||||||||
881 | if (childStartedPipe[0] != -1 && FD_ISSETqt_pollfd_check(poller.childStartedPipe[0], &fdread(), POLLIN)) {
| 0-38309 | ||||||||||||
882 | if (!_q_startupNotification())
| 0 | ||||||||||||
883 | return false; never executed: return false; | 0 | ||||||||||||
884 | } never executed: end of block | 0 | ||||||||||||
885 | if (stdinChannel.pipe[1] != -1 && FD_ISSETqt_pollfd_check(stdinChannelpoller.pipe[1], &fdwritestdinPipe(), POLLOUT))
| 66-38243 | ||||||||||||
886 | _q_canWrite(); executed 66 times by 3 tests: _q_canWrite(); Executed by:
| 66 | ||||||||||||
887 | - | |||||||||||||
888 | if (stdoutChannel.pipe[0] != -1 && FD_ISSETqt_pollfd_check(stdoutChannelpoller.pipe[0], &fdreadstdoutPipe(), POLLIN))
| 4930-33379 | ||||||||||||
889 | _q_canReadStandardOutput(); executed 33379 times by 29 tests: _q_canReadStandardOutput(); Executed by:
| 33379 | ||||||||||||
890 | - | |||||||||||||
891 | if (stderrChannel.pipe[0] != -1 && FD_ISSETqt_pollfd_check(stderrChannelpoller.pipe[0], &fdreadstderrPipe(), POLLIN))
| 2969-35340 | ||||||||||||
892 | _q_canReadStandardError(); executed 2969 times by 24 tests: _q_canReadStandardError(); Executed by:
| 2969 | ||||||||||||
893 | - | |||||||||||||
894 | if (forkfd == -1 || FD_ISSETqt_pollfd_check(poller.forkfd, &fdread(), POLLIN)) {
| 2250-36059 | ||||||||||||
895 | if (_q_processDied())
| 0-2250 | ||||||||||||
896 | return true; executed 2250 times by 29 tests: return true; Executed by:
| 2250 | ||||||||||||
897 | } never executed: end of block | 0 | ||||||||||||
898 | } executed 36059 times by 26 tests: end of block Executed by:
| 36059 | ||||||||||||
899 | return false; never executed: return false; | 0 | ||||||||||||
} never executed: return false; | ||||||||||||||
bool QProcessPrivate::waitForWrite(int msecs) never executed: return false; | ||||||||||||||
{ never executed: return false; | ||||||||||||||
fd_set fdwrite; never executed: return false; | ||||||||||||||
FD_ZERO(&fdwrite); never executed: return false; | ||||||||||||||
FD_SET(stdinChannel.pipe[1], &fdwrite); never executed: return false; | ||||||||||||||
return qt_select_msecs(stdinChannel.pipe[1] + 1, 0, &fdwrite, msecs < 0 ? 0 : msecs) == 1 never executed: ;return false; never executed: return false; | ||||||||||||||
900 | } | - | ||||||||||||
901 | - | |||||||||||||
902 | void QProcessPrivate::findExitCode() | - | ||||||||||||
903 | { | - | ||||||||||||
904 | } | - | ||||||||||||
905 | - | |||||||||||||
906 | bool QProcessPrivate::waitForDeadChild() | - | ||||||||||||
907 | { | - | ||||||||||||
908 | if (forkfd == -1) | - | ||||||||||||
909 | return true; // child has already exited | - | ||||||||||||
910 | - | |||||||||||||
911 | // read the process information from our fd | - | ||||||||||||
912 | forkfd_info info; | - | ||||||||||||
913 | int ret; | - | ||||||||||||
914 | EINTR_LOOP(ret, forkfd_wait(forkfd, &info, Q_NULLPTR)); | - | ||||||||||||
915 | - | |||||||||||||
916 | exitCode = info.status; | - | ||||||||||||
917 | crashed = info.code != CLD_EXITED; | - | ||||||||||||
918 | - | |||||||||||||
919 | delete deathNotifier; | - | ||||||||||||
920 | deathNotifier = 0; | - | ||||||||||||
921 | - | |||||||||||||
922 | EINTR_LOOP(ret, forkfd_close(forkfd)); | - | ||||||||||||
923 | forkfd = -1; // Child is dead, don't try to kill it anymore | - | ||||||||||||
924 | - | |||||||||||||
925 | #if defined QPROCESS_DEBUG | - | ||||||||||||
926 | qDebug() << "QProcessPrivate::waitForDeadChild() dead with exitCode" | - | ||||||||||||
927 | << exitCode << ", crashed?" << crashed; | - | ||||||||||||
928 | #endif | - | ||||||||||||
929 | return true; | - | ||||||||||||
930 | } | - | ||||||||||||
931 | - | |||||||||||||
932 | bool QProcessPrivate::startDetached(const QString &program, const QStringList &arguments, const QString &workingDirectory, qint64 *pid) | - | ||||||||||||
933 | { | - | ||||||||||||
934 | QByteArray encodedWorkingDirectory = QFile::encodeName(workingDirectory); | - | ||||||||||||
935 | - | |||||||||||||
936 | // To catch the startup of the child | - | ||||||||||||
937 | int startedPipe[2]; | - | ||||||||||||
938 | if (qt_safe_pipe(startedPipe) != 0) | - | ||||||||||||
939 | return false; | - | ||||||||||||
940 | // To communicate the pid of the child | - | ||||||||||||
941 | int pidPipe[2]; | - | ||||||||||||
942 | if (qt_safe_pipe(pidPipe) != 0) { | - | ||||||||||||
943 | qt_safe_close(startedPipe[0]); | - | ||||||||||||
944 | qt_safe_close(startedPipe[1]); | - | ||||||||||||
945 | return false; | - | ||||||||||||
946 | } | - | ||||||||||||
947 | - | |||||||||||||
948 | pid_t childPid = fork(); | - | ||||||||||||
949 | if (childPid == 0) { | - | ||||||||||||
950 | struct sigaction noaction; | - | ||||||||||||
951 | memset(&noaction, 0, sizeof(noaction)); | - | ||||||||||||
952 | noaction.sa_handler = SIG_IGN; | - | ||||||||||||
953 | ::sigaction(SIGPIPE, &noaction, 0); | - | ||||||||||||
954 | - | |||||||||||||
955 | ::setsid(); | - | ||||||||||||
956 | - | |||||||||||||
957 | qt_safe_close(startedPipe[0]); | - | ||||||||||||
958 | qt_safe_close(pidPipe[0]); | - | ||||||||||||
959 | - | |||||||||||||
960 | pid_t doubleForkPid = fork(); | - | ||||||||||||
961 | if (doubleForkPid == 0) { | - | ||||||||||||
962 | qt_safe_close(pidPipe[1]); | - | ||||||||||||
963 | - | |||||||||||||
964 | if (!encodedWorkingDirectory.isEmpty()) { | - | ||||||||||||
965 | if (QT_CHDIR(encodedWorkingDirectory.constData()) == -1) | - | ||||||||||||
966 | qWarning("QProcessPrivate::startDetached: failed to chdir to %s", encodedWorkingDirectory.constData()); | - | ||||||||||||
967 | } | - | ||||||||||||
968 | - | |||||||||||||
969 | char **argv = new char *[arguments.size() + 2]; | - | ||||||||||||
970 | for (int i = 0; i < arguments.size(); ++i) | - | ||||||||||||
971 | argv[i + 1] = ::strdup(QFile::encodeName(arguments.at(i)).constData()); | - | ||||||||||||
972 | argv[arguments.size() + 1] = 0; | - | ||||||||||||
973 | - | |||||||||||||
974 | if (!program.contains(QLatin1Char('/'))) { | - | ||||||||||||
975 | const QString path = QString::fromLocal8Bit(qgetenv("PATH")); | - | ||||||||||||
976 | if (!path.isEmpty()) { | - | ||||||||||||
977 | QStringList pathEntries = path.split(QLatin1Char(':')); | - | ||||||||||||
978 | for (int k = 0; k < pathEntries.size(); ++k) { | - | ||||||||||||
979 | QByteArray tmp = QFile::encodeName(pathEntries.at(k)); | - | ||||||||||||
980 | if (!tmp.endsWith('/')) tmp += '/'; | - | ||||||||||||
981 | tmp += QFile::encodeName(program); | - | ||||||||||||
982 | argv[0] = tmp.data(); | - | ||||||||||||
983 | qt_safe_execv(argv[0], argv); | - | ||||||||||||
984 | } | - | ||||||||||||
985 | } | - | ||||||||||||
986 | } else { | - | ||||||||||||
987 | QByteArray tmp = QFile::encodeName(program); | - | ||||||||||||
988 | argv[0] = tmp.data(); | - | ||||||||||||
989 | qt_safe_execv(argv[0], argv); | - | ||||||||||||
990 | } | - | ||||||||||||
991 | - | |||||||||||||
992 | struct sigaction noaction; | - | ||||||||||||
993 | memset(&noaction, 0, sizeof(noaction)); | - | ||||||||||||
994 | noaction.sa_handler = SIG_IGN; | - | ||||||||||||
995 | ::sigaction(SIGPIPE, &noaction, 0); | - | ||||||||||||
996 | - | |||||||||||||
997 | // '\1' means execv failed | - | ||||||||||||
998 | char c = '\1'; | - | ||||||||||||
999 | qt_safe_write(startedPipe[1], &c, 1); | - | ||||||||||||
1000 | qt_safe_close(startedPipe[1]); | - | ||||||||||||
1001 | ::_exit(1); | - | ||||||||||||
1002 | } else if (doubleForkPid == -1) { | - | ||||||||||||
1003 | struct sigaction noaction; | - | ||||||||||||
1004 | memset(&noaction, 0, sizeof(noaction)); | - | ||||||||||||
1005 | noaction.sa_handler = SIG_IGN; | - | ||||||||||||
1006 | ::sigaction(SIGPIPE, &noaction, 0); | - | ||||||||||||
1007 | - | |||||||||||||
1008 | // '\2' means internal error | - | ||||||||||||
1009 | char c = '\2'; | - | ||||||||||||
1010 | qt_safe_write(startedPipe[1], &c, 1); | - | ||||||||||||
1011 | } | - | ||||||||||||
1012 | - | |||||||||||||
1013 | qt_safe_close(startedPipe[1]); | - | ||||||||||||
1014 | qt_safe_write(pidPipe[1], (const char *)&doubleForkPid, sizeof(pid_t)); | - | ||||||||||||
1015 | if (QT_CHDIR("/") == -1) | - | ||||||||||||
1016 | qWarning("QProcessPrivate::startDetached: failed to chdir to /"); | - | ||||||||||||
1017 | ::_exit(1); | - | ||||||||||||
1018 | } | - | ||||||||||||
1019 | - | |||||||||||||
1020 | qt_safe_close(startedPipe[1]); | - | ||||||||||||
1021 | qt_safe_close(pidPipe[1]); | - | ||||||||||||
1022 | - | |||||||||||||
1023 | if (childPid == -1) { | - | ||||||||||||
1024 | qt_safe_close(startedPipe[0]); | - | ||||||||||||
1025 | qt_safe_close(pidPipe[0]); | - | ||||||||||||
1026 | return false; | - | ||||||||||||
1027 | } | - | ||||||||||||
1028 | - | |||||||||||||
1029 | char reply = '\0'; | - | ||||||||||||
1030 | int startResult = qt_safe_read(startedPipe[0], &reply, 1); | - | ||||||||||||
1031 | int result; | - | ||||||||||||
1032 | qt_safe_close(startedPipe[0]); | - | ||||||||||||
1033 | qt_safe_waitpid(childPid, &result, 0); | - | ||||||||||||
1034 | bool success = (startResult != -1 && reply == '\0'); | - | ||||||||||||
1035 | if (success && pid) { | - | ||||||||||||
1036 | pid_t actualPid = 0; | - | ||||||||||||
1037 | if (qt_safe_read(pidPipe[0], (char *)&actualPid, sizeof(pid_t)) == sizeof(pid_t)) { | - | ||||||||||||
1038 | *pid = actualPid; | - | ||||||||||||
1039 | } else { | - | ||||||||||||
1040 | *pid = 0; | - | ||||||||||||
1041 | } | - | ||||||||||||
1042 | } | - | ||||||||||||
1043 | qt_safe_close(pidPipe[0]); | - | ||||||||||||
1044 | return success; | - | ||||||||||||
1045 | } | - | ||||||||||||
1046 | - | |||||||||||||
1047 | QT_END_NAMESPACE | - | ||||||||||||
1048 | - | |||||||||||||
1049 | #endif // QT_NO_PROCESS | - | ||||||||||||
Source code | Switch to Preprocessed file |