Obj-C server and signal handling

  • 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;
    }
  • On Feb 14, 2008, at 10:48 AM, Matt Mashyna wrote:

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

    Creating an NSDate in that tight loop will leak memory on each
    iteration.

    I've done something like this in the past (perhaps there is a better
    way):

    while (running && !gSigTerm)
    {
      NSAutoreleasePool *innerPool = [[NSAutoreleasePool alloc] init];
      running = [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
    beforeDate:[NSDate dateWithTimeIntervalSinceNow:1.0]];
      [innerPool release];
    }

    Dave
  • On Feb 14, 2008, at 11:32 AM, Dave Camp wrote:

    > On Feb 14, 2008, at 10:48 AM, Matt Mashyna wrote:
    >
    >> NSRunLoop *loopy = [NSRunLoop currentRunLoop];
    >> while (keepRunning && [loopy runMode:NSDefaultRunLoopMode
    >> beforeDate:[NSDate dateWithTimeIntervalSinceNow:1.0]]);
    >>
    >
    >
    > Creating an NSDate in that tight loop will leak memory on each
    > iteration.

    Clarification: it's not really leaked as it goes into the main
    autorelease pool, but that pool isn't drained until you exit..

    Dave
  • On 14.02.2008, at 19:48, Matt Mashyna wrote:
    > 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?

      For a raw command-line tool, handling the right signal is what
    you're expected to do (i.e. if you started with a "Foundation Tool"
    target or similar).

      If you actually created a bundled Cocoa application, but just
    removed all the GUI stuff, you really should be running the NSApp you
    have and have it take care of quitting. Or you could use Carbon/
    CoreFoundation methods to register for the quit Apple Event, which is
    what the OS seems to send every app in that case.

    Cheers,
    -- Uli Kusterer
    "The Witnesses of TeachText are everywhere..."
    http://www.zathras.de
previous month february 2008 next month
MTWTFSS
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29    
Go to today
MindNode
MindNode offered a free license !