Uevent是一种在内核空间和用户空间之间通信的机制,主要用于热插拔事件(hotplug)。
编辑获取热插拔事件的源文件get_uevent.c
#define _GNU_SOURCE
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#include <sys/socket.h>
#include <sys/user.h>
#include <sys/un.h>
#include <linux/types.h>
#include <linux/netlink.h>
#define HOTPLUG_BUFFER_SIZE 1024
#define HOTPLUG_NUM_ENVP 32
#define OBJECT_SIZE 512
struct uevent {
void *next;
char buffer[HOTPLUG_BUFFER_SIZE + OBJECT_SIZE];
char *devpath;
char *action;
char *envp[HOTPLUG_NUM_ENVP];
};
static struct uevent * alloc_uevent (void)
{
return (struct uevent *)malloc(sizeof(struct uevent));
}
int main(int argc, char *argv[])
{
int sock;
struct sockaddr_nl snl;
struct sockaddr_un sun;
socklen_t addrlen;
int retval;
int rcvbufsz = 128*1024;
int rcvsz = 0;
int rcvszsz = sizeof(rcvsz);
unsigned int *prcvszsz = (unsigned int *)&rcvszsz;
pthread_attr_t attr;
const int feature_on = 1;
memset(&snl, 0x00, sizeof(struct sockaddr_nl));
snl.nl_family = AF_NETLINK;
snl.nl_pid = get