From 936ca49edb4ea8efa15d336797a3d66269c9e1f4 Mon Sep 17 00:00:00 2001 From: David Zeuthen Date: Fri, 6 Apr 2007 16:23:47 -0400 Subject: [PATCH] move to using _pk_debug and respect $POLKIT_DEBUG --- libpolkit/Makefile.am | 3 +- libpolkit/libpolkit-caller.c | 13 +++--- libpolkit/libpolkit-context.c | 12 ++++-- libpolkit/libpolkit-debug.c | 69 ++++++++++++++++++++++++++++++ libpolkit/libpolkit-debug.h | 38 ++++++++++++++++ libpolkit/libpolkit-privilege-cache.c | 11 +++-- libpolkit/libpolkit-privilege-default.c | 21 ++++----- libpolkit/libpolkit-privilege-file-entry.c | 7 +-- libpolkit/libpolkit-privilege.c | 3 +- libpolkit/libpolkit-resource.c | 3 +- libpolkit/libpolkit-seat.c | 3 +- libpolkit/libpolkit-session.c | 16 ++++--- libpolkit/libpolkit.c | 9 ++-- 13 files changed, 167 insertions(+), 41 deletions(-) create mode 100644 libpolkit/libpolkit-debug.c create mode 100644 libpolkit/libpolkit-debug.h diff --git a/libpolkit/Makefile.am b/libpolkit/Makefile.am index 90e9695..244e21e 100644 --- a/libpolkit/Makefile.am +++ b/libpolkit/Makefile.am @@ -43,7 +43,8 @@ libpolkit_la_SOURCES = \ libpolkit-privilege-file-entry.h libpolkit-privilege-file-entry.c \ libpolkit-privilege-file.h libpolkit-privilege-file.c \ libpolkit-privilege-cache.h libpolkit-privilege-cache.c \ - libpolkit-privilege-default.h libpolkit-privilege-default.c + libpolkit-privilege-default.h libpolkit-privilege-default.c \ + libpolkit-debug.h libpolkit-debug.c libpolkit_la_LIBADD = @GLIB_LIBS@ @DBUS_LIBS@ diff --git a/libpolkit/libpolkit-caller.c b/libpolkit/libpolkit-caller.c index a325256..56ef035 100644 --- a/libpolkit/libpolkit-caller.c +++ b/libpolkit/libpolkit-caller.c @@ -44,6 +44,7 @@ #include #include +#include "libpolkit-debug.h" #include "libpolkit-caller.h" /** @@ -414,10 +415,10 @@ libpolkit_caller_new_from_dbus_name (DBusConnection *con, const char *dbus_name, } not_in_session: - g_debug ("uid %d", uid); - g_debug ("pid %d", pid); - g_debug ("selinux context '%s'", selinux_context != NULL ? selinux_context : "(not set)"); - g_debug ("ck session '%s'", ck_session_objpath != NULL ? ck_session_objpath : "(not in a session)"); + _pk_debug ("uid %d", uid); + _pk_debug ("pid %d", pid); + _pk_debug ("selinux context '%s'", selinux_context != NULL ? selinux_context : "(not set)"); + _pk_debug ("ck session '%s'", ck_session_objpath != NULL ? ck_session_objpath : "(not in a session)"); caller = libpolkit_caller_new (); libpolkit_caller_set_dbus_name (caller, dbus_name); @@ -445,8 +446,8 @@ void libpolkit_caller_debug (PolKitCaller *caller) { g_return_if_fail (caller != NULL); - g_debug ("PolKitCaller: refcount=%d dbus_name=%s uid=%d pid=%d selinux_context=%s", - caller->refcount, caller->dbus_name, caller->uid, caller->pid, caller->selinux_context); + _pk_debug ("PolKitCaller: refcount=%d dbus_name=%s uid=%d pid=%d selinux_context=%s", + caller->refcount, caller->dbus_name, caller->uid, caller->pid, caller->selinux_context); if (caller->session != NULL) libpolkit_session_debug (caller->session); } diff --git a/libpolkit/libpolkit-context.c b/libpolkit/libpolkit-context.c index 3a6fc32..b1cf968 100644 --- a/libpolkit/libpolkit-context.c +++ b/libpolkit/libpolkit-context.c @@ -37,6 +37,7 @@ #include #include +#include "libpolkit-debug.h" #include "libpolkit-context.h" #include "libpolkit-privilege-cache.h" @@ -65,7 +66,12 @@ struct PolKitContext * libpolkit_context_new: * @error: return location for error * - * Create a new context. + * Create a new context; loads PolicyKit files from + * /etc/PolicyKit/privileges unless the environment variable + * $POLKIT_PRIVILEGE_DIR points to a location. + * + * If the environment $POLKIT_DEBUG is set, libpolkit will spew lots + * of debug. * * Returns: #NULL if @error was set, otherwise the #PolKitPrivilegeCache object **/ @@ -77,9 +83,9 @@ libpolkit_context_new (GError **error) pk_context = g_new0 (PolKitContext, 1); pk_context->refcount = 1; - dirname = getenv ("POLKIT_PRIV_DIR"); + dirname = getenv ("POLKIT_PRIVILEGE_DIR"); if (dirname != NULL) { - g_debug ("Using directory %s", dirname); + _pk_debug ("Using directory %s", dirname); } else { dirname = PACKAGE_SYSCONF_DIR "/PolicyKit/privileges"; } diff --git a/libpolkit/libpolkit-debug.c b/libpolkit/libpolkit-debug.c new file mode 100644 index 0000000..1a07435 --- /dev/null +++ b/libpolkit/libpolkit-debug.c @@ -0,0 +1,69 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */ +/*************************************************************************** + * + * libpolkit.c : library for querying system-wide policy + * + * Copyright (C) 2007 David Zeuthen, + * + * Licensed under the Academic Free License version 2.1 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + **************************************************************************/ + +/** + * SECTION:libpolkit-debug + * @short_description: Debug functions. + * + * These functions are used for debug purposes + **/ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include +#include +#include + +#include "libpolkit-debug.h" + +/** + * pk_debug: + * @format: format + * + * Print debug message + **/ +void +_pk_debug (const char *format, ...) +{ + va_list args; + static gboolean show_debug = FALSE; + static gboolean init = FALSE; + + if (!init) { + init = TRUE; + if (getenv ("POLKIT_DEBUG") != NULL) { + show_debug = TRUE; + } + } + + if (show_debug) { + va_start (args, format); + vfprintf (stdout, format, args); + va_end (args); + fprintf (stdout, "\n"); + } +} diff --git a/libpolkit/libpolkit-debug.h b/libpolkit/libpolkit-debug.h new file mode 100644 index 0000000..c576f11 --- /dev/null +++ b/libpolkit/libpolkit-debug.h @@ -0,0 +1,38 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */ +/*************************************************************************** + * + * libpolkit-debug.h : debug infrastructure for libpolkit + * + * Copyright (C) 2007 David Zeuthen, + * + * Licensed under the Academic Free License version 2.1 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + **************************************************************************/ + +#ifndef LIBPOLKIT_DEBUG_H +#define LIBPOLKIT_DEBUG_H + +#include +#include +#include +#include + +void _pk_debug (const char *format, ...) __attribute__((__format__ (__printf__, 1, 2))); + +#endif /* LIBPOLKIT_DEBUG_H */ + + diff --git a/libpolkit/libpolkit-privilege-cache.c b/libpolkit/libpolkit-privilege-cache.c index 51c3a26..517de24 100644 --- a/libpolkit/libpolkit-privilege-cache.c +++ b/libpolkit/libpolkit-privilege-cache.c @@ -37,6 +37,7 @@ #include #include +#include "libpolkit-debug.h" #include "libpolkit-privilege-file.h" #include "libpolkit-privilege-cache.h" @@ -112,7 +113,7 @@ libpolkit_privilege_cache_new (const char *dirname, GError **error) path = g_strdup_printf ("%s/%s", dirname, file); - g_debug ("Loading %s", path); + _pk_debug ("Loading %s", path); pf = libpolkit_privilege_file_new (path, error); g_free (path); @@ -188,9 +189,9 @@ libpolkit_privilege_cache_debug (PolKitPrivilegeCache *privilege_cache) GSList *i; g_return_if_fail (privilege_cache != NULL); - g_debug ("PolKitPrivilegeCache: refcount=%d num_entries=%d ...", - privilege_cache->refcount, - privilege_cache->priv_entries == NULL ? 0 : g_slist_length (privilege_cache->priv_entries)); + _pk_debug ("PolKitPrivilegeCache: refcount=%d num_entries=%d ...", + privilege_cache->refcount, + privilege_cache->priv_entries == NULL ? 0 : g_slist_length (privilege_cache->priv_entries)); for (i = privilege_cache->priv_entries; i != NULL; i = g_slist_next (i)) { PolKitPrivilegeFileEntry *pfe = i->data; @@ -221,6 +222,8 @@ libpolkit_privilege_cache_get_entry (PolKitPrivilegeCache *privilege_cache, pfe = NULL; + /* I'm sure it would be easy to make this O(1)... */ + g_return_val_if_fail (privilege_cache != NULL, NULL); g_return_val_if_fail (privilege != NULL, NULL); diff --git a/libpolkit/libpolkit-privilege-default.c b/libpolkit/libpolkit-privilege-default.c index 3a2eb99..018786c 100644 --- a/libpolkit/libpolkit-privilege-default.c +++ b/libpolkit/libpolkit-privilege-default.c @@ -37,6 +37,7 @@ #include #include +#include "libpolkit-debug.h" #include "libpolkit-error.h" #include "libpolkit-privilege-default.h" @@ -199,16 +200,16 @@ void libpolkit_privilege_default_debug (PolKitPrivilegeDefault *privilege_default) { g_return_if_fail (privilege_default != NULL); - g_debug ("PolKitPrivilegeDefault: refcount=%d\n" - " default_remote_inactive=%s\n" - " default_remote_active=%s\n" - " default_local_inactive=%s\n" - " default_local_active=%s", - privilege_default->refcount, - libpolkit_result_to_string_representation (privilege_default->default_remote_inactive), - libpolkit_result_to_string_representation (privilege_default->default_remote_active), - libpolkit_result_to_string_representation (privilege_default->default_local_inactive), - libpolkit_result_to_string_representation (privilege_default->default_local_active)); + _pk_debug ("PolKitPrivilegeDefault: refcount=%d\n" + " default_remote_inactive=%s\n" + " default_remote_active=%s\n" + " default_local_inactive=%s\n" + " default_local_active=%s", + privilege_default->refcount, + libpolkit_result_to_string_representation (privilege_default->default_remote_inactive), + libpolkit_result_to_string_representation (privilege_default->default_remote_active), + libpolkit_result_to_string_representation (privilege_default->default_local_inactive), + libpolkit_result_to_string_representation (privilege_default->default_local_active)); } diff --git a/libpolkit/libpolkit-privilege-file-entry.c b/libpolkit/libpolkit-privilege-file-entry.c index bd9c034..22052a8 100644 --- a/libpolkit/libpolkit-privilege-file-entry.c +++ b/libpolkit/libpolkit-privilege-file-entry.c @@ -37,6 +37,7 @@ #include #include +#include "libpolkit-debug.h" #include "libpolkit-error.h" #include "libpolkit-result.h" #include "libpolkit-privilege-file-entry.h" @@ -140,9 +141,9 @@ void libpolkit_privilege_file_entry_debug (PolKitPrivilegeFileEntry *privilege_file_entry) { g_return_if_fail (privilege_file_entry != NULL); - g_debug ("PolKitPrivilegeFileEntry: refcount=%d privilege=%s", - privilege_file_entry->refcount, - privilege_file_entry->privilege); + _pk_debug ("PolKitPrivilegeFileEntry: refcount=%d privilege=%s", + privilege_file_entry->refcount, + privilege_file_entry->privilege); libpolkit_privilege_default_debug (privilege_file_entry->defaults); } diff --git a/libpolkit/libpolkit-privilege.c b/libpolkit/libpolkit-privilege.c index faf9747..56c257e 100644 --- a/libpolkit/libpolkit-privilege.c +++ b/libpolkit/libpolkit-privilege.c @@ -37,6 +37,7 @@ #include #include +#include "libpolkit-debug.h" #include "libpolkit-privilege.h" /** @@ -155,5 +156,5 @@ void libpolkit_privilege_debug (PolKitPrivilege *privilege) { g_return_if_fail (privilege != NULL); - g_debug ("PolKitPrivilege: refcount=%d id=%s", privilege->refcount, privilege->id); + _pk_debug ("PolKitPrivilege: refcount=%d id=%s", privilege->refcount, privilege->id); } diff --git a/libpolkit/libpolkit-resource.c b/libpolkit/libpolkit-resource.c index 1ed4bae..24d4ff1 100644 --- a/libpolkit/libpolkit-resource.c +++ b/libpolkit/libpolkit-resource.c @@ -37,6 +37,7 @@ #include #include +#include "libpolkit-debug.h" #include "libpolkit-resource.h" /** @@ -203,5 +204,5 @@ void libpolkit_resource_debug (PolKitResource *resource) { g_return_if_fail (resource != NULL); - g_debug ("PolKitResource: refcount=%d type=%s id=%s", resource->refcount, resource->type, resource->id); + _pk_debug ("PolKitResource: refcount=%d type=%s id=%s", resource->refcount, resource->type, resource->id); } diff --git a/libpolkit/libpolkit-seat.c b/libpolkit/libpolkit-seat.c index 64e7997..aeb0aa3 100644 --- a/libpolkit/libpolkit-seat.c +++ b/libpolkit/libpolkit-seat.c @@ -37,6 +37,7 @@ #include #include +#include "libpolkit-debug.h" #include "libpolkit-seat.h" /** @@ -153,5 +154,5 @@ void libpolkit_seat_debug (PolKitSeat *seat) { g_return_if_fail (seat != NULL); - g_debug ("PolKitSeat: refcount=%d objpath=%s", seat->refcount, seat->ck_objref); + _pk_debug ("PolKitSeat: refcount=%d objpath=%s", seat->refcount, seat->ck_objref); } diff --git a/libpolkit/libpolkit-session.c b/libpolkit/libpolkit-session.c index 8aa526a..0591122 100644 --- a/libpolkit/libpolkit-session.c +++ b/libpolkit/libpolkit-session.c @@ -37,6 +37,7 @@ #include #include +#include "libpolkit-debug.h" #include "libpolkit-session.h" @@ -476,13 +477,13 @@ libpolkit_session_new_from_objpath (DBusConnection *con, const char *objpath, ui dbus_message_unref (reply); } - g_debug ("is_active %d", is_active); - g_debug ("is_local %d", is_local); - g_debug ("uid %d", uid); + _pk_debug ("is_active %d", is_active); + _pk_debug ("is_local %d", is_local); + _pk_debug ("uid %d", uid); if (!is_local) { - g_debug ("remote host '%s'", remote_host); + _pk_debug ("remote host '%s'", remote_host); } - g_debug ("ck seat '%s'", seat_path); + _pk_debug ("ck seat '%s'", seat_path); session = libpolkit_session_new (); libpolkit_session_set_ck_is_active (session, is_active); @@ -573,8 +574,9 @@ void libpolkit_session_debug (PolKitSession *session) { g_return_if_fail (session != NULL); - g_debug ("PolKitSession: refcount=%d objpath=%s is_active=%d is_local=%d remote_host=%s", - session->refcount, session->ck_objref, session->is_active, session->is_local, session->remote_host); + _pk_debug ("PolKitSession: refcount=%d uid=%d objpath=%s is_active=%d is_local=%d remote_host=%s", + session->refcount, session->uid, + session->ck_objref, session->is_active, session->is_local, session->remote_host); if (session->seat != NULL) libpolkit_seat_debug (session->seat); } diff --git a/libpolkit/libpolkit.c b/libpolkit/libpolkit.c index e12ed96..651e54c 100644 --- a/libpolkit/libpolkit.c +++ b/libpolkit/libpolkit.c @@ -45,6 +45,7 @@ #include #include "libpolkit.h" +#include "libpolkit-debug.h" /** * libpolkit_get_seat_resource_association: @@ -128,7 +129,7 @@ libpolkit_can_session_access_resource (PolKitContext *pk_context, if (cache == NULL) goto out; - g_debug ("entering libpolkit_can_session_access_resource()"); + _pk_debug ("entering libpolkit_can_session_access_resource()"); libpolkit_privilege_debug (privilege); libpolkit_resource_debug (resource); libpolkit_session_debug (session); @@ -154,7 +155,7 @@ libpolkit_can_session_access_resource (PolKitContext *pk_context, resource, session); out: - g_debug ("... result was %s", libpolkit_result_to_string_representation (result)); + _pk_debug ("... result was %s", libpolkit_result_to_string_representation (result)); return result; } @@ -186,7 +187,7 @@ libpolkit_can_caller_access_resource (PolKitContext *pk_context, if (cache == NULL) goto out; - g_debug ("entering libpolkit_can_caller_access_resource()"); + _pk_debug ("entering libpolkit_can_caller_access_resource()"); libpolkit_privilege_debug (privilege); libpolkit_resource_debug (resource); libpolkit_caller_debug (caller); @@ -213,6 +214,6 @@ libpolkit_can_caller_access_resource (PolKitContext *pk_context, caller); out: - g_debug ("... result was %s", libpolkit_result_to_string_representation (result)); + _pk_debug ("... result was %s", libpolkit_result_to_string_representation (result)); return result; } -- 2.7.4