diff options
author | Matthias Clasen <mclasen@redhat.com> | 2009-08-13 11:48:11 +0200 |
---|---|---|
committer | Carlos Garcia Campos <carlosgc@gnome.org> | 2009-08-13 11:48:11 +0200 |
commit | b9ca2c4e5d94015c1882fd7b834da80c615d3b43 (patch) | |
tree | a690ee13415ea00f9fdab19d93dc54fdcf95cc0b /cpufreq | |
parent | 574b6bc734964b56529bcf9324588e79d1cbcc75 (diff) |
cpufreq: port to PolicyKit 1.0
Fixes bgo#585307.
Diffstat (limited to 'cpufreq')
-rw-r--r-- | cpufreq/src/Makefile.am | 8 | ||||
-rw-r--r-- | cpufreq/src/cpufreq-selector.c | 118 | ||||
-rw-r--r-- | cpufreq/src/cpufreq-selector/Makefile.am | 2 | ||||
-rw-r--r-- | cpufreq/src/cpufreq-selector/cpufreq-selector-service.c | 187 | ||||
-rw-r--r-- | cpufreq/src/cpufreq-selector/cpufreq-selector-service.h | 2 | ||||
-rw-r--r-- | cpufreq/src/cpufreq-selector/cpufreq-selector-service.xml | 7 | ||||
-rw-r--r-- | cpufreq/src/cpufreq-selector/org.gnome.cpufreqselector.policy.in | 2 | ||||
-rw-r--r-- | cpufreq/src/cpufreq-utils.c | 122 |
8 files changed, 127 insertions, 321 deletions
diff --git a/cpufreq/src/Makefile.am b/cpufreq/src/Makefile.am index c254a2aa1..9b82538e5 100644 --- a/cpufreq/src/Makefile.am +++ b/cpufreq/src/Makefile.am @@ -9,10 +9,6 @@ INCLUDES = \ $(GNOME_LIBS2_CFLAGS) \ $(LIBGLADE_CFLAGS) -if HAVE_POLKIT_GNOME -INCLUDES += $(POLKIT_GNOME_CFLAGS) -endif - libexec_PROGRAMS = cpufreq-applet if HAVE_LIBCPUFREQ @@ -38,8 +34,4 @@ cpufreq_applet_LDADD = \ $(LIBGLADE_LIBS) \ $(LIBCPUFREQ_LIBS) -if HAVE_POLKIT_GNOME -cpufreq_applet_LDADD += $(POLKIT_GNOME_LIBS) -endif - diff --git a/cpufreq/src/cpufreq-selector.c b/cpufreq/src/cpufreq-selector.c index 48f7fc736..45d0c17b8 100644 --- a/cpufreq/src/cpufreq-selector.c +++ b/cpufreq/src/cpufreq-selector.c @@ -19,19 +19,18 @@ #include <config.h> -#ifdef HAVE_POLKIT_GNOME +#ifdef HAVE_POLKIT #include <dbus/dbus-glib.h> -#endif /* HAVE_POLKIT_GNOME */ +#endif /* HAVE_POLKIT */ #include "cpufreq-selector.h" struct _CPUFreqSelector { GObject parent; -#ifdef HAVE_POLKIT_GNOME +#ifdef HAVE_POLKIT DBusGConnection *system_bus; - DBusGConnection *session_bus; -#endif /* HAVE_POLKIT_GNOME */ +#endif /* HAVE_POLKIT */ }; struct _CPUFreqSelectorClass { @@ -45,10 +44,9 @@ cpufreq_selector_finalize (GObject *object) { CPUFreqSelector *selector = CPUFREQ_SELECTOR (object); -#ifdef HAVE_POLKIT_GNOME +#ifdef HAVE_POLKIT selector->system_bus = NULL; - selector->session_bus = NULL; -#endif /* HAVE_POLKIT_GNOME */ +#endif /* HAVE_POLKIT */ G_OBJECT_CLASS (cpufreq_selector_parent_class)->finalize (object); } @@ -77,7 +75,7 @@ cpufreq_selector_get_default (void) return selector; } -#ifdef HAVE_POLKIT_GNOME +#ifdef HAVE_POLKIT typedef enum { FREQUENCY, GOVERNOR @@ -114,90 +112,12 @@ cpufreq_selector_connect_to_system_bus (CPUFreqSelector *selector, { if (selector->system_bus) return TRUE; - + selector->system_bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, error); return (selector->system_bus != NULL); } -static gboolean -cpufreq_selector_connect_to_session_bus (CPUFreqSelector *selector, - GError **error) -{ - if (selector->session_bus) - return TRUE; - - selector->session_bus = dbus_g_bus_get (DBUS_BUS_SESSION, error); - - return (selector->session_bus != NULL); -} - -static void -dbus_auth_call_notify_cb (DBusGProxy *proxy, - DBusGProxyCall *call, - gpointer user_data) -{ - SelectorAsyncData *data; - gboolean gained_privilege; - GError *error = NULL; - - data = (SelectorAsyncData *)user_data; - - if (!dbus_g_proxy_end_call (proxy, call, &error, G_TYPE_BOOLEAN, &gained_privilege, G_TYPE_INVALID)) { - g_warning ("%s", error->message); - g_error_free (error); - - selector_async_data_free (data); - - return; - } - - if (gained_privilege) { - switch (data->call) { - case FREQUENCY: - selector_set_frequency_async (data); - break; - case GOVERNOR: - selector_set_governor_async (data); - break; - default: - g_assert_not_reached (); - } - } else { - selector_async_data_free (data); - } -} - -static void -do_auth_async (SelectorAsyncData *data) -{ - DBusGProxy *proxy; - GError *error = NULL; - - if (!cpufreq_selector_connect_to_session_bus (data->selector, &error)) { - g_warning ("%s", error->message); - g_error_free (error); - - selector_async_data_free (data); - - return; - } - - proxy = dbus_g_proxy_new_for_name (data->selector->session_bus, - "org.gnome.PolicyKit", - "/org/gnome/PolicyKit/Manager", - "org.gnome.PolicyKit.Manager"); - - dbus_g_proxy_begin_call_with_timeout (proxy, - "ShowDialog", - dbus_auth_call_notify_cb, - data, NULL, - INT_MAX, - G_TYPE_STRING, "org.gnome.cpufreqselector", - G_TYPE_UINT, data->parent_xid, - G_TYPE_INVALID); -} - static void dbus_set_call_notify_cb (DBusGProxy *proxy, DBusGProxyCall *call, @@ -213,12 +133,6 @@ dbus_set_call_notify_cb (DBusGProxy *proxy, return; } - if (error->domain == DBUS_GERROR && DBUS_GERROR_REMOTE_EXCEPTION && - dbus_g_error_has_name (error, "org.gnome.CPUFreqSelector.NotAuthorized")) { - do_auth_async (data); - return; - } - selector_async_data_free (data); g_warning ("%s", error->message); g_error_free (error); @@ -229,13 +143,13 @@ selector_set_frequency_async (SelectorAsyncData *data) { DBusGProxy *proxy; GError *error = NULL; - + if (!cpufreq_selector_connect_to_system_bus (data->selector, &error)) { g_warning ("%s", error->message); g_error_free (error); selector_async_data_free (data); - + return; } @@ -243,7 +157,7 @@ selector_set_frequency_async (SelectorAsyncData *data) "org.gnome.CPUFreqSelector", "/org/gnome/cpufreq_selector/selector", "org.gnome.CPUFreqSelector"); - + dbus_g_proxy_begin_call_with_timeout (proxy, "SetFrequency", dbus_set_call_notify_cb, data, NULL, @@ -278,13 +192,13 @@ selector_set_governor_async (SelectorAsyncData *data) { DBusGProxy *proxy; GError *error = NULL; - + if (!cpufreq_selector_connect_to_system_bus (data->selector, &error)) { g_warning ("%s", error->message); g_error_free (error); selector_async_data_free (data); - + return; } @@ -292,7 +206,7 @@ selector_set_governor_async (SelectorAsyncData *data) "org.gnome.CPUFreqSelector", "/org/gnome/cpufreq_selector/selector", "org.gnome.CPUFreqSelector"); - + dbus_g_proxy_begin_call_with_timeout (proxy, "SetGovernor", dbus_set_call_notify_cb, data, NULL, @@ -321,7 +235,7 @@ cpufreq_selector_set_governor_async (CPUFreqSelector *selector, selector_set_governor_async (data); } -#else /* !HAVE_POLKIT_GNOME */ +#else /* !HAVE_POLKIT */ static void cpufreq_selector_run_command (CPUFreqSelector *selector, const gchar *args) @@ -372,4 +286,4 @@ cpufreq_selector_set_governor_async (CPUFreqSelector *selector, cpufreq_selector_run_command (selector, args); g_free (args); } -#endif /* HAVE_POLKIT_GNOME */ +#endif /* HAVE_POLKIT */ diff --git a/cpufreq/src/cpufreq-selector/Makefile.am b/cpufreq/src/cpufreq-selector/Makefile.am index a6c6ab50e..dc9cf739e 100644 --- a/cpufreq/src/cpufreq-selector/Makefile.am +++ b/cpufreq/src/cpufreq-selector/Makefile.am @@ -51,7 +51,7 @@ polkit_in_files = org.gnome.cpufreqselector.policy.in dbus_servicesdir = $(datadir)/dbus-1/system-services dbus_confdir = $(sysconfdir)/dbus-1/system.d -polkitdir = $(datadir)/PolicyKit/policy +polkitdir = $(datadir)/polkit-1/actions if HAVE_POLKIT BUILT_SOURCES = cpufreq-selector-service-glue.h diff --git a/cpufreq/src/cpufreq-selector/cpufreq-selector-service.c b/cpufreq/src/cpufreq-selector/cpufreq-selector-service.c index 3308f6769..381a9fd23 100644 --- a/cpufreq/src/cpufreq-selector/cpufreq-selector-service.c +++ b/cpufreq/src/cpufreq-selector/cpufreq-selector-service.c @@ -17,7 +17,7 @@ * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -#include <polkit-dbus/polkit-dbus.h> +#include <polkit/polkit.h> #include <dbus/dbus-glib-lowlevel.h> #include "cpufreq-selector.h" @@ -37,7 +37,7 @@ struct _CPUFreqSelectorService { DBusGConnection *system_bus; /* PolicyKit */ - PolKitContext *pk_context; + PolkitAuthority *authority; }; struct _CPUFreqSelectorServiceClass { @@ -99,9 +99,9 @@ cpufreq_selector_service_finalize (GObject *object) service->selectors_max = -1; } - if (service->pk_context) { - polkit_context_unref (service->pk_context); - service->pk_context = NULL; + if (service->authority) { + g_object_unref (service->authority); + service->authority = NULL; } G_OBJECT_CLASS (cpufreq_selector_service_parent_class)->finalize (object); @@ -153,42 +153,6 @@ reset_killtimer (void) NULL); } -static gboolean -pk_io_watch_have_data (GIOChannel *channel, - GIOCondition condition, - PolKitContext *pk_context) -{ - polkit_context_io_func (pk_context, - g_io_channel_unix_get_fd (channel)); - return TRUE; -} - -static int -pk_add_io_watch (PolKitContext *pk_context, - int watch_id) -{ - GIOChannel *channel; - guint id = 0; - - channel = g_io_channel_unix_new (watch_id); - if (!channel) - return 0; - - id = g_io_add_watch (channel, G_IO_IN, - (GIOFunc)pk_io_watch_have_data, - pk_context); - g_io_channel_unref (channel); - - return id; -} - -static void -pk_remove_io_watch (PolKitContext *pk_context, - int watch_id) -{ - g_source_remove (watch_id); -} - gboolean cpufreq_selector_service_register (CPUFreqSelectorService *service, GError **error) @@ -198,7 +162,6 @@ cpufreq_selector_service_register (CPUFreqSelectorService *service, gboolean res; guint result; GError *err = NULL; - PolKitError *pk_err = NULL; if (service->system_bus) { g_set_error (error, @@ -268,30 +231,7 @@ cpufreq_selector_service_register (CPUFreqSelectorService *service, return FALSE; } - service->pk_context = polkit_context_new (); - polkit_context_set_io_watch_functions (service->pk_context, - pk_add_io_watch, - pk_remove_io_watch); - if (!polkit_context_init (service->pk_context, &pk_err)) { - polkit_context_unref (service->pk_context); - service->pk_context = NULL; - - if (polkit_error_is_set (pk_err)) { - g_set_error (error, - CPUFREQ_SELECTOR_SERVICE_ERROR, - SERVICE_ERROR_GENERAL, - "Cannot initialize libpolkit: %s", - polkit_error_get_error_message (pk_err)); - polkit_error_free (pk_err); - } else { - g_set_error (error, - CPUFREQ_SELECTOR_SERVICE_ERROR, - SERVICE_ERROR_GENERAL, - "Cannot initialize libpolkit"); - } - - return FALSE; - } + service->authority = polkit_authority_get (); service->system_bus = connection; @@ -330,62 +270,41 @@ cpufreq_selector_service_check_policy (CPUFreqSelectorService *service, DBusGMethodInvocation *context, GError **error) { - PolKitAction *pk_action; - PolKitCaller *pk_caller; - PolKitResult pk_result; - gchar *sender; - DBusConnection *connection; - DBusError dbus_error; - PolKitError *pk_error = NULL; + PolkitSubject *subject; + PolkitAuthorizationResult *result; + gchar *sender; + gboolean ret; sender = dbus_g_method_get_sender (context); - connection = dbus_g_connection_get_connection (service->system_bus); - - dbus_error_init (&dbus_error); - pk_caller = polkit_caller_new_from_dbus_name (connection, sender, &dbus_error); - if (!pk_caller) { - g_set_error (error, - CPUFREQ_SELECTOR_SERVICE_ERROR, - SERVICE_ERROR_DBUS, - "Error getting information about caller: %s: %s", - dbus_error.name, dbus_error.message); - dbus_error_free (&dbus_error); - g_free (sender); - - return FALSE; - } + subject = polkit_system_bus_name_new (sender); g_free (sender); - pk_action = polkit_action_new (); - polkit_action_set_action_id (pk_action, "org.gnome.cpufreqselector"); - pk_result = polkit_context_is_caller_authorized (service->pk_context, - pk_action, pk_caller, - TRUE, &pk_error); - polkit_caller_unref (pk_caller); - polkit_action_unref (pk_action); - - if (polkit_error_is_set (pk_error)) { - g_set_error (error, - CPUFREQ_SELECTOR_SERVICE_ERROR, - SERVICE_ERROR_GENERAL, - "Could not determine if caller is authorized: %s", - polkit_error_get_error_message (pk_error)); - polkit_error_free (pk_error); + result = polkit_authority_check_authorization_sync (service->authority, + subject, + "org.gnome.cpufreqselector", + NULL, + POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, + NULL, error); + g_object_unref (subject); + + if (*error) { + g_warning ("Check policy: %s", (*error)->message); + g_object_unref (result); return FALSE; } - if (pk_result != POLKIT_RESULT_YES) { + ret = polkit_authorization_result_get_is_authorized (result); + if (!ret) { g_set_error (error, CPUFREQ_SELECTOR_SERVICE_ERROR, SERVICE_ERROR_NOT_AUTHORIZED, - "Caller is not authorized: %s", - polkit_result_to_string_representation (pk_result)); - - return FALSE; + "Caller is not authorized"); } - - return TRUE; + + g_object_unref (result); + + return ret; } /* D-BUS interface */ @@ -518,3 +437,51 @@ cpufreq_selector_service_set_governor (CPUFreqSelectorService *service, return TRUE; } + + +gboolean +cpufreq_selector_service_can_set (CPUFreqSelectorService *service, + DBusGMethodInvocation *context) +{ + PolkitSubject *subject; + PolkitAuthorizationResult *result; + gchar *sender; + gboolean ret; + GError *error = NULL; + + reset_killtimer (); + + sender = dbus_g_method_get_sender (context); + subject = polkit_system_bus_name_new (sender); + g_free (sender); + + result = polkit_authority_check_authorization_sync (service->authority, + subject, + "org.gnome.cpufreqselector", + NULL, + 0, + NULL, + &error); + g_object_unref (subject); + + if (error) { + dbus_g_method_return_error (context, error); + g_error_free (error); + + return FALSE; + } + + if (polkit_authorization_result_get_is_authorized (result)) { + ret = TRUE; + } else if (polkit_authorization_result_get_is_challenge (result)) { + ret = TRUE; + } else { + ret = FALSE; + } + + g_object_unref (result); + + dbus_g_method_return (context, ret); + + return TRUE; +} diff --git a/cpufreq/src/cpufreq-selector/cpufreq-selector-service.h b/cpufreq/src/cpufreq-selector/cpufreq-selector-service.h index 95d7c94c2..fd321701c 100644 --- a/cpufreq/src/cpufreq-selector/cpufreq-selector-service.h +++ b/cpufreq/src/cpufreq-selector/cpufreq-selector-service.h @@ -62,6 +62,8 @@ gboolean cpufreq_selector_service_set_governor (CPUFreqSelector guint cpu, const gchar *governor, DBusGMethodInvocation *context); +gboolean cpufreq_selector_service_can_set (CPUFreqSelectorService *service, + DBusGMethodInvocation *context); G_END_DECLS diff --git a/cpufreq/src/cpufreq-selector/cpufreq-selector-service.xml b/cpufreq/src/cpufreq-selector/cpufreq-selector-service.xml index 6b742b62c..903e6072b 100644 --- a/cpufreq/src/cpufreq-selector/cpufreq-selector-service.xml +++ b/cpufreq/src/cpufreq-selector/cpufreq-selector-service.xml @@ -13,6 +13,11 @@ <arg name="cpu" direction="in" type="u"/> <arg name="governor" direction="in" type="s"/> </method> - + + <method name="CanSet"> + <annotation name="org.freedesktop.DBus.GLib.Async" value=""/> + <arg name="result" direction="out" type="b"/> + </method> + </interface> </node> diff --git a/cpufreq/src/cpufreq-selector/org.gnome.cpufreqselector.policy.in b/cpufreq/src/cpufreq-selector/org.gnome.cpufreqselector.policy.in index bbf85750f..25698e3f5 100644 --- a/cpufreq/src/cpufreq-selector/org.gnome.cpufreqselector.policy.in +++ b/cpufreq/src/cpufreq-selector/org.gnome.cpufreqselector.policy.in @@ -15,7 +15,7 @@ <_message>Privileges are required to change the CPU Frequency scaling.</_message> <defaults> <allow_inactive>no</allow_inactive> - <allow_active>auth_admin_keep_always</allow_active> + <allow_active>auth_admin_keep</allow_active> </defaults> </action> diff --git a/cpufreq/src/cpufreq-utils.c b/cpufreq/src/cpufreq-utils.c index 9c8cc2374..bd457ba7b 100644 --- a/cpufreq/src/cpufreq-utils.c +++ b/cpufreq/src/cpufreq-utils.c @@ -21,13 +21,6 @@ #include <config.h> -#ifdef HAVE_POLKIT_GNOME -#include <dbus/dbus-glib.h> -#include <dbus/dbus-glib-lowlevel.h> -#include <polkit/polkit.h> -#include <polkit-dbus/polkit-dbus.h> -#endif - #include <glib.h> #include <gtk/gtk.h> #include <sys/types.h> @@ -39,6 +32,10 @@ #include "cpufreq-utils.h" +#ifdef HAVE_POLKIT +#include <dbus/dbus-glib.h> +#endif /* HAVE_POLKIT */ + guint cpufreq_utils_get_n_cpus (void) { @@ -108,56 +105,16 @@ cpufreq_utils_display_error (const gchar *message, gtk_widget_show (dialog); } -#ifdef HAVE_POLKIT_GNOME +#ifdef HAVE_POLKIT #define CACHE_VALIDITY_SEC 2 static gboolean -pk_io_watch_have_data (GIOChannel *channel, - GIOCondition condition, - PolKitContext *pk_context) -{ - polkit_context_io_func (pk_context, - g_io_channel_unix_get_fd (channel)); - return TRUE; -} - -static int -pk_add_io_watch (PolKitContext *pk_context, - int watch_id) -{ - GIOChannel *channel; - guint id = 0; - - channel = g_io_channel_unix_new (watch_id); - if (!channel) - return 0; - - id = g_io_add_watch (channel, G_IO_IN, - (GIOFunc)pk_io_watch_have_data, - pk_context); - g_io_channel_unref (channel); - - return id; -} - -static void -pk_remove_io_watch (PolKitContext *pk_context, - int watch_id) -{ - g_source_remove (watch_id); -} - -static gboolean -selector_is_available (const gchar *action) +selector_is_available (void) { + DBusGProxy *proxy; static DBusGConnection *system_bus = NULL; - static PolKitContext *pk_context = NULL; - PolKitCaller *pk_caller; - PolKitAction *pk_action; - PolKitResult pk_result; - PolKitError *pk_error = NULL; GError *error = NULL; - DBusError dbus_error; + gboolean result; if (!system_bus) { system_bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error); @@ -169,54 +126,23 @@ selector_is_available (const gchar *action) } } - if (!pk_context) { - PolKitError *pk_error = NULL; - - pk_context = polkit_context_new (); - polkit_context_set_io_watch_functions (pk_context, - pk_add_io_watch, - pk_remove_io_watch); - if (!polkit_context_init (pk_context, &pk_error)) { - polkit_context_unref (pk_context); - pk_context = NULL; - - if (polkit_error_is_set (pk_error)) { - g_warning ("%s", polkit_error_get_error_message (pk_error)); - polkit_error_free (pk_error); - } else { - g_warning ("Cannot initialize libpolkit"); - } - - return FALSE; - } - } - - dbus_error_init (&dbus_error); - pk_caller = polkit_caller_new_from_pid (dbus_g_connection_get_connection (system_bus), - getpid (), &dbus_error); - if (!pk_caller) { - g_warning ("Cannot get caller from dbus name"); - - return FALSE; + proxy = dbus_g_proxy_new_for_name (system_bus, + "org.gnome.CPUFreqSelector", + "/org/gnome/cpufreq_selector/selector", + "org.gnome.CPUFreqSelector"); + + if (!dbus_g_proxy_call (proxy, "CanSet", &error, + G_TYPE_INVALID, + G_TYPE_BOOLEAN, &result, + G_TYPE_INVALID)) { + g_warning ("Error calling org.gnome.CPUFreqSelector.CanSet: %s", error->message); + g_error_free (error); + result = FALSE; } - pk_action = polkit_action_new (); - polkit_action_set_action_id (pk_action, action); - pk_result = polkit_context_is_caller_authorized (pk_context, - pk_action, pk_caller, - FALSE, &pk_error); - - polkit_caller_unref (pk_caller); - polkit_action_unref (pk_action); - - if (polkit_error_is_set (pk_error)) { - g_warning ("%s", polkit_error_get_error_message (pk_error)); - polkit_error_free (pk_error); - - return FALSE; - } + g_object_unref (proxy); - return !(pk_result == POLKIT_RESULT_UNKNOWN || pk_result == POLKIT_RESULT_NO); + return result; } gboolean @@ -228,13 +154,13 @@ cpufreq_utils_selector_is_available (void) time (&now); if (ABS (now - last_refreshed) > CACHE_VALIDITY_SEC) { - cache = selector_is_available ("org.gnome.cpufreqselector"); + cache = selector_is_available (); last_refreshed = now; } return cache; } -#else /* !HAVE_POLKIT_GNOME */ +#else /* !HAVE_POLKIT */ gboolean cpufreq_utils_selector_is_available (void) { |