CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2004
    Posts
    1,361

    OSX / BSD / Linux - Knowing when a user loggs out

    I have a generic C++ program that normally constantly runs. However, I want the program to stop when the current user logs out.

    This program is running on OS X, which is very close to BSD and is similar to Linux. Is there some event or signal I can listen for to know when the current user is logging out so my program can exit?

  2. #2
    Join Date
    Dec 2005
    Location
    Prague, Czech Republic
    Posts
    208

    Re: OSX / BSD / Linux - Knowing when a user loggs out

    I don't know how on OSX, but generally on unixes, if the program is run by user on the console (even in background), then it gets SIGHUP signal (or SIGINTR or other depending on the specific system) when the session is terminated.
    If the program is run independently, you can also get information about logged in users using UTMP (see utmp.h manpage) functions. This is however not POSIX standard, just BSD/SysV.

  3. #3
    Join Date
    Sep 2004
    Posts
    1,361

    Re: OSX / BSD / Linux - Knowing when a user loggs out

    Ok. What is the signal API? I am do not know unix / linux very well. I know that a lot of the time the name of the function doesn't sound intuitive. That makes it very hard to search the API to find a function that does X.

  4. #4
    Join Date
    Dec 2005
    Location
    Prague, Czech Republic
    Posts
    208

    Re: OSX / BSD / Linux - Knowing when a user loggs out

    You need to call 'sigaction' to register handler function which get executed when the specific signal is received:

    Code:
    #include <signal.h>
    
    static void signalhandler(int signum)
    {
    // ...
    }
    
    ...
        struct sigaction sa;
    
        sa.sa_handler = signalhandler;
    ...
        sigaction(SIGHUP, &sa, NULL); // returns -1 on error
    The exact API may differ on OSX though.

  5. #5
    Join Date
    Sep 2004
    Posts
    1,361

    Re: OSX / BSD / Linux - Knowing when a user loggs out

    Thanks. I just need to find the right signal It seems. Apparently SIGHUP doesn't get fired when you log out on Mac OSX.

    SIGTERM and SIGINT do not come either. I wonder if any signal is even sent when you log out.
    Last edited by DeepT; July 20th, 2006 at 03:57 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured