diff mbox

ipnetns: fix exec for netns not in NETNS_RUN_DIR

Message ID 1419611232-14206-1-git-send-email-shahar@stratoscale.com
State Rejected, archived
Delegated to: stephen hemminger
Headers show

Commit Message

Shahar Lev Dec. 26, 2014, 4:27 p.m. UTC
Enabling "ip netns exec" to be run with a net namespace
specified by a file path rather than a filename under /var/run/nets.

Signed-off-by: Shahar Lev <shahar@stratoscale.com>
---
 ip/ipnetns.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

Comments

Stephen Hemminger Dec. 27, 2014, 6:05 p.m. UTC | #1
On Fri, 26 Dec 2014 18:27:12 +0200
Shahar Lev <shahar@stratoscale.com> wrote:

> Enabling "ip netns exec" to be run with a net namespace
> specified by a file path rather than a filename under /var/run/nets.
> 

This breaks existing users since you now require full pathname.

--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Shahar Lev Dec. 29, 2014, 9:25 a.m. UTC | #2
>
> This breaks existing users since you now require full pathname.
>

Providing a full path is not a requirement.
If there's no dash ('/') in the parameter provided it defaults to
opening relative to NETNS_RUN_DIR (the existing behavior).
So this is just an extension.
--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/ip/ipnetns.c b/ip/ipnetns.c
index 1c8aa02..5310d0c 100644
--- a/ip/ipnetns.c
+++ b/ip/ipnetns.c
@@ -66,7 +66,7 @@  static int usage(void)
 	exit(-1);
 }
 
-int get_netns_fd(const char *name)
+static int get_netns_fd_flags(const char *name, int flags)
 {
 	char pathbuf[MAXPATHLEN];
 	const char *path, *ptr;
@@ -78,7 +78,12 @@  int get_netns_fd(const char *name)
 			NETNS_RUN_DIR, name );
 		path = pathbuf;
 	}
-	return open(path, O_RDONLY);
+	return open(path, flags);
+}
+
+int get_netns_fd(const char *name)
+{
+	return get_netns_fd_flags(name, O_RDONLY);
 }
 
 static int netns_list(int argc, char **argv)
@@ -135,7 +140,6 @@  static int netns_exec(int argc, char **argv)
 	 * aware, and execute a program in that environment.
 	 */
 	const char *name, *cmd;
-	char net_path[MAXPATHLEN];
 	int netns;
 
 	if (argc < 1) {
@@ -149,8 +153,7 @@  static int netns_exec(int argc, char **argv)
 
 	name = argv[0];
 	cmd = argv[1];
-	snprintf(net_path, sizeof(net_path), "%s/%s", NETNS_RUN_DIR, name);
-	netns = open(net_path, O_RDONLY | O_CLOEXEC);
+	netns = get_netns_fd_flags(name, O_RDONLY | O_CLOEXEC);
 	if (netns < 0) {
 		fprintf(stderr, "Cannot open network namespace \"%s\": %s\n",
 			name, strerror(errno));