Pierre-Hugues Husson | d8f43b5 | 2018-10-01 10:54:12 +0200 | [diff] [blame] | 1 | typedef unsigned short int sa_family_t; |
| 2 | #define __KERNEL_STRICT_NAMES |
| 3 | #include <sys/types.h> |
| 4 | #include <linux/netlink.h> |
| 5 | #include <sys/socket.h> |
| 6 | #include <string.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <stdio.h> |
| 9 | #include <unistd.h> |
| 10 | |
| 11 | //From a uevent patch for hal |
| 12 | #define HOTPLUG_BUFFER_SIZE 1024 |
| 13 | #define HOTPLUG_NUM_ENVP 32 |
| 14 | #define OBJECT_SIZE 512 |
| 15 | |
| 16 | #ifndef NETLINK_KOBJECT_UEVENT |
| 17 | #error Your kernel headers are too old, and do not define NETLINK_KOBJECT_UEVENT. You need Linux 2.6.10 or higher for KOBJECT_UEVENT support. |
| 18 | #endif |
| 19 | |
| 20 | int main(int argc, char **argv, char **envp) { |
| 21 | //Start listening |
| 22 | int fd; |
| 23 | struct sockaddr_nl ksnl; |
| 24 | memset(&ksnl, 0x00, sizeof(struct sockaddr_nl)); |
| 25 | ksnl.nl_family=AF_NETLINK; |
| 26 | ksnl.nl_pid=getpid(); |
| 27 | ksnl.nl_groups=0xffffffff; |
| 28 | fd=socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT); |
| 29 | if (fd==-1) { |
| 30 | printf("Couldn't open kobject-uevent netlink socket"); |
| 31 | perror(""); |
| 32 | exit(1); |
| 33 | } |
| 34 | if (bind(fd, (struct sockaddr *) &ksnl, sizeof(struct sockaddr_nl))<0) { |
| 35 | fprintf (stderr, "Error binding to netlink socket"); |
| 36 | close(fd); |
| 37 | exit(1); |
| 38 | } |
| 39 | |
| 40 | while(1) { |
| 41 | char buffer[HOTPLUG_BUFFER_SIZE + OBJECT_SIZE]; |
| 42 | int buflen; |
| 43 | buflen=recv(fd, &buffer, sizeof(buffer), 0); |
| 44 | if (buflen<0) { |
| 45 | exit(1); |
| 46 | } |
| 47 | printf("%s\n", buffer); |
| 48 | char *pos = buffer + strlen(buffer); |
| 49 | char *end = buffer + buflen; |
| 50 | while(pos < end) { |
| 51 | int l = strlen(pos); |
| 52 | printf("\t%s\n", pos); |
| 53 | pos += l+1; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | } |