git: ecd6631b19d9 - main - pam_xdg: Add linger option

From: Emmanuel Vadot <manu_at_FreeBSD.org>
Date: Wed, 18 Nov 2026 07:29:20 UTC
The branch main has been updated by manu:

URL: https://cgit.FreeBSD.org/src/commit/?id=ecd6631b19d9a98f045ad1a0ece0b73b6946d02a

commit ecd6631b19d9a98f045ad1a0ece0b73b6946d02a
Author:     Ross Williams <ross@ross-williams.net>
AuthorDate: 2026-11-18 07:28:18 +0000
Commit:     Emmanuel Vadot <manu@FreeBSD.org>
CommitDate: 2026-11-18 07:28:18 +0000

    pam_xdg: Add linger option
    
    The pam_xdg module deletes a user's XDG_RUNTIME_DIR when the last
    interactive session logs out. If the user leaves background processes
    running on the system, like a terminal multiplexer, that puts state or
    domain sockets in XDG_RUNTIME_DIR, then that state gets destroyed and
    the processes become effectively orphaned.
    
    On Linux, systemd-logind not only unlinks XDG_RUNTIME_DIR but also kills
    all user processes on logout. To allow long-lived background processes,
    it implements a per-user "linger" mode to disable the unlink and kill.
    
    Because FreeBSD's pam_xdg does not implement process killing, this
    "linger" option implementation skips only XDG_RUNTIME_DIR unlinking
    when the user still has running processes after the last session logs
    out.
    
    Differential Revision:  https://reviews.freebsd.org/D61332
---
 lib/libpam/modules/pam_xdg/pam_xdg.8 |  9 +++++++-
 lib/libpam/modules/pam_xdg/pam_xdg.c | 40 ++++++++++++++++++++++++++++++------
 2 files changed, 42 insertions(+), 7 deletions(-)

diff --git a/lib/libpam/modules/pam_xdg/pam_xdg.8 b/lib/libpam/modules/pam_xdg/pam_xdg.8
index 031010953e98..5043be0915a9 100644
--- a/lib/libpam/modules/pam_xdg/pam_xdg.8
+++ b/lib/libpam/modules/pam_xdg/pam_xdg.8
@@ -41,10 +41,17 @@ according to the Cross Desktop Group Base Directory Specification.
 By default the directory is created under
 .Pa /var/run/xdg/ Ns $ Ns Ev USER .
 .Pp
-The following option may be passed to the authentication module:
+The following options may be passed to the
+.Nm
+module:
 .Bl -tag -width "runtime_dir_prefix=directory"
 .It Cm runtime_dir_prefix Ns = Ns Ar directory
 Use an alternate base directory
+.It Cm linger
+Keeps the runtime directory of a user if they have running processes, even when
+their last session logs out. Allows the user to continue running processes in
+the background that create sockets or files in
+.Ev XDG_RUNTIME_DIR.
 .El
 .Sh ENVIRONMENT
 .Bl -tag -width indent
diff --git a/lib/libpam/modules/pam_xdg/pam_xdg.c b/lib/libpam/modules/pam_xdg/pam_xdg.c
index 10c6467776a3..a9d37ed27e19 100644
--- a/lib/libpam/modules/pam_xdg/pam_xdg.c
+++ b/lib/libpam/modules/pam_xdg/pam_xdg.c
@@ -26,6 +26,8 @@
  */
 
 #include <sys/stat.h>
+#include <sys/sysctl.h>
+#include <sys/user.h>
 #include <dirent.h>
 #include <errno.h>
 #include <fcntl.h>
@@ -205,6 +207,27 @@ remove_dir(int fd)
 	return (0);
 }
 
+static int
+uid_has_procs(int uid)
+{
+	int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_UID, 0};
+	struct kinfo_proc kp;
+	size_t kp_len = sizeof(kp);
+
+	mib[3] = uid;
+
+	/*
+	 * Only pass a stack allocation with room for one kinfo_proc, and ignore
+	 * ENOMEM. This gives a zero-allocation way to discover whether the UID
+	 * has any processes.
+	 */
+	if (sysctl(mib, nitems(mib), &kp, &kp_len, NULL, 0) && errno != ENOMEM) {
+		return -1;
+	}
+
+	return (kp_len > 0);
+}
+
 static int
 _pam_xdg_close(pam_handle_t *pamh __unused, int flags __unused,
     int argc __unused, const char *argv[] __unused)
@@ -291,13 +314,18 @@ _pam_xdg_close(pam_handle_t *pamh __unused, int flags __unused,
 	}
 	close(session_file);
 
-	/* Final cleanup if last user session */
+	/*
+	 * Final cleanup if last user session and linger not enabled,
+	 * or linger enabled but user has no processes
+	 */
 	if (i == 0) {
-		remove_dir(rt_dir);
-		if (unlinkat(rt_dir_prefix, user, AT_REMOVEDIR) != 0) {
-			PAM_VERBOSE_ERROR("Can't cleanup %s/%s\n", runtime_dir_prefix, user);
-			rv = PAM_SESSION_ERR;
-			goto out;
+		if (openpam_get_option(pamh, "linger") == NULL || !uid_has_procs(passwd->pw_uid)) {
+			remove_dir(rt_dir);
+			if (unlinkat(rt_dir_prefix, user, AT_REMOVEDIR) != 0) {
+				PAM_VERBOSE_ERROR("Can't cleanup %s/%s\n", runtime_dir_prefix, user);
+				rv = PAM_SESSION_ERR;
+				goto out;
+			}
 		}
 	}