Thursday, April 25, 2024
 Popular · Latest · Hot · Upcoming
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 13204  / 2 Years 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

 Answers
2

A few things:




  • You want to use "unlocked_ioctl" not "compat_ioctl".

  • The function interface for "device_ioctl" is wrong (see include/linux/fs.h), it should be:



        long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);

  • The appln.c doesn't check error codes (open, ioctl).



After fixing that, the code will work fine.


[#44133] Friday, July 8, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
fenddy

Total Points: 361
Total Questions: 103
Total Answers: 113

Location: Turkmenistan
Member since Sun, Aug 2, 2020
4 Years ago
fenddy questions
Tue, Nov 22, 22, 10:11, 1 Year ago
Tue, Sep 27, 22, 09:16, 2 Years ago
Wed, Dec 28, 22, 13:09, 1 Year ago
Fri, Jun 18, 21, 14:04, 3 Years ago
;