/
kobj.c
executable file
·174 lines (115 loc) · 3.16 KB
/
kobj.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/kobject.h>
volatile int aa = 0;
struct my_kobj {
int val;
struct kobject kobj;
};
struct my_kobj *obj1, *obj2;
struct my_kobj obj3;
//memset(&obj3, 0, sizeof(stuct my_kobj));
//struct bin_attribute bin_attri;
//memset(&bin_attri, 0, sizeof(struct bin_attribute);
static int bin_show(struct file *filp, struct kobject *kobj,struct bin_attribute *bin_attr, char *buf, loff_t offset, size_t count)
{
printk("Line :%d, Fun:%s\n",__LINE__,__FUNCTION__);
return 0;
}
static int bin_store(struct file *filp, struct kobject *kobj, struct bin_attribute *bin_attr, char *buf, loff_t offset, size_t count)
{
printk("Line:%d, Fun:%s\n",__LINE__,__FUNCTION__);
return 0;
}
static struct bin_attribute bin_attri = {
.attr = {
.name = "bin_attr",
.mode = S_IRUSR,
},
.size = 0,
.read = bin_show,
.write = bin_store
};
struct kobj_type my_type;
struct attribute name_attr = {
.name = "name",
.mode = 0444,
};
struct attribute val_attr = {
.name = "val",
.mode = 0666,
};
struct attribute length_attr = {
.name = "length",
.mode = 0666,
};
struct attribute *my_attrs[] = {
&name_attr,
&val_attr,
NULL,
};
ssize_t my_show(struct kobject *kobj, struct attribute *attr, char *buffer)
{
struct my_kobj *obj = container_of(kobj, struct my_kobj, kobj);
ssize_t count = 0;
printk("aa = %d\n",aa);
aa++;
if(strcmp(attr->name, "name") == 0) {
count = sprintf(buffer, "%s\n",kobject_name(kobj));
}else if(strcmp(attr->name, "val") == 0) {
count = sprintf(buffer, "%d\n",obj->val);
}
return count;
}
ssize_t my_store(struct kobject *kobj, struct attribute *attr, const char* buffer, size_t size)
{
struct my_kobj *obj = container_of(kobj, struct my_kobj, kobj);
if(strcmp(attr->name, "val") == 0) {
sscanf(buffer, "%d", &obj->val);
}
return size;
}
struct sysfs_ops my_sysfsops = {
.show = my_show,
.store = my_store,
};
void obj_release(struct kobject *kobj)
{
struct my_kobj *obj = container_of(kobj, struct my_kobj, kobj);
printk("obj release %s\n",kobject_name(&obj->kobj));
kfree(obj);
}
static int __init mykobj_init(void)
{
printk("kboj init\n");
int ret = 0;
obj1 = kzalloc(sizeof(struct my_kobj),GFP_KERNEL);
obj2 = kzalloc(sizeof(struct my_kobj), GFP_KERNEL);
obj2->val = 2;
my_type.release = obj_release;
my_type.default_attrs = my_attrs;
my_type.sysfs_ops = &my_sysfsops;
kobject_init_and_add(&obj1->kobj, &my_type, NULL, "mykobj1");
kobject_init_and_add(&obj2->kobj, &my_type, &obj1->kobj, "mykobj2");
kobject_init_and_add(&obj3.kobj, &my_type, &obj2->kobj, "mykobj3");
ret = sysfs_create_file(&obj1->kobj, &length_attr);
ret = sysfs_create_bin_file(&obj1->kobj, &bin_attri);
ret = sysfs_create_link(&obj1->kobj, &obj3.kobj,"link_file");
return ret;
}
static void __exit mykobj_exit(void)
{
printk("my kobj exit\n");
int ret;
sysfs_remove_bin_file(&(obj1->kobj), &bin_attri);
sysfs_remove_link(&(obj1->kobj),kobject_name(&obj3.kobj));
kobject_del(&obj2->kobj);
kobject_put(&obj2->kobj);
kobject_del(&obj1->kobj);
kobject_put(&obj1->kobj);
return;
}
module_init(mykobj_init);
module_exit(mykobj_exit);
MODULE_LICENSE("GPL");