Qt 4.8
Classes | Macros | Functions | Variables
qprocess_unix.cpp File Reference
#include "qdebug.h"
#include "qplatformdefs.h"
#include "qprocess.h"
#include "qprocess_p.h"
#include "private/qcore_unix_p.h"
#include <private/qcore_mac_p.h>
#include <private/qcoreapplication_p.h>
#include <private/qthread_p.h>
#include <qfile.h>
#include <qfileinfo.h>
#include <qlist.h>
#include <qhash.h>
#include <qmutex.h>
#include <qsemaphore.h>
#include <qsocketnotifier.h>
#include <qthread.h>
#include <qelapsedtimer.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <crt_externs.h>
#include "qprocess_unix.moc"

Go to the source code of this file.

Classes

struct  QProcessInfo
 
class  QProcessManager
 

Macros

#define environ   (*_NSGetEnviron())
 

Functions

static char ** _q_dupEnvironment (const QProcessEnvironmentPrivate::Hash &environment, int *envc)
 
static void add_fd (int &nfds, int fd, fd_set *fdset)
 
static QProcessManagerprocessManager ()
 
 Q_GLOBAL_STATIC (QMutex, cfbundleMutex)
 
static int qt_create_pipe (int *pipe)
 
static void qt_sa_sigchld_sigaction (int signum, siginfo_t *info, void *context)
 
static int qt_timeout_value (int msecs, int elapsed)
 
static int select_msecs (int nfds, fd_set *fdread, fd_set *fdwrite, int timeout)
 

Variables

static const int errorBufferMax = 512
 
static QBasicAtomicInt idCounter = Q_BASIC_ATOMIC_INITIALIZER(1)
 
static int qt_qprocess_deadChild_pipe [2]
 
static struct sigaction qt_sa_old_sigchld_handler
 

Macro Definition Documentation

◆ environ

#define environ   (*_NSGetEnviron())

Definition at line 493 of file qprocess_unix.cpp.

Function Documentation

◆ _q_dupEnvironment()

static char** _q_dupEnvironment ( const QProcessEnvironmentPrivate::Hash environment,
int *  envc 
)
static

Definition at line 518 of file qprocess_unix.cpp.

Referenced by QProcessPrivate::startProcess().

519 {
520  *envc = 0;
521  if (environment.isEmpty())
522  return 0;
523 
524  // if LD_LIBRARY_PATH exists in the current environment, but
525  // not in the environment list passed by the programmer, then
526  // copy it over.
527 #if defined(Q_OS_MAC)
528  static const char libraryPath[] = "DYLD_LIBRARY_PATH";
529 #else
530  static const char libraryPath[] = "LD_LIBRARY_PATH";
531 #endif
532  const QByteArray envLibraryPath = qgetenv(libraryPath);
533  bool needToAddLibraryPath = !envLibraryPath.isEmpty() &&
534  !environment.contains(QProcessEnvironmentPrivate::Key(QByteArray(libraryPath)));
535 
536  char **envp = new char *[environment.count() + 2];
537  envp[environment.count()] = 0;
538  envp[environment.count() + 1] = 0;
539 
542  for ( ; it != end; ++it) {
543  QByteArray key = it.key().key;
544  QByteArray value = it.value().bytes();
545  key.reserve(key.length() + 1 + value.length());
546  key.append('=');
547  key.append(value);
548 
549  envp[(*envc)++] = ::strdup(key.constData());
550  }
551 
552  if (needToAddLibraryPath)
553  envp[(*envc)++] = ::strdup(QByteArray(QByteArray(libraryPath) + '=' +
554  envLibraryPath).constData());
555  return envp;
556 }
Q_CORE_EXPORT QByteArray qgetenv(const char *varName)
#define it(className, varName)
QByteArray & append(char c)
Appends the character ch to this byte array.
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
const_iterator ConstIterator
Qt-style synonym for QHash::const_iterator.
Definition: qhash.h:474
bool contains(const Key &key) const
Returns true if the hash contains an item with the key; otherwise returns false.
Definition: qhash.h:872
bool isEmpty() const
Returns true if the hash contains no items; otherwise returns false.
Definition: qhash.h:297
int length() const
Same as size().
Definition: qbytearray.h:356
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the hash.
Definition: qhash.h:466
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:469
int key
int count(const Key &key) const
Returns the number of items associated with the key.
Definition: qhash.h:719
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
void reserve(int size)
Attempts to allocate memory for at least size bytes.
Definition: qbytearray.h:449
static const KeyPair *const end

◆ add_fd()

static void add_fd ( int &  nfds,
int  fd,
fd_set *  fdset 
)
inlinestatic

Definition at line 161 of file qprocess_unix.cpp.

Referenced by QProcessPrivate::waitForBytesWritten(), QProcessPrivate::waitForFinished(), and QProcessPrivate::waitForReadyRead().

162 {
163  FD_SET(fd, fdset);
164  if ((fd) > nfds)
165  nfds = fd;
166 }

◆ processManager()

static QProcessManager* processManager ( )
static

Definition at line 198 of file qprocess_unix.cpp.

Referenced by QProcessPrivate::findExitCode(), QProcessPrivate::startDetached(), QProcessPrivate::startProcess(), and QProcessPrivate::waitForDeadChild().

198  {
199  // The constructor of QProcessManager should be called only once
200  // so we cannot use Q_GLOBAL_STATIC directly for QProcessManager
201  QMutex *mutex = processManagerGlobalMutex();
202  QMutexLocker locker(mutex);
204  return &processManager;
205 }
The QMutex class provides access serialization between threads.
Definition: qmutex.h:60
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
static QProcessManager * processManager()

◆ Q_GLOBAL_STATIC()

Q_GLOBAL_STATIC ( QMutex  ,
cfbundleMutex   
)

Referenced by _q_dupEnvironment().

◆ qt_create_pipe()

static int qt_create_pipe ( int *  pipe)
static

Definition at line 352 of file qprocess_unix.cpp.

Referenced by QProcessPrivate::createChannel(), and QProcessPrivate::startProcess().

353 {
354  if (pipe[0] != -1)
355  qt_safe_close(pipe[0]);
356  if (pipe[1] != -1)
357  qt_safe_close(pipe[1]);
358  int pipe_ret = qt_safe_pipe(pipe);
359  if (pipe_ret != 0) {
360  qWarning("QProcessPrivate::createPipe: Cannot create pipe %p: %s",
362  }
363  return pipe_ret;
364 }
QString qt_error_string(int errorCode)
Definition: qglobal.cpp:2600
static int qt_safe_close(int fd)
Definition: qcore_unix_p.h:297
Q_CORE_EXPORT void qWarning(const char *,...)
static int qt_safe_pipe(int pipefd[2], int flags=0)
Definition: qcore_unix_p.h:191
#define qPrintable(string)
Definition: qglobal.h:1750
int errno

◆ qt_sa_sigchld_sigaction()

static void qt_sa_sigchld_sigaction ( int  signum,
siginfo_t *  info,
void *  context 
)
static

Definition at line 131 of file qprocess_unix.cpp.

Referenced by QProcessManager::QProcessManager(), and QProcessManager::~QProcessManager().

132 {
133  // *Never* use the info or contect variables in this function
134  // (except for passing them to the next signal in the chain).
135  // We cannot be sure if another library or if the application
136  // installed a signal handler for SIGCHLD without SA_SIGINFO
137  // and fails to pass the arguments to us. If they do that,
138  // these arguments contain garbage and we'd most likely crash.
139 
141 #if defined (QPROCESS_DEBUG)
142  fprintf(stderr, "*** SIGCHLD\n");
143 #endif
144 
145  // load as volatile
146  volatile struct sigaction *vsa = &qt_sa_old_sigchld_handler;
147 
148  if (qt_sa_old_sigchld_handler.sa_flags & SA_SIGINFO) {
149  void (*oldAction)(int, siginfo_t *, void *) = vsa->sa_sigaction;
150 
151  if (oldAction)
152  oldAction(signum, info, context);
153  } else {
154  void (*oldAction)(int) = vsa->sa_handler;
155 
156  if (oldAction && oldAction != SIG_IGN)
157  oldAction(signum);
158  }
159 }
static int qt_qprocess_deadChild_pipe[2]
static mach_timebase_info_data_t info
static struct sigaction qt_sa_old_sigchld_handler
int sigaction(int, const struct sigaction *, struct sigaction *)
if(void) toggleToolbarShown
static qint64 qt_safe_write(int fd, const void *data, qint64 len)
Definition: qcore_unix_p.h:282

◆ qt_timeout_value()

static int qt_timeout_value ( int  msecs,
int  elapsed 
)
static

Definition at line 1045 of file qprocess_unix.cpp.

Referenced by QProcessPrivate::waitForBytesWritten(), QProcessPrivate::waitForFinished(), and QProcessPrivate::waitForReadyRead().

1046 {
1047  if (msecs == -1)
1048  return -1;
1049 
1050  int timeout = msecs - elapsed;
1051  return timeout < 0 ? 0 : timeout;
1052 }
static double elapsed(qint64 after, qint64 before)

◆ select_msecs()

static int select_msecs ( int  nfds,
fd_set *  fdread,
fd_set *  fdwrite,
int  timeout 
)
static

Definition at line 1030 of file qprocess_unix.cpp.

Referenced by QProcessPrivate::waitForBytesWritten(), QProcessPrivate::waitForFinished(), QProcessPrivate::waitForReadyRead(), QProcessPrivate::waitForStarted(), and QProcessPrivate::waitForWrite().

1031 {
1032  if (timeout < 0)
1033  return qt_safe_select(nfds, fdread, fdwrite, 0, 0);
1034 
1035  struct timeval tv;
1036  tv.tv_sec = timeout / 1000;
1037  tv.tv_usec = (timeout % 1000) * 1000;
1038  return qt_safe_select(nfds, fdread, fdwrite, 0, &tv);
1039 }
int qt_safe_select(int nfds, fd_set *fdread, fd_set *fdwrite, fd_set *fdexcept, const struct timeval *orig_timeout)
Definition: qcore_unix.cpp:73

Variable Documentation

◆ errorBufferMax

const int errorBufferMax = 512
static

Definition at line 120 of file qprocess_unix.cpp.

Referenced by QProcessPrivate::processStarted().

◆ idCounter

Definition at line 309 of file qprocess_unix.cpp.

◆ qt_qprocess_deadChild_pipe

int qt_qprocess_deadChild_pipe[2]
static

Definition at line 129 of file qprocess_unix.cpp.

◆ qt_sa_old_sigchld_handler

struct sigaction qt_sa_old_sigchld_handler
static