]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
ip netns: Identify netns for the current process
authorvadimk <vadim4j@gmail.com>
Fri, 7 Nov 2014 16:25:30 +0000 (18:25 +0200)
committerStephen Hemminger <shemming@brocade.com>
Sat, 29 Nov 2014 19:19:11 +0000 (11:19 -0800)
As 'ip' util will share the same netns from the caller
process then we can just look at /proc/self/.. to show
the netns of the current process by:

    ip netns id

Signed-off-by: Vadim Kochan <vadim4j@gmail.com>
ip/ipnetns.c
man/man8/ip-netns.8

index 90a496f7ad5acf3c841fab7035945053da282765..1c8aa029073eda73a6093ff1c806b061a1ced64e 100644 (file)
@@ -59,7 +59,7 @@ static int usage(void)
        fprintf(stderr, "Usage: ip netns list\n");
        fprintf(stderr, "       ip netns add NAME\n");
        fprintf(stderr, "       ip netns delete NAME\n");
-       fprintf(stderr, "       ip netns identify PID\n");
+       fprintf(stderr, "       ip netns identify [PID]\n");
        fprintf(stderr, "       ip netns pids NAME\n");
        fprintf(stderr, "       ip netns exec NAME cmd ...\n");
        fprintf(stderr, "       ip netns monitor\n");
@@ -299,19 +299,17 @@ static int netns_identify(int argc, char **argv)
        struct dirent *entry;
 
        if (argc < 1) {
-               fprintf(stderr, "No pid specified\n");
-               return -1;
-       }
-       if (argc > 1) {
+               pidstr = "self";
+       } else if (argc > 1) {
                fprintf(stderr, "extra arguments specified\n");
                return -1;
-       }
-       pidstr = argv[0];
-
-       if (!is_pid(pidstr)) {
-               fprintf(stderr, "Specified string '%s' is not a pid\n",
-                       pidstr);
-               return -1;
+       } else {
+               pidstr = argv[0];
+               if (!is_pid(pidstr)) {
+                       fprintf(stderr, "Specified string '%s' is not a pid\n",
+                                       pidstr);
+                       return -1;
+               }
        }
 
        snprintf(net_path, sizeof(net_path), "/proc/%s/ns/net", pidstr);
index 6aa6e934f6bacb374d3c283b170f58895a73d728..74343ed6b640f76c0a1e37b9377a3a41ce6e876b 100644 (file)
@@ -21,7 +21,7 @@ ip-netns \- process network namespace management
 
 .ti -8
 .BR "ip netns identify"
-.I PID
+.RI "[ " PID " ]"
 
 .ti -8
 .BR "ip netns pids"
@@ -85,10 +85,11 @@ persists until it has no more users.  ip netns delete may fail if
 the mount point is in use in another mount namespace.
 
 .TP
-.B ip netns identify PID - Report network namespaces names for process
+.B ip netns identify [PID] - Report network namespaces names for process
 .sp
 This command walks through /var/run/netns and finds all the network
-namespace names for network namespace of the specified process.
+namespace names for network namespace of the specified process, if PID is
+not specified then the current process will be used.
 
 .TP
 .B ip netns pids NAME - Report processes in the named network namespace