---
name: "setusercontext"
section: "3"
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/login_class"
---

# setusercontext(3)

functions for using the login class capabilities database

Part of [login\_class(3)](/login_class/3.md).

## NAME

**setclasscontext**, **setclasscpumask**, **setclassenvironment**, **setclassresources**, **setusercontext** – functions for using the login class capabilities database

## LIBRARY

System Utilities Library (libutil, -lutil)

## SYNOPSIS

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

**#include \<login\_cap.h\>**

*int*\

**setclasscontext**(*const char \*classname*, *unsigned int flags*);

*void*\

**setclasscpumask**(*login\_cap\_t \*lc*);

*void*\

**setclassenvironment**(*login\_cap\_t \*lc*, *const struct passwd \*pwd*, *int paths*);

*void*\

**setclassresources**(*login\_cap\_t \*lc*);

*int*\

**setusercontext**(*login\_cap\_t \*lc*, *const struct passwd \*pwd*, *uid\_t uid*, *unsigned int flags*);

## DESCRIPTION

These functions provide a higher level interface to the login class database than those documented in [login\_cap(3)](/login_cap/3.md). These functions are used to set resource limits, environment and accounting settings for users on logging into the system and when selecting an appropriate set of environment and resource settings for system daemons based on login classes. These functions may only be called if the current process is running with root privileges. If the LOGIN\_SETLOGIN flag is used this function calls [setlogin(2)](/setlogin/2.md), and due care must be taken as detailed in the manpage for that function and this affects all processes running in the same session and not just the current process.

The **setclasscontext**() function sets various class context values (resource limits, umask and process priorities) based on values for a specific named class.

The **setusercontext**() function sets class context values based on a given login\_cap\_t object and a specific passwd record (if login\_cap\_t is NULL), the current session's login, and the current process user and group ownership. Each of these actions is selectable via bit-flags passed in the *flags* parameter, which is comprised of one or more of the following:

- LOGIN\_SETLOGIN

  Set the login associated with the current session to the user specified in the passwd structure using [setlogin(2)](/setlogin/2.md). The *pwd* parameter must not be NULL if this option is used.

- LOGIN\_SETUSER

  Set ownership of the current process to the uid specified in the *uid* parameter using [setuid(2)](/setuid/2.md).

- LOGIN\_SETGROUP

  Set group ownership of the current process to the group id specified in the passwd structure using [setgid(2)](/setgid/2.md), and calls [initgroups(3)](/initgroups/3.md) to set up the group access list for the current process. The *pwd* parameter must not be NULL if this option is used.

- LOGIN\_SETRESOURCES

  Set resource limits for the current process based on values specified in the system login class database. Class capability tags used, with and without -cur (soft limit) or -max (hard limit) suffixes and the corresponding resource setting:

  ```
  cputime          RLIMIT_CPU
  filesize         RLIMIT_FSIZE
  datasize         RLIMIT_DATA
  stacksize        RLIMIT_STACK
  coredumpsize     RLIMIT_CORE
  memoryuse        RLIMIT_RSS
  memorylocked     RLIMIT_MEMLOCK
  maxproc          RLIMIT_NPROC
  openfiles        RLIMIT_NOFILE
  sbsize           RLIMIT_SBSIZE
  vmemoryuse       RLIMIT_VMEM
  pseudoterminals  RLIMIT_NPTS
  swapuse          RLIMIT_SWAP
  kqueues          RLIMIT_KQUEUES
  umtxp            RLIMIT_UMTXP
  pipebuf          RLIMIT_PIPEBUF
  ```

- LOGIN\_SETPRIORITY

  Set the scheduling priority for the current process based on the value specified in the system login class database. Class capability tags used:

  ```
  priority
  ```

- LOGIN\_SETUMASK

  Set the umask for the current process to a value in the user or system login class database. Class capability tags used:

  ```
  umask
  ```

- LOGIN\_SETPATH

  Set the "path" and "manpath" environment variables based on values in the user or system login class database. Class capability tags used with the corresponding environment variables set:

  ```
  path          PATH
  manpath       MANPATH
  ```

- LOGIN\_SETENV

  Set various environment variables based on values in the user or system login class database. Class capability tags used with the corresponding environment variables set:

  ```
  lang          LANG
  charset       MM_CHARSET
  timezone      TZ
  term          TERM
  ```

  Additional environment variables may be set using the list type capability "setenv=var1 val1,var2 val2..,varN valN".

- LOGIN\_SETMAC

  Set the MAC label for the current process to the label specified in system login class database.

- LOGIN\_SETCPUMASK

  Create a new [cpuset(2)](/cpuset/2.md) and set the cpu affinity to the specified mask. The string may contain a comma separated list of numbers and/or number ranges as handled by the [cpuset(1)](/cpuset/1.md) utility or the case-insensitive string ‘default’. If the string is ‘default’ no action will be taken.

- LOGIN\_SETLOGINCLASS

  Set the login class of the current process using [setloginclass(2)](/setloginclass/2.md).

- LOGIN\_SETALL

  Enables all of the above settings.

Note that when setting environment variables and a valid passwd pointer is provided in the *pwd* parameter, the characters ‘~’ and ‘$’ are substituted for the user's home directory and login name respectively.

The **setclasscpumask**(), **setclassresources**() and **setclassenvironment**() functions are subsets of the setcontext functions above, but may be useful in isolation.

## RETURN VALUES

The **setclasscontext**() and **setusercontext**() functions return -1 if an error occurred, or 0 on success. If an error occurs when attempting to set the user, login, group or resources, a message is reported to [syslog(3)](/syslog/3.md), with LOG\_ERR priority and directed to the currently active facility.

## SEE ALSO

[cpuset(1)](/cpuset/1.md), [ps(1)](/ps/1.md), [cpuset(2)](/cpuset/2.md), [setgid(2)](/setgid/2.md), [setlogin(2)](/setlogin/2.md), [setloginclass(2)](/setloginclass/2.md), [setuid(2)](/setuid/2.md), [getcap(3)](/getcap/3.md), [initgroups(3)](/initgroups/3.md), [login\_cap(3)](/login_cap/3.md), [mac\_set\_proc(3)](/mac_set_proc/3.md), [login.conf(5)](/login.conf/5.md), [termcap(5)](/termcap/5.md)

## HISTORY

The functions **setclasscontext**(), **setclasscpumask**(), **setclassenvironment**(), **setclassresources**() and **setusercontext**() first appeared in FreeBSD 2.1.5.
