-
Notifications
You must be signed in to change notification settings - Fork 2
/
unshare.c
50 lines (42 loc) · 1.56 KB
/
unshare.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <Python.h>
#include <sched.h>
static PyObject *_unshare(PyObject *self, PyObject *args) {
int clone_flags;
if (!PyArg_ParseTuple(args, "i", &clone_flags))
return NULL;
if (unshare(clone_flags) == -1) {
PyErr_SetFromErrno(PyExc_RuntimeError);
return NULL;
} else {
Py_INCREF(Py_None);
return Py_None;
}
}
static PyMethodDef unshare_methods[] = {
{"unshare", _unshare, METH_VARARGS,
"Use as: unshare(flag)\n"
"possible flags: CLONE_NEWNS, CLONE_NEWUTS, CLONE_NEWPID, "
"CLONE_NEWUSER, CLONE_NEWIPC, CLONE_NEWNET, CLONE_THREAD\n"
"See man 2 unshare.\n"
},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef unshare_module = {
PyModuleDef_HEAD_INIT,
"unshare",
"Python extension for calling C's unshare function",
-1,
unshare_methods
};
PyMODINIT_FUNC PyInit_unshare(void) {
PyObject *module = PyModule_Create(&unshare_module);
// clone constants
PyModule_AddIntConstant(module, "CLONE_NEWNS", CLONE_NEWNS); // mount namespace
PyModule_AddIntConstant(module, "CLONE_NEWUTS", CLONE_NEWUTS); // UTS (hostname) namespace
PyModule_AddIntConstant(module, "CLONE_NEWPID", CLONE_NEWPID); // PID namespace
PyModule_AddIntConstant(module, "CLONE_NEWUSER", CLONE_NEWUSER); // users namespace
PyModule_AddIntConstant(module, "CLONE_NEWIPC", CLONE_NEWIPC); // IPC namespace
PyModule_AddIntConstant(module, "CLONE_NEWNET", CLONE_NEWNET); // network namespace
PyModule_AddIntConstant(module, "CLONE_THREAD", CLONE_THREAD);
return module;
}