1 |
NAME |
2 |
Linux::Clone - an interface to the linux clone(2) and unshare(2) |
3 |
syscalls |
4 |
|
5 |
SYNOPSIS |
6 |
use Linux::Clone; |
7 |
|
8 |
DESCRIPTION |
9 |
This module exposes the linux clone(2), unshare(2) and related syscalls |
10 |
to Perl. |
11 |
|
12 |
$retval = unshare $flags |
13 |
The following CLONE_ flag values (without CLONE_ prefix) are |
14 |
supported for unshare, if found, in this release. See the |
15 |
documentation for unshare(2) for more info on what they do: |
16 |
|
17 |
Linux::Clone::FILES |
18 |
Linux::Clone::FS |
19 |
Linux::Clone::NEWNS (in unshare, implies FS) |
20 |
Linux::Clone::VM (in unshare, implies SIGHAND) |
21 |
Linux::Clone::THREAD (in unshare, implies VM, SIGHAND) |
22 |
Linux::Clone::SIGHAND |
23 |
Linux::Clone::SYSVSEM |
24 |
Linux::Clone::NEWUSER (in unshare, implies CLONE_THREAD) |
25 |
Linux::Clone::NEWPID |
26 |
Linux::Clone::NEWUTS |
27 |
Linux::Clone::NEWIPC |
28 |
Linux::Clone::NEWNET |
29 |
Linux::Clone::NEWCGROUP |
30 |
|
31 |
Example: unshare the network namespace and prove that by calling |
32 |
ifconfig, showing only an unconfigured lo interface. |
33 |
|
34 |
Linux::Clone::unshare Linux::Clone::NEWNET |
35 |
and "unshare: $!"; |
36 |
system "ifconfig -a"; |
37 |
|
38 |
Example: unshare the network namespace, initialise the loopback |
39 |
interface, create a veth interface pair, put one interface into the |
40 |
parent processes namespace (use ifconfig -a from another shell), |
41 |
configure the other interface with 192.168.99.2 -> 192.168.99.1 and |
42 |
start a shell. |
43 |
|
44 |
use Linux::Clone; |
45 |
|
46 |
# unshare our network namespace |
47 |
Linux::Clone::unshare Linux::Clone::NEWNET |
48 |
and "unshare: $!"; |
49 |
|
50 |
my $ppid = getppid; |
51 |
|
52 |
system " |
53 |
# configure loopback interface |
54 |
ip link set lo up |
55 |
ip route add 127.0.0.0/8 dev lo |
56 |
|
57 |
# create veth pair |
58 |
ip link add name veth_master type veth peer name veth_slave |
59 |
|
60 |
# move veth_master to our parent process' namespace |
61 |
ip link set veth_master netns $ppid |
62 |
|
63 |
# configure the local interface |
64 |
ip link set veth_slave up |
65 |
ip addr add 192.168.99.2/32 dev veth_slave |
66 |
ip route add 192.168.99.1/32 dev veth_slave |
67 |
"; |
68 |
|
69 |
print <<EOF; |
70 |
say hi to your new network namespace, use exit to return. |
71 |
|
72 |
try this from another shell to get networking up: |
73 |
|
74 |
ip link set veth_master up |
75 |
ip addr add 192.168.99.1/32 dev veth_master |
76 |
ip route add 192.168.99.2/32 dev veth_master |
77 |
|
78 |
EOF |
79 |
system "bash"; |
80 |
|
81 |
Example: unshare the filesystem namespace and make a confusing bind |
82 |
mount only visible to the current process. |
83 |
|
84 |
use Linux::Clone; |
85 |
|
86 |
Linux::Clone::unshare Linux::Clone::NEWNS |
87 |
and die "unshare: $!"; |
88 |
|
89 |
# now bind-mount /lib over /etc and ls -l /etc - scary |
90 |
system "mount -n --bind /lib /etc"; |
91 |
system "ls -l /etc"; |
92 |
|
93 |
$retval = Linux::Clone::clone $coderef, $stacksize, $flags[, $ptid, |
94 |
$tls, $ctid] |
95 |
Clones a new process as specified via $flags and calls $coderef |
96 |
without any arguments (a closure might help you if you need to pass |
97 |
arguments without global variables). The return value from coderef |
98 |
is returned to the system. |
99 |
|
100 |
The $stacksize specifies how large a stack to allocate for the |
101 |
child. If it is 0, then a default stack size (currently 4MB) will be |
102 |
allocated. There is currently no way to free this area again in the |
103 |
child. |
104 |
|
105 |
$ptid, if specified, will receive the thread id, $tls, if specified, |
106 |
must contain a "struct user_desc" and $ctid is currently totally |
107 |
unsupported and must not be specified. |
108 |
|
109 |
Since this call basically bypasses both perl and your libc (for |
110 |
example, $$ might reflect the parent *or* child pid in the child), |
111 |
you need to be very careful when using this call, which means you |
112 |
should probably have a very good understanding of perl memory |
113 |
management and how fork and clone work. |
114 |
|
115 |
The following flags are supported for clone, in addition to all |
116 |
flags supported by "unshare", above, and a signal number. When in |
117 |
doubt, refer to the clone(2) manual page. |
118 |
|
119 |
Linux::Clone::PTRACE |
120 |
Linux::Clone::VFORK |
121 |
Linux::Clone::SETTLS (not yet implemented) |
122 |
Linux::Clone::PARENT_SETTID (not yet implemented) |
123 |
Linux::Clone::CHILD_SETTID (not yet implemented) |
124 |
Linux::Clone::CHILD_CLEARTID (not yet implemented) |
125 |
Linux::Clone::DETACHED |
126 |
Linux::Clone::UNTRACED |
127 |
Linux::Clone::IO |
128 |
|
129 |
Note that for practical reasons you basically must not use |
130 |
"Linux::Clone::VM" or "Linux::Clone::VFORK", as perl is unlikely to |
131 |
cope with that. |
132 |
|
133 |
This is the glibc clone call, it cannot be used to emulate fork. |
134 |
|
135 |
Example: do a fork-like clone, sharing nothing, slightly confusing |
136 |
perl and your libc, and exit immediately. |
137 |
|
138 |
my $pid = Linux::Clone::clone sub { warn "in child"; 77 }, 0, POSIX::SIGCHLD; |
139 |
|
140 |
Linux::Clone::setns $fh_or_fd[, $nstype] |
141 |
Calls setns(2) on the file descriptor (or file handle) $fh_or_fd. If |
142 |
$nstype is missing, then 0 is used. |
143 |
|
144 |
At the time of this writing, $nstype can be 0, |
145 |
"Linux::Clone::NEWIPC", "Linux::Clone::NEWNET", |
146 |
"Linux::Clone::NEUTS", "Linux::Clone::NEWCGROUP", |
147 |
"Linux::Clone::NEWNS", "Linux::Clone::NEWPID" or |
148 |
"Linux::Clone::NEWUSER". |
149 |
|
150 |
AUTHOR |
151 |
Marc Lehmann <schmorp@schmorp.de> |
152 |
http://home.schmorp.de/ |
153 |
|