【问题标题】:Exporting information from kernel space to user space via sysfs通过 sysfs 将信息从内核空间导出到用户空间
【发布时间】:2014-06-02 13:01:17
【问题描述】:

我编写了一个内核模块内存泄漏检测器,它通过将有关可能的内存泄漏的信息添加到列表来工作。我希望能够遍历列表并将信息写入用户的文件中,这在用户空间中使用下面的代码很容易做到,但是我如何使用 sysfs 将信息从内核空间导出到用户所以用户可以在文件中读取它吗?

/*
 * writes a memory leak summary to a file
 */
void mem_leak_summary(void)
{
    unsigned int i;
    MEM_PROFILER_LIST * mem_output;

    FILE * fp_write = fopen (SUMMARY_FILE, "wt");
    char info[1024];

    if(fp_write != NULL)
    {

        fwrite(info, (strlen(info) + 1) , 1, fp_write);
        sprintf(info, "%s\n", "-----------------------------------");   
        fwrite(info, (strlen(info) + 1) , 1, fp_write);

        for(mem_output= ptr_start; mem_output!= NULL; mem_output= mem_output->next)
        {
            sprintf(info, "address : %d\n", leak_info->mem_output.address);
            fwrite(info, (strlen(info) + 1) , 1, fp_write);
            sprintf(info, "size    : %d bytes\n", leak_info->mem_output.size);          
            fwrite(info, (strlen(info) + 1) , 1, fp_write);
            sprintf(info, "line    : %d\n", leak_info->mem_output.line);
            fwrite(info, (strlen(info) + 1) , 1, fp_write);
            sprintf(info, "%s\n", "-----------------------------------");   
            fwrite(info, (strlen(info) + 1) , 1, fp_write);
        }
    }   
    clear();
}

【问题讨论】:

    标签: linux-kernel kernel-module sysfs


    【解决方案1】:

    我不能说 /sys,但是 /proc 做这些事情相当简单。 This site shows how to create a /proc entry 可以加猫。

    【讨论】:

      猜你喜欢
      • 2011-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-22
      • 2014-08-18
      • 1970-01-01
      • 2012-06-21
      相关资源
      最近更新 更多