blob: 30b513889e6e2423daefd749234260449db9bfbd [file] [log] [blame]
Vadim Kochaneb67e442014-12-24 23:04:08 +02001/*
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>
Felix Jandaea343662015-07-26 21:25:32 +020012#include <limits.h>
Vadim Kochaneb67e442014-12-24 23:04:08 +020013
14#include "utils.h"
15#include "namespace.h"
16
17static void bind_etc(const char *name)
18{
Felix Jandaea343662015-07-26 21:25:32 +020019 char etc_netns_path[PATH_MAX];
20 char netns_name[PATH_MAX];
21 char etc_name[PATH_MAX];
Vadim Kochaneb67e442014-12-24 23:04:08 +020022 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
45int netns_switch(char *name)
46{
Felix Jandaea343662015-07-26 21:25:32 +020047 char net_path[PATH_MAX];
Vadim Kochaneb67e442014-12-24 23:04:08 +020048 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));
Phil Sutter9e5ba072015-08-18 18:11:08 +020061 close(netns);
62 return -1;
Vadim Kochaneb67e442014-12-24 23:04:08 +020063 }
Phil Sutter9e5ba072015-08-18 18:11:08 +020064 close(netns);
Vadim Kochaneb67e442014-12-24 23:04:08 +020065
66 if (unshare(CLONE_NEWNS) < 0) {
67 fprintf(stderr, "unshare failed: %s\n", strerror(errno));
Phil Sutter9e5ba072015-08-18 18:11:08 +020068 return -1;
Vadim Kochaneb67e442014-12-24 23:04:08 +020069 }
70 /* Don't let any mounts propagate back to the parent */
71 if (mount("", "/", "none", MS_SLAVE | MS_REC, NULL)) {
72 fprintf(stderr, "\"mount --make-rslave /\" failed: %s\n",
73 strerror(errno));
Phil Sutter9e5ba072015-08-18 18:11:08 +020074 return -1;
Vadim Kochaneb67e442014-12-24 23:04:08 +020075 }
76 /* Mount a version of /sys that describes the network namespace */
77 if (umount2("/sys", MNT_DETACH) < 0) {
78 fprintf(stderr, "umount of /sys failed: %s\n", strerror(errno));
Phil Sutter9e5ba072015-08-18 18:11:08 +020079 return -1;
Vadim Kochaneb67e442014-12-24 23:04:08 +020080 }
81 if (mount(name, "/sys", "sysfs", 0, NULL) < 0) {
82 fprintf(stderr, "mount of /sys failed: %s\n",strerror(errno));
Phil Sutter9e5ba072015-08-18 18:11:08 +020083 return -1;
Vadim Kochaneb67e442014-12-24 23:04:08 +020084 }
85
86 /* Setup bind mounts for config files in /etc */
87 bind_etc(name);
88 return 0;
89}
Vadim Kochanc3087c12015-01-13 22:08:44 +020090
91int netns_get_fd(const char *name)
92{
Felix Jandaea343662015-07-26 21:25:32 +020093 char pathbuf[PATH_MAX];
Vadim Kochanc3087c12015-01-13 22:08:44 +020094 const char *path, *ptr;
95
96 path = name;
97 ptr = strchr(name, '/');
98 if (!ptr) {
99 snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
100 NETNS_RUN_DIR, name );
101 path = pathbuf;
102 }
103 return open(path, O_RDONLY);
104}
Vadim Kochane998e112015-01-18 16:10:17 +0200105
106int netns_foreach(int (*func)(char *nsname, void *arg), void *arg)
107{
108 DIR *dir;
109 struct dirent *entry;
110
111 dir = opendir(NETNS_RUN_DIR);
112 if (!dir)
113 return -1;
114
115 while ((entry = readdir(dir)) != NULL) {
116 if (strcmp(entry->d_name, ".") == 0)
117 continue;
118 if (strcmp(entry->d_name, "..") == 0)
119 continue;
120 if (func(entry->d_name, arg))
121 break;
122 }
123
124 closedir(dir);
125 return 0;
126}