uevent_get_property 使用的代码示例
时间: 2024-06-08 13:09:34 浏览: 120
以下是使用`uevent_get_property`函数获取`uevent`事件属性的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <linux/netlink.h>
#define UEVENT_BUFFER_SIZE 2048
int main()
{
int sockfd;
struct sockaddr_nl sa;
int len;
char buffer[UEVENT_BUFFER_SIZE];
sockfd = socket(AF_NETLINK, SOCK_RAW, NETLINK_KOBJECT_UEVENT);
if (sockfd == -1) {
perror("socket");
return EXIT_FAILURE;
}
memset(&sa, 0, sizeof(sa));
sa.nl_family = AF_NETLINK;
sa.nl_groups = NETLINK_KOBJECT_UEVENT;
if (bind(sockfd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
perror("bind");
close(sockfd);
return EXIT_FAILURE;
}
while (1) {
len = recv(sockfd, buffer, sizeof(buffer), 0);
if (len == -1) {
perror("recv");
close(sockfd);
return EXIT_FAILURE;
}
if (len > 0) {
buffer[len] = '\0';
char *str = buffer;
while (*str) {
printf("%s\n", str);
str += strlen(str) + 1;
}
char *action = uevent_get_property(buffer, "ACTION");
if (action) {
printf("ACTION=%s\n", action);
}
char *subsystem = uevent_get_property(buffer, "SUBSYSTEM");
if (subsystem) {
printf("SUBSYSTEM=%s\n", subsystem);
}
}
}
close(sockfd);
return EXIT_SUCCESS;
}
```
此示例代码创建一个`NETLINK_KOBJECT_UEVENT`类型的套接字,监听系统中的`uevent`事件,并使用`uevent_get_property`函数获取事件属性。每当接收到一个新事件时,程序将打印所有事件信息以及`ACTION`和`SUBSYSTEM`属性。
阅读全文