8 #include <sys/socket.h>
12 #include <linux/types.h>
13 #include <linux/netlink.h>
15 #include "hotplug2_utils.h"
18 * A trivial function that reads kernel seqnum from sysfs.
20 * Returns: Seqnum as read from sysfs
22 inline event_seqnum_t
get_kernel_seqnum() {
28 strcpy(filename
, sysfs_seqnum_path
);
30 fp
= fopen(filename
, "r");
34 fread(seqnum
, 1, 64, fp
);
37 return strtoull(seqnum
, NULL
, 0);
41 * Opens a PF_NETLINK socket into the kernel, to read uevents.
43 * @1 Specifies type of socket (whether we bind or whether we connect)
45 * Returns: Socket fd if succesful, -1 otherwise.
47 inline int init_netlink_socket(int type
) {
49 struct sockaddr_nl snl
;
50 int buffersize
= 16 * 1024 * 1024;
52 memset(&snl
, 0x00, sizeof(struct sockaddr_nl
));
53 snl
.nl_family
= AF_NETLINK
;
54 snl
.nl_pid
= getpid();
56 netlink_socket
= socket(PF_NETLINK
, SOCK_DGRAM
, NETLINK_KOBJECT_UEVENT
);
57 if (netlink_socket
== -1) {
58 ERROR("opening netlink","Failed socket: %s.", strerror(errno
));
63 * We're trying to override buffer size. If we fail, we attempt to set a big buffer and pray.
65 if (setsockopt(netlink_socket
, SOL_SOCKET
, SO_RCVBUFFORCE
, &buffersize
, sizeof(buffersize
))) {
66 ERROR("opening netlink","Failed setsockopt: %s. (non-critical)", strerror(errno
));
68 /* Somewhat safe default. */
71 if (setsockopt(netlink_socket
, SOL_SOCKET
, SO_RCVBUF
, &buffersize
, sizeof(buffersize
))) {
72 ERROR("opening netlink","Failed setsockopt: %s. (critical)", strerror(errno
));
77 * hotplug2-dnode performs connect, while hotplug2 daemon binds
81 if (connect(netlink_socket
, (struct sockaddr
*) &snl
, sizeof(struct sockaddr_nl
))) {
82 ERROR("opening netlink","Failed connect: %s.", strerror(errno
));
83 close(netlink_socket
);
88 if (bind(netlink_socket
, (struct sockaddr
*) &snl
, sizeof(struct sockaddr_nl
))) {
89 ERROR("opening netlink","Failed bind: %s.", strerror(errno
));
90 close(netlink_socket
);
95 return netlink_socket
;