diff options
author | Richard Hughes <richard@hughsie.com> | 2020-01-09 12:59:26 +0000 |
---|---|---|
committer | Richard Hughes <richard@hughsie.com> | 2020-01-10 09:06:26 +0000 |
commit | c8b5314a50af80acb60ac66b565e9309ca08b72d (patch) | |
tree | ca235678ca15398adac9fefdb434bf66025b7deb | |
parent | 75aba6354b90d3b799d2ab7b7edca1c121505496 (diff) |
Do not do failable actions in the PkDbus constructor
-rw-r--r-- | src/pk-dbus.c | 61 | ||||
-rw-r--r-- | src/pk-dbus.h | 3 | ||||
-rw-r--r-- | src/pk-transaction.c | 9 |
3 files changed, 47 insertions, 26 deletions
diff --git a/src/pk-dbus.c b/src/pk-dbus.c index 37cc1d83c..660139781 100644 --- a/src/pk-dbus.c +++ b/src/pk-dbus.c @@ -282,8 +282,10 @@ pk_dbus_finalize (GObject *object) g_return_if_fail (PK_IS_DBUS (object)); dbus = PK_DBUS (object); - g_object_unref (dbus->priv->proxy_pid); - g_object_unref (dbus->priv->proxy_uid); + if (dbus->priv->proxy_pid != NULL) + g_object_unref (dbus->priv->proxy_pid); + if (dbus->priv->proxy_uid != NULL) + g_object_unref (dbus->priv->proxy_uid); if (dbus->priv->proxy_session != NULL) g_object_unref (dbus->priv->proxy_session); @@ -299,25 +301,18 @@ pk_dbus_class_init (PkDbusClass *klass) g_type_class_add_private (klass, sizeof (PkDbusPrivate)); } -/** - * pk_dbus_init: - * - * initializes the dbus class. NOTE: We expect dbus objects - * to *NOT* be removed or added during the session. - * We only control the first dbus object if there are more than one. - **/ -static void -pk_dbus_init (PkDbus *dbus) +gboolean +pk_dbus_connect (PkDbus *dbus, GError **error) { - g_autoptr(GError) error = NULL; - dbus->priv = PK_DBUS_GET_PRIVATE (dbus); + if (dbus->priv->connection != NULL) + return TRUE; /* use the bus to get the uid */ dbus->priv->connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, - NULL, &error); + NULL, error); if (dbus->priv->connection == NULL) { - g_warning ("cannot connect to the system bus: %s", error->message); - return; + g_prefix_error (error, "cannot connect to the system bus: "); + return FALSE; } /* connect to DBus so we can get the pid */ @@ -330,10 +325,10 @@ pk_dbus_init (PkDbus *dbus) "/org/freedesktop/DBus/Bus", "org.freedesktop.DBus", NULL, - &error); + error); if (dbus->priv->proxy_pid == NULL) { - g_warning ("cannot connect to DBus: %s", error->message); - return; + g_prefix_error (error, "cannot connect to DBus: "); + return FALSE; } /* connect to DBus so we can get the uid */ @@ -346,10 +341,10 @@ pk_dbus_init (PkDbus *dbus) "/org/freedesktop/DBus", "org.freedesktop.DBus", NULL, - &error); + error); if (dbus->priv->proxy_uid == NULL) { - g_warning ("cannot connect to DBus: %s", error->message); - return; + g_prefix_error (error, "cannot connect to DBus: "); + return FALSE; } /* use ConsoleKit to get the session */ @@ -362,11 +357,27 @@ pk_dbus_init (PkDbus *dbus) "/org/freedesktop/ConsoleKit/Manager", "org.freedesktop.ConsoleKit.Manager", NULL, - &error); + error); if (dbus->priv->proxy_session == NULL) { - g_warning ("cannot connect to DBus: %s", error->message); - return; + g_prefix_error (error, "cannot connect to DBus: "); + return FALSE; } + + /* success */ + return TRUE; +} + +/** + * pk_dbus_init: + * + * initializes the dbus class. NOTE: We expect dbus objects + * to *NOT* be removed or added during the session. + * We only control the first dbus object if there are more than one. + **/ +static void +pk_dbus_init (PkDbus *dbus) +{ + dbus->priv = PK_DBUS_GET_PRIVATE (dbus); } PkDbus * diff --git a/src/pk-dbus.h b/src/pk-dbus.h index b287b991e..5fe7b4d59 100644 --- a/src/pk-dbus.h +++ b/src/pk-dbus.h @@ -52,7 +52,8 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC(PkDbus, g_object_unref) GType pk_dbus_get_type (void); PkDbus *pk_dbus_new (void); - +gboolean pk_dbus_connect (PkDbus *dbus, + GError **error); guint pk_dbus_get_uid (PkDbus *dbus, const gchar *sender); gchar *pk_dbus_get_cmdline (PkDbus *dbus, diff --git a/src/pk-transaction.c b/src/pk-transaction.c index 83ee5588c..73148406f 100644 --- a/src/pk-transaction.c +++ b/src/pk-transaction.c @@ -1548,6 +1548,8 @@ pk_transaction_set_session_state (PkTransaction *transaction, PkTransactionPrivate *priv = transaction->priv; /* get session */ + if (!pk_dbus_connect (priv->dbus, error)) + return FALSE; session = pk_dbus_get_session (priv->dbus, priv->sender); if (session == NULL) { g_set_error_literal (error, 1, 0, "failed to get the session"); @@ -1997,6 +1999,7 @@ gboolean pk_transaction_set_sender (PkTransaction *transaction, const gchar *sender) { PkTransactionPrivate *priv = transaction->priv; + g_autoptr(GError) error = NULL; g_return_val_if_fail (PK_IS_TRANSACTION (transaction), FALSE); g_return_val_if_fail (sender != NULL, FALSE); @@ -2016,6 +2019,10 @@ pk_transaction_set_sender (PkTransaction *transaction, const gchar *sender) /* we get the UID for all callers as we need to know when to cancel */ priv->subject = polkit_system_bus_name_new (sender); + if (!pk_dbus_connect (priv->dbus, &error)) { + g_warning ("cannot get UID: %s", error->message); + return FALSE; + } priv->uid = pk_dbus_get_uid (priv->dbus, sender); /* only get when it's going to be saved into the database */ @@ -2690,6 +2697,8 @@ pk_transaction_cancel (PkTransaction *transaction, } /* get the UID of the caller */ + if (!pk_dbus_connect (transaction->priv->dbus, &error)) + goto out; uid = pk_dbus_get_uid (transaction->priv->dbus, sender); if (uid == PK_TRANSACTION_UID_INVALID) { g_set_error (&error, |