1

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;
4

2 回答 2

0

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

  • 统计文件获取st_devst_ino
  • /proc/*/fd/*为每个条目走和:
  • 如果st_dev&st_ino匹配则增加计数
于 2013-03-14T11:40:20.807 回答
0

int scan_proc(data* fileData, int* Count)

{
DIR*           pDir;
struct dirent* dEntry;
int            retCode = 0;
int            dirPattern;

pDir = opendir(".");
if (!pDir)
    {
    return -1;
    }
while ((dEntry = readdir(pDir)) != NULL)
    {
    if (!strcmp(dEntry->d_name, ".") || !strcmp(dEntry->d_name, ".."))
        {
        continue;
        }

    dirPattern = strtol(dEntry->d_name, NULL, 10);
    if (errno == ERANGE || dirPattern == 0)
        {
        continue;
        }
    if (chdir(dEntry->d_name) < 0)
        {
        continue;
        }
    retCode = scan_pid_dir("fd", fileData, pCount);
    if(retCode)
       {
       return -1;
       }
    chdir("/proc");
  }
closedir(pDir);
return 0;
}
于 2013-04-11T07:33:20.007 回答