Search code examples
cmacososx-yosemiteosx-yosemite-beta

OSX Yosemite getutxent() returning the same user twice


I am trying to get a list of current logged in users using the getutxent() function defined in apple's <utmpx.h>. The test code I am using is this:

#include <stdio.h>
#include <utmpx.h>

int main(void) {
    setutxent();
    while (1) {
        struct utmpx *user_info = getutxent();
        if (user_info == NULL) break;
        printf("%s\n", user_info->ut_user);
    }
    return 0;
}

I am testing it with only one logged in user. The output I get is this:

myusername
myusername

Why does my username appear twice? Would that happen if there were multiple users?

Details about my mac: enter image description here


Solution

  • getutxent() doesn't report users, it reports sessions.

    If you have multiple sessions open (for example, a terminal session), it will be logged in the user accounting database and retrieved blindly. You can verify this by checking the ut_id and ud_line elements of the utmpx structure. They should be different for each instance where ut_user is the same, as they inhabit separate processes (and terminals, if you're using that).