anubhav
anubhav

Reputation: 131

kernel module program using proc

I have made the following kernel module to create a process "hello_proc" in /proc directory:

#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>

static int hello_proc_show(struct seq_file *m, void *v) 
{
    seq_printf(m, "P5 : Hello proc!\n");
    return 0;
}

static int hello_proc_open(struct inode *inode, struct  file *file) 
{
    return single_open(file, hello_proc_show, NULL);
}

static const struct file_operations hello_proc_fops = {
    .owner = THIS_MODULE,
    .open = hello_proc_open,
    .read = seq_read,
    //.write = seq_write,
    .llseek = seq_lseek,
    .release = single_release,
};

static int hello_proc_init(void) 
{
    proc_create("hello_proc", 0, NULL, &hello_proc_fops);
    //printk("P5 : Process hello proc created");
    return 0;
}

static void hello_proc_exit(void) 
{
    remove_proc_entry("hello_proc", NULL);
}

MODULE_LICENSE("GPL");
module_init(hello_proc_init);
module_exit(hello_proc_exit);

I now want to write (and read) contents of a command, say "ls -l /proc" to the proc file "hello_proc".

My question is, how to resolve the following error that I am getting while writing following data to proc file "hello_proc":

anubhav@anubhav-Inspiron-3421:~/Desktop/pro/p5$ sudo ls -l -t /proc | head -21 > /proc/hello_proc
bash: /proc/hello_proc: Permission denied 

anubhav@anubhav-Inspiron-3421:~/Desktop/pro/p5$ ls -l -t /proc | head -21 > sudo /proc/hello_proc
ls: cannot access /proc/4273: No such file or directory

anubhav@anubhav-Inspiron-3421:~/Desktop/pro/p5$ ls -l -t /proc | head -21 > /proc/hello_proc
bash: /proc/hello_proc: Permission denied

Upvotes: 0

Views: 870

Answers (1)

Santosh A
Santosh A

Reputation: 5351

Firstly to resolve that Permission Denied Error

  1. Login as root (su)
  2. Run the command

    $ ls -l -t /proc | head -21 > /proc/hello_proc
    

Now you will face another issue as below.

head: write error: Input/output error
head: write error

The reason being that you not have written the write file operations fops for the proc file. In the code you have commented out the write operation.

Upvotes: 2

Related Questions