@Sweet,我開發了一個使用內核暴露列表的小內核模塊,如果您有任何疑問或反饋,請通過並回復給我。
void todo_add_entry(struct todo_struct *new)
{
#ifdef QUEUE
list_add_tail(&new->list, &first.list);
#else
list_add(&new->list, &first.list);
#endif
}
void todo_del_entry(struct todo_struct *new)
{
struct todo_struct *str, *temp;
list_for_each_entry_safe(str, temp, &first.list, list)
{
if((str->priority == new->priority) && (str->name == new->name))
{
list_del(&str->list);
#ifdef DEBUG
dmsg("Same:%d \t %d\n", str->priority, new->priority);
#endif
kfree(str);
}
}
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
static long
list_minor_ioctl(struct file *filp, U32 cmd, unsigned long arg)
#else
static int
list_minor_ioctl(struct inode *inode, struct file *filp, U32 cmd,
unsigned long arg)
#endif
{
S32 res = 0;
S32 err = 0;
struct todo_struct *new;
struct test *a;
if(_IOC_TYPE(cmd) != IOCTL_LIST_MAGIC)
return -ENOTTY;
if(_IOC_NR(cmd) > IOCTL_MAX_CMDS)
return -ENOTTY;
if(_IOC_DIR(cmd) & _IOC_READ)
err = !access_ok(VERIFY_WRITE, (void __user*) arg, _IOC_SIZE(cmd));
if(_IOC_DIR(cmd) & _IOC_WRITE)
err = !access_ok(VERIFY_READ, (void __user*) arg, _IOC_SIZE(cmd));
if(err)
return -EFAULT;
new = kmalloc(sizeof(*new), GFP_KERNEL);
a = (struct test *) arg;
switch(cmd)
{
case IOCTL_LIST_ADD:
new->priority = a->priority;
new->name = a->name;
INIT_LIST_HEAD(&new->list);
todo_add_entry(new);
break;
case IOCTL_LIST_DEL:
new->priority = a->priority;
new->name = a->name;
#ifdef DEBUG
dmsg("prio:%d \t name:%s \n", new->priority, new->name);
#endif
todo_del_entry(new);
break;
default: /* Default case --> Ignore, will never get executed */
break;
}
return res;
}
謝謝,但我沒有訪問上述鏈接。 – Sara
即使在博客中粘貼的來源看起來很奇怪。等一下我只會在這裏發佈它:-)。 –
如果使用list_add_tail它遵循QUEUE實現,但如果使用list_add,則遵循STACK實現。 –