summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Koegel <eric.koegel@gmail.com>2015-08-09 10:43:52 +0300
committerEric Koegel <eric.koegel@gmail.com>2015-08-09 10:46:18 +0300
commitbee63889f4a0b8f57807a61d8396346cec8cd5b1 (patch)
tree1fe1a9471a4217eec28ecd5ee995c17d361c3160
parent5d4296bbf60e56a7d5891f107158106b66b8ca63 (diff)
downloadConsoleKit2-bee63889f4a0b8f57807a61d8396346cec8cd5b1.tar.gz
Minor code refactoring
This removes and cleans up some duplicated code. Both the ck-manager and the ck-session had an implementation to get the calling uid/pid so it's now moved to a common location. The inhibit changed signal has been made more explicit in when the delay is removed. The polkit permissions was duplicated over two functions and has been moved to a single common one. So this patch is just minor house keeping.
-rw-r--r--src/ck-manager.c160
-rw-r--r--src/ck-session.c56
-rw-r--r--src/ck-sysdeps-unix.c59
-rw-r--r--src/ck-sysdeps.h6
-rw-r--r--tools/Makefile.am3
5 files changed, 121 insertions, 163 deletions
diff --git a/src/ck-manager.c b/src/ck-manager.c
index 66da8b2..db5f906 100644
--- a/src/ck-manager.c
+++ b/src/ck-manager.c
@@ -960,13 +960,11 @@ logind_ready_cb (PolkitAuthority *authority,
g_object_unref (ret);
}
-/* We use this to avoid breaking API compability with ConsoleKit1 for
- * CanStop and CanRestart, but this method emulates how logind
- * presents it's API */
static void
-get_polkit_logind_permissions (CkManager *manager,
- const char *action,
- GDBusMethodInvocation *context)
+get_polkit_permissions_for_callback (CkManager *manager,
+ const char *action,
+ GDBusMethodInvocation *context,
+ GAsyncReadyCallback callback)
{
const char *sender;
PolkitSubject *subject;
@@ -982,96 +980,37 @@ get_polkit_logind_permissions (CkManager *manager,
NULL,
0,
NULL,
- (GAsyncReadyCallback) logind_ready_cb,
+ callback,
context);
g_object_unref (subject);
}
+/* We use this to avoid breaking API compability with ConsoleKit1 for
+ * CanStop and CanRestart, but this method emulates how logind
+ * presents it's API */
+static void
+get_polkit_logind_permissions (CkManager *manager,
+ const char *action,
+ GDBusMethodInvocation *context)
+{
+ get_polkit_permissions_for_callback (manager,
+ action,
+ context,
+ (GAsyncReadyCallback) logind_ready_cb);
+}
+
static void
get_polkit_permissions (CkManager *manager,
const char *action,
GDBusMethodInvocation *context)
{
- const char *sender;
- PolkitSubject *subject;
-
- g_debug ("get permissions for action %s", action);
-
- sender = g_dbus_method_invocation_get_sender (context);
- subject = polkit_system_bus_name_new (sender);
-
- polkit_authority_check_authorization (manager->priv->pol_ctx,
- subject,
- action,
- NULL,
- 0,
- NULL,
- (GAsyncReadyCallback) ready_cb,
- context);
- g_object_unref (subject);
+ get_polkit_permissions_for_callback (manager,
+ action,
+ context,
+ (GAsyncReadyCallback) ready_cb);
}
#endif
-/* adapted from PolicyKit */
-static gboolean
-get_caller_info (CkManager *manager,
- const char *sender,
- uid_t *calling_uid,
- pid_t *calling_pid)
-{
- gboolean res = FALSE;
- GVariant *value = NULL;
- GError *error = NULL;
-
- if (sender == NULL) {
- g_debug ("sender == NULL");
- goto out;
- }
-
- if (manager->priv->bus_proxy == NULL) {
- g_debug ("manager->priv->bus_proxy == NULL");
- goto out;
- }
-
- value = g_dbus_proxy_call_sync (manager->priv->bus_proxy, "GetConnectionUnixUser",
- g_variant_new ("(s)", sender),
- G_DBUS_CALL_FLAGS_NONE,
- 2000,
- NULL,
- &error);
-
- if (value == NULL) {
- g_warning ("GetConnectionUnixUser() failed: %s", error->message);
- g_error_free (error);
- goto out;
- }
- g_variant_get (value, "(u)", calling_uid);
- g_variant_unref (value);
-
- value = g_dbus_proxy_call_sync (manager->priv->bus_proxy, "GetConnectionUnixProcessID",
- g_variant_new ("(s)", sender),
- G_DBUS_CALL_FLAGS_NONE,
- 2000,
- NULL,
- &error);
-
- if (value == NULL) {
- g_warning ("GetConnectionUnixProcessID() failed: %s", error->message);
- g_error_free (error);
- goto out;
- }
- g_variant_get (value, "(u)", calling_pid);
- g_variant_unref (value);
-
- res = TRUE;
-
- g_debug ("uid = %d", *calling_uid);
- g_debug ("pid = %d", *calling_pid);
-
-out:
- return res;
-}
-
static char *
get_user_name (uid_t uid)
{
@@ -1179,7 +1118,7 @@ check_rbac_permissions (CkManager *manager,
username = NULL;
sender = g_dbus_method_invocation_get_sender (context);
- res = get_caller_info (manager,
+ res = get_caller_info (manager->priv->bus_proxy,
sender,
&uid,
&pid);
@@ -2116,7 +2055,7 @@ dbus_inhibit (ConsoleKitManager *ckmanager,
priv = CK_MANAGER_GET_PRIVATE (CK_MANAGER (ckmanager));
sender = g_dbus_method_invocation_get_sender (context);
- res = get_caller_info (CK_MANAGER (ckmanager),
+ res = get_caller_info (priv->bus_proxy,
sender,
&uid,
&pid);
@@ -2759,7 +2698,7 @@ create_session_for_sender (CkManager *manager,
g_debug ("CkManager: create session for sender: %s", sender);
- res = get_caller_info (manager,
+ res = get_caller_info (manager->priv->bus_proxy,
sender,
&uid,
&pid);
@@ -2828,7 +2767,7 @@ dbus_get_session_for_cookie (ConsoleKitManager *ckmanager,
sender = g_dbus_method_invocation_get_sender (context);
- res = get_caller_info (manager,
+ res = get_caller_info (manager->priv->bus_proxy,
sender,
&calling_uid,
&calling_pid);
@@ -2913,7 +2852,7 @@ dbus_get_session_for_unix_process (ConsoleKitManager *ckmanager,
TRACE ();
g_debug ("pid: %u", pid);
- res = get_caller_info (manager,
+ res = get_caller_info (manager->priv->bus_proxy,
sender,
&calling_uid,
&calling_pid);
@@ -2961,7 +2900,7 @@ dbus_get_current_session (ConsoleKitManager *ckmanager,
g_debug ("CkManager: get current session");
- res = get_caller_info (manager,
+ res = get_caller_info (manager->priv->bus_proxy,
sender,
&calling_uid,
&calling_pid);
@@ -3161,7 +3100,7 @@ dbus_close_session (ConsoleKitManager *ckmanager,
manager = CK_MANAGER (ckmanager);
sender = g_dbus_method_invocation_get_sender (context);
- res = get_caller_info (manager,
+ res = get_caller_info (manager->priv->bus_proxy,
sender,
&calling_uid,
&calling_pid);
@@ -3504,6 +3443,17 @@ create_seats (CkManager *manager)
}
static void
+cancel_timeout_and_call_system_action (CkManagerPrivate *priv)
+{
+ /* The inhibit lock for this action was removed.
+ * Stop the timeout and call the system action now.
+ */
+ g_source_remove (priv->system_action_idle_id);
+ priv->system_action_idle_id = 0;
+ system_action_idle_cb (priv->system_action_data);
+}
+
+static void
on_inhibit_manager_changed_event (CkInhibitManager *manager, gint inhibit_mode, gint event, gboolean enabled, gpointer user_data)
{
CkManagerPrivate *priv;
@@ -3522,17 +3472,6 @@ on_inhibit_manager_changed_event (CkInhibitManager *manager, gint inhibit_mode,
return;
}
- /* this system action must be for a sleep or shutdown operation */
- if (priv->system_action_data->signal != PREPARE_FOR_SLEEP &&
- priv->system_action_data->signal != PREPARE_FOR_SHUTDOWN) {
- return;
- }
-
- /* the inhibit change must be for sleep or shutdown */
- if (event != CK_INHIBIT_EVENT_SUSPEND && event != CK_INHIBIT_EVENT_SHUTDOWN) {
- return;
- }
-
/* must be a delay inhibitor */
if (inhibit_mode != CK_INHIBIT_MODE_DELAY) {
return;
@@ -3543,12 +3482,17 @@ on_inhibit_manager_changed_event (CkInhibitManager *manager, gint inhibit_mode,
return;
}
- /* The inhibit lock for this action was removed.
- * Stop the timeout and call the system action now.
- */
- g_source_remove (priv->system_action_idle_id);
- priv->system_action_idle_id = 0;
- system_action_idle_cb (priv->system_action_data);
+ /* Did we stop inhibiting sleep? */
+ if (priv->system_action_data->signal == PREPARE_FOR_SLEEP &&
+ event == CK_INHIBIT_EVENT_SUSPEND) {
+ cancel_timeout_and_call_system_action (priv);
+ }
+
+ /* Did we stop inhibiting shutdown? */
+ if (priv->system_action_data->signal == PREPARE_FOR_SHUTDOWN &&
+ event == CK_INHIBIT_EVENT_SHUTDOWN) {
+ cancel_timeout_and_call_system_action (priv);
+ }
}
static void
diff --git a/src/ck-session.c b/src/ck-session.c
index ca0f8ca..478dce2 100644
--- a/src/ck-session.c
+++ b/src/ck-session.c
@@ -248,60 +248,6 @@ dbus_unlock (ConsoleKitSession *cksession,
return TRUE;
}
-/* adapted from PolicyKit */
-static gboolean
-get_caller_info (CkSession *session,
- const char *sender,
- uid_t *calling_uid,
- pid_t *calling_pid)
-{
- gboolean res = FALSE;
- GVariant *value = NULL;
- GError *error = NULL;
-
- if (sender == NULL) {
- goto out;
- }
-
- value = g_dbus_proxy_call_sync (session->priv->bus_proxy, "GetConnectionUnixUser",
- g_variant_new ("(s)", sender),
- G_DBUS_CALL_FLAGS_NONE,
- 2000,
- NULL,
- &error);
-
- if (value == NULL) {
- g_warning ("GetConnectionUnixUser() failed: %s", error->message);
- g_error_free (error);
- goto out;
- }
- g_variant_get (value, "(u)", calling_uid);
- g_variant_unref (value);
-
- value = g_dbus_proxy_call_sync (session->priv->bus_proxy, "GetConnectionUnixProcessID",
- g_variant_new ("(s)", sender),
- G_DBUS_CALL_FLAGS_NONE,
- 2000,
- NULL,
- &error);
-
- if (value == NULL) {
- g_warning ("GetConnectionUnixProcessID() failed: %s", error->message);
- g_error_free (error);
- goto out;
- }
- g_variant_get (value, "(u)", calling_pid);
- g_variant_unref (value);
-
- res = TRUE;
-
- g_debug ("uid = %d", *calling_uid);
- g_debug ("pid = %d", *calling_pid);
-
-out:
- return res;
-}
-
static gboolean
session_set_idle_hint_internal (CkSession *session,
gboolean idle_hint)
@@ -380,7 +326,7 @@ dbus_set_idle_hint (ConsoleKitSession *cksession,
sender = g_dbus_method_invocation_get_sender (context);
- res = get_caller_info (session,
+ res = get_caller_info (session->priv->bus_proxy,
sender,
&calling_uid,
&calling_pid);
diff --git a/src/ck-sysdeps-unix.c b/src/ck-sysdeps-unix.c
index 0f751b9..87333d1 100644
--- a/src/ck-sysdeps-unix.c
+++ b/src/ck-sysdeps-unix.c
@@ -132,6 +132,65 @@ ck_get_socket_peer_credentials (int socket_fd,
return ret;
}
+/* adapted from PolicyKit */
+gboolean
+get_caller_info (GDBusProxy *bus_proxy,
+ const char *sender,
+ uid_t *calling_uid,
+ pid_t *calling_pid)
+{
+ gboolean res = FALSE;
+ GVariant *value = NULL;
+ GError *error = NULL;
+
+ if (sender == NULL) {
+ g_debug ("sender == NULL");
+ goto out;
+ }
+
+ if (bus_proxy == NULL) {
+ g_debug ("bus_proxy == NULL");
+ goto out;
+ }
+
+ value = g_dbus_proxy_call_sync (bus_proxy, "GetConnectionUnixUser",
+ g_variant_new ("(s)", sender),
+ G_DBUS_CALL_FLAGS_NONE,
+ 2000,
+ NULL,
+ &error);
+
+ if (value == NULL) {
+ g_warning ("GetConnectionUnixUser() failed: %s", error->message);
+ g_error_free (error);
+ goto out;
+ }
+ g_variant_get (value, "(u)", calling_uid);
+ g_variant_unref (value);
+
+ value = g_dbus_proxy_call_sync (bus_proxy, "GetConnectionUnixProcessID",
+ g_variant_new ("(s)", sender),
+ G_DBUS_CALL_FLAGS_NONE,
+ 2000,
+ NULL,
+ &error);
+
+ if (value == NULL) {
+ g_warning ("GetConnectionUnixProcessID() failed: %s", error->message);
+ g_error_free (error);
+ goto out;
+ }
+ g_variant_get (value, "(u)", calling_pid);
+ g_variant_unref (value);
+
+ res = TRUE;
+
+ g_debug ("uid = %d", *calling_uid);
+ g_debug ("pid = %d", *calling_pid);
+
+out:
+ return res;
+}
/*
* getfd.c
diff --git a/src/ck-sysdeps.h b/src/ck-sysdeps.h
index bc32631..66df581 100644
--- a/src/ck-sysdeps.h
+++ b/src/ck-sysdeps.h
@@ -24,6 +24,7 @@
#include "config.h"
#include <glib.h>
+#include <gio/gio.h>
G_BEGIN_DECLS
@@ -54,6 +55,11 @@ gboolean ck_get_socket_peer_credentials (int socket_fd,
uid_t *uid,
GError **error);
+gboolean get_caller_info (GDBusProxy *bus_proxy,
+ const char *sender,
+ uid_t *calling_uid,
+ pid_t *calling_pid);
+
int ck_get_a_console_fd (void);
gboolean ck_fd_is_a_console (int fd);
diff --git a/tools/Makefile.am b/tools/Makefile.am
index 81f4d1f..69dd1eb 100644
--- a/tools/Makefile.am
+++ b/tools/Makefile.am
@@ -131,6 +131,7 @@ ck_collect_session_info_SOURCES = \
$(NULL)
ck_collect_session_info_LDADD = \
+ $(CONSOLE_KIT_LIBS) \
$(TOOLS_LIBS) \
$(top_builddir)/src/libck.la \
$(NULL)
@@ -140,6 +141,7 @@ ck_get_x11_server_pid_SOURCES = \
$(NULL)
ck_get_x11_server_pid_LDADD = \
+ $(CONSOLE_KIT_LIBS) \
$(TOOLS_LIBS) \
$(XLIB_LIBS) \
$(top_builddir)/src/libck.la \
@@ -150,6 +152,7 @@ ck_get_x11_display_device_SOURCES = \
$(NULL)
ck_get_x11_display_device_LDADD = \
+ $(CONSOLE_KIT_LIBS) \
$(TOOLS_LIBS) \
$(top_builddir)/src/libck.la \
$(NULL)