Linux::Clone - an interface to the linux clone, unshare, setns, pivot_root and kcmp syscalls
use Linux::Clone;
This module exposes the linux clone(2), unshare(2) and related syscalls to Perl.
The following CLONE_ flag values (without CLONE_ prefix) are supported for unshare, if found, in this release. See the documentation for unshare(2) for more info on what they do:
Linux::Clone::FILES Linux::Clone::FS Linux::Clone::NEWNS (in unshare, implies FS) Linux::Clone::VM (in unshare, implies SIGHAND) Linux::Clone::THREAD (in unshare, implies VM, SIGHAND) Linux::Clone::SIGHAND Linux::Clone::SYSVSEM Linux::Clone::NEWUSER (in unshare, implies CLONE_THREAD) Linux::Clone::NEWPID Linux::Clone::NEWUTS Linux::Clone::NEWIPC Linux::Clone::NEWNET Linux::Clone::NEWCGROUP
Example: unshare the network namespace and prove that by calling ifconfig, showing only an unconfigured lo interface.
Linux::Clone::unshare Linux::Clone::NEWNET and "unshare: $!"; system "ifconfig -a";
Example: unshare the network namespace, initialise the loopback interface, create a veth interface pair, put one interface into the parent processes namespace (use ifconfig -a from another shell), configure the other interface with 192.168.99.2 -> 192.168.99.1 and start a shell.
use Linux::Clone; # unshare our network namespace Linux::Clone::unshare Linux::Clone::NEWNET and "unshare: $!"; my $ppid = getppid; system " # configure loopback interface ip link set lo up ip route add 127.0.0.0/8 dev lo # create veth pair ip link add name veth_master type veth peer name veth_slave # move veth_master to our parent process' namespace ip link set veth_master netns $ppid # configure the local interface ip link set veth_slave up ip addr add 192.168.99.2/32 dev veth_slave ip route add 192.168.99.1/32 dev veth_slave "; print <<EOF; say hi to your new network namespace, use exit to return. try this from another shell to get networking up: ip link set veth_master up ip addr add 192.168.99.1/32 dev veth_master ip route add 192.168.99.2/32 dev veth_master EOF system "bash";
Example: unshare the filesystem namespace and make a confusing bind mount only visible to the current process.
use Linux::Clone; Linux::Clone::unshare Linux::Clone::NEWNS and die "unshare: $!"; # now bind-mount /lib over /etc and ls -l /etc - scary system "mount -n --bind /lib /etc"; system "ls -l /etc";
Clones a new process as specified via $flags and calls $coderef without any arguments (a closure might help you if you need to pass arguments without global variables). The return value from coderef is returned to the system.
$flags
$coderef
The $stacksize specifies how large a stack to allocate for the child. If it is 0, then a default stack size (currently 4MB) will be allocated. There is currently no way to free this area again in the child.
$stacksize
0
$ptid, if specified, will receive the thread id, $tls, if specified, must contain a struct user_desc and $ctid is currently totally unsupported and must not be specified.
$ptid
$tls
struct user_desc
$ctid
Since this call basically bypasses both perl and your libc (for example, $$ might reflect the parent or child pid in the child), you need to be very careful when using this call, which means you should probably have a very good understanding of perl memory management and how fork and clone work.
$$
The following flags are supported for clone, in addition to all flags supported by unshare, above, and a signal number. When in doubt, refer to the clone(2) manual page.
unshare
Linux::Clone::PTRACE Linux::Clone::VFORK Linux::Clone::SETTLS (not yet implemented) Linux::Clone::PARENT_SETTID (not yet implemented) Linux::Clone::CHILD_SETTID (not yet implemented) Linux::Clone::CHILD_CLEARTID (not yet implemented) Linux::Clone::DETACHED Linux::Clone::UNTRACED Linux::Clone::IO
Note that for practical reasons you basically must not use Linux::Clone::VM or Linux::Clone::VFORK, as perl is unlikely to cope with that.
Linux::Clone::VM
Linux::Clone::VFORK
This is the glibc clone call, it cannot be used to emulate fork.
Example: do a fork-like clone, sharing nothing, slightly confusing perl and your libc, and exit immediately.
my $pid = Linux::Clone::clone sub { warn "in child"; 77 }, 0, POSIX::SIGCHLD;
Calls setns(2) on the file descriptor (or file handle) $fh_or_fd. If $nstype is missing, then 0 is used.
$fh_or_fd
$nstype
The argument $nstype can be 0, Linux::Clone::NEWIPC, Linux::Clone::NEWNET, Linux::Clone::NEUTS, Linux::Clone::NEWCGROUP, Linux::Clone::NEWNS, Linux::Clone::NEWPID or Linux::Clone::NEWUSER.
Linux::Clone::NEWIPC
Linux::Clone::NEWNET
Linux::Clone::NEUTS
Linux::Clone::NEWCGROUP
Linux::Clone::NEWNS
Linux::Clone::NEWPID
Linux::Clone::NEWUSER
Calls pivot_root(2) - refer to its manpage for details.
Calls kcmp(2) - refer to its manpage for details on operations.
The following $type constants are available if the kcmp syscall number was available during compilation:
$type
Linux::Clone::KCMP_FILE, Linux::Clone::KCMP_VM, Linux::Clone::KCMP_FILES, Linux::Clone::KCMP_FS, Linux::Clone::KCMP_SIGHAND, Linux::Clone::KCMP_IO and Linux::Clone::KCMP_SYSVSEM.
Linux::Clone::KCMP_FILE
Linux::Clone::KCMP_VM
Linux::Clone::KCMP_FILES
Linux::Clone::KCMP_FS
Linux::Clone::KCMP_SIGHAND
Linux::Clone::KCMP_IO
Linux::Clone::KCMP_SYSVSEM
Marc Lehmann <schmorp@schmorp.de> http://home.schmorp.de/
To install Linux::Clone, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Linux::Clone
CPAN shell
perl -MCPAN -e shell install Linux::Clone
For more information on module installation, please visit the detailed CPAN module installation guide.