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
60
61
62
63
64
65
66
67
68
69
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/poll.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <linux/types.h>
#include <linux/netlink.h>
FILE *fp;
void exit_handler()
{
fprintf(stdout, "exiting from uevent reader...\n");
fclose(fp);
}
int main(int argc, char *argv[])
{
struct sockaddr_nl nls;
struct pollfd pfd;
char buf[512];
fp = fopen(argv[1], "w+");
if (fp == NULL) {
fprintf(stderr, "Can't open input file\n");
exit(1);
}
signal(SIGINT, exit_handler);
/* bind to uevent 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) {
perror("error: socket()");
exit_handler();
}
if (bind(pfd.fd, (struct sockaddr *) &nls,
sizeof(struct sockaddr_nl))) {
perror("error : bind()");
exit_handler();
}
while (-1 != poll(&pfd, 1, -1)) {
int i, len = recv(pfd.fd, buf, sizeof(buf), MSG_DONTWAIT);
if (len == -1) {
perror("error : recv()");
exit_handler();
}
i = 0;
while (i < len) {
fprintf(fp, "%s\n", buf+i);
i += strlen(buf+i)+1;
}
}
return 0;
}
|