12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <errno.h>
- #include <asm/types.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <linux/netlink.h>
- #include <poll.h>
- #include "nl.h"
- int nl_init(void)
- {
- struct sockaddr_nl nl_addr;
- int nl;
- memset(&nl_addr, 0, sizeof(nl_addr));
- nl_addr.nl_family = AF_NETLINK;
- nl_addr.nl_pad = 0;
- nl_addr.nl_pid = 0; /* Let kernel to assign id */
- nl_addr.nl_groups = -1; /* Listen all multicast */
- if ((nl = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT)) < 0) {
- fprintf(stderr, "Error: Can't create socket\n");
- return -1;
- }
- if (bind(nl, (void *)&nl_addr, sizeof(nl_addr))) {
- fprintf(stderr, "Error: Can't bind NetLink\n");
- return -1;
- }
- return nl;
- }
- void nl_close(int nl)
- {
- if (nl >= 0)
- close(nl);
- }
- int nl_poll(int nl, int timeout)
- {
- struct pollfd pfd;
- char buf[10];
- int n;
- pfd.events = POLLIN;
- pfd.fd = nl;
- n = poll(&pfd, 1, (timeout * 1000));
- if (n < 0) {
- if (EINTR == errno)
- return -2;
- return -1;
- }
- /* Some device-related event */
- /* Read all messages. We don't need a message content. */
- if (n > 0)
- while (recv(nl, buf, sizeof(buf), MSG_DONTWAIT) > 0);
- return n;
- }
|