This is a work-in-progress change inspired by BKO ticket #399473
Goals:
1- make signal handling safe, i.e. make only async-signal safe calls in the actual signal handler, like write() or sem_post().
2- be as cross-platform as possible. ANSI/C signals do exist on MS Windows but I cannot be the judge of whether they'd ever be encountered there.
Bonus goal:
- give users an option to quit a remote session as cleanly as possible if the remote display is inaccessible (whatever the reason).
Goal 1:
Two alternative implementations, both using a static CorePrivate member variable to store the received signal number
a- Use a pair of file descriptors connected via a pipe and a QSocketNotifier. In the signal handler, write() to the pipe; this will cause the CorePrivate::shutdownGracefully slot to be activated which does the actual work (basically unchanged from the current implementation) outside the signal handler context.
b- Use a semaphore and a monitor started through QtConcurrent::run. In the signal handler, sem_post() the semaphore which triggers the monitor which then calls CorePrivate::shutdownGracefully().
Goal 2:
Creating a pipe with pipe() is Unix-specific; MS Windows has a dedicated call the result of which can (probably) not be fed to QSocketNotifier.
Unnamed semaphores are platform-specific too (note the use of Mach semaphores on Mac which doesn't have unnamed POSIX semaphores) but it should be straightforward to adapt the implementation.
The actual graceful shutdown routine has been modified slightly: it will call Core::shutdown() when a 2nd signal is received, before raising that signal again. I propose to make this the only action when a SIGHUP is received, in order to speed up the exit procedure and reduce the chances of getting stuck in the normal exit procedure. This seems more in line with the nature of the signal.
BUG: 399473