[Nelug] ioctl() problem

peter.holmes at fopet-esl.com peter.holmes at fopet-esl.com
Sat Feb 22 15:30:02 UTC 2003

Something for the weekend, sir?
If you know kernel hacking well you'll see my mistake.
I obviously don't know it well.

An acknowledgement here to Chris Burton who started me down the right path..
It's been a painful, if profitable(?), experience.

I've written and tested a LKM which accesses the parallel port.
I know the code works by looking at KERN_INFO messages in
/var/log/syslog. i.e. received characters are printed there.
Problem is that I can't see this information in User space.

Sample code:

Note: my_ioctl() aleady loaded and visible in /dev/

main() {
  int fd, data, retval;
  ulong ularg;
  fd = open( "foo", O_RDWR);
  if( fd < 0)  .. error handling.
  retval = ioctl( fd, FOO_IO, ularg);
  printf("Data %d %lx\n", retval, ularg);
 ...... etc..

What I see on the terminal screen is:

Data 0 804977c 
Data 0 bffff928

The first is when 'ularg' is allocated before "int fd, data, retval;"
and the second when it's allocated afterwards.
To me this looks very like a pointer being written into 'ularg'.

The '0' (zero) after "Data" indicates that the call has succeeded.

Part of foo_ioctl() code:

int foo_ioctl( struct inode *inode,  
               struct file *filep, 
               unsigned int cmd, 
               unsigned long arg)
   int dat, rval;
  <... test here for correct command 'cmd' ...>
 spin_lock( &foo_lock);
 switch (cmd)
  case: FOO_IO:
     dat = _foo_pull( );
   spin_unlock( &foo_lock);
      rval = put_user( dat, (unsigned long *) arg);
#ifdef DEBUG
  printk( KERN_INFO "foo: read %d %x", rval, dat);
      return rval;
      .............  other case(s)
OK. The 'return()' isn't tidy but for debugging....
The printk() does what I expect and shows me correct data and zero return 
i.e. the _foo_pull() works OK and put_user() doesn't find an error in my 
passed 'ulong'.

So where is it going wrong??

Linux 2.4.18, gcc 3, MDK8.2

If anyone can see my mistake I'd be SEVERELY obliged.
Call me 'stupid' if it's obvious, Peter otherwise.

More information about the Nelug mailing list