Question

I am learning about the proc and loadable kernel module (LKM) for reading data from the kernel to the user space. I asked for some info regarding procfs in another post.

Could someone please tell me what lkm and procfs are, and where I can write the code for lkm and proc (location in kernel source code)?

Was it helpful?

Solution

You don't write code for an LKM inside kernel source (though it's possible, it's not recommended unless you're working on what will become a normally distributed module). You instead create your own directory and provide your code.

The functions you write to provide procfs interfaces is just code that is part of your LKM source.

http://linux.die.net/lkmpg/x769.html has a simple example using procfs, reproduced here:

/**
 *  procfs2.c -  create a "file" in /proc
 *
 */

#include <linux/module.h>   /* Specifically, a module */
#include <linux/kernel.h>   /* We're doing kernel work */
#include <linux/proc_fs.h>  /* Necessary because we use the proc fs */
#include <asm/uaccess.h>    /* for copy_from_user */

#define PROCFS_MAX_SIZE     1024
#define PROCFS_NAME         "buffer1k"

/**
 * This structure hold information about the /proc file
 *
 */
static struct proc_dir_entry *Our_Proc_File;

/**
 * The buffer used to store character for this module
 *
 */
static char procfs_buffer[PROCFS_MAX_SIZE];

/**
 * The size of the buffer
 *
 */
static unsigned long procfs_buffer_size = 0;

/** 
 * This function is called then the /proc file is read
 *
 */
int 
procfile_read(char *buffer,
          char **buffer_location,
          off_t offset, int buffer_length, int *eof, void *data)
{
    int ret;

    printk(KERN_INFO "procfile_read (/proc/%s) called\n", PROCFS_NAME);

    if (offset > 0) {
        /* we have finished to read, return 0 */
        ret  = 0;
    } else {
        /* fill the buffer, return the buffer size */
        memcpy(buffer, procfs_buffer, procfs_buffer_size);
        ret = procfs_buffer_size;
    }

    return ret;
}

/**
 * This function is called with the /proc file is written
 *
 */
int procfile_write(struct file *file, const char *buffer, unsigned long count,
           void *data)
{
    /* get buffer size */
    procfs_buffer_size = count;
    if (procfs_buffer_size > PROCFS_MAX_SIZE ) {
        procfs_buffer_size = PROCFS_MAX_SIZE;
    }

    /* write data to the buffer */
    if ( copy_from_user(procfs_buffer, buffer, procfs_buffer_size) ) {
        return -EFAULT;
    }

    return procfs_buffer_size;
}

/**
 *This function is called when the module is loaded
 *
 */
int init_module()
{
    /* create the /proc file */
    Our_Proc_File = create_proc_entry(PROCFS_NAME, 0644, NULL);

    if (Our_Proc_File == NULL) {
        remove_proc_entry(PROCFS_NAME, &proc_root);
        printk(KERN_ALERT "Error: Could not initialize /proc/%s\n",
            PROCFS_NAME);
        return -ENOMEM;
    }

    Our_Proc_File->read_proc  = procfile_read;
    Our_Proc_File->write_proc = procfile_write;
    Our_Proc_File->owner      = THIS_MODULE;
    Our_Proc_File->mode       = S_IFREG | S_IRUGO;
    Our_Proc_File->uid    = 0;
    Our_Proc_File->gid    = 0;
    Our_Proc_File->size       = 37;

    printk(KERN_INFO "/proc/%s created\n", PROCFS_NAME);    
    return 0;   /* everything is ok */
}

/**
 *This function is called when the module is unloaded
 *
 */
void cleanup_module()
{
    remove_proc_entry(PROCFS_NAME, &proc_root);
    printk(KERN_INFO "/proc/%s removed\n", PROCFS_NAME);
}

The module initialization establishes a procfs entry with create_proc_entry(). The functions procfile_write and procfile_read are initialized to handle writes and reads on this entry. The module's cleanup_module() function, called when the module is unloaded, removes the procfs entry by calling cleanup_module().


You'll find a tutorial for building kernel modules at http://www.cyberciti.biz/tips/compiling-linux-kernel-module.html. The summary of that is:

1) Ensure you have kernel source installed in /usr/src.

2) Create a makefile that looks like:

obj-m = procfs2.o
KVERSION = $(shell uname -r)
all:
        make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
clean:
        make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean

3) build the module with the command make 4) load the module into memory with the command insmod procfs2.ko (do this as the root user)

Not listed in the tutorial is: if your module has problems, expect to reboot. Crashes in kernel modules will often take down your system.

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