Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kernel Module Programming

Tags:

c

linux

I'm trying to read and write to a proc file through kernel module
But when I run this command :

echo "hello" >> /proc/hello && cat /proc/hello

It doesn't print anything and when i open the file through text editor. I found mysterious symbols like this

 ^@^@^@^@^@^@^@^@^@^@ 

Any help will be appreciated thanks in advance

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include<linux/sched.h>
#include <asm/uaccess.h>
#include <linux/slab.h>

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;
}

int write_proc(struct file *filp,const char *buf,size_t count,loff_t *offp){
    copy_from_user(msg,buf,count);
    len=count;
    temp=len;
    return count;
}

struct file_operations proc_fops = {
    read: read_proc,
    write: write_proc
};

void create_new_proc_entry(void){
    proc_create("hello",0,NULL,&proc_fops);
    msg=kmalloc(GFP_KERNEL,10*sizeof(char));
}

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);
like image 408
Master AN HA Avatar asked Apr 13 '16 14:04

Master AN HA


People also ask

What is a kernel module?

Kernel modules are pieces of code that can be loaded and unloaded into the kernel upon demand. They extend the functionality of the kernel without the need to reboot the system. A module can be configured as built-in or loadable.

What is a kernel programming?

January 2022) The kernel is a computer program at the core of a computer's operating system and generally has complete control over everything in the system. It is the portion of the operating system code that is always resident in memory and facilitates interactions between hardware and software components.

What is Linux kernel programming?

The Linux kernel is a free and open-source, monolithic, modular, multitasking, Unix-like operating system kernel. It was originally authored in 1991 by Linus Torvalds for his i386-based PC, and it was soon adopted as the kernel for the GNU operating system, which was written to be a free (libre) replacement for Unix.

Can I write my own kernel?

If you are writing your own bootloader for loading a kernel you need to know the overall addressing/interrupts of memory as well as BIOS. Mostly each operating system has specific bootloader for it. There are lots of bootloaders available out there in online market.


1 Answers

Apart from other problems of your kernel module (like boundaries check)

This

msg=kmalloc(GFP_KERNEL,10*sizeof(char));

have to be

 msg=kmalloc(10*sizeof(char), GFP_KERNEL);

With your call to kmalloc you are trying, probably, to allocate too many or not enough bytes and it refuses your kmalloc request.

You should always check the kmalloc return value to be consistent: != NULL

like image 107
LPs Avatar answered Sep 29 '22 11:09

LPs