Re: ioctl between user/kernel space

From: Richard B. Johnson (root@chaos.analogic.com)
Date: Thu Jul 11 2002 - 15:13:41 EST


On Thu, 11 Jul 2002, Shipman, Jeffrey E wrote:

> I'm not sure if this is the right place to ask this, but
> I have a question about ioctl(). I have a situation where
> I need to parse a file and build a hash table out of the
> information in user space. Then, I must pass that hash
> table into my module that's in kernel space. My question
> is: is ioctl() the way to go about this? I really don't
> know much about the function, but some people have mentioned
> it to me as the way to pass information between user and
> kernel space.
>
> If anyone has advice on if this is the way to go about it
> or how we could go about doing this would be greatly
> appreciated. Also, if anyone knows of any websites which
> may be helpful in this area, we'd appreciate that as
> well.
>
> Thanks.
>
> Jeff Shipman - CCD
> Sandia National Laboratories
> (505) 844-1158 / MS-1372

That's what ioctl() is/was designed for. In user-space, you have

            int ioctl(fd, FUNCTION_NR, parameter);

... where fd is your open file-handle, FUNCTION_NR is whatever you want
to define a specific control for your device, and parameter is usually
a pointer to some parameters (like a buffer).

In the module, you have.
  
 int any_name(struct inode *ip, struct file *fp, unsigned int command,
unsigned long arg);

    'ip' and 'fp' will probably be ignored in your module.
    'command' is your FUNCTION_NR, and 'arg' is your parameter.
    You cast 'arg' to a pointer of your choice if the user-mode
    code supplies a pointer.

    The address (pointer) of your function goes into the
   'struct file_operations' (7th member) that you pass a pointer
   to when you initialize your device (register_xxxdev()).

   The normal return code is 0. You return a -ERRNO if any errors
   are encountered in your function.

   Typically, you do:

   switch(command)
   {
   case FIRST_FUNCTION:
   ....
   break;
   default:
       return -ESPIPE; // Invalid stuff, lets you still test with
                       // standard text tools (od, hexdump, etc).
   }

Cheers,
Dick Johnson

Penguin : Linux version 2.4.18 on an i686 machine (797.90 BogoMips).

                 Windows-2000/Professional isn't.

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Mon Jul 15 2002 - 22:00:20 EST