The file was added to glibc 2.7 along with the functions we need (Added
2007-10-05). But they forgot to install the file until a month and a
half later (2007-11-17), which means it missed the 2.7 release
(2007-10-19).
Note that EFD_CLOEXEC wasn't added until glibc 2.9, so effectively glibc
2.9 is required.
Change-Id: I3868166e5efc45538544fffd14d773ba576fb793
Reviewed-by: Kai Koehne <kai.koehne@qt.io>
Reviewed-by: David Faure <david.faure@kdab.com>
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@qt.io>
On some stupid systems, execve() may clear the handler but not clear
the SA_SIGINFO flag.
This change now requires that sa_handler and sa_sigaction be in a union
together. We can't operate otherwise.
Task-number: QTBUG-59246
Change-Id: I33850dcdb2ce4a47878efffd14a84b48a8f6b1e8
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
This existed in QProcess before forkfd, but was lost in the port to it
(commit 1814142b7a). The original QProcess
fix was done in 97279d0582.
Task-number: QTBUG-57584
Change-Id: Ibc5c715fda334a75bd2efffd14a425871f3162b5
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Bring the copyright lines in the license and the one's
in the metadata into line by using the ones from forkfd.c.
Also bring back description of forkfd from 5.6 documentation.
Change-Id: I423ee8d5d1e1c866a34c346f78520d33ea099b5e
Reviewed-by: Topi Reiniö <topi.reinio@qt.io>
The format is documented in
http://wiki.qt.io/Qt_attribution.json
Also add a LICENSE file in case there is none yet
(usually copied from the source headers).
Task-number: QTBUG-55139
Change-Id: Ib54c73d0bb9946cfd8579e86c6858035184ca516
Reviewed-by: Topi Reiniö <topi.reinio@theqtcompany.com>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Add the necessary defines for HAVE_PIPE2 for NetBSD and OpenBSD depending
on OS version when pipe2(2) was added. This also fixes the compile error
on NetBSD if -Werror=unused-function for ignore_sigpipe() as the
HAVE_PIPE2 tree is prior to O_NOSIGPIPE in create_pipe().
Change-Id: Ic8f875e34ef826a7bf046c77588afecaa097deca
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
We can't depend on the application/library ignoring the signal for us,
so we do it. O_NOSIGPIPE exists on the BSDs and I'll add it to Linux. If
it isn't supported, then we need to ignore SIGPIPE globally.
Change-Id: I25d85d86649448d5b2b3fffd1450f6afeaea8b18
Reviewed-by: Ralf Nolden <nolden@kde.org>
Reviewed-by: Rafael Roquetto <rafael.roquetto@kdab.com>
Not that we require it, but since The Qt Company did it for all files
they have copyright, even if they haven't touched the file in years
(especially not in 2016), I'm doing the same.
Change-Id: I7a9e11d7b64a4cc78e24ffff142b4c9d53039846
Reviewed-by: Lars Knoll <lars.knoll@theqtcompany.com>
The system call is not present on earlier releases, and since the 9.x
series will be supported until the end of 2016, add a check for the
__FreeBSD_version macro and only enable pipe2 support if the value is
high enough.
Change-Id: I5633531cec7e95d42ff5f4b14afe772ae8d7d66d
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
pipe2's availability on BSD operating systems depends on the
__BSD_VISIBLE macro on FreeBSD and OpenBSD and _NETBSD_SOURCE on
NetBSD (DragonFly BSD appears to define it unconditionally).
Those two macros are generally set by default, except when
_POSIX_C_SOURCE is set. Since we consciously set _POSIX_C_SOURCE but
need pipe2, explicitly define the visibility macros.
This fixes the -no-pch build on FreeBSD at least.
Change-Id: Icc77f6b5d1f9a5bf7bd8048cabbb01f8f89397cc
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
That is the case on Hurd, where the code currently breaks because Hurd
does not have WEXITED or WNOWAIT defined.
Change-Id: I4b13633612b1168d36c949d9e8b35bc05bca7d5c
Reviewed-by: Lisandro Damián Nicanor Pérez Meyer <perezmeyer@gmail.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Major << 16 is 0x90000. Reported in QTBUG-45139
Change-Id: I42e7ef1a481840699a8dffff14057022bc4df8e9
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@theqtcompany.com>
Reviewed-by: Rafael Roquetto <rafael.roquetto@kdab.com>
pdfork(2) has semantics very close to what we want in forkfd, but not
quite. Differences:
- we still get SIGCHLD and need to do a wait4
- no support for atomic FD_CLOEXEC and O_NONBLOCK
On the SIGCHLD case: this commit is an improvement over the generic Unix
case, since we no longer need to install a SIGCHLD handler and do not
need to keep the arrays for matching PIDs and file descriptors. That
matching is done entirely inside the kernel.
However, since SIGCHLD is still sent to the process, an uncooperative
SIGCHLD handler can still "steal" our response. At least Glib is
documented not to reap children it wasn't explicitly asked to watch for
(source code matches), but other libraries are known to do waitpid(-1)
(e.g., EFL's Ecore). At least now the behavior is consistent: we will
never install a handler, so the behavior won't depend on the order in
which the handlers are installed.
Change-Id: Iee8cbc07c4434ce9b560ffff13cb4c63306e43ef
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@theqtcompany.com>
Reviewed-by: Rafael Roquetto <rafael.roquetto@kdab.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This is just to make the code cleaner. The static variable will control
whether we've detected that the system call exists -- if it does, we are
expected to always use it.
Change-Id: Iee8cbc07c4434ce9b560ffff13cb4adc049413c1
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@theqtcompany.com>
Reviewed-by: Rafael Roquetto <rafael.roquetto@kdab.com>
That version of uClibc has neither pipe2 nor eventfd.
There were two problems with our detection. First, it checked for glibc
incorrectly, so the comparison was always true as
__GLIBC__ << 16 = 0x20000
Second, we needed to check for uClibc's version.
Task-number: QTBUG-47337
Change-Id: Ib306f8f647014b399b87ffff13f3023b7f8d6d4a
Reviewed-by: Rafael Roquetto <rafael.roquetto@kdab.com>
wait4 gives us the struct rusage contents for us, which may contain
interesting data for other users of forkfd (not Qt, though).
Change-Id: Iee8cbc07c4434ce9b560ffff13cb4aa28e18e790
Reviewed-by: Rafael Roquetto <rafael.roquetto@kdab.com>
Right now the two functions being added are just wrappers, but this will
allow us in the future to support FreeBSD and Linux's system calls that
do the equivalent of forkfd, but have slightly different semantics in
the actual getting of the information from the file descriptor.
See-Also: https://lkml.org/lkml/2015/3/12/1044
See-Also: http://www.freebsd.org/cgi/man.cgi?query=pdfork
Change-Id: Ia0aac2f09e9245339951ffff13c94acb5f4ff204
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@theqtcompany.com>
Reviewed-by: Rafael Roquetto <rafael.roquetto@kdab.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
On OS X 10.7 and earlier, waitid() never sets si_pid, even when using
P_PID. So on OS X, check if waitid() works, and if not, use the same
codepath as if HAVE_WAITID were not defined.
Change-Id: I64331a090f9358bb01f435954d3dfd3ab430a96c
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
glibc 2.9 is required for pipe2 and version 2.7 for eventfd.
Bionic added them to Android version 2.3.1, but I can't find a version
macro.
uclibc masquerades as glibc version 2.2, so this function won't be
thread-safe with uclibc.
Change-Id: Iee8cbc07c4434ce9b560ffff13cd3c3b63dd7e83
Reviewed-by: Rafael Roquetto <rafael.roquetto@kdab.com>
Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
The difference between BSD and MIT is the need to reproduce the
copyright in the documentation and the non-endorsement by a particular
company (the name of which was stale in the forkfd code).
Change-Id: Iee8cbc07c4434ce9b560ffff13cd0174934935e9
Reviewed-by: Rafael Roquetto <rafael.roquetto@kdab.com>
The Darwin kernel that came with Mac OS X 10.7 has a broken
implementation of waitid when passed a P_ALL first argument. It does
tell us that there is a process that can be wait()ed, but does not fill
in the siginfo_t structure.
See commit 9931fa9df4 for more
information.
Change-Id: Iee8cbc07c4434ce9b560ffff13cafa4c88cdabd6
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@theqtcompany.com>
It no longer compiled after 614f37c8b5 due to
the following:
- forkfd_qt.cpp set _XOPEN_SOURCE to 500
- It then includes qatomic.h which include sys/cdefs.h (the FreeBSD header
that parses and sets _POSIX_C_SOURCE, _XOPEN_SOURCE and other macros)
- sys/cdefs.h redefines _POSIX_C_SOURCE to 199506 due to _XOPEN_SOURCE's
value
- Several libc symbols expected to exist by libc++ are hidden due to
_POSIX_C_SOURCE's value and the build fails
Setting _XOPEN_SOURCE to 700 ensures that _POSIX_C_SOURCE is set to
200809 which is required for libc++ to work correctly
Task-number: QTBUG-45006
Change-Id: Iac93220d19ca5ab9ba8ac61a79748252283c3c47
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
We only ever use one, never both.
Change-Id: Iee8cbc07c4434ce9b560ffff13caf94c05dba338
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@theqtcompany.com>
In certain environments, using fork() is not recommended due to the need
for an MMU. This commit adds support for those environments, by using
posix_spawn. Limitations of this environment are:
- we cannot reliably detect failure to exec (e.g. non-existing executable)
- we cannot do setsid(); we do setpgrp(0, 0) instead
- we cannot thread-safely chdir() to the requested dir
Because of the former limitation, the QProcess unit tests that rely on
failure-to-start error conditions are either skipped or marked as
expected failures. There's a non-reliable solution that is implemented
in a another commit.
This change also makes it easier to transition the QNX builds to using
fork(), which is supported from QNX Neutrino 6.6 and onwards.
Change-Id: I5cb46abf2ef8783941525d35cc991f00d2bf2d58
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
dtruss logs show that the signal handler does enter and is active, since
it does the first waitid() call, but then returns immediately:
waitid(0x0, 0x0, 0x7FFF62D7C468) = 0 0
sigreturn(0x7FFF62D7C9A0, 0x1E, 0x0) = 0 Err#-2
Since there was no error return, we conclude that si_pid was zero on
return. Source code for OS X 10.7 confirms that si_pid is set to zero
unconditionally, which is rather stupid:
http://fxr.watson.org/fxr/source/bsd/kern/kern_exit.c?v=xnu-1699.24.8#L1330
This is fixed for OS X 10.8:
http://fxr.watson.org/fxr/source/bsd/kern/kern_exit.c?v=xnu-2050.18.24#L1399
Without that information, we have to scan each child anyway, so
just disable the waitid() solution on OS X. This is a "hammer" solution
which will get forkfd working. We can later try and detect at runtime
whether waitid() is working.
Change-Id: Ic5d393bfd36e48a193fcffff13bb584927cdeafe
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@theqtcompany.com>
POSIX.1 does not guarantee the presence of the si_utime and si_stime
members. So instead of trying to set those members to zero, ask the
compiler to initialize everything for us.
This was found on OS X when HAVE_WAITTID was removed.
forkfd.c:192:11: error: no member named 'si_utime' in '__siginfo'
forkfd.c:193:11: error: no member named 'si_stime' in '__siginfo'
Change-Id: Ic5d393bfd36e48a193fcffff13b90aa6ccf592ae
Reviewed-by: Rafael Roquetto <rafael.roquetto@kdab.com>
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@theqtcompany.com>
The previous implementation required one syscall per child we're waiting
on to see which one exited. That means the algorithm was O(n).
This implementation uses WNOWAIT to find out which child exited and then
goes straight to that one. So it's O(1) on the number of children, but
runs 2 * number_of_children_that_exited + 1 syscalls, assuming there are
no race conditions with other threads. If there are or if a child not
started by forkfd exits, we'll still iterate over each child we're
managing to see which one exited.
It modifies the existing code so that it will do a waitid() with WNOWAIT
to check on the status of the child: if the child has exited, we'll try
to lock the entry so only one thread will do the final wait(). In the
case we read the PID, then the child exited, was reaped by another
thread, the PID got recycled and that child exited again, we'll fail to
lock the ProcessInfo entry so no harm comes. If by an absurd coincidence
this other child was started by forkfd() and its ProcessInfo is exactly
the one we are looking at, then we'll succeed in locking but that's a
benign race: we'll do what the other thread was trying to do and the
other thread will give up.
Future improvements to the algorithm are discussed in the Gerrit change.
Change-Id: Ie74836dbc388cd9b3fa375a41a8d944602a32df1
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
O_CLOEXEC was introduced with the 2008 revision of POSIX.1 and it's the
only way of doing child processes safely with fork(2) in multithreaded
applications.
But we need to support pre-2008 systems, so we can't use that constant.
So let's just choose two arbitrary values for both of our constants --
we need to change both because we need to be sure that FFD_CLOEXEC won't
be the same as FFD_NONBLOCK.
Linux will probably implement them to the O_ constants, like epoll,
signalfd and inotify have done.
Change-Id: I20a5aa6e6264e7a219e19759eeb8747e01df05ff
Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
Reviewed-by: Rafael Roquetto <rafael.roquetto@kdab.com>
forkfd is a tool that I designed to facilitate spawning
sub-processes. It's implemented in C, not C++, so that it could be
used by other libraries as well.
To work in all platforms Qt supports and with all compilers Qt is
known to work with, we'll need to replace the generic GCC atomics that
are provided here.
Change-Id: I0a6f86cc220a7c52c8d4284bb7140c56d5cf836a
Reviewed-by: Rafael Roquetto <rafael.roquetto@kdab.com>
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@theqtcompany.com>