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#if QT_CONFIG(process) && defined(QPROCESS_DEBUG)
45#include "private/qtools_p.h"
46#include <ctype.h>
47
48/*
49 Returns a human readable representation of the first \a len
50 characters in \a data.
51*/
52QT_BEGIN_NAMESPACE
53static QByteArray qt_prettyDebug(const char *data, int len, int maxSize)
54{
55 if (!data) return "(null)";
56 QByteArray out;
57 for (int i = 0; i < len; ++i) {
58 char c = data[i];
59 if (isprint(c)) {
60 out += c;
61 } else switch (c) {
62 case '\n': out += "\\n"; break;
63 case '\r': out += "\\r"; break;
64 case '\t': out += "\\t"; break;
65 default: {
66 const char buf[] = {
67 '\\',
68 QtMiscUtils::toOct(uchar(c) / 64),
69 QtMiscUtils::toOct(uchar(c) % 64 / 8),
70 QtMiscUtils::toOct(uchar(c) % 8),
71 0
72 };
73 out += buf;
74 }
75 }
76 }
77
78 if (len < maxSize)
79 out += "...";
80
81 return out;
82}
83QT_END_NAMESPACE
84#endif
85
86#include "qplatformdefs.h"
87
88#include "qprocess.h"
89#include "qprocess_p.h"
90#include "qstandardpaths.h"
91#include "private/qcore_unix_p.h"
92#include "private/qlocking_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 <qsocketnotifier.h>
106#include <qthread.h>
107#include <qelapsedtimer.h>
108
109#ifdef Q_OS_QNX
110# include <sys/neutrino.h>
111#endif
112
113#include <errno.h>
114#include <stdlib.h>
115#include <string.h>
116
117#if QT_CONFIG(process)
118#include <forkfd.h>
119#endif
120
121QT_BEGIN_NAMESPACE
122
123#if !defined(Q_OS_DARWIN)
124
125QT_BEGIN_INCLUDE_NAMESPACE
126extern char **environ;
127QT_END_INCLUDE_NAMESPACE
128
129QProcessEnvironment QProcessEnvironment::systemEnvironment()
130{
131 QProcessEnvironment env;
132 const char *entry;
133 for (int count = 0; (entry = environ[count]); ++count) {
134 const char *equal = strchr(entry, '=');
135 if (!equal)
136 continue;
137
138 QByteArray name(entry, equal - entry);
139 QByteArray value(equal + 1);
140 env.d->vars.insert(QProcessEnvironmentPrivate::Key(name),
141 QProcessEnvironmentPrivate::Value(value));
142 }
143 return env;
144}
145
146#endif // !defined(Q_OS_DARWIN)
147
148#if QT_CONFIG(process)
149
150namespace {
151struct QProcessPoller
152{
153 QProcessPoller(const QProcessPrivate &proc);
154
155 int poll(int timeout);
156
157 pollfd &stdinPipe() { return pfds[0]; }
158 pollfd &stdoutPipe() { return pfds[1]; }
159 pollfd &stderrPipe() { return pfds[2]; }
160 pollfd &forkfd() { return pfds[3]; }
161 pollfd &childStartedPipe() { return pfds[4]; }
162
163 enum { n_pfds = 5 };
164 pollfd pfds[n_pfds];
165};
166
167QProcessPoller::QProcessPoller(const QProcessPrivate &proc)
168{
169 for (int i = 0; i < n_pfds; i++)
170 pfds[i] = qt_make_pollfd(-1, POLLIN);
171
172 stdoutPipe().fd = proc.stdoutChannel.pipe[0];
173 stderrPipe().fd = proc.stderrChannel.pipe[0];
174
175 if (!proc.writeBuffer.isEmpty()) {
176 stdinPipe().fd = proc.stdinChannel.pipe[1];
177 stdinPipe().events = POLLOUT;
178 }
179
180 forkfd().fd = proc.forkfd;
181
182 if (proc.processState == QProcess::Starting)
183 childStartedPipe().fd = proc.childStartedPipe[0];
184}
185
186int QProcessPoller::poll(int timeout)
187{
188 const nfds_t nfds = (childStartedPipe().fd == -1) ? 4 : 5;
189 return qt_poll_msecs(pfds, nfds, timeout);
190}
191} // anonymous namespace
192
193static bool qt_pollfd_check(const pollfd &pfd, short revents)
194{
195 return pfd.fd >= 0 && (pfd.revents & (revents | POLLHUP | POLLERR | POLLNVAL)) != 0;
196}
197
198static int qt_create_pipe(int *pipe)
199{
200 if (pipe[0] != -1)
201 qt_safe_close(pipe[0]);
202 if (pipe[1] != -1)
203 qt_safe_close(pipe[1]);
204 int pipe_ret = qt_safe_pipe(pipe);
205 if (pipe_ret != 0) {
206 qErrnoWarning("QProcessPrivate::createPipe: Cannot create pipe %p", pipe);
207 }
208 return pipe_ret;
209}
210
211void QProcessPrivate::destroyPipe(int *pipe)
212{
213 if (pipe[1] != -1) {
214 qt_safe_close(pipe[1]);
215 pipe[1] = -1;
216 }
217 if (pipe[0] != -1) {
218 qt_safe_close(pipe[0]);
219 pipe[0] = -1;
220 }
221}
222
223void QProcessPrivate::closeChannel(Channel *channel)
224{
225 destroyPipe(channel->pipe);
226}
227
228/*
229 Create the pipes to a QProcessPrivate::Channel.
230
231 This function must be called in order: stdin, stdout, stderr
232*/
233bool QProcessPrivate::openChannel(Channel &channel)
234{
235 Q_Q(QProcess);
236
237 if (&channel == &stderrChannel && processChannelMode == QProcess::MergedChannels) {
238 channel.pipe[0] = -1;
239 channel.pipe[1] = -1;
240 return true;
241 }
242
243 if (channel.type == Channel::Normal) {
244 // we're piping this channel to our own process
245 if (qt_create_pipe(channel.pipe) != 0)
246 return false;
247
248 // create the socket notifiers
249 if (threadData.loadRelaxed()->hasEventDispatcher()) {
250 if (&channel == &stdinChannel) {
251 channel.notifier = new QSocketNotifier(channel.pipe[1],
252 QSocketNotifier::Write, q);
253 channel.notifier->setEnabled(false);
254 QObject::connect(channel.notifier, SIGNAL(activated(QSocketDescriptor)),
255 q, SLOT(_q_canWrite()));
256 } else {
257 channel.notifier = new QSocketNotifier(channel.pipe[0],
258 QSocketNotifier::Read, q);
259 const char *receiver;
260 if (&channel == &stdoutChannel)
261 receiver = SLOT(_q_canReadStandardOutput());
262 else
263 receiver = SLOT(_q_canReadStandardError());
264 QObject::connect(channel.notifier, SIGNAL(activated(QSocketDescriptor)),
265 q, receiver);
266 }
267 }
268
269 return true;
270 } else if (channel.type == Channel::Redirect) {
271 // we're redirecting the channel to/from a file
272 QByteArray fname = QFile::encodeName(channel.file);
273
274 if (&channel == &stdinChannel) {
275 // try to open in read-only mode
276 channel.pipe[1] = -1;
277 if ( (channel.pipe[0] = qt_safe_open(fname, O_RDONLY)) != -1)
278 return true; // success
279 setErrorAndEmit(QProcess::FailedToStart,
280 QProcess::tr("Could not open input redirection for reading"));
281 } else {
282 int mode = O_WRONLY | O_CREAT;
283 if (channel.append)
284 mode |= O_APPEND;
285 else
286 mode |= O_TRUNC;
287
288 channel.pipe[0] = -1;
289 if ( (channel.pipe[1] = qt_safe_open(fname, mode, 0666)) != -1)
290 return true; // success
291
292 setErrorAndEmit(QProcess::FailedToStart,
293 QProcess::tr("Could not open input redirection for reading"));
294 }
295 cleanup();
296 return false;
297 } else {
298 Q_ASSERT_X(channel.process, "QProcess::start", "Internal error");
299
300 Channel *source;
301 Channel *sink;
302
303 if (channel.type == Channel::PipeSource) {
304 // we are the source
305 source = &channel;
306 sink = &channel.process->stdinChannel;
307
308 Q_ASSERT(source == &stdoutChannel);
309 Q_ASSERT(sink->process == this && sink->type == Channel::PipeSink);
310 } else {
311 // we are the sink;
312 source = &channel.process->stdoutChannel;
313 sink = &channel;
314
315 Q_ASSERT(sink == &stdinChannel);
316 Q_ASSERT(source->process == this && source->type == Channel::PipeSource);
317 }
318
319 if (source->pipe[1] != INVALID_Q_PIPE || sink->pipe[0] != INVALID_Q_PIPE) {
320 // already created, do nothing
321 return true;
322 } else {
323 Q_ASSERT(source->pipe[0] == INVALID_Q_PIPE && source->pipe[1] == INVALID_Q_PIPE);
324 Q_ASSERT(sink->pipe[0] == INVALID_Q_PIPE && sink->pipe[1] == INVALID_Q_PIPE);
325
326 Q_PIPE pipe[2] = { -1, -1 };
327 if (qt_create_pipe(pipe) != 0)
328 return false;
329 sink->pipe[0] = pipe[0];
330 source->pipe[1] = pipe[1];
331
332 return true;
333 }
334 }
335}
336
337static char **_q_dupEnvironment(const QProcessEnvironmentPrivate::Map &environment, int *envc)
338{
339 *envc = 0;
340 if (environment.isEmpty())
341 return nullptr;
342
343 char **envp = new char *[environment.count() + 2];
344 envp[environment.count()] = nullptr;
345 envp[environment.count() + 1] = nullptr;
346
347 auto it = environment.constBegin();
348 const auto end = environment.constEnd();
349 for ( ; it != end; ++it) {
350 QByteArray key = it.key();
351 QByteArray value = it.value().bytes();
352 key.reserve(key.length() + 1 + value.length());
353 key.append('=');
354 key.append(value);
355
356 envp[(*envc)++] = ::strdup(key.constData());
357 }
358
359 return envp;
360}
361
362void QProcessPrivate::startProcess()
363{
364 Q_Q(QProcess);
365
366#if defined (QPROCESS_DEBUG)
367 qDebug("QProcessPrivate::startProcess()");
368#endif
369
370 // Initialize pipes
371 if (!openChannel(stdinChannel) ||
372 !openChannel(stdoutChannel) ||
373 !openChannel(stderrChannel) ||
374 qt_create_pipe(childStartedPipe) != 0) {
375 setErrorAndEmit(QProcess::FailedToStart, qt_error_string(errno));
376 cleanup();
377 return;
378 }
379
380 if (threadData.loadRelaxed()->hasEventDispatcher()) {
381 startupSocketNotifier = new QSocketNotifier(childStartedPipe[0],
382 QSocketNotifier::Read, q);
383 QObject::connect(startupSocketNotifier, SIGNAL(activated(QSocketDescriptor)),
384 q, SLOT(_q_startupNotification()));
385 }
386
387 // Start the process (platform dependent)
388 q->setProcessState(QProcess::Starting);
389
390 // Create argument list with right number of elements, and set the final
391 // one to 0.
392 char **argv = new char *[arguments.count() + 2];
393 argv[arguments.count() + 1] = nullptr;
394
395 // Encode the program name.
396 QByteArray encodedProgramName = QFile::encodeName(program);
397#ifdef Q_OS_MAC
398 // allow invoking of .app bundles on the Mac.
399 QFileInfo fileInfo(program);
400 if (encodedProgramName.endsWith(".app") && fileInfo.isDir()) {
401 QCFType<CFURLRef> url = CFURLCreateWithFileSystemPath(0,
402 QCFString(fileInfo.absoluteFilePath()),
403 kCFURLPOSIXPathStyle, true);
404 {
405 // CFBundle is not reentrant, since CFBundleCreate might return a reference
406 // to a cached bundle object. Protect the bundle calls with a mutex lock.
407 static QBasicMutex cfbundleMutex;
408 const auto locker = qt_scoped_lock(cfbundleMutex);
409 QCFType<CFBundleRef> bundle = CFBundleCreate(0, url);
410 // 'executableURL' can be either relative or absolute ...
411 QCFType<CFURLRef> executableURL = CFBundleCopyExecutableURL(bundle);
412 // not to depend on caching - make sure it's always absolute.
413 url = CFURLCopyAbsoluteURL(executableURL);
414 }
415 if (url) {
416 const QCFString str = CFURLCopyFileSystemPath(url, kCFURLPOSIXPathStyle);
417 encodedProgramName += (QDir::separator() + QDir(program).relativeFilePath(QString::fromCFString(str))).toUtf8();
418 }
419 }
420#endif
421
422 // Add the program name to the argument list.
423 argv[0] = nullptr;
424 if (!program.contains(QLatin1Char('/'))) {
425 const QString &exeFilePath = QStandardPaths::findExecutable(program);
426 if (!exeFilePath.isEmpty()) {
427 const QByteArray &tmp = QFile::encodeName(exeFilePath);
428 argv[0] = ::strdup(tmp.constData());
429 }
430 }
431 if (!argv[0])
432 argv[0] = ::strdup(encodedProgramName.constData());
433
434 // Add every argument to the list
435 for (int i = 0; i < arguments.count(); ++i)
436 argv[i + 1] = ::strdup(QFile::encodeName(arguments.at(i)).constData());
437
438 // Duplicate the environment.
439 int envc = 0;
440 char **envp = nullptr;
441 if (environment.d.constData()) {
442 envp = _q_dupEnvironment(environment.d.constData()->vars, &envc);
443 }
444
445 // Encode the working directory if it's non-empty, otherwise just pass 0.
446 const char *workingDirPtr = nullptr;
447 QByteArray encodedWorkingDirectory;
448 if (!workingDirectory.isEmpty()) {
449 encodedWorkingDirectory = QFile::encodeName(workingDirectory);
450 workingDirPtr = encodedWorkingDirectory.constData();
451 }
452
453 // Select FFD_USE_FORK and FFD_VFORK_SEMANTICS based on whether there's
454 // user code running in the child process: if there is, we don't know what
455 // the user will want to do, so we err on the safe side and request an
456 // actual fork() (for example, the user could attempt to do some
457 // synchronization with the parent process). But if there isn't, then our
458 // code in execChild() is just a handful of dup2() and a chdir(), so it's
459 // safe with vfork semantics: suspend the parent execution until the child
460 // either execve()s or _exit()s.
461 int ffdflags = FFD_CLOEXEC;
462 if (childProcessModifier)
463 ffdflags |= FFD_USE_FORK;
464
465 // QTBUG-86285
466#if !QT_CONFIG(forkfd_pidfd)
467 ffdflags |= FFD_USE_FORK;
468#endif
469
470 pid_t childPid;
471 forkfd = ::forkfd(ffdflags , &childPid);
472 int lastForkErrno = errno;
473 if (forkfd != FFD_CHILD_PROCESS) {
474 // Parent process.
475 // Clean up duplicated memory.
476 for (int i = 0; i <= arguments.count(); ++i)
477 free(argv[i]);
478 for (int i = 0; i < envc; ++i)
479 free(envp[i]);
480 delete [] argv;
481 delete [] envp;
482 }
483
484 // On QNX, if spawnChild failed, childPid will be -1 but forkfd is still 0.
485 // This is intentional because we only want to handle failure to fork()
486 // here, which is a rare occurrence. Handling of the failure to start is
487 // done elsewhere.
488 if (forkfd == -1) {
489 // Cleanup, report error and return
490#if defined (QPROCESS_DEBUG)
491 qDebug("fork failed: %ls", qUtf16Printable(qt_error_string(lastForkErrno)));
492#endif
493 q->setProcessState(QProcess::NotRunning);
494 setErrorAndEmit(QProcess::FailedToStart,
495 QProcess::tr("Resource error (fork failure): %1").arg(qt_error_string(lastForkErrno)));
496 cleanup();
497 return;
498 }
499
500 // Start the child.
501 if (forkfd == FFD_CHILD_PROCESS) {
502 execChild(workingDirPtr, argv, envp);
503 ::_exit(-1);
504 }
505
506 pid = qint64(childPid);
507
508 // parent
509 // close the ends we don't use and make all pipes non-blocking
510 qt_safe_close(childStartedPipe[1]);
511 childStartedPipe[1] = -1;
512
513 if (stdinChannel.pipe[0] != -1) {
514 qt_safe_close(stdinChannel.pipe[0]);
515 stdinChannel.pipe[0] = -1;
516 }
517
518 if (stdinChannel.pipe[1] != -1)
519 ::fcntl(stdinChannel.pipe[1], F_SETFL, ::fcntl(stdinChannel.pipe[1], F_GETFL) | O_NONBLOCK);
520
521 if (stdoutChannel.pipe[1] != -1) {
522 qt_safe_close(stdoutChannel.pipe[1]);
523 stdoutChannel.pipe[1] = -1;
524 }
525
526 if (stdoutChannel.pipe[0] != -1)
527 ::fcntl(stdoutChannel.pipe[0], F_SETFL, ::fcntl(stdoutChannel.pipe[0], F_GETFL) | O_NONBLOCK);
528
529 if (stderrChannel.pipe[1] != -1) {
530 qt_safe_close(stderrChannel.pipe[1]);
531 stderrChannel.pipe[1] = -1;
532 }
533 if (stderrChannel.pipe[0] != -1)
534 ::fcntl(stderrChannel.pipe[0], F_SETFL, ::fcntl(stderrChannel.pipe[0], F_GETFL) | O_NONBLOCK);
535
536 if (threadData.loadRelaxed()->eventDispatcher.loadAcquire()) {
537 deathNotifier = new QSocketNotifier(forkfd, QSocketNotifier::Read, q);
538 QObject::connect(deathNotifier, SIGNAL(activated(QSocketDescriptor)),
539 q, SLOT(_q_processDied()));
540 }
541}
542
543struct ChildError
544{
545 int code;
546 char function[8];
547};
548
549void QProcessPrivate::execChild(const char *workingDir, char **argv, char **envp)
550{
551 ::signal(SIGPIPE, SIG_DFL); // reset the signal that we ignored
552
553 ChildError error = { 0, {} }; // force zeroing of function[8]
554
555 // copy the stdin socket if asked to (without closing on exec)
556 if (inputChannelMode != QProcess::ForwardedInputChannel)
557 qt_safe_dup2(stdinChannel.pipe[0], STDIN_FILENO, 0);
558
559 // copy the stdout and stderr if asked to
560 if (processChannelMode != QProcess::ForwardedChannels) {
561 if (processChannelMode != QProcess::ForwardedOutputChannel)
562 qt_safe_dup2(stdoutChannel.pipe[1], STDOUT_FILENO, 0);
563
564 // merge stdout and stderr if asked to
565 if (processChannelMode == QProcess::MergedChannels) {
566 qt_safe_dup2(STDOUT_FILENO, STDERR_FILENO, 0);
567 } else if (processChannelMode != QProcess::ForwardedErrorChannel) {
568 qt_safe_dup2(stderrChannel.pipe[1], STDERR_FILENO, 0);
569 }
570 }
571
572 // make sure this fd is closed if execv() succeeds
573 qt_safe_close(childStartedPipe[0]);
574
575 // enter the working directory
576 if (workingDir && QT_CHDIR(workingDir) == -1) {
577 // failed, stop the process
578 strcpy(error.function, "chdir");
579 goto report_errno;
580 }
581
582 if (childProcessModifier)
583 childProcessModifier();
584
585 // execute the process
586 if (!envp) {
587 qt_safe_execv(argv[0], argv);
588 strcpy(error.function, "execvp");
589 } else {
590#if defined (QPROCESS_DEBUG)
591 fprintf(stderr, "QProcessPrivate::execChild() starting %s\n", argv[0]);
592#endif
593 qt_safe_execve(argv[0], argv, envp);
594 strcpy(error.function, "execve");
595 }
596
597 // notify failure
598 // don't use strerror or any other routines that may allocate memory, since
599 // some buggy libc versions can deadlock on locked mutexes.
600report_errno:
601 error.code = errno;
602 qt_safe_write(childStartedPipe[1], &error, sizeof(error));
603 childStartedPipe[1] = -1;
604}
605
606bool QProcessPrivate::processStarted(QString *errorMessage)
607{
608 ChildError buf;
609 int ret = qt_safe_read(childStartedPipe[0], &buf, sizeof(buf));
610
611 if (startupSocketNotifier) {
612 startupSocketNotifier->setEnabled(false);
613 startupSocketNotifier->deleteLater();
614 startupSocketNotifier = nullptr;
615 }
616 qt_safe_close(childStartedPipe[0]);
617 childStartedPipe[0] = -1;
618
619#if defined (QPROCESS_DEBUG)
620 qDebug("QProcessPrivate::processStarted() == %s", i <= 0 ? "true" : "false");
621#endif
622
623 // did we read an error message?
624 if (ret > 0 && errorMessage)
625 *errorMessage = QLatin1String(buf.function) + QLatin1String(": ") + qt_error_string(buf.code);
626
627 return ret <= 0;
628}
629
630qint64 QProcessPrivate::bytesAvailableInChannel(const Channel *channel) const
631{
632 Q_ASSERT(channel->pipe[0] != INVALID_Q_PIPE);
633 int nbytes = 0;
634 qint64 available = 0;
635 if (::ioctl(channel->pipe[0], FIONREAD, (char *) &nbytes) >= 0)
636 available = (qint64) nbytes;
637#if defined (QPROCESS_DEBUG)
638 qDebug("QProcessPrivate::bytesAvailableInChannel(%d) == %lld", int(channel - &stdinChannel), available);
639#endif
640 return available;
641}
642
643qint64 QProcessPrivate::readFromChannel(const Channel *channel, char *data, qint64 maxlen)
644{
645 Q_ASSERT(channel->pipe[0] != INVALID_Q_PIPE);
646 qint64 bytesRead = qt_safe_read(channel->pipe[0], data, maxlen);
647#if defined QPROCESS_DEBUG
648 int save_errno = errno;
649 qDebug("QProcessPrivate::readFromChannel(%d, %p \"%s\", %lld) == %lld",
650 int(channel - &stdinChannel),
651 data, qt_prettyDebug(data, bytesRead, 16).constData(), maxlen, bytesRead);
652 errno = save_errno;
653#endif
654 if (bytesRead == -1 && errno == EWOULDBLOCK)
655 return -2;
656 return bytesRead;
657}
658
659bool QProcessPrivate::writeToStdin()
660{
661 const char *data = writeBuffer.readPointer();
662 const qint64 bytesToWrite = writeBuffer.nextDataBlockSize();
663
664 qint64 written = qt_safe_write_nosignal(stdinChannel.pipe[1], data, bytesToWrite);
665#if defined QPROCESS_DEBUG
666 qDebug("QProcessPrivate::writeToStdin(), write(%p \"%s\", %lld) == %lld",
667 data, qt_prettyDebug(data, bytesToWrite, 16).constData(), bytesToWrite, written);
668 if (written == -1)
669 qDebug("QProcessPrivate::writeToStdin(), failed to write (%ls)", qUtf16Printable(qt_error_string(errno)));
670#endif
671 if (written == -1) {
672 // If the O_NONBLOCK flag is set and If some data can be written without blocking
673 // the process, write() will transfer what it can and return the number of bytes written.
674 // Otherwise, it will return -1 and set errno to EAGAIN
675 if (errno == EAGAIN)
676 return true;
677
678 closeChannel(&stdinChannel);
679 setErrorAndEmit(QProcess::WriteError);
680 return false;
681 }
682 writeBuffer.free(written);
683 if (!emittedBytesWritten && written != 0) {
684 emittedBytesWritten = true;
685 emit q_func()->bytesWritten(written);
686 emittedBytesWritten = false;
687 }
688 return true;
689}
690
691void QProcessPrivate::terminateProcess()
692{
693#if defined (QPROCESS_DEBUG)
694 qDebug("QProcessPrivate::terminateProcess()");
695#endif
696 if (pid)
697 ::kill(pid_t(pid), SIGTERM);
698}
699
700void QProcessPrivate::killProcess()
701{
702#if defined (QPROCESS_DEBUG)
703 qDebug("QProcessPrivate::killProcess()");
704#endif
705 if (pid)
706 ::kill(pid_t(pid), SIGKILL);
707}
708
709bool QProcessPrivate::waitForStarted(int msecs)
710{
711#if defined (QPROCESS_DEBUG)
712 qDebug("QProcessPrivate::waitForStarted(%d) waiting for child to start (fd = %d)", msecs,
713 childStartedPipe[0]);
714#endif
715
716 pollfd pfd = qt_make_pollfd(childStartedPipe[0], POLLIN);
717
718 if (qt_poll_msecs(&pfd, 1, msecs) == 0) {
719 setError(QProcess::Timedout);
720#if defined (QPROCESS_DEBUG)
721 qDebug("QProcessPrivate::waitForStarted(%d) == false (timed out)", msecs);
722#endif
723 return false;
724 }
725
726 bool startedEmitted = _q_startupNotification();
727#if defined (QPROCESS_DEBUG)
728 qDebug("QProcessPrivate::waitForStarted() == %s", startedEmitted ? "true" : "false");
729#endif
730 return startedEmitted;
731}
732
733bool QProcessPrivate::waitForReadyRead(int msecs)
734{
735#if defined (QPROCESS_DEBUG)
736 qDebug("QProcessPrivate::waitForReadyRead(%d)", msecs);
737#endif
738
739 QElapsedTimer stopWatch;
740 stopWatch.start();
741
742 forever {
743 QProcessPoller poller(*this);
744
745 int timeout = qt_subtract_from_timeout(msecs, stopWatch.elapsed());
746 int ret = poller.poll(timeout);
747
748 if (ret < 0) {
749 break;
750 }
751 if (ret == 0) {
752 setError(QProcess::Timedout);
753 return false;
754 }
755
756 if (qt_pollfd_check(poller.childStartedPipe(), POLLIN)) {
757 if (!_q_startupNotification())
758 return false;
759 }
760
761 bool readyReadEmitted = false;
762 if (qt_pollfd_check(poller.stdoutPipe(), POLLIN)) {
763 bool canRead = _q_canReadStandardOutput();
764 if (currentReadChannel == QProcess::StandardOutput && canRead)
765 readyReadEmitted = true;
766 }
767 if (qt_pollfd_check(poller.stderrPipe(), POLLIN)) {
768 bool canRead = _q_canReadStandardError();
769 if (currentReadChannel == QProcess::StandardError && canRead)
770 readyReadEmitted = true;
771 }
772 if (readyReadEmitted)
773 return true;
774
775 if (qt_pollfd_check(poller.stdinPipe(), POLLOUT))
776 _q_canWrite();
777
778 // Signals triggered by I/O may have stopped this process:
779 if (processState == QProcess::NotRunning)
780 return false;
781
782 if (qt_pollfd_check(poller.forkfd(), POLLIN)) {
783 if (_q_processDied())
784 return false;
785 }
786 }
787 return false;
788}
789
790bool QProcessPrivate::waitForBytesWritten(int msecs)
791{
792#if defined (QPROCESS_DEBUG)
793 qDebug("QProcessPrivate::waitForBytesWritten(%d)", msecs);
794#endif
795
796 QElapsedTimer stopWatch;
797 stopWatch.start();
798
799 while (!writeBuffer.isEmpty()) {
800 QProcessPoller poller(*this);
801
802 int timeout = qt_subtract_from_timeout(msecs, stopWatch.elapsed());
803 int ret = poller.poll(timeout);
804
805 if (ret < 0) {
806 break;
807 }
808
809 if (ret == 0) {
810 setError(QProcess::Timedout);
811 return false;
812 }
813
814 if (qt_pollfd_check(poller.childStartedPipe(), POLLIN)) {
815 if (!_q_startupNotification())
816 return false;
817 }
818
819 if (qt_pollfd_check(poller.stdinPipe(), POLLOUT))
820 return _q_canWrite();
821
822 if (qt_pollfd_check(poller.stdoutPipe(), POLLIN))
823 _q_canReadStandardOutput();
824
825 if (qt_pollfd_check(poller.stderrPipe(), POLLIN))
826 _q_canReadStandardError();
827
828 // Signals triggered by I/O may have stopped this process:
829 if (processState == QProcess::NotRunning)
830 return false;
831
832 if (qt_pollfd_check(poller.forkfd(), POLLIN)) {
833 if (_q_processDied())
834 return false;
835 }
836 }
837
838 return false;
839}
840
841bool QProcessPrivate::waitForFinished(int msecs)
842{
843#if defined (QPROCESS_DEBUG)
844 qDebug("QProcessPrivate::waitForFinished(%d)", msecs);
845#endif
846
847 QElapsedTimer stopWatch;
848 stopWatch.start();
849
850 forever {
851 QProcessPoller poller(*this);
852
853 int timeout = qt_subtract_from_timeout(msecs, stopWatch.elapsed());
854 int ret = poller.poll(timeout);
855
856 if (ret < 0) {
857 break;
858 }
859 if (ret == 0) {
860 setError(QProcess::Timedout);
861 return false;
862 }
863
864 if (qt_pollfd_check(poller.childStartedPipe(), POLLIN)) {
865 if (!_q_startupNotification())
866 return false;
867 }
868 if (qt_pollfd_check(poller.stdinPipe(), POLLOUT))
869 _q_canWrite();
870
871 if (qt_pollfd_check(poller.stdoutPipe(), POLLIN))
872 _q_canReadStandardOutput();
873
874 if (qt_pollfd_check(poller.stderrPipe(), POLLIN))
875 _q_canReadStandardError();
876
877 // Signals triggered by I/O may have stopped this process:
878 if (processState == QProcess::NotRunning)
879 return true;
880
881 if (qt_pollfd_check(poller.forkfd(), POLLIN)) {
882 if (_q_processDied())
883 return true;
884 }
885 }
886 return false;
887}
888
889void QProcessPrivate::findExitCode()
890{
891}
892
893bool QProcessPrivate::waitForDeadChild()
894{
895 if (forkfd == -1)
896 return true; // child has already exited
897
898 // read the process information from our fd
899 forkfd_info info;
900 int ret;
901 EINTR_LOOP(ret, forkfd_wait(forkfd, &info, nullptr));
902
903 exitCode = info.status;
904 crashed = info.code != CLD_EXITED;
905
906 delete deathNotifier;
907 deathNotifier = nullptr;
908
909 EINTR_LOOP(ret, forkfd_close(forkfd));
910 forkfd = -1; // Child is dead, don't try to kill it anymore
911
912#if defined QPROCESS_DEBUG
913 qDebug() << "QProcessPrivate::waitForDeadChild() dead with exitCode"
914 << exitCode << ", crashed?" << crashed;
915#endif
916 return true;
917}
918
919bool QProcessPrivate::startDetached(qint64 *pid)
920{
921 QByteArray encodedWorkingDirectory = QFile::encodeName(workingDirectory);
922
923 // To catch the startup of the child
924 int startedPipe[2];
925 if (qt_safe_pipe(startedPipe) != 0)
926 return false;
927 // To communicate the pid of the child
928 int pidPipe[2];
929 if (qt_safe_pipe(pidPipe) != 0) {
930 qt_safe_close(startedPipe[0]);
931 qt_safe_close(startedPipe[1]);
932 return false;
933 }
934
935 if ((stdinChannel.type == Channel::Redirect && !openChannel(stdinChannel))
936 || (stdoutChannel.type == Channel::Redirect && !openChannel(stdoutChannel))
937 || (stderrChannel.type == Channel::Redirect && !openChannel(stderrChannel))) {
938 closeChannel(&stdinChannel);
939 closeChannel(&stdoutChannel);
940 closeChannel(&stderrChannel);
941 qt_safe_close(pidPipe[0]);
942 qt_safe_close(pidPipe[1]);
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, nullptr);
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 // copy the stdin socket if asked to (without closing on exec)
965 if (stdinChannel.type == Channel::Redirect)
966 qt_safe_dup2(stdinChannel.pipe[0], STDIN_FILENO, 0);
967
968 // copy the stdout and stderr if asked to
969 if (stdoutChannel.type == Channel::Redirect)
970 qt_safe_dup2(stdoutChannel.pipe[1], STDOUT_FILENO, 0);
971 if (stderrChannel.type == Channel::Redirect)
972 qt_safe_dup2(stderrChannel.pipe[1], STDERR_FILENO, 0);
973
974 if (!encodedWorkingDirectory.isEmpty()) {
975 if (QT_CHDIR(encodedWorkingDirectory.constData()) == -1)
976 qWarning("QProcessPrivate::startDetached: failed to chdir to %s", encodedWorkingDirectory.constData());
977 }
978
979 char **argv = new char *[arguments.size() + 2];
980 for (int i = 0; i < arguments.size(); ++i)
981 argv[i + 1] = ::strdup(QFile::encodeName(arguments.at(i)).constData());
982 argv[arguments.size() + 1] = nullptr;
983
984 // Duplicate the environment.
985 int envc = 0;
986 char **envp = nullptr;
987 if (environment.d.constData()) {
988 envp = _q_dupEnvironment(environment.d.constData()->vars, &envc);
989 }
990
991 QByteArray tmp;
992 if (!program.contains(QLatin1Char('/'))) {
993 const QString &exeFilePath = QStandardPaths::findExecutable(program);
994 if (!exeFilePath.isEmpty())
995 tmp = QFile::encodeName(exeFilePath);
996 }
997 if (tmp.isEmpty())
998 tmp = QFile::encodeName(program);
999 argv[0] = tmp.data();
1000
1001 if (envp)
1002 qt_safe_execve(argv[0], argv, envp);
1003 else
1004 qt_safe_execv(argv[0], argv);
1005
1006 struct sigaction noaction;
1007 memset(&noaction, 0, sizeof(noaction));
1008 noaction.sa_handler = SIG_IGN;
1009 ::sigaction(SIGPIPE, &noaction, nullptr);
1010
1011 // '\1' means execv failed
1012 char c = '\1';
1013 qt_safe_write(startedPipe[1], &c, 1);
1014 qt_safe_close(startedPipe[1]);
1015 ::_exit(1);
1016 } else if (doubleForkPid == -1) {
1017 struct sigaction noaction;
1018 memset(&noaction, 0, sizeof(noaction));
1019 noaction.sa_handler = SIG_IGN;
1020 ::sigaction(SIGPIPE, &noaction, nullptr);
1021
1022 // '\2' means internal error
1023 char c = '\2';
1024 qt_safe_write(startedPipe[1], &c, 1);
1025 }
1026
1027 qt_safe_close(startedPipe[1]);
1028 qt_safe_write(pidPipe[1], (const char *)&doubleForkPid, sizeof(pid_t));
1029 if (QT_CHDIR("/") == -1)
1030 qWarning("QProcessPrivate::startDetached: failed to chdir to /");
1031 ::_exit(1);
1032 }
1033
1034 closeChannel(&stdinChannel);
1035 closeChannel(&stdoutChannel);
1036 closeChannel(&stderrChannel);
1037 qt_safe_close(startedPipe[1]);
1038 qt_safe_close(pidPipe[1]);
1039
1040 if (childPid == -1) {
1041 qt_safe_close(startedPipe[0]);
1042 qt_safe_close(pidPipe[0]);
1043 return false;
1044 }
1045
1046 char reply = '\0';
1047 int startResult = qt_safe_read(startedPipe[0], &reply, 1);
1048 int result;
1049 qt_safe_close(startedPipe[0]);
1050 qt_safe_waitpid(childPid, &result, 0);
1051 bool success = (startResult != -1 && reply == '\0');
1052 if (success && pid) {
1053 pid_t actualPid = 0;
1054 if (qt_safe_read(pidPipe[0], (char *)&actualPid, sizeof(pid_t)) == sizeof(pid_t)) {
1055 *pid = actualPid;
1056 } else {
1057 *pid = 0;
1058 }
1059 }
1060 qt_safe_close(pidPipe[0]);
1061 return success;
1062}
1063
1064#endif // QT_CONFIG(process)
1065
1066QT_END_NAMESPACE
1067