1

This question is in regard to my ioctl error call from the user space to kernel space in a Ubuntu Linux OS running as a Virtual Machine in Oracle's Virtual Box. After the module code was compiled, it was loaded into the Linux kernel using the "sudo insmod ioctl_module.ko" command and you can see that succeeded by the message listing below. For some reason, the ioctl call from the user space to the kernel module function says one of the arguments is invalid, and I don't see that it is. (Also, this is only the 2nd time I've ever posted a question to stack overflow, so I hope I followed your conventions correctly!) Thanks, Karen

user level code error message:

saasbook@saasbook:~/cs552/osPrimer/invokeKernelServices/user_level_code$ ./ioctl_test

size of testStruct.field1 = 1

size of testStruct.field2 = 1

first ioctl: Invalid argument

code listing for ioctl_test.c:

https://gist.github.com/KarenWest/6629582

can see ioctl_module is in kernel:

/invokeKernelServices$ dmesg

[13563.849593] Loading module

[13578.846063] pseudo_device_ioctl

code_listing for ioctl_module.c:

https://gist.github.com/KarenWest/6629557


I can now be more specific on my question from a few days ago. Using the ioctl.h bit definitions, using the #define macros for _IOW, shifting and oring them together, I see that the IOCTL_TEST = 0x4002006 is correct, and the cmd = 0x804a030 is incorrect, but I cannot figure out why cmd has the wrong value. cmd is an argument to the above ioctl entry point:

static int pseudo_device_ioctl(struct inode *inode, struct file *file,
            unsigned int cmd, unsigned long arg)

So I had a look at the proc_fs.h input file's proc_dir_entry structure, where the proc_fops pointer in that structure is set to my ioctl entry point function, with pseudo_dev_proc_operations initialized to a file_operations structure, and proc_entry initialized as a pointer to a proc_dir_entry structure (all in code links on git above), with all these initialized in the ioctl_module.c's initialization_routine(), which I understood had set up this ioctl with the correct values, once code running at the ioctl_test.c level calls, and the OS should be set up to direct it to my ioctl function, and it does. However, the argument sent to my ioctl function set up (cmd) is incorrect, and does not match IOCTL_TEST, which is why this fails.

pseudo_dev_proc_operations.unlocked_ioctl = pseudo_device_ioctl;
pseudo_dev_proc_operations.compat_ioctl = pseudo_device_ioctl;

proc_entry = create_proc_entry("ioctl_test", 0666, NULL);
proc_entry->proc_fops = &pseudo_dev_proc_operations;

So I followed the proc_fs.h's proc_fops pointer (which is initialized to pseudo_dev_proc_operations, which takes it to my ioctl pseudo_device_ioctl function) to see why the cmd argument might get messed up in processing, and I am lost - no idea why this is happening.

If anyone has any further comments they can make that may help me figure out where I've gone wrong, please let me know. Thanks! Karen


This might help - just read this:

"The Linux Kernel Module Programming Guide:

Chapter 7. Talking To Device Files

7.1. Talking to Device Files (writes and IOCTLs)"

4
  • please paste the relevant code here
    – stdcall
    Sep 19, 2013 at 20:52
  • @user1943660 What is the value of 'cmd' in the ioctl() handler? Also, you should use the driver magic signature on all ioctls.
    – Peter L.
    Sep 19, 2013 at 21:42
  • Thank you for the help. I did at first try to indent my code 4 spaces but the submit button complained that there was something wrong between the code post and text post mixed in between that was not indented as the code part was, so I decided to take the easy way out and post my gist instead, so thank you for looking there and responding - new to stack overflow and exchange. I did print cmd and that comment follows the ioctl.h file posted below. Sep 20, 2013 at 23:58
  • I could not fit my next part of my question in this comment section, so it is in the Answer section below - was not sure what to do there! When I posted it, it said I should put it in the comment response section here, but it would not fit? Sep 25, 2013 at 17:33

1 Answer 1

0
#define _IOC_NRBITS     8
#define _IOC_TYPEBITS   8

/*
 * Let any architecture override either of the following before
 * including this file.
 */
#ifndef _IOC_SIZEBITS
# define _IOC_SIZEBITS  14
#endif
#ifndef _IOC_DIRBITS
# define _IOC_DIRBITS   2
#endif

#define _IOC_NRMASK     ((1 << _IOC_NRBITS)-1)
#define _IOC_TYPEMASK   ((1 << _IOC_TYPEBITS)-1)
#define _IOC_SIZEMASK   ((1 << _IOC_SIZEBITS)-1)
#define _IOC_DIRMASK    ((1 << _IOC_DIRBITS)-1)

#define _IOC_NRSHIFT    0
#define _IOC_TYPESHIFT  (_IOC_NRSHIFT+_IOC_NRBITS)
#define _IOC_SIZESHIFT  (_IOC_TYPESHIFT+_IOC_TYPEBITS)
#define _IOC_DIRSHIFT   (_IOC_SIZESHIFT+_IOC_SIZEBITS)

/*
 * Direction bits, which any architecture can choose to override
 * before including this file.
 */
#ifndef _IOC_NONE
# define _IOC_NONE      0U
#endif
#ifndef _IOC_WRITE
# define _IOC_WRITE     1U
#endif
#ifndef _IOC_READ
# define _IOC_READ      2U
#endif

#define _IOC(dir,type,nr,size) \
        (((dir)  << _IOC_DIRSHIFT) | \
         ((type) << _IOC_TYPESHIFT) | \
         ((nr)   << _IOC_NRSHIFT) | \
         ((size) << _IOC_SIZESHIFT))

#define _IOC_TYPECHECK(t) (sizeof(t))
#define _IOR(type,nr,size)      _IOC(_IOC_READ,(type),(nr),(_IOC_TYPECHECK(size)))
#define _IOW(type,nr,size)      _IOC(_IOC_WRITE,(type),(nr),(_IOC_TYPECHECK(size)))**
#define _IOWR(type,nr,size)     _IOC(_IOC_READ|_IOC_WRITE,(type),nr),_IOC_TYPECHECK(size)))

As you can see, _IOW which you use in you userspace ioctl_test.c and _IOWR which you use in your module ioctl function evaluate to different values for the same arguments. Hence, your switch case is failing. Try using _IOW inside your module too.

1
  • I did print cmd and that value is 0x804a030 and IOCTL_TEST = 0x0x40020006 and I also changed the _IOWR in the module to match the _IOW in the test code. So I'm not sure why cmd is not equal to IOCTL_TEST, and I've been trying to figure that out by using the ioctl.h file bit definitions, shifts of the arguments, etc. If I don't figure it out - will check back here to be more specific about my question! Thanks, Karen Sep 20, 2013 at 23:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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