简体   繁体   English

OSX Yosemite getutxent()两次返回同一用户

[英]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> . 我试图使用在苹果的<utmpx.h>定义的getutxent()函数来获取当前登录用户的列表。 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: 有关我的mac的详细信息: 在此处输入图片说明

getutxent() doesn't report users, it reports sessions. getutxent()不报告用户,它报告会话。

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. 您可以通过检查utmpx结构的ut_idud_line元素来验证这一点。 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). 对于每个ut_user相同的实例,它们应该是不同的,因为它们ut_user在单独的进程(和终端,如果使用的话)中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM