unshare: Add bindings for unshare(2)

master
Dustin C. Hatch 2016-11-15 09:25:49 -06:00
parent 5970b83721
commit 07d2e75e67
1 changed files with 35 additions and 0 deletions

35
src/linuxapi/unshare.py Normal file
View File

@ -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()