blob: 5b911f76d18e404aa5394fcfe741521afd820620 [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
JunweiZhang95592b42013-07-09 08:55:20 -070045extern char *batch_file;
Stephen Hemminger03fdb012013-04-17 13:33:26 -070046
Eric W. Biederman2e8a07f2011-07-15 14:26:59 +000047#ifndef HAVE_SETNS
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070048static int setns(int fd, int nstype)
49{
50#ifdef __NR_setns
51 return syscall(__NR_setns, fd, nstype);
52#else
53 errno = ENOSYS;
54 return -1;
55#endif
56}
Eric W. Biederman2e8a07f2011-07-15 14:26:59 +000057#endif /* HAVE_SETNS */
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070058
Eric W. Biederman8e2d47d2013-01-17 14:46:09 +000059static int usage(void)
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070060{
61 fprintf(stderr, "Usage: ip netns list\n");
62 fprintf(stderr, " ip netns add NAME\n");
63 fprintf(stderr, " ip netns delete NAME\n");
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +000064 fprintf(stderr, " ip netns identify PID\n");
65 fprintf(stderr, " ip netns pids NAME\n");
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070066 fprintf(stderr, " ip netns exec NAME cmd ...\n");
67 fprintf(stderr, " ip netns monitor\n");
Stephen Hemmingera05f6512013-07-12 08:43:23 -070068 exit(-1);
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070069}
70
71int get_netns_fd(const char *name)
72{
73 char pathbuf[MAXPATHLEN];
74 const char *path, *ptr;
75
76 path = name;
77 ptr = strchr(name, '/');
78 if (!ptr) {
79 snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
80 NETNS_RUN_DIR, name );
81 path = pathbuf;
82 }
83 return open(path, O_RDONLY);
84}
85
86static int netns_list(int argc, char **argv)
87{
88 struct dirent *entry;
89 DIR *dir;
90
91 dir = opendir(NETNS_RUN_DIR);
92 if (!dir)
Stephen Hemmingera05f6512013-07-12 08:43:23 -070093 return 0;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -070094
95 while ((entry = readdir(dir)) != NULL) {
96 if (strcmp(entry->d_name, ".") == 0)
97 continue;
98 if (strcmp(entry->d_name, "..") == 0)
99 continue;
100 printf("%s\n", entry->d_name);
101 }
102 closedir(dir);
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700103 return 0;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700104}
105
106static void bind_etc(const char *name)
107{
108 char etc_netns_path[MAXPATHLEN];
109 char netns_name[MAXPATHLEN];
110 char etc_name[MAXPATHLEN];
111 struct dirent *entry;
112 DIR *dir;
113
114 snprintf(etc_netns_path, sizeof(etc_netns_path), "%s/%s", NETNS_ETC_DIR, name);
115 dir = opendir(etc_netns_path);
116 if (!dir)
117 return;
118
119 while ((entry = readdir(dir)) != NULL) {
120 if (strcmp(entry->d_name, ".") == 0)
121 continue;
122 if (strcmp(entry->d_name, "..") == 0)
123 continue;
124 snprintf(netns_name, sizeof(netns_name), "%s/%s", etc_netns_path, entry->d_name);
125 snprintf(etc_name, sizeof(etc_name), "/etc/%s", entry->d_name);
126 if (mount(netns_name, etc_name, "none", MS_BIND, NULL) < 0) {
127 fprintf(stderr, "Bind %s -> %s failed: %s\n",
128 netns_name, etc_name, strerror(errno));
129 }
130 }
131 closedir(dir);
132}
133
134static int netns_exec(int argc, char **argv)
135{
136 /* Setup the proper environment for apps that are not netns
137 * aware, and execute a program in that environment.
138 */
139 const char *name, *cmd;
140 char net_path[MAXPATHLEN];
141 int netns;
142
143 if (argc < 1) {
144 fprintf(stderr, "No netns name specified\n");
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700145 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700146 }
147 if (argc < 2) {
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000148 fprintf(stderr, "No command specified\n");
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700149 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700150 }
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700151
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700152 name = argv[0];
153 cmd = argv[1];
154 snprintf(net_path, sizeof(net_path), "%s/%s", NETNS_RUN_DIR, name);
Andrey Vaginbcb9d402013-06-04 12:01:14 +0400155 netns = open(net_path, O_RDONLY | O_CLOEXEC);
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700156 if (netns < 0) {
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000157 fprintf(stderr, "Cannot open network namespace \"%s\": %s\n",
Eric W. Biederman4395d482013-01-17 14:46:43 +0000158 name, strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700159 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700160 }
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700161
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700162 if (setns(netns, CLONE_NEWNET) < 0) {
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000163 fprintf(stderr, "seting the network namespace \"%s\" failed: %s\n",
164 name, strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700165 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700166 }
167
168 if (unshare(CLONE_NEWNS) < 0) {
169 fprintf(stderr, "unshare failed: %s\n", strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700170 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700171 }
Eric W. Biederman144e6ce2013-01-17 14:45:33 +0000172 /* Don't let any mounts propogate back to the parent */
173 if (mount("", "/", "none", MS_SLAVE | MS_REC, NULL)) {
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000174 fprintf(stderr, "\"mount --make-rslave /\" failed: %s\n",
Eric W. Biederman144e6ce2013-01-17 14:45:33 +0000175 strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700176 return -1;
Eric W. Biederman144e6ce2013-01-17 14:45:33 +0000177 }
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700178 /* Mount a version of /sys that describes the network namespace */
179 if (umount2("/sys", MNT_DETACH) < 0) {
180 fprintf(stderr, "umount of /sys failed: %s\n", strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700181 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700182 }
183 if (mount(name, "/sys", "sysfs", 0, NULL) < 0) {
184 fprintf(stderr, "mount of /sys failed: %s\n",strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700185 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700186 }
187
188 /* Setup bind mounts for config files in /etc */
189 bind_etc(name);
190
JunweiZhang95592b42013-07-09 08:55:20 -0700191 fflush(stdout);
192
193 if (batch_file) {
194 int status;
195 pid_t pid;
196
197 pid = fork();
198 if (pid < 0) {
199 perror("fork");
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700200 exit(1);
JunweiZhang95592b42013-07-09 08:55:20 -0700201 }
202
203 if (pid != 0) {
204 /* Parent */
205 if (waitpid(pid, &status, 0) < 0) {
206 perror("waitpid");
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700207 exit(1);
JunweiZhang95592b42013-07-09 08:55:20 -0700208 }
209
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700210 /* If child failed, propogate status */
211 if (WIFEXITED(status))
212 exit(WEXITSTATUS(status));
JunweiZhang95592b42013-07-09 08:55:20 -0700213
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700214 return 0;
JunweiZhang95592b42013-07-09 08:55:20 -0700215 }
216 }
217
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700218 if (execvp(cmd, argv + 1) < 0)
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000219 fprintf(stderr, "exec of \"%s\" failed: %s\n",
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700220 cmd, strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700221 _exit(1);
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700222}
223
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000224static int is_pid(const char *str)
225{
226 int ch;
227 for (; (ch = *str); str++) {
228 if (!isdigit(ch))
229 return 0;
230 }
231 return 1;
232}
233
234static int netns_pids(int argc, char **argv)
235{
236 const char *name;
237 char net_path[MAXPATHLEN];
238 int netns;
239 struct stat netst;
240 DIR *dir;
241 struct dirent *entry;
242
243 if (argc < 1) {
244 fprintf(stderr, "No netns name specified\n");
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700245 return -1;
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000246 }
247 if (argc > 1) {
248 fprintf(stderr, "extra arguments specified\n");
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700249 return -1;
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000250 }
251
252 name = argv[0];
253 snprintf(net_path, sizeof(net_path), "%s/%s", NETNS_RUN_DIR, name);
254 netns = open(net_path, O_RDONLY);
255 if (netns < 0) {
256 fprintf(stderr, "Cannot open network namespace: %s\n",
257 strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700258 return -1;
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000259 }
260 if (fstat(netns, &netst) < 0) {
261 fprintf(stderr, "Stat of netns failed: %s\n",
262 strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700263 return -1;
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000264 }
265 dir = opendir("/proc/");
266 if (!dir) {
267 fprintf(stderr, "Open of /proc failed: %s\n",
268 strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700269 return -1;
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000270 }
271 while((entry = readdir(dir))) {
272 char pid_net_path[MAXPATHLEN];
273 struct stat st;
274 if (!is_pid(entry->d_name))
275 continue;
276 snprintf(pid_net_path, sizeof(pid_net_path), "/proc/%s/ns/net",
277 entry->d_name);
278 if (stat(pid_net_path, &st) != 0)
279 continue;
280 if ((st.st_dev == netst.st_dev) &&
281 (st.st_ino == netst.st_ino)) {
282 printf("%s\n", entry->d_name);
283 }
284 }
285 closedir(dir);
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700286 return 0;
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000287
288}
289
290static int netns_identify(int argc, char **argv)
291{
292 const char *pidstr;
293 char net_path[MAXPATHLEN];
294 int netns;
295 struct stat netst;
296 DIR *dir;
297 struct dirent *entry;
298
299 if (argc < 1) {
300 fprintf(stderr, "No pid specified\n");
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700301 return -1;
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000302 }
303 if (argc > 1) {
304 fprintf(stderr, "extra arguments specified\n");
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700305 return -1;
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000306 }
307 pidstr = argv[0];
308
309 if (!is_pid(pidstr)) {
310 fprintf(stderr, "Specified string '%s' is not a pid\n",
311 pidstr);
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700312 return -1;
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;
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000360
361}
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
384static int netns_add(int argc, char **argv)
385{
386 /* This function creates a new network namespace and
387 * a new mount namespace and bind them into a well known
388 * location in the filesystem based on the name provided.
389 *
390 * The mount namespace is created so that any necessary
391 * userspace tweaks like remounting /sys, or bind mounting
392 * a new /etc/resolv.conf can be shared between uers.
393 */
394 char netns_path[MAXPATHLEN];
395 const char *name;
Eric W. Biederman223f4d82011-07-15 14:29:41 +0000396 int fd;
Eric W. Biederman58a3e822013-01-17 14:47:18 +0000397 int made_netns_run_dir_mount = 0;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700398
399 if (argc < 1) {
400 fprintf(stderr, "No netns name specified\n");
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700401 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700402 }
403 name = argv[0];
404
405 snprintf(netns_path, sizeof(netns_path), "%s/%s", NETNS_RUN_DIR, name);
406
407 /* Create the base netns directory if it doesn't exist */
408 mkdir(NETNS_RUN_DIR, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
409
Eric W. Biederman58a3e822013-01-17 14:47:18 +0000410 /* Make it possible for network namespace mounts to propogate between
411 * mount namespaces. This makes it likely that a unmounting a network
412 * namespace file in one namespace will unmount the network namespace
413 * file in all namespaces allowing the network namespace to be freed
414 * sooner.
415 */
416 while (mount("", NETNS_RUN_DIR, "none", MS_SHARED | MS_REC, NULL)) {
417 /* Fail unless we need to make the mount point */
418 if (errno != EINVAL || made_netns_run_dir_mount) {
419 fprintf(stderr, "mount --make-shared %s failed: %s\n",
420 NETNS_RUN_DIR, strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700421 return -1;
Eric W. Biederman58a3e822013-01-17 14:47:18 +0000422 }
423
424 /* Upgrade NETNS_RUN_DIR to a mount point */
425 if (mount(NETNS_RUN_DIR, NETNS_RUN_DIR, "none", MS_BIND, NULL)) {
426 fprintf(stderr, "mount --bind %s %s failed: %s\n",
427 NETNS_RUN_DIR, NETNS_RUN_DIR, strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700428 return -1;
Eric W. Biederman58a3e822013-01-17 14:47:18 +0000429 }
430 made_netns_run_dir_mount = 1;
431 }
432
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700433 /* Create the filesystem state */
Eric W. Biederman223f4d82011-07-15 14:29:41 +0000434 fd = open(netns_path, O_RDONLY|O_CREAT|O_EXCL, 0);
435 if (fd < 0) {
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000436 fprintf(stderr, "Cannot not create namespace file \"%s\": %s\n",
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700437 netns_path, strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700438 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700439 }
Eric W. Biederman223f4d82011-07-15 14:29:41 +0000440 close(fd);
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700441 if (unshare(CLONE_NEWNET) < 0) {
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +0000442 fprintf(stderr, "Failed to create a new network namespace \"%s\": %s\n",
443 name, strerror(errno));
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700444 goto out_delete;
445 }
446
447 /* Bind the netns last so I can watch for it */
448 if (mount("/proc/self/ns/net", netns_path, "none", MS_BIND, NULL) < 0) {
449 fprintf(stderr, "Bind /proc/self/ns/net -> %s failed: %s\n",
450 netns_path, strerror(errno));
451 goto out_delete;
452 }
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700453 return 0;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700454out_delete:
455 netns_delete(argc, argv);
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700456 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700457}
458
459
460static int netns_monitor(int argc, char **argv)
461{
462 char buf[4096];
463 struct inotify_event *event;
464 int fd;
465 fd = inotify_init();
466 if (fd < 0) {
467 fprintf(stderr, "inotify_init failed: %s\n",
468 strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700469 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700470 }
471 if (inotify_add_watch(fd, NETNS_RUN_DIR, IN_CREATE | IN_DELETE) < 0) {
472 fprintf(stderr, "inotify_add_watch failed: %s\n",
473 strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700474 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700475 }
476 for(;;) {
477 ssize_t len = read(fd, buf, sizeof(buf));
478 if (len < 0) {
479 fprintf(stderr, "read failed: %s\n",
480 strerror(errno));
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700481 return -1;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700482 }
483 for (event = (struct inotify_event *)buf;
484 (char *)event < &buf[len];
485 event = (struct inotify_event *)((char *)event + sizeof(*event) + event->len)) {
486 if (event->mask & IN_CREATE)
487 printf("add %s\n", event->name);
488 if (event->mask & IN_DELETE)
489 printf("delete %s\n", event->name);
490 }
491 }
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700492 return 0;
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700493}
494
495int do_netns(int argc, char **argv)
496{
497 if (argc < 1)
498 return netns_list(0, NULL);
499
500 if ((matches(*argv, "list") == 0) || (matches(*argv, "show") == 0) ||
501 (matches(*argv, "lst") == 0))
502 return netns_list(argc-1, argv+1);
503
504 if (matches(*argv, "help") == 0)
Eric W. Biederman8e2d47d2013-01-17 14:46:09 +0000505 return usage();
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700506
507 if (matches(*argv, "add") == 0)
508 return netns_add(argc-1, argv+1);
509
510 if (matches(*argv, "delete") == 0)
511 return netns_delete(argc-1, argv+1);
512
Eric W. Biederman9a7b3d92013-01-17 14:48:15 +0000513 if (matches(*argv, "identify") == 0)
514 return netns_identify(argc-1, argv+1);
515
516 if (matches(*argv, "pids") == 0)
517 return netns_pids(argc-1, argv+1);
518
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700519 if (matches(*argv, "exec") == 0)
520 return netns_exec(argc-1, argv+1);
521
522 if (matches(*argv, "monitor") == 0)
523 return netns_monitor(argc-1, argv+1);
524
525 fprintf(stderr, "Command \"%s\" is unknown, try \"ip netns help\".\n", *argv);
Stephen Hemmingera05f6512013-07-12 08:43:23 -0700526 exit(-1);
Eric W. Biederman0dc34c72011-07-13 09:48:26 -0700527}