我正在尝试为包含整数“timer_interval”(全局变量)的 sysfs 文件创建一个存储函数。
static ssize_t sys_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count)
{
printk(KERN_ALERT "BUF IS %s and count is %d", buf, count);
int ret;
char *pTemp = kmalloc(100, GFP_KERNEL);
if (pTemp == NULL)
{
printk(KERN_ALERT "malloc WRONG");
return -EFAULT;
}
printk(KERN_ALERT "AFTER MALLOC");
if (ret = copy_from_user(pTemp ,buf, count))
{
printk(KERN_ALERT "COPY WRONG %d", ret);
kfree(pTemp);
printk(KERN_ALERT "%d" , pTemp);
return -EFAULT;
}
printk(KERN_ALERT "AFTER COPY");
if (sscanf(pTemp, "%d", &Timer_interval) < count);
{
printk(KERN_ALERT "SCANF WRONG");
kfree(pTemp);
return -EFAULT;
}
printk(KERN_ALERT "AFTER SCANF COUNT = %d", Timer_interval);
kfree(pTemp);
return count;
}
问题在于复制给用户。dmesg 显示分配正常,buf 包含正确的数据,但 copy_from_user 返回缓冲区的大小,这意味着没有复制任何内容。
我在这里做错了什么?