linux - 访问/proc时会触发哪个内核函数?

标签 linux linux-kernel

哪个函数负责/proc创建的主要逻辑?

我必须实现这个问题中提到的行为。 Is /proc directory generated dynamically per request?

我想有一个函数可以循环任务列表并创建相应的条目。我正在寻找类似的东西。

我寻找了使用 create_proc_entry() 函数的函数,但找不到突出的东西。

最佳答案

I suppose there is a function that loops through the task list and creates the corresponding entries. I am looking for something like that.

proc_pid_readdir()函数位于 fs/proc/base.c正是这样做的。

for 循环创建了所有 /proc/PID条目。 iter.task当前是task_struct指针

int proc_pid_readdir(struct file *file, struct dir_context *ctx)
{
    /*
     .
     .
     .
      */
    for (iter = next_tgid(ns, iter);
         iter.task;
         iter.tgid += 1, iter = next_tgid(ns, iter)) {
        char name[PROC_NUMBUF];
        int len;

        if (!has_pid_permissions(ns, iter.task, 2))
            continue;

        len = snprintf(name, sizeof(name), "%d", iter.tgid);
        ctx->pos = iter.tgid + TGID_OFFSET;
        if (!proc_fill_cache(file, ctx, name, len,
                     proc_pid_instantiate, iter.task, NULL)) {
            put_task_struct(iter.task);
            return 0;
        }
    }
    ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
    return 0;
}

关于linux - 访问/proc时会触发哪个内核函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33317088/

相关文章:

linux - 命令行 SFTP 文件浏览器?

linux - 通过通配符排除文件扩展名

linux - 编译linux内核

memory-management - Linux内核设置GDT的位置

c++ - Linux/GCC 中 mmap() 和 memalign() 的组合?

c - 如何使 USB 设备启用多点触控?

c++ - C++ Linux 中的套接字超时

linux - 附加到 shell 中具有特定键的行

linux - 带有变量的awk中的gsub

c - 如何向内核 task_struct 添加一个字段?