#include <systemd/sd-device.h>
+#include "daemon.h"
#include "devmon.h"
+#include "logging.h"
+
+static int nw_daemon_handle_uevent_net(nw_daemon* daemon,
+ sd_device* device, sd_device_action_t action) {
+ nw_link* link = NULL;
+ int ifindex;
+ int r;
+
+ // Fetch ifindex
+ r = sd_device_get_ifindex(device, &ifindex);
+ if (r < 0) {
+ ERROR("Could not get ifindex from uevent: %s\n", strerror(-r));
+ goto ERROR;
+ }
+
+ // Fetch the link
+ link = nw_daemon_get_link_by_ifindex(daemon, ifindex);
+ if (!link) {
+ DEBUG("Could not fetch link %d, ignoring\n", ifindex);
+ r = 0;
+ goto ERROR;
+ }
+
+ // Let the link handle its uevent
+ r = nw_link_handle_uevent(link, device, action);
+
+ERROR:
+ if (link)
+ nw_link_unref(link);
+
+ return r;
+}
int nw_devmon_handle_uevent(sd_device_monitor* monitor, sd_device* device, void* data) {
+ sd_device_action_t action;
+ const char* subsystem = NULL;
+ int r;
+
+ // Fetch daemon
+ nw_daemon* daemon = (nw_daemon*)data;
+
+ // Fetch action
+ r = sd_device_get_action(device, &action);
+ if (r < 0) {
+ WARNING("Could not get uevent action, ignoring: %s\n", strerror(-r));
+ return r;
+ }
+
+ // Fetch subsystem
+ r = sd_device_get_subsystem(device, &subsystem);
+ if (r < 0) {
+ ERROR("Could not get uevent subsystem, ignoring: %s\n", strerror(-r));
+ return r;
+ }
+
+ // Handle any links
+ if (strcmp(subsystem, "net") == 0) {
+ r = nw_daemon_handle_uevent_net(daemon, device, action);
+
+ } else {
+ DEBUG("Received an uevent for an unhandled subsystem '%s', ignoring\n", subsystem);
+ return 0;
+ }
+
+ // Log if something went wrong
+ if (r < 0)
+ ERROR("Failed processing uevent: %s\n", strerror(-r));
+
return 0;
}
return r;
}
+// uevent
+
+int nw_link_handle_uevent(nw_link* link, sd_device* device, sd_device_action_t action) {
+ // We need to remove or replace the stored device as it is now outdated
+ if (link->device) {
+ sd_device_unref(link->device);
+
+ // If the device has been removed, we remove its reference
+ if (action == SD_DEVICE_REMOVE)
+ link->device = NULL;
+
+ // Otherwise we just store the new one
+ else
+ link->device = sd_device_ref(device);
+ }
+
+ return 0;
+}
+
// JSON
static int nw_link_device_to_json(nw_link* link, struct json_object* o) {
#include <linux/if_link.h>
+#include <systemd/sd-device.h>
+
typedef struct nw_link nw_link;
#include "daemon.h"
int nw_link_destroy(nw_link* link);
+// uevent
+int nw_link_handle_uevent(nw_link* link, sd_device* device, sd_device_action_t action);
+
// JSON
int nw_link_to_json(nw_link* link, struct json_object* o);