SD_PID_GET_OWNER_UID(3)
π SD_PID_GET_OWNER_UID(3) β like, the sd-login girlies that tell you who owns what
okay so bestie, this whole page lives in sd-login, and it's basically one big family of functions that answer the question "hey, who does this process belong to?" The main character is sd_pid_get_owner_uid, and the rest of the crew determine the owner uid of the user unit or session, or the session, user unit, system unit, container/VM or slice that a specific PID or socket peer belongs to. no cap, that's the whole vibe. π
β¨ SYNOPSIS β¨
first things first, you gotta invite the library in:
#include <systemd/sd-login.h>
then the whole squad of functions, exactly as they are, no edits:
int sd_pid_get_owner_uid(pid_t pid, uid_t *uid);
int sd_pid_get_session(pid_t pid, char **session);
int sd_pid_get_user_unit(pid_t pid, char **unit);
int sd_pid_get_unit(pid_t pid, char **unit);
int sd_pid_get_machine_name(pid_t pid, char **name);
int sd_pid_get_slice(pid_t pid, char **slice);
int sd_pid_get_user_slice(pid_t pid, char **slice);
int sd_pid_get_cgroup(pid_t pid, char **cgroup);
int sd_pidfd_get_owner_uid(int pidfd, uid_t *uid);
int sd_pidfd_get_session(int pidfd, char **session);
int sd_pidfd_get_user_unit(int pidfd, char **unit);
int sd_pidfd_get_unit(int pidfd, char **unit);
int sd_pidfd_get_machine_name(int pidfd, char **name);
int sd_pidfd_get_slice(int pidfd, char **slice);
int sd_pidfd_get_user_slice(int pidfd, char **slice);
int sd_pidfd_get_cgroup(int pidfd, char **cgroup);
int sd_peer_get_owner_uid(int fd, uid_t *uid);
int sd_peer_get_session(int fd, char **session);
int sd_peer_get_user_unit(int fd, char **unit);
int sd_peer_get_unit(int fd, char **unit);
int sd_peer_get_machine_name(int fd, char **name);
int sd_peer_get_slice(int fd, char **slice);
int sd_peer_get_user_slice(int fd, char **slice);
int sd_peer_get_cgroup(int fd, char **cgroup);
like, yes, that's a lot of them. they're all iconic in their own way tho π«Ά
πΈ DESCRIPTION πΈ
the sd_pid_* girlies (they take a PID)
sd_pid_get_owner_uid()β this one tells you the Unix UID (user identifier) which owns the login session or systemd user unit of a process identified by the specified PID. and if the process is not part of a login session and not managed by a user manager? it just fails with-ENODATA. sorry bestie πsd_pid_get_session()β gives you the login session identifier of a process identified by the specified process identifier. the session identifier is a short string, suitable for usage in file system paths. important tea: 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 which are not part of a login session, this function will fail with-ENODATA. and the returned string needs to be freed with the libcfree(3)call after use β don't forget, that's a leak waiting to happen πsd_pid_get_user_unit()β determines the systemd user unit (i.e. user service or scope unit) identifier of a process identified by the specified PID. the unit name is a short string, suitable for usage in file system paths. for processes which are not managed by a user manager, this function will fail with-ENODATA. returned string β free it with the libcfree(3)call after use. πsd_pid_get_unit()β determines the systemd system unit (i.e. system service or scope unit) identifier of a process identified by the specified PID. the unit name is a short string, suitable for usage in file system paths. note that not all processes are part of a system unit/service. for processes not being part of a systemd system unit, this function will fail with-ENODATA. (more specifically, this call will not work for kernel threads β yeah, kernel threads don't get an invite to this party.) the returned string needs to be freed with the libcfree(3)call after use.sd_pid_get_machine_name()β determines the name of the VM or container is a member of. (that's literally how the original says it, we're not fixing it, it's part of the charm β¨) the machine name is a short string, suitable for usage in file system paths. the returned string needs to be freed with the libcfree(3)call after use. for processes not part of a VM or container, this function fails with-ENODATA.sd_pid_get_slice()β determines the slice unit the process is a member of. seesystemd.slice(5)for details about slices. the returned string needs to be freed with the libcfree(3)call after use. πsd_pid_get_user_slice()β similarly, this one returns the user slice (as managed by the user's systemd instance) of a process.sd_pid_get_cgroup()β returns the control group path of the specified process, relative to the root of the hierarchy. it returns the path without trailing slash, except for processes located in the root control group, where"/"is returned. to find the actual control group path in the file system, the returned path needs to be prefixed with/sys/fs/cgroup/. iykyk π
π«Ά cute shortcut: if the pid parameter of any of these functions is passed as 0, the operation is executed for the calling process. it's giving self-care.
the sd_pidfd_* girlies (they take a PIDFD)
the sd_pidfd_get_owner_uid(), sd_pidfd_get_session(), sd_pidfd_get_user_unit(), sd_pidfd_get_unit(), sd_pidfd_get_machine_name(), sd_pidfd_get_slice(), sd_pidfd_get_user_slice() and sd_pidfd_get_cgroup() calls operate similarly to their PID counterparts, but accept a PIDFD instead of a PID, which means they are not subject to recycle race conditions as the process is pinned by the file descriptor during the whole duration of the invocation. that's the upgrade, honestly π
note that these require a kernel that supports PIDFD. a suitable file descriptor may be acquired via pidfd_open(2).
the sd_peer_* girlies (they take a socket)
the sd_peer_get_owner_uid(), sd_peer_get_session(), sd_peer_get_user_unit(), sd_peer_get_unit(), sd_peer_get_machine_name(), sd_peer_get_slice(), sd_peer_get_user_slice() and sd_peer_get_cgroup() calls operate similarly to their PID counterparts, but accept a connected AF_UNIX socket and retrieve information about the connected peer process.
β οΈ real talk: note that these fields are retrieved via /proc/, and hence are not suitable for authorization purposes, as they are subject to races. do not use them to gatekeep. periodt.
π RETURN VALUE π
on success, these calls return 0 or a positive integer. on failure, these calls return a negative errno-style error code. simple as that π
π Errors
returned errors may indicate the following problems:
-ESRCHβ the specified PID does not refer to a running process.-EBADFβ the specified file descriptor is invalid.-ENODATAβ the given field is not specified for the described process or peer.-EINVALβ an input parameter was invalid (out of range, orNULL, where that is not accepted).-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) file. yes, that's how you actually get them, don't just vibe and hope π
β οΈ also: note that the login session identifier as returned bysd_pid_get_session()is completely unrelated to the process session identifier as returned bygetsid(2). like, different things entirely. don't mix them up bestie π
π HISTORY π
sd_peer_get_cgroup(), sd_peer_get_machine_name(), sd_peer_get_owner_uid(), sd_peer_get_session(), sd_peer_get_slice(), sd_peer_get_unit(), sd_peer_get_user_slice(), sd_peer_get_user_unit(), sd_pid_get_cgroup(), sd_pid_get_machine_name(), sd_pid_get_owner_uid(), sd_pid_get_session(), sd_pid_get_slice(), sd_pid_get_unit(), sd_pid_get_user_slice(), and sd_pid_get_user_unit() were added in version 236. the OGs β¨
sd_pidfd_get_owner_uid(), sd_pidfd_get_session(), sd_pidfd_get_user_unit(), sd_pidfd_get_unit(), sd_pidfd_get_machine_name(), sd_pidfd_get_slice(), sd_pidfd_get_user_slice(), and sd_pidfd_get_cgroup() were added in version 253. the glow-up generation π
π SEE ALSO π
systemd(1), sd-login(3), sd_session_is_active(3), getsid(2), systemd.slice(5), systemd-machined.service(8)
and that's the whole page, bestie π«Ά this one's from systemd 261.3, dated SD_PID_GET_OWNER_UID(3) β go forth and free your strings, mwah πππ¦
π¦ where this page comes from
- ubuntu stonking: libsystemd-dev 261.3-0ubuntu3 Β· LGPL-2.1-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms