The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Linux::Setns - Perl extension for switching the current process namespace to another namespace pointed by a path to the ns file descriptor.

SYNOPSIS

use Linux::setns qw(setns CLONE_ALL CLONE_NEWIPC CLONE_NEWNET CLONE_NEWUTS CLONE_NEWUSER CLONE_NEWPID);

die "setns() requires root privileges\n" if $>;

setns("/proc/PID/ns/mnt", CLONE_ALL);
# now your process is in the same namespaces(IPC,NET,UTS,PID,USER,MOUNT) as the /proc/PID/ns/mnt 

# If you want to change only one of your namespaces you can use any of the bellow examples:

# Switch your current Mount namespace to the one pointed by /proc/PID/ns/mnt
setns("/proc/PID/ns/mnt", CLONE_NEWNS);

# Switch your current IPC namespace to the one pointed by /proc/PID/ns/ipc
setns("/proc/PID/ns/ipc", CLONE_NEWIPC);

# Switch your current Network namespace to the one pointed by /proc/PID/ns/net
setns("/proc/PID/ns/net", CLONE_NEWNET);

# Switch your current UTS namespace to the one pointed by /proc/PID/ns/uts
setns("/proc/PID/ns/uts", CLONE_NEWUTS);

# Switch your current Pid namespace to the one pointed by /proc/PID/ns/pid
setns("/proc/PID/ns/pid", CLONE_NEWPID);

# Switch your current User namespace to the one pointed by /proc/PID/ns/user
setns("/proc/PID/ns/user", CLONE_NEWUSER);

DESCRIPTION

This trivial module provides interface to the Linux setns system call. It also provides the CLONE_* constants that are used to specify which kind of namespace you are entering. Also a new CLONE_ALL constat is provided so you can join/switch to any type of namespace.

The setns system call allows a process to 'join/switch' one of its namespaces to namespaces pointed by a file descriptor(usually located in /proc/PID/ns/{ipc,mnt,net,pid,user,uts}).

Note: keep in mind that using any specific CLONE_NEW* constant will fail if the FD path you gave is not of that type.

RETRUN VALUE 1 on success 0 on failure

EXPORT

setns			- the subroutine

CLONE_ALL		- flag that tells that the path can be of any namespace type
CLONE_NEWNS	- when this flag is used the path must be from another Mount namespace
CLONE_NEWIPC	- when this flag is used the path must be from another IPC namespace
CLONE_NEWNET	- when this flag is used the path must be from another Network namespace
CLONE_NEWUTS	- when this flag is used the path must be from another UTS namespace
CLONE_NEWPID	- when this flag is used the path must be from another PID namespace
CLONE_NEWUSER	- when this flag is used the path must be from another User namespace

SEE ALSO

setns(s) Linux man page.

AUTHOR

Marian HackMan Marinov, <hackman@cpan.org>

COPYRIGHT AND LICENSE

Copyright (C) 2016 by Marian HackMan Marinov

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10.1 or, at your option, any later version of Perl 5 you may have available.