From 454d6da49fb2b9dae4f7347a8f056fb5f4d103e8 Mon Sep 17 00:00:00 2001 From: David Zeuthen Date: Mon, 12 Jan 2009 16:06:24 -0500 Subject: [PATCH] introduce a PendingCall type to make all backend operations async --- src/polkitbackend/Makefile.am | 3 + src/polkitbackend/polkitbackend.h | 1 + src/polkitbackend/polkitbackendauthority.c | 26 ++-- src/polkitbackend/polkitbackendauthority.h | 39 +++--- src/polkitbackend/polkitbackendlocalauthority.c | 166 ++++++------------------ src/polkitbackend/polkitbackendpendingcall.c | 154 ++++++++++++++++++++++ src/polkitbackend/polkitbackendpendingcall.h | 85 ++++++++++++ src/polkitbackend/polkitbackendprivate.h | 34 +++++ src/polkitbackend/polkitbackendserver.c | 100 +++++++------- src/polkitbackend/polkitbackendtypes.h | 3 + src/programs/polkit.c | 46 ++++++- 11 files changed, 446 insertions(+), 211 deletions(-) create mode 100644 src/polkitbackend/polkitbackendpendingcall.c create mode 100644 src/polkitbackend/polkitbackendpendingcall.h create mode 100644 src/polkitbackend/polkitbackendprivate.h diff --git a/src/polkitbackend/Makefile.am b/src/polkitbackend/Makefile.am index 41ea29a..6e3e566 100644 --- a/src/polkitbackend/Makefile.am +++ b/src/polkitbackend/Makefile.am @@ -25,15 +25,18 @@ libpolkit_backend_1include_HEADERS = \ polkitbackendauthority.h \ polkitbackendlocalauthority.h \ polkitbackendactionpool.h \ + polkitbackendpendingcall.h \ polkitbackendserver.h \ $(NULL) libpolkit_backend_1_la_SOURCES = \ polkitbackend.h \ polkitbackendtypes.h \ + polkitbackendprivate.h \ polkitbackendauthority.h polkitbackendauthority.c \ polkitbackendlocalauthority.h polkitbackendlocalauthority.c \ polkitbackendactionpool.h polkitbackendactionpool.c \ + polkitbackendpendingcall.h polkitbackendpendingcall.c \ polkitbackendserver.h polkitbackendserver.c \ $(NULL) diff --git a/src/polkitbackend/polkitbackend.h b/src/polkitbackend/polkitbackend.h index 63090aa..84de7af 100644 --- a/src/polkitbackend/polkitbackend.h +++ b/src/polkitbackend/polkitbackend.h @@ -24,6 +24,7 @@ #define _POLKIT_BACKEND_INSIDE_POLKIT_BACKEND_H 1 #include +#include #include #include #include diff --git a/src/polkitbackend/polkitbackendauthority.c b/src/polkitbackend/polkitbackendauthority.c index 135dcd7..36d997d 100644 --- a/src/polkitbackend/polkitbackendauthority.c +++ b/src/polkitbackend/polkitbackendauthority.c @@ -38,36 +38,36 @@ polkit_backend_authority_class_init (PolkitBackendAuthorityClass *klass) { } -GList * -polkit_backend_authority_enumerate_actions (PolkitBackendAuthority *authority, - const gchar *locale, - GError **error) +void +polkit_backend_authority_enumerate_actions (PolkitBackendAuthority *authority, + const gchar *locale, + PolkitBackendPendingCall *pending_call) { PolkitBackendAuthorityClass *klass; klass = POLKIT_BACKEND_AUTHORITY_GET_CLASS (authority); - return klass->enumerate_actions (authority, locale, error); + klass->enumerate_actions (authority, locale, pending_call); } -GList * -polkit_backend_authority_enumerate_users (PolkitBackendAuthority *authority, - GError **error) +void +polkit_backend_authority_enumerate_users (PolkitBackendAuthority *authority, + PolkitBackendPendingCall *pending_call) { PolkitBackendAuthorityClass *klass; klass = POLKIT_BACKEND_AUTHORITY_GET_CLASS (authority); - return klass->enumerate_users (authority, error); + klass->enumerate_users (authority, pending_call); } -GList * -polkit_backend_authority_enumerate_groups (PolkitBackendAuthority *authority, - GError **error) +void +polkit_backend_authority_enumerate_groups (PolkitBackendAuthority *authority, + PolkitBackendPendingCall *pending_call) { PolkitBackendAuthorityClass *klass; klass = POLKIT_BACKEND_AUTHORITY_GET_CLASS (authority); - return klass->enumerate_groups (authority, error); + klass->enumerate_groups (authority, pending_call); } diff --git a/src/polkitbackend/polkitbackendauthority.h b/src/polkitbackend/polkitbackendauthority.h index 413f414..80e9725 100644 --- a/src/polkitbackend/polkitbackendauthority.h +++ b/src/polkitbackend/polkitbackendauthority.h @@ -51,15 +51,15 @@ struct _PolkitBackendAuthorityClass /*< public >*/ - GList * (*enumerate_actions) (PolkitBackendAuthority *authority, - const gchar *locale, - GError **error); + void (*enumerate_actions) (PolkitBackendAuthority *authority, + const gchar *locale, + PolkitBackendPendingCall *pending_call); - GList * (*enumerate_users) (PolkitBackendAuthority *authority, - GError **error); + void (*enumerate_users) (PolkitBackendAuthority *authority, + PolkitBackendPendingCall *pending_call); - GList * (*enumerate_groups) (PolkitBackendAuthority *authority, - GError **error); + void (*enumerate_groups) (PolkitBackendAuthority *authority, + PolkitBackendPendingCall *pending_call); /*< private >*/ /* Padding for future expansion */ @@ -73,17 +73,26 @@ struct _PolkitBackendAuthorityClass void (*_polkit_reserved8) (void); }; -GType polkit_backend_authority_get_type (void) G_GNUC_CONST; +GType polkit_backend_authority_get_type (void) G_GNUC_CONST; -GList *polkit_backend_authority_enumerate_actions (PolkitBackendAuthority *authority, - const gchar *locale, - GError **error); +void polkit_backend_authority_enumerate_actions (PolkitBackendAuthority *authority, + const gchar *locale, + PolkitBackendPendingCall *pending_call); -GList *polkit_backend_authority_enumerate_users (PolkitBackendAuthority *authority, - GError **error); +void polkit_backend_authority_enumerate_users (PolkitBackendAuthority *authority, + PolkitBackendPendingCall *pending_call); -GList *polkit_backend_authority_enumerate_groups (PolkitBackendAuthority *authority, - GError **error); +void polkit_backend_authority_enumerate_groups (PolkitBackendAuthority *authority, + PolkitBackendPendingCall *pending_call); + +void polkit_backend_authority_enumerate_actions_finish (PolkitBackendPendingCall *pending_call, + GList *actions); + +void polkit_backend_authority_enumerate_users_finish (PolkitBackendPendingCall *pending_call, + GList *users); + +void polkit_backend_authority_enumerate_groups_finish (PolkitBackendPendingCall *pending_call, + GList *groups); G_END_DECLS diff --git a/src/polkitbackend/polkitbackendlocalauthority.c b/src/polkitbackend/polkitbackendlocalauthority.c index 028adc7..94d0cbb 100644 --- a/src/polkitbackend/polkitbackendlocalauthority.c +++ b/src/polkitbackend/polkitbackendlocalauthority.c @@ -27,6 +27,7 @@ #include #include "polkitbackendlocalauthority.h" #include "polkitbackendactionpool.h" +#include "polkitbackendpendingcall.h" typedef struct { @@ -34,15 +35,15 @@ typedef struct } PolkitBackendLocalAuthorityPrivate; -static GList *polkit_backend_local_authority_enumerate_actions (PolkitBackendAuthority *authority, - const gchar *locale, - GError **error); +static void polkit_backend_local_authority_enumerate_actions (PolkitBackendAuthority *authority, + const gchar *locale, + PolkitBackendPendingCall *pending_call); -static GList *polkit_backend_local_authority_enumerate_users (PolkitBackendAuthority *authority, - GError **error); +static void polkit_backend_local_authority_enumerate_users (PolkitBackendAuthority *authority, + PolkitBackendPendingCall *pending_call); -static GList *polkit_backend_local_authority_enumerate_groups (PolkitBackendAuthority *authority, - GError **error); +static void polkit_backend_local_authority_enumerate_groups (PolkitBackendAuthority *authority, + PolkitBackendPendingCall *pending_call); G_DEFINE_TYPE (PolkitBackendLocalAuthority, polkit_backend_local_authority, POLKIT_BACKEND_TYPE_AUTHORITY); @@ -103,23 +104,27 @@ polkit_backend_local_authority_new (void) /* ---------------------------------------------------------------------------------------------------- */ -static GList * -polkit_backend_local_authority_enumerate_actions (PolkitBackendAuthority *authority, - const gchar *locale, - GError **error) +static void +polkit_backend_local_authority_enumerate_actions (PolkitBackendAuthority *authority, + const gchar *locale, + PolkitBackendPendingCall *pending_call) { PolkitBackendLocalAuthority *local_authority; PolkitBackendLocalAuthorityPrivate *priv; + GList *actions; local_authority = POLKIT_BACKEND_LOCAL_AUTHORITY (authority); priv = POLKIT_BACKEND_LOCAL_AUTHORITY_GET_PRIVATE (local_authority); - return polkit_backend_action_pool_get_all_actions (priv->action_pool, locale); + actions = polkit_backend_action_pool_get_all_actions (priv->action_pool, locale); + + polkit_backend_authority_enumerate_actions_finish (pending_call, + actions); } -static GList * -polkit_backend_local_authority_enumerate_users (PolkitBackendAuthority *authority, - GError **error) +static void +polkit_backend_local_authority_enumerate_users (PolkitBackendAuthority *authority, + PolkitBackendPendingCall *pending_call) { PolkitBackendLocalAuthority *local_authority; PolkitBackendLocalAuthorityPrivate *priv; @@ -134,11 +139,11 @@ polkit_backend_local_authority_enumerate_users (PolkitBackendAuthority *authori passwd = getpwent (); if (passwd == NULL) { - g_set_error (error, - POLKIT_ERROR, - POLKIT_ERROR_FAILED, - "getpwent failed: %s", - strerror (errno)); + polkit_backend_pending_call_return_error (pending_call, + POLKIT_ERROR, + POLKIT_ERROR_FAILED, + "getpwent failed: %s", + strerror (errno)); goto out; } @@ -155,13 +160,15 @@ polkit_backend_local_authority_enumerate_users (PolkitBackendAuthority *authori list = g_list_reverse (list); + polkit_backend_authority_enumerate_users_finish (pending_call, list); + out: - return list; + ; } -static GList * -polkit_backend_local_authority_enumerate_groups (PolkitBackendAuthority *authority, - GError **error) +static void +polkit_backend_local_authority_enumerate_groups (PolkitBackendAuthority *authority, + PolkitBackendPendingCall *pending_call) { PolkitBackendLocalAuthority *local_authority; PolkitBackendLocalAuthorityPrivate *priv; @@ -176,11 +183,11 @@ polkit_backend_local_authority_enumerate_groups (PolkitBackendAuthority *author group = getgrent (); if (group == NULL) { - g_set_error (error, - POLKIT_ERROR, - POLKIT_ERROR_FAILED, - "getgrent failed: %s", - strerror (errno)); + polkit_backend_pending_call_return_error (pending_call, + POLKIT_ERROR, + POLKIT_ERROR_FAILED, + "getpwent failed: %s", + strerror (errno)); goto out; } @@ -197,105 +204,8 @@ polkit_backend_local_authority_enumerate_groups (PolkitBackendAuthority *author list = g_list_reverse (list); - out: - return list; -} - -#if 0 -/* ---------------------------------------------------------------------------------------------------- */ - -static void -authority_iface_handle_say_hello (PolkitAuthority *authority, - const gchar *message, - EggDBusMethodInvocation *method_invocation) -{ - gchar *result; - - result = g_strdup_printf ("You said '%s' to the AUTHORITY!", message); - - polkit_authority_handle_say_hello_finish (method_invocation, - result); - - g_free (result); -} - -/* ---------------------------------------------------------------------------------------------------- */ - -static void -authority_iface_handle_enumerate_users (PolkitAuthority *authority, - EggDBusMethodInvocation *method_invocation) -{ - struct passwd *passwd; - GList *list; - - list = NULL; - - passwd = getpwent (); - if (passwd == NULL) - { - egg_dbus_method_invocation_return_error (method_invocation, - POLKIT_ERROR, - POLKIT_ERROR_FAILED, - "getpwent failed: %s", - strerror (errno)); - goto out; - } - - do - { - PolkitSubject *subject; - - subject = polkit_subject_new_for_unix_user (passwd->pw_uid); - - list = g_list_prepend (list, subject); - } - while ((passwd = getpwent ()) != NULL); - endpwent (); - - list = g_list_reverse (list); - - polkit_authority_handle_enumerate_users_finish (method_invocation, - list); + polkit_backend_authority_enumerate_groups_finish (pending_call, list); out: - g_list_foreach (list, (GFunc) g_object_unref, NULL); - g_list_free (list); + ; } - -/* ---------------------------------------------------------------------------------------------------- */ - -static void -authority_iface_handle_enumerate_actions (PolkitAuthority *authority, - const gchar *locale, - EggDBusMethodInvocation *method_invocation) -{ - PolkitBackendLocalAuthority *local_authority; - PolkitBackendLocalAuthorityPrivate *priv; - GList *list; - - local_authority = POLKIT_BACKEND_LOCAL_AUTHORITY (authority); - priv = POLKIT_BACKEND_LOCAL_AUTHORITY_GET_PRIVATE (local_authority); - - list = polkit_backend_action_pool_get_all_actions (priv->action_pool, - locale); - - polkit_authority_handle_enumerate_actions_finish (method_invocation, - list); - - g_list_foreach (list, (GFunc) g_object_unref, NULL); - g_list_free (list); -} - -/* ---------------------------------------------------------------------------------------------------- */ - -static void -authority_iface_init (PolkitAuthorityIface *authority_iface, - gpointer iface_data) -{ - authority_iface->handle_say_hello = authority_iface_handle_say_hello; - authority_iface->handle_enumerate_users = authority_iface_handle_enumerate_users; - authority_iface->handle_enumerate_actions = authority_iface_handle_enumerate_actions; -} - -/* ---------------------------------------------------------------------------------------------------- */ -#endif diff --git a/src/polkitbackend/polkitbackendpendingcall.c b/src/polkitbackend/polkitbackendpendingcall.c new file mode 100644 index 0000000..baefb25 --- /dev/null +++ b/src/polkitbackend/polkitbackendpendingcall.c @@ -0,0 +1,154 @@ +/* + * Copyright (C) 2008 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307, USA. + * + * Author: David Zeuthen + */ + +#include "config.h" + +#include +#include "polkitbackendpendingcall.h" +#include "polkitbackendprivate.h" + +typedef struct +{ + EggDBusMethodInvocation *method_invocation; + PolkitBackendServer *server; +} PolkitBackendPendingCallPrivate; + +G_DEFINE_TYPE (PolkitBackendPendingCall, polkit_backend_pending_call, G_TYPE_OBJECT); + +#define POLKIT_BACKEND_PENDING_CALL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), POLKIT_BACKEND_TYPE_PENDING_CALL, PolkitBackendPendingCallPrivate)) + +static void +polkit_backend_pending_call_init (PolkitBackendPendingCall *pending_call) +{ + PolkitBackendPendingCallPrivate *priv; + + priv = POLKIT_BACKEND_PENDING_CALL_GET_PRIVATE (pending_call); + +} + +static void +polkit_backend_pending_call_finalize (GObject *object) +{ + PolkitBackendPendingCall *pending_call; + PolkitBackendPendingCallPrivate *priv; + + pending_call = POLKIT_BACKEND_PENDING_CALL (object); + priv = POLKIT_BACKEND_PENDING_CALL_GET_PRIVATE (pending_call); + + g_object_unref (priv->method_invocation); + g_object_unref (priv->server); + + G_OBJECT_CLASS (polkit_backend_pending_call_parent_class)->finalize (object); +} + +static void +polkit_backend_pending_call_class_init (PolkitBackendPendingCallClass *klass) +{ + GObjectClass *gobject_class; + + gobject_class = G_OBJECT_CLASS (klass); + + gobject_class->finalize = polkit_backend_pending_call_finalize; + + g_type_class_add_private (klass, sizeof (PolkitBackendPendingCallPrivate)); +} + +PolkitBackendPendingCall * +_polkit_backend_pending_call_new (EggDBusMethodInvocation *method_invocation, + PolkitBackendServer *server) +{ + PolkitBackendPendingCall *pending_call; + PolkitBackendPendingCallPrivate *priv; + + pending_call = POLKIT_BACKEND_PENDING_CALL (g_object_new (POLKIT_BACKEND_TYPE_PENDING_CALL, + NULL)); + + priv = POLKIT_BACKEND_PENDING_CALL_GET_PRIVATE (pending_call); + + priv->method_invocation = g_object_ref (method_invocation); + priv->server = g_object_ref (server); + + return pending_call; +} + +PolkitBackendServer * +polkit_backend_pending_call_get_server (PolkitBackendPendingCall *pending_call) +{ + PolkitBackendPendingCallPrivate *priv; + priv = POLKIT_BACKEND_PENDING_CALL_GET_PRIVATE (pending_call); + return priv->server; +} + +EggDBusMethodInvocation * +_polkit_backend_pending_call_get_method_invocation (PolkitBackendPendingCall *pending_call) +{ + PolkitBackendPendingCallPrivate *priv; + priv = POLKIT_BACKEND_PENDING_CALL_GET_PRIVATE (pending_call); + return priv->method_invocation; +} + + +PolkitSubject * +polkit_backend_pending_call_get_caller (PolkitBackendPendingCall *pending_call) +{ + /* TODO: implement */ + return NULL; +} + +void +polkit_backend_pending_call_return_gerror (PolkitBackendPendingCall *pending_call, + GError *error) +{ + PolkitBackendPendingCallPrivate *priv; + + priv = POLKIT_BACKEND_PENDING_CALL_GET_PRIVATE (pending_call); + + egg_dbus_method_invocation_return_gerror (priv->method_invocation, + error); + + g_object_unref (pending_call); +} + +void +polkit_backend_pending_call_return_error (PolkitBackendPendingCall *pending_call, + GQuark domain, + gint code, + const gchar *format, + ...) +{ + GError *error; + va_list va_args; + gchar *literal_message; + + va_start (va_args, format); + literal_message = g_strdup_vprintf (format, va_args); + + error = g_error_new_literal (domain, + code, + literal_message); + + polkit_backend_pending_call_return_gerror (pending_call, error); + + g_error_free (error); + g_free (literal_message); + va_end (va_args); +} + diff --git a/src/polkitbackend/polkitbackendpendingcall.h b/src/polkitbackend/polkitbackendpendingcall.h new file mode 100644 index 0000000..de26e52 --- /dev/null +++ b/src/polkitbackend/polkitbackendpendingcall.h @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2008 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307, USA. + * + * Author: David Zeuthen + */ + +#if !defined (_POLKIT_BACKEND_COMPILATION) && !defined(_POLKIT_BACKEND_INSIDE_POLKIT_BACKEND_H) +#error "Only can be included directly, this file may disappear or change contents." +#endif + +#ifndef __POLKIT_BACKEND_PENDING_CALL_H +#define __POLKIT_BACKEND_PENDING_CALL_H + +#include +#include "polkitbackendtypes.h" + +G_BEGIN_DECLS + +#define POLKIT_BACKEND_TYPE_PENDING_CALL (polkit_backend_pending_call_get_type ()) +#define POLKIT_BACKEND_PENDING_CALL(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), POLKIT_BACKEND_TYPE_PENDING_CALL, PolkitBackendPendingCall)) +#define POLKIT_BACKEND_PENDING_CALL_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), POLKIT_BACKEND_TYPE_PENDING_CALL, PolkitBackendPendingCallClass)) +#define POLKIT_BACKEND_PENDING_CALL_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), POLKIT_BACKEND_TYPE_PENDING_CALL,PolkitBackendPendingCallClass)) +#define POLKIT_BACKEND_IS_PENDING_CALL(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), POLKIT_BACKEND_TYPE_PENDING_CALL)) +#define POLKIT_BACKEND_IS_PENDING_CALL_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), POLKIT_BACKEND_TYPE_PENDING_CALL)) + +#if 0 +typedef struct _PolkitBackendPendingCall PolkitBackendPendingCall; +#endif +typedef struct _PolkitBackendPendingCallClass PolkitBackendPendingCallClass; + +struct _PolkitBackendPendingCall +{ + GObject parent_instance; +}; + +struct _PolkitBackendPendingCallClass +{ + GObjectClass parent_class; + + /*< public >*/ + + /*< private >*/ + /* Padding for future expansion */ + void (*_polkit_reserved1) (void); + void (*_polkit_reserved2) (void); + void (*_polkit_reserved3) (void); + void (*_polkit_reserved4) (void); + void (*_polkit_reserved5) (void); + void (*_polkit_reserved6) (void); + void (*_polkit_reserved7) (void); + void (*_polkit_reserved8) (void); +}; + +GType polkit_backend_pending_call_get_type (void) G_GNUC_CONST; +PolkitSubject *polkit_backend_pending_call_get_caller (PolkitBackendPendingCall *pending_call); +PolkitBackendServer *polkit_backend_pending_call_get_server (PolkitBackendPendingCall *pending_call); +void polkit_backend_pending_call_return_error (PolkitBackendPendingCall *pending_call, + GQuark domain, + gint code, + const gchar *format, + ...); +void polkit_backend_pending_call_return_gerror (PolkitBackendPendingCall *pending_call, + GError *error); + + + +G_END_DECLS + +#endif /* __POLKIT_BACKEND_PENDING_CALL_H */ + diff --git a/src/polkitbackend/polkitbackendprivate.h b/src/polkitbackend/polkitbackendprivate.h new file mode 100644 index 0000000..e43635c --- /dev/null +++ b/src/polkitbackend/polkitbackendprivate.h @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2008 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307, USA. + * + * Author: David Zeuthen + */ + +#ifndef __POLKIT_BACKEND_PRIVATE_H +#define __POLKIT_BACKEND_PRIVATE_H + +#include +#include "polkitbackendpendingcall.h" +#include "polkitbackendserver.h" + +PolkitBackendPendingCall *_polkit_backend_pending_call_new (EggDBusMethodInvocation *method_invocation, + PolkitBackendServer *server); + +EggDBusMethodInvocation *_polkit_backend_pending_call_get_method_invocation (PolkitBackendPendingCall *pending_call); + +#endif /* __POLKIT_BACKEND_PRIVATE_H */ diff --git a/src/polkitbackend/polkitbackendserver.c b/src/polkitbackend/polkitbackendserver.c index 05125d1..8cc25c6 100644 --- a/src/polkitbackend/polkitbackendserver.c +++ b/src/polkitbackend/polkitbackendserver.c @@ -28,6 +28,7 @@ #include "polkitbackendauthority.h" #include "polkitbackendserver.h" +#include "polkitbackendprivate.h" struct _PolkitBackendServer { @@ -90,20 +91,19 @@ authority_handle_enumerate_actions (_PolkitAuthority *instance, EggDBusMethodInvocation *method_invocation) { PolkitBackendServer *server = POLKIT_BACKEND_SERVER (instance); - EggDBusArraySeq *array; - GError *error; - GList *actions; - GList *l; + PolkitBackendPendingCall *pending_call; - error = NULL; - actions = polkit_backend_authority_enumerate_actions (server->authority, locale, &error); + pending_call = _polkit_backend_pending_call_new (method_invocation, server); - if (error != NULL) - { - egg_dbus_method_invocation_return_gerror (method_invocation, error); - g_error_free (error); - goto out; - } + polkit_backend_authority_enumerate_actions (server->authority, locale, pending_call); +} + +void +polkit_backend_authority_enumerate_actions_finish (PolkitBackendPendingCall *pending_call, + GList *actions) +{ + EggDBusArraySeq *array; + GList *l; array = egg_dbus_array_seq_new (_POLKIT_TYPE_ACTION_DESCRIPTION, (GDestroyNotify) g_object_unref, NULL, NULL); @@ -116,15 +116,15 @@ authority_handle_enumerate_actions (_PolkitAuthority *instance, egg_dbus_array_seq_add (array, real); } - _polkit_authority_handle_enumerate_actions_finish (method_invocation, array); + _polkit_authority_handle_enumerate_actions_finish (_polkit_backend_pending_call_get_method_invocation (pending_call), + array); g_object_unref (array); g_list_foreach (actions, (GFunc) g_object_unref, NULL); g_list_free (actions); - out: - ; + g_object_unref (pending_call); } static void @@ -132,24 +132,23 @@ authority_handle_enumerate_users (_PolkitAuthority *instance, EggDBusMethodInvocation *method_invocation) { PolkitBackendServer *server = POLKIT_BACKEND_SERVER (instance); - EggDBusArraySeq *array; - GError *error; - GList *subjects; - GList *l; + PolkitBackendPendingCall *pending_call; - error = NULL; - subjects = polkit_backend_authority_enumerate_users (server->authority, &error); + pending_call = _polkit_backend_pending_call_new (method_invocation, server); - if (error != NULL) - { - egg_dbus_method_invocation_return_gerror (method_invocation, error); - g_error_free (error); - goto out; - } + polkit_backend_authority_enumerate_users (server->authority, pending_call); +} + +void +polkit_backend_authority_enumerate_users_finish (PolkitBackendPendingCall *pending_call, + GList *users) +{ + EggDBusArraySeq *array; + GList *l; array = egg_dbus_array_seq_new (_POLKIT_TYPE_SUBJECT, (GDestroyNotify) g_object_unref, NULL, NULL); - for (l = subjects; l != NULL; l = l->next) + for (l = users; l != NULL; l = l->next) { PolkitSubject *subject = POLKIT_SUBJECT (l->data); _PolkitSubject *real; @@ -158,15 +157,13 @@ authority_handle_enumerate_users (_PolkitAuthority *instance, egg_dbus_array_seq_add (array, real); } - _polkit_authority_handle_enumerate_users_finish (method_invocation, array); + _polkit_authority_handle_enumerate_users_finish (_polkit_backend_pending_call_get_method_invocation (pending_call), + array); g_object_unref (array); - g_list_foreach (subjects, (GFunc) g_object_unref, NULL); - g_list_free (subjects); - - out: - ; + g_list_foreach (users, (GFunc) g_object_unref, NULL); + g_list_free (users); } static void @@ -174,24 +171,23 @@ authority_handle_enumerate_groups (_PolkitAuthority *instance, EggDBusMethodInvocation *method_invocation) { PolkitBackendServer *server = POLKIT_BACKEND_SERVER (instance); - EggDBusArraySeq *array; - GError *error; - GList *subjects; - GList *l; + PolkitBackendPendingCall *pending_call; - error = NULL; - subjects = polkit_backend_authority_enumerate_groups (server->authority, &error); + pending_call = _polkit_backend_pending_call_new (method_invocation, server); - if (error != NULL) - { - egg_dbus_method_invocation_return_gerror (method_invocation, error); - g_error_free (error); - goto out; - } + polkit_backend_authority_enumerate_groups (server->authority, pending_call); +} + +void +polkit_backend_authority_enumerate_groups_finish (PolkitBackendPendingCall *pending_call, + GList *groups) +{ + EggDBusArraySeq *array; + GList *l; array = egg_dbus_array_seq_new (_POLKIT_TYPE_SUBJECT, (GDestroyNotify) g_object_unref, NULL, NULL); - for (l = subjects; l != NULL; l = l->next) + for (l = groups; l != NULL; l = l->next) { PolkitSubject *subject = POLKIT_SUBJECT (l->data); _PolkitSubject *real; @@ -200,15 +196,13 @@ authority_handle_enumerate_groups (_PolkitAuthority *instance, egg_dbus_array_seq_add (array, real); } - _polkit_authority_handle_enumerate_groups_finish (method_invocation, array); + _polkit_authority_handle_enumerate_groups_finish (_polkit_backend_pending_call_get_method_invocation (pending_call), + array); g_object_unref (array); - g_list_foreach (subjects, (GFunc) g_object_unref, NULL); - g_list_free (subjects); - - out: - ; + g_list_foreach (groups, (GFunc) g_object_unref, NULL); + g_list_free (groups); } static void diff --git a/src/polkitbackend/polkitbackendtypes.h b/src/polkitbackend/polkitbackendtypes.h index 715adda..a1ebd8e 100644 --- a/src/polkitbackend/polkitbackendtypes.h +++ b/src/polkitbackend/polkitbackendtypes.h @@ -27,6 +27,9 @@ struct _PolkitBackendAuthority; typedef struct _PolkitBackendAuthority PolkitBackendAuthority; +struct _PolkitBackendPendingCall; +typedef struct _PolkitBackendPendingCall PolkitBackendPendingCall; + struct _PolkitBackendServer; typedef struct _PolkitBackendServer PolkitBackendServer; diff --git a/src/programs/polkit.c b/src/programs/polkit.c index c372eb0..d436338 100644 --- a/src/programs/polkit.c +++ b/src/programs/polkit.c @@ -48,6 +48,8 @@ static gboolean list_actions (void); static gboolean list_users (void); static gboolean list_groups (void); +static gboolean check (void); + static gboolean show_action (const gchar *action_id); static void @@ -210,8 +212,13 @@ main (int argc, char *argv[]) } else if (opt_check) { - g_print ("subject '%s' action-id '%s'\n", polkit_subject_to_string (subject), action_id); - g_assert (FALSE); + if (subject == NULL || action_id == NULL) + { + usage (argc, argv); + goto out; + } + + ret = check (); } else { @@ -450,3 +457,38 @@ list_groups (void) out: return ret; } + +static gboolean +check (void) +{ + PolkitAuthorizationResult result; + PolkitAuthorizationClaim *claim; + GError *error; + + error = NULL; + claim = NULL; + result = POLKIT_AUTHORIZATION_RESULT_NOT_AUTHORIZED; + + claim = polkit_authorization_claim_new (subject, + action_id); + + result = polkit_authority_check_claim_sync (authority, + claim, + NULL, + &error); + if (error != NULL) + { + g_printerr ("Error checking authorization claim: %s\n", error->message); + g_error_free (error); + goto out; + } + + g_debug ("result = %d", result); + + out: + if (claim != NULL) + g_object_unref (claim); + + return result == POLKIT_AUTHORIZATION_RESULT_AUTHORIZED; +} + -- 2.7.4