Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decode arg pointer in ioctl() system call in Linux 2.6.29?

I want to print all the parameter values passed to linux system calls. In case of ioctl(), for example, I have following prototype and print statement.

asmlinkage long our_sys_ioctl(unsigned int fd ,  unsigned int cmd , unsigned long arg)
{
    printk ("fd=%u, cmd=%u and arg=%lu \n ", fd, cmd, arg);
    return original_call_ioctl(fd , cmd , arg);
}

I understand, fd is file descriptor of driver files, cmd defines driver , ioctl number, type of operation and size of parameter. But I am confused about arg parameter either it is a pointer to memory or just an immediate value what most of the documentations call it.

By using this arg parameter, how can I get the memory content if it is passed as unsigned long arg as given above, instead of a pointer?

like image 397
Junaid Avatar asked Jan 27 '26 04:01

Junaid


1 Answers

The arg parameter to the ioctl is opaque at the generic vfs level. How to interpret it is up to the driver or filesystem that actually handles it. So it may be a pointer to userspace memory, or it could be an index, a flag, whatever. It might even be unused and conventionally passed in a 0.

For example, look at the implementation of the TCSBRKP ioctl in drivers/tty/tty_io.c:

long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
//...
       case TCSBRKP:   /* support for POSIX tcsendbreak() */
            return send_break(tty, arg ? arg*100 : 250);

You can look at the ioctl_list(2) man page to see the parameters that various ioctls take; all of the entries on that list that have int or other non-pointer parameters are other examples.

So you could do something like

    void __user *argp = (void __user *) arg;

and then use copy_from_user() or get_user() to read the memory that arg points to, but that may fail if the parameter isn't a pointer. And at the generic ioctl syscall, you might not really want to have a huge table of every possible ioctl.

like image 146
Roland Avatar answered Jan 29 '26 20:01

Roland



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!