variuos old test code
[pandora-misc.git] / tests / uevent.c
diff --git a/tests/uevent.c b/tests/uevent.c
new file mode 100644 (file)
index 0000000..de8657a
--- /dev/null
@@ -0,0 +1,65 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <sys/poll.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <linux/types.h>
+#include <linux/netlink.h>
+
+void die(char *s)
+{
+       perror(s);
+       exit(1);
+}
+
+int main(int argc, char *argv[])
+{
+       struct sockaddr_nl nls;
+       struct pollfd pfd;
+       char buf[512];
+
+       // Open hotplug event netlink socket
+
+       memset(&nls,0,sizeof(struct sockaddr_nl));
+       nls.nl_family = AF_NETLINK;
+       nls.nl_pid = getpid();
+       nls.nl_groups = -1;
+
+       pfd.events = POLLIN;
+       pfd.fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
+       if (pfd.fd == -1)
+               die("socket");
+
+       // Listen to netlink socket
+
+       if (bind(pfd.fd, (void *)&nls, sizeof(struct sockaddr_nl)))
+               die("bind");
+
+       while (-1 != poll(&pfd, 1, -1)) {
+               struct sockaddr_nl snl;
+               struct iovec iov = { buf, sizeof(buf) };
+               //char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
+               //struct msghdr hdr = { &snl, sizeof(snl), &iov, 1, cred_msg, sizeof(cred_msg), 0 };
+               struct msghdr hdr = { &snl, sizeof(snl), &iov, 1, NULL, 0, 0 };
+               int i, len;
+               
+               len = recvmsg(pfd.fd, &hdr, MSG_DONTWAIT);
+               if (len == -1)
+                       die("recvmsg");
+
+               // Print the data to stdout.
+               printf("=== nl_groups %d, nl_pid %d\n", snl.nl_groups, snl.nl_pid);
+               i = 0;
+               while (i<len) {
+                       printf("%s\n", buf+i);
+                       i += strlen(buf+i)+1;
+               }
+       }
+       die("poll\n");
+
+       return 0;
+}