at_console: ask systemd whether a user is at the console
authorLennart Poettering <lennart@poettering.net>
Thu, 2 Feb 2012 04:06:25 +0000 (05:06 +0100)
committerSimon McVittie <simon.mcvittie@collabora.co.uk>
Tue, 7 Feb 2012 16:06:33 +0000 (16:06 +0000)
systemd manages seats and users. This patch optionally asks systemd
whether a user is at the console. It used libsystemd-login for that, a
low-level library that allows querying this kind of information without
expensive round trips.

In order to be nice to the Debian folks this patch falls back to
traditional modes of operation if systemd is not found to be around.

Bug: https://bugs.freedesktop.org/show_bug.cgi?id=39609
Reviewed-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
configure.ac
dbus/Makefile.am
dbus/dbus-userdb-util.c

index 6ecea15..2b9e740 100644 (file)
@@ -137,6 +137,7 @@ AC_ARG_ENABLE(kqueue, AS_HELP_STRING([--enable-kqueue],[build with kqueue suppor
 AC_ARG_ENABLE(console-owner-file, AS_HELP_STRING([--enable-console-owner-file],[enable console owner file]),enable_console_owner_file=$enableval,enable_console_owner_file=auto)
 AC_ARG_ENABLE(userdb-cache, AS_HELP_STRING([--enable-userdb-cache],[build with userdb-cache support]),enable_userdb_cache=$enableval,enable_userdb_cache=yes)
 AC_ARG_ENABLE(launchd, AS_HELP_STRING([--enable-launchd],[build with launchd auto-launch support]),enable_launchd=$enableval,enable_launchd=auto)
+AC_ARG_ENABLE(systemd, AS_HELP_STRING([--enable-systemd],[build with systemd at_console support]),enable_systemd=$enableval,enable_systemd=auto)
 
 AC_ARG_WITH(xml, AS_HELP_STRING([--with-xml=[libxml/expat]],[XML library to use (libxml may be named libxml2 on some systems)]))
 AC_ARG_WITH(init-scripts, AS_HELP_STRING([--with-init-scripts=[redhat]],[Style of init scripts to install]))
@@ -1134,6 +1135,26 @@ fi
 
 AM_CONDITIONAL(HAVE_CONSOLE_OWNER_FILE, test x$have_console_owner_file = xyes)
 
+dnl systemd detection
+if test x$enable_systemd = xno ; then
+    have_systemd=no;
+else
+    PKG_CHECK_MODULES(SYSTEMD,
+        [ libsystemd-login libsystemd-daemon ],
+        have_systemd=yes,
+        have_systemd=no)
+    AC_SUBST(SYSTEMD_CFLAGS)
+    AC_SUBST(SYSTEMD_LIBS)
+fi
+
+if test x$have_systemd = xyes; then
+    AC_DEFINE(HAVE_SYSTEMD,1,[Have systemd])
+fi
+
+if test x$enable_systemd = xyes -a x$have_systemd != xyes ; then
+    AC_MSG_ERROR([Explicitly requested systemd support, but systemd not found])
+fi
+
 # libaudit detection
 if test x$enable_libaudit = xno ; then
     have_libaudit=no;
@@ -1720,6 +1741,7 @@ echo "
         Building inotify support: ${have_inotify}
         Building dnotify support: ${have_dnotify}
         Building kqueue support:  ${have_kqueue}
+        Building systemd support: ${have_systemd}
         Building X11 code:        ${enable_x11}
         Building Doxygen docs:    ${enable_doxygen_docs}
         Building XML docs:        ${enable_xml_docs}
index 1c8fe8c..9ea254e 100644 (file)
@@ -4,6 +4,7 @@ configdir=$(sysconfdir)/dbus-1
 AM_CPPFLAGS = \
        -I$(top_builddir) \
        -I$(top_srcdir) \
+       $(SYSTEMD_CFLAGS) \
        -DDBUS_COMPILATION \
        -DDBUS_MACHINE_UUID_FILE=\""$(localstatedir)/lib/dbus/machine-id"\" \
        -DDBUS_SYSTEM_CONFIG_FILE=\""$(configdir)/system.conf"\" \
@@ -284,7 +285,7 @@ libdbus_internal_la_CPPFLAGS = \
        $(AM_CPPFLAGS) \
        -DDBUS_STATIC_BUILD \
        $(NULL)
-libdbus_internal_la_LIBADD=$(LIBDBUS_LIBS)
+libdbus_internal_la_LIBADD=$(LIBDBUS_LIBS) $(SYSTEMD_LIBS)
 
 noinst_PROGRAMS =
 
index c44c014..16bf229 100644 (file)
 #include "dbus-protocol.h"
 #include <string.h>
 
+#if HAVE_SYSTEMD
+#include <systemd/sd-daemon.h>
+#include <systemd/sd-login.h>
+#endif
+
 /**
  * @addtogroup DBusInternalsUtils
  * @{
@@ -47,7 +52,28 @@ _dbus_is_console_user (dbus_uid_t uid,
 
   DBusUserDatabase *db;
   const DBusUserInfo *info;
-  dbus_bool_t result = FALSE; 
+  dbus_bool_t result = FALSE;
+
+#ifdef HAVE_SYSTEMD
+  if (sd_booted () > 0)
+    {
+      int r;
+
+      /* Check whether this user is logged in on at least one physical
+         seat */
+      r = sd_uid_get_seats (uid, 0, NULL);
+      if (r < 0)
+        {
+          dbus_set_error (error, _dbus_error_from_errno (-r),
+                          "Failed to determine seats of user \"" DBUS_UID_FORMAT "\": %s",
+                          uid,
+                          _dbus_strerror (-r));
+          return FALSE;
+        }
+
+      return (r > 0);
+    }
+#endif
 
 #ifdef HAVE_CONSOLE_OWNER_FILE
 
@@ -414,6 +440,7 @@ _dbus_userdb_test (const char *test_data_dir)
   dbus_uid_t uid;
   unsigned long *group_ids;
   int n_group_ids, i;
+  DBusError error;
 
   if (!_dbus_username_from_current_process (&username))
     _dbus_assert_not_reached ("didn't get username");
@@ -435,7 +462,17 @@ _dbus_userdb_test (const char *test_data_dir)
       printf(" %ld", group_ids[i]);
 
   printf ("\n");
+
+  dbus_error_init (&error);
+  printf ("Is Console user: %i\n",
+          _dbus_is_console_user (uid, &error));
+  printf ("Invocation was OK: %s\n", error.message ? error.message : "yes");
+  dbus_error_free (&error);
+  printf ("Is Console user 4711: %i\n",
+          _dbus_is_console_user (4711, &error));
+  printf ("Invocation was OK: %s\n", error.message ? error.message : "yes");
+  dbus_error_free (&error);
+
   dbus_free (group_ids);
 
   return TRUE;