---
name: "jail_set"
section: "2"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["freebsd"]
url: "https://new.carta.tech/jail/2"
---

# jail\_set(2)

create and manage system jails

Part of [jail(2)](/jail/2.md).

## NAME

**jail**, **jail\_get**, **jail\_set**, **jail\_remove**, **jail\_attach**, **jail\_remove\_jd**, **jail\_attach\_jd** – create and manage system jails

## LIBRARY

Standard C Library (libc, -lc)

## SYNOPSIS

**#include \<sys/param.h\>**

**#include \<sys/jail.h\>**

*int*\

**jail**(*struct jail \*jail*);

*int*\

**jail\_attach**(*int jid*);

*int*\

**jail\_remove**(*int jid*);

*int*\

**jail\_attach\_jd**(*int fd*);

*int*\

**jail\_remove\_jd**(*int fd*);

**#include \<sys/uio.h\>**

*int*\

**jail\_get**(*struct iovec \*iov*, *u\_int niov*, *int flags*);

*int*\

**jail\_set**(*struct iovec \*iov*, *u\_int niov*, *int flags*);

## DESCRIPTION

The **jail**() system call sets up a jail and locks the current process in it.

The argument is a pointer to a structure describing the prison:

```
struct jail {
	uint32_t	version;
	char		*path;
	char		*hostname;
	char		*jailname;
	unsigned int	ip4s;
	unsigned int	ip6s;
	struct in_addr	*ip4;
	struct in6_addr	*ip6;
};
```

“version” defines the version of the API in use. JAIL\_API\_VERSION is defined for the current version.

The “path” pointer should be set to the directory which is to be the root of the prison.

The “hostname” pointer can be set to the hostname of the prison. This can be changed from the inside of the prison.

The “jailname” pointer is an optional name that can be assigned to the jail for example for management purposes.

The “ip4s” and “ip6s” give the numbers of IPv4 and IPv6 addresses that will be passed via their respective pointers.

The “ip4” and “ip6” pointers can be set to an arrays of IPv4 and IPv6 addresses to be assigned to the prison, or NULL if none. IPv4 addresses must be in network byte order.

This is equivalent to, and deprecated in favor of, the **jail\_set**() system call (see below), with the parameters *path*, *host.hostname*, *name*, *ip4.addr*, and *ip6.addr*, and with the JAIL\_ATTACH flag.

The **jail\_set**() system call creates a new jail, or modifies an existing one, and optionally locks the current process in it. Jail parameters are passed as an array of name-value pairs in the array *iov*, containing *niov* elements. Parameter names are a null-terminated string, and values may be strings, integers, or other arbitrary data. Some parameters are boolean, and do not have a value (their length is zero) but are set by the name alone with or without a “no” prefix, e.g. *persist* or *nopersist*. Any parameters not set will be given default values, generally based on the current environment.

Jails have a set of core parameters, and modules can add their own jail parameters. The current set of available parameters, and their formats, can be retrieved via the *security.jail.param* sysctl MIB entry. Notable parameters include those mentioned in the **jail**() description above, as well as *jid* and *name*, which identify the jail being created or modified. See [jail(8)](/jail/8.md) for more information on the core jail parameters.

The *flags* arguments consists of one or more of the following flags:

- JAIL\_CREATE

  Create a new jail. If a *jid* or *name* parameters exists, they must not refer to an existing jail.

- JAIL\_UPDATE

  Modify an existing jail. One of the *jid* or *name* parameters must exist, and must refer to an existing jail. If both JAIL\_CREATE and JAIL\_UPDATE are set, a jail will be created if it does not yet exist, and modified if it does exist.

- JAIL\_ATTACH

  In addition to creating or modifying the jail, attach the current process to it, as with the **jail\_attach**() system call.

- JAIL\_DYING

  This is deprecated in **jail\_set**() and has no effect.

- JAIL\_USE\_DESC

  Identify the jail by a descriptor in the *desc* parameter.

- JAIL\_AT\_DESC

  Operate in the context of the jail described by the *desc* parameter, instead of the current jail. Only one of JAIL\_USE\_DESC or JAIL\_AT\_DESC may be specified.

- JAIL\_GET\_DESC

  Return a new jail descriptor for the jail in the *desc* parameter.

- JAIL\_OWN\_DESC

  Return an “owning” jail descriptor in the *desc* parameter.

The **jail\_get**() system call retrieves jail parameters, using the same name-value list as **jail\_set**() in the *iov* and *niov* arguments. The jail to read can be specified by either *jid* or *name* by including those parameters in the list. If they are included but are not intended to be the search key, they should be cleared (zero and the empty string respectively).

The special parameter *lastjid* can be used to retrieve a list of all jails. It will fetch the jail with the jid above and closest to the passed value. The first jail (usually but not always jid 1) can be found by passing a *lastjid* of zero.

The *flags* arguments consists of one or more following flags:

- JAIL\_DYING

  Allow getting a jail that is in the process of being removed.

- JAIL\_USE\_DESC, JAIL\_AT\_DESC, JAIL\_GET\_DESC, JAIL\_OWN\_DESC

  These have the same meaning as they do in **jail\_set**().

The **jail\_attach**() system call attaches the current process to an existing jail, identified by *jid*. It changes the process's root and current directories to the jail's *path* directory.

The **jail\_remove**() system call removes the jail identified by *jid*. It will kill all processes belonging to the jail, and remove any children of that jail.

The **jail\_attach\_fd**() and **jail\_remove\_fd**() system calls work the same as **jail\_attach**() and **jail\_remove**(), except that they operate on the jail identified by jail descriptor *fd*.

### Jail Descriptors

In addition to the jail ID, jails can be referred to using a jail descriptor, a type of file descriptor tied to a particular jail. Jail descriptors are created by calling **jail\_set**() or **jail\_get**() with the special parameter *desc*, and either the JAIL\_GET\_DESC or JAIL\_OWN\_DESC flags set. The difference between the two flags is that descriptors created with JAIL\_OWN\_DESC( called “owning” descriptors) will automatically remove the jail when the descriptor is closed.

Jail descriptors can be passed back to **jail\_set**() or with the *desc* parameter, and either the JAIL\_USE\_DESC or JAIL\_AT\_DESC flags set. With JAIL\_USE\_DESC, the descriptor identifies the jail to operate on, instead of the *jid* or *name* parameter. With JAIL\_AT\_DESC, the descriptor is used in place of the current jail, allowing accessing or creating jails that are children of the descriptor jail.

The system calls **jail\_attach\_jd**() and **jail\_remove\_jd**() work the same as **jail\_attach**() and **jail\_remove**(), except that they operate on the jail referred to by the passed descriptor.

## RETURN VALUES

If successful, **jail**(), **jail\_set**(), and **jail\_get**() return a non-negative integer, termed the jail identifier (JID). They return -1 on failure, and set *errno* to indicate the error.

The **jail\_attach()**, **jail\_remove()**, **jail\_attach\_jd()**, and **jail\_remove\_jd()** functions return the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error.

## ERRORS

The **jail**() system call will fail if:

- \[EPERM\]

  This process is not allowed to create a jail, either because it is not the super-user, or because it would exceed the jail's *children.max* limit.

- \[EFAULT\]

  *jail* points to an address outside the allocated address space of the process.

- \[EINVAL\]

  The version number of the argument is not correct.

- \[EAGAIN\]

  No free JID could be found.

The **jail\_set**() system call will fail if:

- \[EPERM\]

  This process is not allowed to create a jail, either because it is not the super-user, or because it would exceed the jail's *children.max* limit.

- \[EPERM\]

  The jail descriptor in the *desc* parameter was created by a user other than the super-user, and the JAIL\_USE\_DESC flag was set.

- \[EPERM\]

  A jail parameter was set to a less restrictive value then the current environment.

- \[EFAULT\]

  *Iov*, or one of the addresses contained within it, points to an address outside the allocated address space of the process.

- \[ENOENT\]

  The jail referred to by a *jid* or *name* parameter does not exist, and the JAIL\_CREATE flag is not set.

- \[ENOENT\]

  The jail referred to by a *jid* parameter is not accessible by the process, because the process is in a different jail.

- \[ENOENT\]

  The jail referred to by a *desc* parameter has been removed.

- \[EEXIST\]

  The jail referred to by a *jid* or *name* parameter exists, and the JAIL\_UPDATE flag is not set.

- \[EINVAL\]

  A supplied parameter is the wrong size.

- \[EINVAL\]

  A supplied parameter is out of range.

- \[EINVAL\]

  A supplied string parameter is not null-terminated.

- \[EINVAL\]

  A supplied parameter name does not match any known parameters.

- \[EINVAL\]

  One of the JAIL\_CREATE or JAIL\_UPDATE flags is not set.

- \[ENAMETOOLONG\]

  A supplied string parameter is longer than allowed.

- \[EAGAIN\]

  There are no jail IDs left.

- \[EMFILE\]

  A jail descriptor could not be created for the *desc* parameter with either the JAIL\_GET\_DESC or JAIL\_OWN\_DESC flag set, because the process has already reached its limit for open file descriptors.

- \[ENFILE\]

  A jail descriptor could not be created for the *desc* parameter with either the JAIL\_GET\_DESC or JAIL\_OWN\_DESC flag set, because the system file table is full.

The **jail\_get**() system call will fail if:

- \[ENOENT\]

  The jail referred to by a *jid* or *name* parameter does not exist.

- \[ENOENT\]

  The jail referred to by a *jid* is not accessible by the process, because the process is in a different jail.

- \[ENOENT\]

  The *lastjid* parameter is greater than the highest current jail ID.

- \[ENOENT\]

  The jail referred to by a *desc* parameter has been removed (even if the JAIL\_CREATE flag has been set).

- \[EINVAL\]

  A supplied parameter is the wrong size.

- \[EINVAL\]

  A supplied parameter is out of range.

- \[EINVAL\]

  A supplied string parameter is not null-terminated.

- \[EINVAL\]

  A supplied parameter name does not match any known parameters.

- \[EMFILE\]

  A jail descriptor could not be created for the *desc* parameter with either the JAIL\_GET\_DESC or JAIL\_OWN\_DESC flag set, because the process has already reached its limit for open file descriptors.

- \[ENFILE\]

  A jail descriptor could not be created for the *desc* parameter with either the JAIL\_GET\_DESC or JAIL\_OWN\_DESC flag set, because the system file table is full.

The **jail\_attach**() and **jail\_remove**() system calls will fail if:

- \[EPERM\]

  A user other than the super-user attempted to attach to or remove a jail.

- \[EINVAL\]

  The jail specified by *jid* does not exist.

The **jail\_attach\_jd**() and **jail\_remove\_jd**() system calls will fail if:

- \[EINVAL\]

  The *fd* argument is not a valid jail descriptor.

- \[EPERM\]

  The jail descriptor was created by a user other than the super-user.

- \[EINVAL\]

  The jail specified by *jid* has been removed.

Further **jail**(), **jail\_set**(), **jail\_attach**(), and **jail\_attach\_jd**() call [chroot(2)](/chroot/2.md) internally, so they can fail for all the same reasons. In particular, they return the \[EPERM\] error when the process to join a jail has open directories. Please consult the [chroot(2)](/chroot/2.md) manual page for details.

## SEE ALSO

[chdir(2)](/chdir/2.md), [chroot(2)](/chroot/2.md), [jail(8)](/jail/8.md)

## HISTORY

The **jail**() system call appeared in FreeBSD 4.0. The **jail\_attach**() system call appeared in FreeBSD 5.1. The **jail\_set**(), **jail\_get**(), and **jail\_remove**() system calls appeared in FreeBSD 8.0.

## AUTHORS

The jail feature was written by Poul-Henning Kamp for R&D Associates who contributed it to FreeBSD. James Gritton added the extensible jail parameters and hierarchical jails.
