path: root/src/corelib/kernel/qcore_unix_p.h
diff options
authorLouai Al-Khanji <>2015-09-21 15:39:16 +0300
committerLouai Al-Khanji <>2015-09-30 05:35:16 +0000
commitac0184d6085d9e4f7f59352e563055311f4d8792 (patch)
treeba5b41d3faf5c01402136a1d567cb2ed7722660a /src/corelib/kernel/qcore_unix_p.h
parent813f468a14fb84af43c1f8fc0a1430277358eba2 (diff)
Add qt_safe_ftok wrapper for ftok
The ftok function unfortunately can return duplicate keys even for different files if the same project id is used. This is discussed e.g. in Stevens' "Advanced Programming in the UNIX Environment". We want the key to be predictable so we cannot just pass a random number as the project id. To reduce the propability of key collisions we hash the file name with the project number as seed for a predictable value. This is the same approach taken e.g. by Apache, but the real fix is to move away from System V IPC completely once this is feasible on our supported platforms. Task-number: QTBUG-48375 Change-Id: If1a57f215f7ddd147aa38919907cfb83db07aea0 Reviewed-by: Thiago Macieira <>
Diffstat (limited to 'src/corelib/kernel/qcore_unix_p.h')
1 files changed, 14 insertions, 0 deletions
diff --git a/src/corelib/kernel/qcore_unix_p.h b/src/corelib/kernel/qcore_unix_p.h
index c744873fce..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"
@@ -322,6 +323,19 @@ union qt_semun {
unsigned short *array; /* array for GETALL, SETALL */
+#ifndef QT_POSIX_IPC
+#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_POSIX_IPC