Creation of proc entrt using proc_create_data in kernel version 3.10 and above.

In the post “Creating read only proc entry in kernel versions above 3.10. ” we saw how to create a proc entry using the function proc_create.

Note: The following module is valid only of kernel version 3.10 and above.

Another function available in kernel version 3.10 and above for creation of proc entry is proc_create_data which is defined in proc_fs.hs as

struct proc_dir_entry *proc_create_data(const char *name, umode_t mode,struct proc_dir_entry *parent,const struct file_operations *proc_fops,void *data);

Where

name: The name of the proc entry
mode: The access mode for proc entry
parent: The name of the parent directory under /proc
proc_fops: The structure in which the file operations for the proc entry will be created.
data: If any data needs to be passed to the proc entry.

For example to create a proc entry by the name “hello” under /proc the above functions will be defined are

proc=proc_create_data(“hello”,0,NULL,&proc_fops,msg);

Along with creation of the entry we are also passing a function to the proc entry in using the porinter “msg”.

Now we need to create file_operations structure proc_fops in which we can map the read function for the proc entry.

struct file_operations proc_fops = { read: read_proc };

Next we need to add the function read_proc which will give to the user space the data that we want to export from the kernel space.

int read_proc(struct file *filp,char *buf,size_t count,loff_t *offp ) { char *data; data=PDE_DATA(file_inode(filp)); if(!(data)){ printk(KERN_INFO “Null data”); return 0; } if(count>temp) { count=temp; } temp=temp-count; copy_to_user(buf,data, count); if(count==0) temp=len; return count; }

To access the data in the proc_dir_structure we need to make use of the function PDE_DATA to which we pass the file pointer. The function in turn returs a pointer to the data that was passed during the creation of the proc entry.

The message that we want to display will be defined in the function create_new_proc_entry in which we will also call the fuction for creation of proc entry.

void create_new_proc_entry() { msg=”Hello World “; proc=proc_create_data(“hello”,0,NULL,&proc_fops,msg); len=strlen(msg); temp=len; }

The init and clean up functions for the module are

int proc_init (void) { create_new_proc_entry(); return 0; } void proc_cleanup(void) { remove_proc_entry(“hello”,NULL); }

The full code for creation of proc entry using proc_create_data is

proc_read_data.c

#include #include #include #include #include #include #include #include struct proc_dir_entry *proc; int len,temp; char *msg; int read_proc(struct file *filp,char *buf,size_t count,loff_t *offp ) { char *data; data=PDE_DATA(file_inode(filp)); if(!(data)){ printk(KERN_INFO “Null data”); return 0; } if(count>temp) { count=temp; } temp=temp-count; copy_to_user(buf,data, count); if(count==0) temp=len; return count; } struct file_operations proc_fops = { read: read_proc }; void create_new_proc_entry() { msg=”Hello World “; proc=proc_create_data(“hello”,0,NULL,&proc_fops,msg); len=strlen(msg); temp=len; } int proc_init (void) { create_new_proc_entry(); return 0; } void proc_cleanup(void) { remove_proc_entry(“hello”,NULL); } MODULE_LICENSE(“GPL”); module_init(proc_init); module_exit(proc_cleanup);

The makefile for the compilation of the modules are

ifneq ($(KERNELRELEASE),) obj-m := proc_read_data_3_10.o else #KERNELDIR ?= /lib/modules/$(shell uname -r)/build KERNELDIR ?= /home/nitin/Desktop/src/linux-3.11.1/ PWD := $(shell pwd) default: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules endif clean: $(MAKE) -C $(KERNELDIR) M=$(PWD) clean

Compile and load the module using

$ make $ sudo insmod proc_read_3_10.ko

We can see the output by using the cat command $ cat /proc/hello Hello World

Related Posts:

Creating read only proc entry in kernel versions above 3.10.

For kernel version before 3.10

Creating a proc entry – 1

Creating a proc read entry


Tags: , ,
Copyright 2017. All rights reserved.

Posted October 5, 2013 by Tux Think in category "Linux