README.md
1# eudev
2udev is a generic device manager running as a daemon on a Linux system and listening (through a netlink socket) for uevents the kernel sends out if a new device is initialized or a device is removed from the system. udev comes with a set of rules that match against exported values of the event and properties of the discovered device. A matching rule will name and create a device node and run configured programs to set up and configure the device.
3
4Each time a change happens within the device structure, the kernel emits a uevent, which is obtained by the device manager. The device manager then follows the declared rules. Based on the information contained in the uevent, it finds the rule or rules it needs to trigger and performs the required actions. These actions may involve the creation or deletion of device files, and may also trigger the loading of particular firmware files into kernel memory.
5
6## Directory Structure
7
8```
9README.md Introduction in English
10README_zh.md Introduction in Chinese
11COPYING Copyright file
12hwdb/ Hardware database file
13rules.d/ Rule file
14export_include/ Exported header file
15src/ C++ source code
16test/ Test file
17man/ Help file
18```
19
20## How OpenHarmony Integrates libudev
21### 1. Importing Header File
22```cpp
23#include "libudev.h"
24```
25### 2. Adding Reference to BUILD.gn
26```cpp
27deps += [ "//third_party/eudev:libudev" ]
28```
29### 3. Example of Invoking the udev Function
30```cpp
31#include <stdio.h>
32#include "libudev.h"
33
34void test_device(struct udev *udev, const char *syspath)
35{
36 struct udev_device *device;
37 printf("looking at device: %s\n", syspath);
38 device = udev_device_new_from_syspath(udev, syspath);
39 if (device == NULL) {
40 printf("no device found\n");
41 return;
42 }
43 printf("*** device: %p ***\n", device);
44 const char *str = udev_device_get_devnode(device);
45 if (str != NULL) {
46 printf("devname: '%s'\n", str);
47 }
48 dev_t devnum = udev_device_get_devnum(device);
49 if (major(devnum) > 0) {
50 printf("devnum: %u:%u\n", major(devnum), minor(devnum));
51 }
52 udev_device_unref(device);
53}
54
55int main(int argc, char *argv[])
56{
57 struct udev *udev = udev_new();
58 printf("udev context: %p\n", udev);
59 if (udev == NULL) {
60 printf("no context\n");
61 return 1;
62 }
63 const char *path = "/sys/devices/virtual/mem/null";
64 test_device(udev, path);
65 udev_unref(udev);
66 return 0;
67}
68```
69
70## udev Document
71
72Official document: https://github.com/eudev-project/eudev
73
74## Copyright
75
76See [COPYING](COPYING).
77
README.OpenSource
README_zh.md
1# eudev
2eudev 是一个通用的内核设备管理器。它运行在用户空间, 在Linux 系统上作为守护程序运行,并侦听(通过 netlink 套接字)在初始化新设备或从系统中删除设备时内核发出的内容。通过其提供的规则与事件的导出值和发现设备的属性匹配,命名并创建设备节点,并运行已配置的程序来设置和配置设备。
3
4每当设备结构发生变化时,内核都会发出一个由设备管理器获取的 uevent ,然后设备管理器查找声明的规则,根据uevent中包含的信息,它会找到触发和执行所需操作所需的规则。这些动作可能涉及设备文件的创建或删除,还可能触发将特定固件文件加载到内核内存中。
5
6## 目录结构
7
8```
9README.md 英文说明
10README_zh.md 中文说明
11COPYING 版权文件
12hwdb/ 硬件数据库文件
13rules.d/ 规则文件
14export_include/ 导出头文件
15src/ C++源码
16test/ 测试文件
17man/ 帮助文件
18```
19
20## OpenHarmony如何集成libudev
21### 1.头文件引入
22```cpp
23#include "libudev.h"
24```
25### 2.BUILD.gn添加引用
26```cpp
27deps += [ "//third_party/eudev:libudev" ]
28```
29### 3.调用udev函数过程举例
30```cpp
31#include <stdio.h>
32#include "libudev.h"
33
34void test_device(struct udev *udev, const char *syspath)
35{
36 struct udev_device *device;
37 printf("looking at device: %s\n", syspath);
38 device = udev_device_new_from_syspath(udev, syspath);
39 if (device == NULL) {
40 printf("no device found\n");
41 return;
42 }
43 printf("*** device: %p ***\n", device);
44 const char *str = udev_device_get_devnode(device);
45 if (str != NULL) {
46 printf("devname: '%s'\n", str);
47 }
48 dev_t devnum = udev_device_get_devnum(device);
49 if (major(devnum) > 0) {
50 printf("devnum: %u:%u\n", major(devnum), minor(devnum));
51 }
52 udev_device_unref(device);
53}
54
55int main(int argc, char *argv[])
56{
57 struct udev *udev = udev_new();
58 printf("udev context: %p\n", udev);
59 if (udev == NULL) {
60 printf("no context\n");
61 return 1;
62 }
63 const char *path = "/sys/devices/virtual/mem/null";
64 test_device(udev, path);
65 udev_unref(udev);
66 return 0;
67}
68```
69
70## udev使用文档
71
72官方文档 https://github.com/eudev-project/eudev
73
74## COPYING
75
76见 [COPYING](COPYING).
77