| 1 | package Sys::Linux::Namespace; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Sets up linux kernel namespaces | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Sys::Linux::Mount qw/:all/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | #use Sys::Linux::Unshare qw/:all/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Linux::Clone; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use POSIX qw/_exit/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Time::HiRes qw/sleep/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Carp qw/croak/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | has no_proc => (is => 'rw'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | has term_child => (is => 'rw', default => 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 1 | our $debug = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | ▶ | sub debug { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
debug: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | ▶ | print STDERR @_ if $debug; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 1 | our $VERSION = '0.016'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 1 | my @signames = grep {!/^__/} keys %SIG; # capture before anyone has probably localized it. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 1 | for my $p (qw/tmp mount pid net ipc user uts sysvsem/) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 8 | my $pp = "private_$p"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 8 | has $pp => (is => 'rw'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | sub _uflags { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_uflags: not called CC=11 SCAR=48.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | my $uflags = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | $uflags |= Linux::Clone::NEWNS if ($self->private_tmp || $self->private_mount || ($self->private_pid && !$self->no_proc)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/9
$self->private_tmp or $self->private_mount or $self->private_pid and not $self->no_proc
$self->private_tmp or $self->private_mount
$self->private_pid and not $self->no_proc
MC/DC0%0/4
$self->private_tmp or $self->private_mount or $self->private_pid and not $self->no_proc
$self->private_tmp
$self->private_mount
$self->private_pid
not $self->no_proc
Truth table0%0/5
A or B or C and not(D)
A= $self->private_tmpB= $self->private_mountC= $self->private_pidD= not $self->no_proc
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | ▶ | $uflags |= Linux::Clone::NEWPID if ($self->private_pid); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | $uflags |= Linux::Clone::NEWNET if ($self->private_net); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | $uflags |= Linux::Clone::NEWIPC if ($self->private_ipc); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | $uflags |= Linux::Clone::NEWUSER if ($self->private_user); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | $uflags |= Linux::Clone::NEWUTS if ($self->private_uts); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | $uflags |= Linux::Clone::SYSVSEM if ($self->private_sysvsem); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | return $uflags; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | sub _subprocess { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_subprocess: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | my ($self, $code, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | croak "_subprocess requires a CODE ref" unless ref $code eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | debug "Forking\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | my $pid = Linux::Clone::clone (sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | local $$ = POSIX::getpid(); # try to fix up $$ if we can. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | debug "Inside Child $$\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | $code->(%args); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | _exit(0); # always exit with 0 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | }, 0, POSIX::SIGCHLD | $self->_uflags); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | croak "Failed to fork: $!" if ($pid < 0); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | local %SIG = map {my $q=$_; $q => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | debug "got signal $q in $$\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | kill 'TERM', $pid; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | sleep(0.2); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | kill 'KILL', $pid; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | kill 'KILL', $pid; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | }} ($self->term_child ? @signames : ()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | waitpid($pid, 0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | return $? | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | sub pre_setup { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pre_setup: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | croak "Private net is not yet supported" if $self->private_net; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | if ($self->private_pid && (ref $args{code} ne 'CODE' || !$args{_run})) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$self->private_pid and ref $args{"code"} ne "CODE" || !$args{"_run"}
ref $args{"code"} ne "CODE" or not $args{"_run"}
MC/DC0%0/3
$self->private_pid and ref $args{"code"} ne "CODE" || !$args{"_run"}
$self->private_pid
ref $args{"code"} ne "CODE"
not $args{"_run"}
Truth table0%0/4
A and B or not(C)
A= $self->private_pidB= ref $args{"code"} ne "CODE"C= not $args{"_run"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | croak "Private PID space requires a coderef to become the new PID 1"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | sub post_setup { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
post_setup: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | # If we want a private /tmp, or private mount we need to recursively make every mount private. it CAN be done without that but this is more reliable. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | if ($self->private_tmp || $self->private_mount || ($self->private_pid && !$self->no_proc)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/9
$self->private_tmp or $self->private_mount or $self->private_pid and not $self->no_proc
$self->private_tmp or $self->private_mount
$self->private_pid and not $self->no_proc
MC/DC0%0/4
$self->private_tmp or $self->private_mount or $self->private_pid and not $self->no_proc
$self->private_tmp
$self->private_mount
$self->private_pid
not $self->no_proc
Truth table0%0/5
A or B or C and not(D)
A= $self->private_tmpB= $self->private_mountC= $self->private_pidD= not $self->no_proc
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | mount("/", "/", undef, MS_REC|MS_PRIVATE, undef); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | if ($self->private_tmp) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | my $data = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | $data = $self->private_tmp if (ref $self->private_tmp eq 'HASH'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | mount("none", "/tmp", "tmpfs", MS_MGC_VAL, undef); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | mount("none", "/tmp", "tmpfs", MS_PRIVATE, $data); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | if ($self->private_pid && !$self->no_proc) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->private_pid and not $self->no_proc
MC/DC0%0/2
$self->private_pid and not $self->no_proc
$self->private_pid
not $self->no_proc
Truth table0%0/3
A and not(B)
A= $self->private_pidB= not $self->no_proc
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | mount("proc", "/proc", "proc", MS_MGC_VAL, undef); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | mount("proc", "/proc", "proc", MS_PRIVATE|MS_REC, undef); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | sub setup { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
setup: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | my $uflags = $self->_uflags; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | $self->pre_setup(%args); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | Linux::Clone::unshare($uflags); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | $self->post_setup(%args); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | sub run { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | my $code = $args{code}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | $args{_run} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | ▶ | croak "Run must be given a codref to run" unless ref $code eq "CODE"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | $self->pre_setup(%args); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | return $self->_subprocess(sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | $self->post_setup(%args); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | $code->(%args); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | }, %args); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||