summaryrefslogtreecommitdiffstats
path: root/Modules/posixmodule.c
diff options
context:
space:
mode:
authorNoam Cohen <noam@noam.me>2022-10-20 09:08:54 (GMT)
committerGitHub <noreply@github.com>2022-10-20 09:08:54 (GMT)
commita371a7e03e43e08cae70235a71904989c0f57a5e (patch)
tree40b9d4b13ec579ae370760e2dafd888e32f2b06f /Modules/posixmodule.c
parentc1e02d4e4e09dfc9d57c4d36eca1bbf312d4162d (diff)
downloadcpython-a371a7e03e43e08cae70235a71904989c0f57a5e.zip
cpython-a371a7e03e43e08cae70235a71904989c0f57a5e.tar.gz
cpython-a371a7e03e43e08cae70235a71904989c0f57a5e.tar.bz2
gh-95023: Added os.setns and os.unshare functions (#95046)
Added os.setns and os.unshare to easily switch between namespaces on Linux. Co-authored-by: Christian Heimes <christian@python.org> Co-authored-by: CAM Gerlach <CAM.Gerlach@Gerlach.CAM> Co-authored-by: Victor Stinner <vstinner@python.org>
Diffstat (limited to 'Modules/posixmodule.c')
-rw-r--r--Modules/posixmodule.c107
1 files changed, 107 insertions, 0 deletions
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 39198cb..56ea319 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -8581,6 +8581,64 @@ os_pidfd_open_impl(PyObject *module, pid_t pid, unsigned int flags)
#endif
+#ifdef HAVE_SETNS
+/*[clinic input]
+os.setns
+ fd: fildes
+ A file descriptor to a namespace.
+ nstype: int = 0
+ Type of namespace.
+
+Move the calling thread into different namespaces.
+[clinic start generated code]*/
+
+static PyObject *
+os_setns_impl(PyObject *module, int fd, int nstype)
+/*[clinic end generated code: output=5dbd055bfb66ecd0 input=42787871226bf3ee]*/
+{
+ int res;
+
+ Py_BEGIN_ALLOW_THREADS
+ res = setns(fd, nstype);
+ Py_END_ALLOW_THREADS
+
+ if (res != 0) {
+ return posix_error();
+ }
+
+ Py_RETURN_NONE;
+}
+#endif
+
+
+#ifdef HAVE_UNSHARE
+/*[clinic input]
+os.unshare
+ flags: int
+ Namespaces to be unshared.
+
+Disassociate parts of a process (or thread) execution context.
+[clinic start generated code]*/
+
+static PyObject *
+os_unshare_impl(PyObject *module, int flags)
+/*[clinic end generated code: output=1b3177906dd237ee input=9e065db3232b8b1b]*/
+{
+ int res;
+
+ Py_BEGIN_ALLOW_THREADS
+ res = unshare(flags);
+ Py_END_ALLOW_THREADS
+
+ if (res != 0) {
+ return posix_error();
+ }
+
+ Py_RETURN_NONE;
+}
+#endif
+
+
#if defined(HAVE_READLINK) || defined(MS_WINDOWS)
/*[clinic input]
os.readlink
@@ -14945,6 +15003,8 @@ static PyMethodDef posix_methods[] = {
OS__ADD_DLL_DIRECTORY_METHODDEF
OS__REMOVE_DLL_DIRECTORY_METHODDEF
OS_WAITSTATUS_TO_EXITCODE_METHODDEF
+ OS_SETNS_METHODDEF
+ OS_UNSHARE_METHODDEF
{NULL, NULL} /* Sentinel */
};
@@ -15390,6 +15450,53 @@ all_ins(PyObject *m)
#ifdef SCHED_FX
if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1;
#endif
+
+/* constants for namespaces */
+#if defined(HAVE_SETNS) || defined(HAVE_UNSHARE)
+#ifdef CLONE_FS
+ if (PyModule_AddIntMacro(m, CLONE_FS)) return -1;
+#endif
+#ifdef CLONE_FILES
+ if (PyModule_AddIntMacro(m, CLONE_FILES)) return -1;
+#endif
+#ifdef CLONE_NEWNS
+ if (PyModule_AddIntMacro(m, CLONE_NEWNS)) return -1;
+#endif
+#ifdef CLONE_NEWCGROUP
+ if (PyModule_AddIntMacro(m, CLONE_NEWCGROUP)) return -1;
+#endif
+#ifdef CLONE_NEWUTS
+ if (PyModule_AddIntMacro(m, CLONE_NEWUTS)) return -1;
+#endif
+#ifdef CLONE_NEWIPC
+ if (PyModule_AddIntMacro(m, CLONE_NEWIPC)) return -1;
+#endif
+#ifdef CLONE_NEWUSER
+ if (PyModule_AddIntMacro(m, CLONE_NEWUSER)) return -1;
+#endif
+#ifdef CLONE_NEWPID
+ if (PyModule_AddIntMacro(m, CLONE_NEWPID)) return -1;
+#endif
+#ifdef CLONE_NEWNET
+ if (PyModule_AddIntMacro(m, CLONE_NEWNET)) return -1;
+#endif
+#ifdef CLONE_NEWTIME
+ if (PyModule_AddIntMacro(m, CLONE_NEWTIME)) return -1;
+#endif
+#ifdef CLONE_SYSVSEM
+ if (PyModule_AddIntMacro(m, CLONE_SYSVSEM)) return -1;
+#endif
+#ifdef CLONE_THREAD
+ if (PyModule_AddIntMacro(m, CLONE_THREAD)) return -1;
+#endif
+#ifdef CLONE_SIGHAND
+ if (PyModule_AddIntMacro(m, CLONE_SIGHAND)) return -1;
+#endif
+#ifdef CLONE_VM
+ if (PyModule_AddIntMacro(m, CLONE_VM)) return -1;
+#endif
+#endif
+
#endif
#ifdef USE_XATTRS