The TERM signal handling in ksmserver invokes functions which are not async-signal safe, like Qt functions and C++ destructors. Moreover, the signal handling can occur in other than the main thread, which leads to Qt complaining about functions being invoked from the wrong thread. Such a crash can be seen in a report of https://bugs.kde.org/show_bug.cgi?id=384316.
To fix both of these issues, this change makes the signal handling use the self-pipe trick, which signals the need for termination to the main thread by writing to a special-purpose file descriptor. The main loop then takes care of the termination. This is mostly inspired by http://doc.qt.io/qt-5/unix-signals.html.
Note that QApplication::quit already does what we need when destroying the server, particularly deleting the_server/calling cleanUp().
BTW I noticed the code (here and in XIO error handler) makes quite sure the_server is zeroed. However, other code in ksmserver is not so keen on checking the_server is !0 when using it.