1

Possible Duplicate:
Unsigned long with negative value

I have written one kernel module which interrupts any system call, prints its current user_id and input parameters passed to the system call function. Among them, one is sys_ioctl() as below:

asmlinkage long sys_ioctl(unsigned int fd, unsigned int cmd,unsigned long arg);

which means all input parameters are unsigned int numbers.

But when I print input parameters, I get the following output:

 fd=21, cmd=-1072143871 and arg=3202983648 
 fd=21, cmd=-1072143871 and arg=3202983648 
 fd=21, cmd=-1072143871 and arg=3202983648 
 ----------

Here is my function definition:

asmlinkage long our_sys_ioctl(unsigned int fd ,  unsigned int cmd , unsigned long arg)
{
    uid_t gtuid ;
    gtuid = getuid_call();
    printk ("our_sys_ioctl ---> uid = %d with fd=%i, cmd=%i and arg=%lu \n ", gtuid, fd, cmd, arg);
    return original_call_ioctl(fd , cmd , arg);
}

Any idea why cmd value is negative and what these values mean?

Community
  • 1
  • 1
Junaid
  • 1,626
  • 7
  • 30
  • 50

2 Answers2

5

When you use %i to print the cmd you are casting it to signed int as @Mario previously guessed. This is why it is negative.

You need to use %u to do the printing for it to remain an unsigned int

d or i - Signed decimal integer

u - Unsigned decimal integer

(from: http://www.cplusplus.com/reference/cstdio/printf/)

This will work as you would expect.

printk ("our_sys_ioctl ---> uid = %d with fd=%i, cmd=%u and arg=%lu \n ", gtuid, fd, cmd, arg);
                                                    ^^^^
Community
  • 1
  • 1
Karthik T
  • 30,638
  • 5
  • 66
  • 86
3

You print with %i which converts the argument to a signed int. Print with %u.

gustaf r
  • 1,174
  • 8
  • 14