Creating read only proc entry in kernel versions above 3.10.

The creation of proc entries has undergone a considerable change in kernel version 3.10 and above. In this post we will see one of the methods we can use in linux kernel version 3.10 and above let us see how we can create proc entries in version 3.10 and above.

The function is defined in proc_fs.h as

static inline struct proc_dir_entry *proc_create(const char *name, umode_t mode,struct proc_dir_entry *parent, const struct file_operations *proc_fops)

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.

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

proc_create(“hello”,0,NULL,&proc_fops);

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 ) { if(count>temp) { count=temp; } temp=temp-count; copy_to_user(buf,msg, count); if(count==0) temp=len; return count; }

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() { proc_create(“hello”,0,NULL,&proc_fops); msg=” Hello World “; len=strlen(msg); temp=len; printk(KERN_INFO “1.len=%d”,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 of module for creation of proc entry using proc_create is

#include #include #include #include#include int len,temp; char *msg; int read_proc(struct file *filp,char *buf,size_t count,loff_t *offp ) { if(count>temp) { count=temp; } temp=temp-count; copy_to_user(buf,msg, count); if(count==0) temp=len; return count; } struct file_operations proc_fops = { read: read_proc }; void create_new_proc_entry() { proc_create(“hello”,0,NULL,&proc_fops); msg=” Hello World “; len=strlen(msg); temp=len; printk(KERN_INFO “1.len=%d”,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 module is

ifneq ($(KERNELRELEASE),) obj-m := proc_read_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

We can see that the message that we passed in the read function of the module is being displayed when the proc entry is read.

Related Posts : For kernel version before 3.10 Creating a proc entry – 1 Creating a proc read entry


Tags: , ,
Copyright 2017. All rights reserved.

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