1

I have a Mac OS X Carbon Daemon that runs as root. It needs to receive and handle SIGTERM (and other signals) in order for it to terminate properly. The code looks something like this:

static void sig_hup(int sig)
{
   fprintf(stderr, "Caught signal %d.\n" sig); fflush(stderr);
}

int main(int argc, const char * argv[]) {
   ...

   signal(SIGTERM, sig_hup);

   ...

   CFRunLoopRun();       // run the event loop

   ...
   return 0;
}

The problem is that sig_hup() is never gets called.

Perhaps the signal is caught by the CFRunLoopRun()? If so, how does the Daemon get notified about that something (e.g. sudo launchctl unload ...) is trying to terminate it?

  • Have a look at creating a *"dispatch_source"*... https://developer.apple.com/library/mac/documentation/General/Conceptual/ConcurrencyProgrammingGuide/GCDWorkQueues/GCDWorkQueues.html#//apple_ref/doc/uid/TP40008091-CH103-SW1 – Mark Setchell Jun 08 '16 at 14:14
  • 1
    Problem with this is that it requires a dispatch_main() loop that executes until the program terminates. This conflicts with that I need the CFRunLoopRun() for everything else to work. I believe I need to add an 'observer' to the CFRunLoopRun() that will catch the signals. – Ole Bjørn Setnes Jun 10 '16 at 10:07
  • Did you ever find a solution @OleBjørnSetnes? I tried creating an observer for the main `CFRunLoop`, but the runLoopExit signal is called all the time. Also tried to create `dispatch_source_t` on the main queue, catching `SIGTERM` signals, but it's never called. – Noah Nuebling Jul 10 '22 at 18:36

0 Answers0