gsphinx.addnodesdocument)}( rawsourcechildren]( translations LanguagesNode)}(hhh](h pending_xref)}(hhh]docutils.nodesTextChinese (Simplified)}parenthsba attributes}(ids]classes]names]dupnames]backrefs] refdomainstdreftypedoc reftarget)/translations/zh_CN/userspace-api/unsharemodnameN classnameN refexplicitutagnamehhh ubh)}(hhh]hChinese (Traditional)}hh2sbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget)/translations/zh_TW/userspace-api/unsharemodnameN classnameN refexplicituh1hhh ubh)}(hhh]hItalian}hhFsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget)/translations/it_IT/userspace-api/unsharemodnameN classnameN refexplicituh1hhh ubh)}(hhh]hJapanese}hhZsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget)/translations/ja_JP/userspace-api/unsharemodnameN classnameN refexplicituh1hhh ubh)}(hhh]hKorean}hhnsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget)/translations/ko_KR/userspace-api/unsharemodnameN classnameN refexplicituh1hhh ubh)}(hhh]hSpanish}hhsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget)/translations/sp_SP/userspace-api/unsharemodnameN classnameN refexplicituh1hhh ubeh}(h]h ]h"]h$]h&]current_languageEnglishuh1h hh _documenthsourceNlineNubhsection)}(hhh](htitle)}(hunshare system callh]hunshare system call}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhhhC/var/lib/git/docbuild/linux/Documentation/userspace-api/unshare.rsthKubh paragraph)}(hThis document describes the new system call, unshare(). The document provides an overview of the feature, why it is needed, how it can be used, its interface specification, design, implementation and how it can be tested.h]hThis document describes the new system call, unshare(). The document provides an overview of the feature, why it is needed, how it can be used, its interface specification, design, implementation and how it can be tested.}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhhhhubh)}(hhh](h)}(h Change Logh]h Change Log}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhhhhhK ubh)}(hKversion 0.1 Initial document, Janak Desai (janak@us.ibm.com), Jan 11, 2006h](h,version 0.1 Initial document, Janak Desai (}(hhhhhNhNubh reference)}(hjanak@us.ibm.comh]hjanak@us.ibm.com}(hhhhhNhNubah}(h]h ]h"]h$]h&]refurimailto:janak@us.ibm.comuh1hhhubh), Jan 11, 2006}(hhhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK hhhhubeh}(h] change-logah ]h"] change logah$]h&]uh1hhhhhhhhK ubh)}(hhh](h)}(hContentsh]hContents}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubh block_quote)}(h1) Overview 2) Benefits 3) Cost 4) Requirements 5) Functional Specification 6) High Level Design 7) Low Level Design 8) Test Specification 9) Future Work h]henumerated_list)}(hhh](h list_item)}(hOverviewh]h)}(hj$h]hOverview}(hj&hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj"ubah}(h]h ]h"]h$]h&]uh1j hjubj!)}(hBenefitsh]h)}(hj;h]hBenefits}(hj=hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj9ubah}(h]h ]h"]h$]h&]uh1j hjubj!)}(hCosth]h)}(hjRh]hCost}(hjThhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjPubah}(h]h ]h"]h$]h&]uh1j hjubj!)}(h Requirementsh]h)}(hjih]h Requirements}(hjkhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjgubah}(h]h ]h"]h$]h&]uh1j hjubj!)}(hFunctional Specificationh]h)}(hjh]hFunctional Specification}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj~ubah}(h]h ]h"]h$]h&]uh1j hjubj!)}(hHigh Level Designh]h)}(hjh]hHigh Level Design}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1j hjubj!)}(hLow Level Designh]h)}(hjh]hLow Level Design}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1j hjubj!)}(hTest Specificationh]h)}(hjh]hTest Specification}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1j hjubj!)}(h Future Work h]h)}(h Future Workh]h Future Work}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1j hjubeh}(h]h ]h"]h$]h&]enumtypearabicprefixhsuffix)uh1jhjubah}(h]h ]h"]h$]h&]uh1jhhhKhjhhubeh}(h]contentsah ]h"]contentsah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(h 1) Overviewh]h 1) Overview}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj hhhhhKubh)}(hXRMost legacy operating system kernels support an abstraction of threads as multiple execution contexts within a process. These kernels provide special resources and mechanisms to maintain these "threads". The Linux kernel, in a clever and simple manner, does not make distinction between processes and "threads". The kernel allows processes to share resources and thus they can achieve legacy "threads" behavior without requiring additional data structures and mechanisms in the kernel. The power of implementing threads in this manner comes not only from its simplicity but also from allowing application programmers to work outside the confinement of all-or-nothing shared resources of legacy threads. On Linux, at the time of thread creation using the clone system call, applications can selectively choose which resources to share between threads.h]hX^Most legacy operating system kernels support an abstraction of threads as multiple execution contexts within a process. These kernels provide special resources and mechanisms to maintain these “threads”. The Linux kernel, in a clever and simple manner, does not make distinction between processes and “threads”. The kernel allows processes to share resources and thus they can achieve legacy “threads” behavior without requiring additional data structures and mechanisms in the kernel. The power of implementing threads in this manner comes not only from its simplicity but also from allowing application programmers to work outside the confinement of all-or-nothing shared resources of legacy threads. On Linux, at the time of thread creation using the clone system call, applications can selectively choose which resources to share between threads.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj hhubh)}(hXgunshare() system call adds a primitive to the Linux thread model that allows threads to selectively 'unshare' any resources that were being shared at the time of their creation. unshare() was conceptualized by Al Viro in the August of 2000, on the Linux-Kernel mailing list, as part of the discussion on POSIX threads on Linux. unshare() augments the usefulness of Linux threads for applications that would like to control shared resources without creating a new process. unshare() is a natural addition to the set of available primitives on Linux that implement the concept of process/thread as a virtual machine.h]hXkunshare() system call adds a primitive to the Linux thread model that allows threads to selectively ‘unshare’ any resources that were being shared at the time of their creation. unshare() was conceptualized by Al Viro in the August of 2000, on the Linux-Kernel mailing list, as part of the discussion on POSIX threads on Linux. unshare() augments the usefulness of Linux threads for applications that would like to control shared resources without creating a new process. unshare() is a natural addition to the set of available primitives on Linux that implement the concept of process/thread as a virtual machine.}(hj*hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK*hj hhubeh}(h]overviewah ]h"] 1) overviewah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(h 2) Benefitsh]h 2) Benefits}(hjChhhNhNubah}(h]h ]h"]h$]h&]uh1hhj@hhhhhK5ubh)}(hXunshare() would be useful to large application frameworks such as PAM where creating a new process to control sharing/unsharing of process resources is not possible. Since namespaces are shared by default when creating a new process using fork or clone, unshare() can benefit even non-threaded applications if they have a need to disassociate from default shared namespace. The following lists two use-cases where unshare() can be used.h]hXunshare() would be useful to large application frameworks such as PAM where creating a new process to control sharing/unsharing of process resources is not possible. Since namespaces are shared by default when creating a new process using fork or clone, unshare() can benefit even non-threaded applications if they have a need to disassociate from default shared namespace. The following lists two use-cases where unshare() can be used.}(hjQhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK7hj@hhubh)}(hhh](h)}(h#2.1 Per-security context namespacesh]h#2.1 Per-security context namespaces}(hjbhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj_hhhhhK@ubh)}(hX$unshare() can be used to implement polyinstantiated directories using the kernel's per-process namespace mechanism. Polyinstantiated directories, such as per-user and/or per-security context instance of /tmp, /var/tmp or per-security context instance of a user's home directory, isolate user processes when working with these directories. Using unshare(), a PAM module can easily setup a private namespace for a user at login. Polyinstantiated directories are required for Common Criteria certification with Labeled System Protection Profile, however, with the availability of shared-tree feature in the Linux kernel, even regular Linux systems can benefit from setting up private namespaces at login and polyinstantiating /tmp, /var/tmp and other directories deemed appropriate by system administrators.h]hX(unshare() can be used to implement polyinstantiated directories using the kernel’s per-process namespace mechanism. Polyinstantiated directories, such as per-user and/or per-security context instance of /tmp, /var/tmp or per-security context instance of a user’s home directory, isolate user processes when working with these directories. Using unshare(), a PAM module can easily setup a private namespace for a user at login. Polyinstantiated directories are required for Common Criteria certification with Labeled System Protection Profile, however, with the availability of shared-tree feature in the Linux kernel, even regular Linux systems can benefit from setting up private namespaces at login and polyinstantiating /tmp, /var/tmp and other directories deemed appropriate by system administrators.}(hjphhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKBhj_hhubeh}(h]per-security-context-namespacesah ]h"]#2.1 per-security context namespacesah$]h&]uh1hhj@hhhhhK@ubh)}(hhh](h)}(h12.2 unsharing of virtual memory and/or open filesh]h12.2 unsharing of virtual memory and/or open files}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKPubh)}(hXConsider a client/server application where the server is processing client requests by creating processes that share resources such as virtual memory and open files. Without unshare(), the server has to decide what needs to be shared at the time of creating the process which services the request. unshare() allows the server an ability to disassociate parts of the context during the servicing of the request. For large and complex middleware application frameworks, this ability to unshare() after the process was created can be very useful.h]hXConsider a client/server application where the server is processing client requests by creating processes that share resources such as virtual memory and open files. Without unshare(), the server has to decide what needs to be shared at the time of creating the process which services the request. unshare() allows the server an ability to disassociate parts of the context during the servicing of the request. For large and complex middleware application frameworks, this ability to unshare() after the process was created can be very useful.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKRhjhhubeh}(h]-unsharing-of-virtual-memory-and-or-open-filesah ]h"]12.2 unsharing of virtual memory and/or open filesah$]h&]uh1hhj@hhhhhKPubeh}(h]benefitsah ]h"] 2) benefitsah$]h&]uh1hhhhhhhhK5ubh)}(hhh](h)}(h3) Costh]h3) Cost}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhK]ubh)}(hXQIn order to not duplicate code and to handle the fact that unshare() works on an active task (as opposed to clone/fork working on a newly allocated inactive task) unshare() had to make minor reorganizational changes to copy_* functions utilized by clone/fork system call. There is a cost associated with altering existing, well tested and stable code to implement a new feature that may not get exercised extensively in the beginning. However, with proper design and code review of the changes and creation of an unshare() test for the LTP the benefits of this new feature can exceed its cost.h]hXQIn order to not duplicate code and to handle the fact that unshare() works on an active task (as opposed to clone/fork working on a newly allocated inactive task) unshare() had to make minor reorganizational changes to copy_* functions utilized by clone/fork system call. There is a cost associated with altering existing, well tested and stable code to implement a new feature that may not get exercised extensively in the beginning. However, with proper design and code review of the changes and creation of an unshare() test for the LTP the benefits of this new feature can exceed its cost.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK_hjhhubeh}(h]costah ]h"]3) costah$]h&]uh1hhhhhhhhK]ubh)}(hhh](h)}(h4) Requirementsh]h4) Requirements}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKjubh)}(hXunshare() reverses sharing that was done using clone(2) system call, so unshare() should have a similar interface as clone(2). That is, since flags in clone(int flags, void \*stack) specifies what should be shared, similar flags in unshare(int flags) should specify what should be unshared. Unfortunately, this may appear to invert the meaning of the flags from the way they are used in clone(2). However, there was no easy solution that was less confusing and that allowed incremental context unsharing in future without an ABI change.h]hXunshare() reverses sharing that was done using clone(2) system call, so unshare() should have a similar interface as clone(2). That is, since flags in clone(int flags, void *stack) specifies what should be shared, similar flags in unshare(int flags) should specify what should be unshared. Unfortunately, this may appear to invert the meaning of the flags from the way they are used in clone(2). However, there was no easy solution that was less confusing and that allowed incremental context unsharing in future without an ABI change.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKlhjhhubh)}(hX unshare() interface should accommodate possible future addition of new context flags without requiring a rebuild of old applications. If and when new context flags are added, unshare() design should allow incremental unsharing of those resources on an as needed basis.h]hX unshare() interface should accommodate possible future addition of new context flags without requiring a rebuild of old applications. If and when new context flags are added, unshare() design should allow incremental unsharing of those resources on an as needed basis.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKuhjhhubeh}(h] requirementsah ]h"]4) requirementsah$]h&]uh1hhhhhhhhKjubh)}(hhh](h)}(h5) Functional Specificationh]h5) Functional Specification}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhK{ubhdefinition_list)}(hhh](hdefinition_list_item)}(hCNAME unshare - disassociate parts of the process execution context h](hterm)}(hNAMEh]hNAME}(hj/hhhNhNubah}(h]h ]h"]h$]h&]uh1j-hhhK~hj)ubh definition)}(hhh]h)}(h=unshare - disassociate parts of the process execution contexth]h=unshare - disassociate parts of the process execution context}(hjBhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK~hj?ubah}(h]h ]h"]h$]h&]uh1j=hj)ubeh}(h]h ]h"]h$]h&]uh1j'hhhK~hj$ubj()}(h5SYNOPSIS #include int unshare(int flags); h](j.)}(hSYNOPSISh]hSYNOPSIS}(hj`hhhNhNubah}(h]h ]h"]h$]h&]uh1j-hhhKhj\ubj>)}(hhh](h)}(h#include h]h#include }(hjqhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjnubh)}(hint unshare(int flags);h]hint unshare(int flags);}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjnubeh}(h]h ]h"]h$]h&]uh1j=hj\ubeh}(h]h ]h"]h$]h&]uh1j'hhhKhj$hhubj()}(hXDESCRIPTION unshare() allows a process to disassociate parts of its execution context that are currently being shared with other processes. Part of execution context, such as the namespace, is shared by default when a new process is created using fork(2), while other parts, such as the virtual memory, open file descriptors, etc, may be shared by explicit request to share them when creating a process using clone(2). The main use of unshare() is to allow a process to control its shared execution context without creating a new process. The flags argument specifies one or bitwise-or'ed of several of the following constants. CLONE_FS If CLONE_FS is set, file system information of the caller is disassociated from the shared file system information. CLONE_FILES If CLONE_FILES is set, the file descriptor table of the caller is disassociated from the shared file descriptor table. CLONE_NEWNS If CLONE_NEWNS is set, the namespace of the caller is disassociated from the shared namespace. CLONE_VM If CLONE_VM is set, the virtual memory of the caller is disassociated from the shared virtual memory. h](j.)}(h DESCRIPTIONh]h DESCRIPTION}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j-hhhKhjubj>)}(hhh](h)}(hXunshare() allows a process to disassociate parts of its execution context that are currently being shared with other processes. Part of execution context, such as the namespace, is shared by default when a new process is created using fork(2), while other parts, such as the virtual memory, open file descriptors, etc, may be shared by explicit request to share them when creating a process using clone(2).h]hXunshare() allows a process to disassociate parts of its execution context that are currently being shared with other processes. Part of execution context, such as the namespace, is shared by default when a new process is created using fork(2), while other parts, such as the virtual memory, open file descriptors, etc, may be shared by explicit request to share them when creating a process using clone(2).}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubh)}(hwThe main use of unshare() is to allow a process to control its shared execution context without creating a new process.h]hwThe main use of unshare() is to allow a process to control its shared execution context without creating a new process.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubh)}(hXThe flags argument specifies one or bitwise-or'ed of several of the following constants.h]hZThe flags argument specifies one or bitwise-or’ed of several of the following constants.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubj#)}(hhh](j()}(h}CLONE_FS If CLONE_FS is set, file system information of the caller is disassociated from the shared file system information. h](j.)}(hCLONE_FSh]hCLONE_FS}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j-hhhKhjubj>)}(hhh]h)}(hsIf CLONE_FS is set, file system information of the caller is disassociated from the shared file system information.h]hsIf CLONE_FS is set, file system information of the caller is disassociated from the shared file system information.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1j=hjubeh}(h]h ]h"]h$]h&]uh1j'hhhKhjubj()}(hCLONE_FILES If CLONE_FILES is set, the file descriptor table of the caller is disassociated from the shared file descriptor table. h](j.)}(h CLONE_FILESh]h CLONE_FILES}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j-hhhKhj ubj>)}(hhh]h)}(hvIf CLONE_FILES is set, the file descriptor table of the caller is disassociated from the shared file descriptor table.h]hvIf CLONE_FILES is set, the file descriptor table of the caller is disassociated from the shared file descriptor table.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1j=hj ubeh}(h]h ]h"]h$]h&]uh1j'hhhKhjubj()}(hkCLONE_NEWNS If CLONE_NEWNS is set, the namespace of the caller is disassociated from the shared namespace. h](j.)}(h CLONE_NEWNSh]h CLONE_NEWNS}(hj=hhhNhNubah}(h]h ]h"]h$]h&]uh1j-hhhKhj9ubj>)}(hhh]h)}(h^If CLONE_NEWNS is set, the namespace of the caller is disassociated from the shared namespace.h]h^If CLONE_NEWNS is set, the namespace of the caller is disassociated from the shared namespace.}(hjNhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjKubah}(h]h ]h"]h$]h&]uh1j=hj9ubeh}(h]h ]h"]h$]h&]uh1j'hhhKhjubj()}(hoCLONE_VM If CLONE_VM is set, the virtual memory of the caller is disassociated from the shared virtual memory. h](j.)}(hCLONE_VMh]hCLONE_VM}(hjlhhhNhNubah}(h]h ]h"]h$]h&]uh1j-hhhKhjhubj>)}(hhh]h)}(heIf CLONE_VM is set, the virtual memory of the caller is disassociated from the shared virtual memory.h]heIf CLONE_VM is set, the virtual memory of the caller is disassociated from the shared virtual memory.}(hj}hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjzubah}(h]h ]h"]h$]h&]uh1j=hjhubeh}(h]h ]h"]h$]h&]uh1j'hhhKhjubeh}(h]h ]h"]h$]h&]uh1j"hjubeh}(h]h ]h"]h$]h&]uh1j=hjubeh}(h]h ]h"]h$]h&]uh1j'hhhKhj$hhubj()}(hPRETURN VALUE On success, zero returned. On failure, -1 is returned and errno is h](j.)}(h RETURN VALUEh]h RETURN VALUE}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j-hhhKhjubj>)}(hhh]h)}(hBOn success, zero returned. On failure, -1 is returned and errno ish]hBOn success, zero returned. On failure, -1 is returned and errno is}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1j=hjubeh}(h]h ]h"]h$]h&]uh1j'hhhKhj$hhubj()}(hX ERRORS EPERM CLONE_NEWNS was specified by a non-root process (process without CAP_SYS_ADMIN). ENOMEM Cannot allocate sufficient memory to copy parts of caller's context that need to be unshared. EINVAL Invalid flag was specified as an argument. h](j.)}(hERRORSh]hERRORS}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j-hhhKhjubj>)}(hhh](j#)}(hhh](j()}(hYEPERM CLONE_NEWNS was specified by a non-root process (process without CAP_SYS_ADMIN). h](j.)}(h@EPERM CLONE_NEWNS was specified by a non-root process (processh]h@EPERM CLONE_NEWNS was specified by a non-root process (process}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j-hhhKhjubj>)}(hhh]h)}(hwithout CAP_SYS_ADMIN).h]hwithout CAP_SYS_ADMIN).}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1j=hjubeh}(h]h ]h"]h$]h&]uh1j'hhhKhjubj()}(hfENOMEM Cannot allocate sufficient memory to copy parts of caller's context that need to be unshared. h](j.)}(hCENOMEM Cannot allocate sufficient memory to copy parts of caller'sh]hEENOMEM Cannot allocate sufficient memory to copy parts of caller’s}(hj#hhhNhNubah}(h]h ]h"]h$]h&]uh1j-hhhKhjubj>)}(hhh]h)}(h!context that need to be unshared.h]h!context that need to be unshared.}(hj4hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj1ubah}(h]h ]h"]h$]h&]uh1j=hjubeh}(h]h ]h"]h$]h&]uh1j'hhhKhjubeh}(h]h ]h"]h$]h&]uh1j"hjubh)}(h2EINVAL Invalid flag was specified as an argument.h]h2EINVAL Invalid flag was specified as an argument.}(hjThhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubeh}(h]h ]h"]h$]h&]uh1j=hjubeh}(h]h ]h"]h$]h&]uh1j'hhhKhj$hhubj()}(hqCONFORMING TO The unshare() call is Linux-specific and should not be used in programs intended to be portable. h](j.)}(h CONFORMING TOh]h CONFORMING TO}(hjrhhhNhNubah}(h]h ]h"]h$]h&]uh1j-hhhKhjnubj>)}(hhh]h)}(hbThe unshare() call is Linux-specific and should not be used in programs intended to be portable.h]hbThe unshare() call is Linux-specific and should not be used in programs intended to be portable.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1j=hjnubeh}(h]h ]h"]h$]h&]uh1j'hhhKhj$hhubj()}(hSEE ALSO clone(2), fork(2) h](j.)}(hSEE ALSOh]hSEE ALSO}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j-hhhKhjubj>)}(hhh]h)}(hclone(2), fork(2)h]hclone(2), fork(2)}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1j=hjubeh}(h]h ]h"]h$]h&]uh1j'hhhKhj$hhubeh}(h]h ]h"]h$]h&]uh1j"hjhhhhhNubeh}(h]functional-specificationah ]h"]5) functional specificationah$]h&]uh1hhhhhhhhK{ubh)}(hhh](h)}(h6) High Level Designh]h6) High Level Design}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubh)}(hXDepending on the flags argument, the unshare() system call allocates appropriate process context structures, populates it with values from the current shared version, associates newly duplicated structures with the current task structure and releases corresponding shared versions. Helper functions of clone (copy_*) could not be used directly by unshare() because of the following two reasons.h]hXDepending on the flags argument, the unshare() system call allocates appropriate process context structures, populates it with values from the current shared version, associates newly duplicated structures with the current task structure and releases corresponding shared versions. Helper functions of clone (copy_*) could not be used directly by unshare() because of the following two reasons.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubj)}(hX1) clone operates on a newly allocated not-yet-active task structure, where as unshare() operates on the current active task. Therefore unshare() has to take appropriate task_lock() before associating newly duplicated context structures 2) unshare() has to allocate and duplicate all context structures that are being unshared, before associating them with the current task and releasing older shared structures. Failure do so will create race conditions and/or oops when trying to backout due to an error. Consider the case of unsharing both virtual memory and namespace. After successfully unsharing vm, if the system call encounters an error while allocating new namespace structure, the error return code will have to reverse the unsharing of vm. As part of the reversal the system call will have to go back to older, shared, vm structure, which may not exist anymore. h]j)}(hhh](j!)}(hclone operates on a newly allocated not-yet-active task structure, where as unshare() operates on the current active task. Therefore unshare() has to take appropriate task_lock() before associating newly duplicated context structures h]h)}(hclone operates on a newly allocated not-yet-active task structure, where as unshare() operates on the current active task. Therefore unshare() has to take appropriate task_lock() before associating newly duplicated context structuresh]hclone operates on a newly allocated not-yet-active task structure, where as unshare() operates on the current active task. Therefore unshare() has to take appropriate task_lock() before associating newly duplicated context structures}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1j hjubj!)}(hXyunshare() has to allocate and duplicate all context structures that are being unshared, before associating them with the current task and releasing older shared structures. Failure do so will create race conditions and/or oops when trying to backout due to an error. Consider the case of unsharing both virtual memory and namespace. After successfully unsharing vm, if the system call encounters an error while allocating new namespace structure, the error return code will have to reverse the unsharing of vm. As part of the reversal the system call will have to go back to older, shared, vm structure, which may not exist anymore. h]h)}(hXxunshare() has to allocate and duplicate all context structures that are being unshared, before associating them with the current task and releasing older shared structures. Failure do so will create race conditions and/or oops when trying to backout due to an error. Consider the case of unsharing both virtual memory and namespace. After successfully unsharing vm, if the system call encounters an error while allocating new namespace structure, the error return code will have to reverse the unsharing of vm. As part of the reversal the system call will have to go back to older, shared, vm structure, which may not exist anymore.h]hXxunshare() has to allocate and duplicate all context structures that are being unshared, before associating them with the current task and releasing older shared structures. Failure do so will create race conditions and/or oops when trying to backout due to an error. Consider the case of unsharing both virtual memory and namespace. After successfully unsharing vm, if the system call encounters an error while allocating new namespace structure, the error return code will have to reverse the unsharing of vm. As part of the reversal the system call will have to go back to older, shared, vm structure, which may not exist anymore.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1j hjubeh}(h]h ]h"]h$]h&]jjjhjjuh1jhjubah}(h]h ]h"]h$]h&]uh1jhhhKhjhhubh)}(hXoTherefore code from copy_* functions that allocated and duplicated current context structure was moved into new dup_* functions. Now, copy_* functions call dup_* functions to allocate and duplicate appropriate context structures and then associate them with the task structure that is being constructed. unshare() system call on the other hand performs the following:h]hXoTherefore code from copy_* functions that allocated and duplicated current context structure was moved into new dup_* functions. Now, copy_* functions call dup_* functions to allocate and duplicate appropriate context structures and then associate them with the task structure that is being constructed. unshare() system call on the other hand performs the following:}(hj<hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubj)}(hX11) Check flags to force missing, but implied, flags 2) For each context structure, call the corresponding unshare() helper function to allocate and duplicate a new context structure, if the appropriate bit is set in the flags argument. 3) If there is no error in allocation and duplication and there are new context structures then lock the current task structure, associate new context structures with the current task structure, and release the lock on the current task structure. 4) Appropriately release older, shared, context structures. h]j)}(hhh](j!)}(h1Check flags to force missing, but implied, flags h]h)}(h0Check flags to force missing, but implied, flagsh]h0Check flags to force missing, but implied, flags}(hjUhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjQubah}(h]h ]h"]h$]h&]uh1j hjNubj!)}(hFor each context structure, call the corresponding unshare() helper function to allocate and duplicate a new context structure, if the appropriate bit is set in the flags argument. h]h)}(hFor each context structure, call the corresponding unshare() helper function to allocate and duplicate a new context structure, if the appropriate bit is set in the flags argument.h]hFor each context structure, call the corresponding unshare() helper function to allocate and duplicate a new context structure, if the appropriate bit is set in the flags argument.}(hjmhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjiubah}(h]h ]h"]h$]h&]uh1j hjNubj!)}(hIf there is no error in allocation and duplication and there are new context structures then lock the current task structure, associate new context structures with the current task structure, and release the lock on the current task structure. h]h)}(hIf there is no error in allocation and duplication and there are new context structures then lock the current task structure, associate new context structures with the current task structure, and release the lock on the current task structure.h]hIf there is no error in allocation and duplication and there are new context structures then lock the current task structure, associate new context structures with the current task structure, and release the lock on the current task structure.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1j hjNubj!)}(h9Appropriately release older, shared, context structures. h]h)}(h8Appropriately release older, shared, context structures.h]h8Appropriately release older, shared, context structures.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1j hjNubeh}(h]h ]h"]h$]h&]jjjhjjuh1jhjJubah}(h]h ]h"]h$]h&]uh1jhhhKhjhhubeh}(h]high-level-designah ]h"]6) high level designah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(h7) Low Level Designh]h7) Low Level Design}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubh)}(hNImplementation of unshare() can be grouped in the following 4 different items:h]hNImplementation of unshare() can be grouped in the following 4 different items:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubj)}(ha) Reorganization of existing copy_* functions b) unshare() system call service function c) unshare() helper functions for each different process context d) Registration of system call number for different architectures h]j)}(hhh](j!)}(h,Reorganization of existing copy_* functions h]h)}(h+Reorganization of existing copy_* functionsh]h+Reorganization of existing copy_* functions}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1j hjubj!)}(h'unshare() system call service function h]h)}(h&unshare() system call service functionh]h&unshare() system call service function}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1j hjubj!)}(h>unshare() helper functions for each different process context h]h)}(h=unshare() helper functions for each different process contexth]h=unshare() helper functions for each different process context}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1j hjubj!)}(h?Registration of system call number for different architectures h]h)}(h>Registration of system call number for different architecturesh]h>Registration of system call number for different architectures}(hj7hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj3ubah}(h]h ]h"]h$]h&]uh1j hjubeh}(h]h ]h"]h$]h&]j loweralphajhjjuh1jhjubah}(h]h ]h"]h$]h&]uh1jhhhKhjhhubh)}(hhh](h)}(h'7.1) Reorganization of copy_* functionsh]h'7.1) Reorganization of copy_* functions}(hj[hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjXhhhhhKubh)}(hXREach copy function such as copy_mm, copy_namespace, copy_files, etc, had roughly two components. The first component allocated and duplicated the appropriate structure and the second component linked it to the task structure passed in as an argument to the copy function. The first component was split into its own function. These dup_* functions allocated and duplicated the appropriate context structure. The reorganized copy_* functions invoked their corresponding dup_* functions and then linked the newly duplicated structures to the task structure with which the copy function was called.h]hXREach copy function such as copy_mm, copy_namespace, copy_files, etc, had roughly two components. The first component allocated and duplicated the appropriate structure and the second component linked it to the task structure passed in as an argument to the copy function. The first component was split into its own function. These dup_* functions allocated and duplicated the appropriate context structure. The reorganized copy_* functions invoked their corresponding dup_* functions and then linked the newly duplicated structures to the task structure with which the copy function was called.}(hjihhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjXhhubeh}(h] reorganization-of-copy-functionsah ]h"]'7.1) reorganization of copy_* functionsah$]h&]uh1hhjhhhhhKubh)}(hhh](h)}(h+7.2) unshare() system call service functionh]h+7.2) unshare() system call service function}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubj)}(hX* Check flags Force implied flags. If CLONE_THREAD is set force CLONE_VM. If CLONE_VM is set, force CLONE_SIGHAND. If CLONE_SIGHAND is set and signals are also being shared, force CLONE_THREAD. If CLONE_NEWNS is set, force CLONE_FS. * For each context flag, invoke the corresponding unshare_* helper routine with flags passed into the system call and a reference to pointer pointing the new unshared structure * If any new structures are created by unshare_* helper functions, take the task_lock() on the current task, modify appropriate context pointers, and release the task lock. * For all newly unshared structures, release the corresponding older, shared, structures. h]h bullet_list)}(hhh](j!)}(hCheck flags Force implied flags. If CLONE_THREAD is set force CLONE_VM. If CLONE_VM is set, force CLONE_SIGHAND. If CLONE_SIGHAND is set and signals are also being shared, force CLONE_THREAD. If CLONE_NEWNS is set, force CLONE_FS. h]h)}(hCheck flags Force implied flags. If CLONE_THREAD is set force CLONE_VM. If CLONE_VM is set, force CLONE_SIGHAND. If CLONE_SIGHAND is set and signals are also being shared, force CLONE_THREAD. If CLONE_NEWNS is set, force CLONE_FS.h]hCheck flags Force implied flags. If CLONE_THREAD is set force CLONE_VM. If CLONE_VM is set, force CLONE_SIGHAND. If CLONE_SIGHAND is set and signals are also being shared, force CLONE_THREAD. If CLONE_NEWNS is set, force CLONE_FS.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1j hjubj!)}(hFor each context flag, invoke the corresponding unshare_* helper routine with flags passed into the system call and a reference to pointer pointing the new unshared structure h]h)}(hFor each context flag, invoke the corresponding unshare_* helper routine with flags passed into the system call and a reference to pointer pointing the new unshared structureh]hFor each context flag, invoke the corresponding unshare_* helper routine with flags passed into the system call and a reference to pointer pointing the new unshared structure}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hjubah}(h]h ]h"]h$]h&]uh1j hjubj!)}(hIf any new structures are created by unshare_* helper functions, take the task_lock() on the current task, modify appropriate context pointers, and release the task lock. h]h)}(hIf any new structures are created by unshare_* helper functions, take the task_lock() on the current task, modify appropriate context pointers, and release the task lock.h]hIf any new structures are created by unshare_* helper functions, take the task_lock() on the current task, modify appropriate context pointers, and release the task lock.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1j hjubj!)}(hXFor all newly unshared structures, release the corresponding older, shared, structures. h]h)}(hWFor all newly unshared structures, release the corresponding older, shared, structures.h]hWFor all newly unshared structures, release the corresponding older, shared, structures.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1j hjubeh}(h]h ]h"]h$]h&]bullet*uh1jhhhMhjubah}(h]h ]h"]h$]h&]uh1jhhhMhjhhubeh}(h]$unshare-system-call-service-functionah ]h"]+7.2) unshare() system call service functionah$]h&]uh1hhjhhhhhMubh)}(hhh](h)}(h7.3) unshare_* helper functionsh]h7.3) unshare_* helper functions}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubh)}(hXXFor unshare_* helpers corresponding to CLONE_SYSVSEM, CLONE_SIGHAND, and CLONE_THREAD, return -EINVAL since they are not implemented yet. For others, check the flag value to see if the unsharing is required for that structure. If it is, invoke the corresponding dup_* function to allocate and duplicate the structure and return a pointer to it.h]hXXFor unshare_* helpers corresponding to CLONE_SYSVSEM, CLONE_SIGHAND, and CLONE_THREAD, return -EINVAL since they are not implemented yet. For others, check the flag value to see if the unsharing is required for that structure. If it is, invoke the corresponding dup_* function to allocate and duplicate the structure and return a pointer to it.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubeh}(h]unshare-helper-functionsah ]h"]7.3) unshare_* helper functionsah$]h&]uh1hhjhhhhhMubh)}(hhh](h)}(h 7.4) Finallyh]h 7.4) Finally}(hj9hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj6hhhhhM#ubh)}(hPAppropriately modify architecture specific code to register the new system call.h]hPAppropriately modify architecture specific code to register the new system call.}(hjGhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM%hj6hhubeh}(h]finallyah ]h"] 7.4) finallyah$]h&]uh1hhjhhhhhM#ubeh}(h]low-level-designah ]h"]7) low level designah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(h8) Test Specificationh]h8) Test Specification}(hjhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjehhhhhM)ubh)}(h1The test for unshare() should test the following:h]h1The test for unshare() should test the following:}(hjvhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM+hjehhubj)}(hX1) Valid flags: Test to check that clone flags for signal and signal handlers, for which unsharing is not implemented yet, return -EINVAL. 2) Missing/implied flags: Test to make sure that if unsharing namespace without specifying unsharing of filesystem, correctly unshares both namespace and filesystem information. 3) For each of the four (namespace, filesystem, files and vm) supported unsharing, verify that the system call correctly unshares the appropriate structure. Verify that unsharing them individually as well as in combination with each other works as expected. 4) Concurrent execution: Use shared memory segments and futex on an address in the shm segment to synchronize execution of about 10 threads. Have a couple of threads execute execve, a couple _exit and the rest unshare with different combination of flags. Verify that unsharing is performed as expected and that there are no oops or hangs. h]j)}(hhh](j!)}(hValid flags: Test to check that clone flags for signal and signal handlers, for which unsharing is not implemented yet, return -EINVAL. h]h)}(hValid flags: Test to check that clone flags for signal and signal handlers, for which unsharing is not implemented yet, return -EINVAL.h]hValid flags: Test to check that clone flags for signal and signal handlers, for which unsharing is not implemented yet, return -EINVAL.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM-hjubah}(h]h ]h"]h$]h&]uh1j hjubj!)}(hMissing/implied flags: Test to make sure that if unsharing namespace without specifying unsharing of filesystem, correctly unshares both namespace and filesystem information. h]h)}(hMissing/implied flags: Test to make sure that if unsharing namespace without specifying unsharing of filesystem, correctly unshares both namespace and filesystem information.h]hMissing/implied flags: Test to make sure that if unsharing namespace without specifying unsharing of filesystem, correctly unshares both namespace and filesystem information.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM1hjubah}(h]h ]h"]h$]h&]uh1j hjubj!)}(hFor each of the four (namespace, filesystem, files and vm) supported unsharing, verify that the system call correctly unshares the appropriate structure. Verify that unsharing them individually as well as in combination with each other works as expected. h]h)}(hFor each of the four (namespace, filesystem, files and vm) supported unsharing, verify that the system call correctly unshares the appropriate structure. Verify that unsharing them individually as well as in combination with each other works as expected.h]hFor each of the four (namespace, filesystem, files and vm) supported unsharing, verify that the system call correctly unshares the appropriate structure. Verify that unsharing them individually as well as in combination with each other works as expected.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM5hjubah}(h]h ]h"]h$]h&]uh1j hjubj!)}(hXPConcurrent execution: Use shared memory segments and futex on an address in the shm segment to synchronize execution of about 10 threads. Have a couple of threads execute execve, a couple _exit and the rest unshare with different combination of flags. Verify that unsharing is performed as expected and that there are no oops or hangs. h]h)}(hXOConcurrent execution: Use shared memory segments and futex on an address in the shm segment to synchronize execution of about 10 threads. Have a couple of threads execute execve, a couple _exit and the rest unshare with different combination of flags. Verify that unsharing is performed as expected and that there are no oops or hangs.h]hXOConcurrent execution: Use shared memory segments and futex on an address in the shm segment to synchronize execution of about 10 threads. Have a couple of threads execute execve, a couple _exit and the rest unshare with different combination of flags. Verify that unsharing is performed as expected and that there are no oops or hangs.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM;hjubah}(h]h ]h"]h$]h&]uh1j hjubeh}(h]h ]h"]h$]h&]jjjhjjuh1jhjubah}(h]h ]h"]h$]h&]uh1jhhhM-hjehhubeh}(h]test-specificationah ]h"]8) test specificationah$]h&]uh1hhhhhhhhM)ubh)}(hhh](h)}(h9) Future Workh]h9) Future Work}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMCubh)}(hXThe current implementation of unshare() does not allow unsharing of signals and signal handlers. Signals are complex to begin with and to unshare signals and/or signal handlers of a currently running process is even more complex. If in the future there is a specific need to allow unsharing of signals and/or signal handlers, it can be incrementally added to unshare() without affecting legacy applications using unshare().h]hXThe current implementation of unshare() does not allow unsharing of signals and signal handlers. Signals are complex to begin with and to unshare signals and/or signal handlers of a currently running process is even more complex. If in the future there is a specific need to allow unsharing of signals and/or signal handlers, it can be incrementally added to unshare() without affecting legacy applications using unshare().}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMEhjhhubeh}(h] future-workah ]h"]9) future workah$]h&]uh1hhhhhhhhMCubeh}(h]unshare-system-callah ]h"]unshare system callah$]h&]uh1hhhhhhhhKubeh}(h]h ]h"]h$]h&]sourcehuh1hcurrent_sourceN current_lineNsettingsdocutils.frontendValues)}(hN generatorN datestampN source_linkN source_urlN toc_backlinksentryfootnote_backlinksK sectnum_xformKstrip_commentsNstrip_elements_with_classesN strip_classesN report_levelK halt_levelKexit_status_levelKdebugNwarning_streamN tracebackinput_encoding utf-8-siginput_encoding_error_handlerstrictoutput_encodingutf-8output_encoding_error_handlerjQ error_encodingutf-8error_encoding_error_handlerbackslashreplace language_codeenrecord_dependenciesNconfigN id_prefixhauto_id_prefixid dump_settingsNdump_internalsNdump_transformsNdump_pseudo_xmlNexpose_internalsNstrict_visitorN_disable_configN_sourceh _destinationN _config_files]7/var/lib/git/docbuild/linux/Documentation/docutils.confafile_insertion_enabled raw_enabledKline_length_limitM'pep_referencesN pep_base_urlhttps://peps.python.org/pep_file_url_templatepep-%04drfc_referencesN rfc_base_url&https://datatracker.ietf.org/doc/html/ tab_widthKtrim_footnote_reference_spacesyntax_highlightlong smart_quotessmartquotes_locales]character_level_inline_markupdoctitle_xform docinfo_xformKsectsubtitle_xform image_loadinglinkembed_stylesheetcloak_email_addressessection_self_linkenvNubreporterNindirect_targets]substitution_defs}substitution_names}refnames}refids}nameids}(j+ j( jhjjj=j:jjjjjjjjjj jjjjjbj_j|jyj j j3j0jZjWjjj# j u nametypes}(j+ jjj=jjjjjjjjbj|j j3jZjj# uh}(j( hhhjjj:j jj@jj_jjjjj jjjjjj_jjyjXj jj0jjWj6jjej ju footnote_refs} citation_refs} autofootnotes]autofootnote_refs]symbol_footnotes]symbol_footnote_refs] footnotes] citations]autofootnote_startKsymbol_footnote_startK id_counter collectionsCounter}Rparse_messages]transform_messages] transformerN include_log] decorationNhhub.