Moose
Moose

Reputation: 755

Structs in Linux Kernel Space?

My question is about usage and behavior of structs in Linux Kernel Space. I am writing a char device driver:

struct LEDs{

    int red_l;
};

ssize_t gmem_driver_write(struct file *file, const char *buf,
       size_t count, loff_t *ppos)
{

    struct LEDs myled;
    printk("Red is: %d \n", myled.red_l);       
    return 0;
}

static long my_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
{

    struct LEDs myled = {};
    myled.red_l = 1;
    return 0;
}

If I call my_ioctl first and then write from user space, I am expecting red_l to have value 1 in struct LEDs and then I am expecting it to print inside write function. But it prints a garbage value.

My question: Since this logic works in user space, is there something different at play here ? What can be done to make it work in Linux Kernel Space ?

Upvotes: 1

Views: 1450

Answers (1)

That logic does work the same in userspace as in kernelspace; i.e., it doesn't work at all in either. Declaring two variables with the same name in different functions doesn't make them the same variable.

Upvotes: 4

Related Questions