-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathns.cpp
71 lines (63 loc) · 1.27 KB
/
ns.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>
#include <sched.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#ifndef CLONE_NEWNET
#define CLONE_NEWNET 0x40000000
#endif
#ifndef CLONE_NEWNS
#define CLONE_NEWNS 0x00020000
#endif
#include "ns.hpp"
namespace ns
{
static inline void switch_ns(const char* path, int nstype, int flags)
{
int netns = open(path, O_RDONLY | O_CLOEXEC);
if (netns < 0)
{
throw exception(errno);
}
if (setns(netns, nstype) < 0)
{
int _errno = errno;
close(netns);
throw exception(_errno);
}
close(netns);
if (unshare(flags) < 0)
{
throw exception(errno);
}
}
net::net(const char* netns)
{
if (netns[0] == '/')
{
switch_ns(netns, CLONE_NEWNET, CLONE_NEWNS);
}
else
{
switch_ns(std::string("/run/netns/").append(netns).c_str(), CLONE_NEWNET, CLONE_NEWNS);
}
}
net::net(const std::string& netns)
{
if (netns.size() > 0)
{
if (netns.at(0) == '/')
{
switch_ns(netns.c_str(), CLONE_NEWNET, CLONE_NEWNS);
}
else
{
switch_ns(std::string("/run/netns/").append(netns).c_str(), CLONE_NEWNET, CLONE_NEWNS);
}
}
}
net::~net()
{
switch_ns("/proc/1/ns/net", CLONE_NEWNET, CLONE_NEWNS);
}
}