]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
resolvectl: rework ifname_mangle()
authorLennart Poettering <lennart@poettering.net>
Wed, 5 Dec 2018 12:33:31 +0000 (13:33 +0100)
committerLennart Poettering <lennart@poettering.net>
Wed, 5 Dec 2018 17:46:23 +0000 (18:46 +0100)
Let's compare the ifname passed in with what is set already if there is
something set already. Complain in that case. This makes commands such
as "resolvectl -i foo dns bar" less weird, as we'll refuse the duplicate
ifname specifications.

Also, free the old arg_ifname right before assigning the new, instead of
doing so in advance.

src/resolve/resolvectl.c

index 1e3fabc450bfa85a8fede5920ffdfc6289ce8a37..e6d56b0ef4d7dfdfb31b4678cdadd588f5c67116 100644 (file)
@@ -91,36 +91,23 @@ static int parse_ifindex_and_warn(const char *s) {
 int ifname_mangle(const char *s, bool allow_loopback) {
         _cleanup_free_ char *iface = NULL;
         const char *dot;
-        int r;
+        int ifi;
 
         assert(s);
 
-        if (arg_ifname) {
-                assert(arg_ifindex >= 0);
-
-                if (!allow_loopback && arg_ifindex == LOOPBACK_IFINDEX)
-                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
-                                               "Interface can't be the loopback interface (lo). Sorry.");
-
-                return 1;
-        }
-
         dot = strchr(s, '.');
         if (dot) {
+                log_debug("Ignoring protocol specifier '%s'.", dot + 1);
                 iface = strndup(s, dot - s);
-                if (!iface)
-                        return log_oom();
 
-                log_debug("Ignoring protocol specifier '%s'.", dot + 1);
-        } else {
+        } else
                 iface = strdup(s);
-                if (!iface)
-                        return log_oom();
-        }
+        if (!iface)
+                return log_oom();
 
-        if (parse_ifindex(iface, &r) < 0) {
-                r = if_nametoindex(iface);
-                if (r <= 0) {
+        if (parse_ifindex(iface, &ifi) < 0) {
+                ifi = if_nametoindex(iface);
+                if (ifi <= 0) {
                         if (errno == ENODEV && arg_ifindex_permissive) {
                                 log_debug("Interface '%s' not found, but -f specified, ignoring.", iface);
                                 return 0; /* done */
@@ -130,12 +117,17 @@ int ifname_mangle(const char *s, bool allow_loopback) {
                 }
         }
 
-        if (!allow_loopback && r == LOOPBACK_IFINDEX)
+        if (arg_ifindex > 0 && arg_ifindex != ifi) {
+                log_error("Specified multiple different interfaces. Refusing.");
+                return -EINVAL;
+        }
+
+        if (!allow_loopback && ifi == LOOPBACK_IFINDEX)
                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                        "Interface can't be the loopback interface (lo). Sorry.");
 
-        arg_ifindex = r;
-        arg_ifname = TAKE_PTR(iface);
+        arg_ifindex = ifi;
+        free_and_replace(arg_ifname, iface);
 
         return 1;
 }
@@ -2501,7 +2493,6 @@ static int compat_parse_argv(int argc, char *argv[]) {
                         break;
 
                 case 'i':
-                        arg_ifname = mfree(arg_ifname);
                         r = ifname_mangle(optarg, true);
                         if (r < 0)
                                 return r;
@@ -2793,7 +2784,6 @@ static int native_parse_argv(int argc, char *argv[]) {
                         break;
 
                 case 'i':
-                        arg_ifname = mfree(arg_ifname);
                         r = ifname_mangle(optarg, true);
                         if (r < 0)
                                 return r;