variuos old test code
[pandora-misc.git] / tests / uevent.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include <sys/poll.h>
6 #include <sys/socket.h>
7 #include <sys/types.h>
8 #include <unistd.h>
9
10 #include <linux/types.h>
11 #include <linux/netlink.h>
12
13 void die(char *s)
14 {
15         perror(s);
16         exit(1);
17 }
18
19 int main(int argc, char *argv[])
20 {
21         struct sockaddr_nl nls;
22         struct pollfd pfd;
23         char buf[512];
24
25         // Open hotplug event netlink socket
26
27         memset(&nls,0,sizeof(struct sockaddr_nl));
28         nls.nl_family = AF_NETLINK;
29         nls.nl_pid = getpid();
30         nls.nl_groups = -1;
31
32         pfd.events = POLLIN;
33         pfd.fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
34         if (pfd.fd == -1)
35                 die("socket");
36
37         // Listen to netlink socket
38
39         if (bind(pfd.fd, (void *)&nls, sizeof(struct sockaddr_nl)))
40                 die("bind");
41
42         while (-1 != poll(&pfd, 1, -1)) {
43                 struct sockaddr_nl snl;
44                 struct iovec iov = { buf, sizeof(buf) };
45                 //char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
46                 //struct msghdr hdr = { &snl, sizeof(snl), &iov, 1, cred_msg, sizeof(cred_msg), 0 };
47                 struct msghdr hdr = { &snl, sizeof(snl), &iov, 1, NULL, 0, 0 };
48                 int i, len;
49                 
50                 len = recvmsg(pfd.fd, &hdr, MSG_DONTWAIT);
51                 if (len == -1)
52                         die("recvmsg");
53
54                 // Print the data to stdout.
55                 printf("=== nl_groups %d, nl_pid %d\n", snl.nl_groups, snl.nl_pid);
56                 i = 0;
57                 while (i<len) {
58                         printf("%s\n", buf+i);
59                         i += strlen(buf+i)+1;
60                 }
61         }
62         die("poll\n");
63
64         return 0;
65 }