Qt Reference Documentation

# Calling Qt Functions From Unix Signal Handlers

You can't call Qt functions from Unix signal handlers. The standard POSIX rule applies: You can only call async-signal-safe functions from signal handlers. See Signal Actions for the complete list of functions you can call from Unix signal handlers.

But don't despair, there is a way to use Unix signal handlers with Qt. The strategy is to have your Unix signal handler do something that will eventually cause a Qt signal to be emitted, and then you simply return from your Unix signal handler. Back in your Qt program, that Qt signal gets emitted and then received by your Qt slot function, where you can safely do whatever Qt stuff you weren't allowed to do in the Unix signal handler.

One simple way to make this happen is to declare a socket pair in your class for each Unix signal you want to handle. The socket pairs are declared as static data members. You also create a QSocketNotifier to monitor the read end of each socket pair, declare your Unix signal handlers to be static class methods, and declare a slot function corresponding to each of your Unix signal handlers. In this example, we intend to handle both the SIGHUP and SIGTERM signals. Note: You should read the socketpair(2) and the sigaction(2) man pages before plowing through the following code snippets.

     class MyDaemon : public QObject
     {
         Q_OBJECT

       public:
         MyDaemon(QObject *parent = 0);
         ~MyDaemon();

         // Unix signal handlers.
         static void hupSignalHandler(int unused);
         static void termSignalHandler(int unused);

       public slots:
         // Qt signal handlers.
         void handleSigHup();
         void handleSigTerm();

       private:
         static int sighupFd[2];
         static int sigtermFd[2];

         QSocketNotifier *snHup;
         QSocketNotifier *snTerm;
     };

In the MyDaemon constructor, use the socketpair(2) function to initialize each file descriptor pair, and then create the QSocketNotifier to monitor the read end of each pair. The activated() signal of each QSocketNotifier is connected to the appropriate slot function, which effectively converts the Unix signal to the QSocketNotifier::activated() signal.

     MyDaemon::MyDaemon(QObject *parent)
             : QObject(parent)
     {
         if (::socketpair(AF_UNIX, SOCK_STREAM, 0, sighupFd))
            qFatal("Couldn't create HUP socketpair");

         if (::socketpair(AF_UNIX, SOCK_STREAM, 0, sigtermFd))
            qFatal("Couldn't create TERM socketpair");
         snHup = new QSocketNotifier(sighupFd[1], QSocketNotifier::Read, this);
         connect(snHup, SIGNAL(activated(int)), this, SLOT(handleSigHup()));
         snTerm = new QSocketNotifier(sigtermFd[1], QSocketNotifier::Read, this);
         connect(snTerm, SIGNAL(activated(int)), this, SLOT(handleSigTerm()));

         ...
     }

Somewhere else in your startup code, you install your Unix signal handlers with sigaction(2).

     static int setup_unix_signal_handlers()
     {
         struct sigaction hup, term;

         hup.sa_handler = MyDaemon::hupSignalHandler;
         sigemptyset(&hup.sa_mask);
         hup.sa_flags = 0;
         hup.sa_flags |= SA_RESTART;

         if (sigaction(SIGHUP, &hup, 0) > 0)
            return 1;

         term.sa_handler = MyDaemon::termSignalHandler;
         sigemptyset(&term.sa_mask);
         term.sa_flags |= SA_RESTART;

         if (sigaction(SIGTERM, &term, 0) > 0)
            return 2;

         return 0;
     }

In your Unix signal handlers, you write a byte to the write end of a socket pair and return. This will cause the corresponding QSocketNotifier to emit its activated() signal, which will in turn cause the appropriate Qt slot function to run.

     void MyDaemon::hupSignalHandler(int)
     {
         char a = 1;
         ::write(sighupFd[0], &a, sizeof(a));
     }

     void MyDaemon::termSignalHandler(int)
     {
         char a = 1;
         ::write(sigtermFd[0], &a, sizeof(a));
     }

In the slot functions connected to the QSocketNotifier::activated() signals, you read the byte. Now you are safely back in Qt with your signal, and you can do all the Qt stuff you weren'tr allowed to do in the Unix signal handler.

     void MyDaemon::handleSigTerm()
     {
         snTerm->setEnabled(false);
         char tmp;
         ::read(sigtermFd[1], &tmp, sizeof(tmp));

         // do Qt stuff

         snTerm->setEnabled(true);
     }

     void MyDaemon::handleSigHup()
     {
         snHup->setEnabled(false);
         char tmp;
         ::read(sighupFd[1], &tmp, sizeof(tmp));

         // do Qt stuff

         snHup->setEnabled(true);
     }