chroot() — Change root directory

Standards

Standards / Extensions C or C++ Dependencies

XPG4
XPG4.2

both  

Format

#define _XOPEN_SOURCE
#include <unistd.h>

int chroot(const char *path);

General description

The path argument points to a path name naming a directory. The chroot() function causes the named directory to become the root directory, that is the starting point for path searches for path names beginning with /. The process's working directory is unaffected by chroot(). Only a superuser can request chroot().

The dot-dot entry in the root directory is interpreted to mean the root directory. Thus, dot-dot cannot be used to access files outside the subtree rooted at the root directory.

Note:

This function is kept for historical reasons. It was part of the Legacy Feature in Single UNIX Specification, Version 2, but has been withdrawn and is not supported as part of Single UNIX Specification, Version 3.

If it is necessary to continue using this function in an application written for Single UNIX Specification, Version 3, define the feature test macro _UNIX03_WITHDRAWN before including any standard system headers. The macro exposes all interfaces and symbols removed in Single UNIX Specification, Version 3.

Returned value

If successful, chroot() changes the root directory, and returns 0.

If unsuccessful, chroot() returns -1 and sets errno to one of the following values:
Error Code
Description
EACCES
Search permission is denied for a component of path
ELOOP
A loop exists in symbolic links. This error is issued if the number of symbolic links detected in the resolution of path name is greater than POSIX_SYMLOOP (a value defined in the limits.h header file).
ENAMETOOLONG
Path name is longer that PATH_MAX characters, or some component of path name is longer that NAME_MAX characters while _POSIX_NO_TRUNC is in effect. For symbolic links, the length of the path name string substituted for a symbolic link exceeds PATH_MAX. The PATH_MAX and NAME_MAX values are determined using pathconf().
ENOENT
A component of path does not name an existing directory or path is an empty string.
ENOTDIR
A component of the path name is not a directory.
EPERM
The effective user ID does not have appropriate privileges.

Related information