xiaohuan4518 发表于 2013-06-14 16:30

linux rmmod失败,代码附上,求大神指点~~~

          最近在学习sysfs文件系统,参考网上的例子,编写了一个应用层和内核交互的简单驱动程序,程序在卸载模块部分有问题,rmmod后由module_exit函数注册的处理函数不被调用,出错显示为ERROR: Removing 'kobject': Device or resource busy。
          注明:rmmod前我已经查看过模块的使用情况: lsmod结果显示为 kobject1332 0permanent.我的内核版本为3.5.0-17-generic,发行版本是UBUNTU
         /******************************************/

#include <linux/device.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/sysfs.h>
#include <linux/stat.h>
//set limit to the parament
#include<asm/uaccess.h>
char *on="mem";
char *off="disk";
char *p=NULL;
//set end
ssize_t test_show(struct kobject *kobj,struct attribute *attr,char *buf)
{
        printk("kernel part:show\n");
        printk("attribute name:%s",attr->name);
        sprintf(buf,"current status is %s\n",p);
        return 40;


}
ssize_t test_store(struct kobject *kobj,struct attribute *attr,const char *buf,size_t count)
{
         printk("kernel part:store\n");
        char c;
        int i=0;
        for(;i<strlen(buf)-1;i++)
        {
          c=buf;
        }
        c='\0';
       if(strcmp(c,on)==0)
        {
                printk("write effctive:%s",buf);
                p=on;
                return count;
        }
        else if(strcmp(c,off)==0)
        {
       printk("write effctive:%s",buf);
                p=off;
                return count;

        }
        else
        {
                printk("valid data%s,%d\n",c,strlen(c));
        //        return ERROR;
        }
       



}
void release(struct kobject *kobj)
{
        printk("relase part \n");

}



struct kobject kobj;
struct sysfs_ops op=
{
        .show=test_show,
        .store=test_store,

};
struct attribute attr=
{
        .name="fuck_meng",
        .mode=0777,
//        .owner=THIS_MODULE,
};
static struct attribute *def[]=
{
        &attr,NULL,

};
struct kobj_type ktype=
{
        .release=release,
        .sysfs_ops=&op,
        .default_attrs=def,
};//this is the core structure

static void __init test_init()
{
        p=on;
        kobject_init_and_add(&kobj,&ktype,NULL,"test");
        printk("this is a test\n");

}
static void __exit test_exit()
{
        printk("waring:test exit\n");

        kobject_del(&kobj);
//        printk("waring:test exit\n");
}

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("TEST");
module_init(test_init);
module_exit(test_exit);

wwxxxxll 发表于 2013-06-21 10:18

你的程序编译好多警告,最严重的是test_init应该是int返回。
我在arm920t平台linux-3.2.0内核上运行结果是
# insmod kobj.ko
this is a test
# rmmod kobj   
waring:test exit
我对kobj不太了解,不知结果是否是你想要的
我改后的代码如下(编译没有警),
#include <linux/device.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/sysfs.h>
#include <linux/stat.h>
//set limit to the parament
#include<asm/uaccess.h>
char *on="mem";
char *off="disk";
char *p=NULL;
//set end
ssize_t test_show(struct kobject *kobj,struct attribute *attr,char *buf)
{
      printk("kernel part:show\n");
      printk("attribute name:%s",attr->name);
      sprintf(buf,"current status is %s\n",p);
      return 40;


}
ssize_t test_store(struct kobject *kobj,struct attribute *attr,const char *buf,size_t count)
{
      char c;
      int i=0;

         printk("kernel part:store\n");
      for(;i<strlen(buf)-1;i++)
      {
            c=buf;
      }
      c='\0';
         if(strcmp(c,on)==0)
      {
                printk("write effctive:%s",buf);
                p=on;
                return count;
      }
      else if(strcmp(c,off)==0)
      {
         printk("write effctive:%s",buf);
                p=off;
                return count;

      }
      else
      {
                printk("valid data%s,%d\n",c,strlen(c));
      //      return ERROR;
      }



      return 0;
}
void release(struct kobject *kobj)
{
      printk("relase part \n");

}



struct kobject kobj;
struct sysfs_ops op=
{
      .show=test_show,
      .store=test_store,

};
struct attribute attr=
{
      .name="fuck_meng",
      .mode=0777,
//      .owner=THIS_MODULE,
};
static struct attribute *def[]=
{
      &attr,NULL,

};
struct kobj_type ktype=
{
      .release=release,
      .sysfs_ops=&op,
      .default_attrs=def,
};//this is the core structure

static int __init test_init(void)
{
      p=on;
      kobject_init_and_add(&kobj,&ktype,NULL,"test");
      printk("this is a test\n");
      return 0;
}
static void __exit test_exit(void)
{
      printk("waring:test exit\n");

      kobject_del(&kobj);
//      printk("waring:test exit\n");
}

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("TEST");
module_init(test_init);
module_exit(test_exit);
                                                                                                                                                                                                                                                                                                            

xiaohuan4518 发表于 2013-06-21 10:58

首先特别感谢2楼:em02:,编译问题后来改过了,还是影响不大,昨天问了师兄,然后综合讨论了下,在UBUNTU 下要下载内核源码了搭建驱动开发环境才行,但是本人太菜了,编译内核(一个未定义的引用,而且找不到相应的内核配置选项)都出错了,所以方案是否可行还说不准,所以没贴出来!!!!!这个驱动挂载以后会在/sys/目录下生成一个test的新目录,对里面的(属性)文件write or read会调用STORE,SHOW函数。再次感谢二楼,谢谢谢谢!

wwxxxxll 发表于 2013-06-21 11:07

# pwd
/sys/test
# ls
fuck_meng
# echo aa>fuck_meng
kernel part:store
valid dataaa,2
kernel part:store
valid dataaa,2
kernel part:store
valid dataaa,2
kernel part:store
valid dataaa,2
kernel part:store
valid dataaa,2
kernel part:store
valid dataaa,2
kernel part:store

刚运行的结果,是这样。我的这个arm平台的内核是我自己编译的,如有内核编译的问题,可以一起讨论一下!:))
页: [1]
查看完整版本: linux rmmod失败,代码附上,求大神指点~~~