From 07d2e75e675a1b05e87fe6fda066e2cc38c7242d Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Tue, 15 Nov 2016 09:25:49 -0600 Subject: [PATCH] unshare: Add bindings for unshare(2) --- src/linuxapi/unshare.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/linuxapi/unshare.py diff --git a/src/linuxapi/unshare.py b/src/linuxapi/unshare.py new file mode 100644 index 0000000..a383a98 --- /dev/null +++ b/src/linuxapi/unshare.py @@ -0,0 +1,35 @@ +import ctypes.util +import os + + +_libc = ctypes.CDLL(ctypes.util.find_library('c')) + +_errno = _libc.__errno_location +_errno.restype = ctypes.POINTER(ctypes.c_int) + +_libc.unshare.argtypes = (ctypes.c_int,) +_libc.unshare_restype = ctypes.c_int + + +CLONE_FILES = 0x00000400 +CLONE_FS = 0x00000200 +CLONE_NEWIPC = 0x08000000 +CLONE_NEWNET = 0x40000000 +CLONE_NEWNS = 0x00020000 +CLONE_NEWPID = 0x20000000 +CLONE_NEWUSER = 0x10000000 +CLONE_NEWUTS = 0x04000000 +CLONE_SYSVSEM = 0x00040000 + + +class UnshareError(OSError): + + @classmethod + def from_c_err(cls): + errno = _errno().contents.value + return cls(errno, os.strerror(errno)) + + +def unshare(flags): + if _libc.unshare(flags) != 0: + raise UnshareError.from_c_err()