0
votes

I am getting the following error on my kernel code in linux for file operations.

Could you help me identify what's going wrong here?

Code:

struct GraphData{
 unsigned long addr;
 long time;
};
static int position = 0;
struct GraphData buffer[BUFFER_SIZE]; 


static int dev_open(struct inode *in,struct file *f);
static ssize_t dev_read(struct file *f,struct GraphData *out,size_t count);
static int dev_release(struct inode *inod,struct file *f);

static struct file_operations fops =
{
  .read = dev_read,
  .open = dev_open,
  .release = dev_release
};

static int dev_open(struct inode *in, struct file *f){
 printk(KERN_INFO " Device has been opened \n");
 return 0;
}

static int dev_release(struct inode *in, struct file *f){
  printk(KERN_INFO "Device successfully closed\n");
  return 0;
}
static ssize_t dev_read(struct file *f,struct GraphData *out, size_t count)
{
  int ret=0,i=0;

  for(i=0;i<BUFFER_SIZE;i++)
  {
    count = copy_to_user(&out[i],&buffer[i],sizeof(buffer[i]));


    if(count<0)
    {
        printk(KERN_ALERT "Copy!");
        ret = -1;
    }
  }
  return count;
}

Kernel program has file operations, device could be passed a buffer and it stores data into the buffer

User Program

#define SIZE 500
struct Buffer
{
  unsigned long addr;
  long time;
};
int main()
{
 struct Buffer buf[SIZE];
 int i=0;
 int errorFlag = 0;
 int fp = open("/proc/Probe",O_RDONLY);
 errorFlag = read(fp,buf,1);
    printf("Page Fault Address           Time\n");
 for(i=0;i<SIZE;i++)
 {
    //printf("Read worked the Page fault address is 0x%lx caught at time  %ld\n",buf[i].address,buf[i].time);
    printf("0x%lx           %ld\n",buf[i].addr,buf[i].time);
}
close(fp);
return 0;
}

The user program opens the file descriptor, reads from device and passes the buffer for kernel devce to copy into.

Error: error: initialization from incompatible pointer type [-Werror=incompatible-pointer-types] .read = dev_read, /home/csvb/OS/assign4/hello2.c:36:12: note: (near initialization for ‘fops.read’) cc1: some warnings being treated as errors

1

1 Answers

0
votes

Could you help me identify what's going wrong here? [...] Error: error: initialization from incompatible pointer type [-Werror=incompatible-pointer-types] .read = dev_read, /home/csvb/OS/assign4/hello2.c:36:12: note: (near initialization for ‘fops.read’) cc1: some warnings being treated as errors

The diagnostic seems pretty clear to me. It localizes the problem to line 36 of file /home/csvb/OS/assign4/hello2.c, in the initialization of member read of the object identified by fops; specifically, the designated initialization .read = dev_read. "Initialization from incompatible pointer type" means exactly what it says: the type of dev_read is incompatible with the type of fops.read. And such an issue would merit a warning by default, but compilation options direct that it cause an error instead.

Generally speaking, you should check the docs, or at least the headers, for the version of the kernel you're targeting to determine what is expected, but even without doing so it would be safe to assume that none of the parameters are expected to have type struct GraphData *. That's sure to be one incompatibility, then.

This particular detail of the kernel's device driver interface is the same in the latest prereleases of kernel 5.7 as it was at least as far back back as kernel 2.4, however, so even without knowing which kernel version you're targeting, I am confident in saying that the read function of a character device is expected to have this signature:

ssize_t dev_read_fn(struct file *, char __user *, size_t, loff_t *);

In addition differing from your function in the type of the second parameter, it takes four parameters, not three.