blob: 1c8aa029073eda73a6093ff1c806b061a1ced64e [file] [log] [blame]
Eric W. Biederman0dc34c72011-07-13 09:48:26 -07001#define _ATFILE_SOURCE
2#include <sys/types.h>
3#include <sys/stat.h>
4#include <sys/wait.h>
5#include <sys/inotify.h>
6#include <sys/mount.h>
7#include <sys/param.h>
8#include <sys/syscall.h>
9#include <stdio.h>
10#include <string.h>
11#include <sched.h>
12#include <fcntl.h>
13#include <dirent.h>
14#include <errno.h>
15#include <unistd.h>
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +000016#include <ctype.h>
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070017
18#include "utils.h"
19#include "ip_common.h"
20
21#define NETNS_RUN_DIR "/var/run/netns"
22#define NETNS_ETC_DIR "/etc/netns"
23
24#ifndef CLONE_NEWNET
25#define CLONE_NEWNET 0x40000000 /* New network namespace (lo, device, names sockets, etc) */
26#endif
27
28#ifndef MNT_DETACH
29#define MNT_DETACH 0x00000002 /* Just detach from the tree */
30#endif /* MNT_DETACH */
31
Stephen Hemminger03fdb012013-04-17 13:33:26 -070032/* sys/mount.h may be out too old to have these */
33#ifndef MS_REC
34#define MS_REC 16384
35#endif
36
37#ifndef MS_SLAVE
38#define MS_SLAVE (1 << 19)
39#endif
40
41#ifndef MS_SHARED
42#define MS_SHARED (1 << 20)
43#endif
44
Eric W. Biederman2e8a07f2011-07-15 14:26:59 +000045#ifndef HAVE_SETNS
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070046static int setns(int fd, int nstype)
47{
48#ifdef __NR_setns
49 return syscall(__NR_setns, fd, nstype);
50#else
51 errno = ENOSYS;
52 return -1;
53#endif
54}
Eric W. Biederman2e8a07f2011-07-15 14:26:59 +000055#endif /* HAVE_SETNS */
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070056
Eric W. Biederman8e2d47d2013-01-17 14:46:09 +000057static int usage(void)
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070058{
59 fprintf(stderr, "Usage: ip netns list\n");
60 fprintf(stderr, " ip netns add NAME\n");
61 fprintf(stderr, " ip netns delete NAME\n");
vadimk0948adc2014-11-07 18:25:30 +020062 fprintf(stderr, " ip netns identify [PID]\n");
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +000063 fprintf(stderr, " ip netns pids NAME\n");
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070064 fprintf(stderr, " ip netns exec NAME cmd ...\n");
65 fprintf(stderr, " ip netns monitor\n");
Stephen Hemmingera05f6512013-07-12 08:43:23 -070066 exit(-1);
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070067}
68
69int get_netns_fd(const char *name)
70{
71 char pathbuf[MAXPATHLEN];
72 const char *path, *ptr;
73
74 path = name;
75 ptr = strchr(name, '/');
76 if (!ptr) {
77 snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
78 NETNS_RUN_DIR, name );
79 path = pathbuf;
80 }
81 return open(path, O_RDONLY);
82}
83
84static int netns_list(int argc, char **argv)
85{
86 struct dirent *entry;
87 DIR *dir;
88
89 dir = opendir(NETNS_RUN_DIR);
90 if (!dir)
Stephen Hemmingera05f6512013-07-12 08:43:23 -070091 return 0;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070092
93 while ((entry = readdir(dir)) != NULL) {
94 if (strcmp(entry->d_name, ".") == 0)
95 continue;
96 if (strcmp(entry->d_name, "..") == 0)
97 continue;
98 printf("%s\n", entry->d_name);
99 }
100 closedir(dir);
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700101 return 0;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700102}
103
104static void bind_etc(const char *name)
105{
106 char etc_netns_path[MAXPATHLEN];
107 char netns_name[MAXPATHLEN];
108 char etc_name[MAXPATHLEN];
109 struct dirent *entry;
110 DIR *dir;
111
112 snprintf(etc_netns_path, sizeof(etc_netns_path), "%s/%s", NETNS_ETC_DIR, name);
113 dir = opendir(etc_netns_path);
114 if (!dir)
115 return;
116
117 while ((entry = readdir(dir)) != NULL) {
118 if (strcmp(entry->d_name, ".") == 0)
119 continue;
120 if (strcmp(entry->d_name, "..") == 0)
121 continue;
122 snprintf(netns_name, sizeof(netns_name), "%s/%s", etc_netns_path, entry->d_name);
123 snprintf(etc_name, sizeof(etc_name), "/etc/%s", entry->d_name);
124 if (mount(netns_name, etc_name, "none", MS_BIND, NULL) < 0) {
125 fprintf(stderr, "Bind %s -> %s failed: %s\n",
126 netns_name, etc_name, strerror(errno));
127 }
128 }
129 closedir(dir);
130}
131
132static int netns_exec(int argc, char **argv)
133{
134 /* Setup the proper environment for apps that are not netns
135 * aware, and execute a program in that environment.
136 */
137 const char *name, *cmd;
138 char net_path[MAXPATHLEN];
139 int netns;
140
141 if (argc < 1) {
142 fprintf(stderr, "No netns name specified\n");
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700143 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700144 }
145 if (argc < 2) {
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000146 fprintf(stderr, "No command specified\n");
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700147 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700148 }
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700149
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700150 name = argv[0];
151 cmd = argv[1];
152 snprintf(net_path, sizeof(net_path), "%s/%s", NETNS_RUN_DIR, name);
Andrey Vaginbcb9d402013-06-04 12:01:14 +0400153 netns = open(net_path, O_RDONLY | O_CLOEXEC);
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700154 if (netns < 0) {
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000155 fprintf(stderr, "Cannot open network namespace \"%s\": %s\n",
Eric W. Biederman4395d482013-01-17 14:46:43 +0000156 name, strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700157 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700158 }
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700159
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700160 if (setns(netns, CLONE_NEWNET) < 0) {
Vasily Averin31962442014-06-22 23:11:28 +0400161 fprintf(stderr, "setting the network namespace \"%s\" failed: %s\n",
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000162 name, strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700163 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700164 }
165
166 if (unshare(CLONE_NEWNS) < 0) {
167 fprintf(stderr, "unshare failed: %s\n", strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700168 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700169 }
Stephen Hemmingerd259f032013-08-04 15:00:56 -0700170 /* Don't let any mounts propagate back to the parent */
Eric W. Biederman144e6ce2013-01-17 14:45:33 +0000171 if (mount("", "/", "none", MS_SLAVE | MS_REC, NULL)) {
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000172 fprintf(stderr, "\"mount --make-rslave /\" failed: %s\n",
Eric W. Biederman144e6ce2013-01-17 14:45:33 +0000173 strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700174 return -1;
Eric W. Biederman144e6ce2013-01-17 14:45:33 +0000175 }
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700176 /* Mount a version of /sys that describes the network namespace */
177 if (umount2("/sys", MNT_DETACH) < 0) {
178 fprintf(stderr, "umount of /sys failed: %s\n", strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700179 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700180 }
181 if (mount(name, "/sys", "sysfs", 0, NULL) < 0) {
182 fprintf(stderr, "mount of /sys failed: %s\n",strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700183 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700184 }
185
186 /* Setup bind mounts for config files in /etc */
187 bind_etc(name);
188
JunweiZhang95592b42013-07-09 08:55:20 -0700189 fflush(stdout);
190
Stephen Hemmingera3aa47a2013-07-16 10:04:05 -0700191 if (batch_mode) {
JunweiZhang95592b42013-07-09 08:55:20 -0700192 int status;
193 pid_t pid;
194
195 pid = fork();
196 if (pid < 0) {
197 perror("fork");
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700198 exit(1);
JunweiZhang95592b42013-07-09 08:55:20 -0700199 }
200
201 if (pid != 0) {
202 /* Parent */
203 if (waitpid(pid, &status, 0) < 0) {
204 perror("waitpid");
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700205 exit(1);
JunweiZhang95592b42013-07-09 08:55:20 -0700206 }
207
Nicolas Dichtel3c61c012013-08-29 14:29:07 +0200208 if (WIFEXITED(status)) {
209 /* ip must return the status of the child,
210 * but do_cmd() will add a minus to this,
211 * so let's add another one here to cancel it.
212 */
213 return -WEXITSTATUS(status);
214 }
JunweiZhang95592b42013-07-09 08:55:20 -0700215
Nicolas Dichtel3c61c012013-08-29 14:29:07 +0200216 exit(1);
JunweiZhang95592b42013-07-09 08:55:20 -0700217 }
218 }
219
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700220 if (execvp(cmd, argv + 1) < 0)
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000221 fprintf(stderr, "exec of \"%s\" failed: %s\n",
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700222 cmd, strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700223 _exit(1);
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700224}
225
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000226static int is_pid(const char *str)
227{
228 int ch;
229 for (; (ch = *str); str++) {
230 if (!isdigit(ch))
231 return 0;
232 }
233 return 1;
234}
235
236static int netns_pids(int argc, char **argv)
237{
238 const char *name;
239 char net_path[MAXPATHLEN];
240 int netns;
241 struct stat netst;
242 DIR *dir;
243 struct dirent *entry;
244
245 if (argc < 1) {
246 fprintf(stderr, "No netns name specified\n");
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700247 return -1;
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000248 }
249 if (argc > 1) {
250 fprintf(stderr, "extra arguments specified\n");
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700251 return -1;
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000252 }
253
254 name = argv[0];
255 snprintf(net_path, sizeof(net_path), "%s/%s", NETNS_RUN_DIR, name);
256 netns = open(net_path, O_RDONLY);
257 if (netns < 0) {
258 fprintf(stderr, "Cannot open network namespace: %s\n",
259 strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700260 return -1;
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000261 }
262 if (fstat(netns, &netst) < 0) {
263 fprintf(stderr, "Stat of netns failed: %s\n",
264 strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700265 return -1;
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000266 }
267 dir = opendir("/proc/");
268 if (!dir) {
269 fprintf(stderr, "Open of /proc failed: %s\n",
270 strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700271 return -1;
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000272 }
273 while((entry = readdir(dir))) {
274 char pid_net_path[MAXPATHLEN];
275 struct stat st;
276 if (!is_pid(entry->d_name))
277 continue;
278 snprintf(pid_net_path, sizeof(pid_net_path), "/proc/%s/ns/net",
279 entry->d_name);
280 if (stat(pid_net_path, &st) != 0)
281 continue;
282 if ((st.st_dev == netst.st_dev) &&
283 (st.st_ino == netst.st_ino)) {
284 printf("%s\n", entry->d_name);
285 }
286 }
287 closedir(dir);
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700288 return 0;
Stephen Hemminger06125192014-02-17 10:55:31 -0800289
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000290}
291
292static int netns_identify(int argc, char **argv)
293{
294 const char *pidstr;
295 char net_path[MAXPATHLEN];
296 int netns;
297 struct stat netst;
298 DIR *dir;
299 struct dirent *entry;
300
301 if (argc < 1) {
vadimk0948adc2014-11-07 18:25:30 +0200302 pidstr = "self";
303 } else if (argc > 1) {
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000304 fprintf(stderr, "extra arguments specified\n");
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700305 return -1;
vadimk0948adc2014-11-07 18:25:30 +0200306 } else {
307 pidstr = argv[0];
308 if (!is_pid(pidstr)) {
309 fprintf(stderr, "Specified string '%s' is not a pid\n",
310 pidstr);
311 return -1;
312 }
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000313 }
314
315 snprintf(net_path, sizeof(net_path), "/proc/%s/ns/net", pidstr);
316 netns = open(net_path, O_RDONLY);
317 if (netns < 0) {
318 fprintf(stderr, "Cannot open network namespace: %s\n",
319 strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700320 return -1;
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000321 }
322 if (fstat(netns, &netst) < 0) {
323 fprintf(stderr, "Stat of netns failed: %s\n",
324 strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700325 return -1;
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000326 }
327 dir = opendir(NETNS_RUN_DIR);
328 if (!dir) {
329 /* Succeed treat a missing directory as an empty directory */
330 if (errno == ENOENT)
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700331 return 0;
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000332
333 fprintf(stderr, "Failed to open directory %s:%s\n",
334 NETNS_RUN_DIR, strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700335 return -1;
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000336 }
337
338 while((entry = readdir(dir))) {
339 char name_path[MAXPATHLEN];
340 struct stat st;
341
342 if (strcmp(entry->d_name, ".") == 0)
343 continue;
344 if (strcmp(entry->d_name, "..") == 0)
345 continue;
346
347 snprintf(name_path, sizeof(name_path), "%s/%s", NETNS_RUN_DIR,
348 entry->d_name);
349
350 if (stat(name_path, &st) != 0)
351 continue;
352
353 if ((st.st_dev == netst.st_dev) &&
354 (st.st_ino == netst.st_ino)) {
355 printf("%s\n", entry->d_name);
356 }
357 }
358 closedir(dir);
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700359 return 0;
Stephen Hemminger06125192014-02-17 10:55:31 -0800360
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000361}
362
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700363static int netns_delete(int argc, char **argv)
364{
365 const char *name;
366 char netns_path[MAXPATHLEN];
367
368 if (argc < 1) {
369 fprintf(stderr, "No netns name specified\n");
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700370 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700371 }
372
373 name = argv[0];
374 snprintf(netns_path, sizeof(netns_path), "%s/%s", NETNS_RUN_DIR, name);
375 umount2(netns_path, MNT_DETACH);
376 if (unlink(netns_path) < 0) {
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000377 fprintf(stderr, "Cannot remove namespace file \"%s\": %s\n",
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700378 netns_path, strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700379 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700380 }
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700381 return 0;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700382}
383
vadimkc1cbb182014-08-31 22:45:29 +0300384static int create_netns_dir(void)
385{
386 /* Create the base netns directory if it doesn't exist */
387 if (mkdir(NETNS_RUN_DIR, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)) {
388 if (errno != EEXIST) {
389 fprintf(stderr, "mkdir %s failed: %s\n",
390 NETNS_RUN_DIR, strerror(errno));
391 return -1;
392 }
393 }
394
395 return 0;
396}
397
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700398static int netns_add(int argc, char **argv)
399{
400 /* This function creates a new network namespace and
401 * a new mount namespace and bind them into a well known
402 * location in the filesystem based on the name provided.
403 *
404 * The mount namespace is created so that any necessary
405 * userspace tweaks like remounting /sys, or bind mounting
406 * a new /etc/resolv.conf can be shared between uers.
407 */
408 char netns_path[MAXPATHLEN];
409 const char *name;
Eric W. Biederman223f4d82011-07-15 14:29:41 +0000410 int fd;
Eric W. Biederman58a3e822013-01-17 14:47:18 +0000411 int made_netns_run_dir_mount = 0;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700412
413 if (argc < 1) {
414 fprintf(stderr, "No netns name specified\n");
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700415 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700416 }
417 name = argv[0];
418
419 snprintf(netns_path, sizeof(netns_path), "%s/%s", NETNS_RUN_DIR, name);
420
vadimkc1cbb182014-08-31 22:45:29 +0300421 if (create_netns_dir())
422 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700423
Stephen Hemmingerd259f032013-08-04 15:00:56 -0700424 /* Make it possible for network namespace mounts to propagate between
Eric W. Biederman58a3e822013-01-17 14:47:18 +0000425 * mount namespaces. This makes it likely that a unmounting a network
426 * namespace file in one namespace will unmount the network namespace
427 * file in all namespaces allowing the network namespace to be freed
428 * sooner.
429 */
430 while (mount("", NETNS_RUN_DIR, "none", MS_SHARED | MS_REC, NULL)) {
431 /* Fail unless we need to make the mount point */
432 if (errno != EINVAL || made_netns_run_dir_mount) {
433 fprintf(stderr, "mount --make-shared %s failed: %s\n",
434 NETNS_RUN_DIR, strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700435 return -1;
Eric W. Biederman58a3e822013-01-17 14:47:18 +0000436 }
437
438 /* Upgrade NETNS_RUN_DIR to a mount point */
439 if (mount(NETNS_RUN_DIR, NETNS_RUN_DIR, "none", MS_BIND, NULL)) {
440 fprintf(stderr, "mount --bind %s %s failed: %s\n",
441 NETNS_RUN_DIR, NETNS_RUN_DIR, strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700442 return -1;
Eric W. Biederman58a3e822013-01-17 14:47:18 +0000443 }
444 made_netns_run_dir_mount = 1;
445 }
446
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700447 /* Create the filesystem state */
Eric W. Biederman223f4d82011-07-15 14:29:41 +0000448 fd = open(netns_path, O_RDONLY|O_CREAT|O_EXCL, 0);
449 if (fd < 0) {
Mike Rapoport55713c82014-05-11 12:21:26 +0300450 fprintf(stderr, "Cannot create namespace file \"%s\": %s\n",
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700451 netns_path, strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700452 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700453 }
Eric W. Biederman223f4d82011-07-15 14:29:41 +0000454 close(fd);
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700455 if (unshare(CLONE_NEWNET) < 0) {
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000456 fprintf(stderr, "Failed to create a new network namespace \"%s\": %s\n",
457 name, strerror(errno));
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700458 goto out_delete;
459 }
460
461 /* Bind the netns last so I can watch for it */
462 if (mount("/proc/self/ns/net", netns_path, "none", MS_BIND, NULL) < 0) {
463 fprintf(stderr, "Bind /proc/self/ns/net -> %s failed: %s\n",
464 netns_path, strerror(errno));
465 goto out_delete;
466 }
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700467 return 0;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700468out_delete:
469 netns_delete(argc, argv);
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700470 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700471}
472
473
474static int netns_monitor(int argc, char **argv)
475{
476 char buf[4096];
477 struct inotify_event *event;
478 int fd;
479 fd = inotify_init();
480 if (fd < 0) {
481 fprintf(stderr, "inotify_init failed: %s\n",
482 strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700483 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700484 }
vadimkc1cbb182014-08-31 22:45:29 +0300485
486 if (create_netns_dir())
487 return -1;
488
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700489 if (inotify_add_watch(fd, NETNS_RUN_DIR, IN_CREATE | IN_DELETE) < 0) {
490 fprintf(stderr, "inotify_add_watch failed: %s\n",
491 strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700492 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700493 }
494 for(;;) {
495 ssize_t len = read(fd, buf, sizeof(buf));
496 if (len < 0) {
497 fprintf(stderr, "read failed: %s\n",
498 strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700499 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700500 }
501 for (event = (struct inotify_event *)buf;
502 (char *)event < &buf[len];
503 event = (struct inotify_event *)((char *)event + sizeof(*event) + event->len)) {
504 if (event->mask & IN_CREATE)
505 printf("add %s\n", event->name);
506 if (event->mask & IN_DELETE)
507 printf("delete %s\n", event->name);
508 }
509 }
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700510 return 0;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700511}
512
513int do_netns(int argc, char **argv)
514{
515 if (argc < 1)
516 return netns_list(0, NULL);
517
518 if ((matches(*argv, "list") == 0) || (matches(*argv, "show") == 0) ||
519 (matches(*argv, "lst") == 0))
520 return netns_list(argc-1, argv+1);
521
522 if (matches(*argv, "help") == 0)
Eric W. Biederman8e2d47d2013-01-17 14:46:09 +0000523 return usage();
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700524
525 if (matches(*argv, "add") == 0)
526 return netns_add(argc-1, argv+1);
527
528 if (matches(*argv, "delete") == 0)
529 return netns_delete(argc-1, argv+1);
530
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000531 if (matches(*argv, "identify") == 0)
532 return netns_identify(argc-1, argv+1);
533
534 if (matches(*argv, "pids") == 0)
535 return netns_pids(argc-1, argv+1);
536
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700537 if (matches(*argv, "exec") == 0)
538 return netns_exec(argc-1, argv+1);
539
540 if (matches(*argv, "monitor") == 0)
541 return netns_monitor(argc-1, argv+1);
542
543 fprintf(stderr, "Command \"%s\" is unknown, try \"ip netns help\".\n", *argv);
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700544 exit(-1);
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700545}