]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blobdiff - src/login1.c
Load all users only when really needed
[sojka/lightdm.git] / src / login1.c
index 9ae9d33f9c1001f7fabb36b19b8c3e6edefe0dc1..dc60a492a02a09557ba842d0809a71210103e330 100644 (file)
 #include "login1.h"
 
 #define LOGIN1_SERVICE_NAME "org.freedesktop.login1"
+#define LOGIN1_OBJECT_NAME "/org/freedesktop/login1"
+#define LOGIN1_MANAGER_INTERFACE_NAME "org.freedesktop.login1.Manager"
 
 enum {
     SEAT_ADDED,
     SEAT_REMOVED,
-    LAST_SIGNAL
+    LAST_SERVICE_SIGNAL
 };
-static guint signals[LAST_SIGNAL] = { 0 };
+static guint service_signals[LAST_SERVICE_SIGNAL] = { 0 };
 
 struct Login1ServicePrivate
 {
@@ -39,14 +41,27 @@ struct Login1ServicePrivate
     guint signal_id;
 };
 
+enum {
+    CAN_GRAPHICAL_CHANGED,
+    ACTIVE_SESSION_CHANGED,
+    LAST_SEAT_SIGNAL
+};
+static guint seat_signals[LAST_SEAT_SIGNAL] = { 0 };
+
 struct Login1SeatPrivate
 {
+    /* Connection to bus seat is running on */
+    GDBusConnection *connection;
+
     /* Seat Id */
     gchar *id;
 
     /* D-Bus path for this seat */
     gchar *path;
 
+    /* Handle to signal subscription */
+    guint signal_id;
+
     /* TRUE if can run a graphical display on this seat */
     gboolean can_graphical;
 
@@ -59,175 +74,77 @@ G_DEFINE_TYPE (Login1Seat, login1_seat, G_TYPE_OBJECT);
 
 static Login1Service *singleton = NULL;
 
-gchar *
-login1_get_session_id (void)
+Login1Service *
+login1_service_get_instance (void)
 {
-    GDBusConnection *bus;
-    GVariant *result;
-    gchar *session_path;
-    GError *error = NULL;
-
-    bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
-    if (error)
-        g_warning ("Failed to get system bus: %s", error->message);
-    g_clear_error (&error);
-    if (!bus)
-        return NULL;
-    result = g_dbus_connection_call_sync (bus,
-                                          LOGIN1_SERVICE_NAME,
-                                          "/org/freedesktop/login1",
-                                          "org.freedesktop.login1.Manager",
-                                          "GetSessionByPID",
-                                          g_variant_new ("(u)", getpid()),
-                                          G_VARIANT_TYPE ("(o)"),
-                                          G_DBUS_CALL_FLAGS_NONE,
-                                          -1,
-                                          NULL,
-                                          &error);
-    g_object_unref (bus);
-
-    if (error)
-        g_warning ("Failed to open login1 session: %s", error->message);
-    g_clear_error (&error);
-    if (!result)
-        return NULL;
-
-    g_variant_get (result, "(o)", &session_path);
-    g_variant_unref (result);
-    g_debug ("Got login1 session id: %s", session_path);
-
-    return session_path;
+    if (!singleton)
+        singleton = g_object_new (LOGIN1_SERVICE_TYPE, NULL);
+    return singleton;
 }
 
-void
-login1_lock_session (const gchar *session_path)
+static void
+update_property (Login1Seat *seat, const gchar *name, GVariant *value)
 {
-    GDBusConnection *bus;
-    GError *error = NULL;
-
-    g_return_if_fail (session_path != NULL);
-
-    g_debug ("Locking login1 session %s", session_path);
-
-    bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
-    if (error)
-        g_warning ("Failed to get system bus: %s", error->message);
-    g_clear_error (&error);
-    if (!bus)
-        return;
-
-    if (session_path)
+    if (strcmp (name, "CanGraphical") == 0 && g_variant_is_of_type (value, G_VARIANT_TYPE_BOOLEAN))
     {
-        GVariant *result;
-
-        result = g_dbus_connection_call_sync (bus,
-                                              LOGIN1_SERVICE_NAME,
-                                              session_path,
-                                              "org.freedesktop.login1.Session",
-                                              "Lock",
-                                              g_variant_new ("()"),
-                                              G_VARIANT_TYPE ("()"),
-                                              G_DBUS_CALL_FLAGS_NONE,
-                                              -1,
-                                              NULL,
-                                              &error);
-        if (error)
-            g_warning ("Error locking login1 session: %s", error->message);
-        g_clear_error (&error);
-        if (result)
-            g_variant_unref (result);
+        seat->priv->can_graphical = g_variant_get_boolean (value);
+        g_signal_emit (seat, seat_signals[CAN_GRAPHICAL_CHANGED], 0);
     }
-    g_object_unref (bus);
-}
-
-void
-login1_unlock_session (const gchar *session_path)
-{
-    GDBusConnection *bus;
-    GError *error = NULL;
-
-    g_return_if_fail (session_path != NULL);
-
-    g_debug ("Unlocking login1 session %s", session_path);
-
-    bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
-    if (error)
-        g_warning ("Failed to get system bus: %s", error->message);
-    g_clear_error (&error);
-    if (!bus)
-        return;
-
-    if (session_path)
+    else if (strcmp (name, "ActiveSession") == 0 && g_variant_is_of_type (value, G_VARIANT_TYPE ("(so)")))
     {
-        GVariant *result;
-
-        result = g_dbus_connection_call_sync (bus,
-                                              LOGIN1_SERVICE_NAME,
-                                              session_path,
-                                              "org.freedesktop.login1.Session",
-                                              "Unlock",
-                                              g_variant_new ("()"),
-                                              G_VARIANT_TYPE ("()"),
-                                              G_DBUS_CALL_FLAGS_NONE,
-                                              -1,
-                                              NULL,
-                                              &error);
-        if (error)
-            g_warning ("Error unlocking login1 session: %s", error->message);
-        g_clear_error (&error);
-        if (result)
-            g_variant_unref (result);
+        const gchar *login1_session_id;
+        g_variant_get (value, "(&so)", &login1_session_id, NULL);
+        g_signal_emit (seat, seat_signals[ACTIVE_SESSION_CHANGED], 0, login1_session_id);
     }
-    g_object_unref (bus);
 }
 
-void
-login1_activate_session (const gchar *session_path)
+static void
+seat_properties_changed_cb (GDBusConnection *connection,
+                            const gchar *sender_name,
+                            const gchar *object_path,
+                            const gchar *interface_name,
+                            const gchar *signal_name,
+                            GVariant *parameters,
+                            gpointer user_data)
 {
-    GDBusConnection *bus;
-    GError *error = NULL;
-
-    g_return_if_fail (session_path != NULL);
-
-    g_debug ("Activating login1 session %s", session_path);
-
-    bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
-    if (error)
-        g_warning ("Failed to get system bus: %s", error->message);
-    g_clear_error (&error);
-    if (!bus)
-        return;
-
-    if (session_path)
+    Login1Seat *seat = user_data;
+    GVariantIter *iter;
+    GVariantIter *invalidated_properties;
+    const gchar *name;
+    GVariant *value;
+
+    g_variant_get (parameters, "(sa{sv}as)", NULL, &iter, &invalidated_properties);
+    while (g_variant_iter_loop (iter, "{&sv}", &name, &value))
+        update_property (seat, name, value);
+    g_variant_iter_free (iter);
+    while (g_variant_iter_loop (invalidated_properties, "&s", &name))
     {
         GVariant *result;
+        GError *error = NULL;
 
-        result = g_dbus_connection_call_sync (bus,
+        result = g_dbus_connection_call_sync (connection,
                                               LOGIN1_SERVICE_NAME,
-                                              session_path,
-                                              "org.freedesktop.login1.Session",
-                                              "Activate",
-                                              g_variant_new ("()"),
-                                              G_VARIANT_TYPE ("()"),
+                                              seat->priv->path,
+                                              "org.freedesktop.DBus.Properties",
+                                              "Get",
+                                              g_variant_new ("(ss)", "org.freedesktop.login1.Seat", name),
+                                              G_VARIANT_TYPE ("(v)"),
                                               G_DBUS_CALL_FLAGS_NONE,
                                               -1,
                                               NULL,
                                               &error);
         if (error)
-            g_warning ("Error activating login1 session: %s", error->message);
+            g_warning ("Error updating seat property %s: %s", name, error->message);
         g_clear_error (&error);
         if (result)
+        {
+            g_variant_get (result, "(v)", &value);
+            update_property (seat, name, value);
+            g_variant_unref (value);
             g_variant_unref (result);
+        }
     }
-    g_object_unref (bus);
-}
-
-Login1Service *
-login1_service_get_instance (void)
-{
-    if (!singleton)
-        singleton = g_object_new (LOGIN1_SERVICE_TYPE, NULL);
-    return singleton;
+    g_variant_iter_free (invalidated_properties);
 }
 
 static Login1Seat *
@@ -238,11 +155,23 @@ add_seat (Login1Service *service, const gchar *id, const gchar *path)
     GError *error = NULL;
 
     seat = g_object_new (LOGIN1_SEAT_TYPE, NULL);
+    seat->priv->connection = g_object_ref (service->priv->connection);
     seat->priv->id = g_strdup (id);
     seat->priv->path = g_strdup (path);
 
+    seat->priv->signal_id = g_dbus_connection_signal_subscribe (seat->priv->connection,
+                                                                LOGIN1_SERVICE_NAME,
+                                                                "org.freedesktop.DBus.Properties",
+                                                                "PropertiesChanged",
+                                                                path,
+                                                                "org.freedesktop.login1.Seat",
+                                                                G_DBUS_SIGNAL_FLAGS_NONE,
+                                                                seat_properties_changed_cb,
+                                                                g_object_ref (seat),
+                                                                g_object_unref);
+
     /* Get properties for this seat */
-    result = g_dbus_connection_call_sync (service->priv->connection,
+    result = g_dbus_connection_call_sync (seat->priv->connection,
                                           LOGIN1_SERVICE_NAME,
                                           path,
                                           "org.freedesktop.DBus.Properties",
@@ -269,7 +198,6 @@ add_seat (Login1Service *service, const gchar *id, const gchar *path)
                 seat->priv->can_graphical = g_variant_get_boolean (value);
             else if (strcmp (name, "CanMultiSession") == 0 && g_variant_is_of_type (value, G_VARIANT_TYPE_BOOLEAN))
                 seat->priv->can_multi_session = g_variant_get_boolean (value);
-            g_variant_unref (value);
         }
         g_variant_iter_free (properties);
         g_variant_unref (result);
@@ -301,7 +229,7 @@ signal_cb (GDBusConnection *connection,
         if (!seat)
         {
             seat = add_seat (service, id, path);
-            g_signal_emit (service, signals[SEAT_ADDED], 0, seat);
+            g_signal_emit (service, service_signals[SEAT_ADDED], 0, seat);
         }
     }
     else if (strcmp (signal_name, "SeatRemoved") == 0)
@@ -313,11 +241,11 @@ signal_cb (GDBusConnection *connection,
         seat = login1_service_get_seat (service, id);
         if (seat)
         {
-            service->priv->seats = g_list_remove (service->priv->seats, seat);            
-            g_signal_emit (service, signals[SEAT_REMOVED], 0, seat);
+            service->priv->seats = g_list_remove (service->priv->seats, seat);
+            g_signal_emit (service, service_signals[SEAT_REMOVED], 0, seat);
             g_object_unref (seat);
-        }                        
-    } 
+        }
+    }
 }
 
 gboolean
@@ -342,19 +270,19 @@ login1_service_connect (Login1Service *service)
 
     service->priv->signal_id = g_dbus_connection_signal_subscribe (service->priv->connection,
                                                                    LOGIN1_SERVICE_NAME,
-                                                                   "org.freedesktop.login1.Manager",
+                                                                   LOGIN1_MANAGER_INTERFACE_NAME,
                                                                    NULL,
-                                                                   "/org/freedesktop/login1",
+                                                                   LOGIN1_OBJECT_NAME,
                                                                    NULL,
                                                                    G_DBUS_SIGNAL_FLAGS_NONE,
                                                                    signal_cb,
-                                                                   service,
-                                                                   NULL);
+                                                                   g_object_ref (service),
+                                                                   g_object_unref);
 
     result = g_dbus_connection_call_sync (service->priv->connection,
                                           LOGIN1_SERVICE_NAME,
-                                          "/org/freedesktop/login1",
-                                          "org.freedesktop.login1.Manager",
+                                          LOGIN1_OBJECT_NAME,
+                                          LOGIN1_MANAGER_INTERFACE_NAME,
                                           "ListSeats",
                                           g_variant_new ("()"),
                                           G_VARIANT_TYPE ("(a(so))"),
@@ -410,6 +338,105 @@ login1_service_get_seat (Login1Service *service, const gchar *id)
     return NULL;
 }
 
+void
+login1_service_lock_session (Login1Service *service, const gchar *session_id)
+{
+    GError *error = NULL;
+
+    g_return_if_fail (service != NULL);
+    g_return_if_fail (session_id != NULL);
+
+    g_debug ("Locking login1 session %s", session_id);
+
+    if (session_id)
+    {
+        GVariant *result;
+
+        result = g_dbus_connection_call_sync (service->priv->connection,
+                                              LOGIN1_SERVICE_NAME,
+                                              LOGIN1_OBJECT_NAME,
+                                              LOGIN1_MANAGER_INTERFACE_NAME,
+                                              "LockSession",
+                                              g_variant_new ("(s)", session_id),
+                                              G_VARIANT_TYPE ("()"),
+                                              G_DBUS_CALL_FLAGS_NONE,
+                                              -1,
+                                              NULL,
+                                              &error);
+        if (error)
+            g_warning ("Error locking login1 session: %s", error->message);
+        g_clear_error (&error);
+        if (result)
+            g_variant_unref (result);
+    }
+}
+
+void
+login1_service_unlock_session (Login1Service *service, const gchar *session_id)
+{
+    GError *error = NULL;
+
+    g_return_if_fail (service != NULL);
+    g_return_if_fail (session_id != NULL);
+
+    g_debug ("Unlocking login1 session %s", session_id);
+
+    if (session_id)
+    {
+        GVariant *result;
+
+        result = g_dbus_connection_call_sync (service->priv->connection,
+                                              LOGIN1_SERVICE_NAME,
+                                              LOGIN1_OBJECT_NAME,
+                                              LOGIN1_MANAGER_INTERFACE_NAME,
+                                              "UnlockSession",
+                                              g_variant_new ("(s)", session_id),
+                                              G_VARIANT_TYPE ("()"),
+                                              G_DBUS_CALL_FLAGS_NONE,
+                                              -1,
+                                              NULL,
+                                              &error);
+        if (error)
+            g_warning ("Error unlocking login1 session: %s", error->message);
+        g_clear_error (&error);
+        if (result)
+            g_variant_unref (result);
+    }
+}
+
+void
+login1_service_activate_session (Login1Service *service, const gchar *session_id)
+{
+    GError *error = NULL;
+
+    g_return_if_fail (service != NULL);
+    g_return_if_fail (session_id != NULL);
+
+    g_debug ("Activating login1 session %s", session_id);
+
+    if (session_id)
+    {
+        GVariant *result;
+
+        result = g_dbus_connection_call_sync (service->priv->connection,
+                                              LOGIN1_SERVICE_NAME,
+                                              LOGIN1_OBJECT_NAME,
+                                              LOGIN1_MANAGER_INTERFACE_NAME,
+                                              "ActivateSession",
+                                              g_variant_new ("(s)", session_id),
+                                              G_VARIANT_TYPE ("()"),
+                                              G_DBUS_CALL_FLAGS_NONE,
+                                              -1,
+                                              NULL,
+                                              &error);
+        if (error)
+            g_warning ("Error activating login1 session: %s", error->message);
+        g_clear_error (&error);
+        if (result)
+            g_variant_unref (result);
+    }
+}
+
 static void
 login1_service_init (Login1Service *service)
 {
@@ -423,7 +450,7 @@ login1_service_finalize (GObject *object)
 
     g_list_free_full (self->priv->seats, g_object_unref);
     g_dbus_connection_signal_unsubscribe (self->priv->connection, self->priv->signal_id);
-    g_object_unref (self->priv->connection);
+    g_clear_object (&self->priv->connection);
 
     G_OBJECT_CLASS (login1_service_parent_class)->finalize (object);
 }
@@ -437,16 +464,16 @@ login1_service_class_init (Login1ServiceClass *klass)
 
     g_type_class_add_private (klass, sizeof (Login1ServicePrivate));
 
-    signals[SEAT_ADDED] =
-        g_signal_new ("seat-added",
+    service_signals[SEAT_ADDED] =
+        g_signal_new (LOGIN1_SERVICE_SIGNAL_SEAT_ADDED,
                       G_TYPE_FROM_CLASS (klass),
                       G_SIGNAL_RUN_LAST,
                       G_STRUCT_OFFSET (Login1ServiceClass, seat_added),
                       NULL, NULL,
                       NULL,
                       G_TYPE_NONE, 1, LOGIN1_SEAT_TYPE);
-    signals[SEAT_REMOVED] =
-        g_signal_new ("seat-removed",
+    service_signals[SEAT_REMOVED] =
+        g_signal_new (LOGIN1_SERVICE_SIGNAL_SEAT_REMOVED,
                       G_TYPE_FROM_CLASS (klass),
                       G_SIGNAL_RUN_LAST,
                       G_STRUCT_OFFSET (Login1ServiceClass, seat_removed),
@@ -489,6 +516,8 @@ login1_seat_finalize (GObject *object)
 
     g_free (self->priv->id);
     g_free (self->priv->path);
+    g_dbus_connection_signal_unsubscribe (self->priv->connection, self->priv->signal_id);
+    g_object_unref (self->priv->connection);
 
     G_OBJECT_CLASS (login1_seat_parent_class)->finalize (object);
 }
@@ -501,4 +530,22 @@ login1_seat_class_init (Login1SeatClass *klass)
     object_class->finalize = login1_seat_finalize;
 
     g_type_class_add_private (klass, sizeof (Login1SeatPrivate));
+
+    seat_signals[CAN_GRAPHICAL_CHANGED] =
+        g_signal_new (LOGIN1_SEAT_SIGNAL_CAN_GRAPHICAL_CHANGED,
+                      G_TYPE_FROM_CLASS (klass),
+                      G_SIGNAL_RUN_LAST,
+                      G_STRUCT_OFFSET (Login1SeatClass, can_graphical_changed),
+                      NULL, NULL,
+                      NULL,
+                      G_TYPE_NONE, 0);
+
+    seat_signals[ACTIVE_SESSION_CHANGED] =
+        g_signal_new (LOGIN1_SIGNAL_ACTIVE_SESION_CHANGED,
+                      G_TYPE_FROM_CLASS (klass),
+                      G_SIGNAL_RUN_LAST,
+                      G_STRUCT_OFFSET (Login1SeatClass, active_session_changed),
+                      NULL, NULL,
+                      NULL,
+                      G_TYPE_NONE, 1, G_TYPE_STRING);
 }