在/sys目录下建立一个文件,并且实现信息的读取和存储
2017-07-14 11:19
471 查看
此文件是在/sys/kernel/目录里面建立三个文件foo、baz、bar,每个文件都是静态的int型的缓存空间。
可以在这三个文件中读写一些整形的数据。
/*
* * Sample kobject implementation
* *
* * Copyright (C) 2004-2007 Greg Kroah-Hartman <greg@kroah.com>
* * Copyright (C) 2007 Novell Inc.
* *
* * Released under the GPL version 2 only.
* *
* */
#include <linux/kobject.h>
#include <linux/string.h>
#include <linux/sysfs.h>
#include <linux/module.h>
#include <linux/init.h>
/*
* * This module shows how to create a simple subdirectory in sysfs called
* * /sys/kernel/kobject-example In that directory, 3 files are created:
* * "foo", "baz", and "bar". If an integer is written to these files, it can be
* * later read out of it.
* */
static int foo;
static int baz;
static int bar;
/*
* * The "foo" file where a static variable is read from and written to.
* */
static ssize_t foo_show(struct kobject *kobj, struct kobj_attribute *attr,char *buf)
{
return sprintf(buf, "%d\n", foo);
}
static ssize_t foo_store(struct kobject *kobj, struct kobj_attribute *attr,const char *buf, size_t count)
{
sscanf(buf, "%du", &foo);
return count;
}
static struct kobj_attribute foo_attribute = __ATTR(foo, 0666, foo_show, foo_store);
/*
* * More complex function where we determine which variable is being accessed by
* * looking at the attribute for the "baz" and "bar" files.
* */
static ssize_t b_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
int var;
if (strcmp(attr->attr.name, "baz") == 0)
var = baz;
else
var = bar;
return sprintf(buf, "%d\n", var);
}
static ssize_t b_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count)
{
int var;
sscanf(buf, "%du", &var);
if (strcmp(attr->attr.name, "baz") == 0)
baz = var;
else
bar = var;
return count;
}
static struct kobj_attribute baz_attribute = __ATTR(baz, 0666, b_show, b_store);
static struct kobj_attribute bar_attribute = __ATTR(bar, 0666, b_show, b_store);
/*
* * Create a group of attributes so that we can create and destroy them all
* * at once.
* */
static struct attribute *attrs[] =
{
&foo_attribute.attr,
&baz_attribute.attr,
&bar_attribute.attr,
NULL, /* need to NULL terminate the list of attributes */
};
/*
* * An unnamed attribute group will put all of the attributes directly in
* * the kobject directory. If we specify a name, a subdirectory will be
* * created for the attributes with the directory being the name of the
* * attribute group.
* */
static struct attribute_group attr_group =
{
.attrs = attrs,
};
static struct kobject *example_kobj;
static int __init example_init(void)
{
int retval;
/*
* Create a simple kobject with the name of "kobject_example",
* located under /sys/kernel/
*
* As this is a simple directory, no uevent will be sent to
* userspace. That is why this function should not be used for
* any type of dynamic kobjects, where the name and number are
* not known ahead of time.
*/
example_kobj = kobject_create_and_add("kobject_example", kernel_kobj);
if (!example_kobj)
return -ENOMEM;
/* Create the files associated with this kobject */
retval = sysfs_create_group(example_kobj, &attr_group);
if (retval)
kobject_put(example_kobj);
return retval;
}
static void __exit example_exit(void)
{
kobject_put(example_kobj);
}
module_init(example_init);
module_exit(example_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>");
Makefile:
obj-m = systest.o
K_DIR = $(shell uname -r)
PWD = $(shell pwd)
all:
make -C /lib/modules/$(K_DIR)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(K_DIR)/build M=$(PWD) clean
测试:
root@qinrenzhi-G31M-ES2C:/home/qinrenzhi/private/driver/sys# insmod systest.ko
root@qinrenzhi-G31M-ES2C:/home/qinrenzhi/private/driver/sys# cd /sys/kernel/kobject_example/
root@qinrenzhi-G31M-ES2C:/sys/kernel/kobject_example# ls
bar baz foo
root@qinrenzhi-G31M-ES2C:/sys/kernel/kobject_example# cat bar
1234
root@qinrenzhi-G31M-ES2C:/sys/kernel/kobject_example# echo 8888 > bar
root@qinrenzhi-G31M-ES2C:/sys/kernel/kobject_example# cat bar
8888
可以在这三个文件中读写一些整形的数据。
/*
* * Sample kobject implementation
* *
* * Copyright (C) 2004-2007 Greg Kroah-Hartman <greg@kroah.com>
* * Copyright (C) 2007 Novell Inc.
* *
* * Released under the GPL version 2 only.
* *
* */
#include <linux/kobject.h>
#include <linux/string.h>
#include <linux/sysfs.h>
#include <linux/module.h>
#include <linux/init.h>
/*
* * This module shows how to create a simple subdirectory in sysfs called
* * /sys/kernel/kobject-example In that directory, 3 files are created:
* * "foo", "baz", and "bar". If an integer is written to these files, it can be
* * later read out of it.
* */
static int foo;
static int baz;
static int bar;
/*
* * The "foo" file where a static variable is read from and written to.
* */
static ssize_t foo_show(struct kobject *kobj, struct kobj_attribute *attr,char *buf)
{
return sprintf(buf, "%d\n", foo);
}
static ssize_t foo_store(struct kobject *kobj, struct kobj_attribute *attr,const char *buf, size_t count)
{
sscanf(buf, "%du", &foo);
return count;
}
static struct kobj_attribute foo_attribute = __ATTR(foo, 0666, foo_show, foo_store);
/*
* * More complex function where we determine which variable is being accessed by
* * looking at the attribute for the "baz" and "bar" files.
* */
static ssize_t b_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
int var;
if (strcmp(attr->attr.name, "baz") == 0)
var = baz;
else
var = bar;
return sprintf(buf, "%d\n", var);
}
static ssize_t b_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count)
{
int var;
sscanf(buf, "%du", &var);
if (strcmp(attr->attr.name, "baz") == 0)
baz = var;
else
bar = var;
return count;
}
static struct kobj_attribute baz_attribute = __ATTR(baz, 0666, b_show, b_store);
static struct kobj_attribute bar_attribute = __ATTR(bar, 0666, b_show, b_store);
/*
* * Create a group of attributes so that we can create and destroy them all
* * at once.
* */
static struct attribute *attrs[] =
{
&foo_attribute.attr,
&baz_attribute.attr,
&bar_attribute.attr,
NULL, /* need to NULL terminate the list of attributes */
};
/*
* * An unnamed attribute group will put all of the attributes directly in
* * the kobject directory. If we specify a name, a subdirectory will be
* * created for the attributes with the directory being the name of the
* * attribute group.
* */
static struct attribute_group attr_group =
{
.attrs = attrs,
};
static struct kobject *example_kobj;
static int __init example_init(void)
{
int retval;
/*
* Create a simple kobject with the name of "kobject_example",
* located under /sys/kernel/
*
* As this is a simple directory, no uevent will be sent to
* userspace. That is why this function should not be used for
* any type of dynamic kobjects, where the name and number are
* not known ahead of time.
*/
example_kobj = kobject_create_and_add("kobject_example", kernel_kobj);
if (!example_kobj)
return -ENOMEM;
/* Create the files associated with this kobject */
retval = sysfs_create_group(example_kobj, &attr_group);
if (retval)
kobject_put(example_kobj);
return retval;
}
static void __exit example_exit(void)
{
kobject_put(example_kobj);
}
module_init(example_init);
module_exit(example_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>");
Makefile:
obj-m = systest.o
K_DIR = $(shell uname -r)
PWD = $(shell pwd)
all:
make -C /lib/modules/$(K_DIR)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(K_DIR)/build M=$(PWD) clean
测试:
root@qinrenzhi-G31M-ES2C:/home/qinrenzhi/private/driver/sys# insmod systest.ko
root@qinrenzhi-G31M-ES2C:/home/qinrenzhi/private/driver/sys# cd /sys/kernel/kobject_example/
root@qinrenzhi-G31M-ES2C:/sys/kernel/kobject_example# ls
bar baz foo
root@qinrenzhi-G31M-ES2C:/sys/kernel/kobject_example# cat bar
1234
root@qinrenzhi-G31M-ES2C:/sys/kernel/kobject_example# echo 8888 > bar
root@qinrenzhi-G31M-ES2C:/sys/kernel/kobject_example# cat bar
8888
相关文章推荐
- 在/sys目录下建立一个文件,并且实现信息的读取和存储
- 在/sys目录下建立一个文件,并且实现信息的读取和存储
- 创建一个目录info,并在目录中创建一个文件test.txt,把该文件的信息读取出来,并显示出来
- php读取目录所有文件信息实现代码
- 设计一个学生类,其中包含学号、姓名、成绩等数据成员,创建学生对象并且倒入到文件file.txt,然后由文件读取到另一个学生对象并输出,试编程实现。
- 实现读取文件夹中文件,复制到另外一个文件夹,并且删除原来的文件包括(图片和PDF)
- python实现读取给定文件夹下文件目录与文件信息(包括子文件夹)
- python读取一个目录下的文件,并且写入.txt文件中
- 将后台数据读取到前台的EXCEL文件中去,用javascript实现.(发一个原创)
- 读取目录下的文件得到一个数组
- 本文给出了一种方便实用的解决大文件的读取、存储等处理的方法,并结合相关程序代码对具体的实现过程进行了介绍
- 我的图象是JPEG文件,是放在远程一个固定IP电脑下面的一个目录,我如何读取这个JPEG文件呢?
- 用jsp实现一个简单的购物车web应用系统。实现的添加购物商品,删除购物商品并且显示购物车信息。
- 一个专门用于文件下载的类。实现文件下载时流量等相关信息的处理。
- Linux下c语言实现将一个目录下的所有文件和目录复制到另一个目录下
- C#读取一个目录,然后点击其中一个节点动态加载他的子目录和文件,节省开销
- 分享下java 读取目录及子目录下指定文件名的路径 并放到一个List数组里面返回遍历和写到文件里
- 一个利用客户端空间存储树形导航栏信息,从而实现ajax技术动态导航的雏形
- FSO读取一个文件相关信息的范例
- 以上的文章是我解决这样一个问题时候的大致的思路: 把Matlab读取二进制文件的函数变成VBA读取二进制文件,实现同样的功能