c - 移植代码 : is there any API equivalent to utssys() in Linux? 我需要将 utssys() unix API 移植到 Linux 环境吗?

标签 c

unix 中的 utssys() 给出文件上的用户总数。它是一个未记录的 API,在 Solaris 手册页和 Linux 手册页中都没有此 API 的手动条目。Linux 中是否有任何等效的 API 或我也可以在 Linux 中使用相同的东西(我不知道它没有记录)。我也用谷歌搜索但没有得到任何信息。请帮助。

int DU_Utssys_Unix(void* buf, int arg, int type, void* out)
    {
    int result;
    // Perform system-call
    errno = 0;
    if ((result = utssys(buf, arg, type, out)) < 0)
        return result;
    // Look into the result:
    return ((fu_data_t*)outbp)->fud_user_count;
    }

以上是我需要为 Linux 替换的代码段。我可以使用 syscall() 来找出文件上的用户数吗?如果是,它保存在哪里???

在 Unix 中,它们有以下结构来保存上面代码片段中使用的信息,我们在 Linux 中有相同的还是不同的?

typedef struct f_user 
    {
    int     fu_flags;       /* see below */
    union 
        {
        struct 
            {
            pid_t   u_pid;
            uid_t   u_uid;
            }u_info;
        struct 
            {
            int     k_modid;
            int     k_instance;
            int     k_minor;
            }k_info;
        } fu_info;
    }f_user_t;

typedef struct fu_data 
    {
    int             fud_user_max;
    int             fud_user_count;
    struct f_user   fud_user[1];
    }fu_data_t;

最佳答案

我认为在 Linux 中没有执行此操作的系统调用,但您可以以 root 身份执行以下操作:

  • 统计文件获取 st_devst_ino
  • 遍历 /proc/*/fd/* 并为每个条目:
  • 如果 st_devst_ino 匹配则增加计数

关于c - 移植代码 : is there any API equivalent to utssys() in Linux? 我需要将 utssys() unix API 移植到 Linux 环境吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15407135/

相关文章:

C 模拟到 STL

c - 编译 libPNG 示例时 undefined reference

c - 用 void * 转换奇怪的指针

c++ - FFmpeg:如何在从 RTSP 读取时控制控制台输出?

c - 两个消费者一个生产者的 pthread 同步

c - 循环平铺以旋转矩阵

c - 如何在 C 中将变量分配给 rand() ?

c - Malloc 与自定义分配器 : Malloc has a lot of overhead. 为什么?

c - 如何发送数组作为函数参数?

条件变量适用于 MacOS 但不适用于 Ubuntu