ioctl, copy structure from user

John Baldwin jhb at freebsd.org
Tue Feb 7 19:03:47 UTC 2012


On Monday, February 06, 2012 2:44:23 pm PRATIK MOHANTY wrote:
> Hello sir,
>  I need some example for ioctl to copy structure from user space to kernel
> space

In BSD the kernel copies the immediate ioctl argument into and out of userland
for you.  Thus, you can do something like this:

struct foo {
    ...
};

#define MY_IOCTL	_IOWR('M', 1, struct foo)

And in your kernel code:

int
foo_ioctl(..., u_long cmd, caddr_t data)
{
	struct foo *f;

	switch (cmd) {
	case MY_IOCTL:
		f = (struct foo *)data;
		/* 
         * 'f' is now a pointer to an in-kernel copy of
         * the structure.  Any changes made to it will
         * be copied back out to userland after your
         * routine returns.
         */
        break;
    }
}

-- 
John Baldwin


More information about the freebsd-hackers mailing list