Tutorial :
Program in a KERNEL to be use by user for open,close,read and write, along with the USER program to access Kernel program, with make file.
-----------------------------------------------------------------------------------------------------
/* Program to make a kernel to be accessed by the user */
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/kdev_t.h>
#include<linux/fs.h>
#include<linux/cdev.h>
#include<asm/uaccess.h>
MODULE_LICENSE("GPL");
struct cdev rss_str;
int major=0;
int minor=0;
dev_t rss;
void rss_reg();
static int rss_init()
{
printk("intializing module\n");
rss_reg();
return 0; }
static void rss_release()
{
printk("Exiting module\n");
cdev_del(&rss_str);
unregister_chrdev_region(rss,1);
}
// READ file
ssize_t rss_read(struct file *rss_str,char *rssuserbuff,size_t rssnumbytes,loff_t *rss_off)
{
char buff[]="NOIDA\0";
printk("Reading:\n");
copy_to_user(rssuserbuff,buff,6);
return 6;
}
// Write file
ssize_t rss_write(struct file *rss_str,const char * rssuserbuff, size_t rssnumbytes, loff_t *rss_off)
{
char buff[]="NOIDA\0";
printk("writing:\n");
copy_from_user(rssuserbuff,buff,6);
printk("%s\n",buff);
return 6;
}
//open file
int rss_open(struct inode *rss_str, struct file rss_file)
{ printk("opening:\n");
return 0;
}
//close file int rss_close(struct inode *rss_str, struct file rss_file)
{ printk("closing:\n");
return 0;
}
struct file_operations rss_fops={
.owner=THIS_MODULE,
.read=rss_read,
.write=rss_write,
.open=rss_open,
.release=rss_close,
};
void rss_reg()
{
int res,num;
res=alloc_chrdev_region(&rss,0,1,"rss");
if(res<0)
printk("Registration Failed.... \n");
major=MAJOR(rss);
minor=MINOR(rss);
printk("Major no = %d \n",major);
printk("Minor no = %d \n",minor);
num=MKDEV(major,minor);
printk("Device no = %d \n",num);
cdev_init(&rss_str,&rss_fops);
rss_str.owner=THIS_MODULE;
cdev_add(&rss_str,num,1);
}
module_init(rss_init);
module_exit(rss_release);
------------------------------------------------------------------------------------------------
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/kdev_t.h>
#include<linux/fs.h>
#include<linux/cdev.h>
#include<asm/uaccess.h>
MODULE_LICENSE("GPL");
struct cdev rss_str;
int major=0;
int minor=0;
dev_t rss;
void rss_reg();
static int rss_init()
{
printk("intializing module\n");
rss_reg();
return 0; }
static void rss_release()
{
printk("Exiting module\n");
cdev_del(&rss_str);
unregister_chrdev_region(rss,1);
}
// READ file
ssize_t rss_read(struct file *rss_str,char *rssuserbuff,size_t rssnumbytes,loff_t *rss_off)
{
char buff[]="NOIDA\0";
printk("Reading:\n");
copy_to_user(rssuserbuff,buff,6);
return 6;
}
// Write file
ssize_t rss_write(struct file *rss_str,const char * rssuserbuff, size_t rssnumbytes, loff_t *rss_off)
{
char buff[]="NOIDA\0";
printk("writing:\n");
copy_from_user(rssuserbuff,buff,6);
printk("%s\n",buff);
return 6;
}
//open file
int rss_open(struct inode *rss_str, struct file rss_file)
{ printk("opening:\n");
return 0;
}
//close file int rss_close(struct inode *rss_str, struct file rss_file)
{ printk("closing:\n");
return 0;
}
struct file_operations rss_fops={
.owner=THIS_MODULE,
.read=rss_read,
.write=rss_write,
.open=rss_open,
.release=rss_close,
};
void rss_reg()
{
int res,num;
res=alloc_chrdev_region(&rss,0,1,"rss");
if(res<0)
printk("Registration Failed.... \n");
major=MAJOR(rss);
minor=MINOR(rss);
printk("Major no = %d \n",major);
printk("Minor no = %d \n",minor);
num=MKDEV(major,minor);
printk("Device no = %d \n",num);
cdev_init(&rss_str,&rss_fops);
rss_str.owner=THIS_MODULE;
cdev_add(&rss_str,num,1);
}
module_init(rss_init);
module_exit(rss_release);
------------------------------------------------------------------------------------------------
User Program:
#include<stdio.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
main()
{
int fd;
char ubuff[80]="hai hello world", kbuff[80];
fd = open ("/dev/rss", O_RDWR , 0777 ) ;
if (fd <0 )
{
printf("error in opening file\n");
exit (-1 ) ;
}
if(write ( fd, ubuff, 6 ) < 0)
printf("can't write\n");
if(read (fd, kbuff,100 )<0)
printf("can't read\n") ;
printf ("\n Kernel Data: %s", kbuff);
close(fd);
return 0 ;
}
---------------------------------------------------------------------------------------------------------
Make File
obj-m:=rss.o
KERNELDIR=/lib/modules/$(shell uname -r)/build
all:
make -C $(KERNELDIR) M=$(shell pwd) modules
clean:
make -C $(KERNELDIR) M=$(shell pwd) clean
0 comments:
Post a Comment