Skip to contentSkip to conversation

ipbatch: fix use of 'ip netns exec'

PATCH Superseded Nicolas Dichtel 3 participants 9 messages Jul 8, 2013 – Jul 10, 2013 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): 20130705141639.56b47e7e@samsung-9

#1 lore raw

PATCH v2 1/1 ipbatch: fix use of 'ip netns exec'

From: JunweiZhang <[email protected]>

execvp() does not return when the command succeed, hence all commands in the batch file after the line 'ip netns exec' are not executed.

Let's fork before calling execvp().

Example:

$ cat test.batch
netns add netns1
netns exec netns1 ip l
netns
$ ip -b test.batch
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: sit0: <NOARP> mtu 1480 qdisc noop state DOWN mode DEFAULT
    link/sit 0.0.0.0 brd 0.0.0.0

All command after 'netns exec' are never executed.

With the patch:

$ ip -b test.batch
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: sit0: <NOARP> mtu 1480 qdisc noop state DOWN mode DEFAULT
    link/sit 0.0.0.0 brd 0.0.0.0
netns1

Now, existing netns are displayed.

---

v2: keep exit status of the child
add an example in the commit log

 ip/ipnetns.c | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)
ip/ipnetns.c +18 -4
index fa2b681..9f401ca 100644
@@ -138,6 +138,7 @@ static int netns_exec(int argc, char **argv)
 	const char *name, *cmd;
 	char net_path[MAXPATHLEN];
 	int netns;
+	int pid, status;
 
 	if (argc < 1) {
 		fprintf(stderr, "No netns name specified\n");
@@ -185,10 +186,23 @@ static int netns_exec(int argc, char **argv)
 	/* Setup bind mounts for config files in /etc */
 	bind_etc(name);
 
-	if (execvp(cmd, argv + 1)  < 0)
-		fprintf(stderr, "exec of \"%s\" failed: %s\n",
-			cmd, strerror(errno));
-	return EXIT_FAILURE;
+	pid = fork();
+	if (pid < 0)
+		return EXIT_FAILURE;
+	else if (pid > 0)
+		waitpid(pid, &status, 0);
+	else {
+		/* Child */
+		if (execvp(cmd, argv + 1)  < 0)
+			fprintf(stderr, "exec of \"%s\" failed: %s\n",
+				cmd, strerror(errno));
+		return EXIT_FAILURE;
+	}
+	/* ip must returns the status of the child, but do_cmd() will add a
+	 * minus to this returned value, so let's add another one here to
+	 * cancel it.
+	 */
+	return -WEXITSTATUS(status);
 }
 
 static int is_pid(const char *str)
Signature · 1 line
1.8.2.1
#2 lore raw

On Mon, 2013-07-08 at 11:44 +0200, Nicolas Dichtel wrote:
[...]

Quoted 11 lines
ip/ipnetns.c +1 -0
@@ -138,6 +138,7 @@ static int netns_exec(int argc, char **argv)
 	const char *name, *cmd;
 	char net_path[MAXPATHLEN];
 	int netns;
+	int pid, status;
 
 	if (argc < 1) {
 		fprintf(stderr, "No netns name specified\n");

ip/ipnetns.c static int netns_exec(int argc, char **argv) · line 191

Quoted 10 lines
ip/ipnetns.c +3 -4
@@ -185,10 +186,23 @@ static int netns_exec(int argc, char **argv)
 	/* Setup bind mounts for config files in /etc */
 	bind_etc(name);
 
-	if (execvp(cmd, argv + 1)  < 0)
-		fprintf(stderr, "exec of \"%s\" failed: %s\n",
-			cmd, strerror(errno));
-	return EXIT_FAILURE;
+	pid = fork();
+	if (pid < 0)
+		return EXIT_FAILURE;

On failure, this should emit an error message:

		perror("fork");

and then return -1, surely?

ip/ipnetns.c static int netns_exec(int argc, char **argv) · line 199

Quoted 8 lines
ip/ipnetns.c +8 -0
+	else if (pid > 0)
+		waitpid(pid, &status, 0);
+	else {
+		/* Child */
+		if (execvp(cmd, argv + 1)  < 0)
+			fprintf(stderr, "exec of \"%s\" failed: %s\n",
+				cmd, strerror(errno));
+		return EXIT_FAILURE;

No, the child must not return from this function. Otherwise there will be two processes trying to do the same cleanup on the resources that they still share. The child should call _exit(127) if exec*() fails.

ip/ipnetns.c static int netns_exec(int argc, char **argv) · line 208

Quoted 9 lines
ip/ipnetns.c +6 -0
+	}
+	/* ip must returns the status of the child, but do_cmd() will add a
+	 * minus to this returned value, so let's add another one here to
+	 * cancel it.
+	 */
+	return -WEXITSTATUS(status);
 }
 
 static int is_pid(const char *str)

WEXITSTATUS() is valid only if waitpid() was successful and WIFEXITED() yields true.

Ben.

Signature · 3 lines
Ben Hutchings, Staff Engineer, Solarflare
Not speaking for my employer; that's the marketing department's job.
They asked us to note that Solarflare product names are trademarked.
#3 lore raw

PATCH v3 1/1 ipbatch: fix use of 'ip netns exec'

From: JunweiZhang <[email protected]>

execvp() does not return when the command succeed, hence all commands in the batch file after the line 'ip netns exec' are not executed.

Let's fork before calling execvp().

Example:

$ cat test.batch
netns add netns1
netns exec netns1 ip l
netns
$ ip -b test.batch
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: sit0: <NOARP> mtu 1480 qdisc noop state DOWN mode DEFAULT
    link/sit 0.0.0.0 brd 0.0.0.0

All command after 'netns exec' are never executed.

With the patch:

$ ip -b test.batch
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: sit0: <NOARP> mtu 1480 qdisc noop state DOWN mode DEFAULT
    link/sit 0.0.0.0 brd 0.0.0.0
netns1

Now, existing netns are displayed.

---

v3: be more verbose on error (fork(), waitpid())
when child fails, just exit to avoid double cleanning check WIFEXITED() and waitpid() before using WEXITSTATUS()

v2: keep exit status of the child
add an example in the commit log

 ip/ipnetns.c | 30 +++++++++++++++++++++++++++---
 1 file changed, 27 insertions(+), 3 deletions(-)
ip/ipnetns.c +27 -3
index fa2b681..474a296 100644
@@ -138,6 +138,7 @@ static int netns_exec(int argc, char **argv)
 	const char *name, *cmd;
 	char net_path[MAXPATHLEN];
 	int netns;
+	int pid, status;
 
 	if (argc < 1) {
 		fprintf(stderr, "No netns name specified\n");
@@ -185,9 +186,32 @@ static int netns_exec(int argc, char **argv)
 	/* Setup bind mounts for config files in /etc */
 	bind_etc(name);
 
-	if (execvp(cmd, argv + 1)  < 0)
-		fprintf(stderr, "exec of \"%s\" failed: %s\n",
-			cmd, strerror(errno));
+	pid = fork();
+	if (pid < 0) {
+		perror("fork");
+		return EXIT_FAILURE;
+	} else if (pid == 0) {
+		/* Child */
+		if (execvp(cmd, argv + 1)  < 0)
+			fprintf(stderr, "exec of \"%s\" failed: %s\n",
+				cmd, strerror(errno));
+		exit(EXIT_FAILURE);
+	}
+
+	/* Parent */
+	if (waitpid(pid, &status, 0) < 0) {
+		perror("waitpid");
+		return EXIT_FAILURE;
+	}
+
+	if (WIFEXITED(status)) {
+		/* ip must returns the status of the child, but do_cmd() will
+		 * add a minus to this returned value, so let's add another one
+		 * here to cancel it.
+		 */
+		return -WEXITSTATUS(status);
+	}
+
 	return EXIT_FAILURE;
 }
 
Signature · 1 line
1.8.2.1
#4 lore raw
Quoted 49 lines · Nicolas Dichtel

From: JunweiZhang <[email protected]>

execvp() does not return when the command succeed, hence all commands in the batch file after the line 'ip netns exec' are not executed.

Let's fork before calling execvp().

Example:

$ cat test.batch
netns add netns1
netns exec netns1 ip l
netns
$ ip -b test.batch
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: sit0: <NOARP> mtu 1480 qdisc noop state DOWN mode DEFAULT
    link/sit 0.0.0.0 brd 0.0.0.0

All command after 'netns exec' are never executed.

With the patch:

$ ip -b test.batch
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: sit0: <NOARP> mtu 1480 qdisc noop state DOWN mode DEFAULT
    link/sit 0.0.0.0 brd 0.0.0.0
netns1

Now, existing netns are displayed.

---

v3: be more verbose on error (fork(), waitpid())
when child fails, just exit to avoid double cleanning check WIFEXITED() and waitpid() before using WEXITSTATUS()

v2: keep exit status of the child
add an example in the commit log

 ip/ipnetns.c | 30 +++++++++++++++++++++++++++---
 1 file changed, 27 insertions(+), 3 deletions(-)
ip/ipnetns.c +0 -0
index fa2b681..474a296 100644

ip/ipnetns.c static int netns_exec(int argc, char **argv) · line 141

Quoted 4 lines
ip/ipnetns.c +1 -0
@@ -138,6 +138,7 @@ static int netns_exec(int argc, char **argv)
 	const char *name, *cmd;
 	char net_path[MAXPATHLEN];
 	int netns;
+	int pid, status;
	pid_t pid;
Quoted 10 lines
ip/ipnetns.c +0 -3
 	if (argc < 1) {
 		fprintf(stderr, "No netns name specified\n");
@@ -185,9 +186,32 @@ static int netns_exec(int argc, char **argv)
 	/* Setup bind mounts for config files in /etc */
 	bind_etc(name);
 
-	if (execvp(cmd, argv + 1)  < 0)
-		fprintf(stderr, "exec of \"%s\" failed: %s\n",
-			cmd, strerror(errno));

ip/ipnetns.c static int netns_exec(int argc, char **argv) · line 198

Quoted 10 lines
ip/ipnetns.c +10 -0
+	pid = fork();
+	if (pid < 0) {
+		perror("fork");
+		return EXIT_FAILURE;
+	} else if (pid == 0) {
+		/* Child */
+		if (execvp(cmd, argv + 1)  < 0)
+			fprintf(stderr, "exec of \"%s\" failed: %s\n",
+				cmd, strerror(errno));
+		exit(EXIT_FAILURE);
	_exit(EXIT_FAILURE);

Or else you could have buffered data in stdout buffer, and it will be emitted twice.

#5 lore raw

PATCH v4 1/1 ipbatch: fix use of 'ip netns exec'

From: JunweiZhang <[email protected]>

execvp() does not return when the command succeed, hence all commands in the batch file after the line 'ip netns exec' are not executed.

Let's fork before calling execvp() if batch mode is used..

Example:

$ cat test.batch
netns add netns1
netns exec netns1 ip l
netns
$ ip -b test.batch
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: sit0: <NOARP> mtu 1480 qdisc noop state DOWN mode DEFAULT
    link/sit 0.0.0.0 brd 0.0.0.0

All command after 'netns exec' are never executed.

With the patch:

$ ip -b test.batch
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: sit0: <NOARP> mtu 1480 qdisc noop state DOWN mode DEFAULT
    link/sit 0.0.0.0 brd 0.0.0.0
netns1

Now, existing netns are displayed.

---

v4: use pid_t type for pid
use _exit() instead of exit()
fork only if batch is used

v3: be more verbose on error (fork(), waitpid())
when child fails, just exit to avoid double cleanning check WIFEXITED() and waitpid() before using WEXITSTATUS()

v2: keep exit status of the child
add an example in the commit log

 ip/ipnetns.c | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
ip/ipnetns.c +37 -0
index fa2b681..cdc3101 100644
@@ -42,6 +42,7 @@
 #define MS_SHARED	(1 << 20)
 #endif
 
+extern char *batch_file;
 
 #ifndef HAVE_SETNS
 static int setns(int fd, int nstype)
@@ -185,6 +186,42 @@ static int netns_exec(int argc, char **argv)
 	/* Setup bind mounts for config files in /etc */
 	bind_etc(name);
 
+	if (batch_file) {
+		int status;
+		pid_t pid;
+
+		pid = fork();
+		if (pid < 0) {
+			perror("fork");
+			return EXIT_FAILURE;
+		}
+
+		if (pid == 0) {
+			/* Child */
+			if (execvp(cmd, argv + 1)  < 0)
+				fprintf(stderr, "exec of \"%s\" failed: %s\n",
+					cmd, strerror(errno));
+
+			_exit(EXIT_FAILURE);
+		}
+
+		/* Parent */
+		if (waitpid(pid, &status, 0) < 0) {
+			perror("waitpid");
+			return EXIT_FAILURE;
+		}
+
+		if (WIFEXITED(status)) {
+			/* ip must returns the status of the child, but do_cmd()
+			 * will add a minus to this returned value, so let's add
+			 * another one here to cancel it.
+			 */
+			return -WEXITSTATUS(status);
+		}
+
+		return EXIT_FAILURE;
+	}
+
 	if (execvp(cmd, argv + 1)  < 0)
 		fprintf(stderr, "exec of \"%s\" failed: %s\n",
 			cmd, strerror(errno));
Signature · 1 line
1.8.2.1
#6 lore raw

On Tue, 2013-07-09 at 17:55 +0200, Nicolas Dichtel wrote:
[...]

ip/ipnetns.c line 45

Quoted 9 lines
ip/ipnetns.c +1 -0
index fa2b681..cdc3101 100644
@@ -42,6 +42,7 @@
 #define MS_SHARED	(1 << 20)
 #endif
 
+extern char *batch_file;

Should be declared in a header file.

Quoted 4 lines
ip/ipnetns.c +0 -0
 #ifndef HAVE_SETNS
 static int setns(int fd, int nstype)
@@ -185,6 +186,42 @@ static int netns_exec(int argc, char **argv)
 	/* Setup bind mounts for config files in /etc */

ip/ipnetns.c static int netns_exec(int argc, char **argv) · line 196

Quoted 10 lines
ip/ipnetns.c +8 -0
 	bind_etc(name);
 
+	if (batch_file) {
+		int status;
+		pid_t pid;
+
+		pid = fork();
+		if (pid < 0) {
+			perror("fork");
+			return EXIT_FAILURE;

return -1;

Quoted 5 lines
ip/ipnetns.c +5 -0
+		}
+
+		if (pid == 0) {
+			/* Child */
+			if (execvp(cmd, argv + 1)  < 0)

ip/ipnetns.c static int netns_exec(int argc, char **argv) · line 211

Quoted 10 lines
ip/ipnetns.c +10 -0
+				fprintf(stderr, "exec of \"%s\" failed: %s\n",
+					cmd, strerror(errno));
+
+			_exit(EXIT_FAILURE);
+		}
+
+		/* Parent */
+		if (waitpid(pid, &status, 0) < 0) {
+			perror("waitpid");
+			return EXIT_FAILURE;

return -1;

ip/ipnetns.c static int netns_exec(int argc, char **argv) · line 222

Quoted 11 lines
ip/ipnetns.c +11 -0
+		}
+
+		if (WIFEXITED(status)) {
+			/* ip must returns the status of the child, but do_cmd()
+			 * will add a minus to this returned value, so let's add
+			 * another one here to cancel it.
+			 */
+			return -WEXITSTATUS(status);
+		}
+
+		return EXIT_FAILURE;

return -1;

Quoted 5 lines
ip/ipnetns.c +2 -0
+	}
+
 	if (execvp(cmd, argv + 1)  < 0)
 		fprintf(stderr, "exec of \"%s\" failed: %s\n",
 			cmd, strerror(errno));
Signature · 3 lines
Ben Hutchings, Staff Engineer, Solarflare
Not speaking for my employer; that's the marketing department's job.
They asked us to note that Solarflare product names are trademarked.
#7 lore raw
Quoted 11 lines · Ben Hutchings

On Tue, 2013-07-09 at 17:55 +0200, Nicolas Dichtel wrote:
[...]

ip/ipnetns.c line 45

Quoted 9 lines
ip/ipnetns.c +1 -0
index fa2b681..cdc3101 100644
@@ -42,6 +42,7 @@
  #define MS_SHARED	(1 << 20)
  #endif
 
+extern char *batch_file;

I let Stephen choose, he suggest me this.

Quoted 9 lines

Should be declared in a header file.

Quoted 6 lines
ip/ipnetns.c +0 -0
  #ifndef HAVE_SETNS
  static int setns(int fd, int nstype)
@@ -185,6 +186,42 @@ static int netns_exec(int argc, char **argv)
  	/* Setup bind mounts for config files in /etc */
  	bind_etc(name);
 
Quoted 10 lines

ip/ipnetns.c static int netns_exec(int argc, char **argv) · line 196

Quoted 8 lines
ip/ipnetns.c +8 -0
+	if (batch_file) {
+		int status;
+		pid_t pid;
+
+		pid = fork();
+		if (pid < 0) {
+			perror("fork");
+			return EXIT_FAILURE;

return -1;

man exit says:
The use of EXIT_SUCCESS and EXIT_FAILURE is slightly more portable (to non-UNIX environments) than the use of 0 and some nonzero value like 1 or -1.

And in fact, it was so before my patch.

#8 lore raw
Quoted 33 lines · Nicolas Dichtel
Quoted 11 lines · Ben Hutchings

On Tue, 2013-07-09 at 17:55 +0200, Nicolas Dichtel wrote:
[...]

ip/ipnetns.c line 45

Quoted 9 lines
ip/ipnetns.c +1 -0
index fa2b681..cdc3101 100644
@@ -42,6 +42,7 @@
  #define MS_SHARED	(1 << 20)
  #endif
 
+extern char *batch_file;

I let Stephen choose, he suggest me this.

Quoted 9 lines

Should be declared in a header file.

Quoted 6 lines
ip/ipnetns.c +0 -0
  #ifndef HAVE_SETNS
  static int setns(int fd, int nstype)
@@ -185,6 +186,42 @@ static int netns_exec(int argc, char **argv)
  	/* Setup bind mounts for config files in /etc */
  	bind_etc(name);
 
Quoted 10 lines

ip/ipnetns.c static int netns_exec(int argc, char **argv) · line 196

Quoted 8 lines
ip/ipnetns.c +8 -0
+	if (batch_file) {
+		int status;
+		pid_t pid;
+
+		pid = fork();
+		if (pid < 0) {
+			perror("fork");
+			return EXIT_FAILURE;

return -1;

Quoted 5 lines

man exit says:
The use of EXIT_SUCCESS and EXIT_FAILURE is slightly more portable (to non-UNIX environments) than the use of 0 and some nonzero value like 1 or -1.

And in fact, it was so before my patch.

As your own earlier comment says, do_cmd() negates the return value.

Ben.

Signature · 3 lines
Ben Hutchings, Staff Engineer, Solarflare
Not speaking for my employer; that's the marketing department's job.
They asked us to note that Solarflare product names are trademarked.
#9 lore raw
Quoted 34 lines · Ben Hutchings
Quoted 33 lines · Nicolas Dichtel
Quoted 11 lines · Ben Hutchings

On Tue, 2013-07-09 at 17:55 +0200, Nicolas Dichtel wrote:
[...]

ip/ipnetns.c line 45

Quoted 9 lines
ip/ipnetns.c +1 -0
index fa2b681..cdc3101 100644
@@ -42,6 +42,7 @@
   #define MS_SHARED	(1 << 20)
   #endif
 
+extern char *batch_file;

I let Stephen choose, he suggest me this.

Quoted 19 lines

Should be declared in a header file.

Quoted 4 lines
ip/ipnetns.c +0 -0
   #ifndef HAVE_SETNS
   static int setns(int fd, int nstype)
@@ -185,6 +186,42 @@ static int netns_exec(int argc, char **argv)
   	/* Setup bind mounts for config files in /etc */

ip/ipnetns.c static int netns_exec(int argc, char **argv) · line 196

Quoted 10 lines
ip/ipnetns.c +8 -0
   	bind_etc(name);
 
+	if (batch_file) {
+		int status;
+		pid_t pid;
+
+		pid = fork();
+		if (pid < 0) {
+			perror("fork");
+			return EXIT_FAILURE;

return -1;

Quoted 7 lines
Quoted 5 lines

man exit says:
The use of EXIT_SUCCESS and EXIT_FAILURE is slightly more portable (to non-UNIX environments) than the use of 0 and some nonzero value like 1 or -1.

And in fact, it was so before my patch.

As your own earlier comment says, do_cmd() negates the return value.

Yes, but EXIT_FAILURE is used everywhere in ip/ipnetns.c.

Stephen, should I send a patch to replace use of EXIT_FAILURE by -1 in ipnetns.c?