libmnl  1.0.4
kobject-event.c
1 /* This example is placed in the public domain. */
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <time.h>
6 
7 #include <libmnl/libmnl.h>
8 #include <linux/netlink.h>
9 
10 int main(int argc, char *argv[])
11 {
12  struct mnl_socket *nl;
13  char buf[MNL_SOCKET_BUFFER_SIZE];
14  int ret;
15 
16  nl = mnl_socket_open(NETLINK_KOBJECT_UEVENT);
17  if (nl == NULL) {
18  perror("mnl_socket_open");
19  exit(EXIT_FAILURE);
20  }
21 
22  /* There is one single group in kobject over netlink */
23  if (mnl_socket_bind(nl, (1<<0), MNL_SOCKET_AUTOPID) < 0) {
24  perror("mnl_socket_bind");
25  exit(EXIT_FAILURE);
26  }
27 
28  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
29  while (ret > 0) {
30  int i;
31 
32  /* kobject uses a string based protocol, with no initial
33  * netlink header.
34  */
35  for (i=0; i<ret; i++)
36  printf("%c", buf[i]);
37 
38  printf("\n");
39  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
40  }
41  if (ret == -1) {
42  perror("error");
43  exit(EXIT_FAILURE);
44  }
45 
46  mnl_socket_close(nl);
47 
48  return 0;
49 }