summaryrefslogtreecommitdiff
authoreilers <eilers>2004-04-10 13:29:30 (UTC)
committer eilers <eilers>2004-04-10 13:29:30 (UTC)
commit8350779f2628ce800b4bca7cef7d2b9bb644879e (patch) (side-by-side diff)
tree90895aef8cede58449905601406e1119cd5d4a29
parent24fcd64ca69588f3f6364dbc5e14df9f1686134a (diff)
downloadopie-8350779f2628ce800b4bca7cef7d2b9bb644879e.zip
opie-8350779f2628ce800b4bca7cef7d2b9bb644879e.tar.gz
opie-8350779f2628ce800b4bca7cef7d2b9bb644879e.tar.bz2
resource.h must stay behind time.h to compile on mac
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--core/launcher/qprocess_unix.cpp2
1 files changed, 1 insertions, 1 deletions
diff --git a/core/launcher/qprocess_unix.cpp b/core/launcher/qprocess_unix.cpp
index d62e4e6..56e1b1d 100644
--- a/core/launcher/qprocess_unix.cpp
+++ b/core/launcher/qprocess_unix.cpp
@@ -1,439 +1,439 @@
/**********************************************************************
** Copyright (C) 2000-2002 Trolltech AS. All rights reserved.
**
** This file is part of the Qtopia Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/
// Solaris redefines connect -> __xnet_connect with _XOPEN_SOURCE_EXTENDED.
#if defined(connect)
#undef connect
#endif
#include "qprocess.h"
/* OPIE */
#include <opie2/odebug.h>
using namespace Opie::Core;
/* QT */
#ifndef QT_NO_PROCESS
#include <qapplication.h>
#include <qqueue.h>
#include <qlist.h>
#include <qsocketnotifier.h>
#include <qtimer.h>
#include <qregexp.h>
#include "qcleanuphandler_p.h"
/* STD */
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <sys/fcntl.h>
-#include <sys/resource.h>
#include <errno.h>
#ifdef Q_OS_MACX
#include <sys/time.h>
#endif
+#include <sys/resource.h>
#ifdef __MIPSEL__
# ifndef SOCK_DGRAM
# define SOCK_DGRAM 1
# endif
# ifndef SOCK_STREAM
# define SOCK_STREAM 2
# endif
#endif
//#define QT_QPROCESS_DEBUG
#ifdef Q_C_CALLBACKS
extern "C" {
#endif // Q_C_CALLBACKS
#define QT_SIGNAL_RETTYPE void
#define QT_SIGNAL_ARGS int
#define QT_SIGNAL_IGNORE SIG_IGN
QT_SIGNAL_RETTYPE qt_C_sigchldHnd(QT_SIGNAL_ARGS);
QT_SIGNAL_RETTYPE qt_C_sigpipeHnd(QT_SIGNAL_ARGS);
#ifdef Q_C_CALLBACKS
}
#endif // Q_C_CALLBACKS
class QProc;
class QProcessManager;
class QProcessPrivate
{
public:
QProcessPrivate();
~QProcessPrivate();
void closeOpenSocketsForChild();
void newProc( pid_t pid, QProcess *process );
QByteArray bufStdout;
QByteArray bufStderr;
QQueue<QByteArray> stdinBuf;
QSocketNotifier *notifierStdin;
QSocketNotifier *notifierStdout;
QSocketNotifier *notifierStderr;
ssize_t stdinBufRead;
QProc *proc;
bool exitValuesCalculated;
bool socketReadCalled;
static QProcessManager *procManager;
};
/***********************************************************************
*
* QProc
*
**********************************************************************/
/*
The class QProcess does not necessarily map exactly to the running
child processes: if the process is finished, the QProcess class may still be
there; furthermore a user can use QProcess to start more than one process.
The helper-class QProc has the semantics that one instance of this class maps
directly to a running child process.
*/
class QProc
{
public:
QProc( pid_t p, QProcess *proc=0 ) : pid(p), process(proc)
{
#if defined(QT_QPROCESS_DEBUG)
odebug << "QProc: Constructor for pid " << pid << " and QProcess " << process << "" << oendl;
#endif
socketStdin = 0;
socketStdout = 0;
socketStderr = 0;
}
~QProc()
{
#if defined(QT_QPROCESS_DEBUG)
odebug << "QProc: Destructor for pid " << pid << " and QProcess " << process << "" << oendl;
#endif
if ( process != 0 ) {
if ( process->d->notifierStdin )
process->d->notifierStdin->setEnabled( FALSE );
if ( process->d->notifierStdout )
process->d->notifierStdout->setEnabled( FALSE );
if ( process->d->notifierStderr )
process->d->notifierStderr->setEnabled( FALSE );
process->d->proc = 0;
}
if( socketStdin != 0 )
::close( socketStdin );
// ### close these sockets even on parent exit or is it better only on
// sigchld (but what do I have to do with them on exit then)?
if( socketStdout != 0 )
::close( socketStdout );
if( socketStderr != 0 )
::close( socketStderr );
}
pid_t pid;
int socketStdin;
int socketStdout;
int socketStderr;
QProcess *process;
};
/***********************************************************************
*
* QProcessManager
*
**********************************************************************/
class QProcessManager : public QObject
{
Q_OBJECT
public:
QProcessManager();
~QProcessManager();
void append( QProc *p );
void remove( QProc *p );
void cleanup();
public slots:
void removeMe();
void sigchldHnd( int );
public:
struct sigaction oldactChld;
struct sigaction oldactPipe;
QList<QProc> *procList;
int sigchldFd[2];
};
QCleanupHandler<QProcessManager> qprocess_cleanup_procmanager;
QProcessManager::QProcessManager()
{
procList = new QList<QProc>;
procList->setAutoDelete( TRUE );
// The SIGCHLD handler writes to a socket to tell the manager that
// something happened. This is done to get the processing in sync with the
// event reporting.
if ( ::socketpair( AF_UNIX, SOCK_STREAM, 0, sigchldFd ) ) {
sigchldFd[0] = 0;
sigchldFd[1] = 0;
} else {
#if defined(QT_QPROCESS_DEBUG)
odebug << "QProcessManager: install socket notifier (" << sigchldFd[1] << ")" << oendl;
#endif
QSocketNotifier *sn = new QSocketNotifier( sigchldFd[1],
QSocketNotifier::Read, this );
connect( sn, SIGNAL(activated(int)),
this, SLOT(sigchldHnd(int)) );
sn->setEnabled( TRUE );
}
// install a SIGCHLD handler and ignore SIGPIPE
struct sigaction act;
#if defined(QT_QPROCESS_DEBUG)
odebug << "QProcessManager: install a SIGCHLD handler" << oendl;
#endif
act.sa_handler = qt_C_sigchldHnd;
sigemptyset( &(act.sa_mask) );
sigaddset( &(act.sa_mask), SIGCHLD );
act.sa_flags = SA_NOCLDSTOP;
#if defined(SA_RESTART)
act.sa_flags |= SA_RESTART;
#endif
if ( sigaction( SIGCHLD, &act, &oldactChld ) != 0 )
owarn << "Error installing SIGCHLD handler" << oendl;
#if defined(QT_QPROCESS_DEBUG)
odebug << "QProcessManager: install a SIGPIPE handler (SIG_IGN)" << oendl;
#endif
/*
Using qt_C_sigpipeHnd rather than SIG_IGN is a workaround
for a strange problem where GNU tar (called by backuprestore)
would hang on filesystem-full. Strangely, the qt_C_sigpipeHnd
is never even called, yet this avoids the hang.
*/
act.sa_handler = qt_C_sigpipeHnd;
sigemptyset( &(act.sa_mask) );
sigaddset( &(act.sa_mask), SIGPIPE );
act.sa_flags = 0;
if ( sigaction( SIGPIPE, &act, &oldactPipe ) != 0 )
owarn << "Error installing SIGPIPE handler" << oendl;
}
QProcessManager::~QProcessManager()
{
delete procList;
if ( sigchldFd[0] != 0 )
::close( sigchldFd[0] );
if ( sigchldFd[1] != 0 )
::close( sigchldFd[1] );
// restore SIGCHLD handler
#if defined(QT_QPROCESS_DEBUG)
odebug << "QProcessManager: restore old sigchild handler" << oendl;
#endif
if ( sigaction( SIGCHLD, &oldactChld, 0 ) != 0 )
owarn << "Error restoring SIGCHLD handler" << oendl;
#if defined(QT_QPROCESS_DEBUG)
odebug << "QProcessManager: restore old sigpipe handler" << oendl;
#endif
if ( sigaction( SIGPIPE, &oldactPipe, 0 ) != 0 )
owarn << "Error restoring SIGPIPE handler" << oendl;
}
void QProcessManager::append( QProc *p )
{
procList->append( p );
#if defined(QT_QPROCESS_DEBUG)
odebug << "QProcessManager: append process (procList.count(): " << procList->count() << ")" << oendl;
#endif
}
void QProcessManager::remove( QProc *p )
{
procList->remove( p );
#if defined(QT_QPROCESS_DEBUG)
odebug << "QProcessManager: remove process (procList.count(): " << procList->count() << ")" << oendl;
#endif
cleanup();
}
void QProcessManager::cleanup()
{
if ( procList->count() == 0 ) {
QTimer::singleShot( 0, this, SLOT(removeMe()) );
}
}
void QProcessManager::removeMe()
{
if ( procList->count() == 0 ) {
qprocess_cleanup_procmanager.remove( &QProcessPrivate::procManager );
QProcessPrivate::procManager = 0;
delete this;
}
}
void QProcessManager::sigchldHnd( int fd )
{
char tmp;
::read( fd, &tmp, sizeof(tmp) );
#if defined(QT_QPROCESS_DEBUG)
odebug << "QProcessManager::sigchldHnd()" << oendl;
#endif
QProc *proc;
QProcess *process;
bool removeProc;
proc = procList->first();
while ( proc != 0 ) {
removeProc = FALSE;
process = proc->process;
QProcess *process_exit_notify=0;
if ( process != 0 ) {
if ( !process->isRunning() ) {
#if defined(QT_QPROCESS_DEBUG)
odebug << "QProcessManager::sigchldHnd() (PID: " << proc->pid << "): process exited (QProcess available)" << oendl;
#endif
// read pending data
int nbytes = 0;
if ( ::ioctl(proc->socketStdout, FIONREAD, (char*)&nbytes)==0 && nbytes>0 ) {
#if defined(QT_QPROCESS_DEBUG)
odebug << "QProcessManager::sigchldHnd() (PID: " << proc->pid << "): reading " << nbytes << " bytes of pending data on stdout" << oendl;
#endif
process->socketRead( proc->socketStdout );
}
nbytes = 0;
if ( ::ioctl(proc->socketStderr, FIONREAD, (char*)&nbytes)==0 && nbytes>0 ) {
#if defined(QT_QPROCESS_DEBUG)
odebug << "QProcessManager::sigchldHnd() (PID: " << proc->pid << "): reading " << nbytes << " bytes of pending data on stderr" << oendl;
#endif
process->socketRead( proc->socketStderr );
}
if ( process->notifyOnExit )
process_exit_notify = process;
removeProc = TRUE;
}
} else {
int status;
if ( ::waitpid( proc->pid, &status, WNOHANG ) == proc->pid ) {
#if defined(QT_QPROCESS_DEBUG)
odebug << "QProcessManager::sigchldHnd() (PID: " << proc->pid << "): process exited (QProcess not available)" << oendl;
#endif
removeProc = TRUE;
}
}
if ( removeProc ) {
QProc *oldproc = proc;
proc = procList->next();
remove( oldproc );
} else {
proc = procList->next();
}
if ( process_exit_notify )
emit process_exit_notify->processExited();
}
}
#include "qprocess_unix.moc"
/***********************************************************************
*
* QProcessPrivate
*
**********************************************************************/
QProcessManager *QProcessPrivate::procManager = 0;
QProcessPrivate::QProcessPrivate()
{
#if defined(QT_QPROCESS_DEBUG)
odebug << "QProcessPrivate: Constructor" << oendl;
#endif
stdinBufRead = 0;
notifierStdin = 0;
notifierStdout = 0;
notifierStderr = 0;
exitValuesCalculated = FALSE;
socketReadCalled = FALSE;
proc = 0;
}
QProcessPrivate::~QProcessPrivate()
{
#if defined(QT_QPROCESS_DEBUG)
odebug << "QProcessPrivate: Destructor" << oendl;
#endif
if ( proc != 0 ) {
if ( proc->socketStdin != 0 ) {
::close( proc->socketStdin );
proc->socketStdin = 0;
}
proc->process = 0;
}
while ( !stdinBuf.isEmpty() ) {
delete stdinBuf.dequeue();
}
delete notifierStdin;
delete notifierStdout;
delete notifierStderr;
}
/*
Closes all open sockets in the child process that are not needed by the child
process. Otherwise one child may have an open socket on standard input, etc.
of another child.
*/
void QProcessPrivate::closeOpenSocketsForChild()
{
if ( procManager != 0 ) {
if ( procManager->sigchldFd[0] != 0 )
::close( procManager->sigchldFd[0] );
if ( procManager->sigchldFd[1] != 0 )
::close( procManager->sigchldFd[1] );
// close also the sockets from other QProcess instances
QProc *proc;
for ( proc=procManager->procList->first(); proc!=0; proc=procManager->procList->next() ) {