#include <linux/netlink.h>
#ifndef NETLINK_KOBJECT_UEVENT
#define NETLINK_KOBJECT_UEVENT 15
#endif
int main(int argc, char *argv[])
{
int sock;
struct sockaddr_nl snl;
int retval;
if (getuid() != 0) {
fprintf(stderr, "need to be root, exit\n");
exit(1);
}
memset(&snl, 0x00, sizeof(struct sockaddr_nl));
snl.nl_family = AF_NETLINK;
snl.nl_pid = getpid();
snl.nl_groups = 0xffff;
sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
if (sock == -1) {
fprintf(stderr, "error getting socket, exit\n");
exit(1);
}
retval = bind(sock, (struct sockaddr *)&snl,
sizeof(struct sockaddr_nl));
if (retval < 0) {
fprintf(stderr, "bind failed, exit\n");
goto exit;
}
while(1) {
char buf[1024];
char object[MAX_OBJECT];
char *signal;
char *pos;
int len;
len = recv(sock, &buf, sizeof(buf), 0);
if (len < 0) {
fprintf(stderr, "error receiving message\n");
continue;
}
buf[len] = '\0';
/* sending object */
pos = strchar(buf, '@');
pos[0] = '\0';
signal = buf;
object = &pos[1];
...
}
...
exit:
close(sock);
exit(1);
}