SD_SESSION_IS_ACTIVE(3)

πŸ“¦
version 262.1 Β· 4 versions
, in arch rolling

πŸ’– SD_SESSION_IS_ACTIVE(3) πŸ’–

okay so bestie, welcome to sd_session_is_active β€” section 3, library calls, the whole login-session cinematic universe. this is the little squad of functions that lets you peek at what a session is actually doing. pull up a chair, we're doing lore. 🫢


🌸 NAME 🌸

sd_session_is_active, sd_session_is_remote, sd_session_get_state, sd_session_get_uid, sd_session_get_username, sd_session_get_seat, sd_session_get_start_time, sd_session_get_service, sd_session_get_type, sd_session_get_class, sd_session_get_desktop, sd_session_get_display, sd_session_get_tty, sd_session_get_vt, sd_session_get_remote_host, sd_session_get_remote_user, sd_session_get_leader, sd_session_get_extra_device_access β€” determine state of a specific session πŸ‘‘

✨ SYNOPSIS ✨

#include <systemd/sd-login.h>

int sd_session_is_active(const char *session);

int sd_session_is_remote(const char *session);

int sd_session_get_state(const char *session, char **state);

int sd_session_get_uid(const char *session, uid_t *uid);

int sd_session_get_username(const char *session, char **username);

int sd_session_get_seat(const char *session, char **seat);

int sd_session_get_start_time(const char *session, uint64_t *usec);

int sd_session_get_service(const char *session, char **service);

int sd_session_get_type(const char *session, char **type);

int sd_session_get_class(const char *session, char **class);

int sd_session_get_desktop(const char *session, char **desktop);

int sd_session_get_display(const char *session, char **display);

int sd_session_get_leader(const char *session, pid_t *leader);

int sd_session_get_remote_host(const char *session, char **remote_host);

int sd_session_get_remote_user(const char *session, char **remote_user);

int sd_session_get_tty(const char *session, char **tty);

int sd_session_get_vt(const char *session, unsigned int *vt);

int sd_session_get_extra_device_access(const char *session,
                                       char ***ret_ids);

πŸ¦‹ DESCRIPTION πŸ¦‹

literally a whole friend group of getters, one per session fact. here's the tea on each πŸ’…

sd_session_is_active() β€” use this to find out whether the session with that session identifier is currently active right now, meaning it's in the foreground and available for user input, or not. main character energy check.

sd_session_is_remote() β€” tells you whether the session with that session identifier is a remote session (i.e. its remote host is known) or not. long distance relationship, fr.

sd_session_get_state() β€” gives you the state of that session. the states we currently know about are:

  • "online" β€” session logged in, but session not active, i.e. not in the foreground
  • "active" β€” session logged in and active, i.e. in the foreground
  • "closing" β€” session nominally logged out, but some processes belonging to it are still around

and in the future, more states might get defined, so write your client code to be robust in regards to additional state strings being returned. don't be weird about it. this function is a more generic version of sd_session_is_active(). the returned string needs to be freed with the libc free(3) call after use. ✨

sd_session_get_uid() β€” gives you the user identifier of the Unix user that the session belongs to.

sd_session_get_username() β€” gives you the name of the Unix user that the session belongs to. the returned string needs to be freed with the libc free(3) call after use.

sd_session_get_seat() β€” gives you the seat identifier of the seat the session belongs to. ⚠️ heads up: not all sessions are attached to a seat, and for those this call will fail, returning -ENODATA. the returned string needs to be freed with the libc free(3) call after use.

sd_session_get_start_time() β€” gives you the start time of the session. the usec is in microseconds since the epoch (CLOCK_REALTIME).

sd_session_get_service() β€” gives you the name of the service (as passed during PAM session setup) that registered the session. the returned string needs to be freed with the libc free(3) call after use.

sd_session_get_type() β€” gives you the type of the session. the returned string is one of "x11", "wayland", "tty", "mir" or "unspecified", and it needs to be freed with the libc free(3) call after use.

sd_session_get_class() β€” gives you the class of the session. the returned string is one of "user", "user-early", "user-light", "user-early-light", "user-incomplete", "greeter", "lock-screen", "background", "background-light", "manager" or "manager-early", and it needs to be freed with the libc free(3) call after use.

sd_session_get_desktop() β€” gives you the brand of the desktop running on the session. this field can be set freely by desktop environments and doesn't follow any special formatting. however, desktops are strongly recommended to use the same identifiers and capitalization as for $XDG_CURRENT_DESKTOP, as defined by the Desktop Entry Specification[1]. the returned string needs to be freed with the libc free(3) call after use.

sd_session_get_display() β€” gives you the X11 display of the session. the returned string needs to be freed with the libc free(3) call after use.

sd_session_get_leader() β€” gives you the PID of the leader of the session.

sd_session_get_remote_host() β€” gives you the remote hostname of the session. the returned string needs to be freed with the libc free(3) call after use.

sd_session_get_remote_user() β€” gives you the remote username of the session. the returned string needs to be freed with the libc free(3) call after use. 🚨 note that this value is rarely known to the system, and even then should not be relied on. don't build your whole personality on it.

sd_session_get_tty() β€” gives you the TTY device of the session. the returned string needs to be freed with the libc free(3) call after use.

sd_session_get_vt() β€” gives you the VT number of the session. this function will return an error if the seat does not support VTs.

sd_session_get_extra_device_access() β€” tells you which additional hardware devices the session is granted access to. for every "ID" in the list, the session is granted access to all devices tagged with "xaccess-ID" in udev.

and here's the little shortcut that ties it all together: if the session parameter of any of these functions is passed as NULL, the operation is executed for the session the calling process is a member of, if there is any. πŸŽ€


πŸ”₯ RETURN VALUE πŸ”₯

if the test succeeds, sd_session_is_active() and sd_session_is_remote() return a positive integer; if it fails, 0.

on success, sd_session_get_state(), sd_session_get_uid(), sd_session_get_username(), sd_session_get_seat(), sd_session_get_service(), sd_session_get_type(), sd_session_get_class(), sd_session_get_display(), sd_session_get_leader(), sd_session_get_remote_user(), sd_session_get_remote_host(), sd_session_get_tty(), and sd_session_get_extra_device_access() 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:

  • -ENXIO β€” the specified session does not exist. it's giving ghosted. πŸ‘»
  • -ENODATA β€” the given field is not specified for the described session.
  • -EINVAL β€” an input parameter was invalid (out of range, or NULL, where that is not accepted).
  • -ENOMEM β€” memory allocation failed. 😭

πŸ“ NOTES πŸ“

the functions described here are available as a shared library</shared> β€” wait, as a shared library β€” which can be compiled against and linked to with the libsystemd pkg-config(1) file. ✨

⚠️ important bestie warning: the code described here uses getenv(3), which is declared to be not multi-thread-safe. this means that the code calling the functions described here must not call setenv(3) 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. do not fight the threads, you will lose. πŸ’€

πŸ“œ HISTORY πŸ“œ

the timeline, in order, no cap:

  • sd_session_get_state() was added in version 186.
  • sd_session_get_tty() was added in version 198.
  • sd_session_get_vt() was added in version 207.
  • sd_session_is_remote(), sd_session_get_remote_host(), and sd_session_get_remote_user() were added in version 209.
  • sd_session_get_desktop() was added in version 217.
  • sd_session_get_username(), sd_session_get_start_time(), and sd_session_get_leader() were added in version 254.
  • sd_session_get_extra_device_access() was added in version 260.

πŸ‘€ SEE ALSO πŸ‘€

systemd(1), sd-login(3), sd_pid_get_session(3) β€” go read the whole extended universe, iykyk. 🌈


πŸ“Ž NOTES πŸ“Ž (the footnote sequel)

  1. Desktop Entry Specification β€” https://standards.freedesktop.org/desktop-entry-spec/latest/

and that's the page, bestie! this was systemd 262.1, page SD_SESSION_IS_ACTIVE(3), and it ate, honestly. now go free your strings with free(3) and hydrate. πŸ’–πŸŽ€πŸ¦‹πŸŒΈπŸ’…πŸ«Ά

πŸ“¦ where this page comes from
  • arch rolling: systemd-libs 262.1-1 Β· LGPL-2.1-or-later AND CC0-1.0 AND GPL-2.0-or-later WITH Linux-syscall-note
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms