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