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