-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathunshare.c
More file actions
133 lines (121 loc) · 4.52 KB
/
Copy pathunshare.c
File metadata and controls
133 lines (121 loc) · 4.52 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* vim:ts=4:sw=4:et:ai:sts=4
*
* python-unshare: Python bindings for the Linux unshare() syscall
* Copyright © 2010 Martina Ferrari <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <Python.h>
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include <sched.h>
static PyObject * _unshare(PyObject *self, PyObject *args) {
int flags, ret;
if (!PyArg_ParseTuple(args, "i", &flags))
return NULL;
ret = unshare(flags);
if(ret == -1)
return PyErr_SetFromErrno(PyExc_OSError);
Py_RETURN_NONE;
}
static PyObject * _setns(PyObject *self, PyObject *args) {
int fd, nstype, ret;
if (!PyArg_ParseTuple(args, "ii", &fd, &nstype))
return NULL;
ret = setns(fd, nstype);
if(ret == -1)
return PyErr_SetFromErrno(PyExc_OSError);
Py_RETURN_NONE;
}
static PyMethodDef methods[] = {
{"unshare", _unshare, METH_VARARGS,
"unshare(flags)\n\n"
"Disassociate parts of the process execution context.\n"
"flags is a bitmask that specifies which parts to unshare.\n\n"
"Possible values for flags:\n"
" CLONE_VM CLONE_FS CLONE_FILES CLONE_SIGHAND CLONE_THREAD "
"CLONE_NEWNS\n"
" CLONE_SYSVSEM CLONE_NEWUTS CLONE_NEWIPC CLONE_NEWUSER "
"CLONE_NEWPID\n"
" CLONE_NEWNET\n"
},
{"setns", _setns, METH_VARARGS,
"setns(fd, nstype)\n\n"
"Reassociate the calling thread with a new namespace.\n"
"fd is a filedescriptor referring to a namespace.\n"
"nstype specifies which type of namespace the calling thread\n"
"may be reassociated with.\n\n"
"Possible values for nstype:\n"
" 0 Allow any type of namespace to be joined.\n"
" CLONE_NEWIPC fd must refer to an IPC namespace.\n"
" CLONE_NEWNET fd must refer to a network namespace.\n"
" CLONE_NEWNS fd must refer to a mount namespace.\n"
" CLONE_NEWPID fd must refer to a descendant PID namespace.\n"
" CLONE_NEWUSER fd must refer to a user namespace.\n"
" CLONE_NEWUTS fd must refer to a UTS namespace.\n"
},
{NULL, NULL, 0, NULL}
};
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef moduledef =
{
PyModuleDef_HEAD_INIT,
"unshare", /* name of module */
"", /* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
methods
};
#endif
#if PY_MAJOR_VERSION >= 3
#define MOD_INIT(name) PyMODINIT_FUNC PyInit_##name(void)
#else
#define MOD_INIT(name) PyMODINIT_FUNC init##name(void)
#endif
MOD_INIT(unshare) {
PyObject *m;
#if PY_MAJOR_VERSION >= 3
m = PyModule_Create(&moduledef);
if (m == NULL)
return NULL;
#else
m = Py_InitModule3("unshare", methods, "");
if (m == NULL)
return;
#endif
/* Currently (2.6.33) not-implemented: CLONE_VM, CLONE_SIGHAND,
* CLONE_THREAD, CLONE_NEWUSER, CLONE_NEWPID */
PyModule_AddIntConstant(m, "CLONE_VM", CLONE_VM);
/* No CAP_* needed */
PyModule_AddIntConstant(m, "CLONE_FS", CLONE_FS);
/* No CAP_* needed */
PyModule_AddIntConstant(m, "CLONE_FILES", CLONE_FILES);
PyModule_AddIntConstant(m, "CLONE_SIGHAND", CLONE_SIGHAND);
PyModule_AddIntConstant(m, "CLONE_THREAD", CLONE_THREAD);
/* CAP_SYS_ADMIN */
PyModule_AddIntConstant(m, "CLONE_NEWNS", CLONE_NEWNS);
PyModule_AddIntConstant(m, "CLONE_SYSVSEM", CLONE_SYSVSEM);
/* CAP_SYS_ADMIN */
PyModule_AddIntConstant(m, "CLONE_NEWUTS", CLONE_NEWUTS);
/* CAP_SYS_ADMIN */
PyModule_AddIntConstant(m, "CLONE_NEWIPC", CLONE_NEWIPC);
PyModule_AddIntConstant(m, "CLONE_NEWUSER", CLONE_NEWUSER);
PyModule_AddIntConstant(m, "CLONE_NEWPID", CLONE_NEWPID);
/* CAP_SYS_ADMIN */
PyModule_AddIntConstant(m, "CLONE_NEWNET", CLONE_NEWNET);
#if PY_MAJOR_VERSION >= 3
return m;
#endif
}