summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel/qcore_unix_p.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/kernel/qcore_unix_p.h')
-rw-r--r--src/corelib/kernel/qcore_unix_p.h78
1 files changed, 31 insertions, 47 deletions
diff --git a/src/corelib/kernel/qcore_unix_p.h b/src/corelib/kernel/qcore_unix_p.h
index fa083a3f44..f80dcb5a50 100644
--- a/src/corelib/kernel/qcore_unix_p.h
+++ b/src/corelib/kernel/qcore_unix_p.h
@@ -47,6 +47,7 @@
#include "qplatformdefs.h"
#include "qatomic.h"
+#include "qhash.h"
#ifndef Q_OS_UNIX
# error "qcore_unix_p.h included on a non-Unix system"
@@ -67,24 +68,6 @@
struct sockaddr;
-#if defined(Q_OS_LINUX) && defined(O_CLOEXEC)
-# define QT_UNIX_SUPPORTS_THREADSAFE_CLOEXEC 1
-QT_BEGIN_NAMESPACE
-namespace QtLibcSupplement {
- inline int accept4(int, sockaddr *, QT_SOCKLEN_T *, int)
- { errno = ENOSYS; return -1; }
- inline int dup3(int, int, int)
- { errno = ENOSYS; return -1; }
- inline int pipe2(int [], int )
- { errno = ENOSYS; return -1; }
-}
-QT_END_NAMESPACE
-using namespace QT_PREPEND_NAMESPACE(QtLibcSupplement);
-
-#else
-# define QT_UNIX_SUPPORTS_THREADSAFE_CLOEXEC 0
-#endif
-
#define EINTR_LOOP(var, cmd) \
do { \
var = cmd; \
@@ -179,22 +162,14 @@ static inline int qt_safe_open(const char *pathname, int flags, mode_t mode = 07
// call qt_safe_pipe
static inline int qt_safe_pipe(int pipefd[2], int flags = 0)
{
-#ifdef O_CLOEXEC
- Q_ASSERT((flags & ~(O_CLOEXEC | O_NONBLOCK)) == 0);
-#else
Q_ASSERT((flags & ~O_NONBLOCK) == 0);
-#endif
- int ret;
-#if QT_UNIX_SUPPORTS_THREADSAFE_CLOEXEC && defined(O_CLOEXEC)
+#ifdef QT_THREADSAFE_CLOEXEC
// use pipe2
flags |= O_CLOEXEC;
- ret = ::pipe2(pipefd, flags); // pipe2 is Linux-specific and is documented not to return EINTR
- if (ret == 0 || errno != ENOSYS)
- return ret;
-#endif
-
- ret = ::pipe(pipefd);
+ return ::pipe2(pipefd, flags); // pipe2 is documented not to return EINTR
+#else
+ int ret = ::pipe(pipefd);
if (ret == -1)
return -1;
@@ -208,6 +183,7 @@ static inline int qt_safe_pipe(int pipefd[2], int flags = 0)
}
return 0;
+#endif
}
#endif // Q_OS_VXWORKS
@@ -217,22 +193,19 @@ static inline int qt_safe_dup(int oldfd, int atleast = 0, int flags = FD_CLOEXEC
{
Q_ASSERT(flags == FD_CLOEXEC || flags == 0);
- int ret;
#ifdef F_DUPFD_CLOEXEC
- // use this fcntl
- if (flags & FD_CLOEXEC) {
- ret = ::fcntl(oldfd, F_DUPFD_CLOEXEC, atleast);
- if (ret != -1 || errno != EINVAL)
- return ret;
- }
-#endif
-
+ int cmd = F_DUPFD;
+ if (flags & FD_CLOEXEC)
+ cmd = F_DUPFD_CLOEXEC;
+ return ::fcntl(oldfd, cmd, atleast);
+#else
// use F_DUPFD
- ret = ::fcntl(oldfd, F_DUPFD, atleast);
+ int ret = ::fcntl(oldfd, F_DUPFD, atleast);
if (flags && ret != -1)
::fcntl(ret, F_SETFD, flags);
return ret;
+#endif
}
// don't call dup2
@@ -242,14 +215,11 @@ static inline int qt_safe_dup2(int oldfd, int newfd, int flags = FD_CLOEXEC)
Q_ASSERT(flags == FD_CLOEXEC || flags == 0);
int ret;
-#if QT_UNIX_SUPPORTS_THREADSAFE_CLOEXEC && defined(O_CLOEXEC)
+#ifdef QT_THREADSAFE_CLOEXEC
// use dup3
- if (flags & FD_CLOEXEC) {
- EINTR_LOOP(ret, ::dup3(oldfd, newfd, O_CLOEXEC));
- if (ret == 0 || errno != ENOSYS)
- return ret;
- }
-#endif
+ EINTR_LOOP(ret, ::dup3(oldfd, newfd, flags ? O_CLOEXEC : 0));
+ return ret;
+#else
EINTR_LOOP(ret, ::dup2(oldfd, newfd));
if (ret == -1)
return -1;
@@ -257,6 +227,7 @@ static inline int qt_safe_dup2(int oldfd, int newfd, int flags = FD_CLOEXEC)
if (flags)
::fcntl(newfd, F_SETFD, flags);
return 0;
+#endif
}
static inline qint64 qt_safe_read(int fd, void *data, qint64 maxlen)
@@ -352,6 +323,19 @@ union qt_semun {
unsigned short *array; /* array for GETALL, SETALL */
};
+#ifndef QT_POSIX_IPC
+#ifndef QT_NO_SHAREDMEMORY
+#ifndef Q_OS_ANDROID
+static inline key_t qt_safe_ftok(const QByteArray &filename, int proj_id)
+{
+ // Unfortunately ftok can return colliding keys even for different files.
+ // Try to add some more entropy via qHash.
+ return ::ftok(filename.constData(), qHash(filename, proj_id));
+}
+#endif // !Q_OS_ANDROID
+#endif // !QT_NO_SHAREDMEMORY
+#endif // !QT_POSIX_IPC
+
QT_END_NAMESPACE
#endif