error: invalid operands to binary == (have ‘uid_t’ {aka ‘unsigned int’} and ‘kuid_t’

在看《LINUX设备驱动程序》的时候书籍,实机跑scull示例

scull源码下载地址:

链接:https://pan.baidu.com/s/1pw4D5VBQqn3LMSbE2T9Z6w?pwd=1234 
提取码:1234

在编译scull驱动程序时,报如上错误,原因是scull最初适配的是2.6的内核版本,而我现在需要在5.15的内核上编译,内核代码已经发生很大的改变了。

报错代码如下:

static struct scull_dev scull_u_device;
static int scull_u_count;       /* initialized to 0 by default */
static uid_t scull_u_owner;     /* initialized to 0 by default */
static spinlock_t scull_u_lock; /* = SPIN_LOCK_UNLOCKED; */

static int scull_u_open(struct inode *inode, struct file *filp) {
   struct scull_dev *dev = &scull_u_device; /* device information */
 
   spin_lock(&scull_u_lock);
   if (scull_u_count && 
       (scull_u_owner != current->cred->uid) &&  /* allow user */
       (scull_u_owner != current->cred->euid) && /* allow whoever did su */
       !capable(CAP_DAC_OVERRIDE)) { /* still allow root */
      spin_unlock(&scull_u_lock);
      return -EBUSY;   /* -EPERM would confuse the user */
   }
 
   if (scull_u_count == 0)
      scull_u_owner = current->cred->uid; /* grab it */

   scull_u_count++;
   spin_unlock(&scull_u_lock);
 
   /* then, everything else is copied from the bare scull device */
   if ((filp->f_flags & O_ACCMODE) == O_WRONLY) scull_trim(dev);
   filp->private_data = dev;
   return 0;          /* success */
}

scull_u_owner的类型是uid_t ,而新的内核current->cred->uid的类型是kuid_t,所以才报上述错误。

current是一个宏,返回当前进程的上下文,返回类型为struct task_struct*, 结构体定义在<linux/sched.h>;

current->cred的类型为struct cred;结构体定义在<linux/cred.h>

current->cred->uid的类型是struct kgid_t;结构体定义在<linux/uidgid.h>

内容为:

typedef struct {
	uid_t val;
} kuid_t;

所以把current->cred->uid改为current->cred->uid.val就可以解决上述错误了

猜你喜欢

转载自blog.csdn.net/MashiMaroJ/article/details/127099278