title: ioctl kernel call failing - ioctl module is in kernel, Ubuntu Linux VM in Oracle's Virtual Box
body: <p>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</p>
  
  <p>user level code error message:</p>
  
  <p>saasbook@saasbook:~/cs552/osPrimer/invokeKernelServices/user_level_code$ ./ioctl_test</p>
  
  <p>size of testStruct.field1 = 1</p>
  
  <p>size of testStruct.field2 = 1</p>
  
  <p>first ioctl: Invalid argument</p>
  
  <p>code listing for ioctl_test.c:</p>
  
  <p><a href="https://gist.github.com/KarenWest/6629582" rel="nofollow">https://gist.github.com/KarenWest/6629582</a></p>
  
  <p>can see ioctl_module is in kernel:</p>
  
  <p>/invokeKernelServices$ dmesg</p>
  
  <p>[13563.849593]  Loading module</p>
  
  <p>[13578.846063] pseudo_device_ioctl</p>
  
  <p>code_listing for ioctl_module.c:</p>
  
  <p><a href="https://gist.github.com/KarenWest/6629557" rel="nofollow">https://gist.github.com/KarenWest/6629557</a></p>
  
  <hr>
  
  <p>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: </p>
  
  <pre><code>static int pseudo_device_ioctl(struct inode *inode, struct file *file,
              unsigned int cmd, unsigned long arg)
  </code></pre>
  
  <p>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.</p>
  
  <pre><code>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-&gt;proc_fops = &amp;pseudo_dev_proc_operations;
  </code></pre>
  
  <p>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.</p>
  
  <p>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</p>
  
  <hr>
  
  <p>This might help - just read this:</p>
  
  <p>"The Linux Kernel Module Programming Guide: </p>
  
  <p>Chapter 7. Talking To Device Files </p>
  
  <p>7.1. Talking to Device Files (writes and IOCTLs)"</p>