[PATCH] ip: Exit exec in child process if setup fails

Subsystems: library code, the rest

STALE873d

3 messages, 2 authors, 2024-04-21 · open the first message on its own page

[PATCH] ip: Exit exec in child process if setup fails

From: Yedaya Katsman <yedaya.ka@gmail.com>
Date: 2024-03-24 16:34:54

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>
---
 lib/exec.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/lib/exec.c b/lib/exec.c
index 9b1c8f4a1396..893937550079 100644
--- a/lib/exec.c
+++ b/lib/exec.c
@@ -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",
-- 
2.34.1

Re: [PATCH] ip: Exit exec in child process if setup fails

From: Yedaya <yedaya.ka@gmail.com>
Date: 2024-04-18 20:16:52

Ping - in case you missed this

On Sun, Mar 24, 2024 at 06:34:36PM +0200, Yedaya Katsman wrote:
quoted hunk
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>
---
 lib/exec.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/lib/exec.c b/lib/exec.c
index 9b1c8f4a1396..893937550079 100644
--- a/lib/exec.c
+++ b/lib/exec.c
@@ -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",
-- 
2.34.1

Re: [PATCH] ip: Exit exec in child process if setup fails

From: David Ahern <hidden>
Date: 2024-04-21 01:58:39

On 4/18/24 2:16 PM, Yedaya wrote:
Ping - in case you missed this
please re-send; I think it fell through the cracks.
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help