Question

I wrote a kernel module memory leak detector that works by adding information about possible memory leaks to a list. I want to be able to loop through the list and write the information the a file for the user, this would be easy to do in user space with the code below but how world I use sysfs to export that information from kernel space to user so that the user can read it in a file?

/*
 * 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();
}
Was it helpful?

Solution

I can't say for /sys, but for /proc it's rather straightforward to do such things. This site shows how to create a /proc entry which can be cat'd.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top