---
name: "selinux_restorecon_parallel"
section: "3"
platform: "linux"
package: "libselinux1-dev"
version: "3.8.1-1"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/selinux_restorecon"
---

# selinux\_restorecon\_parallel(3)

restore file(s) default SELinux security contexts

Part of [selinux\_restorecon(3)](/selinux_restorecon/3.md).

## NAME

selinux\_restorecon - restore file(s) default SELinux security contexts

## SYNOPSIS

**#include \<selinux/restorecon.h\>**

**int selinux\_restorecon(const char \****pathname***,**\
 **unsigned int** *restorecon\_flags***);**

**int selinux\_restorecon\_parallel(const char \****pathname***,**\
 **unsigned int** *restorecon\_flags***,**\
 **size\_t** *nthreads***);**

## DESCRIPTION

**selinux\_restorecon**() restores file default security contexts on filesystems that support extended attributes (see [**xattr**(7)](/xattr/7.md)), based on:

*pathname* containing a directory or file to be relabeled.\
 If this is a directory and the *restorecon\_flags* **SELINUX\_RESTORECON\_RECURSE** has been set (for descending through directories), then **selinux\_restorecon**() will write an SHA1 digest of specfile entries calculated by [**selabel\_get\_digests\_all\_partial\_matches**(3)](/selabel_get_digests_all_partial_matches/3.md) to an extended attribute of *security.sehash* once the relabeling has been completed successfully (see the **NOTES** section for details).\
 These digests will be checked should **selinux\_restorecon**() be rerun with the *restorecon\_flags* **SELINUX\_RESTORECON\_RECURSE** flag set. If any of the specfile entries had been updated, the digest will also be updated. However if the digest is the same, no relabeling checks will take place.\
 The *restorecon\_flags* that can be used to manage the usage of the SHA1 digest are:

**SELINUX\_RESTORECON\_SKIP\_DIGEST**\
 **SELINUX\_RESTORECON\_IGNORE\_DIGEST**

*restorecon\_flags* contains the labeling option/rules as follows:

**SELINUX\_RESTORECON\_SKIP\_DIGEST** Do not check or update any extended attribute *security.sehash* entries.

**SELINUX\_RESTORECON\_IGNORE\_DIGEST** force the checking of labels even if the stored SHA1 digest matches the specfile entries SHA1 digest. The specfile entries digest will be written to the *security.sehash* extended attribute once relabeling has been completed successfully provided the **SELINUX\_RESTORECON\_NOCHANGE** flag has not been set, and no errors have been skipped during the file tree walk due to the **SELINUX\_RESTORECON\_COUNT\_ERRORS** flag.

**SELINUX\_RESTORECON\_NOCHANGE** don't change any file labels (passive check) or update the digest in the *security.sehash* extended attribute.

**SELINUX\_RESTORECON\_SET\_SPECFILE\_CTX** If set, reset the files label to match the default specfile context. If not set only reset the files "type" component of the context to match the default specfile context.

**SELINUX\_RESTORECON\_RECURSE** change file and directory labels recursively (descend directories) and if successful write an SHA1 digest of the specfile entries to an extended attribute as described in the **NOTES** section.

**SELINUX\_RESTORECON\_VERBOSE** log file label changes.

Note that if **SELINUX\_RESTORECON\_VERBOSE** and **SELINUX\_RESTORECON\_PROGRESS** flags are set, then **SELINUX\_RESTORECON\_PROGRESS** will take precedence.

**SELINUX\_RESTORECON\_PROGRESS** show progress by outputting the number of files in 1k blocks processed to stdout. If the **SELINUX\_RESTORECON\_MASS\_RELABEL** flag is also set then the approximate percentage complete will be shown.

**SELINUX\_RESTORECON\_MASS\_RELABEL** generally set when relabeling the entire OS, that will then show the approximate percentage complete. The **SELINUX\_RESTORECON\_PROGRESS** flag must also be set.

**SELINUX\_RESTORECON\_REALPATH** convert passed-in *pathname* to the canonical pathname using [**realpath**(3)](/realpath/3.md).

**SELINUX\_RESTORECON\_XDEV** prevent descending into directories that have a different device number than the *pathname* entry from which the descent began.

**SELINUX\_RESTORECON\_ADD\_ASSOC** attempt to add an association between an inode and a specification. If there is already an association for the inode and it conflicts with the specification, then use the last matching specification.

**SELINUX\_RESTORECON\_ABORT\_ON\_ERROR** abort on errors during the file tree walk.

**SELINUX\_RESTORECON\_SYSLOG\_CHANGES** log any label changes to [**syslog**(3)](/syslog/3.md).

**SELINUX\_RESTORECON\_LOG\_MATCHES** log what specfile context matched each file.

**SELINUX\_RESTORECON\_IGNORE\_NOENTRY** ignore files that do not exist.

**SELINUX\_RESTORECON\_IGNORE\_MOUNTS** do not read **/proc/mounts** to obtain a list of non-seclabel mounts to be excluded from relabeling checks.\
 Setting **SELINUX\_RESTORECON\_IGNORE\_MOUNTS** is useful where there is a non-seclabel fs mounted with a seclabel fs mounted on a directory below this.

**SELINUX\_RESTORECON\_CONFLICT\_ERROR** to treat conflicting specifications, such as where two hardlinks for the same inode have different contexts, as errors.

**SELINUX\_RESTORECON\_COUNT\_ERRORS** Count, but otherwise ignore, errors during the file tree walk. Only makes a difference if the **SELINUX\_RESTORECON\_ABORT\_ON\_ERROR** flag is clear. Call [**selinux\_restorecon\_get\_skipped\_errors**(3)](/selinux_restorecon_get_skipped_errors/3.md) for fetching the ignored (skipped) error count after [**selinux\_restorecon**(3)](/selinux_restorecon/3.md) or [**selinux\_restorecon\_parallel**(3)](/selinux_restorecon_parallel/3.md) completes with success. In case any errors were skipped during the file tree walk, the specfile entries SHA1 digest will not have been written to the *security.sehash* extended attribute.

The behavior regarding the checking and updating of the SHA1 digest described above is the default behavior. It is possible to change this by first calling [**selabel\_open**(3)](/selabel_open/3.md) and not enabling the **SELABEL\_OPT\_DIGEST** option, then calling [**selinux\_restorecon\_set\_sehandle**(3)](/selinux_restorecon_set_sehandle/3.md) to set the handle to be used by [**selinux\_restorecon**(3)](/selinux_restorecon/3.md).

If the *pathname* is a directory path, then it is possible to set directories to be excluded from the path by calling [**selinux\_restorecon\_set\_exclude\_list**(3)](/selinux_restorecon_set_exclude_list/3.md) with a **NULL** terminated list before calling [**selinux\_restorecon**(3)](/selinux_restorecon/3.md).

By default [**selinux\_restorecon**(3)](/selinux_restorecon/3.md) reads **/proc/mounts** to obtain a list of non-seclabel mounts to be excluded from relabeling checks unless the **SELINUX\_RESTORECON\_IGNORE\_MOUNTS** flag has been set.

**selinux\_restorecon\_parallel()** is similar to [**selinux\_restorecon**(3)](/selinux_restorecon/3.md), but accepts another parameter that allows to run relabeling over multiple threads:

*nthreads* specifies the number of threads to use during relabeling. When set to 1, the behavior is the same as calling [**selinux\_restorecon**(3)](/selinux_restorecon/3.md). When set to 0, the function will try to use as many threads as there are online CPU cores. When set to any other number, the function will try to use the given number of threads.

Note that to use the parallel relabeling capability, the calling process must be linked with the **libpthread** library (either at compile time or dynamically at run time). Otherwise the function will print a warning and fall back to the single threaded mode.

## RETURN VALUE

On success, zero is returned.  On error, -1 is returned and *errno* is set appropriately.

## NOTES

- 1\.

  To improve performance when relabeling file systems recursively (e.g. the *restorecon\_flags* **SELINUX\_RESTORECON\_RECURSE** flag is set) **selinux\_restorecon**() will write a calculated SHA1 digest of the specfile entries returned by [**selabel\_get\_digests\_all\_partial\_matches**(3)](/selabel_get_digests_all_partial_matches/3.md) to an extended attribute named *security.sehash* for each directory in the *pathname* path.

- 2\.

  To check the extended attribute entry use [**getfattr**(1)](/getfattr/1.md)**,** for example:

  getfattr -e hex -n security.sehash /

- 3\.

  Should any of the specfile entries have changed, then when **selinux\_restorecon**() is run again with the **SELINUX\_RESTORECON\_RECURSE** flag set, new SHA1 digests will be calculated and all files automatically relabeled depending on the settings of the **SELINUX\_RESTORECON\_SET\_SPECFILE\_CTX** flag (provided **SELINUX\_RESTORECON\_NOCHANGE** is not set).

- 4\.

  **/sys** and in-memory filesystems do not support the *security.sehash* extended attribute and are automatically excluded from any relabeling checks.

- 5\.

  By default **stderr** is used to log output messages and errors. This may be changed by calling [**selinux\_set\_callback**(3)](/selinux_set_callback/3.md) with the **SELINUX\_CB\_LOG** *type* option.

## SEE ALSO

[**selabel\_get\_digests\_all\_partial\_matches**(3)](/selabel_get_digests_all_partial_matches/3.md),\
 [**selinux\_restorecon\_set\_sehandle**(3)](/selinux_restorecon_set_sehandle/3.md),\
 [**selinux\_restorecon\_default\_handle**(3)](/selinux_restorecon_default_handle/3.md),\
 [**selinux\_restorecon\_get\_skipped\_errors**(3)](/selinux_restorecon_get_skipped_errors/3.md),\
 [**selinux\_restorecon\_set\_exclude\_list**(3)](/selinux_restorecon_set_exclude_list/3.md),\
 [**selinux\_restorecon\_set\_alt\_rootpath**(3)](/selinux_restorecon_set_alt_rootpath/3.md),\
 [**selinux\_restorecon\_xattr**(3)](/selinux_restorecon_xattr/3.md),\
 [**selinux\_set\_callback**(3)](/selinux_set_callback/3.md)
