0

I wanted to create two entries in /proc through the kernel module; So, when the user reads either of them, some different output is printed from each of them. e.g:

cat /proc/entry1
>>>Hello World
cat /proc/entry2
>>> Goodbye World

Here's what i tried to do:

#include<linux/init.h>
#include<linux/module.h>
#include<linux/kernel.h>
#define PROC_NAME1 "entry1"
#define PROC_NAME2 "entry2"
#define BUFFER_SIZE 1024
ssize proc_read (struct file*,char __user*,size_t,loff_t*);
static struct file_operations fops 
{
     .read = proc_read
};
init_func() 
{
    create_proc(PROC_NAME1,0,NULL,&fops);
    create_proc(PROC_NAME2,0,NULL,&fops);

}
exit_func() 
{
    remove_proc(PROC_NAME1,0,NULL);
    remove_proc(PROC_NAME2,0,NULL);
}
ssize_t proc_read (struct file* file,char __user* usr_buf, size_t cout, loff_t *pos) 
{
      static char char_buffer[BUFFER_SIZE];
      int ret;
      static int done = 0;
      if(done) 
      {
       done = 0;
       return 0;
      }
       done = 1;
       ret = sprintf(chat_buffer,"some message");
       raw_copy_to_user(usr_buf,char_buffer,ret);
       return ret;
}

init_module(init_func);
exit_module(exit_func);

Here's my problem: I created two entries but I have no idea how to write two different strings (actually where to write them) so they are printed when one of the files is read. Anyone can help???

Amin
  • 7
  • 3
  • Many of the functions you're using are either not valid or don't exist. `sprintf` is not in the Linux Kernel (In the same way `printf` is replaced by `printk`) and `create_proc`/`remove_proc` are not real. I recommend checking out proc_fs.h to see how to create entries in procfs. – Sharad Khanna Oct 30 '21 at 21:31
  • @SharadKhanna you're right; I know that a couple of other headers are needed to add. But that ain't the issue; I already explained what i'm looking for – Amin Oct 31 '21 at 06:44
  • @SharadKhanna, `sprintf()` is valid Linux kernel API. – 0andriy Oct 31 '21 at 09:01
  • Your problem is in usage of the same file operations. Each file should have its own, different to the rest, operations structure. – 0andriy Oct 31 '21 at 09:02
  • @0andriy Yes! That was the missing part! I thought the file_operation structure is for this entire process of reading & writing files, but I didn't know it must be defined for each file. Please write your comment as an answer so i can accept it. – Amin Oct 31 '21 at 11:07

0 Answers0