From 36241cb1d12d3bea36ac5def0cc5f7648c067089 Mon Sep 17 00:00:00 2001 From: Ryan Voots Date: Wed, 3 May 2017 00:14:54 -0700 Subject: [PATCH] Initial work on this --- dist.ini | 13 +++++++++ lib/Sys/Linux/Mount.pm | 59 ++++++++++++++++++++++++++++++++++++++++ lib/Sys/Linux/Unshare.pm | 56 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 dist.ini create mode 100644 lib/Sys/Linux/Mount.pm create mode 100644 lib/Sys/Linux/Unshare.pm diff --git a/dist.ini b/dist.ini new file mode 100644 index 0000000..d5149af --- /dev/null +++ b/dist.ini @@ -0,0 +1,13 @@ +name = Sys-Linux-Unshare +author = Ryan Voots +license = Perl_5 +copyright_holder = Ryan Voots +copyright_year = 2017 + +[@Starter] +revision = 2 +installer = ModuleBuildTiny + +[Git::NextVersion] +[AutoPrereqs] +[Prereqs] diff --git a/lib/Sys/Linux/Mount.pm b/lib/Sys/Linux/Mount.pm new file mode 100644 index 0000000..daf7bc2 --- /dev/null +++ b/lib/Sys/Linux/Mount.pm @@ -0,0 +1,59 @@ +package Sys::Linux::Mount; + +use strict; +use warnings; +require Exporter; +our @ISA = qw/Exporter/; + +require 'syscall.ph'; + +my @mount_consts = qw/MS_RDONLY MS_NOSUID MS_NODEV MS_NOEXEC MS_SYNCHRONOUS MS_REMOUNT MS_MANDLOCK MS_DIRSYNC MS_NOATIME MS_NODIRATIME MS_BIND MS_MOVE MS_REC MS_SILENT MS_POSIXACL MS_UNBINDABLE MS_PRIVATE MS_SLAVE MS_SHARED MS_RELATIME MS_KERNMOUNT MS_I_VERSION MS_STRICTATIME MS_LAZYTIME MS_ACTIVE MS_NOUSER/; + +our @EXPORT_OK = (@mount_consts, qw/mount/); + +our %EXPORT_TAGS = ( + 'all' => [@mount_consts, qw/mount/], +); + +sub mount { + my ($source, $target, $filesystem, $flags, $options_hr) = @_; + + my $options_str = join ',', map {"$_=".$options_hr->{$_}} keys %$options_hr; + + my $ret = syscall(SYS_mount(), $source, $target, $filesystem, $flags, $options_str); + + if ($ret != 0) { + die "mount failed: $ret $!"; + } + + return; +} + +use constant {MS_RDONLY => 1, + MS_NOSUID => 2, + MS_NODEV => 4, + MS_NOEXEC => 8, + MS_SYNCHRONOUS => 16, + MS_REMOUNT => 32, + MS_MANDLOCK => 64, + MS_DIRSYNC => 128, + MS_NOATIME => 1024, + MS_NODIRATIME => 2048, + MS_BIND => 4096, + MS_MOVE => 8192, + MS_REC => 16384, + MS_SILENT => 32768, + MS_POSIXACL => 1 << 16, + MS_UNBINDABLE => 1 << 17, + MS_PRIVATE => 1 << 18, + MS_SLAVE => 1 << 19, + MS_SHARED => 1 << 20, + MS_RELATIME => 1 << 21, + MS_KERNMOUNT => 1 << 22, + MS_I_VERSION => 1 << 23, + MS_STRICTATIME => 1 << 24, + MS_LAZYTIME => 1 << 25, + MS_ACTIVE => 1 << 30, + MS_NOUSER => 1 << 31}; + +1; diff --git a/lib/Sys/Linux/Unshare.pm b/lib/Sys/Linux/Unshare.pm new file mode 100644 index 0000000..812e6c8 --- /dev/null +++ b/lib/Sys/Linux/Unshare.pm @@ -0,0 +1,56 @@ +package Sys::Linux::Unshare; + +use strict; +use warnings; +require Exporter; +our @ISA = qw/Exporter/; + +require 'syscall.ph'; + +my @unshare_consts = qw/CSIGNAL CLONE_VM CLONE_FS CLONE_FILES CLONE_SIGHAND CLONE_PTRACE CLONE_VFORK CLONE_PARENT CLONE_THREAD CLONE_NEWNS CLONE_SYSVSEM CLONE_SETTLS CLONE_PARENT_SETTID CLONE_CHILD_CLEARTID CLONE_DETACHED CLONE_UNTRACED CLONE_CHILD_SETTID CLONE_NEWCGROUP CLONE_NEWUTS CLONE_NEWIPC CLONE_NEWUSER CLONE_NEWPID CLONE_NEWNET CLONE_IO/; + +our @EXPORT_OK = (@unshare_consts, qw/unshare/); + +our %EXPORT_TAGS = ( + 'all' => [@unshare_consts, qw/unshare/], +); + +sub unshare { + my ($flags) = @_; + + my $ret = syscall(SYS_unshare(), $flags); + + if ($ret != 0) { + die "unshare failed $ret $!"; + } + + return; +} + +use constant {CSIGNAL => 0x000000ff, + CLONE_VM => 0x00000100, + CLONE_FS => 0x00000200, + CLONE_FILES => 0x00000400, + CLONE_SIGHAND => 0x00000800, + CLONE_PTRACE => 0x00002000, + CLONE_VFORK => 0x00004000, + CLONE_PARENT => 0x00008000, + CLONE_THREAD => 0x00010000, + CLONE_NEWNS => 0x00020000, + CLONE_SYSVSEM => 0x00040000, + CLONE_SETTLS => 0x00080000, + CLONE_PARENT_SETTID => 0x00100000, + CLONE_CHILD_CLEARTID => 0x00200000, + CLONE_DETACHED => 0x00400000, + CLONE_UNTRACED => 0x00800000, + CLONE_CHILD_SETTID => 0x01000000, + CLONE_NEWCGROUP => 0x02000000, + CLONE_NEWUTS => 0x04000000, + CLONE_NEWIPC => 0x08000000, + CLONE_NEWUSER => 0x10000000, + CLONE_NEWPID => 0x20000000, + CLONE_NEWNET => 0x40000000, + CLONE_IO => 0x80000000}; + +1; +