chiark / gitweb /
udev: link-config - split connection to sockets from loading of configs
[elogind.git] / src / udev / net / link-config.c
index 598f93e384cdfaa40627fc62ad279f7047c3ed0f..f93a4d835d44b2971d60cf018892ddb5c9dacbf6 100644 (file)
@@ -49,9 +49,11 @@ struct link_config_ctx {
         usec_t link_dirs_ts_usec;
 };
 
+DEFINE_TRIVIAL_CLEANUP_FUNC(link_config_ctx*, link_config_ctx_free);
+#define _cleanup_link_config_ctx_free_ _cleanup_(link_config_ctx_freep)
+
 int link_config_ctx_new(link_config_ctx **ret) {
-        link_config_ctx *ctx;
-        int r;
+        _cleanup_link_config_ctx_free_ link_config_ctx *ctx = NULL;
 
         if (!ret)
                 return -EINVAL;
@@ -60,36 +62,44 @@ int link_config_ctx_new(link_config_ctx **ret) {
         if (!ctx)
                 return -ENOMEM;
 
-        r = ethtool_connect(&ctx->ethtool_fd);
-        if (r < 0) {
-                link_config_ctx_free(ctx);
-                return r;
-        }
-
-        r = sd_rtnl_open(0, &ctx->rtnl);
-        if (r < 0) {
-                link_config_ctx_free(ctx);
-                return r;
-        }
-
         LIST_HEAD_INIT(ctx->links);
 
+        ctx->ethtool_fd = -1;
+
         ctx->link_dirs = strv_new("/etc/systemd/network",
                                   "/run/systemd/network",
                                   "/usr/lib/systemd/network",
                                   NULL);
         if (!ctx->link_dirs) {
                 log_error("failed to build link config directory array");
-                link_config_ctx_free(ctx);
                 return -ENOMEM;
         }
+
         if (!path_strv_canonicalize_uniq(ctx->link_dirs)) {
                 log_error("failed to canonicalize link config directories\n");
-                link_config_ctx_free(ctx);
                 return -ENOMEM;
         }
 
         *ret = ctx;
+        ctx = NULL;
+
+        return 0;
+}
+
+static int link_config_ctx_connect(link_config_ctx *ctx) {
+        int r;
+
+        if (ctx->ethtool_fd >= 0 && ctx->rtnl)
+                return 0;
+
+        r = ethtool_connect(&ctx->ethtool_fd);
+        if (r < 0)
+                return r;
+
+        r = sd_rtnl_open(0, &ctx->rtnl);
+        if (r < 0)
+                return r;
+
         return 0;
 }
 
@@ -293,7 +303,7 @@ static int rtnl_set_properties(sd_rtnl *rtnl, int ifindex, const char *name, con
         }
 
         if  (need_update) {
-                r = sd_rtnl_send_with_reply_and_block(rtnl, message, 5 * USEC_PER_SEC, NULL);
+                r = sd_rtnl_send_with_reply_and_block(rtnl, message, 0, NULL);
                 if (r < 0)
                         return r;
         }
@@ -322,12 +332,15 @@ static bool enable_name_policy(void) {
 
 static bool mac_is_random(struct udev_device *device) {
         const char *s;
-        int type;
+        unsigned type;
+        int r;
 
         s = udev_device_get_sysattr_value(device, "addr_assign_type");
         if (!s)
-                return -EINVAL;
-        type = strtoul(s, NULL, 0);
+                return false; /* if we don't know, assume it is not random */
+        r = safe_atou(s, &type);
+        if (r < 0)
+                return false;
 
         /* check for NET_ADDR_RANDOM */
         return type == 1;
@@ -335,12 +348,15 @@ static bool mac_is_random(struct udev_device *device) {
 
 static bool mac_is_permanent(struct udev_device *device) {
         const char *s;
-        int type;
+        unsigned type;
+        int r;
 
         s = udev_device_get_sysattr_value(device, "addr_assign_type");
         if (!s)
-                return -EINVAL;
-        type = strtoul(s, NULL, 0);
+                return true; /* if we don't know, assume it is permanent */
+        r = safe_atou(s, &type);
+        if (r < 0)
+                return true;
 
         /* check for NET_ADDR_PERM */
         return type == 0;
@@ -365,13 +381,13 @@ static int get_mac(struct udev_device *device, bool want_random, struct ether_ad
                         if (!name) {
                                 name = udev_device_get_property_value(device, "ID_NET_NAME_PATH");
                                 if (!name)
-                                        return -1;
+                                        return -ENOENT;
                         }
                 }
                 /* fetch some persistent data unique to this machine */
                 r = sd_id128_get_machine(&machine);
                 if (r < 0)
-                        return -1;
+                        return r;
 
                 /* combine the data */
                 seed_str = strappenda(name, sd_id128_to_string(machine, machineid_buf));
@@ -400,6 +416,10 @@ int link_config_apply(link_config_ctx *ctx, link_config *config, struct udev_dev
         struct ether_addr *mac = NULL;
         int r, ifindex;
 
+        r = link_config_ctx_connect(ctx);
+        if (r < 0)
+                return r;
+
         name = udev_device_get_sysname(device);
         if (!name)
                 return -EINVAL;