2

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

jamespick
  • 1,974
  • 3
  • 23
  • 45
  • 1
    Yosemite is only a beta product. This may be a known issue which is fixed in the final release. I suggest asking this in the Apple Developer Forums: https://devforums.apple.com/ – TheDarkKnight Sep 03 '14 at 12:43

1 Answers1

4

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).

yossarian
  • 1,537
  • 14
  • 21