/* * test kobject_uevent * bushi@mizi.com */ #include #include #include #include #include #include #include #include #ifndef NETLINK_KOBJECT_UEVENT #define NETLINK_KOBJECT_UEVENT (15) #endif int main(int argc, char **argv) { int ret = EXIT_FAILURE; int ns = -1; struct sockaddr_nl sa; char buf[4096] __attribute__((aligned (8))); /* for non-x86 */ struct iovec iov = { buf, sizeof(buf) }; struct msghdr msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 }; int len; memset (&sa, 0, sizeof(sa)); sa.nl_family = AF_NETLINK; sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR; ns = socket(AF_NETLINK, SOCK_RAW, NETLINK_KOBJECT_UEVENT); if (ns < 0) { perror("socket()"); goto sock_err; } if (bind(ns, (struct sockaddr*)&sa, sizeof(sa))) { perror("bind()"); goto bind_err; } while (1) { len = recvmsg (ns, &msg, 0); if (len < 0) { perror("recvmsg()"); break; } buf[4095] = 0; printf("%s\n", &buf[0]); fflush(stdout); #if 0 /* ... refer to 'man 7 netlink' */ struct nlmsghdr *h; for (h = (struct nlmsghdr*)buf; NLMSG_OK(h, len);) { printf("nlmsg_len: %d\n", h->nlmsg_len); h = NLMSG_NEXT(h, len); } #endif } bind_err: close(ns); sock_err: return ret; }