Text of docs.kernel.org › userspace-api › unshare.html as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

unshare system call — The Linux Kernel documentation

The Linux Kernel 7.4.0-rc3 Quick search Contents Working with the development community Development process Submitting patches Code of conduct Maintainer handbook All development-process docs Internal API manuals Core API Driver APIs Subsystems Locking Development tools and processes Licensing rules Writing documentation Development tools Testing guide Hacking guide Tracing Fault injection Livepatching Rust User-oriented documentation Administration Build system Reporting issues Userspace tools Userspace API System calls unshare system call futex2 eBPF Userspace API IOCTLs Introduction of mseal Restartable Sequences Security-related interfaces Devices and I/O Everything else Firmware-related documentation Firmware Firmware and Devicetree Architecture-specific documentation CPU architectures Other documentation Unsorted documentation Translations Translations This Page Show Source unshare system call ¶ This 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. Change Log ¶ version 0.1 Initial document, Janak Desai ( janak @ us . ibm . com ), Jan 11, 2006 Contents ¶ Overview Benefits Cost Requirements Functional Specification High Level Design Low Level Design Test Specification Future Work 1) Overview ¶ 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. unshare() 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.

2) Benefits ¶ unshare() 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.

2.1 Per-security context namespaces ¶ 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. 2.2 unsharing of virtual memory and/or open files ¶ Consider 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. 3) Cost ¶ In 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. 4) Requirements ¶ unshare() 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. 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. 5) Functional Specification ¶ NAME unshare - disassociate parts of the process execution context SYNOPSIS #include <sched.h> int unshare(int flags); DESCRIPTION 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.

RETURN VALUE On success, zero returned. On failure, -1 is returned and errno is 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. CONFORMING TO The unshare() call is Linux-specific and should not be used in programs intended to be portable.

SEE ALSO clone(2), fork(2) 6) High Level Design ¶ Depending on the flags argument, the unshare() system cal

The rest of this page is on the live site.