Page MenuHomeFreeBSD

D61332.1794319803.diff
No OneTemporary

Authored By
Unknown
Size
2 KB
Referenced Files
None
Subscribers
None

D61332.1794319803.diff

diff --git a/lib/libpam/modules/pam_xdg/pam_xdg.8 b/lib/libpam/modules/pam_xdg/pam_xdg.8
--- a/lib/libpam/modules/pam_xdg/pam_xdg.8
+++ b/lib/libpam/modules/pam_xdg/pam_xdg.8
@@ -41,10 +41,17 @@
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
--- 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,24 @@
return (0);
}
+static int
+uid_has_procs(int uid)
+{
+ const int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_UID, uid};
+ const size_t mib_len = 4;
+ struct kinfo_proc kp;
+ size_t kp_len = sizeof(kp);
+
+ // 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, mib_len, &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 +311,18 @@
}
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;
+ }
}
}

File Metadata

Mime Type
text/plain
Expires
Tue, Nov 10, 2:10 PM (1 h, 43 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41626806
Default Alt Text
D61332.1794319803.diff (2 KB)

Event Timeline