]>
git.proxmox.com Git - mirror_iproute2.git/blob - lib/namespace.c
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.
14 #include "namespace.h"
16 static void bind_etc(const char *name
)
18 char etc_netns_path
[MAXPATHLEN
];
19 char netns_name
[MAXPATHLEN
];
20 char etc_name
[MAXPATHLEN
];
24 snprintf(etc_netns_path
, sizeof(etc_netns_path
), "%s/%s", NETNS_ETC_DIR
, name
);
25 dir
= opendir(etc_netns_path
);
29 while ((entry
= readdir(dir
)) != NULL
) {
30 if (strcmp(entry
->d_name
, ".") == 0)
32 if (strcmp(entry
->d_name
, "..") == 0)
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
));
44 int netns_switch(char *name
)
46 char net_path
[MAXPATHLEN
];
49 snprintf(net_path
, sizeof(net_path
), "%s/%s", NETNS_RUN_DIR
, name
);
50 netns
= open(net_path
, O_RDONLY
| O_CLOEXEC
);
52 fprintf(stderr
, "Cannot open network namespace \"%s\": %s\n",
53 name
, strerror(errno
));
57 if (setns(netns
, CLONE_NEWNET
) < 0) {
58 fprintf(stderr
, "setting the network namespace \"%s\" failed: %s\n",
59 name
, strerror(errno
));
63 if (unshare(CLONE_NEWNS
) < 0) {
64 fprintf(stderr
, "unshare failed: %s\n", strerror(errno
));
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",
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
));
78 if (mount(name
, "/sys", "sysfs", 0, NULL
) < 0) {
79 fprintf(stderr
, "mount of /sys failed: %s\n",strerror(errno
));
83 /* Setup bind mounts for config files in /etc */
88 int netns_get_fd(const char *name
)
90 char pathbuf
[MAXPATHLEN
];
91 const char *path
, *ptr
;
94 ptr
= strchr(name
, '/');
96 snprintf(pathbuf
, sizeof(pathbuf
), "%s/%s",
97 NETNS_RUN_DIR
, name
);
100 return open(path
, O_RDONLY
);
103 int netns_foreach(int (*func
)(char *nsname
, void *arg
), void *arg
)
106 struct dirent
*entry
;
108 dir
= opendir(NETNS_RUN_DIR
);
112 while ((entry
= readdir(dir
)) != NULL
) {
113 if (strcmp(entry
->d_name
, ".") == 0)
115 if (strcmp(entry
->d_name
, "..") == 0)
117 if (func(entry
->d_name
, arg
))