Advertisement
Guest User

Ankur Patel

a guest
Jun 9th, 2014
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.11 KB | None | 0 0
  1.  
  2.   #include <stdio.h>
  3.   #include <stdlib.h>
  4.   #include <string.h>
  5.  
  6.   #include <sys/poll.h>
  7.   #include <sys/socket.h>
  8.   #include <sys/types.h>
  9.   #include <unistd.h>
  10.  
  11.   #include <linux/types.h>
  12.   #include <linux/netlink.h>
  13.  
  14.   void die(char *s)
  15.   {
  16.     write(2,s,strlen(s));
  17.     exit(1);
  18.   }
  19.  
  20.   int main(int argc, char *argv[])
  21.   {
  22.     struct sockaddr_nl nls;
  23.     struct pollfd pfd;
  24.     char buf[512];
  25.  
  26.     // Open hotplug event netlink socket
  27.  
  28.     memset(&nls,0,sizeof(struct sockaddr_nl));
  29.     nls.nl_family = AF_NETLINK;
  30.     nls.nl_pid = getpid();
  31.     nls.nl_groups = -1;
  32.  
  33.     pfd.events = POLLIN;
  34.     pfd.fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
  35.     if (pfd.fd==-1)
  36.         die("Not root\n");
  37.  
  38.     // Listen to netlink socket
  39.  
  40.     if (bind(pfd.fd, (void *)&nls, sizeof(struct sockaddr_nl)))
  41.         die("Bind failed\n");
  42.     while (-1!=poll(&pfd, 1, -1)) {
  43.         int i, len = recv(pfd.fd, buf, sizeof(buf), MSG_DONTWAIT);
  44.         if (len == -1) die("recv\n");
  45.  
  46.         // Print the data to stdout.
  47.         i = 0;
  48.         while (i<len) {
  49.             printf("%s\n", buf+i);
  50.             i += strlen(buf+i)+1;
  51.         }
  52.     }
  53.     die("poll\n");
  54.  
  55.     // Dear gcc: shut up.
  56.     return 0;
  57.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement