From ee9f36a21ed661f52758a6d3badfdcac3716119c Mon Sep 17 00:00:00 2001 From: Robert Ancell Date: Wed, 20 Dec 2017 15:41:54 +1300 Subject: Refactor global variables out of DBus code --- tests/src/test-runner.c | 417 +++++++++++++++++++++++------------------------- 1 file changed, 199 insertions(+), 218 deletions(-) (limited to 'tests/src') diff --git a/tests/src/test-runner.c b/tests/src/test-runner.c index 4ad0bc13..7a0dd75e 100644 --- a/tests/src/test-runner.c +++ b/tests/src/test-runner.c @@ -42,8 +42,6 @@ static GHashTable *children = NULL; static gboolean stop = FALSE; static gint exit_status = 0; static GDBusConnection *accounts_connection = NULL; -static GDBusNodeInfo *accounts_info; -static GDBusNodeInfo *user_info; typedef struct { guint uid; @@ -61,27 +59,6 @@ typedef struct gboolean hidden; } AccountsUser; static GList *accounts_users = NULL; -static void handle_user_call (GDBusConnection *connection, - const gchar *sender, - const gchar *object_path, - const gchar *interface_name, - const gchar *method_name, - GVariant *parameters, - GDBusMethodInvocation *invocation, - gpointer user_data); -static GVariant *handle_user_get_property (GDBusConnection *connection, - const gchar *sender, - const gchar *object_path, - const gchar *interface_name, - const gchar *property_name, - GError **error, - gpointer user_data); -static const GDBusInterfaceVTable user_vtable = -{ - handle_user_call, - handle_user_get_property, -}; -static GDBusNodeInfo *ck_session_info; typedef struct { gchar *cookie; @@ -91,18 +68,6 @@ typedef struct } CKSession; static GList *ck_sessions = NULL; static gint ck_session_index = 0; -static void handle_ck_session_call (GDBusConnection *connection, - const gchar *sender, - const gchar *object_path, - const gchar *interface_name, - const gchar *method_name, - GVariant *parameters, - GDBusMethodInvocation *invocation, - gpointer user_data); -static const GDBusInterfaceVTable ck_session_vtable = -{ - handle_ck_session_call, -}; typedef struct { @@ -1291,6 +1256,47 @@ start_upower_daemon (void) NULL); } +static void +handle_ck_session_call (GDBusConnection *connection, + const gchar *sender, + const gchar *object_path, + const gchar *interface_name, + const gchar *method_name, + GVariant *parameters, + GDBusMethodInvocation *invocation, + gpointer user_data) +{ + CKSession *session = user_data; + + if (strcmp (method_name, "GetXDGRuntimeDir") == 0 && !g_key_file_get_boolean (config, "test-runner-config", "ck-no-xdg-runtime", NULL)) + { + g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", "/run/console-kit")); + } + else if (strcmp (method_name, "Lock") == 0) + { + if (!session->locked) + check_status ("CONSOLE-KIT LOCK-SESSION"); + session->locked = TRUE; + g_dbus_method_invocation_return_value (invocation, g_variant_new ("()")); + } + else if (strcmp (method_name, "Unlock") == 0) + { + if (session->locked) + check_status ("CONSOLE-KIT UNLOCK-SESSION"); + session->locked = FALSE; + g_dbus_method_invocation_return_value (invocation, g_variant_new ("()")); + } + else if (strcmp (method_name, "Activate") == 0) + { + g_autofree gchar *status = g_strdup_printf ("CONSOLE-KIT ACTIVATE-SESSION SESSION=%s", session->cookie); + check_status (status); + + g_dbus_method_invocation_return_value (invocation, g_variant_new ("()")); + } + else + g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such method: %s", method_name); +} + static CKSession * open_ck_session (GDBusConnection *connection, GVariant *params) { @@ -1300,6 +1306,30 @@ open_ck_session (GDBusConnection *connection, GVariant *params) const gchar *name; GVariant *value; g_autoptr(GError) error = NULL; + const gchar *ck_session_interface_old = + "" + " " + " " + " " + " " + " " + ""; + const gchar *ck_session_interface = + "" + " " + " " + " " + " " + " " + " " + " " + " " + ""; + g_autoptr(GDBusNodeInfo) ck_session_info = NULL; + static const GDBusInterfaceVTable ck_session_vtable = + { + handle_ck_session_call, + }; session = g_malloc0 (sizeof (CKSession)); ck_sessions = g_list_append (ck_sessions, session); @@ -1316,6 +1346,13 @@ open_ck_session (GDBusConnection *connection, GVariant *params) } } + if (g_key_file_get_boolean (config, "test-runner-config", "ck-no-xdg-runtime", NULL)) + ck_session_info = g_dbus_node_info_new_for_xml (ck_session_interface_old, &error); + else + ck_session_info = g_dbus_node_info_new_for_xml (ck_session_interface, &error); + if (!ck_session_info) + g_warning ("Failed to parse D-Bus interface: %s", error->message); + session->cookie = g_strdup (cookie->str); session->path = g_strdup_printf ("/org/freedesktop/ConsoleKit/Session%d", ck_session_index++); session->id = g_dbus_connection_register_object (connection, @@ -1418,47 +1455,6 @@ handle_ck_call (GDBusConnection *connection, g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such method: %s", method_name); } -static void -handle_ck_session_call (GDBusConnection *connection, - const gchar *sender, - const gchar *object_path, - const gchar *interface_name, - const gchar *method_name, - GVariant *parameters, - GDBusMethodInvocation *invocation, - gpointer user_data) -{ - CKSession *session = user_data; - - if (strcmp (method_name, "GetXDGRuntimeDir") == 0 && !g_key_file_get_boolean (config, "test-runner-config", "ck-no-xdg-runtime", NULL)) - { - g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", "/run/console-kit")); - } - else if (strcmp (method_name, "Lock") == 0) - { - if (!session->locked) - check_status ("CONSOLE-KIT LOCK-SESSION"); - session->locked = TRUE; - g_dbus_method_invocation_return_value (invocation, g_variant_new ("()")); - } - else if (strcmp (method_name, "Unlock") == 0) - { - if (session->locked) - check_status ("CONSOLE-KIT UNLOCK-SESSION"); - session->locked = FALSE; - g_dbus_method_invocation_return_value (invocation, g_variant_new ("()")); - } - else if (strcmp (method_name, "Activate") == 0) - { - g_autofree gchar *status = g_strdup_printf ("CONSOLE-KIT ACTIVATE-SESSION SESSION=%s", session->cookie); - check_status (status); - - g_dbus_method_invocation_return_value (invocation, g_variant_new ("()")); - } - else - g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such method: %s", method_name); -} - static void ck_name_acquired_cb (GDBusConnection *connection, const gchar *name, @@ -1514,25 +1510,6 @@ ck_name_acquired_cb (GDBusConnection *connection, { handle_ck_call, }; - const gchar *ck_session_interface_old = - "" - " " - " " - " " - " " - " " - ""; - const gchar *ck_session_interface = - "" - " " - " " - " " - " " - " " - " " - " " - " " - ""; g_autoptr(GDBusNodeInfo) ck_info = NULL; g_autoptr(GError) error = NULL; @@ -1542,15 +1519,6 @@ ck_name_acquired_cb (GDBusConnection *connection, g_warning ("Failed to parse D-Bus interface: %s", error->message); return; } - if (g_key_file_get_boolean (config, "test-runner-config", "ck-no-xdg-runtime", NULL)) - ck_session_info = g_dbus_node_info_new_for_xml (ck_session_interface_old, &error); - else - ck_session_info = g_dbus_node_info_new_for_xml (ck_session_interface, &error); - if (!ck_session_info) - { - g_warning ("Failed to parse D-Bus interface: %s", error->message); - return; - } if (g_dbus_connection_register_object (connection, "/org/freedesktop/ConsoleKit/Manager", ck_info->interfaces[0], @@ -2099,6 +2067,86 @@ get_accounts_user_by_name (const gchar *username) return NULL; } +static void +handle_user_call (GDBusConnection *connection, + const gchar *sender, + const gchar *object_path, + const gchar *interface_name, + const gchar *method_name, + GVariant *parameters, + GDBusMethodInvocation *invocation, + gpointer user_data) +{ + AccountsUser *user = user_data; + + if (strcmp (method_name, "SetXSession") == 0) + { + gchar *xsession; + + g_variant_get (parameters, "(&s)", &xsession); + + g_free (user->xsession); + user->xsession = g_strdup (xsession); + + g_dbus_method_invocation_return_value (invocation, g_variant_new ("()")); + + /* And notify others that it took */ + g_dbus_connection_emit_signal (accounts_connection, + NULL, + user->path, + "org.freedesktop.Accounts.User", + "Changed", + g_variant_new ("()"), + NULL); + } + else + g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such method: %s", method_name); +} + +static GVariant * +handle_user_get_property (GDBusConnection *connection, + const gchar *sender, + const gchar *object_path, + const gchar *interface_name, + const gchar *property_name, + GError **error, + gpointer user_data) +{ + AccountsUser *user = user_data; + + if (strcmp (property_name, "UserName") == 0) + return g_variant_new_string (user->user_name); + else if (strcmp (property_name, "RealName") == 0) + return g_variant_new_string (user->real_name); + else if (strcmp (property_name, "HomeDirectory") == 0) + return g_variant_new_string (user->home_directory); + else if (strcmp (property_name, "SystemAccount") == 0) + return g_variant_new_boolean (user->uid < 1000); + else if (strcmp (property_name, "BackgroundFile") == 0) + return g_variant_new_string (user->background ? user->background : ""); + else if (strcmp (property_name, "Language") == 0) + return g_variant_new_string (user->language ? user->language : ""); + else if (strcmp (property_name, "IconFile") == 0) + return g_variant_new_string (user->image ? user->image : ""); + else if (strcmp (property_name, "Shell") == 0) + return g_variant_new_string ("/bin/sh"); + else if (strcmp (property_name, "Uid") == 0) + return g_variant_new_uint64 (user->uid); + else if (strcmp (property_name, "XSession") == 0) + return g_variant_new_string (user->xsession ? user->xsession : ""); + else if (strcmp (property_name, "XKeyboardLayouts") == 0) + { + if (user->layouts != NULL) + return g_variant_new_strv ((const gchar * const *) user->layouts, -1); + else + return g_variant_new_strv (NULL, 0); + } + else if (strcmp (property_name, "XHasMessages") == 0) + return g_variant_new_boolean (user->has_messages); + + return NULL; +} + static void accounts_user_set_hidden (AccountsUser *user, gboolean hidden, gboolean emit_signal) { @@ -2122,18 +2170,55 @@ accounts_user_set_hidden (AccountsUser *user, gboolean hidden, gboolean emit_sig } if (!user->hidden && user->id == 0) { - g_autoptr(GError) register_error = NULL; - g_autoptr(GError) emit_error = NULL; + const gchar *user_interface = + "" + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + ""; + g_autoptr(GDBusNodeInfo) user_info = NULL; + g_autoptr(GError) error = NULL; + static const GDBusInterfaceVTable user_vtable = + { + handle_user_call, + handle_user_get_property, + }; + user_info = g_dbus_node_info_new_for_xml (user_interface, &error); + if (!user_info) + { + g_warning ("Failed to parse D-Bus interface: %s", error->message); + return; + } user->id = g_dbus_connection_register_object (accounts_connection, user->path, user_info->interfaces[0], &user_vtable, user, NULL, - ®ister_error); + &error); if (user->id == 0) - g_warning ("Failed to register user: %s", register_error->message); + { + g_warning ("Failed to register user: %s", error->message); + return; + } if (!g_dbus_connection_emit_signal (accounts_connection, NULL, @@ -2141,8 +2226,8 @@ accounts_user_set_hidden (AccountsUser *user, gboolean hidden, gboolean emit_sig "org.freedesktop.Accounts", "UserAdded", g_variant_new ("(o)", user->path), - &emit_error)) - g_warning ("Failed to emit UserAdded: %s", emit_error->message); + &error)) + g_warning ("Failed to emit UserAdded: %s", error->message); } } @@ -2281,86 +2366,6 @@ handle_accounts_call (GDBusConnection *connection, g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such method: %s", method_name); } -static void -handle_user_call (GDBusConnection *connection, - const gchar *sender, - const gchar *object_path, - const gchar *interface_name, - const gchar *method_name, - GVariant *parameters, - GDBusMethodInvocation *invocation, - gpointer user_data) -{ - AccountsUser *user = user_data; - - if (strcmp (method_name, "SetXSession") == 0) - { - gchar *xsession; - - g_variant_get (parameters, "(&s)", &xsession); - - g_free (user->xsession); - user->xsession = g_strdup (xsession); - - g_dbus_method_invocation_return_value (invocation, g_variant_new ("()")); - - /* And notify others that it took */ - g_dbus_connection_emit_signal (accounts_connection, - NULL, - user->path, - "org.freedesktop.Accounts.User", - "Changed", - g_variant_new ("()"), - NULL); - } - else - g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "No such method: %s", method_name); -} - -static GVariant * -handle_user_get_property (GDBusConnection *connection, - const gchar *sender, - const gchar *object_path, - const gchar *interface_name, - const gchar *property_name, - GError **error, - gpointer user_data) -{ - AccountsUser *user = user_data; - - if (strcmp (property_name, "UserName") == 0) - return g_variant_new_string (user->user_name); - else if (strcmp (property_name, "RealName") == 0) - return g_variant_new_string (user->real_name); - else if (strcmp (property_name, "HomeDirectory") == 0) - return g_variant_new_string (user->home_directory); - else if (strcmp (property_name, "SystemAccount") == 0) - return g_variant_new_boolean (user->uid < 1000); - else if (strcmp (property_name, "BackgroundFile") == 0) - return g_variant_new_string (user->background ? user->background : ""); - else if (strcmp (property_name, "Language") == 0) - return g_variant_new_string (user->language ? user->language : ""); - else if (strcmp (property_name, "IconFile") == 0) - return g_variant_new_string (user->image ? user->image : ""); - else if (strcmp (property_name, "Shell") == 0) - return g_variant_new_string ("/bin/sh"); - else if (strcmp (property_name, "Uid") == 0) - return g_variant_new_uint64 (user->uid); - else if (strcmp (property_name, "XSession") == 0) - return g_variant_new_string (user->xsession ? user->xsession : ""); - else if (strcmp (property_name, "XKeyboardLayouts") == 0) - { - if (user->layouts != NULL) - return g_variant_new_strv ((const gchar * const *) user->layouts, -1); - else - return g_variant_new_strv (NULL, 0); - } - else if (strcmp (property_name, "XHasMessages") == 0) - return g_variant_new_boolean (user->has_messages); - - return NULL; -} - static void accounts_name_acquired_cb (GDBusConnection *connection, const gchar *name, @@ -2384,31 +2389,11 @@ accounts_name_acquired_cb (GDBusConnection *connection, " " " " ""; + g_autoptr(GDBusNodeInfo) accounts_info = NULL; static const GDBusInterfaceVTable accounts_vtable = { handle_accounts_call, }; - const gchar *user_interface = - "" - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - ""; g_autoptr(GError) error = NULL; accounts_connection = connection; @@ -2419,12 +2404,6 @@ accounts_name_acquired_cb (GDBusConnection *connection, g_warning ("Failed to parse D-Bus interface: %s", error->message); return; } - user_info = g_dbus_node_info_new_for_xml (user_interface, &error); - if (!user_info) - { - g_warning ("Failed to parse D-Bus interface: %s", error->message); - return; - } if (g_dbus_connection_register_object (connection, "/org/freedesktop/Accounts", accounts_info->interfaces[0], @@ -2432,8 +2411,10 @@ accounts_name_acquired_cb (GDBusConnection *connection, NULL, NULL, &error) == 0) + { g_warning ("Failed to register accounts service: %s", error->message); - g_dbus_node_info_unref (accounts_info); + return; + } service_count--; if (service_count == 0) -- cgit v1.2.1