=head1 NAME Linux::Clone - an interface to the linux clone(2) and unshare(2) syscalls =head1 SYNOPSIS use Linux::Clone; =head1 DESCRIPTION This module exposes the linux clone(2), unshare(2) and related syscalls to Perl. =over 4 =item $retval = unshare $flags 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 < and calls C<$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. The C<$stacksize> specifies how large a stack to allocate for the child. If it is C<0>, then a default stack size (currently 4MB) will be allocated. There is currently no way to free this area again in the child. C<$ptid>, if specified, will receive the thread id, C<$tls>, if specified, must contain a C and C<$ctid> is currently totally unsupported and must not be specified. Since this call basically bypasses both perl and your libc (for example, C<$$> might reflect the parent I 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 C, above, and a signal number. When in doubt, refer to the clone(2) manual page. 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 C or C, as perl is unlikely to cope with that. 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; =item Linux::Clone::setns $fh_or_fd[, $nstype] Calls setns(2) on the file descriptor (or file handle) C<$fh_or_fd>. If C<$nstype> is missing, then C<0> is used. At the time of this writing, C<$nstype> can be C<0>, C, C, C, C, C, C or C. =back =cut package Linux::Clone; # use common::sense; BEGIN { our $VERSION = '1.0'; require XSLoader; XSLoader::load (__PACKAGE__, $VERSION); } 1; =head1 AUTHOR Marc Lehmann http://home.schmorp.de/ =cut