4
rated 0 times
[
4]
[
0]
/ answers: 1 / hits: 13088
/ 1 Year ago, thu, july 7, 2022, 2:54:26
I am trying to build a simple kernel module. Following are the contents of file involved in it:
module.c:
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/kernel.h>
#include "header.h"
static int device_open(struct inode *inode, struct file *file)
{
printk("
Open
");
return 0;
}
static int device_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long args)
{
switch(cmd)
{
case IOCTL_CMD:
printk(KERN_ALERT "
%s
", (char *)args);
break;
}
return 0;
}
static int device_release(struct inode *inode, struct file *file)
{
printk("
Release
");
return 0;
}
static struct class *my_class;
static struct file_operations fops={
.open = device_open,
.release = device_release,
.compat_ioctl = device_ioctl
};
static int hello_init(void)
{
major_no = register_chrdev(0, DEVICE_NAME, &fops);
printk("
Major_no : %d", major_no);
my_class = class_create(THIS_MODULE, DEVICE_NAME);
device_create(my_class, NULL, MKDEV(major_no,0), NULL, DEVICE_NAME);
printk("
Device Initialized in kernel ....!!!");
return 0;
}
static void hello_exit(void)
{
printk("
Device is Released or closed
");
device_destroy(my_class,MKDEV(major_no,0));
class_unregister(my_class);
class_destroy(my_class);
unregister_chrdev(major_no, DEVICE_NAME);
printk("
===============================================================
");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
appln.c
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include "header.h"
int main()
{
int fd;
char * msg = "yahoooo";
fd = open(DEVICE_PATH, O_RDWR);
ioctl(fd, IOCTL_CMD, msg);
printf("ioctl executed
");
close(fd);
return 0;
}
header.h:
#include <linux/ioctl.h>
#include <linux/kdev_t.h> /* for MKDEV */
#define DEVICE_NAME "my_dev"
#define DEVICE_PATH "/dev/my_dev"
#define WRITE 0
static int major_no;
#define MAGIC_NO '4'
/*
* Set the message of the device driver
*/
#define IOCTL_CMD _IOR(MAGIC_NO, 0, char *)
My module loads perfectly(I can see the mesg in hello_init() function). But when i run the appln.c program, even when it makes the ioctl() call, I see no result of it. Can someone tell why is the module ignoring my ioctl call.
Thanks,
More From » kernel