Click to See Complete Forum and Search --> : OSX / BSD / Linux - Knowing when a user loggs out
DeepT
July 20th, 2006, 11:42 AM
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?
JohnyDog
July 20th, 2006, 12:52 PM
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.
DeepT
July 20th, 2006, 01:17 PM
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.
JohnyDog
July 20th, 2006, 01:34 PM
You need to call 'sigaction' to register handler function which get executed when the specific signal is received:
#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.
DeepT
July 20th, 2006, 03:51 PM
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.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.