]> git.proxmox.com Git - mirror_iproute2.git/blob - lib/namespace.c
lib: Exec func on each netns
[mirror_iproute2.git] / lib / namespace.c
1 /*
2 * namespace.c
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 */
9
10 #include <fcntl.h>
11 #include <dirent.h>
12
13 #include "utils.h"
14 #include "namespace.h"
15
16 static void bind_etc(const char *name)
17 {
18 char etc_netns_path[MAXPATHLEN];
19 char netns_name[MAXPATHLEN];
20 char etc_name[MAXPATHLEN];
21 struct dirent *entry;
22 DIR *dir;
23
24 snprintf(etc_netns_path, sizeof(etc_netns_path), "%s/%s", NETNS_ETC_DIR, name);
25 dir = opendir(etc_netns_path);
26 if (!dir)
27 return;
28
29 while ((entry = readdir(dir)) != NULL) {
30 if (strcmp(entry->d_name, ".") == 0)
31 continue;
32 if (strcmp(entry->d_name, "..") == 0)
33 continue;
34 snprintf(netns_name, sizeof(netns_name), "%s/%s", etc_netns_path, entry->d_name);
35 snprintf(etc_name, sizeof(etc_name), "/etc/%s", entry->d_name);
36 if (mount(netns_name, etc_name, "none", MS_BIND, NULL) < 0) {
37 fprintf(stderr, "Bind %s -> %s failed: %s\n",
38 netns_name, etc_name, strerror(errno));
39 }
40 }
41 closedir(dir);
42 }
43
44 int netns_switch(char *name)
45 {
46 char net_path[MAXPATHLEN];
47 int netns;
48
49 snprintf(net_path, sizeof(net_path), "%s/%s", NETNS_RUN_DIR, name);
50 netns = open(net_path, O_RDONLY | O_CLOEXEC);
51 if (netns < 0) {
52 fprintf(stderr, "Cannot open network namespace \"%s\": %s\n",
53 name, strerror(errno));
54 return -1;
55 }
56
57 if (setns(netns, CLONE_NEWNET) < 0) {
58 fprintf(stderr, "setting the network namespace \"%s\" failed: %s\n",
59 name, strerror(errno));
60 return -1;
61 }
62
63 if (unshare(CLONE_NEWNS) < 0) {
64 fprintf(stderr, "unshare failed: %s\n", strerror(errno));
65 return -1;
66 }
67 /* Don't let any mounts propagate back to the parent */
68 if (mount("", "/", "none", MS_SLAVE | MS_REC, NULL)) {
69 fprintf(stderr, "\"mount --make-rslave /\" failed: %s\n",
70 strerror(errno));
71 return -1;
72 }
73 /* Mount a version of /sys that describes the network namespace */
74 if (umount2("/sys", MNT_DETACH) < 0) {
75 fprintf(stderr, "umount of /sys failed: %s\n", strerror(errno));
76 return -1;
77 }
78 if (mount(name, "/sys", "sysfs", 0, NULL) < 0) {
79 fprintf(stderr, "mount of /sys failed: %s\n",strerror(errno));
80 return -1;
81 }
82
83 /* Setup bind mounts for config files in /etc */
84 bind_etc(name);
85 return 0;
86 }
87
88 int netns_get_fd(const char *name)
89 {
90 char pathbuf[MAXPATHLEN];
91 const char *path, *ptr;
92
93 path = name;
94 ptr = strchr(name, '/');
95 if (!ptr) {
96 snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
97 NETNS_RUN_DIR, name );
98 path = pathbuf;
99 }
100 return open(path, O_RDONLY);
101 }
102
103 int netns_foreach(int (*func)(char *nsname, void *arg), void *arg)
104 {
105 DIR *dir;
106 struct dirent *entry;
107
108 dir = opendir(NETNS_RUN_DIR);
109 if (!dir)
110 return -1;
111
112 while ((entry = readdir(dir)) != NULL) {
113 if (strcmp(entry->d_name, ".") == 0)
114 continue;
115 if (strcmp(entry->d_name, "..") == 0)
116 continue;
117 if (func(entry->d_name, arg))
118 break;
119 }
120
121 closedir(dir);
122 return 0;
123 }