---
name: "sd_bus_creds_get_sgid"
section: "3"
platform: "linux"
package: "libsystemd-dev"
version: "257.13-1~deb13u1"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/sd_bus_creds_get_pid"
---

# sd\_bus\_creds\_get\_sgid(3)

Retrieve fields from a credentials object

Part of [sd\_bus\_creds\_get\_pid(3)](/sd_bus_creds_get_pid/3.md).

## NAME

sd\_bus\_creds\_get\_pid, sd\_bus\_creds\_get\_pidfd\_dup, sd\_bus\_creds\_get\_ppid, sd\_bus\_creds\_get\_tid, sd\_bus\_creds\_get\_uid, sd\_bus\_creds\_get\_euid, sd\_bus\_creds\_get\_suid, sd\_bus\_creds\_get\_fsuid, sd\_bus\_creds\_get\_gid, sd\_bus\_creds\_get\_egid, sd\_bus\_creds\_get\_sgid, sd\_bus\_creds\_get\_fsgid, sd\_bus\_creds\_get\_supplementary\_gids, sd\_bus\_creds\_get\_comm, sd\_bus\_creds\_get\_tid\_comm, sd\_bus\_creds\_get\_exe, sd\_bus\_creds\_get\_cmdline, sd\_bus\_creds\_get\_cgroup, sd\_bus\_creds\_get\_unit, sd\_bus\_creds\_get\_slice, sd\_bus\_creds\_get\_user\_unit, sd\_bus\_creds\_get\_user\_slice, sd\_bus\_creds\_get\_session, sd\_bus\_creds\_get\_owner\_uid, sd\_bus\_creds\_has\_effective\_cap, sd\_bus\_creds\_has\_permitted\_cap, sd\_bus\_creds\_has\_inheritable\_cap, sd\_bus\_creds\_has\_bounding\_cap, sd\_bus\_creds\_get\_selinux\_context, sd\_bus\_creds\_get\_audit\_session\_id, sd\_bus\_creds\_get\_audit\_login\_uid, sd\_bus\_creds\_get\_tty, sd\_bus\_creds\_get\_unique\_name, sd\_bus\_creds\_get\_well\_known\_names, sd\_bus\_creds\_get\_description - Retrieve fields from a credentials object

## SYNOPSIS

```
#include <systemd/sd-bus.h>
```

**int sd\_bus\_creds\_get\_pid(sd\_bus\_creds \****c***, pid\_t \****pid***);**

**int sd\_bus\_creds\_get\_pidfd\_dup(sd\_bus\_creds \****c***, int \****ret\_fd***);**

**int sd\_bus\_creds\_get\_ppid(sd\_bus\_creds \****c***, pid\_t \****ppid***);**

**int sd\_bus\_creds\_get\_tid(sd\_bus\_creds \****c***, pid\_t \****tid***);**

**int sd\_bus\_creds\_get\_uid(sd\_bus\_creds \****c***, uid\_t \****uid***);**

**int sd\_bus\_creds\_get\_euid(sd\_bus\_creds \****c***, uid\_t \****uid***);**

**int sd\_bus\_creds\_get\_suid(sd\_bus\_creds \****c***, uid\_t \****uid***);**

**int sd\_bus\_creds\_get\_fsuid(sd\_bus\_creds \****c***, uid\_t \****uid***);**

**int sd\_bus\_creds\_get\_gid(sd\_bus\_creds \****c***, gid\_t \****gid***);**

**int sd\_bus\_creds\_get\_egid(sd\_bus\_creds \****c***, gid\_t \****gid***);**

**int sd\_bus\_creds\_get\_sgid(sd\_bus\_creds \****c***, gid\_t \****gid***);**

**int sd\_bus\_creds\_get\_fsgid(sd\_bus\_creds \****c***, gid\_t \****gid***);**

**int sd\_bus\_creds\_get\_supplementary\_gids(sd\_bus\_creds \****c***, const gid\_t \*\****gids***);**

**int sd\_bus\_creds\_get\_comm(sd\_bus\_creds \****c***, const char \*\****comm***);**

**int sd\_bus\_creds\_get\_tid\_comm(sd\_bus\_creds \****c***, const char \*\****comm***);**

**int sd\_bus\_creds\_get\_exe(sd\_bus\_creds \****c***, const char \*\****exe***);**

**int sd\_bus\_creds\_get\_cmdline(sd\_bus\_creds \****c***, char \*\*\****cmdline***);**

**int sd\_bus\_creds\_get\_cgroup(sd\_bus\_creds \****c***, const char \*\****cgroup***);**

**int sd\_bus\_creds\_get\_unit(sd\_bus\_creds \****c***, const char \*\****unit***);**

**int sd\_bus\_creds\_get\_slice(sd\_bus\_creds \****c***, const char \*\****slice***);**

**int sd\_bus\_creds\_get\_user\_unit(sd\_bus\_creds \****c***, const char \*\****unit***);**

**int sd\_bus\_creds\_get\_user\_slice(sd\_bus\_creds \****c***, const char \*\****slice***);**

**int sd\_bus\_creds\_get\_session(sd\_bus\_creds \****c***, const char \*\****slice***);**

**int sd\_bus\_creds\_get\_owner\_uid(sd\_bus\_creds \****c***, uid\_t \****uid***);**

**int sd\_bus\_creds\_has\_effective\_cap(sd\_bus\_creds \****c***, int** *capability***);**

**int sd\_bus\_creds\_has\_permitted\_cap(sd\_bus\_creds \****c***, int** *capability***);**

**int sd\_bus\_creds\_has\_inheritable\_cap(sd\_bus\_creds \****c***, int** *capability***);**

**int sd\_bus\_creds\_has\_bounding\_cap(sd\_bus\_creds \****c***, int** *capability***);**

**int sd\_bus\_creds\_get\_selinux\_context(sd\_bus\_creds \****c***, const char \*\****context***);**

**int sd\_bus\_creds\_get\_audit\_session\_id(sd\_bus\_creds \****c***, uint32\_t \****sessionid***);**

**int sd\_bus\_creds\_get\_audit\_login\_uid(sd\_bus\_creds \****c***, uid\_t \****loginuid***);**

**int sd\_bus\_creds\_get\_tty(sd\_bus\_creds \****c***, const char \*\****tty***);**

**int sd\_bus\_creds\_get\_unique\_name(sd\_bus\_creds \****c***, const char \*\****name***);**

**int sd\_bus\_creds\_get\_well\_known\_names(sd\_bus\_creds \****c***, char \*\*\****name***);**

**int sd\_bus\_creds\_get\_description(sd\_bus\_creds \****c***, const char \*\****name***);**

## DESCRIPTION

These functions return credential information from an *sd\_bus\_creds* object. Credential objects may be created with [**sd\_bus\_creds\_new\_from\_pid**(3)](/sd_bus_creds_new_from_pid/3.md), in which case they describe the credentials of the process identified by the specified PID, with [**sd\_bus\_get\_name\_creds**(3)](/sd_bus_get_name_creds/3.md), in which case they describe the credentials of a bus peer identified by the specified bus name, with [**sd\_bus\_get\_owner\_creds**(3)](/sd_bus_get_owner_creds/3.md), in which case they describe the credentials of the creator of a bus, or with [**sd\_bus\_message\_get\_creds**(3)](/sd_bus_message_get_creds/3.md), in which case they describe the credentials of the sender of the message.

Not all credential fields are part of every "sd\_bus\_creds" object. Use [**sd\_bus\_creds\_get\_mask**(3)](/sd_bus_creds_get_mask/3.md) to determine the mask of fields available.

**sd\_bus\_creds\_get\_pid()** will retrieve the PID (process identifier). Similarly, **sd\_bus\_creds\_get\_ppid()** will retrieve the parent PID. Note that PID 1 has no parent process, in which case -ENXIO is returned.

**sd\_bus\_creds\_get\_pidfd\_dup()** will retrieve the PID file descriptor (pidfd), see [**pidfd\_open**(2)](/pidfd_open/2.md) for details. The file descriptor is duplicated and thus must be closed by the caller.

**sd\_bus\_creds\_get\_tid()** will retrieve the TID (thread identifier).

**sd\_bus\_creds\_get\_uid()** will retrieve the numeric UID (user identifier). Similarly, **sd\_bus\_creds\_get\_euid()** returns the effective UID, **sd\_bus\_creds\_get\_suid()** the saved UID and **sd\_bus\_creds\_get\_fsuid()** the file system UID.

**sd\_bus\_creds\_get\_gid()** will retrieve the numeric GID (group identifier). Similarly, **sd\_bus\_creds\_get\_egid()** returns the effective GID, **sd\_bus\_creds\_get\_sgid()** the saved GID and **sd\_bus\_creds\_get\_fsgid()** the file system GID.

**sd\_bus\_creds\_get\_supplementary\_gids()** will retrieve the supplementary GIDs list.

**sd\_bus\_creds\_get\_comm()** will retrieve the comm field (truncated name of the executable, as stored in /proc/*pid*/comm).

**sd\_bus\_creds\_get\_tid\_comm()** will retrieve the comm field of the thread (as stored in /proc/*pid*/task/*tid*/comm).

**sd\_bus\_creds\_get\_exe()** will retrieve the path to the program executable (as stored in the /proc/*pid*/exe link, but with the " (deleted)" suffix removed). Note that kernel threads do not have an executable path, in which case -ENXIO is returned. Note that this property should not be used for more than explanatory information, in particular it should not be used for security-relevant decisions. That's because the executable might have been replaced or removed by the time the value can be processed. Moreover, the kernel exports this information in an ambiguous way (i.e. a deleted executable cannot be safely distinguished from one whose name suffix is " (deleted)").

**sd\_bus\_creds\_get\_cmdline()** will retrieve an array of command line arguments (as stored in /proc/*pid*/cmdline). Note that kernel threads do not have a command line, in which case -ENXIO is returned.

**sd\_bus\_creds\_get\_cgroup()** will retrieve the control group path. See **Control Groups v2**\[1\].

**sd\_bus\_creds\_get\_unit()** will retrieve the systemd unit name (in the system instance of systemd) that the process is a part of. See [**systemd.unit**(5)](/systemd.unit/5.md). For processes that are not part of a unit, returns -ENXIO.

**sd\_bus\_creds\_get\_user\_unit()** will retrieve the systemd unit name (in the user instance of systemd) that the process is a part of. See [**systemd.unit**(5)](/systemd.unit/5.md). For processes that are not part of a user unit, returns -ENXIO.

**sd\_bus\_creds\_get\_slice()** will retrieve the systemd slice (a unit in the system instance of systemd) that the process is a part of. See [**systemd.slice**(5)](/systemd.slice/5.md). Similarly, **sd\_bus\_creds\_get\_user\_slice()** retrieves the systemd slice of the process, in the user instance of systemd.

**sd\_bus\_creds\_get\_session()** will retrieve the identifier of the login session that the process is a part of. Please note the login session may be limited to a stub process or two. User processes may instead be started from their systemd user manager, e.g. GUI applications started using DBus activation, as well as service processes which are shared between multiple logins of the same user. For processes that are not part of a session, returns -ENXIO.

**sd\_bus\_creds\_get\_owner\_uid()** will retrieve the numeric UID (user identifier) of the user who owns the user unit or login session that the process is a part of. See [**systemd-logind.service**(8)](/systemd-logind.service/8.md). For processes that are not part of a user unit or session, returns -ENXIO.

**sd\_bus\_creds\_has\_effective\_cap()** will check whether the capability specified by *capability* was set in the effective capabilities mask. A positive return value means that it was set, zero means that it was not set, and a negative return value indicates an error. See [**capabilities**(7)](/capabilities/7.md) and the *AmbientCapabilities=* and *CapabilityBoundingSet=* settings in [**systemd.exec**(5)](/systemd.exec/5.md).

**sd\_bus\_creds\_has\_permitted\_cap()** is similar to **sd\_bus\_creds\_has\_effective\_cap()**, but will check the permitted capabilities mask.

**sd\_bus\_creds\_has\_inheritable\_cap()** is similar to **sd\_bus\_creds\_has\_effective\_cap()**, but will check the inheritable capabilities mask.

**sd\_bus\_creds\_has\_bounding\_cap()** is similar to **sd\_bus\_creds\_has\_effective\_cap()**, but will check the bounding capabilities mask.

**sd\_bus\_creds\_get\_selinux\_context()** will retrieve the SELinux security context (label) of the process.

**sd\_bus\_creds\_get\_audit\_session\_id()** will retrieve the audit session identifier of the process. Returns -ENXIO for processes that are not part of an audit session.

**sd\_bus\_creds\_get\_audit\_login\_uid()** will retrieve the audit user login identifier (the identifier of the user who is "responsible" for the session). Returns -ENXIO for processes that are not part of an audit session.

**sd\_bus\_creds\_get\_tty()** will retrieve the controlling TTY, without the prefixing "/dev/". Returns -ENXIO for processes that have no controlling TTY.

**sd\_bus\_creds\_get\_unique\_name()** will retrieve the D-Bus unique name. See **The D-Bus specification**\[2\].

**sd\_bus\_creds\_get\_well\_known\_names()** will retrieve the set of D-Bus well-known names. See **The D-Bus specification**\[2\].

**sd\_bus\_creds\_get\_description()** will retrieve a descriptive name of the bus connection of the peer. This name is useful to discern multiple bus connections by the same peer, and may be altered by the peer with the [**sd\_bus\_set\_description**(3)](/sd_bus_set_description/3.md) call.

All functions that take a *const char\*\** parameter will store the answer there as an address of a **NUL**-terminated string. It will be valid as long as *c* remains valid, and should not be freed or modified by the caller.

All functions that take a *char\*\*\** parameter will store the answer there as an address of an array of strings. Each individual string is **NUL**-terminated, and the array is **NULL**-terminated as a whole. It will be valid as long as *c* remains valid, and should not be freed or modified by the caller.

## RETURN VALUE

On success, these calls return 0 or a positive integer. On failure, these calls return a negative errno-style error code.

### Errors

Returned errors may indicate the following problems:

**-ENODATA**

The given field is not available in the credentials object *c*.

**-ENXIO**

The given field is not specified for the described process or peer. This will be returned by **sd\_bus\_creds\_get\_unit()**, **sd\_bus\_creds\_get\_slice()**, **sd\_bus\_creds\_get\_user\_unit()**, **sd\_bus\_creds\_get\_user\_slice()**, and **sd\_bus\_creds\_get\_session()** if the process is not part of a systemd system unit, systemd user unit, systemd slice, or logind session. It will be returned by **sd\_bus\_creds\_get\_owner\_uid()** if the process is not part of a systemd user unit or logind session. It will also be returned by **sd\_bus\_creds\_get\_exe()** and **sd\_bus\_creds\_get\_cmdline()** for kernel threads (since these are not started from an executable binary, nor have a command line), and by **sd\_bus\_creds\_get\_audit\_session\_id()** and **sd\_bus\_creds\_get\_audit\_login\_uid()** when the process is not part of an audit session, and **sd\_bus\_creds\_get\_tty()** if the process has no controlling TTY.

**-EINVAL**

Specified pointer parameter is **NULL**.

**-ENOMEM**

Memory allocation failed.

## NOTES

Functions described here are available as a shared library, which can be compiled against and linked to with the **libsystemd** [**pkg-config**(1)](/pkg-config/1.md) file.

The code described here uses [**getenv**(3)](/getenv/3.md), which is declared to be not multi-thread-safe. This means that the code calling the functions described here must not call [**setenv**(3)](/setenv/3.md) from a parallel thread. It is recommended to only do calls to **setenv()** from an early phase of the program when no other threads have been started.

## HISTORY

**sd\_bus\_creds\_get\_pid()**, **sd\_bus\_creds\_get\_tid()**, **sd\_bus\_creds\_get\_gid()**, **sd\_bus\_creds\_get\_comm()**, **sd\_bus\_creds\_get\_tid\_comm()**, **sd\_bus\_creds\_get\_exe()**, **sd\_bus\_creds\_get\_cmdline()**, **sd\_bus\_creds\_get\_cgroup()**, **sd\_bus\_creds\_get\_unit()**, **sd\_bus\_creds\_get\_user\_unit()**, **sd\_bus\_creds\_get\_slice()**, **sd\_bus\_creds\_get\_session()**, **sd\_bus\_creds\_get\_owner\_uid()**, **sd\_bus\_creds\_has\_effective\_cap()**, **sd\_bus\_creds\_has\_permitted\_cap()**, **sd\_bus\_creds\_has\_inheritable\_cap()**, **sd\_bus\_creds\_has\_bounding\_cap()**, **sd\_bus\_creds\_get\_selinux\_context()**, **sd\_bus\_creds\_get\_audit\_session\_id()**, **sd\_bus\_creds\_get\_audit\_login\_uid()**, **sd\_bus\_creds\_get\_unique\_name()**, **sd\_bus\_creds\_get\_well\_known\_names()**, **sd\_bus\_creds\_get\_ppid()**, **sd\_bus\_creds\_get\_uid()**, **sd\_bus\_creds\_get\_euid()**, **sd\_bus\_creds\_get\_suid()**, **sd\_bus\_creds\_get\_fsuid()**, **sd\_bus\_creds\_get\_egid()**, **sd\_bus\_creds\_get\_sgid()**, **sd\_bus\_creds\_get\_fsgid()**, **sd\_bus\_creds\_get\_supplementary\_gids()**, **sd\_bus\_creds\_get\_tty()**, **sd\_bus\_creds\_get\_description()**, and **sd\_bus\_creds\_get\_user\_slice()** were added in version 221.

**sd\_bus\_creds\_get\_pidfd\_dup()** was added in version 256.

## SEE ALSO

[**systemd**(1)](/systemd/1.md), [**sd-bus**(3)](/sd-bus/3.md), [**sd\_bus\_creds\_new\_from\_pid**(3)](/sd_bus_creds_new_from_pid/3.md), [**fork**(2)](/fork/2.md), [**execve**(2)](/execve/2.md), [**credentials**(7)](/credentials/7.md), [**free**(3)](/free/3.md), [**proc**(5)](/proc/5.md), [**systemd.journal-fields**(7)](/systemd.journal-fields/7.md)

## NOTES

- 1\.

  Control Groups v2

  https://docs.kernel.org/admin-guide/cgroup-v2.html

- 2\.

  The D-Bus specification

  https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names-bus
