]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
ip: Exit exec in child process if setup fails
authorYedaya Katsman <yedaya.ka@gmail.com>
Tue, 23 Apr 2024 18:38:20 +0000 (21:38 +0300)
committerStephen Hemminger <stephen@networkplumber.org>
Thu, 25 Apr 2024 19:00:25 +0000 (12:00 -0700)
If we forked, returning from the function will make the calling code to
continue in both the child and parent process. Make cmd_exec exit if
setup failed and it forked already.

An example of issues this causes, where a failure in setup causes
multiple unnecessary tries:

```
$ ip netns
ef
ab
$ ip -all netns exec ls

netns: ef
setting the network namespace "ef" failed: Operation not permitted

netns: ab
setting the network namespace "ab" failed: Operation not permitted

netns: ab
setting the network namespace "ab" failed: Operation not permitted
```

Signed-off-by: Yedaya Katsman <yedaya.ka@gmail.com>
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
lib/exec.c

index 9b1c8f4a13960b10063f67959000f32bd1b7b1f1..89393755007957dd7e4fb4337e7e021fc9a25c79 100644 (file)
@@ -36,8 +36,13 @@ int cmd_exec(const char *cmd, char **argv, bool do_fork,
                }
        }
 
-       if (setup && setup(arg))
+       if (setup && setup(arg)) {
+               if (do_fork) {
+                       /* In child, nothing to do */
+                       _exit(1);
+               }
                return -1;
+       }
 
        if (execvp(cmd, argv)  < 0)
                fprintf(stderr, "exec of \"%s\" failed: %s\n",