Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to build a simple kernel module. Following are the contents of file involved in it:


    #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("\n Open \n");
    return 0;

    static int device_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long args)
    case IOCTL_CMD:
    printk(KERN_ALERT "\n %s \n", (char *)args);
    return 0;

    static int device_release(struct inode *inode, struct file *file)
    printk("\n Release \n");
    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("\n 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("\n Device Initialized in kernel ....!!!");
    return 0;

    static void hello_exit(void)
    printk("\n Device is Released or closed \n");
    unregister_chrdev(major_no, DEVICE_NAME);




    #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\n");
    return 0;


    #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.


share|improve this question
up vote 2 down vote accepted

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.

share|improve this answer
Kees Cook: Thanks, One more thing what is the difference b/w unlocked_ioctl and device_ioctl. Is there any updated source of reference, for module development, that lists all such changes with kernel code. – gkt Jul 25 '11 at 18:12

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.