-
Notifications
You must be signed in to change notification settings - Fork 0
/
anal-monitor-monitorEvents.c
59 lines (53 loc) · 1.38 KB
/
anal-monitor-monitorEvents.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <linux/netlink.h>
int main()
{
setbuf(stdout, NULL);
struct sockaddr_nl nl;
char buf[8192];
memset(&nl,0,sizeof(struct sockaddr_nl));
nl.nl_family = AF_NETLINK;
nl.nl_pid = getpid();
nl.nl_groups = -1;
int fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
if (fd < 0) {
fprintf(stderr, "Unable to create socket\n");
exit(1);
}
if (bind(fd, (void *)&nl, sizeof(struct sockaddr_nl)) != 0) {
fprintf(stderr, "Unable to bind socket\n");
exit(1);
}
for(;;) {
int len = recv(fd, buf, sizeof(buf), 0);
if (len < 0) {
fprintf(stderr, "Error reading from socket: %s", strerror(errno));
continue;
}
int i = 0;
int line = 0;
while (i < len) {
char *p = buf+i;
if (line == 0) {
if (!strncmp(p, "libudev", 7)) {
break;
}
} else {
if (!strncmp(p, "SUBSYSTEM=drm", 13)) {
printf("%s\n", p);
break;
}
}
// printf("%d: %s\n", line, p);
i += strlen(buf+i)+1;
line++;
}
}
return 0;
}