From 97279d05822a70da1fb3dab083d823a5f5a008fe Mon Sep 17 00:00:00 2001 From: "H. Rittich" Date: Fri, 13 Sep 2013 17:28:23 +0200 Subject: Fix sigchld-Handler Changed the sigchld-Handler such that the SA_SIGINFO flag is handeled correctly. Furthermore the signal mask is preserved such that the original signal handler is not interrupted when not allowed. Task-number: QTBUG-32979 Change-Id: Iec7663e7289ea5d95155f52cf8788ebf646cfabd Reviewed-by: Thiago Macieira --- src/corelib/io/qprocess_unix.cpp | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) (limited to 'src/corelib/io') diff --git a/src/corelib/io/qprocess_unix.cpp b/src/corelib/io/qprocess_unix.cpp index e9957d2384..7e42f4b722 100644 --- a/src/corelib/io/qprocess_unix.cpp +++ b/src/corelib/io/qprocess_unix.cpp @@ -121,17 +121,33 @@ static const int errorBufferMax = 512; static int qt_qprocess_deadChild_pipe[2]; static struct sigaction qt_sa_old_sigchld_handler; -static void qt_sa_sigchld_handler(int signum) +static void qt_sa_sigchld_sigaction(int signum, siginfo_t *info, void *context) { + // *Never* use the info or contect variables in this function + // (except for passing them to the next signal in the chain). + // We cannot be sure if another library or if the application + // installed a signal handler for SIGCHLD without SA_SIGINFO + // and fails to pass the arguments to us. If they do that, + // these arguments contain garbage and we'd most likely crash. + qt_safe_write(qt_qprocess_deadChild_pipe[1], "", 1); #if defined (QPROCESS_DEBUG) fprintf(stderr, "*** SIGCHLD\n"); #endif - // load it as volatile - void (*oldAction)(int) = ((volatile struct sigaction *)&qt_sa_old_sigchld_handler)->sa_handler; - if (oldAction && oldAction != SIG_IGN) - oldAction(signum); + // load as volatile + volatile struct sigaction *vsa = &qt_sa_old_sigchld_handler; + + if (qt_sa_old_sigchld_handler.sa_flags & SA_SIGINFO) { + void (*oldAction)(int, siginfo_t *, void *) = vsa->sa_sigaction; + + oldAction(signum, info, context); + } else { + void (*oldAction)(int) = vsa->sa_handler; + + if (oldAction && oldAction != SIG_IGN) + oldAction(signum); + } } static inline void add_fd(int &nfds, int fd, fd_set *fdset) @@ -197,10 +213,16 @@ QProcessManager::QProcessManager() // set up the SIGCHLD handler, which writes a single byte to the dead // child pipe every time a child dies. + struct sigaction action; - memset(&action, 0, sizeof(action)); - action.sa_handler = qt_sa_sigchld_handler; - action.sa_flags = SA_NOCLDSTOP; + // use the old handler as template, i.e., preserve the signal mask + // otherwise the original signal handler might be interrupted although it + // was marked to never be interrupted + ::sigaction(SIGCHLD, NULL, &action); + action.sa_sigaction = qt_sa_sigchld_sigaction; + // set the SA_SIGINFO flag such that we can use the three argument handler + // function + action.sa_flags = SA_NOCLDSTOP | SA_SIGINFO; ::sigaction(SIGCHLD, &action, &qt_sa_old_sigchld_handler); processManagerInstance = this; @@ -225,7 +247,7 @@ QProcessManager::~QProcessManager() struct sigaction currentAction; ::sigaction(SIGCHLD, 0, ¤tAction); - if (currentAction.sa_handler == qt_sa_sigchld_handler) { + if (currentAction.sa_sigaction == qt_sa_sigchld_sigaction) { ::sigaction(SIGCHLD, &qt_sa_old_sigchld_handler, 0); } -- cgit v1.2.3 From 31db8728cf038f0a38c42d64c375403fde8598c9 Mon Sep 17 00:00:00 2001 From: Riccardo Ferrazzo Date: Thu, 29 Aug 2013 12:36:34 +0200 Subject: Fix QFileSystemWatcher inotify On linux using QFileSystemWatcher with inotify backend when a watched file is moved and added again to the watched files its path is not replaced with the new one. This behavior prevents the emission of the fileChanged signal with the wrong file path. Task-number: QTBUG-33211 Change-Id: Ib45d8efdf5afbf8b8f6b4b26e43f3d6ee740aca6 Reviewed-by: Thiago Macieira --- src/corelib/io/qfilesystemwatcher_inotify.cpp | 21 +++++++++++++++++---- src/corelib/io/qfilesystemwatcher_inotify_p.h | 5 ++++- 2 files changed, 21 insertions(+), 5 deletions(-) (limited to 'src/corelib/io') diff --git a/src/corelib/io/qfilesystemwatcher_inotify.cpp b/src/corelib/io/qfilesystemwatcher_inotify.cpp index 024af79c33..c731f3d417 100644 --- a/src/corelib/io/qfilesystemwatcher_inotify.cpp +++ b/src/corelib/io/qfilesystemwatcher_inotify.cpp @@ -365,11 +365,11 @@ void QInotifyFileSystemWatcherEngine::readFromInotify() // qDebug() << "inotify event, wd" << event.wd << "mask" << hex << event.mask; int id = event.wd; - QString path = idToPath.value(id); + QString path = getPathFromID(id); if (path.isEmpty()) { // perhaps a directory? id = -id; - path = idToPath.value(id); + path = getPathFromID(id); if (path.isEmpty()) continue; } @@ -378,8 +378,9 @@ void QInotifyFileSystemWatcherEngine::readFromInotify() if ((event.mask & (IN_DELETE_SELF | IN_MOVE_SELF | IN_UNMOUNT)) != 0) { pathToID.remove(path); - idToPath.remove(id); - inotify_rm_watch(inotifyFd, event.wd); + idToPath.remove(id, getPathFromID(id)); + if (!idToPath.contains(id)) + inotify_rm_watch(inotifyFd, event.wd); if (id < 0) emit directoryChanged(path, true); @@ -394,6 +395,18 @@ void QInotifyFileSystemWatcherEngine::readFromInotify() } } +QString QInotifyFileSystemWatcherEngine::getPathFromID(int id) const +{ + QHash::const_iterator i = idToPath.find(id); + while (i != idToPath.constEnd() && i.key() == id) { + if ((i + 1) == idToPath.constEnd() || (i + 1).key() != id) { + return i.value(); + } + ++i; + } + return QString(); +} + QT_END_NAMESPACE #endif // QT_NO_FILESYSTEMWATCHER diff --git a/src/corelib/io/qfilesystemwatcher_inotify_p.h b/src/corelib/io/qfilesystemwatcher_inotify_p.h index 959d9edc12..d02f04eed6 100644 --- a/src/corelib/io/qfilesystemwatcher_inotify_p.h +++ b/src/corelib/io/qfilesystemwatcher_inotify_p.h @@ -78,11 +78,14 @@ public: private Q_SLOTS: void readFromInotify(); +private: + QString getPathFromID(int id) const; + private: QInotifyFileSystemWatcherEngine(int fd, QObject *parent); int inotifyFd; QHash pathToID; - QHash idToPath; + QMultiHash idToPath; QSocketNotifier notifier; }; -- cgit v1.2.3