tags:

views:

251

answers:

2

I'm writing a piece of software that runs as a system service in Mac OSX and cannot seem to find out how to get the 'active' user.

There appears to be APIs to iterate through the list of logged-in users and be able to tell from a value which one has the machine console... however I cannot find any documentation on these APIs.

Looking at /dev, it appears that the /dev/console device is owned by the currently logged-in user... however I have been unable to find any documentation to confirm this.

Any ideas?

+1  A: 

You can use getuid and getgid to get the user's user id and group id. You can use getlogin or getlogin_r to retrieve a string representation of the current user's name. You can get additional user info with getpwuid.

As a side note, "man" is your friend. Whenever you're in doubt, consult the UNIX Manual Pages.

Michael Aaron Safyan
Sorry, I didn't fully understand what you were asking. I don't think you can get the "active" user, because there is always the possibility that more than one user will be logged in at a time. If you want to do something for a specific user, you might want to create a per-user launch agent.
Michael Aaron Safyan
There can only be one person physically using a Mac at once. Fast-user switching doesn't allow multiple people to have control of a terminal at a given time. Nor does SSH, it gets you console access, but no GUI access.getuid and the like will return the current user of the running process (ex: root)... getlogin appears to have the same behavior.
harningt
A: 

Answer courtesy of Apple's Technical Q&A

  • Require SystemConfiguration.framework

    #include <SystemConfiguration/SystemConfiguration.h>
    /* .... */
    CFStringRef result = SCDynamicStoreCopyConsoleUser(NULL, NULL, NULL);
    /* .... */
    

OR use the utmpx APIs to get a list of all logged-in accounts from which you should be able to get the necessary information...

harningt