diff options
Diffstat (limited to 'src/gs-listener-dbus.c')
-rw-r--r-- | src/gs-listener-dbus.c | 238 |
1 files changed, 224 insertions, 14 deletions
diff --git a/src/gs-listener-dbus.c b/src/gs-listener-dbus.c index 601689c..1f759e0 100644 --- a/src/gs-listener-dbus.c +++ b/src/gs-listener-dbus.c @@ -14,7 +14,7 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * * Authors: William Jon McCann <[email protected]> * @@ -32,6 +32,11 @@ #include <dbus/dbus-glib.h> #include <dbus/dbus-glib-lowlevel.h> +#ifdef WITH_SYSTEMD +#include <systemd/sd-daemon.h> +#include <systemd/sd-login.h> +#endif + #include "gs-listener-dbus.h" #include "gs-marshal.h" #include "gs-debug.h" @@ -59,6 +64,15 @@ static DBusHandlerResult gs_listener_message_handler (DBusConnection *connec #define HAL_DEVICE_INTERFACE "org.freedesktop.Hal.Device" +/* systemd logind */ +#define SYSTEMD_LOGIND_SERVICE "org.freedesktop.login1" +#define SYSTEMD_LOGIND_PATH "/org/freedesktop/login1" +#define SYSTEMD_LOGIND_INTERFACE "org.freedesktop.login1.Manager" + +#define SYSTEMD_LOGIND_SESSION_INTERFACE "org.freedesktop.login1.Session" +#define SYSTEMD_LOGIND_SESSION_PATH "/org/freedesktop/login1/session" + +/* consolekit */ #define CK_NAME "org.freedesktop.ConsoleKit" #define CK_MANAGER_PATH "/org/freedesktop/ConsoleKit/Manager" #define CK_MANAGER_INTERFACE "org.freedesktop.ConsoleKit.Manager" @@ -87,6 +101,10 @@ struct GSListenerPrivate time_t session_idle_start; char *session_id; +#ifdef WITH_SYSTEMD + gboolean have_systemd; +#endif + guint32 ck_throttle_cookie; }; @@ -1539,20 +1557,101 @@ _listener_message_path_is_our_session (GSListener *listener, DBusMessage *message) { const char *ssid; - gboolean ours; - - ours = FALSE; ssid = dbus_message_get_path (message); - if (ssid != NULL - && listener->priv->session_id != NULL - && strcmp (ssid, listener->priv->session_id) == 0) - { - ours = TRUE; + + if (ssid == NULL) + return FALSE; + + if (listener->priv->session_id == NULL) + return FALSE; + +#ifdef WITH_SYSTEMD + /* The bus object path is simply the actual session ID + * prefixed to make it a bus path */ + if (listener->priv->have_systemd) + return g_str_has_prefix (ssid, SYSTEMD_LOGIND_SESSION_PATH "/") + && strcmp (ssid + sizeof (SYSTEMD_LOGIND_SESSION_PATH), + listener->priv->session_id) == 0; +#endif + +#ifdef WITH_CONSOLE_KIT + if (strcmp (ssid, listener->priv->session_id) == 0) + return TRUE; +#endif + + return FALSE; +} + +#ifdef WITH_SYSTEMD +static gboolean +properties_changed_match (DBusMessage *message, + const char *property) +{ + DBusMessageIter iter, sub, sub2; + + /* Checks whether a certain property is listed in the + * specified PropertiesChanged message */ + + if (!dbus_message_iter_init (message, &iter)) + goto failure; + + /* Jump over interface name */ + if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_STRING) + goto failure; + + dbus_message_iter_next (&iter); + + /* First, iterate through the changed properties array */ + if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY || + dbus_message_iter_get_element_type (&iter) != DBUS_TYPE_DICT_ENTRY) + goto failure; + + dbus_message_iter_recurse (&iter, &sub); + while (dbus_message_iter_get_arg_type (&sub) != DBUS_TYPE_INVALID) { + const char *name; + + if (dbus_message_iter_get_arg_type (&sub) != DBUS_TYPE_DICT_ENTRY) + goto failure; + + dbus_message_iter_recurse (&sub, &sub2); + dbus_message_iter_get_basic (&sub2, &name); + + if (strcmp (name, property) == 0) + return TRUE; + + dbus_message_iter_next (&sub); } - return ours; + dbus_message_iter_next (&iter); + + /* Second, iterate through the invalidated properties array */ + if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY || + dbus_message_iter_get_element_type (&iter) != DBUS_TYPE_STRING) + goto failure; + + dbus_message_iter_recurse (&iter, &sub); + while (dbus_message_iter_get_arg_type (&sub) != DBUS_TYPE_INVALID) { + const char *name; + + if (dbus_message_iter_get_arg_type (&sub) != DBUS_TYPE_STRING) + goto failure; + + dbus_message_iter_get_basic (&sub, &name); + + if (strcmp (name, property) == 0) + return TRUE; + + dbus_message_iter_next (&sub); + } + + return FALSE; + +failure: + gs_debug ("Failed to decode PropertiesChanged message."); + return FALSE; } +#endif static DBusHandlerResult listener_dbus_handle_system_message (DBusConnection *connection, @@ -1573,6 +1672,52 @@ listener_dbus_handle_system_message (DBusConnection *connection, dbus_message_get_destination (message)); #endif +#ifdef WITH_SYSTEMD + if (listener->priv->have_systemd) { + + if (dbus_message_is_signal (message, SYSTEMD_LOGIND_SESSION_INTERFACE, "Unlock")) { + if (_listener_message_path_is_our_session (listener, message)) { + gs_debug ("Console kit requested session unlock"); + gs_listener_set_active (listener, FALSE); + } + + return DBUS_HANDLER_RESULT_HANDLED; + } else if (dbus_message_is_signal (message, SYSTEMD_LOGIND_SESSION_INTERFACE, "Lock")) { + if (_listener_message_path_is_our_session (listener, message)) { + gs_debug ("ConsoleKit requested session lock"); + g_signal_emit (listener, signals [LOCK], 0); + } + + return DBUS_HANDLER_RESULT_HANDLED; + } else if (dbus_message_is_signal (message, DBUS_INTERFACE_PROPERTIES, "PropertiesChanged")) { + + if (_listener_message_path_is_our_session (listener, message)) { + + if (properties_changed_match (message, "Active")) { + gboolean new_active; + + /* Instead of going via the + * bus to read the new + * property state, let's + * shortcut this and ask + * directly the low-level + * information */ + + new_active = sd_session_is_active (listener->priv->session_id) != 0; + if (new_active) + g_signal_emit (listener, signals [SIMULATE_USER_ACTIVITY], 0); + } + } + + return DBUS_HANDLER_RESULT_HANDLED; + } + + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + } +#endif + +#ifdef WITH_CONSOLE_KIT + if (dbus_message_is_signal (message, HAL_DEVICE_INTERFACE, "Condition")) { DBusError error; @@ -1600,6 +1745,8 @@ listener_dbus_handle_system_message (DBusConnection *connection, return DBUS_HANDLER_RESULT_HANDLED; } + + else if (dbus_message_is_signal (message, CK_SESSION_INTERFACE, "Unlock")) { if (_listener_message_path_is_our_session (listener, message)) @@ -1683,7 +1830,7 @@ listener_dbus_handle_system_message (DBusConnection *connection, return DBUS_HANDLER_RESULT_HANDLED; } - +#endif return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; } @@ -2057,7 +2204,7 @@ gboolean gs_listener_acquire (GSListener *listener, GError **error) { - gboolean acquired; + int acquired; DBusError buserror; gboolean is_connected; @@ -2107,7 +2254,8 @@ gs_listener_acquire (GSListener *listener, acquired = dbus_bus_request_name (listener->priv->connection, GS_LISTENER_SERVICE, - 0, &buserror) != -1; + DBUS_NAME_FLAG_DO_NOT_QUEUE, + &buserror); if (dbus_error_is_set (&buserror)) { g_set_error (error, @@ -2116,6 +2264,14 @@ gs_listener_acquire (GSListener *listener, "%s", buserror.message); } + if (acquired == DBUS_REQUEST_NAME_REPLY_EXISTS) { + g_set_error (error, + GS_LISTENER_ERROR, + GS_LISTENER_ERROR_ACQUISITION_FAILURE, + "%s", + _("screensaver already running in this session")); + return FALSE; + } dbus_error_free (&buserror); @@ -2134,7 +2290,32 @@ gs_listener_acquire (GSListener *listener, listener_dbus_system_filter_function, listener, NULL); +#ifdef WITH_SYSTEMD + if (listener->priv->have_systemd) { + dbus_bus_add_match (listener->priv->system_connection, + "type='signal'" + ",sender='"SYSTEMD_LOGIND_SERVICE"'" + ",interface='"SYSTEMD_LOGIND_SESSION_INTERFACE"'" + ",member='Unlock'", + NULL); + dbus_bus_add_match (listener->priv->system_connection, + "type='signal'" + ",sender='"SYSTEMD_LOGIND_SERVICE"'" + ",interface='"SYSTEMD_LOGIND_SESSION_INTERFACE"'" + ",member='Lock'", + NULL); + dbus_bus_add_match (listener->priv->system_connection, + "type='signal'" + ",sender='"SYSTEMD_LOGIND_SERVICE"'" + ",interface='"DBUS_INTERFACE_PROPERTIES"'" + ",member='PropertiesChanged'", + NULL); + + goto finish; + } +#endif +#ifdef WITH_CONSOLE_KIT dbus_bus_add_match (listener->priv->system_connection, "type='signal'" ",interface='"HAL_DEVICE_INTERFACE"'" @@ -2155,9 +2336,11 @@ gs_listener_acquire (GSListener *listener, ",interface='"CK_SESSION_INTERFACE"'" ",member='ActiveChanged'", NULL); +#endif } - return acquired; +finish: + return (acquired != -1); } static char * @@ -2179,6 +2362,26 @@ query_session_id (GSListener *listener) dbus_error_init (&error); +#ifdef WITH_SYSTEMD + if (listener->priv->have_systemd) { + char *t; + int r; + + r = sd_pid_get_session (0, &t); + if (r < 0) { + gs_debug ("Couldn't determine our own session id: %s", strerror (-r)); + return NULL; + } + + /* t is allocated with malloc(), we need it with g_malloc() */ + ssid = g_strdup(t); + free (t); + + return ssid; + } +#endif + +#ifdef WITH_CONSOLE_KIT message = dbus_message_new_method_call (CK_NAME, CK_MANAGER_PATH, CK_MANAGER_INTERFACE, "GetCurrentSession"); if (message == NULL) { @@ -2205,6 +2408,9 @@ query_session_id (GSListener *listener) dbus_message_unref (reply); return g_strdup (ssid); +#else + return NULL; +#endif } static void @@ -2220,6 +2426,10 @@ gs_listener_init (GSListener *listener) { listener->priv = GS_LISTENER_GET_PRIVATE (listener); +#ifdef WITH_SYSTEMD + listener->priv->have_systemd = sd_booted () > 0; +#endif + gs_listener_dbus_init (listener); init_session_id (listener); |