Skip navigation.
 
mlObj-C server and signal handling
FROM : Matt Mashyna
DATE : Thu Feb 14 19:48:40 2008

I whipped up a little cocoafied server  without a UI and it works 
great, easy to read and all that good stuff but I'm stuck on one last 
thing. I'm trying to catch a quit signal from Activity Monitor. Force 
Quit definitely works but I'd like to be able to catch whatever signal 
the Activity Monitor sends for the polite quit. Doesn't seem to be 
sending a SIGQUIT, SIGTERM or anything like that. Sometimes I see a 
SIGINT. I can catch signals sent from the terminal fine. Is this the 
wrong way to go about this?

Now that I think about it, it would be even better to be able to have 
a delegate object to catch applicationWillTerminate and shutdown more 
gracefully. I'm not sure how I would do that without loading up a nib.

Here's my main.m which works to catch some signals and exit:

static BOOL    keepRunning = YES;
static void
signal_handler(const int signum)
{
//    if(signum == SIGTERM)
       keepRunning = NO;
   NSLog(@"signal=%d", signum);
}

int main(int argc, char *argv[])
{
       NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   NSApplicationLoad();
   [NSApp setDelegate:del];

   AppController*    myServer = [[AppController alloc] init];

   signal(SIGHUP, signal_handler);
   signal(SIGINT, signal_handler);
   signal(SIGQUIT, signal_handler);
   signal(SIGABRT, signal_handler);
   signal(SIGTERM, signal_handler);

   NSRunLoop *loopy = [NSRunLoop currentRunLoop];
   while (keepRunning && [loopy runMode:NSDefaultRunLoopMode
       beforeDate:[NSDate dateWithTimeIntervalSinceNow:1.0]]);

   [pool release];
   
   return 0;
}

Related mailsAuthorDate
mlObj-C server and signal handling Matt Mashyna Feb 14, 19:48
mlRe: Obj-C server and signal handling Dave Camp Feb 14, 20:32
mlRe: Obj-C server and signal handling Dave Camp Feb 14, 20:41
mlRe: Obj-C server and signal handling Uli Kusterer Feb 14, 20:54