chiark / gitweb /
Be more careful when checking for empty files
[elogind.git] / src / udev / net / link-config.c
index a9acc3ddb7f3173058a07209f4680abb16abba54..512885f9c8604ced0757f4dd476133e7b3ab9dc8 100644 (file)
@@ -92,14 +92,20 @@ static int link_config_ctx_connect(link_config_ctx *ctx) {
 
         if (ctx->ethtool_fd == -1) {
                 r = ethtool_connect(&ctx->ethtool_fd);
-                if (r < 0)
+                if (r < 0) {
+                        log_warning("link_config: could not connect to ethtool: %s",
+                                    strerror(-r));
                         return r;
+                }
         }
 
         if (!ctx->rtnl) {
                 r = sd_rtnl_open(&ctx->rtnl, 0);
-                if (r < 0)
+                if (r < 0) {
+                        log_warning("link_config: could not connect to rtnl: %s",
+                                    strerror(-r));
                         return r;
+                }
         }
 
         return 0;
@@ -147,11 +153,6 @@ static int load_link(link_config_ctx *ctx, const char *filename) {
         assert(ctx);
         assert(filename);
 
-        if (null_or_empty_path(filename)) {
-                log_debug("skipping empty file: %s", filename);
-                return 0;
-        }
-
         file = fopen(filename, "re");
         if (!file) {
                 if (errno == ENOENT)
@@ -160,6 +161,11 @@ static int load_link(link_config_ctx *ctx, const char *filename) {
                         return -errno;
         }
 
+        if (null_or_empty_fd(fileno(file))) {
+                log_debug("Skipping empty file: %s", filename);
+                return 0;
+        }
+
         link = new0(link_config, 1);
         if (!link)
                 return log_oom();
@@ -168,12 +174,13 @@ static int load_link(link_config_ctx *ctx, const char *filename) {
         link->wol = _WOL_INVALID;
         link->duplex = _DUP_INVALID;
 
-        r = config_parse(NULL, filename, file, "Match\0Link\0Ethernet\0", config_item_perf_lookup,
-                         (void*) link_config_gperf_lookup, false, false, link);
-        if (r < 0) {
-                log_warning("Could not parse config file %s: %s", filename, strerror(-r));
+        r = config_parse(NULL, filename, file,
+                         "Match\0Link\0Ethernet\0",
+                         config_item_perf_lookup, link_config_gperf_lookup,
+                         false, false, true, link);
+        if (r < 0)
                 return r;
-        else
+        else
                 log_debug("Parsed configuration file %s", filename);
 
         link->filename = strdup(filename);
@@ -185,7 +192,7 @@ static int load_link(link_config_ctx *ctx, const char *filename) {
 }
 
 static bool enable_name_policy(void) {
-        _cleanup_free_ char *line;
+        _cleanup_free_ char *line = NULL;
         char *w, *state;
         int r;
         size_t l;
@@ -377,7 +384,9 @@ int link_config_apply(link_config_ctx *ctx, link_config *config, struct udev_dev
                 case MACPOLICY_PERSISTENT:
                         if (mac_is_random(device)) {
                                 r = get_mac(device, false, &generated_mac);
-                                if (r < 0)
+                                if (r == -ENOENT)
+                                        break;
+                                else if (r < 0)
                                         return r;
                                 mac = &generated_mac;
                         }
@@ -385,7 +394,9 @@ int link_config_apply(link_config_ctx *ctx, link_config *config, struct udev_dev
                 case MACPOLICY_RANDOM:
                         if (!mac_is_random(device)) {
                                 r = get_mac(device, true, &generated_mac);
-                                if (r < 0)
+                                if (r == -ENOENT)
+                                        break;
+                                else if (r < 0)
                                         return r;
                                 mac = &generated_mac;
                         }