歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Linux設備模型中的kobject使用

Linux設備模型中的kobject使用

日期:2017/3/1 10:09:20   编辑:Linux編程

要在目錄/sys/下創建一個目錄,就需要向內核注冊一個kobject結構。
該結構就是該目錄的一個載體。而該結構的屬性由struct kobj_type來保存。
注冊kojbect的方法為:

/*初始化 kobject結構*/
void kobject_init(struct kobject * kobj);

/*將kobject對象注冊到linux系統*/
Int kobject_add(struct kobject * kobj);
或者將上面兩步合為一步:
Int kobject_init_and_add(struct kobject * kobj, struct kobj_type * ktype
Struct kobject *parent, const char * fmt, ...);
目錄下的文件文件和其操作都在 struct kobj_type中定義:
struct kobj_type {
void (*release)(struct kobject *kobj);
struct sysfs_ops *sysfs_ops;/*文件操作*/
struct attribute **default_attrs;/*文件定義*/
};

文件的操作:
struct sysfs_ops {
ssize_t (*show)(struct kobject *kobj, struct attribute *attr,
char *buffer);
ssize_t (*store)(struct kobject *kobj, struct attribute *attr,
const char *buffer, size_t size);
};
Show: 當用戶讀屬性文件時,該函數被調用,該函數將屬性值存入buffer中返回給用戶態。
store: 當用戶寫屬性文件時,該函數被調用,用於存儲用戶傳入的屬性值。
文件的定義:
目錄下的文件由struct attribute **default_attrs; 數組來定義,該結構如下:
struct attribute {
char *name;/*屬性文件名*/
struct module *owner;
mode_t mode;/*屬性的保護位*/
};

Copyright © Linux教程網 All Rights Reserved