我可以使用“sysctl”来检索用户的进程列表吗?

发布于 2024-12-09 04:13:18 字数 1733 浏览 0 评论 0原文

我需要一种方法来检索 Mac 上所有用户的所有正在运行的进程(使用 Cocoa)。我找到了一个使用 sysctl 检索进程的实现,但我还需要运行用户。这是我获取进程列表的内容的片段,但是有没有办法修改它以包含用户?

int             err;
kinfo_proc *    result;
bool            done;

static const int    name[] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 };
size_t          length;

// a valid pointer procList holder should be passed
assert( procList != NULL );
// But it should not be pre-allocated
assert( *procList == NULL );
// a valid pointer to procCount should be passed
assert( procCount != NULL );

*procCount = 0;

result = NULL;
done = false;

do
{
    assert( result == NULL );

    // Call sysctl with a NULL buffer to get proper length
    length = 0;
    err = sysctl((int *)name,(sizeof(name)/sizeof(*name))-1,NULL,&length,NULL,0);
    if( err == -1 )
        err = errno;

    // Now, proper length is optained
    if( err == 0 )
    {
        result = malloc(length);
        if( result == NULL )
            err = ENOMEM;   // not allocated
    }

    if( err == 0 )
    {
        err = sysctl( (int *)name, (sizeof(name)/sizeof(*name))-1, result, &length, NULL, 0);
        if( err == -1 )
            err = errno;

        if( err == 0 )
            done = true;
        else if( err == ENOMEM )
        {
            assert( result != NULL );
            free( result );
            result = NULL;
            err = 0;
        }
    }
} while ( err == 0 && !done );

// Clean up and establish post condition
if( err != 0 && result != NULL )
{
    free(result);
    result = NULL;
}

*procList = result; // will return the result as procList
if( err == 0 )
    *procCount = length / sizeof( kinfo_proc );

assert( (err == 0) == (*procList != NULL ) );

return err;

I am in need of a way to retrieve all running processes for all users on a Mac (using Cocoa). I found an implementation to retrieve the process using sysctl, but I also need the running user. This is a snipping of what I've got to get the process list, but is there a way to modify it to include the user as well?

int             err;
kinfo_proc *    result;
bool            done;

static const int    name[] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 };
size_t          length;

// a valid pointer procList holder should be passed
assert( procList != NULL );
// But it should not be pre-allocated
assert( *procList == NULL );
// a valid pointer to procCount should be passed
assert( procCount != NULL );

*procCount = 0;

result = NULL;
done = false;

do
{
    assert( result == NULL );

    // Call sysctl with a NULL buffer to get proper length
    length = 0;
    err = sysctl((int *)name,(sizeof(name)/sizeof(*name))-1,NULL,&length,NULL,0);
    if( err == -1 )
        err = errno;

    // Now, proper length is optained
    if( err == 0 )
    {
        result = malloc(length);
        if( result == NULL )
            err = ENOMEM;   // not allocated
    }

    if( err == 0 )
    {
        err = sysctl( (int *)name, (sizeof(name)/sizeof(*name))-1, result, &length, NULL, 0);
        if( err == -1 )
            err = errno;

        if( err == 0 )
            done = true;
        else if( err == ENOMEM )
        {
            assert( result != NULL );
            free( result );
            result = NULL;
            err = 0;
        }
    }
} while ( err == 0 && !done );

// Clean up and establish post condition
if( err != 0 && result != NULL )
{
    free(result);
    result = NULL;
}

*procList = result; // will return the result as procList
if( err == 0 )
    *procCount = length / sizeof( kinfo_proc );

assert( (err == 0) == (*procList != NULL ) );

return err;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江挽川 2024-12-16 04:13:18

请注意,sysctl(3) 返回的进程列表是 struct kinfo_proc 的数组。如果你读过 kinfo_proc 的声明,你会发现它有一个 struct eproc 类型的 kp_eproc 成员,而它又有一个 struct _ucred 类型的 e_ucred 成员,而 struct _ucred 又有一个 uid_t 类型的 cr_uid 成员,代表有效用户 id那个过程。

这意味着你可以通过链

.kp_eproc.e_ucred.cr_uid

来获取有效用户的id。例如:

for (int i = 0; i < procCount; i++) {
    printf("pid=%d, uid=%d\n",
        procList[i].kp_proc.p_pid,
        procList[i].kp_eproc.e_ucred.cr_uid);
}

如果要将用户 id 转换为用户名,可以使用 getpwuid(3) 或其可重入/线程安全变体 getpwuid_r(3):

for (int i = 0; i < procCount; i++) {
    struct passwd *user = getpwuid(procList[i].kp_eproc.e_ucred.cr_uid);
    char *username = user ? user->pw_name : "getpwuid() failed";
    printf("pid=%d, user=%s\n",
        procList[i].kp_proc.p_pid,
        username);
}

这是一个示例程序,其中列出了所有进程及其相应的 pid,有效uid和对应的用户名:

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <pwd.h>

int main(void) {
    int err = 0;
    struct kinfo_proc *proc_list = NULL;
    size_t length = 0;

    static const int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 };

    // Call sysctl with a NULL buffer to get proper length
    err = sysctl((int *)name, (sizeof(name) / sizeof(*name)) - 1, NULL, &length, NULL, 0);
    if (err) goto ERROR;

    // Allocate buffer
    proc_list = malloc(length);
    if (!proc_list) goto ERROR;

    // Get the actual process list
    err = sysctl((int *)name, (sizeof(name) / sizeof(*name)) - 1, proc_list, &length, NULL, 0);
    if (err) goto ERROR;

    int proc_count = length / sizeof(struct kinfo_proc);

    // use getpwuid_r() if you want to be thread-safe

    for (int i = 0; i < proc_count; i++) {
        uid_t uid = proc_list[i].kp_eproc.e_ucred.cr_uid;
        struct passwd *user = getpwuid(uid);
        char *username = user ? user->pw_name : "user name not found";

        printf("pid=%d, uid=%d, username=%s\n",
                proc_list[i].kp_proc.p_pid,
                uid,
                username);
    }

    free(proc_list);

    return EXIT_SUCCESS;

ERROR:
    perror(NULL);
    free(proc_list);
    return EXIT_FAILURE;
}

Note that the process list returned by sysctl(3) is an array of struct kinfo_proc. If you read kinfo_proc’s declaration, you’ll see that it has a kp_eproc member of type struct eproc, which in turn has an e_ucred member of type struct _ucred, which in turn has a cr_uid member of type uid_t, representing the effective user id of that process.

This means that you can use the chain

.kp_eproc.e_ucred.cr_uid

to obtain the id of the effective user. For example:

for (int i = 0; i < procCount; i++) {
    printf("pid=%d, uid=%d\n",
        procList[i].kp_proc.p_pid,
        procList[i].kp_eproc.e_ucred.cr_uid);
}

If you want to convert the user id to a user name, you can use getpwuid(3) or its reentrant/thread-safe variant, getpwuid_r(3):

for (int i = 0; i < procCount; i++) {
    struct passwd *user = getpwuid(procList[i].kp_eproc.e_ucred.cr_uid);
    char *username = user ? user->pw_name : "getpwuid() failed";
    printf("pid=%d, user=%s\n",
        procList[i].kp_proc.p_pid,
        username);
}

Here’s a sample program that lists all processes with their corresponding pids, effective uids and corresponding usernames:

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <pwd.h>

int main(void) {
    int err = 0;
    struct kinfo_proc *proc_list = NULL;
    size_t length = 0;

    static const int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 };

    // Call sysctl with a NULL buffer to get proper length
    err = sysctl((int *)name, (sizeof(name) / sizeof(*name)) - 1, NULL, &length, NULL, 0);
    if (err) goto ERROR;

    // Allocate buffer
    proc_list = malloc(length);
    if (!proc_list) goto ERROR;

    // Get the actual process list
    err = sysctl((int *)name, (sizeof(name) / sizeof(*name)) - 1, proc_list, &length, NULL, 0);
    if (err) goto ERROR;

    int proc_count = length / sizeof(struct kinfo_proc);

    // use getpwuid_r() if you want to be thread-safe

    for (int i = 0; i < proc_count; i++) {
        uid_t uid = proc_list[i].kp_eproc.e_ucred.cr_uid;
        struct passwd *user = getpwuid(uid);
        char *username = user ? user->pw_name : "user name not found";

        printf("pid=%d, uid=%d, username=%s\n",
                proc_list[i].kp_proc.p_pid,
                uid,
                username);
    }

    free(proc_list);

    return EXIT_SUCCESS;

ERROR:
    perror(NULL);
    free(proc_list);
    return EXIT_FAILURE;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文