In each case, fd refers to a /proc/[pid]/ns/* file.
Both operations return a new file descriptor on success.

NS_GET_USERNS (since Linux 4.9)

Returns a file descriptor that refers to the owning user namespace for the
namespace referred to by fd.

NS_GET_PARENT (since Linux 4.9)

Returns a file descriptor that refers to the parent namespace of the
namespace referred to by fd. This operation is valid only for
hierarchical namespaces (i.e., PID and user namespaces). For user
namespaces, NS_GET_PARENT is synonymous with
NS_GET_USERNS.

The new file descriptor returned by these operations is opened
with the O_RDONLY and O_CLOEXEC (close-on-exec; see
fcntl(2)) flags.

By applying fstat(2) to the returned file descriptor, one
obtains a stat structure whose st_dev (resident device) and
st_ino (inode number) fields together identify the owning/parent
namespace. This inode number can be matched with the inode number of another
/proc/[pid]/ns/{pid,user} file to determine whether that is the
owning/parent namespace.

Either of these ioctl(2) operations can fail with the
following errors:

EPERM

The requested namespace is outside of the caller's namespace scope. This
error can occur if, for example, the owning user namespace is an ancestor
of the caller's current user namespace. It can also occur on attempts to
obtain the parent of the initial user or PID namespace.

ENOTTY

The operation is not supported by this kernel version.

Additionally, the NS_GET_PARENT operation can fail with the
following error:

EINVAL

fd refers to a nonhierarchical namespace.

See the EXAMPLE section for an example of the use of these
operations.

The NS_GET_OWNER_UID operation (available since Linux 4.11) can be used
to discover the owner user ID of a user namespace (i.e., the effective user ID
of the process that created the user namespace). The form of the call is:

uid_t uid;
ioctl(fd, NS_GET_OWNER_UID, &uid);

fd refers to a /proc/[pid]/ns/user file.

The owner user ID is returned in the uid_t pointed to by
the third argument.

The example shown below uses the ioctl(2) operations described above to
perform simple discovery of namespace relationships. The following shell
sessions show various examples of the use of this program.

Trying to get the parent of the initial user namespace fails,
since it has no parent:

Start a shell in a new user namespace, and show that from within
this shell, the parent user namespace can't be discovered. Similarly, the
UTS namespace (which is associated with the initial user namespace) can't be
discovered.

This page is part of release 5.00 of the Linux man-pages project. A
description of the project, information about reporting bugs, and the latest
version of this page, can be found at https://www.kernel.org/doc/man-pages/.