title: sched_setaffinity(2)

text:
# sched_setaffinity(2)

## NAME
sched_setaffinity, sched_getaffinity - set and get a thread's CPU affinity mask

## SYNOPSIS

```
#define _GNU_SOURCE             /* See feature_test_macros(7) */
#include <sched.h>
```

```
int sched_setaffinity(pid_t pid, size_t cpusetsize,
                      const cpu_set_t *mask);
int sched_getaffinity(pid_t pid, size_t cpusetsize,
                      cpu_set_t *mask);
```

## DESCRIPTION
A thread's CPU affinity mask determines the set of CPUs on which it is eligible to run. On a multiprocessor system, setting the CPU affinity mask can be used to obtain performance benefits. For example, by dedicating one CPU to a particular thread (i.e., setting the affinity mask of that thread to specify a single CPU, and setting the affinity mask of all other threads to exclude that CPU), it is possible to ensure maximum execution speed for that thread. Restricting a thread to run on a single CPU also avoids the performance cost caused by the cache invalidation that occurs when a thread ceases to execute on one CPU and then recommences execution on a different CPU.

A CPU affinity mask is represented by the cpu_set_t structure, a "CPU set", pointed to by mask. A set of macros for manipulating CPU sets is described in CPU_SET(3).

sched_setaffinity() sets the CPU affinity mask of the thread whose ID is pid to the value specified by mask. If pid is zero, then the calling thread is used. The argument cpusetsize is the length (in bytes) of the data pointed to by mask. Normally this argument would be specified as sizeof(cpu_set_t).

If the thread specified by pid is not currently running on one of the CPUs specified in mask, then that thread is migrated to one of the CPUs specified in mask.

sched_getaffinity() writes the affinity mask of the thread whose ID is pid into the cpu_set_t structure pointed to by mask. The cpusetsize argument specifies the size (in bytes) of mask. If pid is zero, then the mask of the calling thread is returned.

## RETURN VALUE
On success, sched_setaffinity() and sched_getaffinity() return 0 (but see "C library/kernel differences" below, which notes that the underlying sched_getaffinity() differs in its return value). On failure, -1 is returned, and errno is set to indicate the error.

## ERRORS
EFAULT

A supplied memory address was invalid.

EINVAL

The affinity bit mask mask contains no processors that are currently physically on the system and permitted to the thread according to any restrictions that may be imposed by cpuset cgroups or the "cpuset" mechanism described in cpuset(7).

EINVAL

(sched_getaffinity() and, in kernels before 2.6.9, sched_setaffinity()) cpusetsize is smaller than the size of the affinity mask used by the kernel.

EPERM

(sched_setaffinity()) The calling thread does not have appropriate privileges. The caller needs an effective user ID equal to the real user ID or effective user ID of the thread identified by pid, or it must possess the CAP_SYS_NICE capability in the user namespace of the thread pid.

ESRCH

The thread whose ID is pid could not be found.

## VERSIONS
The CPU affinity system calls were introduced in Linux kernel 2.5.8. The system call wrappers were introduced in glibc 2.3. Initially, the glibc interfaces included a cpusetsize argument, typed as unsigned int. In glibc 2.3.3, the cpusetsize argument was removed, but was then restored in glibc 2.3.4, with type size_t.

## CONFORMING TO
These system calls are Linux-specific.

## NOTES
After a call to sched_setaffinity(), the set of CPUs on which the thread will actually run is the intersection of the set specified in the mask argument and the set of CPUs actually present on the system. The system may further restrict the set of CPUs on which the thread runs if the "cpuset" mechanism described in cpuset(7) is being used. These restrictions on the actual set of CPUs on which the thread will run are silently imposed by the kernel.

There are various ways of determining the number of CPUs available on the system, including: inspecting the contents of /proc/cpuinfo; using sysconf(3) to obtain the values of the _SC_NPROCESSORS_CONF and _SC_NPROCESSORS_ONLN parameters; and inspecting the list of CPU directories under /sys/devices/system/cpu/.

sched(7) has a description of the Linux scheduling scheme.

The affinity mask is a per-thread attribute that can be adjusted independently for each of the threads in a thread group. The value returned from a call to gettid(2) can be passed in the argument pid. Specifying pid as 0 will set the attribute for the calling thread, and passing the value returned from a call to getpid(2) will set the attribute for the main thread of the thread group. (If you are using the POSIX threads API, then use pthread_setaffinity_np(3) instead of sched_setaffinity().)

The isolcpus boot option can be used to isolate one or more CPUs at boot time, so that no processes are scheduled onto those CPUs. Following the use of this boot option, the only way to schedule processes onto the isolated CPUs is via sched_setaffinity() or the cpuset(7) mechanism. For further information, see the kernel source file Documentation/admin-guide/kernel-parameters.txt. As noted in that file, isolcpus is the preferred mechanism of isolating CPUs (versus the alternative of manually setting the CPU affinity of all processes on the system).

A child created via fork(2) inherits its parent's CPU affinity mask. The affinity mask is preserved across an execve(2).

### C library/kernel differences
This manual page describes the glibc interface for the CPU affinity calls. The actual system call interface is slightly different, with the mask being typed as unsigned long *, reflecting the fact that the underlying implementation of CPU sets is a simple bit mask.

On success, the raw sched_getaffinity() system call returns the number of bytes placed copied into the mask buffer; this will be the minimum of cpusetsize and the size (in bytes) of the cpumask_t data type that is used internally by the kernel to represent the CPU set bit mask.

### Handling systems with large CPU affinity masks
The underlying system calls (which represent CPU masks as bit masks of type unsigned long *) impose no restriction on the size of the CPU mask. However, the cpu_set_t data type used by glibc has a fixed size of 128 bytes, meaning that the maximum CPU number that can be represented is 1023. If the kernel CPU affinity mask is larger than 1024, then calls of the form:

sched_getaffinity(pid, sizeof(cpu_set_t), &mask);

fail with the error EINVAL, the error produced by the underlying system call for the case where the mask size specified in cpusetsize is smaller than the size of the affinity mask used by the kernel. (Depending on the system CPU topology, the kernel affinity mask can be substantially larger than the number of active CPUs in the system.)

When working on systems with large kernel CPU affinity masks, one must dynamically allocate the mask argument (see CPU_ALLOC(3)). Currently, the only way to do this is by probing for the size of the required mask using sched_getaffinity() calls with increasing mask sizes (until the call does not fail with the error EINVAL).

Be aware that CPU_ALLOC(3) may allocate a slightly larger CPU set than requested (because CPU sets are implemented as bit masks allocated in units of sizeof(long)). Consequently, sched_getaffinity() can set bits beyond the requested allocation size, because the kernel sees a few additional bits. Therefore, the caller should iterate over the bits in the returned set, counting those which are set, and stop upon reaching the value returned by CPU_COUNT(3) (rather than iterating over the number of bits requested to be allocated).

## EXAMPLES
The program below creates a child process. The parent and child then each assign themselves to a specified CPU and execute identical loops that consume some CPU time. Before terminating, the parent waits for the child to complete. The program takes three command-line arguments: the CPU number for the parent, the CPU number for the child, and the number of loop iterations that both processes should perform.

As the sample runs below demonstrate, the amount of real and CPU time consumed when running the program will depend on intra-core caching effects and whether the processes are using the same CPU.

We first employ lscpu(1) to determine that this (x86) system has two cores, each with two CPUs:

```
$ lscpu | egrep -i 'core.*:|socket'
Thread(s) per core:    2
Core(s) per socket:    2
Socket(s):             1
```

We then time the operation of the example program for three cases: both processes running on the same CPU; both processes running on different CPUs on the same core; and both processes running on different CPUs on different cores.

```
$ time -p ./a.out 0 0 100000000
real 14.75
user 3.02
sys 11.73
$ time -p ./a.out 0 1 100000000
real 11.52
user 3.98
sys 19.06
$ time -p ./a.out 0 3 100000000
real 7.89
user 3.29
sys 12.07
```

### Program source

```
#define _GNU_SOURCE
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \

                        } while (0)
int
main(int argc, char *argv[])
{

    cpu_set_t set;

    int parentCPU, childCPU;

    int nloops;

    if (argc != 4) {

        fprintf(stderr, "Usage: %s parent-cpu child-cpu num-loops\n",

                argv[0]);

        exit(EXIT_FAILURE);

    }

    parentCPU = atoi(argv[1]);

    childCPU = atoi(argv[2]);

    nloops = atoi(argv[3]);

    CPU_ZERO(&set);

    switch (fork()) {

    case -1:            /* Error */

        errExit("fork");

    case 0:             /* Child */

        CPU_SET(childCPU, &set);

        if (sched_setaffinity(getpid(), sizeof(set), &set) == -1)

            errExit("sched_setaffinity");

        for (int j = 0; j < nloops; j++)

            getppid();

        exit(EXIT_SUCCESS);

    default:            /* Parent */

        CPU_SET(parentCPU, &set);

        if (sched_setaffinity(getpid(), sizeof(set), &set) == -1)

            errExit("sched_setaffinity");

        for (int j = 0; j < nloops; j++)

            getppid();

        wait(NULL);     /* Wait for child to terminate */

        exit(EXIT_SUCCESS);

    }
}
```

## SEE ALSO
lscpu(1), nproc(1), taskset(1), clone(2), getcpu(2), getpriority(2), gettid(2), nice(2), sched_get_priority_max(2), sched_get_priority_min(2), sched_getscheduler(2), sched_setscheduler(2), setpriority(2), CPU_SET(3), get_nprocs(3), pthread_setaffinity_np(3), sched_getcpu(3), capabilities(7), cpuset(7), sched(7), numactl(8)


package: man-pages

title: sched_setattr(2)

text:
# sched_setattr(2)

## NAME
sched_setattr, sched_getattr - set and get scheduling policy and attributes

## SYNOPSIS

```
#include <sched.h>            /* Definition of SCHED_* constants */
#include <sys/syscall.h>      /* Definition of SYS_* constants */
#include <unistd.h>
```

```
int syscall(SYS_sched_setattr, pid_t pid, struct sched_attr *attr,
            unsigned int flags);
int syscall(SYS_sched_getattr, pid_t pid, struct sched_attr *attr,
            unsigned int size, unsigned int flags);
```

Note: glibc provides no wrappers for these system calls, necessitating the use of syscall(2).

## DESCRIPTION

### sched_setattr()
The sched_setattr() system call sets the scheduling policy and associated attributes for the thread whose ID is specified in pid. If pid equals zero, the scheduling policy and attributes of the calling thread will be set.

Currently, Linux supports the following "normal" (i.e., non-real-time) scheduling policies as values that may be specified in policy:

SCHED_OTHER

the standard round-robin time-sharing policy;

SCHED_BATCH

for "batch" style execution of processes; and

SCHED_IDLE

for running very low priority background jobs.

Various "real-time" policies are also supported, for special time-critical applications that need precise control over the way in which runnable threads are selected for execution. For the rules governing when a process may use these policies, see sched(7). The real-time policies that may be specified in policy are:

SCHED_FIFO

a first-in, first-out policy; and

SCHED_RR

a round-robin policy.

Linux also provides the following policy:

SCHED_DEADLINE

a deadline scheduling policy; see sched(7) for details.

The attr argument is a pointer to a structure that defines the new scheduling policy and attributes for the specified thread. This structure has the following form:

```
struct sched_attr {

    u32 size;              /* Size of this structure */

    u32 sched_policy;      /* Policy (SCHED_*) */

    u64 sched_flags;       /* Flags */

    s32 sched_nice;        /* Nice value (SCHED_OTHER,

                              SCHED_BATCH) */

    u32 sched_priority;    /* Static priority (SCHED_FIFO,

                              SCHED_RR) */

    /* Remaining fields are for SCHED_DEADLINE */

    u64 sched_runtime;

    u64 sched_deadline;

    u64 sched_period;
};
```

The fields of the sched_attr structure are as follows:

size

This field should be set to the size of the structure in bytes, as in sizeof(struct sched_attr). If the provided structure is smaller than the kernel structure, any additional fields are assumed to be '0'. If the provided structure is larger than the kernel structure, the kernel verifies that all additional fields are 0; if they are not, sched_setattr() fails with the error E2BIG and updates size to contain the size of the kernel structure.

The above behavior when the size of the user-space sched_attr structure does not match the size of the kernel structure allows for future extensibility of the interface. Malformed applications that pass oversize structures won't break in the future if the size of the kernel sched_attr structure is increased. In the future, it could also allow applications that know about a larger user-space sched_attr structure to determine whether they are running on an older kernel that does not support the larger structure.

sched_policy

This field specifies the scheduling policy, as one of the SCHED_* values listed above.

sched_flags

This field contains zero or more of the following flags that are ORed together to control scheduling behavior:

SCHED_FLAG_RESET_ON_FORK

Children created by fork(2) do not inherit privileged scheduling policies. See sched(7) for details.

SCHED_FLAG_RECLAIM (since Linux 4.13)

This flag allows a SCHED_DEADLINE thread to reclaim bandwidth unused by other real-time threads.

SCHED_FLAG_DL_OVERRUN (since Linux 4.16)

This flag allows an application to get informed about run-time overruns in SCHED_DEADLINE threads. Such overruns may be caused by (for example) coarse execution time accounting or incorrect parameter assignment. Notification takes the form of a SIGXCPU signal which is generated on each overrun.

This SIGXCPU signal is process-directed (see signal(7)) rather than thread-directed. This is probably a bug. On the one hand, sched_setattr() is being used to set a per-thread attribute. On the other hand, if the process-directed signal is delivered to a thread inside the process other than the one that had a run-time overrun, the application has no way of knowing which thread overran.

sched_nice

This field specifies the nice value to be set when specifying sched_policy as SCHED_OTHER or SCHED_BATCH. The nice value is a number in the range -20 (high priority) to +19 (low priority); see sched(7).

sched_priority

This field specifies the static priority to be set when specifying sched_policy as SCHED_FIFO or SCHED_RR. The allowed range of priorities for these policies can be determined using sched_get_priority_min(2) and sched_get_priority_max(2). For other policies, this field must be specified as 0.

sched_runtime

This field specifies the "Runtime" parameter for deadline scheduling. The value is expressed in nanoseconds. This field, and the next two fields, are used only for SCHED_DEADLINE scheduling; for further details, see sched(7).

sched_deadline

This field specifies the "Deadline" parameter for deadline scheduling. The value is expressed in nanoseconds.

sched_period

This field specifies the "Period" parameter for deadline scheduling. The value is expressed in nanoseconds.

The flags argument is provided to allow for future extensions to the interface; in the current implementation it must be specified as 0.

### sched_getattr()
The sched_getattr() system call fetches the scheduling policy and the associated attributes for the thread whose ID is specified in pid. If pid equals zero, the scheduling policy and attributes of the calling thread will be retrieved.

The size argument should be set to the size of the sched_attr structure as known to user space. The value must be at least as large as the size of the initially published sched_attr structure, or the call fails with the error EINVAL.

The retrieved scheduling attributes are placed in the fields of the sched_attr structure pointed to by attr. The kernel sets attr.size to the size of its sched_attr structure.

If the caller-provided attr buffer is larger than the kernel's sched_attr structure, the additional bytes in the user-space structure are not touched. If the caller-provided structure is smaller than the kernel sched_attr structure, the kernel will silently not return any values which would be stored outside the provided space. As with sched_setattr(), these semantics allow for future extensibility of the interface.

The flags argument is provided to allow for future extensions to the interface; in the current implementation it must be specified as 0.

## RETURN VALUE
On success, sched_setattr() and sched_getattr() return 0. On error, -1 is returned, and errno is set to indicate the error.

## ERRORS
sched_getattr() and sched_setattr() can both fail for the following reasons:

EINVAL

attr is NULL; or pid is negative; or flags is not zero.

ESRCH

The thread whose ID is pid could not be found.

In addition, sched_getattr() can fail for the following reasons:

E2BIG

The buffer specified by size and attr is too small.

EINVAL

size is invalid; that is, it is smaller than the initial version of the sched_attr structure (48 bytes) or larger than the system page size.

In addition, sched_setattr() can fail for the following reasons:

E2BIG

The buffer specified by size and attr is larger than the kernel structure, and one or more of the excess bytes is nonzero.

EBUSY

SCHED_DEADLINE admission control failure, see sched(7).

EINVAL

attr.sched_policy is not one of the recognized policies; attr.sched_flags contains a flag other than SCHED_FLAG_RESET_ON_FORK; or attr.sched_priority is invalid; or attr.sched_policy is SCHED_DEADLINE and the deadline scheduling parameters in attr are invalid.

EPERM

The caller does not have appropriate privileges.

EPERM

The CPU affinity mask of the thread specified by pid does not include all CPUs in the system (see sched_setaffinity(2)).

## VERSIONS
These system calls first appeared in Linux 3.14.

## CONFORMING TO
These system calls are nonstandard Linux extensions.

## NOTES
Glibc does not provide wrappers for these system calls; call them using syscall(2).

sched_setattr() provides a superset of the functionality of sched_setscheduler(2), sched_setparam(2), nice(2), and (other than the ability to set the priority of all processes belonging to a specified user or all processes in a specified group) setpriority(2). Analogously, sched_getattr() provides a superset of the functionality of sched_getscheduler(2), sched_getparam(2), and (partially) getpriority(2).

## BUGS
In Linux versions up to 3.15, sched_setattr() failed with the error EFAULT instead of E2BIG for the case described in ERRORS.

In Linux versions up to 5.3, sched_getattr() failed with the error EFBIG if the in-kernel sched_attr structure was larger than the size passed by user space.

## SEE ALSO
chrt(1), nice(2), sched_get_priority_max(2), sched_get_priority_min(2), sched_getaffinity(2), sched_getparam(2), sched_getscheduler(2), sched_rr_get_interval(2), sched_setaffinity(2), sched_setparam(2), sched_setscheduler(2), sched_yield(2), setpriority(2), pthread_getschedparam(3), pthread_setschedparam(3), pthread_setschedprio(3), capabilities(7), cpuset(7), sched(7)


title: sched_setparam(2)

summary: set and get scheduling parameters

text:
# sched_setparam(2)

## NAME
sched_setparam, sched_getparam - set and get scheduling parameters

## SYNOPSIS

```
#include <sched.h>
```

```
int sched_setparam(pid_t pid, const struct sched_param *param);
int sched_getparam(pid_t pid, struct sched_param *param);
```

```
struct sched_param {

    ...

    int sched_priority;

    ...
};
```

## DESCRIPTION
sched_setparam() sets the scheduling parameters associated with the scheduling policy for the thread whose thread ID is specified in pid. If pid is zero, then the parameters of the calling thread are set. The interpretation of the argument param depends on the scheduling policy of the thread identified by pid. See sched(7) for a description of the scheduling policies supported under Linux.

sched_getparam() retrieves the scheduling parameters for the thread identified by pid. If pid is zero, then the parameters of the calling thread are retrieved.

sched_setparam() checks the validity of param for the scheduling policy of the thread. The value param->sched_priority must lie within the range given by sched_get_priority_min(2) and sched_get_priority_max(2).

For a discussion of the privileges and resource limits related to scheduling priority and policy, see sched(7).

POSIX systems on which sched_setparam() and sched_getparam() are available define _POSIX_PRIORITY_SCHEDULING in <unistd.h>.

## RETURN VALUE
On success, sched_setparam() and sched_getparam() return 0. On error, -1 is returned, and errno is set to indicate the error.

## ERRORS
EINVAL

Invalid arguments: param is NULL or pid is negative

EINVAL

(sched_setparam()) The argument param does not make sense for the current scheduling policy.

EPERM

(sched_setparam()) The caller does not have appropriate privileges (Linux: does not have the CAP_SYS_NICE capability).

ESRCH

The thread whose ID is pid could not be found.

## CONFORMING TO
POSIX.1-2001, POSIX.1-2008.

## SEE ALSO
getpriority(2), gettid(2), nice(2), sched_get_priority_max(2), sched_get_priority_min(2), sched_getaffinity(2), sched_getscheduler(2), sched_setaffinity(2), sched_setattr(2), sched_setscheduler(2), setpriority(2), capabilities(7), sched(7)


title: sched_setscheduler(2)

text:
# sched_setscheduler(2)

## NAME
sched_setscheduler, sched_getscheduler - set and get scheduling policy/parameters

## SYNOPSIS

```
#include <sched.h>
```

```
int sched_setscheduler(pid_t pid, int policy,
                       const struct sched_param *param);
int sched_getscheduler(pid_t pid);
```

## DESCRIPTION
The sched_setscheduler() system call sets both the scheduling policy and parameters for the thread whose ID is specified in pid. If pid equals zero, the scheduling policy and parameters of the calling thread will be set.

The scheduling parameters are specified in the param argument, which is a pointer to a structure of the following form:

```
struct sched_param {

    ...

    int sched_priority;

    ...
};
```

In the current implementation, the structure contains only one field, sched_priority. The interpretation of param depends on the selected policy.

Currently, Linux supports the following "normal" (i.e., non-real-time) scheduling policies as values that may be specified in policy:

SCHED_OTHER

the standard round-robin time-sharing policy;

SCHED_BATCH

for "batch" style execution of processes; and

SCHED_IDLE

for running very low priority background jobs.

For each of the above policies, param->sched_priority must be 0.

Various "real-time" policies are also supported, for special time-critical applications that need precise control over the way in which runnable threads are selected for execution. For the rules governing when a process may use these policies, see sched(7). The real-time policies that may be specified in policy are:

SCHED_FIFO

a first-in, first-out policy; and

SCHED_RR

a round-robin policy.

For each of the above policies, param->sched_priority specifies a scheduling priority for the thread. This is a number in the range returned by calling sched_get_priority_min(2) and sched_get_priority_max(2) with the specified policy. On Linux, these system calls return, respectively, 1 and 99.

Since Linux 2.6.32, the SCHED_RESET_ON_FORK flag can be ORed in policy when calling sched_setscheduler(). As a result of including this flag, children created by fork(2) do not inherit privileged scheduling policies. See sched(7) for details.

sched_getscheduler() returns the current scheduling policy of the thread identified by pid. If pid equals zero, the policy of the calling thread will be retrieved.

## RETURN VALUE
On success, sched_setscheduler() returns zero. On success, sched_getscheduler() returns the policy for the thread (a nonnegative integer). On error, both calls return -1, and errno is set to indicate the error.

## ERRORS
EINVAL

Invalid arguments: pid is negative or param is NULL.

EINVAL

(sched_setscheduler()) policy is not one of the recognized policies.

EINVAL

(sched_setscheduler()) param does not make sense for the specified policy.

EPERM

The calling thread does not have appropriate privileges.

ESRCH

The thread whose ID is pid could not be found.

## CONFORMING TO
POSIX.1-2001, POSIX.1-2008 (but see BUGS below). The SCHED_BATCH and SCHED_IDLE policies are Linux-specific.

## NOTES
Further details of the semantics of all of the above "normal" and "real-time" scheduling policies can be found in the sched(7) manual page. That page also describes an additional policy, SCHED_DEADLINE, which is settable only via sched_setattr(2).

POSIX systems on which sched_setscheduler() and sched_getscheduler() are available define _POSIX_PRIORITY_SCHEDULING in <unistd.h>.

POSIX.1 does not detail the permissions that an unprivileged thread requires in order to call sched_setscheduler(), and details vary across systems. For example, the Solaris 7 manual page says that the real or effective user ID of the caller must match the real user ID or the save set-user-ID of the target.

The scheduling policy and parameters are in fact per-thread attributes on Linux. The value returned from a call to gettid(2) can be passed in the argument pid. Specifying pid as 0 will operate on the attributes of the calling thread, and passing the value returned from a call to getpid(2) will operate on the attributes of the main thread of the thread group. (If you are using the POSIX threads API, then use pthread_setschedparam(3), pthread_getschedparam(3), and pthread_setschedprio(3), instead of the sched_*(2) system calls.)

## BUGS
POSIX.1 says that on success, sched_setscheduler() should return the previous scheduling policy. Linux sched_setscheduler() does not conform to this requirement, since it always returns 0 on success.

## SEE ALSO
chrt(1), nice(2), sched_get_priority_max(2), sched_get_priority_min(2), sched_getaffinity(2), sched_getattr(2), sched_getparam(2), sched_rr_get_interval(2), sched_setaffinity(2), sched_setattr(2), sched_setparam(2), sched_yield(2), setpriority(2), capabilities(7), cpuset(7), sched(7)


title: sched_yield(2)

summary: yield the processor

text:
# sched_yield(2)

## NAME
sched_yield - yield the processor

## SYNOPSIS

```
#include <sched.h>
```

```
int sched_yield(void);
```

## DESCRIPTION
sched_yield() causes the calling thread to relinquish the CPU. The thread is moved to the end of the queue for its static priority and a new thread gets to run.

## RETURN VALUE
On success, sched_yield() returns 0. On error, -1 is returned, and errno is set to indicate the error.

## ERRORS
In the Linux implementation, sched_yield() always succeeds.

## CONFORMING TO
POSIX.1-2001, POSIX.1-2008.

## NOTES
If the calling thread is the only thread in the highest priority list at that time, it will continue to run after a call to sched_yield().

POSIX systems on which sched_yield() is available define _POSIX_PRIORITY_SCHEDULING in <unistd.h>.

Strategic calls to sched_yield() can improve performance by giving other threads or processes a chance to run when (heavily) contended resources (e.g., mutexes) have been released by the caller. Avoid calling sched_yield() unnecessarily or inappropriately (e.g., when resources needed by other schedulable threads are still held by the caller), since doing so will result in unnecessary context switches, which will degrade system performance.

sched_yield() is intended for use with real-time scheduling policies (i.e., SCHED_FIFO or SCHED_RR). Use of sched_yield() with nondeterministic scheduling policies such as SCHED_OTHER is unspecified and very likely means your application design is broken.

## SEE ALSO
sched(7)


seccomp

title: seccomp(2)

text:
# seccomp(2)

## NAME
seccomp - operate on Secure Computing state of the process

## SYNOPSIS

```
#include <linux/seccomp.h>  /* Definition of SECCOMP_* constants */
#include <linux/filter.h>   /* Definition of struct sock_fprog */
#include <linux/audit.h>    /* Definition of AUDIT_* constants */
#include <linux/signal.h>   /* Definition of SIG* constants */
#include <sys/ptrace.h>     /* Definition of PTRACE_* constants */
#include <sys/syscall.h>    /* Definition of SYS_* constants */
#include <unistd.h>
```

```
int syscall(SYS_seccomp, unsigned int operation, unsigned int flags,
            void *args);
```

Note: glibc provides no wrapper for seccomp(), necessitating the use of syscall(2).

## DESCRIPTION
The seccomp() system call operates on the Secure Computing (seccomp) state of the calling process.

Currently, Linux supports the following operation values:

SECCOMP_SET_MODE_STRICT

The only system calls that the calling thread is permitted to make are read(2), write(2), _exit(2) (but not exit_group(2)), and sigreturn(2). Other system calls result in the delivery of a SIGKILL signal. Strict secure computing mode is useful for number-crunching applications that may need to execute untrusted byte code, perhaps obtained by reading from a pipe or socket.

Note that although the calling thread can no longer call sigprocmask(2), it can use sigreturn(2) to block all signals apart from SIGKILL and SIGSTOP. This means that alarm(2) (for example) is not sufficient for restricting the process's execution time. Instead, to reliably terminate the process, SIGKILL must be used. This can be done by using timer_create(2) with SIGEV_SIGNAL and sigev_signo set to SIGKILL, or by using setrlimit(2) to set the hard limit for RLIMIT_CPU.

This operation is available only if the kernel is configured with CONFIG_SECCOMP enabled.

The value of flags must be 0, and args must be NULL.

This operation is functionally identical to the call:

```
prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT);

```

SECCOMP_SET_MODE_FILTER

The system calls allowed are defined by a pointer to a Berkeley Packet Filter (BPF) passed via args. This argument is a pointer to a struct sock_fprog; it can be designed to filter arbitrary system calls and system call arguments. If the filter is invalid, seccomp() fails, returning EINVAL in errno.

If fork(2) or clone(2) is allowed by the filter, any child processes will be constrained to the same system call filters as the parent. If execve(2) is allowed, the existing filters will be preserved across a call to execve(2).

In order to use the SECCOMP_SET_MODE_FILTER operation, either the calling thread must have the CAP_SYS_ADMIN capability in its user namespace, or the thread must already have the no_new_privs bit set. If that bit was not already set by an ancestor of this thread, the thread must make the following call:

```
prctl(PR_SET_NO_NEW_PRIVS, 1);

```

Otherwise, the SECCOMP_SET_MODE_FILTER operation fails and returns EACCES in errno. This requirement ensures that an unprivileged process cannot apply a malicious filter and then invoke a set-user-ID or other privileged program using execve(2), thus potentially compromising that program. (Such a malicious filter might, for example, cause an attempt to use setuid(2) to set the caller's user IDs to nonzero values to instead return 0 without actually making the system call. Thus, the program might be tricked into retaining superuser privileges in circumstances where it is possible to influence it to do dangerous things because it did not actually drop privileges.)

If prctl(2) or seccomp() is allowed by the attached filter, further filters may be added. This will increase evaluation time, but allows for further reduction of the attack surface during execution of a thread.

The SECCOMP_SET_MODE_FILTER operation is available only if the kernel is configured with CONFIG_SECCOMP_FILTER enabled.

When flags is 0, this operation is functionally identical to the call:

```
prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, args);

```

The recognized flags are:

SECCOMP_FILTER_FLAG_LOG (since Linux 4.14)

All filter return actions except SECCOMP_RET_ALLOW should be logged. An administrator may override this filter flag by preventing specific actions from being logged via the /proc/sys/kernel/seccomp/actions_logged file.

SECCOMP_FILTER_FLAG_NEW_LISTENER (since Linux 5.0)

After successfully installing the filter program, return a new user-space notification file descriptor. (The close-on-exec flag is set for the file descriptor.) When the filter returns SECCOMP_RET_USER_NOTIF a notification will be sent to this file descriptor.

At most one seccomp filter using the SECCOMP_FILTER_FLAG_NEW_LISTENER flag can be installed for a thread.

See seccomp_unotify(2) for further details.

SECCOMP_FILTER_FLAG_SPEC_ALLOW (since Linux 4.17)

Disable Speculative Store Bypass mitigation.

SECCOMP_FILTER_FLAG_TSYNC

When adding a new filter, synchronize all other threads of the calling process to the same seccomp filter tree. A "filter tree" is the ordered list of filters attached to a thread. (Attaching identical filters in separate seccomp() calls results in different filters from this perspective.)

If any thread cannot synchronize to the same filter tree, the call will not attach the new seccomp filter, and will fail, returning the first thread ID found that cannot synchronize. Synchronization will fail if another thread in the same process is in SECCOMP_MODE_STRICT or if it has attached new seccomp filters to itself, diverging from the calling thread's filter tree.

SECCOMP_GET_ACTION_AVAIL (since Linux 4.14)

Test to see if an action is supported by the kernel. This operation is helpful to confirm that the kernel knows of a more recently added filter return action since the kernel treats all unknown actions as SECCOMP_RET_KILL_PROCESS.

The value of flags must be 0, and args must be a pointer to an unsigned 32-bit filter return action.

SECCOMP_GET_NOTIF_SIZES (since Linux 5.0)

Get the sizes of the seccomp user-space notification structures. Since these structures may evolve and grow over time, this command can be used to determine how much memory to allocate for sending and receiving notifications.

The value of flags must be 0, and args must be a pointer to a struct seccomp_notif_sizes, which has the following form:

```
struct seccomp_notif_sizes

    __u16 seccomp_notif;      /* Size of notification structure */

    __u16 seccomp_notif_resp; /* Size of response structure */

    __u16 seccomp_data;       /* Size of 'struct seccomp_data' */
};

```

See seccomp_unotify(2) for further details.

### Filters
When adding filters via SECCOMP_SET_MODE_FILTER, args points to a filter program:

```
struct sock_fprog {

    unsigned short      len;    /* Number of BPF instructions */

    struct sock_filter *filter; /* Pointer to array of

                                   BPF instructions */
};
```

Each program must contain one or more BPF instructions:

```
struct sock_filter {            /* Filter block */

    __u16 code;                 /* Actual filter code */

    __u8  jt;                   /* Jump true */

    __u8  jf;                   /* Jump false */

    __u32 k;                    /* Generic multiuse field */
};
```

When executing the instructions, the BPF program operates on the system call information made available (i.e., use the BPF_ABS addressing mode) as a (read-only) buffer of the following form:

```
struct seccomp_data {

    int   nr;                   /* System call number */

    __u32 arch;                 /* AUDIT_ARCH_* value

                                   (see <linux/audit.h>) */

    __u64 instruction_pointer;  /* CPU instruction pointer */

    __u64 args[6];              /* Up to 6 system call arguments */
};
```

Because numbering of system calls varies between architectures and some architectures (e.g., x86-64) allow user-space code to use the calling conventions of multiple architectures (and the convention being used may vary over the life of a process that uses execve(2) to execute binaries that employ the different conventions), it is usually necessary to verify the value of the arch field.

It is strongly recommended to use an allow-list approach whenever possible because such an approach is more robust and simple. A deny-list will have to be updated whenever a potentially dangerous system call is added (or a dangerous flag or option if those are deny-listed), and it is often possible to alter the representation of a value without altering its meaning, leading to a deny-list bypass. See also Caveats below.

The arch field is not unique for all calling conventions. The x86-64 ABI and the x32 ABI both use AUDIT_ARCH_X86_64 as arch, and they run on the same processors. Instead, the mask __X32_SYSCALL_BIT is used on the system call number to tell the two ABIs apart.

This means that a policy must either deny all syscalls with __X32_SYSCALL_BIT or it must recognize syscalls with and without __X32_SYSCALL_BIT set. A list of system calls to be denied based on nr that does not also contain nr values with __X32_SYSCALL_BIT set can be bypassed by a malicious program that sets __X32_SYSCALL_BIT.

Additionally, kernels prior to Linux 5.4 incorrectly permitted nr in the ranges 512-547 as well as the corresponding non-x32 syscalls ORed with __X32_SYSCALL_BIT. For example, nr == 521 and nr == (101 | __X32_SYSCALL_BIT) would result in invocations of ptrace(2) with potentially confused x32-vs-x86_64 semantics in the kernel. Policies intended to work on kernels before Linux 5.4 must ensure that they deny or otherwise correctly handle these system calls. On Linux 5.4 and newer, such system calls will fail with the error ENOSYS, without doing anything.

The instruction_pointer field provides the address of the machine-language instruction that performed the system call. This might be useful in conjunction with the use of /proc/[pid]/maps to perform checks based on which region (mapping) of the program made the system call. (Probably, it is wise to lock down the mmap(2) and mprotect(2) system calls to prevent the program from subverting such checks.)

When checking values from args, keep in mind that arguments are often silently truncated before being processed, but after the seccomp check. For example, this happens if the i386 ABI is used on an x86-64 kernel: although the kernel will normally not look beyond the 32 lowest bits of the arguments, the values of the full 64-bit registers will be present in the seccomp data. A less surprising example is that if the x86-64 ABI is used to perform a system call that takes an argument of type int, the more-significant half of the argument register is ignored by the system call, but visible in the seccomp data.

A seccomp filter returns a 32-bit value consisting of two parts: the most significant 16 bits (corresponding to the mask defined by the constant SECCOMP_RET_ACTION_FULL) contain one of the "action" values listed below; the least significant 16-bits (defined by the constant SECCOMP_RET_DATA) are "data" to be associated with this return value.

If multiple filters exist, they are all executed, in reverse order of their addition to the filter tree—that is, the most recently installed filter is executed first. (Note that all filters will be called even if one of the earlier filters returns SECCOMP_RET_KILL. This is done to simplify the kernel code and to provide a tiny speed-up in the execution of sets of filters by avoiding a check for this uncommon case.) The return value for the evaluation of a given system call is the first-seen action value of highest precedence (along with its accompanying data) returned by execution of all of the filters.

In decreasing order of precedence, the action values that may be returned by a seccomp filter are:

SECCOMP_RET_KILL_PROCESS (since Linux 4.14)

This value results in immediate termination of the process, with a core dump. The system call is not executed. By contrast with SECCOMP_RET_KILL_THREAD below, all threads in the thread group are terminated. (For a discussion of thread groups, see the description of the CLONE_THREAD flag in clone(2).)

The process terminates as though killed by a SIGSYS signal. Even if a signal handler has been registered for SIGSYS, the handler will be ignored in this case and the process always terminates. To a parent process that is waiting on this process (using waitpid(2) or similar), the returned wstatus will indicate that its child was terminated as though by a SIGSYS signal.

SECCOMP_RET_KILL_THREAD (or SECCOMP_RET_KILL)

This value results in immediate termination of the thread that made the system call. The system call is not executed. Other threads in the same thread group will continue to execute.

The thread terminates as though killed by a SIGSYS signal. See SECCOMP_RET_KILL_PROCESS above.

Before Linux 4.11, any process terminated in this way would not trigger a coredump (even though SIGSYS is documented in signal(7) as having a default action of termination with a core dump). Since Linux 4.11, a single-threaded process will dump core if terminated in this way.

With the addition of SECCOMP_RET_KILL_PROCESS in Linux 4.14, SECCOMP_RET_KILL_THREAD was added as a synonym for SECCOMP_RET_KILL, in order to more clearly distinguish the two actions.

Note: the use of SECCOMP_RET_KILL_THREAD to kill a single thread in a multithreaded process is likely to leave the process in a permanently inconsistent and possibly corrupt state.

SECCOMP_RET_TRAP

This value results in the kernel sending a thread-directed SIGSYS signal to the triggering thread. (The system call is not executed.) Various fields will be set in the siginfo_t structure (see sigaction(2)) associated with signal:

- si_signo will contain SIGSYS.

- si_call_addr will show the address of the system call instruction.

- si_syscall and si_arch will indicate which system call was attempted.

- si_code will contain SYS_SECCOMP.

- si_errno will contain the SECCOMP_RET_DATA portion of the filter return value.

The program counter will be as though the system call happened (i.e., the program counter will not point to the system call instruction). The return value register will contain an architecture-dependent value; if resuming execution, set it to something appropriate for the system call. (The architecture dependency is because replacing it with ENOSYS could overwrite some useful information.)

SECCOMP_RET_ERRNO

This value results in the SECCOMP_RET_DATA portion of the filter's return value being passed to user space as the errno value without executing the system call.

SECCOMP_RET_USER_NOTIF (since Linux 5.0)

Forward the system call to an attached user-space supervisor process to allow that process to decide what to do with the system call. If there is no attached supervisor (either because the filter was not installed with the SECCOMP_FILTER_FLAG_NEW_LISTENER flag or because the file descriptor was closed), the filter returns ENOSYS (similar to what happens when a filter returns SECCOMP_RET_TRACE and there is no tracer). See seccomp_unotify(2) for further details.

Note that the supervisor process will not be notified if another filter returns an action value with a precedence greater than SECCOMP_RET_USER_NOTIF.

SECCOMP_RET_TRACE

When returned, this value will cause the kernel to attempt to notify a ptrace(2)-based tracer prior to executing the system call. If there is no tracer present, the system call is not executed and returns a failure status with errno set to ENOSYS.

A tracer will be notified if it requests PTRACE_O_TRACESECCOMP using ptrace(PTRACE_SETOPTIONS). The tracer will be notified of a PTRACE_EVENT_SECCOMP and the SECCOMP_RET_DATA portion of the filter's return value will be available to the tracer via PTRACE_GETEVENTMSG.

The tracer can skip the system call by changing the system call number to -1. Alternatively, the tracer can change the system call requested by changing the system call to a valid system call number. If the tracer asks to skip the system call, then the system call will appear to return the value that the tracer puts in the return value register.

Before kernel 4.8, the seccomp check will not be run again after the tracer is notified. (This means that, on older kernels, seccomp-based sandboxes must not allow use of ptrace(2)—even of other sandboxed processes—without extreme care; ptracers can use this mechanism to escape from the seccomp sandbox.)

Note that a tracer process will not be notified if another filter returns an action value with a precedence greater than SECCOMP_RET_TRACE.

SECCOMP_RET_LOG (since Linux 4.14)

This value results in the system call being executed after the filter return action is logged. An administrator may override the logging of this action via the /proc/sys/kernel/seccomp/actions_logged file.

SECCOMP_RET_ALLOW

This value results in the system call being executed.

If an action value other than one of the above is specified, then the filter action is treated as either SECCOMP_RET_KILL_PROCESS (since Linux 4.14) or SECCOMP_RET_KILL_THREAD (in Linux 4.13 and earlier).

### /proc interfaces
The files in the directory /proc/sys/kernel/seccomp provide additional seccomp information and configuration:

actions_avail (since Linux 4.14)

A read-only ordered list of seccomp filter return actions in string form. The ordering, from left-to-right, is in decreasing order of precedence. The list represents the set of seccomp filter return actions supported by the kernel.

actions_logged (since Linux 4.14)

A read-write ordered list of seccomp filter return actions that are allowed to be logged. Writes to the file do not need to be in ordered form but reads from the file will be ordered in the same way as the actions_avail file.

It is important to note that the value of actions_logged does not prevent certain filter return actions from being logged when the audit subsystem is configured to audit a task. If the action is not found in the actions_logged file, the final decision on whether to audit the action for that task is ultimately left up to the audit subsystem to decide for all filter return actions other than SECCOMP_RET_ALLOW.

The "allow" string is not accepted in the actions_logged file as it is not possible to log SECCOMP_RET_ALLOW actions. Attempting to write "allow" to the file will fail with the error EINVAL.

### Audit logging of seccomp actions
Since Linux 4.14, the kernel provides the facility to log the actions returned by seccomp filters in the audit log. The kernel makes the decision to log an action based on the action type, whether or not the action is present in the actions_logged file, and whether kernel auditing is enabled (e.g., via the kernel boot option audit=1). The rules are as follows:

- If the action is SECCOMP_RET_ALLOW, the action is not logged.

- Otherwise, if the action is either SECCOMP_RET_KILL_PROCESS or SECCOMP_RET_KILL_THREAD, and that action appears in the actions_logged file, the action is logged.

- Otherwise, if the filter has requested logging (the SECCOMP_FILTER_FLAG_LOG flag) and the action appears in the actions_logged file, the action is logged.

- Otherwise, if kernel auditing is enabled and the process is being audited (autrace(8)), the action is logged.

- Otherwise, the action is not logged.

## RETURN VALUE
On success, seccomp() returns 0. On error, if SECCOMP_FILTER_FLAG_TSYNC was used, the return value is the ID of the thread that caused the synchronization failure. (This ID is a kernel thread ID of the type returned by clone(2) and gettid(2).) On other errors, -1 is returned, and errno is set to indicate the error.

## ERRORS
seccomp() can fail for the following reasons:

EACCES

The caller did not have the CAP_SYS_ADMIN capability in its user namespace, or had not set no_new_privs before using SECCOMP_SET_MODE_FILTER.

EBUSY

While installing a new filter, the SECCOMP_FILTER_FLAG_NEW_LISTENER flag was specified, but a previous filter had already been installed with that flag.

EFAULT

args was not a valid address.

EINVAL

operation is unknown or is not supported by this kernel version or configuration.

EINVAL

The specified flags are invalid for the given operation.

EINVAL

operation included BPF_ABS, but the specified offset was not aligned to a 32-bit boundary or exceeded sizeof(struct seccomp_data).

EINVAL

A secure computing mode has already been set, and operation differs from the existing setting.

EINVAL

operation specified SECCOMP_SET_MODE_FILTER, but the filter program pointed to by args was not valid or the length of the filter program was zero or exceeded BPF_MAXINSNS (4096) instructions.

ENOMEM

Out of memory.

ENOMEM

The total length of all filter programs attached to the calling thread would exceed MAX_INSNS_PER_PATH (32768) instructions. Note that for the purposes of calculating this limit, each already existing filter program incurs an overhead penalty of 4 instructions.

EOPNOTSUPP

operation specified SECCOMP_GET_ACTION_AVAIL, but the kernel does not support the filter return action specified by args.

ESRCH

Another thread caused a failure during thread sync, but its ID could not be determined.

## VERSIONS
The seccomp() system call first appeared in Linux 3.17.

## CONFORMING TO
The seccomp() system call is a nonstandard Linux extension.

## NOTES
Rather than hand-coding seccomp filters as shown in the example below, you may prefer to employ the libseccomp library, which provides a front-end for generating seccomp filters.

The Seccomp field of the /proc/[pid]/status file provides a method of viewing the seccomp mode of a process; see proc(5).

seccomp() provides a superset of the functionality provided by the prctl(2) PR_SET_SECCOMP operation (which does not support flags).

Since Linux 4.4, the ptrace(2) PTRACE_SECCOMP_GET_FILTER operation can be used to dump a process's seccomp filters.

### Architecture support for seccomp BPF
Architecture support for seccomp BPF filtering is available on the following architectures:

- x86-64, i386, x32 (since Linux 3.5)

- ARM (since Linux 3.8)

- s390 (since Linux 3.8)

- MIPS (since Linux 3.16)

- ARM-64 (since Linux 3.19)

- PowerPC (since Linux 4.3)

- Tile (since Linux 4.3)

- PA-RISC (since Linux 4.6)

### Caveats
There are various subtleties to consider when applying seccomp filters to a program, including the following:

- Some traditional system calls have user-space implementations in the vdso(7) on many architectures. Notable examples include clock_gettime(2), gettimeofday(2), and time(2). On such architectures, seccomp filtering for these system calls will have no effect. (However, there are cases where the vdso(7) implementations may fall back to invoking the true system call, in which case seccomp filters would see the system call.)

- Seccomp filtering is based on system call numbers. However, applications typically do not directly invoke system calls, but instead call wrapper functions in the C library which in turn invoke the system calls. Consequently, one must be aware of the following:

- The glibc wrappers for some traditional system calls may actually employ system calls with different names in the kernel. For example, the exit(2) wrapper function actually employs the exit_group(2) system call, and the fork(2) wrapper function actually calls clone(2).

- The behavior of wrapper functions may vary across architectures, according to the range of system calls provided on those architectures. In other words, the same wrapper function may invoke different system calls on different architectures.

- Finally, the behavior of wrapper functions can change across glibc versions. For example, in older versions, the glibc wrapper function for open(2) invoked the system call of the same name, but starting in glibc 2.26, the implementation switched to calling openat(2) on all architectures.

The consequence of the above points is that it may be necessary to filter for a system call other than might be expected. Various manual pages in Section 2 provide helpful details about the differences between wrapper functions and the underlying system calls in subsections entitled C library/kernel differences.

Furthermore, note that the application of seccomp filters even risks causing bugs in an application, when the filters cause unexpected failures for legitimate operations that the application might need to perform. Such bugs may not easily be discovered when testing the seccomp filters if the bugs occur in rarely used application code paths.

### Seccomp-specific BPF details
Note the following BPF details specific to seccomp filters:

- The BPF_H and BPF_B size modifiers are not supported: all operations must load and store (4-byte) words (BPF_W).

- To access the contents of the seccomp_data buffer, use the BPF_ABS addressing mode modifier.

- The BPF_LEN addressing mode modifier yields an immediate mode operand whose value is the size of the seccomp_data buffer.

## EXAMPLES
The program below accepts four or more arguments. The first three arguments are a system call number, a numeric architecture identifier, and an error number. The program uses these values to construct a BPF filter that is used at run time to perform the following checks:

[1]

If the program is not running on the specified architecture, the BPF filter causes system calls to fail with the error ENOSYS.

[2]

If the program attempts to execute the system call with the specified number, the BPF filter causes the system call to fail, with errno being set to the specified error number.

The remaining command-line arguments specify the pathname and additional arguments of a program that the example program should attempt to execute using execv(3) (a library function that employs the execve(2) system call). Some example runs of the program are shown below.

First, we display the architecture that we are running on (x86-64) and then construct a shell function that looks up system call numbers on this architecture:

```
$ uname -m
x86_64
$ syscall_nr() {

    cat /usr/src/linux/arch/x86/syscalls/syscall_64.tbl | \

    awk '$2 != "x32" && $3 == "'$1'" { print $1 }'
}
```

When the BPF filter rejects a system call (case [2] above), it causes the system call to fail with the error number specified on the command line. In the experiments shown here, we'll use error number 99:

```
$ errno 99
EADDRNOTAVAIL 99 Cannot assign requested address
```

In the following example, we attempt to run the command whoami(1), but the BPF filter rejects the execve(2) system call, so that the command is not even executed:

```
$ syscall_nr execve
59
$ ./a.out
Usage: ./a.out <syscall_nr> <arch> <errno> <prog> [<args>]
Hint for <arch>: AUDIT_ARCH_I386: 0x40000003

                 AUDIT_ARCH_X86_64: 0xC000003E
$ ./a.out 59 0xC000003E 99 /bin/whoami
execv: Cannot assign requested address
```

In the next example, the BPF filter rejects the write(2) system call, so that, although it is successfully started, the whoami(1) command is not able to write output:

```
$ syscall_nr write
1
$ ./a.out 1 0xC000003E 99 /bin/whoami
```

In the final example, the BPF filter rejects a system call that is not used by the whoami(1) command, so it is able to successfully execute and produce output:

```
$ syscall_nr preadv
295
$ ./a.out 295 0xC000003E 99 /bin/whoami
cecilia
```

### Program source

```
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/audit.h>
#include <linux/filter.h>
#include <linux/seccomp.h>
#include <sys/prctl.h>
#define X32_SYSCALL_BIT 0x40000000
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
static int
install_filter(int syscall_nr, int t_arch, int f_errno)
{

    unsigned int upper_nr_limit = 0xffffffff;

    /* Assume that AUDIT_ARCH_X86_64 means the normal x86-64 ABI

       (in the x32 ABI, all system calls have bit 30 set in the

       'nr' field, meaning the numbers are >= X32_SYSCALL_BIT). */

    if (t_arch == AUDIT_ARCH_X86_64)

        upper_nr_limit = X32_SYSCALL_BIT - 1;

    struct sock_filter filter[] = {

        /* [0] Load architecture from 'seccomp_data' buffer into

               accumulator. */

        BPF_STMT(BPF_LD | BPF_W | BPF_ABS,

                 (offsetof(struct seccomp_data, arch))),

        /* [1] Jump forward 5 instructions if architecture does not

               match 't_arch'. */

        BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, t_arch, 0, 5),

        /* [2] Load system call number from 'seccomp_data' buffer into

               accumulator. */

        BPF_STMT(BPF_LD | BPF_W | BPF_ABS,

                 (offsetof(struct seccomp_data, nr))),

        /* [3] Check ABI - only needed for x86-64 in deny-list use

               cases.  Use BPF_JGT instead of checking against the bit

               mask to avoid having to reload the syscall number. */

        BPF_JUMP(BPF_JMP | BPF_JGT | BPF_K, upper_nr_limit, 3, 0),

        /* [4] Jump forward 1 instruction if system call number

               does not match 'syscall_nr'. */

        BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, syscall_nr, 0, 1),

        /* [5] Matching architecture and system call: don't execute

           the system call, and return 'f_errno' in 'errno'. */

        BPF_STMT(BPF_RET | BPF_K,

                 SECCOMP_RET_ERRNO | (f_errno & SECCOMP_RET_DATA)),

        /* [6] Destination of system call number mismatch: allow other

               system calls. */

        BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW),

        /* [7] Destination of architecture mismatch: kill process. */

        BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_KILL_PROCESS),

    };

    struct sock_fprog prog = {

        .len = ARRAY_SIZE(filter),

        .filter = filter,

    };

    if (seccomp(SECCOMP_SET_MODE_FILTER, 0, &prog)) {

        perror("seccomp");

        return 1;

    }

    return 0;
}
int
main(int argc, char **argv)
{

    if (argc < 5) {

        fprintf(stderr, "Usage: "

                "%s <syscall_nr> <arch> <errno> <prog> [<args>]\n"

                "Hint for <arch>: AUDIT_ARCH_I386: 0x%X\n"

                "                 AUDIT_ARCH_X86_64: 0x%X\n"

                "\n", argv[0], AUDIT_ARCH_I386, AUDIT_ARCH_X86_64);

        exit(EXIT_FAILURE);

    }

    if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {

        perror("prctl");

        exit(EXIT_FAILURE);

    }

    if (install_filter(strtol(argv[1], NULL, 0),

                       strtol(argv[2], NULL, 0),

                       strtol(argv[3], NULL, 0)))

        exit(EXIT_FAILURE);

    execv(argv[4], &argv[4]);

    perror("execv");

    exit(EXIT_FAILURE);
}
```

## SEE ALSO
bpfc(1), strace(1), bpf(2), prctl(2), ptrace(2), seccomp_unotify(2), sigaction(2), proc(5), signal(7), socket(7)

Various pages from the libseccomp library, including: scmp_sys_resolver(1), seccomp_export_bpf(3), seccomp_init(3), seccomp_load(3), and seccomp_rule_add(3).

The kernel source files Documentation/networking/filter.txt and Documentation/userspace-api/seccomp_filter.rst (or Documentation/prctl/seccomp_filter.txt before Linux 4.13).

McCanne, S. and Jacobson, V. (1992) The BSD Packet Filter: A New Architecture for User-level Packet Capture, Proceedings of the USENIX Winter 1993 Conference http://www.tcpdump.org/papers/bpf-usenix93.pdf