Proc File system, Sysfs file system, Kobject operation

Source: Internet
Author: User

Proc File system is to provide an interface to the user, so that users can view the system running some state information, let the user modify some parameters of the kernel, for example, PRINTK print level can be Proc to modify

SYSFS File System,

Sysfs is a ram-based filesystem initially based on Ramfs. It provides a means to export kernel data structures, their attributes, and the linkages between them to userspace

---documentation/filesystems/sysfs.txt

The Linux2.6 kernel introduces the Sysfs file system. Sysfs is regarded as a filesystem similar to Proc. SYSFS the devices and buses that are connected to the system to organize component-level files so that they can be accessed from the user space.

SYSFS is loaded in the /sys/ directory, and its subdirectories include:

Block : each block device found in the system corresponds to a subdirectory in the directory. Each subdirectory contains properties files that describe the various aspects of the block device, such as device size. (Loop block device is modeled using a file)

Bus : Each bus registered in the kernel corresponds to a subdirectory in that directory, such as:

IDE PCI SCSI USB PCMCIA

There are two subdirectories in each of the bus directories:

Deviecs and drivers, the devices directory contains devices found in the entire system that belong to that bus type,

The drivers directory contains all the drivers registered to the bus.

class: classifies devices according to functionality, including all network interfaces in the/sys/class/net directory

Devices: contains all devices of the system

Kernel: configuration parameters in the kernel

Module : information for all modules in the system

Firmware: firmware in the system

Fs: Describes the file system in the system

Power : Supply options in the system

Kobject implements the basic object-oriented management mechanism and is the core structure of the linux2.6 device model. It is closely connected to the Sysfs file system, and each Kobject object registered in the kernel corresponds to a directory in the Sysfs file system.

Kobject_init (); --------Initializing the Kobject structure

Kobject_add ()----------registering the Kobject object with the Linux system

Kobject_init_and_add ()------Initialize the Kobject and register it with the Linux system

Kobject_del ()----Remove Kobject objects from a Linux system

Kobject_get ()----Adds a reference count of the Kobject object to 1, and returns the object pointer

Kobject_put ()----Subtract the reference count of the Kobject object by 1, and if the reference count will be 0, call the release method to release the Kobject object

The Ktype member of Kobject is a pointer to the KOBJ_TYPE structure,

Some properties of the Kobject object are recorded in this structure

struct kobj_type{

void (*release) (struct kobject *kobj);

struct Sysfs_ops *sysfs_ops;

struct attribute **default_attrs;

};

Release: Used to release kobject-occupied resources when the reference count of Kobject is 0 o'clock called.

struct Attribute {

char * name; /* Property File name */

struct module * owner;

mode_t mode; /* The protection bit of the property */

};

struct attribute (attribute): A file in the directory corresponding to the Kobject, and the name member is the filename.

struct Sysfs_ops {

ssize_t (*show) (struct kobject*, struct attribute *, char *);

ssize_t (*store) (struct kobject *, struct attribute *, const char*, size_t);

};

Show: When the user reads a property file, the function is called, and the function returns the property value to the user in buffer;

Store: When the user writes a property file, the function is called to store the user's incoming property value.

The code is as follows:

#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>voidObj_test_release (structKobject *kobject); ssize_t Kobj_test_show (structKobject *kobject,structAttribute *attr,Char*buf); ssize_t Kobj_test_store (structKobject *kobject,structAttribute *attr,Const Char*buf, size_t count);structAttribute test_attr ={. Name="Kobj_config",. Mode=S_irwxugo,};Static structAttribute *def_attrs[] = {    &test_attr, NULL,};structSysfs_ops Obj_test_sysops ={. Show=kobj_test_show,. Store=Kobj_test_store,};structKobj_type Ktype ={. Release=obj_test_release,. Sysfs_ops= &obj_test_sysops,. Default_attrs=Def_attrs,};voidObj_test_release (structKobject *kobject) {PRINTK ("eric_test:release. \ n");} ssize_t Kobj_test_show (structKobject *kobject,structAttribute *attr,Char*buf) {PRINTK ("Have show.\n"); PRINTK ("attrname:%s.\n", attr->name); sprintf (BUF,"hehe%s\n",attr->name); returnStrlen (Attr->name) +2+4;} ssize_t Kobj_test_store (structKobject *kobject,structAttribute *attr,Const Char*buf, size_t count) {PRINTK ("havestore\n"); PRINTK ("write:%s\n", BUF); returncount;}structKobject kobj;Static intKobj_test_init () {PRINTK ("kboject Test init.\n"); Kobject_init_and_add (&kobj,&ktype,null,"kobject_test"); return 0;}Static intKobj_test_exit (void) {PRINTK ("\033[1;33;40m kobject Test exit. \033[0m\r\n"); Kobject_del (&kobj); PRINTK (Kern_alert"Goodbye, Cruel world\n"); return 0;} Module_init (Kobj_test_init); Module_exit (Kobj_test_exit); Module_description ("Kmod-demo1 Driver"); Module_author ("ZHANGBH"); Module_license ("Dual BSD/GPL");

If reproduced please indicate the source

Sina Blog: http://blog.sina.com.cn/u/2049150530
Blog Park: http://www.cnblogs.com/sky-heaven/
Know: Http://www.zhihu.com/people/zhang-bing-hua

Proc File system, Sysfs file system, Kobject operation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.