]>
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.
10 #include <sys/statvfs.h>
16 #include "namespace.h"
18 static void bind_etc(const char *name
)
20 char etc_netns_path
[sizeof(NETNS_ETC_DIR
) + NAME_MAX
];
21 char netns_name
[PATH_MAX
];
22 char etc_name
[PATH_MAX
];
26 if (strlen(name
) >= NAME_MAX
)
29 snprintf(etc_netns_path
, sizeof(etc_netns_path
), "%s/%s", NETNS_ETC_DIR
, name
);
30 dir
= opendir(etc_netns_path
);
34 while ((entry
= readdir(dir
)) != NULL
) {
35 if (strcmp(entry
->d_name
, ".") == 0)
37 if (strcmp(entry
->d_name
, "..") == 0)
39 snprintf(netns_name
, sizeof(netns_name
), "%s/%s", etc_netns_path
, entry
->d_name
);
40 snprintf(etc_name
, sizeof(etc_name
), "/etc/%s", entry
->d_name
);
41 if (mount(netns_name
, etc_name
, "none", MS_BIND
, NULL
) < 0) {
42 fprintf(stderr
, "Bind %s -> %s failed: %s\n",
43 netns_name
, etc_name
, strerror(errno
));
49 int netns_switch(char *name
)
51 char net_path
[PATH_MAX
];
53 unsigned long mountflags
= 0;
54 struct statvfs fsstat
;
56 snprintf(net_path
, sizeof(net_path
), "%s/%s", NETNS_RUN_DIR
, name
);
57 netns
= open(net_path
, O_RDONLY
| O_CLOEXEC
);
59 fprintf(stderr
, "Cannot open network namespace \"%s\": %s\n",
60 name
, strerror(errno
));
64 if (setns(netns
, CLONE_NEWNET
) < 0) {
65 fprintf(stderr
, "setting the network namespace \"%s\" failed: %s\n",
66 name
, strerror(errno
));
72 if (unshare(CLONE_NEWNS
) < 0) {
73 fprintf(stderr
, "unshare failed: %s\n", strerror(errno
));
76 /* Don't let any mounts propagate back to the parent */
77 if (mount("", "/", "none", MS_SLAVE
| MS_REC
, NULL
)) {
78 fprintf(stderr
, "\"mount --make-rslave /\" failed: %s\n",
83 /* Mount a version of /sys that describes the network namespace */
85 if (umount2("/sys", MNT_DETACH
) < 0) {
86 /* If this fails, perhaps there wasn't a sysfs instance mounted. Good. */
87 if (statvfs("/sys", &fsstat
) == 0) {
88 /* We couldn't umount the sysfs, we'll attempt to overlay it.
89 * A read-only instance can't be shadowed with a read-write one. */
90 if (fsstat
.f_flag
& ST_RDONLY
)
91 mountflags
= MS_RDONLY
;
94 if (mount(name
, "/sys", "sysfs", mountflags
, NULL
) < 0) {
95 fprintf(stderr
, "mount of /sys failed: %s\n",strerror(errno
));
99 /* Setup bind mounts for config files in /etc */
104 int netns_get_fd(const char *name
)
106 char pathbuf
[PATH_MAX
];
107 const char *path
, *ptr
;
110 ptr
= strchr(name
, '/');
112 snprintf(pathbuf
, sizeof(pathbuf
), "%s/%s",
113 NETNS_RUN_DIR
, name
);
116 return open(path
, O_RDONLY
);
119 int netns_foreach(int (*func
)(char *nsname
, void *arg
), void *arg
)
122 struct dirent
*entry
;
124 dir
= opendir(NETNS_RUN_DIR
);
128 while ((entry
= readdir(dir
)) != NULL
) {
129 if (strcmp(entry
->d_name
, ".") == 0)
131 if (strcmp(entry
->d_name
, "..") == 0)
133 if (func(entry
->d_name
, arg
))