#include <string.h>
#include <fcntl.h>
#include <glob.h>
+#include <time.h>
#include <netlink/msg.h>
#include <netlink/attr.h>
static struct nl_sock *sock_rtnl = NULL;
static int cb_rtnl_event(struct nl_msg *msg, void *arg);
+static void handle_hotplug_event(struct uloop_fd *u, unsigned int events);
-static void handler_nl_event(struct uloop_fd *u, unsigned int events)
+static void
+handler_nl_event(struct uloop_fd *u, unsigned int events)
{
struct event_socket *ev = container_of(u, struct event_socket, uloop);
nl_recvmsgs(ev->sock, ev->cb);
}
+static struct nl_sock *
+create_socket(int protocol, int groups)
+{
+ struct nl_sock *sock;
+
+ sock = nl_socket_alloc();
+ if (!sock)
+ return NULL;
+
+ if (groups)
+ nl_join_groups(sock, groups);
+
+ if (nl_connect(sock, protocol))
+ return NULL;
+
+ return sock;
+}
+
+static bool
+create_raw_event_socket(struct event_socket *ev, int protocol, int groups,
+ uloop_fd_handler cb)
+{
+ ev->sock = create_socket(protocol, groups);
+ if (!ev->sock)
+ return false;
+
+ ev->uloop.fd = nl_socket_get_fd(ev->sock);
+ ev->uloop.cb = cb;
+ uloop_fd_add(&ev->uloop, ULOOP_READ | ULOOP_EDGE_TRIGGER);
+ return true;
+}
+
+static bool
+create_event_socket(struct event_socket *ev, int protocol,
+ int (*cb)(struct nl_msg *msg, void *arg))
+{
+ // Prepare socket for link events
+ ev->cb = nl_cb_alloc(NL_CB_DEFAULT);
+ if (!ev->cb)
+ return false;
+
+ nl_cb_set(ev->cb, NL_CB_VALID, NL_CB_CUSTOM, cb, NULL);
+
+ return create_raw_event_socket(ev, protocol, 0, handler_nl_event);
+}
+
int system_init(void)
{
- static struct event_socket rtnl_event = {
- .uloop.cb = handler_nl_event,
- };
+ static struct event_socket rtnl_event;
+ static struct event_socket hotplug_event;
sock_ioctl = socket(AF_LOCAL, SOCK_DGRAM, 0);
fcntl(sock_ioctl, F_SETFD, fcntl(sock_ioctl, F_GETFD) | FD_CLOEXEC);
// Prepare socket for routing / address control
- sock_rtnl = nl_socket_alloc();
+ sock_rtnl = create_socket(NETLINK_ROUTE, 0);
if (!sock_rtnl)
return -1;
- if (nl_connect(sock_rtnl, NETLINK_ROUTE))
+ if (!create_event_socket(&rtnl_event, NETLINK_ROUTE, cb_rtnl_event))
return -1;
- // Prepare socket for link events
- rtnl_event.cb = nl_cb_alloc(NL_CB_DEFAULT);
- if (!rtnl_event.cb)
- return -1;
-
- nl_cb_set(rtnl_event.cb, NL_CB_VALID, NL_CB_CUSTOM,
- cb_rtnl_event, NULL);
-
- rtnl_event.sock = nl_socket_alloc();
- if (!rtnl_event.sock)
- return -1;
-
- if (nl_connect(rtnl_event.sock, NETLINK_ROUTE))
+ if (!create_raw_event_socket(&hotplug_event, NETLINK_KOBJECT_UEVENT, 1,
+ handle_hotplug_event))
return -1;
// Receive network link events form kernel
nl_socket_add_membership(rtnl_event.sock, RTNLGRP_LINK);
- rtnl_event.uloop.fd = nl_socket_get_fd(rtnl_event.sock);
- uloop_fd_add(&rtnl_event.uloop, ULOOP_READ | ULOOP_EDGE_TRIGGER);
-
return 0;
}
goto out;
dev->ifindex = ifi->ifi_index;
- device_set_present(dev, (nh->nlmsg_type == RTM_NEWLINK));
+ /* TODO: parse link status */
out:
return 0;
}
+static void
+handle_hotplug_msg(char *data, int size)
+{
+ const char *subsystem = NULL, *interface = NULL;
+ char *cur, *end, *sep;
+ struct device *dev;
+ int skip;
+ bool add;
+
+ if (!strncmp(data, "add@", 4))
+ add = true;
+ else if (!strncmp(data, "remove@", 7))
+ add = false;
+ else
+ return;
+
+ skip = strlen(data) + 1;
+ end = data + size;
+
+ for (cur = data + skip; cur < end; cur += skip) {
+ skip = strlen(cur) + 1;
+
+ sep = strchr(cur, '=');
+ if (!sep)
+ continue;
+
+ *sep = 0;
+ if (!strcmp(cur, "INTERFACE"))
+ interface = sep + 1;
+ else if (!strcmp(cur, "SUBSYSTEM")) {
+ subsystem = sep + 1;
+ if (strcmp(subsystem, "net") != 0)
+ return;
+ }
+ if (subsystem && interface)
+ goto found;
+ }
+ return;
+
+found:
+ dev = device_get(interface, false);
+ if (!dev)
+ return;
+
+ if (dev->type != &simple_device_type)
+ return;
+
+ device_set_present(dev, add);
+}
+
+static void
+handle_hotplug_event(struct uloop_fd *u, unsigned int events)
+{
+ struct event_socket *ev = container_of(u, struct event_socket, uloop);
+ struct sockaddr_nl nla;
+ unsigned char *buf = NULL;
+ int size;
+
+ while ((size = nl_recv(ev->sock, &nla, &buf, NULL)) > 0) {
+ if (nla.nl_pid == 0)
+ handle_hotplug_msg((char *) buf, size);
+
+ free(buf);
+ }
+}
+
static int system_rtnl_call(struct nl_msg *msg)
{
int s = -(nl_send_auto_complete(sock_rtnl, msg)
int system_if_check(struct device *dev)
{
- device_set_present(dev, (system_if_resolve(dev) >= 0));
+ device_set_present(dev, (system_if_resolve(dev) > 0));
return 0;
}
+struct device *
+system_if_get_parent(struct device *dev)
+{
+ char buf[64], *devname;
+ int ifindex, iflink, len;
+ FILE *f;
+
+ snprintf(buf, sizeof(buf), "/sys/class/net/%s/iflink", dev->ifname);
+ f = fopen(buf, "r");
+ if (!f)
+ return NULL;
+
+ len = fread(buf, 1, sizeof(buf) - 1, f);
+ fclose(f);
+
+ if (len <= 0)
+ return NULL;
+
+ buf[len] = 0;
+ iflink = strtoul(buf, NULL, 0);
+ ifindex = system_if_resolve(dev);
+ if (!iflink || iflink == ifindex)
+ return NULL;
+
+ devname = if_indextoname(iflink, buf);
+ if (!devname)
+ return NULL;
+
+ return device_get(devname, true);
+}
+
int system_if_dump_stats(struct device *dev, struct blob_buf *b)
{
const char *const counters[] = {