Skip to content

Commit eab072b

Browse files
committed
gh-95023: Add os.setns() and os.unshare() for Linux namespaces
1 parent be09bae commit eab072b

File tree

6 files changed

+196
-5
lines changed

6 files changed

+196
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add :func:`os.setns` and :func:`os.unshare` to manage namespaces on Linux.

Modules/clinic/posixmodule.c.h

+91-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/posixmodule.c

+94
Original file line numberDiff line numberDiff line change
@@ -14743,6 +14743,54 @@ os_waitstatus_to_exitcode_impl(PyObject *module, PyObject *status_obj)
1474314743
}
1474414744
#endif
1474514745

14746+
#ifdef HAVE_SETNS
14747+
/*[clinic input]
14748+
os.setns
14749+
14750+
fd: fildes
14751+
nstype: int
14752+
14753+
Reassociate thread with a namespace
14754+
[clinic start generated code]*/
14755+
14756+
static PyObject *
14757+
os_setns_impl(PyObject *module, int fd, int nstype)
14758+
/*[clinic end generated code: output=5dbd055bfb66ecd0 input=9d2de6d8a880014b]*/
14759+
{
14760+
int result;
14761+
Py_BEGIN_ALLOW_THREADS
14762+
result = setns(fd, nstype);
14763+
Py_END_ALLOW_THREADS
14764+
if (result == -1) {
14765+
return PyErr_SetFromErrno(PyExc_OSError);
14766+
}
14767+
Py_RETURN_NONE;
14768+
}
14769+
#endif
14770+
14771+
#ifdef HAVE_UNSHARE
14772+
/*[clinic input]
14773+
os.unshare
14774+
14775+
flags: int
14776+
14777+
Disassociate parts of the process execution context
14778+
[clinic start generated code]*/
14779+
14780+
static PyObject *
14781+
os_unshare_impl(PyObject *module, int flags)
14782+
/*[clinic end generated code: output=1b3177906dd237ee input=4d655e499ec28dd6]*/
14783+
{
14784+
int result;
14785+
Py_BEGIN_ALLOW_THREADS
14786+
result = unshare(flags);
14787+
Py_END_ALLOW_THREADS
14788+
if (result == -1) {
14789+
return PyErr_SetFromErrno(PyExc_OSError);
14790+
}
14791+
Py_RETURN_NONE;
14792+
}
14793+
#endif
1474614794

1474714795
static PyMethodDef posix_methods[] = {
1474814796

@@ -14930,6 +14978,8 @@ static PyMethodDef posix_methods[] = {
1493014978
OS__ADD_DLL_DIRECTORY_METHODDEF
1493114979
OS__REMOVE_DLL_DIRECTORY_METHODDEF
1493214980
OS_WAITSTATUS_TO_EXITCODE_METHODDEF
14981+
OS_SETNS_METHODDEF
14982+
OS_UNSHARE_METHODDEF
1493314983
{NULL, NULL} /* Sentinel */
1493414984
};
1493514985

@@ -15468,6 +15518,50 @@ all_ins(PyObject *m)
1546815518
if (PyModule_AddIntMacro(m, EFD_SEMAPHORE)) return -1;
1546915519
#endif
1547015520

15521+
// setns(2) and unshare(2) constants
15522+
#ifdef CLONE_FILES
15523+
if (PyModule_AddIntMacro(m, CLONE_FILES)) return -1;
15524+
#endif
15525+
#ifdef CLONE_FS
15526+
if (PyModule_AddIntMacro(m, CLONE_FS)) return -1;
15527+
#endif
15528+
#ifdef CLONE_NEWCGROUP
15529+
if (PyModule_AddIntMacro(m, CLONE_NEWCGROUP)) return -1;
15530+
#endif
15531+
#ifdef CLONE_NEWIPC
15532+
if (PyModule_AddIntMacro(m, CLONE_NEWIPC)) return -1;
15533+
#endif
15534+
#ifdef CLONE_NEWNET
15535+
if (PyModule_AddIntMacro(m, CLONE_NEWNET)) return -1;
15536+
#endif
15537+
#ifdef CLONE_NEWNS
15538+
if (PyModule_AddIntMacro(m, CLONE_NEWNS)) return -1;
15539+
#endif
15540+
#ifdef CLONE_NEWPID
15541+
if (PyModule_AddIntMacro(m, CLONE_NEWPID)) return -1;
15542+
#endif
15543+
#ifdef CLONE_NEWTIME
15544+
if (PyModule_AddIntMacro(m, CLONE_NEWTIME)) return -1;
15545+
#endif
15546+
#ifdef CLONE_NEWUSER
15547+
if (PyModule_AddIntMacro(m, CLONE_NEWUSER)) return -1;
15548+
#endif
15549+
#ifdef CLONE_NEWUTS
15550+
if (PyModule_AddIntMacro(m, CLONE_NEWUTS)) return -1;
15551+
#endif
15552+
#ifdef CLONE_SYSVSEM
15553+
if (PyModule_AddIntMacro(m, CLONE_SYSVSEM)) return -1;
15554+
#endif
15555+
#ifdef CLONE_THREAD
15556+
if (PyModule_AddIntMacro(m, CLONE_THREAD)) return -1;
15557+
#endif
15558+
#ifdef CLONE_SIGHAND
15559+
if (PyModule_AddIntMacro(m, CLONE_SIGHAND)) return -1;
15560+
#endif
15561+
#ifdef CLONE_VM
15562+
if (PyModule_AddIntMacro(m, CLONE_VM)) return -1;
15563+
#endif
15564+
1547115565
#if defined(__APPLE__)
1547215566
if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1;
1547315567
if (PyModule_AddIntConstant(m, "_COPYFILE_STAT", COPYFILE_STAT)) return -1;

configure

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

+2-2
Original file line numberDiff line numberDiff line change
@@ -4669,12 +4669,12 @@ AC_CHECK_FUNCS([ \
46694669
rtpSpawn sched_get_priority_max sched_rr_get_interval sched_setaffinity \
46704670
sched_setparam sched_setscheduler sem_clockwait sem_getvalue sem_open \
46714671
sem_timedwait sem_unlink sendfile setegid seteuid setgid sethostname \
4672-
setitimer setlocale setpgid setpgrp setpriority setregid setresgid \
4672+
setitimer setlocale setns setpgid setpgrp setpriority setregid setresgid \
46734673
setresuid setreuid setsid setuid setvbuf shutdown sigaction sigaltstack \
46744674
sigfillset siginterrupt sigpending sigrelse sigtimedwait sigwait \
46754675
sigwaitinfo snprintf splice strftime strlcpy strsignal symlinkat sync \
46764676
sysconf system tcgetpgrp tcsetpgrp tempnam timegm times tmpfile \
4677-
tmpnam tmpnam_r truncate ttyname umask uname unlinkat utimensat utimes vfork \
4677+
tmpnam tmpnam_r truncate ttyname umask uname unlinkat unshare utimensat utimes vfork \
46784678
wait wait3 wait4 waitid waitpid wcscoll wcsftime wcsxfrm wmemcmp writev \
46794679
])
46804680

pyconfig.h.in

+6
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,9 @@
10191019
/* Define to 1 if you have the `setlocale' function. */
10201020
#undef HAVE_SETLOCALE
10211021

1022+
/* Define to 1 if you have the `setns' function. */
1023+
#undef HAVE_SETNS
1024+
10221025
/* Define to 1 if you have the `setpgid' function. */
10231026
#undef HAVE_SETPGID
10241027

@@ -1383,6 +1386,9 @@
13831386
/* Define to 1 if you have the `unlinkat' function. */
13841387
#undef HAVE_UNLINKAT
13851388

1389+
/* Define to 1 if you have the `unshare' function. */
1390+
#undef HAVE_UNSHARE
1391+
13861392
/* Define if you have a useable wchar_t type defined in wchar.h; useable means
13871393
wchar_t must be an unsigned type with at least 16 bits. (see
13881394
Include/unicodeobject.h). */

0 commit comments

Comments
 (0)