netlink_kobject_uevent
时间: 2023-04-29 17:06:23 浏览: 128
b'netlink_kobject_uevent' 是一个用于 Linux 内核中的 netlink 模块的函数。它允许用户从内核空间发送事件,该事件在用户空间通过 udev 规则进行处理。此函数允许内核在系统中检测并通知用户空间发生的设备变化。
相关问题
请将下面的程序改为非阻塞 接收 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <linux/netlink.h> #include <sys/socket.h> #define UEVENT_BUFFER_SIZE 2048 int main(int argc, char **argv) { int fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT); if (fd < 0) { perror("Failed to create socket"); return EXIT_FAILURE; } struct sockaddr_nl addr; memset(&addr, 0, sizeof(addr)); addr.nl_family = AF_NETLINK; addr.nl_groups = NETLINK_KOBJECT_UEVENT; if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { perror("Failed to bind socket"); close(fd); return EXIT_FAILURE; } char buf[UEVENT_BUFFER_SIZE]; while (1) { ssize_t n = recv(fd, buf, sizeof(buf), 0); if (n < 0) { perror("Failed to receive data"); close(fd); return EXIT_FAILURE; } char *p = buf; while (p < buf + n) { printf("%s\n", p); p += strlen(p) + 1; } } close(fd); return EXIT_SUCCESS; }
可以使用非阻塞方式来接收消息,使用fcntl函数设置socket为非阻塞模式。在接收数据之前,使用select函数来检查是否有数据可以读取,如果有则接收数据,否则继续等待。修改后的代码如下:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/netlink.h>
#include <sys/socket.h>
#include <sys/select.h>
#define UEVENT_BUFFER_SIZE 2048
int main(int argc, char **argv)
{
int fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
if (fd < 0) {
perror("Failed to create socket");
return EXIT_FAILURE;
}
struct sockaddr_nl addr;
memset(&addr, 0, sizeof(addr));
addr.nl_family = AF_NETLINK;
addr.nl_groups = NETLINK_KOBJECT_UEVENT;
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("Failed to bind socket");
close(fd);
return EXIT_FAILURE;
}
fcntl(fd, F_SETFL, O_NONBLOCK);
fd_set rfds;
char buf[UEVENT_BUFFER_SIZE];
while (1) {
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
int ret = select(fd + 1, &rfds, NULL, NULL, NULL);
if (ret == -1) {
perror("Failed to select socket");
close(fd);
return EXIT_FAILURE;
}
else if (ret) {
ssize_t n = recv(fd, buf, sizeof(buf), 0);
if (n < 0) {
perror("Failed to receive data");
close(fd);
return EXIT_FAILURE;
}
char *p = buf;
while (p < buf + n) {
printf("%s\n", p);
p += strlen(p) + 1;
}
}
}
close(fd);
return EXIT_SUCCESS;
}
```
uevent_get_property 使用的代码示例
以下是使用`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`属性。
阅读全文