From 8bc2caac55572bc06c4fc6c2f6e2b28104c9f69c Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 6 Aug 2009 22:55:25 +0200 Subject: move desktop file parser from bus/ to dbus/ We want to make use of it for reading the ConsoleKit database which will need to be implemented in dbus/dbus-userdb-util.c, so let's move this to dbus/. --- bus/Makefile.am | 4 - bus/activation-helper.c | 61 ++-- bus/activation.c | 464 +++++++++++++-------------- bus/bus.h | 16 +- bus/desktop-file.c | 800 ----------------------------------------------- bus/desktop-file.h | 56 ---- dbus/Makefile.am | 18 +- dbus/dbus-desktop-file.c | 799 ++++++++++++++++++++++++++++++++++++++++++++++ dbus/dbus-desktop-file.h | 49 +++ 9 files changed, 1131 insertions(+), 1136 deletions(-) delete mode 100644 bus/desktop-file.c delete mode 100644 bus/desktop-file.h create mode 100644 dbus/dbus-desktop-file.c create mode 100644 dbus/dbus-desktop-file.h diff --git a/bus/Makefile.am b/bus/Makefile.am index 3b4f69db..8cad76e0 100644 --- a/bus/Makefile.am +++ b/bus/Makefile.am @@ -48,8 +48,6 @@ BUS_SOURCES= \ config-parser-common.h \ connection.c \ connection.h \ - desktop-file.c \ - desktop-file.h \ $(DIR_WATCH_SOURCE) \ dir-watch.h \ dispatch.c \ @@ -89,8 +87,6 @@ LAUNCH_HELPER_SOURCES= \ config-parser-common.h \ config-parser-trivial.c \ config-parser-trivial.h \ - desktop-file.c \ - desktop-file.h \ utils.c \ utils.h \ activation-exit-codes.h \ diff --git a/bus/activation-helper.c b/bus/activation-helper.c index baba8f04..6ce02401 100644 --- a/bus/activation-helper.c +++ b/bus/activation-helper.c @@ -27,7 +27,6 @@ #include "bus.h" #include "driver.h" #include "utils.h" -#include "desktop-file.h" #include "config-parser-trivial.h" #include "activation-helper.h" #include "activation-exit-codes.h" @@ -42,13 +41,14 @@ #include #include +#include -static BusDesktopFile * +static DBusDesktopFile * desktop_file_for_name (BusConfigParser *parser, const char *name, DBusError *error) { - BusDesktopFile *desktop_file; + DBusDesktopFile *desktop_file; DBusList **service_dirs; DBusList *link; DBusError tmp_error; @@ -101,7 +101,7 @@ desktop_file_for_name (BusConfigParser *parser, } _dbus_verbose ("Trying to load file '%s'\n", _dbus_string_get_data (&full_path)); - desktop_file = bus_desktop_file_load (&full_path, &tmp_error); + desktop_file = _dbus_desktop_file_load (&full_path, &tmp_error); if (desktop_file == NULL) { _DBUS_ASSERT_ERROR_IS_SET (&tmp_error); @@ -222,9 +222,9 @@ check_permissions (const char *dbus_user, DBusError *error) } static dbus_bool_t -check_service_name (BusDesktopFile *desktop_file, - const char *service_name, - DBusError *error) +check_service_name (DBusDesktopFile *desktop_file, + const char *service_name, + DBusError *error) { char *name_tmp; dbus_bool_t retval; @@ -232,11 +232,11 @@ check_service_name (BusDesktopFile *desktop_file, retval = FALSE; /* try to get Name */ - if (!bus_desktop_file_get_string (desktop_file, - DBUS_SERVICE_SECTION, - DBUS_SERVICE_NAME, - &name_tmp, - error)) + if (!_dbus_desktop_file_get_string (desktop_file, + DBUS_SERVICE_SECTION, + DBUS_SERVICE_NAME, + &name_tmp, + error)) goto failed; /* verify that the name is the same as the file service name */ @@ -257,11 +257,11 @@ failed: } static dbus_bool_t -get_parameters_for_service (BusDesktopFile *desktop_file, - const char *service_name, - char **exec, - char **user, - DBusError *error) +get_parameters_for_service (DBusDesktopFile *desktop_file, + const char *service_name, + char **exec, + char **user, + DBusError *error) { char *exec_tmp; char *user_tmp; @@ -274,22 +274,22 @@ get_parameters_for_service (BusDesktopFile *desktop_file, goto failed; /* get the complete path of the executable */ - if (!bus_desktop_file_get_string (desktop_file, - DBUS_SERVICE_SECTION, - DBUS_SERVICE_EXEC, - &exec_tmp, - error)) + if (!_dbus_desktop_file_get_string (desktop_file, + DBUS_SERVICE_SECTION, + DBUS_SERVICE_EXEC, + &exec_tmp, + error)) { _DBUS_ASSERT_ERROR_IS_SET (error); goto failed; } /* get the user that should run this service - user is compulsary for system activation */ - if (!bus_desktop_file_get_string (desktop_file, - DBUS_SERVICE_SECTION, - DBUS_SERVICE_USER, - &user_tmp, - error)) + if (!_dbus_desktop_file_get_string (desktop_file, + DBUS_SERVICE_SECTION, + DBUS_SERVICE_USER, + &user_tmp, + error)) { _DBUS_ASSERT_ERROR_IS_SET (error); goto failed; @@ -394,7 +394,7 @@ check_bus_name (const char *bus_name, bus_name); return FALSE; } - + return TRUE; } @@ -468,7 +468,7 @@ out: static dbus_bool_t launch_bus_name (const char *bus_name, BusConfigParser *parser, DBusError *error) { - BusDesktopFile *desktop_file; + DBusDesktopFile *desktop_file; char *exec, *user; dbus_bool_t retval; @@ -498,7 +498,7 @@ launch_bus_name (const char *bus_name, BusConfigParser *parser, DBusError *error finish: dbus_free (exec); dbus_free (user); - bus_desktop_file_free (desktop_file); + _dbus_desktop_file_free (desktop_file); return retval; } @@ -560,4 +560,3 @@ error_free_parser: error: return retval; } - diff --git a/bus/activation.c b/bus/activation.c index 782ffed8..dd5528cd 100644 --- a/bus/activation.c +++ b/bus/activation.c @@ -6,7 +6,7 @@ * Copyright (C) 2004 Imendio HB * * 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 @@ -16,7 +16,7 @@ * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA @@ -24,7 +24,6 @@ */ #include "activation.h" #include "activation-exit-codes.h" -#include "desktop-file.h" #include "services.h" #include "test.h" #include "utils.h" @@ -35,6 +34,7 @@ #include #include #include +#include #ifdef HAVE_ERRNO_H #include #endif @@ -100,7 +100,7 @@ static BusServiceDirectory * bus_service_directory_ref (BusServiceDirectory *dir) { _dbus_assert (dir->refcount); - + dir->refcount++; return dir; @@ -110,8 +110,8 @@ bus_service_directory_ref (BusServiceDirectory *dir) static void bus_service_directory_unref (BusServiceDirectory *dir) { - if (dir == NULL) - return; + if (dir == NULL) + return; _dbus_assert (dir->refcount > 0); dir->refcount--; @@ -131,10 +131,10 @@ bus_pending_activation_entry_free (BusPendingActivationEntry *entry) { if (entry->activation_message) dbus_message_unref (entry->activation_message); - + if (entry->connection) dbus_connection_unref (entry->connection); - + dbus_free (entry); } @@ -148,7 +148,7 @@ handle_timeout_callback (DBusTimeout *timeout, _dbus_wait_for_memory (); } -static BusPendingActivation * +static BusPendingActivation * bus_pending_activation_ref (BusPendingActivation *pending_activation) { _dbus_assert (pending_activation->refcount > 0); @@ -161,7 +161,7 @@ static void bus_pending_activation_unref (BusPendingActivation *pending_activation) { DBusList *link; - + if (pending_activation == NULL) /* hash table requires this */ return; @@ -170,7 +170,7 @@ bus_pending_activation_unref (BusPendingActivation *pending_activation) if (pending_activation->refcount > 0) return; - + if (pending_activation->timeout_added) { _dbus_loop_remove_timeout (bus_context_get_loop (pending_activation->activation->context), @@ -181,7 +181,7 @@ bus_pending_activation_unref (BusPendingActivation *pending_activation) if (pending_activation->timeout) _dbus_timeout_unref (pending_activation->timeout); - + if (pending_activation->babysitter) { if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter, @@ -189,10 +189,10 @@ bus_pending_activation_unref (BusPendingActivation *pending_activation) pending_activation->babysitter, NULL)) _dbus_assert_not_reached ("setting watch functions to NULL failed"); - + _dbus_babysitter_unref (pending_activation->babysitter); } - + dbus_free (pending_activation->service_name); dbus_free (pending_activation->exec); @@ -212,7 +212,7 @@ bus_pending_activation_unref (BusPendingActivation *pending_activation) pending_activation->n_entries; _dbus_assert (pending_activation->activation->n_pending_activations >= 0); - + dbus_free (pending_activation); } @@ -230,13 +230,13 @@ bus_activation_entry_unref (BusActivationEntry *entry) { if (entry == NULL) /* hash table requires this */ return; - + _dbus_assert (entry->refcount > 0); entry->refcount--; - - if (entry->refcount > 0) + + if (entry->refcount > 0) return; - + dbus_free (entry->name); dbus_free (entry->exec); dbus_free (entry->user); @@ -249,7 +249,7 @@ static dbus_bool_t update_desktop_file_entry (BusActivation *activation, BusServiceDirectory *s_dir, DBusString *filename, - BusDesktopFile *desktop_file, + DBusDesktopFile *desktop_file, DBusError *error) { char *name, *exec, *user; @@ -259,12 +259,12 @@ update_desktop_file_entry (BusActivation *activation, DBusError tmp_error; _DBUS_ASSERT_ERROR_IS_CLEAR (error); - + name = NULL; exec = NULL; user = NULL; entry = NULL; - + dbus_error_init (&tmp_error); if (!_dbus_string_init (&file_path)) @@ -272,40 +272,40 @@ update_desktop_file_entry (BusActivation *activation, BUS_SET_OOM (error); return FALSE; } - + if (!_dbus_string_append (&file_path, s_dir->dir_c) || !_dbus_concat_dir_and_file (&file_path, filename)) { BUS_SET_OOM (error); goto failed; } - - if (!_dbus_stat (&file_path, &stat_buf, NULL)) + + if (!_dbus_stat (&file_path, &stat_buf, NULL)) { dbus_set_error (error, DBUS_ERROR_FAILED, "Can't stat the service file\n"); goto failed; } - - if (!bus_desktop_file_get_string (desktop_file, - DBUS_SERVICE_SECTION, - DBUS_SERVICE_NAME, - &name, - error)) + + if (!_dbus_desktop_file_get_string (desktop_file, + DBUS_SERVICE_SECTION, + DBUS_SERVICE_NAME, + &name, + error)) goto failed; - if (!bus_desktop_file_get_string (desktop_file, - DBUS_SERVICE_SECTION, - DBUS_SERVICE_EXEC, - &exec, - error)) + if (!_dbus_desktop_file_get_string (desktop_file, + DBUS_SERVICE_SECTION, + DBUS_SERVICE_EXEC, + &exec, + error)) goto failed; /* user is not _required_ unless we are using system activation */ - if (!bus_desktop_file_get_string (desktop_file, - DBUS_SERVICE_SECTION, - DBUS_SERVICE_USER, - &user, &tmp_error)) + if (!_dbus_desktop_file_get_string (desktop_file, + DBUS_SERVICE_SECTION, + DBUS_SERVICE_USER, + &user, &tmp_error)) { _DBUS_ASSERT_ERROR_IS_SET (&tmp_error); /* if we got OOM, then exit */ @@ -324,10 +324,10 @@ update_desktop_file_entry (BusActivation *activation, } _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error); - entry = _dbus_hash_table_lookup_string (s_dir->entries, + entry = _dbus_hash_table_lookup_string (s_dir->entries, _dbus_string_get_const_data (filename)); if (entry == NULL) /* New file */ - { + { /* FIXME we need a better-defined algorithm for which service file to * pick than "whichever one is first in the directory listing" */ @@ -337,19 +337,19 @@ update_desktop_file_entry (BusActivation *activation, "Service %s already exists in activation entry list\n", name); goto failed; } - + entry = dbus_new0 (BusActivationEntry, 1); if (entry == NULL) { BUS_SET_OOM (error); goto failed; } - + entry->name = name; entry->exec = exec; entry->user = user; entry->refcount = 1; - + entry->s_dir = s_dir; entry->filename = _dbus_strdup (_dbus_string_get_const_data (filename)); if (!entry->filename) @@ -363,7 +363,7 @@ update_desktop_file_entry (BusActivation *activation, BUS_SET_OOM (error); goto failed; } - + if (!_dbus_hash_table_insert_string (s_dir->entries, entry->filename, bus_activation_entry_ref (entry))) { /* Revert the insertion in the entries table */ @@ -385,7 +385,7 @@ update_desktop_file_entry (BusActivation *activation, name, _dbus_string_get_const_data (&file_path)); goto failed; } - + dbus_free (entry->name); dbus_free (entry->exec); dbus_free (entry->user); @@ -398,15 +398,15 @@ update_desktop_file_entry (BusActivation *activation, BUS_SET_OOM (error); /* Also remove path to entries hash since we want this in sync with * the entries hash table */ - _dbus_hash_table_remove_string (entry->s_dir->entries, + _dbus_hash_table_remove_string (entry->s_dir->entries, entry->filename); bus_activation_entry_unref (entry); return FALSE; } } - + entry->mtime = stat_buf.mtime; - + _dbus_string_free (&file_path); bus_activation_entry_unref (entry); @@ -420,7 +420,7 @@ failed: if (entry) bus_activation_entry_unref (entry); - + return FALSE; } @@ -438,15 +438,15 @@ check_service_file (BusActivation *activation, retval = TRUE; tmp_entry = entry; - + _dbus_string_init_const (&filename, entry->filename); - + if (!_dbus_string_init (&file_path)) { BUS_SET_OOM (error); return FALSE; } - + if (!_dbus_string_append (&file_path, entry->s_dir->dir_c) || !_dbus_concat_dir_and_file (&file_path, &filename)) { @@ -454,7 +454,7 @@ check_service_file (BusActivation *activation, retval = FALSE; goto out; } - + if (!_dbus_stat (&file_path, &stat_buf, NULL)) { _dbus_verbose ("****** Can't stat file \"%s\", removing from cache\n", @@ -467,20 +467,20 @@ check_service_file (BusActivation *activation, retval = TRUE; goto out; } - else + else { - if (stat_buf.mtime > entry->mtime) + if (stat_buf.mtime > entry->mtime) { - BusDesktopFile *desktop_file; + DBusDesktopFile *desktop_file; DBusError tmp_error; - + dbus_error_init (&tmp_error); - - desktop_file = bus_desktop_file_load (&file_path, &tmp_error); + + desktop_file = _dbus_desktop_file_load (&file_path, &tmp_error); if (desktop_file == NULL) { _dbus_verbose ("Could not load %s: %s\n", - _dbus_string_get_const_data (&file_path), + _dbus_string_get_const_data (&file_path), tmp_error.message); if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY)) { @@ -492,13 +492,13 @@ check_service_file (BusActivation *activation, retval = TRUE; goto out; } - - /* @todo We can return OOM or a DBUS_ERROR_FAILED error + + /* @todo We can return OOM or a DBUS_ERROR_FAILED error * Handle these both better - */ + */ if (!update_desktop_file_entry (activation, entry->s_dir, &filename, desktop_file, &tmp_error)) { - bus_desktop_file_free (desktop_file); + _dbus_desktop_file_free (desktop_file); if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY)) { dbus_move_error (&tmp_error, error); @@ -509,12 +509,12 @@ check_service_file (BusActivation *activation, retval = TRUE; goto out; } - - bus_desktop_file_free (desktop_file); + + _dbus_desktop_file_free (desktop_file); retval = TRUE; } } - + out: _dbus_string_free (&file_path); @@ -534,19 +534,19 @@ update_directory (BusActivation *activation, { DBusDirIter *iter; DBusString dir, filename; - BusDesktopFile *desktop_file; + DBusDesktopFile *desktop_file; DBusError tmp_error; dbus_bool_t retval; BusActivationEntry *entry; DBusString full_path; - + _DBUS_ASSERT_ERROR_IS_CLEAR (error); - + iter = NULL; desktop_file = NULL; - + _dbus_string_init_const (&dir, s_dir->dir_c); - + if (!_dbus_string_init (&filename)) { BUS_SET_OOM (error); @@ -563,24 +563,24 @@ update_directory (BusActivation *activation, retval = FALSE; /* from this point it's safe to "goto out" */ - + iter = _dbus_directory_open (&dir, error); if (iter == NULL) { _dbus_verbose ("Failed to open directory %s: %s\n", - s_dir->dir_c, + s_dir->dir_c, error ? error->message : "unknown"); goto out; } - + /* Now read the files */ dbus_error_init (&tmp_error); while (_dbus_directory_get_next_file (iter, &filename, &tmp_error)) { _dbus_assert (!dbus_error_is_set (&tmp_error)); - + _dbus_string_set_length (&full_path, 0); - + if (!_dbus_string_ends_with_c_str (&filename, ".service")) { _dbus_verbose ("Skipping non-.service file %s\n", @@ -589,23 +589,23 @@ update_directory (BusActivation *activation, } entry = _dbus_hash_table_lookup_string (s_dir->entries, _dbus_string_get_const_data (&filename)); - if (entry) /* Already has this service file in the cache */ + if (entry) /* Already has this service file in the cache */ { if (!check_service_file (activation, entry, NULL, error)) goto out; continue; } - + if (!_dbus_string_append (&full_path, s_dir->dir_c) || !_dbus_concat_dir_and_file (&full_path, &filename)) { BUS_SET_OOM (error); goto out; } - + /* New file */ - desktop_file = bus_desktop_file_load (&full_path, &tmp_error); + desktop_file = _dbus_desktop_file_load (&full_path, &tmp_error); if (desktop_file == NULL) { _dbus_verbose ("Could not load %s: %s\n", @@ -617,19 +617,19 @@ update_directory (BusActivation *activation, dbus_move_error (&tmp_error, error); goto out; } - + dbus_error_free (&tmp_error); continue; } - /* @todo We can return OOM or a DBUS_ERROR_FAILED error + /* @todo We can return OOM or a DBUS_ERROR_FAILED error * Handle these both better - */ + */ if (!update_desktop_file_entry (activation, s_dir, &filename, desktop_file, &tmp_error)) { - bus_desktop_file_free (desktop_file); + _dbus_desktop_file_free (desktop_file); desktop_file = NULL; - + _dbus_verbose ("Could not add %s to activation entry list: %s\n", _dbus_string_get_const_data (&full_path), tmp_error.message); @@ -644,7 +644,7 @@ update_directory (BusActivation *activation, } else { - bus_desktop_file_free (desktop_file); + _dbus_desktop_file_free (desktop_file); desktop_file = NULL; continue; } @@ -655,7 +655,7 @@ update_directory (BusActivation *activation, dbus_move_error (&tmp_error, error); goto out; } - + retval = TRUE; out: @@ -663,12 +663,12 @@ update_directory (BusActivation *activation, _DBUS_ASSERT_ERROR_IS_SET (error); else _DBUS_ASSERT_ERROR_IS_CLEAR (error); - + if (iter != NULL) _dbus_directory_close (iter); _dbus_string_free (&filename); _dbus_string_free (&full_path); - + return retval; } @@ -744,30 +744,30 @@ bus_activation_new (BusContext *context, BusActivation *activation; DBusList *link; char *dir; - + _DBUS_ASSERT_ERROR_IS_CLEAR (error); - + activation = dbus_new0 (BusActivation, 1); if (activation == NULL) { BUS_SET_OOM (error); return NULL; } - + activation->refcount = 1; activation->context = context; activation->n_pending_activations = 0; - + if (!_dbus_string_copy_data (address, &activation->server_address)) { BUS_SET_OOM (error); goto failed; } - + activation->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL, (DBusFreeFunction)bus_activation_entry_unref); if (activation->entries == NULL) - { + { BUS_SET_OOM (error); goto failed; } @@ -783,26 +783,26 @@ bus_activation_new (BusContext *context, activation->directories = _dbus_hash_table_new (DBUS_HASH_STRING, NULL, (DBusFreeFunction)bus_service_directory_unref); - - if (activation->directories == NULL) + + if (activation->directories == NULL) { BUS_SET_OOM (error); goto failed; } - + /* Load service files */ link = _dbus_list_get_first_link (directories); while (link != NULL) { BusServiceDirectory *s_dir; - + dir = _dbus_strdup ((const char *) link->data); if (!dir) { BUS_SET_OOM (error); goto failed; } - + s_dir = dbus_new0 (BusServiceDirectory, 1); if (!s_dir) { @@ -813,7 +813,7 @@ bus_activation_new (BusContext *context, s_dir->refcount = 1; s_dir->dir_c = dir; - + s_dir->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL, (DBusFreeFunction)bus_activation_entry_unref); @@ -833,8 +833,8 @@ bus_activation_new (BusContext *context, /* only fail on OOM, it is ok if we can't read the directory */ if (!update_directory (activation, s_dir, error)) - { - if (dbus_error_has_name (error, DBUS_ERROR_NO_MEMORY)) + { + if (dbus_error_has_name (error, DBUS_ERROR_NO_MEMORY)) goto failed; else dbus_error_free (error); @@ -846,8 +846,8 @@ bus_activation_new (BusContext *context, activation->environment = _dbus_hash_table_new (DBUS_HASH_STRING, (DBusFreeFunction) dbus_free, (DBusFreeFunction) dbus_free); - - if (activation->environment == NULL) + + if (activation->environment == NULL) { BUS_SET_OOM (error); goto failed; @@ -860,9 +860,9 @@ bus_activation_new (BusContext *context, } return activation; - + failed: - bus_activation_unref (activation); + bus_activation_unref (activation); return NULL; } @@ -870,7 +870,7 @@ BusActivation * bus_activation_ref (BusActivation *activation) { _dbus_assert (activation->refcount > 0); - + activation->refcount += 1; return activation; @@ -885,13 +885,13 @@ bus_activation_unref (BusActivation *activation) if (activation->refcount > 0) return; - + dbus_free (activation->server_address); if (activation->entries) _dbus_hash_table_unref (activation->entries); if (activation->pending_activations) _dbus_hash_table_unref (activation->pending_activations); - if (activation->directories) + if (activation->directories) _dbus_hash_table_unref (activation->directories); if (activation->environment) _dbus_hash_table_unref (activation->environment); @@ -904,13 +904,13 @@ add_bus_environment (BusActivation *activation, DBusError *error) { const char *type; - + if (!bus_activation_set_environment_variable (activation, "DBUS_STARTER_ADDRESS", activation->server_address, error)) return FALSE; - + type = bus_context_get_type (activation->context); if (type != NULL) { @@ -957,13 +957,13 @@ restore_pending (void *data) _dbus_verbose ("Restoring pending activation for service %s, has timeout = %d\n", d->pending_activation->service_name, d->pending_activation->timeout_added); - + _dbus_hash_table_insert_string_preallocated (d->pending_activation->activation->pending_activations, d->hash_entry, d->pending_activation->service_name, d->pending_activation); bus_pending_activation_ref (d->pending_activation); - + d->hash_entry = NULL; } @@ -977,7 +977,7 @@ free_pending_restore_data (void *data) d->hash_entry); bus_pending_activation_unref (d->pending_activation); - + dbus_free (d); } @@ -990,12 +990,12 @@ add_restore_pending_to_transaction (BusTransaction *transaction, d = dbus_new (RestorePendingData, 1); if (d == NULL) return FALSE; - + d->pending_activation = pending_activation; d->hash_entry = _dbus_hash_table_preallocate_entry (d->pending_activation->activation->pending_activations); - + bus_pending_activation_ref (d->pending_activation); - + if (d->hash_entry == NULL || !bus_transaction_add_cancel_hook (transaction, restore_pending, d, free_pending_restore_data)) @@ -1005,7 +1005,7 @@ add_restore_pending_to_transaction (BusTransaction *transaction, } _dbus_verbose ("Saved pending activation to be restored if the transaction fails\n"); - + return TRUE; } @@ -1020,7 +1020,7 @@ bus_activation_service_created (BusActivation *activation, DBusList *link; _DBUS_ASSERT_ERROR_IS_CLEAR (error); - + /* Check if it's a pending activation */ pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name); @@ -1032,14 +1032,14 @@ bus_activation_service_created (BusActivation *activation, { BusPendingActivationEntry *entry = link->data; DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link); - + if (dbus_connection_get_is_connected (entry->connection)) { /* Only send activation replies to regular activation requests. */ if (!entry->auto_activation) { dbus_uint32_t result; - + message = dbus_message_new_method_return (entry->activation_message); if (!message) { @@ -1048,7 +1048,7 @@ bus_activation_service_created (BusActivation *activation, } result = DBUS_START_REPLY_SUCCESS; - + if (!dbus_message_append_args (message, DBUS_TYPE_UINT32, &result, DBUS_TYPE_INVALID)) @@ -1057,18 +1057,18 @@ bus_activation_service_created (BusActivation *activation, BUS_SET_OOM (error); goto error; } - + if (!bus_transaction_send_from_driver (transaction, entry->connection, message)) { dbus_message_unref (message); BUS_SET_OOM (error); goto error; } - + dbus_message_unref (message); } } - + link = next; } @@ -1088,7 +1088,7 @@ bus_activation_send_pending_auto_activation_messages (BusActivation *activation DBusList *link; _DBUS_ASSERT_ERROR_IS_CLEAR (error); - + /* Check if it's a pending activation */ pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, bus_service_get_name (service)); @@ -1105,7 +1105,7 @@ bus_activation_send_pending_auto_activation_messages (BusActivation *activation if (entry->auto_activation && dbus_connection_get_is_connected (entry->connection)) { DBusConnection *addressed_recipient; - + addressed_recipient = bus_service_get_primary_owners_connection (service); /* Check the security policy, which has the side-effect of adding an @@ -1134,7 +1134,7 @@ bus_activation_send_pending_auto_activation_messages (BusActivation *activation BUS_SET_OOM (error); goto error; } - + _dbus_hash_table_remove_string (activation->pending_activations, bus_service_get_name (service)); return TRUE; @@ -1156,19 +1156,19 @@ try_send_activation_failure (BusPendingActivation *pending_activation, BusActivation *activation; DBusList *link; BusTransaction *transaction; - + activation = pending_activation->activation; transaction = bus_transaction_new (activation->context); if (transaction == NULL) return FALSE; - + link = _dbus_list_get_first_link (&pending_activation->entries); while (link != NULL) { BusPendingActivationEntry *entry = link->data; DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link); - + if (dbus_connection_get_is_connected (entry->connection)) { if (!bus_transaction_send_error_reply (transaction, @@ -1177,12 +1177,12 @@ try_send_activation_failure (BusPendingActivation *pending_activation, entry->activation_message)) goto error; } - + link = next; } bus_transaction_execute_and_free (transaction); - + return TRUE; error: @@ -1270,9 +1270,9 @@ babysitter_watch_callback (DBusWatch *watch, DBusBabysitter *babysitter; babysitter = pending_activation->babysitter; - + _dbus_babysitter_ref (babysitter); - + retval = dbus_watch_handle (watch, condition); /* FIXME this is broken in the same way that @@ -1284,12 +1284,12 @@ babysitter_watch_callback (DBusWatch *watch, * Fixing this lets us move dbus_watch_handle * calls into dbus-mainloop.c */ - + if (_dbus_babysitter_get_child_exited (babysitter)) { DBusError error; DBusHashIter iter; - + dbus_error_init (&error); _dbus_babysitter_set_child_exit_error (babysitter, &error); @@ -1310,17 +1310,17 @@ babysitter_watch_callback (DBusWatch *watch, while (_dbus_hash_iter_next (&iter)) { BusPendingActivation *p = _dbus_hash_iter_get_value (&iter); - + if (p != pending_activation && strcmp (p->exec, pending_activation->exec) == 0) pending_activation_failed (p, &error); } - + /* Destroys the pending activation */ pending_activation_failed (pending_activation, &error); dbus_error_free (&error); } - + _dbus_babysitter_unref (babysitter); return retval; @@ -1342,7 +1342,7 @@ remove_babysitter_watch (DBusWatch *watch, void *data) { BusPendingActivation *pending_activation = data; - + _dbus_loop_remove_watch (bus_context_get_loop (pending_activation->activation->context), watch, babysitter_watch_callback, pending_activation); } @@ -1352,12 +1352,12 @@ pending_activation_timed_out (void *data) { BusPendingActivation *pending_activation = data; DBusError error; - + /* Kill the spawned process, since it sucks * (not sure this is what we want to do, but * may as well try it for now) */ - if (pending_activation->babysitter) + if (pending_activation->babysitter) _dbus_babysitter_kill_child (pending_activation->babysitter); dbus_error_init (&error); @@ -1383,7 +1383,7 @@ cancel_pending (void *data) if (pending_activation->babysitter) _dbus_babysitter_kill_child (pending_activation->babysitter); - + _dbus_hash_table_remove_string (pending_activation->activation->pending_activations, pending_activation->service_name); } @@ -1392,31 +1392,31 @@ static void free_pending_cancel_data (void *data) { BusPendingActivation *pending_activation = data; - + bus_pending_activation_unref (pending_activation); } static dbus_bool_t add_cancel_pending_to_transaction (BusTransaction *transaction, BusPendingActivation *pending_activation) -{ +{ if (!bus_transaction_add_cancel_hook (transaction, cancel_pending, pending_activation, free_pending_cancel_data)) return FALSE; - bus_pending_activation_ref (pending_activation); - + bus_pending_activation_ref (pending_activation); + _dbus_verbose ("Saved pending activation to be canceled if the transaction fails\n"); - + return TRUE; } -static dbus_bool_t +static dbus_bool_t update_service_cache (BusActivation *activation, DBusError *error) { DBusHashIter iter; - + _dbus_hash_iter_init (activation->directories, &iter); while (_dbus_hash_iter_next (&iter)) { @@ -1438,37 +1438,37 @@ update_service_cache (BusActivation *activation, DBusError *error) continue; } } - + return TRUE; } static BusActivationEntry * -activation_find_entry (BusActivation *activation, +activation_find_entry (BusActivation *activation, const char *service_name, DBusError *error) { BusActivationEntry *entry; - + entry = _dbus_hash_table_lookup_string (activation->entries, service_name); if (!entry) - { - if (!update_service_cache (activation, error)) + { + if (!update_service_cache (activation, error)) return NULL; entry = _dbus_hash_table_lookup_string (activation->entries, service_name); } - else + else { BusActivationEntry *updated_entry; - if (!check_service_file (activation, entry, &updated_entry, error)) + if (!check_service_file (activation, entry, &updated_entry, error)) return NULL; entry = updated_entry; } - if (!entry) + if (!entry) { dbus_set_error (error, DBUS_ERROR_SERVICE_UNKNOWN, "The name %s was not provided by any .service files", @@ -1590,7 +1590,7 @@ bus_activation_activate_service (BusActivation *activation, DBusHashIter iter; dbus_bool_t activated; DBusString command; - + activated = TRUE; _DBUS_ASSERT_ERROR_IS_CLEAR (error); @@ -1605,7 +1605,7 @@ bus_activation_activate_service (BusActivation *activation, } entry = activation_find_entry (activation, service_name, error); - if (!entry) + if (!entry) return FALSE; /* Bypass the registry lookup if we're auto-activating, bus_dispatch would not @@ -1618,9 +1618,9 @@ bus_activation_activate_service (BusActivation *activation, if (bus_registry_lookup (bus_context_get_registry (activation->context), &service_str) != NULL) { dbus_uint32_t result; - + _dbus_verbose ("Service \"%s\" is already active\n", service_name); - + message = dbus_message_new_method_return (activation_message); if (!message) @@ -1631,7 +1631,7 @@ bus_activation_activate_service (BusActivation *activation, } result = DBUS_START_REPLY_ALREADY_RUNNING; - + if (!dbus_message_append_args (message, DBUS_TYPE_UINT32, &result, DBUS_TYPE_INVALID)) @@ -1653,7 +1653,7 @@ bus_activation_activate_service (BusActivation *activation, return retval; } } - + pending_activation_entry = dbus_new0 (BusPendingActivationEntry, 1); if (!pending_activation_entry) { @@ -1668,7 +1668,7 @@ bus_activation_activate_service (BusActivation *activation, dbus_message_ref (activation_message); pending_activation_entry->connection = connection; dbus_connection_ref (connection); - + /* Check if the service is being activated */ pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name); if (pending_activation) @@ -1676,7 +1676,7 @@ bus_activation_activate_service (BusActivation *activation, if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry)) { _dbus_verbose ("Failed to append a new entry to pending activation\n"); - + BUS_SET_OOM (error); bus_pending_activation_entry_free (pending_activation_entry); return FALSE; @@ -1691,23 +1691,23 @@ bus_activation_activate_service (BusActivation *activation, if (!pending_activation) { _dbus_verbose ("Failed to create pending activation\n"); - + BUS_SET_OOM (error); - bus_pending_activation_entry_free (pending_activation_entry); + bus_pending_activation_entry_free (pending_activation_entry); return FALSE; } pending_activation->activation = activation; pending_activation->refcount = 1; - + pending_activation->service_name = _dbus_strdup (service_name); if (!pending_activation->service_name) { _dbus_verbose ("Failed to copy service name for pending activation\n"); - + BUS_SET_OOM (error); bus_pending_activation_unref (pending_activation); - bus_pending_activation_entry_free (pending_activation_entry); + bus_pending_activation_entry_free (pending_activation_entry); return FALSE; } @@ -1729,7 +1729,7 @@ bus_activation_activate_service (BusActivation *activation, if (!pending_activation->timeout) { _dbus_verbose ("Failed to create timeout for pending activation\n"); - + BUS_SET_OOM (error); bus_pending_activation_unref (pending_activation); bus_pending_activation_entry_free (pending_activation_entry); @@ -1743,53 +1743,53 @@ bus_activation_activate_service (BusActivation *activation, NULL)) { _dbus_verbose ("Failed to add timeout for pending activation\n"); - + BUS_SET_OOM (error); bus_pending_activation_unref (pending_activation); - bus_pending_activation_entry_free (pending_activation_entry); + bus_pending_activation_entry_free (pending_activation_entry); return FALSE; } pending_activation->timeout_added = TRUE; - + if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry)) { _dbus_verbose ("Failed to add entry to just-created pending activation\n"); - + BUS_SET_OOM (error); bus_pending_activation_unref (pending_activation); - bus_pending_activation_entry_free (pending_activation_entry); + bus_pending_activation_entry_free (pending_activation_entry); return FALSE; } pending_activation->n_entries += 1; pending_activation->activation->n_pending_activations += 1; - + activated = FALSE; _dbus_hash_iter_init (activation->pending_activations, &iter); while (_dbus_hash_iter_next (&iter)) { BusPendingActivation *p = _dbus_hash_iter_get_value (&iter); - - if (strcmp (p->exec, entry->exec) == 0) + + if (strcmp (p->exec, entry->exec) == 0) { activated = TRUE; break; } } - + if (!_dbus_hash_table_insert_string (activation->pending_activations, pending_activation->service_name, pending_activation)) { _dbus_verbose ("Failed to put pending activation in hash table\n"); - + BUS_SET_OOM (error); bus_pending_activation_unref (pending_activation); return FALSE; } } - + if (!add_cancel_pending_to_transaction (transaction, pending_activation)) { _dbus_verbose ("Failed to add pending activation cancel hook to transaction\n"); @@ -1799,7 +1799,7 @@ bus_activation_activate_service (BusActivation *activation, return FALSE; } - + if (activated) return TRUE; @@ -1858,7 +1858,7 @@ bus_activation_activate_service (BusActivation *activation, { _dbus_verbose ("Failed to parse command line: %s\n", entry->exec); _DBUS_ASSERT_ERROR_IS_SET (error); - + _dbus_hash_table_remove_string (activation->pending_activations, pending_activation->service_name); @@ -1886,7 +1886,7 @@ bus_activation_activate_service (BusActivation *activation, _dbus_verbose ("Spawning %s ...\n", argv[0]); if (!_dbus_spawn_async_with_babysitter (&pending_activation->babysitter, argv, envp, - NULL, activation, + NULL, activation, error)) { _dbus_verbose ("Failed to spawn child\n"); @@ -1901,7 +1901,7 @@ bus_activation_activate_service (BusActivation *activation, envp = NULL; _dbus_assert (pending_activation->babysitter != NULL); - + if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter, add_babysitter_watch, remove_babysitter_watch, @@ -1913,7 +1913,7 @@ bus_activation_activate_service (BusActivation *activation, _dbus_verbose ("Failed to set babysitter watch functions\n"); return FALSE; } - + return TRUE; } @@ -1960,7 +1960,7 @@ bus_activation_list_services (BusActivation *activation, return FALSE; } - + #ifdef DBUS_BUILD_TESTS @@ -1976,8 +1976,8 @@ bus_activation_list_services (BusActivation *activation, static dbus_bool_t test_create_service_file (DBusString *dir, - const char *filename, - const char *name, + const char *filename, + const char *name, const char *exec) { DBusString file_name, full_path; @@ -1996,7 +1996,7 @@ test_create_service_file (DBusString *dir, ret_val = FALSE; goto out; } - + file = fopen (_dbus_string_get_const_data (&full_path), "w"); if (!file) { @@ -2017,9 +2017,9 @@ test_remove_service_file (DBusString *dir, const char *filename) { DBusString file_name, full_path; dbus_bool_t ret_val; - + ret_val = TRUE; - + _dbus_string_init_const (&file_name, filename); if (!_dbus_string_init (&full_path)) @@ -2049,9 +2049,9 @@ test_remove_directory (DBusString *dir) DBusDirIter *iter; DBusString filename, full_path; dbus_bool_t ret_val; - + ret_val = TRUE; - + if (!_dbus_string_init (&filename)) return FALSE; @@ -2060,15 +2060,15 @@ test_remove_directory (DBusString *dir) _dbus_string_free (&filename); return FALSE; } - + iter = _dbus_directory_open (dir, NULL); if (iter == NULL) { ret_val = FALSE; goto out; } - - while (_dbus_directory_get_next_file (iter, &filename, NULL)) + + while (_dbus_directory_get_next_file (iter, &filename, NULL)) { if (!test_remove_service_file (dir, _dbus_string_get_const_data (&filename))) { @@ -2095,13 +2095,13 @@ static dbus_bool_t init_service_reload_test (DBusString *dir) { DBusStat stat_buf; - + if (!_dbus_stat (dir, &stat_buf, NULL)) { if (!_dbus_create_directory (dir, NULL)) return FALSE; } - else + else { if (!test_remove_directory (dir)) return FALSE; @@ -2126,7 +2126,7 @@ cleanup_service_reload_test (DBusString *dir) return TRUE; } -typedef struct +typedef struct { BusActivation *activation; const char *service_name; @@ -2140,16 +2140,16 @@ check_func (void *data) BusActivationEntry *entry; DBusError error; dbus_bool_t ret_val; - + ret_val = TRUE; d = data; - + dbus_error_init (&error); - + entry = activation_find_entry (d->activation, d->service_name, &error); if (entry == NULL) { - if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY)) + if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY)) { ret_val = TRUE; } @@ -2158,10 +2158,10 @@ check_func (void *data) if (d->expecting_find) ret_val = FALSE; } - + dbus_error_free (&error); } - else + else { if (!d->expecting_find) ret_val = FALSE; @@ -2180,7 +2180,7 @@ do_test (const char *description, dbus_bool_t oom_test, CheckData *data) else err = !check_func (data); - if (err) + if (err) _dbus_assert_not_reached ("Test failed"); return TRUE; @@ -2193,19 +2193,19 @@ do_service_reload_test (DBusString *dir, dbus_bool_t oom_test) DBusString address; DBusList *directories; CheckData d; - + directories = NULL; _dbus_string_init_const (&address, ""); - + if (!_dbus_list_append (&directories, _dbus_string_get_data (dir))) - return FALSE; + return FALSE; activation = bus_activation_new (NULL, &address, &directories, NULL); if (!activation) return FALSE; d.activation = activation; - + /* Check for existing service file */ d.expecting_find = TRUE; d.service_name = SERVICE_NAME_1; @@ -2226,10 +2226,10 @@ do_service_reload_test (DBusString *dir, dbus_bool_t oom_test) d.expecting_find = TRUE; d.service_name = SERVICE_NAME_2; - + if (!do_test ("Added service file", oom_test, &d)) return FALSE; - + /* Check for removed service file */ if (!test_remove_service_file (dir, SERVICE_FILE_2)) return FALSE; @@ -2239,9 +2239,9 @@ do_service_reload_test (DBusString *dir, dbus_bool_t oom_test) if (!do_test ("Removed service file", oom_test, &d)) return FALSE; - + /* Check for updated service file */ - + _dbus_sleep_milliseconds (1000); /* Sleep a second to make sure the mtime is updated */ if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_3, "exec-3")) @@ -2257,7 +2257,7 @@ do_service_reload_test (DBusString *dir, dbus_bool_t oom_test) d.service_name = SERVICE_NAME_1; if (!do_test ("Updated service file, part 2", oom_test, &d)) - return FALSE; + return FALSE; bus_activation_unref (activation); _dbus_list_clear (&directories); @@ -2272,36 +2272,36 @@ bus_activation_service_reload_test (const DBusString *test_data_dir) if (!_dbus_string_init (&directory)) return FALSE; - + if (!_dbus_string_append (&directory, _dbus_get_tmpdir())) return FALSE; - + if (!_dbus_string_append (&directory, "/dbus-reload-test-") || !_dbus_generate_random_ascii (&directory, 6)) { return FALSE; } - + /* Do normal tests */ if (!init_service_reload_test (&directory)) _dbus_assert_not_reached ("could not initiate service reload test"); - + if (!do_service_reload_test (&directory, FALSE)) ; /* Do nothing? */ - + /* Do OOM tests */ if (!init_service_reload_test (&directory)) _dbus_assert_not_reached ("could not initiate service reload test"); - + if (!do_service_reload_test (&directory, TRUE)) ; /* Do nothing? */ - + /* Cleanup test directory */ if (!cleanup_service_reload_test (&directory)) return FALSE; - + _dbus_string_free (&directory); - + return TRUE; } diff --git a/bus/bus.h b/bus/bus.h index 94c64c7e..09c7f7af 100644 --- a/bus/bus.h +++ b/bus/bus.h @@ -4,7 +4,7 @@ * Copyright (C) 2003 Red Hat, Inc. * * 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 @@ -14,7 +14,7 @@ * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA @@ -110,11 +110,11 @@ int bus_context_get_max_services_per_connection (BusContext int bus_context_get_max_match_rules_per_connection (BusContext *context); int bus_context_get_max_replies_per_connection (BusContext *context); int bus_context_get_reply_timeout (BusContext *context); -void bus_context_log_info (BusContext *context, - const char *msg, +void bus_context_log_info (BusContext *context, + const char *msg, ...); void bus_context_log_security (BusContext *context, - const char *msg, + const char *msg, ...); dbus_bool_t bus_context_check_security_policy (BusContext *context, BusTransaction *transaction, @@ -124,4 +124,10 @@ dbus_bool_t bus_context_check_security_policy (BusContext DBusMessage *message, DBusError *error); +#define DBUS_SERVICE_SECTION "D-BUS Service" +#define DBUS_SERVICE_NAME "Name" +#define DBUS_SERVICE_EXEC "Exec" +#define DBUS_SERVICE_USER "User" +#define DBUS_SERVICE_GROUP "Group" + #endif /* BUS_BUS_H */ diff --git a/bus/desktop-file.c b/bus/desktop-file.c deleted file mode 100644 index 9f88d6e7..00000000 --- a/bus/desktop-file.c +++ /dev/null @@ -1,800 +0,0 @@ -/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ -/* desktop-file.c .desktop file parser - * - * Copyright (C) 2003 CodeFactory AB - * Copyright (C) 2003 Red Hat Inc. - * - * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA - * - */ -#include -#include -#include "desktop-file.h" -#include "utils.h" - -typedef struct -{ - char *key; - char *value; -} BusDesktopFileLine; - -typedef struct -{ - char *section_name; - - int n_lines; - BusDesktopFileLine *lines; - int n_allocated_lines; -} BusDesktopFileSection; - -struct BusDesktopFile -{ - int n_sections; - BusDesktopFileSection *sections; - int n_allocated_sections; -}; - -/** - * Parser for service files. - */ -typedef struct -{ - DBusString data; /**< The data from the file */ - - BusDesktopFile *desktop_file; /**< The resulting object */ - int current_section; /**< The current section being parsed */ - - int pos; /**< Current position */ - int len; /**< Length */ - int line_num; /**< Current line number */ - -} BusDesktopFileParser; - -#define VALID_KEY_CHAR 1 -#define VALID_LOCALE_CHAR 2 -static unsigned char valid[256] = { - 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , - 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , - 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x3 , 0x2 , 0x0 , - 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , - 0x0 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , - 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x0 , 0x0 , 0x0 , 0x0 , 0x2 , - 0x0 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , - 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , - 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , - 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , - 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , - 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , - 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , - 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , - 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , - 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , -}; - -static void report_error (BusDesktopFileParser *parser, - char *message, - const char *error_name, - DBusError *error); - -static void -parser_free (BusDesktopFileParser *parser) -{ - bus_desktop_file_free (parser->desktop_file); - - _dbus_string_free (&parser->data); -} - -static void -bus_desktop_file_line_free (BusDesktopFileLine *line) -{ - dbus_free (line->key); - dbus_free (line->value); -} - -static void -bus_desktop_file_section_free (BusDesktopFileSection *section) -{ - int i; - - for (i = 0; i < section->n_lines; i++) - bus_desktop_file_line_free (§ion->lines[i]); - - dbus_free (section->lines); - dbus_free (section->section_name); -} - -void -bus_desktop_file_free (BusDesktopFile *desktop_file) -{ - int i; - - for (i = 0; i < desktop_file->n_sections; i++) - bus_desktop_file_section_free (&desktop_file->sections[i]); - dbus_free (desktop_file->sections); - - dbus_free (desktop_file); -} - -static dbus_bool_t -grow_lines_in_section (BusDesktopFileSection *section) -{ - BusDesktopFileLine *lines; - - int new_n_lines; - - if (section->n_allocated_lines == 0) - new_n_lines = 1; - else - new_n_lines = section->n_allocated_lines*2; - - lines = dbus_realloc (section->lines, - sizeof (BusDesktopFileLine) * new_n_lines); - - if (lines == NULL) - return FALSE; - - section->lines = lines; - section->n_allocated_lines = new_n_lines; - - return TRUE; -} - -static dbus_bool_t -grow_sections (BusDesktopFile *desktop_file) -{ - int new_n_sections; - BusDesktopFileSection *sections; - - if (desktop_file->n_allocated_sections == 0) - new_n_sections = 1; - else - new_n_sections = desktop_file->n_allocated_sections*2; - - sections = dbus_realloc (desktop_file->sections, - sizeof (BusDesktopFileSection) * new_n_sections); - if (sections == NULL) - return FALSE; - - desktop_file->sections = sections; - - desktop_file->n_allocated_sections = new_n_sections; - - return TRUE; -} - -static char * -unescape_string (BusDesktopFileParser *parser, - const DBusString *str, - int pos, - int end_pos, - DBusError *error) -{ - char *retval, *q; - - _DBUS_ASSERT_ERROR_IS_CLEAR (error); - - /* len + 1 is enough, because unescaping never makes the - * string longer - */ - retval = dbus_malloc (end_pos - pos + 1); - if (retval == NULL) - { - BUS_SET_OOM (error); - return NULL; - } - - q = retval; - - while (pos < end_pos) - { - if (_dbus_string_get_byte (str, pos) == 0) - { - /* Found an embedded null */ - dbus_free (retval); - report_error (parser, "Text to be unescaped contains embedded nul", - BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, error); - return NULL; - } - - if (_dbus_string_get_byte (str, pos) == '\\') - { - pos ++; - - if (pos >= end_pos) - { - /* Escape at end of string */ - dbus_free (retval); - report_error (parser, "Text to be unescaped ended in \\", - BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, error); - return NULL; - } - - switch (_dbus_string_get_byte (str, pos)) - { - case 's': - *q++ = ' '; - break; - case 't': - *q++ = '\t'; - break; - case 'n': - *q++ = '\n'; - break; - case 'r': - *q++ = '\r'; - break; - case '\\': - *q++ = '\\'; - break; - default: - /* Invalid escape code */ - dbus_free (retval); - report_error (parser, "Text to be unescaped had invalid escape sequence", - BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, error); - return NULL; - } - pos++; - } - else - { - *q++ =_dbus_string_get_byte (str, pos); - - pos++; - } - } - - *q = 0; - - return retval; -} - -static BusDesktopFileSection* -new_section (BusDesktopFile *desktop_file, - const char *name) -{ - int n; - char *name_copy; - - if (desktop_file->n_allocated_sections == desktop_file->n_sections) - { - if (!grow_sections (desktop_file)) - return NULL; - } - - name_copy = _dbus_strdup (name); - if (name_copy == NULL) - return NULL; - - n = desktop_file->n_sections; - desktop_file->sections[n].section_name = name_copy; - - desktop_file->sections[n].n_lines = 0; - desktop_file->sections[n].lines = NULL; - desktop_file->sections[n].n_allocated_lines = 0; - - if (!grow_lines_in_section (&desktop_file->sections[n])) - { - dbus_free (desktop_file->sections[n].section_name); - desktop_file->sections[n].section_name = NULL; - return NULL; - } - - desktop_file->n_sections += 1; - - return &desktop_file->sections[n]; -} - -static BusDesktopFileSection* -open_section (BusDesktopFileParser *parser, - char *name) -{ - BusDesktopFileSection *section; - - section = new_section (parser->desktop_file, name); - if (section == NULL) - return NULL; - - parser->current_section = parser->desktop_file->n_sections - 1; - _dbus_assert (&parser->desktop_file->sections[parser->current_section] == section); - - return section; -} - -static BusDesktopFileLine * -new_line (BusDesktopFileParser *parser) -{ - BusDesktopFileSection *section; - BusDesktopFileLine *line; - - section = &parser->desktop_file->sections[parser->current_section]; - - if (section->n_allocated_lines == section->n_lines) - { - if (!grow_lines_in_section (section)) - return NULL; - } - - line = §ion->lines[section->n_lines++]; - - _DBUS_ZERO(*line); - - return line; -} - -static dbus_bool_t -is_blank_line (BusDesktopFileParser *parser) -{ - int p; - char c; - - p = parser->pos; - - c = _dbus_string_get_byte (&parser->data, p); - - while (c && c != '\n') - { - if (!(c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f')) - return FALSE; - - p++; - c = _dbus_string_get_byte (&parser->data, p); - } - - return TRUE; -} - -static void -parse_comment_or_blank (BusDesktopFileParser *parser) -{ - int line_end, eol_len; - - if (!_dbus_string_find_eol (&parser->data, parser->pos, &line_end, &eol_len)) - line_end = parser->len; - - if (line_end == parser->len) - parser->pos = parser->len; - else - parser->pos = line_end + eol_len; - - parser->line_num += 1; -} - -static dbus_bool_t -is_valid_section_name (const char *name) -{ - /* 5. Group names may contain all ASCII characters except for control characters and '[' and ']'. */ - - while (*name) - { - if (!((*name >= 'A' && *name <= 'Z') || (*name >= 'a' || *name <= 'z') || - *name == '\n' || *name == '\t')) - return FALSE; - - name++; - } - - return TRUE; -} - -static dbus_bool_t -parse_section_start (BusDesktopFileParser *parser, DBusError *error) -{ - int line_end, eol_len; - char *section_name; - - _DBUS_ASSERT_ERROR_IS_CLEAR (error); - - if (!_dbus_string_find_eol (&parser->data, parser->pos, &line_end, &eol_len)) - line_end = parser->len; - - if (line_end - parser->pos <= 2 || - _dbus_string_get_byte (&parser->data, line_end - 1) != ']') - { - report_error (parser, "Invalid syntax for section header", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, error); - parser_free (parser); - return FALSE; - } - - section_name = unescape_string (parser, - &parser->data, parser->pos + 1, line_end - 1, - error); - - if (section_name == NULL) - { - parser_free (parser); - return FALSE; - } - - if (!is_valid_section_name (section_name)) - { - report_error (parser, "Invalid characters in section name", BUS_DESKTOP_PARSE_ERROR_INVALID_CHARS, error); - parser_free (parser); - dbus_free (section_name); - return FALSE; - } - - if (open_section (parser, section_name) == NULL) - { - dbus_free (section_name); - parser_free (parser); - BUS_SET_OOM (error); - return FALSE; - } - - if (line_end == parser->len) - parser->pos = parser->len; - else - parser->pos = line_end + eol_len; - - parser->line_num += 1; - - dbus_free (section_name); - - return TRUE; -} - -static dbus_bool_t -parse_key_value (BusDesktopFileParser *parser, DBusError *error) -{ - int line_end, eol_len; - int key_start, key_end; - int value_start; - int p; - char *value, *tmp; - DBusString key; - BusDesktopFileLine *line; - - _DBUS_ASSERT_ERROR_IS_CLEAR (error); - - if (!_dbus_string_find_eol (&parser->data, parser->pos, &line_end, &eol_len)) - line_end = parser->len; - - p = parser->pos; - key_start = p; - while (p < line_end && - (valid[_dbus_string_get_byte (&parser->data, p)] & VALID_KEY_CHAR)) - p++; - key_end = p; - - if (key_start == key_end) - { - report_error (parser, "Empty key name", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, error); - parser_free (parser); - return FALSE; - } - - /* We ignore locales for now */ - if (p < line_end && _dbus_string_get_byte (&parser->data, p) == '[') - { - if (line_end == parser->len) - parser->pos = parser->len; - else - parser->pos = line_end + eol_len; - - parser->line_num += 1; - - return TRUE; - } - - /* Skip space before '=' */ - while (p < line_end && _dbus_string_get_byte (&parser->data, p) == ' ') - p++; - - if (p < line_end && _dbus_string_get_byte (&parser->data, p) != '=') - { - report_error (parser, "Invalid characters in key name", BUS_DESKTOP_PARSE_ERROR_INVALID_CHARS, error); - parser_free (parser); - return FALSE; - } - - if (p == line_end) - { - report_error (parser, "No '=' in key/value pair", BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, error); - parser_free (parser); - return FALSE; - } - - /* Skip the '=' */ - p++; - - /* Skip space after '=' */ - while (p < line_end && _dbus_string_get_byte (&parser->data, p) == ' ') - p++; - - value_start = p; - - value = unescape_string (parser, &parser->data, value_start, line_end, error); - if (value == NULL) - { - parser_free (parser); - return FALSE; - } - - line = new_line (parser); - if (line == NULL) - { - dbus_free (value); - parser_free (parser); - BUS_SET_OOM (error); - return FALSE; - } - - if (!_dbus_string_init (&key)) - { - dbus_free (value); - parser_free (parser); - BUS_SET_OOM (error); - return FALSE; - } - - if (!_dbus_string_copy_len (&parser->data, key_start, key_end - key_start, - &key, 0)) - { - _dbus_string_free (&key); - dbus_free (value); - parser_free (parser); - BUS_SET_OOM (error); - return FALSE; - } - - if (!_dbus_string_steal_data (&key, &tmp)) - { - _dbus_string_free (&key); - dbus_free (value); - parser_free (parser); - BUS_SET_OOM (error); - return FALSE; - } - - _dbus_string_free (&key); - - line->key = tmp; - line->value = value; - - if (line_end == parser->len) - parser->pos = parser->len; - else - parser->pos = line_end + eol_len; - - parser->line_num += 1; - - return TRUE; -} - -static void -report_error (BusDesktopFileParser *parser, - char *message, - const char *error_name, - DBusError *error) -{ - const char *section_name = NULL; - - _DBUS_ASSERT_ERROR_IS_CLEAR (error); - - if (parser->current_section != -1) - section_name = parser->desktop_file->sections[parser->current_section].section_name; - - if (section_name) - dbus_set_error (error, error_name, - "Error in section %s at line %d: %s\n", section_name, parser->line_num, message); - else - dbus_set_error (error, error_name, - "Error at line %d: %s\n", parser->line_num, message); -} - -#if 0 -static void -dump_desktop_file (BusDesktopFile *file) -{ - int i; - - for (i = 0; i < file->n_sections; i++) - { - int j; - - printf ("[%s]\n", file->sections[i].section_name); - - for (j = 0; j < file->sections[i].n_lines; j++) - { - printf ("%s=%s\n", file->sections[i].lines[j].key, - file->sections[i].lines[j].value); - } - } -} -#endif - -BusDesktopFile* -bus_desktop_file_load (DBusString *filename, - DBusError *error) -{ - DBusString str; - BusDesktopFileParser parser; - DBusStat sb; - - _DBUS_ASSERT_ERROR_IS_CLEAR (error); - - /* Clearly there's a race here, but it's just to make it unlikely - * that we do something silly, we still handle doing it below. - */ - if (!_dbus_stat (filename, &sb, error)) - return NULL; - - if (sb.size > _DBUS_ONE_KILOBYTE * 128) - { - dbus_set_error (error, DBUS_ERROR_FAILED, - "Desktop file size (%ld bytes) is too large", (long) sb.size); - return NULL; - } - - if (!_dbus_string_init (&str)) - { - BUS_SET_OOM (error); - return NULL; - } - - if (!_dbus_file_get_contents (&str, filename, error)) - { - _dbus_string_free (&str); - return NULL; - } - - if (!_dbus_string_validate_utf8 (&str, 0, _dbus_string_get_length (&str))) - { - _dbus_string_free (&str); - dbus_set_error (error, DBUS_ERROR_FAILED, - "invalid UTF-8"); - return NULL; - } - - parser.desktop_file = dbus_new0 (BusDesktopFile, 1); - if (parser.desktop_file == NULL) - { - _dbus_string_free (&str); - BUS_SET_OOM (error); - return NULL; - } - - parser.data = str; - parser.line_num = 1; - parser.pos = 0; - parser.len = _dbus_string_get_length (&parser.data); - parser.current_section = -1; - - while (parser.pos < parser.len) - { - if (_dbus_string_get_byte (&parser.data, parser.pos) == '[') - { - if (!parse_section_start (&parser, error)) - { - return NULL; - } - } - else if (is_blank_line (&parser) || - _dbus_string_get_byte (&parser.data, parser.pos) == '#') - parse_comment_or_blank (&parser); - else - { - if (!parse_key_value (&parser, error)) - { - return NULL; - } - } - } - - _dbus_string_free (&parser.data); - - return parser.desktop_file; -} - -static BusDesktopFileSection * -lookup_section (BusDesktopFile *desktop_file, - const char *section_name) -{ - BusDesktopFileSection *section; - int i; - - if (section_name == NULL) - return NULL; - - for (i = 0; i < desktop_file->n_sections; i ++) - { - section = &desktop_file->sections[i]; - - if (strcmp (section->section_name, section_name) == 0) - return section; - } - - return NULL; -} - -static BusDesktopFileLine * -lookup_line (BusDesktopFile *desktop_file, - BusDesktopFileSection *section, - const char *keyname) -{ - BusDesktopFileLine *line; - int i; - - for (i = 0; i < section->n_lines; i++) - { - line = §ion->lines[i]; - - if (strcmp (line->key, keyname) == 0) - return line; - } - - return NULL; -} - -dbus_bool_t -bus_desktop_file_get_raw (BusDesktopFile *desktop_file, - const char *section_name, - const char *keyname, - const char **val) -{ - BusDesktopFileSection *section; - BusDesktopFileLine *line; - - *val = NULL; - - section = lookup_section (desktop_file, section_name); - - if (!section) - return FALSE; - - line = lookup_line (desktop_file, - section, - keyname); - - if (!line) - return FALSE; - - *val = line->value; - - return TRUE; -} - -dbus_bool_t -bus_desktop_file_get_string (BusDesktopFile *desktop_file, - const char *section, - const char *keyname, - char **val, - DBusError *error) -{ - const char *raw; - - _DBUS_ASSERT_ERROR_IS_CLEAR (error); - - *val = NULL; - - if (!bus_desktop_file_get_raw (desktop_file, section, keyname, &raw)) - { - dbus_set_error (error, DBUS_ERROR_FAILED, - "No \"%s\" key in .service file\n", keyname); - return FALSE; - } - - *val = _dbus_strdup (raw); - - if (*val == NULL) - { - BUS_SET_OOM (error); - return FALSE; - } - - return TRUE; -} diff --git a/bus/desktop-file.h b/bus/desktop-file.h deleted file mode 100644 index 7f43458a..00000000 --- a/bus/desktop-file.h +++ /dev/null @@ -1,56 +0,0 @@ -/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ -/* desktop-file.h .desktop file parser - * - * Copyright (C) 2003 CodeFactory AB - * - * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA - * - */ -#ifndef BUS_DESKTOP_FILE_H -#define BUS_DESKTOP_FILE_H - -#include -#include - -#define BUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX "org.freedesktop.DBus.DesktopParseError.InvalidSyntax" -#define BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES "org.freedesktop.DBus.DesktopParseError.InvalidEscapes" -#define BUS_DESKTOP_PARSE_ERROR_INVALID_CHARS "org.freedesktop.DBus.DesktopParseError.InvalidChars" - -#define DBUS_SERVICE_SECTION "D-BUS Service" -#define DBUS_SERVICE_NAME "Name" -#define DBUS_SERVICE_EXEC "Exec" -#define DBUS_SERVICE_USER "User" -#define DBUS_SERVICE_GROUP "Group" - -typedef struct BusDesktopFile BusDesktopFile; - -BusDesktopFile *bus_desktop_file_load (DBusString *filename, - DBusError *error); -void bus_desktop_file_free (BusDesktopFile *file); - -dbus_bool_t bus_desktop_file_get_raw (BusDesktopFile *desktop_file, - const char *section_name, - const char *keyname, - const char **val); -dbus_bool_t bus_desktop_file_get_string (BusDesktopFile *desktop_file, - const char *section, - const char *keyname, - char **val, - DBusError *error); - - -#endif /* BUS_DESKTOP_FILE_H */ diff --git a/dbus/Makefile.am b/dbus/Makefile.am index e966a438..bf9e1bb5 100644 --- a/dbus/Makefile.am +++ b/dbus/Makefile.am @@ -98,8 +98,8 @@ DBUS_LIB_SOURCES= \ ## dbus-md5.h \ ### source code that goes in the installed client library -### AND is generic utility functionality used by the -### daemon or test programs (all symbols in here should +### AND is generic utility functionality used by the +### daemon or test programs (all symbols in here should ### be underscore-prefixed) DBUS_SHARED_SOURCES= \ dbus-dataslot.c \ @@ -128,8 +128,8 @@ DBUS_SHARED_SOURCES= \ ### source code that is generic utility functionality used ### by the bus daemon or test apps, but is NOT included -### in the D-Bus client library (all symbols in here -### should be underscore-prefixed but don't really need +### in the D-Bus client library (all symbols in here +### should be underscore-prefixed but don't really need ### to be unless they move to DBUS_SHARED_SOURCES later) DBUS_UTIL_SOURCES= \ dbus-auth-util.c \ @@ -151,7 +151,9 @@ DBUS_UTIL_SOURCES= \ dbus-sysdeps-util-unix.c \ dbus-test.c \ dbus-test.h \ - dbus-userdb-util.c + dbus-userdb-util.c \ + dbus-desktop-file.c \ + dbus-desktop-file.h libdbus_1_la_SOURCES= \ $(DBUS_LIB_SOURCES) \ @@ -171,14 +173,14 @@ EXTRA_DIST=dbus-arch-deps.h.in noinst_LTLIBRARIES=libdbus-convenience.la libdbus_1_la_LIBADD= $(DBUS_CLIENT_LIBS) -## don't export symbols that start with "_" (we use this +## don't export symbols that start with "_" (we use this ## convention for internal symbols) libdbus_1_la_LDFLAGS= -export-symbols-regex "^[^_].*" -version-info $(LT_CURRENT):$(LT_REVISION):$(LT_AGE) -no-undefined @R_DYNAMIC_LDFLAG@ @PIC_LDFLAGS@ libdbus_convenience_la_LDFLAGS=@R_DYNAMIC_LDFLAG@ ## note that TESTS has special meaning (stuff to use in make check) -## so if adding tests not to be run in make check, don't add them to +## so if adding tests not to be run in make check, don't add them to ## TESTS if DBUS_BUILD_TESTS TESTS_ENVIRONMENT=DBUS_TEST_DATA=$(top_builddir)/test/data DBUS_TEST_HOMEDIR=$(top_builddir)/dbus @@ -187,7 +189,7 @@ else TESTS= endif -## we use noinst_PROGRAMS not check_PROGRAMS so that we build +## we use noinst_PROGRAMS not check_PROGRAMS so that we build ## even when not doing "make check" noinst_PROGRAMS=$(TESTS) diff --git a/dbus/dbus-desktop-file.c b/dbus/dbus-desktop-file.c new file mode 100644 index 00000000..45932398 --- /dev/null +++ b/dbus/dbus-desktop-file.c @@ -0,0 +1,799 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ +/* desktop-file.c .desktop file parser + * + * Copyright (C) 2003 CodeFactory AB + * Copyright (C) 2003 Red Hat Inc. + * + * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ +#include +#include +#include "dbus-desktop-file.h" + +typedef struct +{ + char *key; + char *value; +} DBusDesktopFileLine; + +typedef struct +{ + char *section_name; + + int n_lines; + DBusDesktopFileLine *lines; + int n_allocated_lines; +} DBusDesktopFileSection; + +struct DBusDesktopFile +{ + int n_sections; + DBusDesktopFileSection *sections; + int n_allocated_sections; +}; + +/** + * Parser for service files. + */ +typedef struct +{ + DBusString data; /**< The data from the file */ + + DBusDesktopFile *desktop_file; /**< The resulting object */ + int current_section; /**< The current section being parsed */ + + int pos; /**< Current position */ + int len; /**< Length */ + int line_num; /**< Current line number */ + +} DBusDesktopFileParser; + +#define VALID_KEY_CHAR 1 +#define VALID_LOCALE_CHAR 2 +static unsigned char valid[256] = { + 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , + 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , + 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x3 , 0x2 , 0x0 , + 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , + 0x0 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , + 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x0 , 0x0 , 0x0 , 0x0 , 0x2 , + 0x0 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , + 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x3 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , + 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , + 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , + 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , + 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , + 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , + 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , + 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , + 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , +}; + +static void report_error (DBusDesktopFileParser *parser, + char *message, + const char *error_name, + DBusError *error); + +static void +parser_free (DBusDesktopFileParser *parser) +{ + _dbus_desktop_file_free (parser->desktop_file); + + _dbus_string_free (&parser->data); +} + +static void +bus_desktop_file_line_free (DBusDesktopFileLine *line) +{ + dbus_free (line->key); + dbus_free (line->value); +} + +static void +bus_desktop_file_section_free (DBusDesktopFileSection *section) +{ + int i; + + for (i = 0; i < section->n_lines; i++) + bus_desktop_file_line_free (§ion->lines[i]); + + dbus_free (section->lines); + dbus_free (section->section_name); +} + +void +_dbus_desktop_file_free (DBusDesktopFile *desktop_file) +{ + int i; + + for (i = 0; i < desktop_file->n_sections; i++) + bus_desktop_file_section_free (&desktop_file->sections[i]); + dbus_free (desktop_file->sections); + + dbus_free (desktop_file); +} + +static dbus_bool_t +grow_lines_in_section (DBusDesktopFileSection *section) +{ + DBusDesktopFileLine *lines; + + int new_n_lines; + + if (section->n_allocated_lines == 0) + new_n_lines = 1; + else + new_n_lines = section->n_allocated_lines*2; + + lines = dbus_realloc (section->lines, + sizeof (DBusDesktopFileLine) * new_n_lines); + + if (lines == NULL) + return FALSE; + + section->lines = lines; + section->n_allocated_lines = new_n_lines; + + return TRUE; +} + +static dbus_bool_t +grow_sections (DBusDesktopFile *desktop_file) +{ + int new_n_sections; + DBusDesktopFileSection *sections; + + if (desktop_file->n_allocated_sections == 0) + new_n_sections = 1; + else + new_n_sections = desktop_file->n_allocated_sections*2; + + sections = dbus_realloc (desktop_file->sections, + sizeof (DBusDesktopFileSection) * new_n_sections); + if (sections == NULL) + return FALSE; + + desktop_file->sections = sections; + + desktop_file->n_allocated_sections = new_n_sections; + + return TRUE; +} + +static char * +unescape_string (DBusDesktopFileParser *parser, + const DBusString *str, + int pos, + int end_pos, + DBusError *error) +{ + char *retval, *q; + + _DBUS_ASSERT_ERROR_IS_CLEAR (error); + + /* len + 1 is enough, because unescaping never makes the + * string longer + */ + retval = dbus_malloc (end_pos - pos + 1); + if (retval == NULL) + { + _DBUS_SET_OOM (error); + return NULL; + } + + q = retval; + + while (pos < end_pos) + { + if (_dbus_string_get_byte (str, pos) == 0) + { + /* Found an embedded null */ + dbus_free (retval); + report_error (parser, "Text to be unescaped contains embedded nul", + DBUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, error); + return NULL; + } + + if (_dbus_string_get_byte (str, pos) == '\\') + { + pos ++; + + if (pos >= end_pos) + { + /* Escape at end of string */ + dbus_free (retval); + report_error (parser, "Text to be unescaped ended in \\", + DBUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, error); + return NULL; + } + + switch (_dbus_string_get_byte (str, pos)) + { + case 's': + *q++ = ' '; + break; + case 't': + *q++ = '\t'; + break; + case 'n': + *q++ = '\n'; + break; + case 'r': + *q++ = '\r'; + break; + case '\\': + *q++ = '\\'; + break; + default: + /* Invalid escape code */ + dbus_free (retval); + report_error (parser, "Text to be unescaped had invalid escape sequence", + DBUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, error); + return NULL; + } + pos++; + } + else + { + *q++ =_dbus_string_get_byte (str, pos); + + pos++; + } + } + + *q = 0; + + return retval; +} + +static DBusDesktopFileSection* +new_section (DBusDesktopFile *desktop_file, + const char *name) +{ + int n; + char *name_copy; + + if (desktop_file->n_allocated_sections == desktop_file->n_sections) + { + if (!grow_sections (desktop_file)) + return NULL; + } + + name_copy = _dbus_strdup (name); + if (name_copy == NULL) + return NULL; + + n = desktop_file->n_sections; + desktop_file->sections[n].section_name = name_copy; + + desktop_file->sections[n].n_lines = 0; + desktop_file->sections[n].lines = NULL; + desktop_file->sections[n].n_allocated_lines = 0; + + if (!grow_lines_in_section (&desktop_file->sections[n])) + { + dbus_free (desktop_file->sections[n].section_name); + desktop_file->sections[n].section_name = NULL; + return NULL; + } + + desktop_file->n_sections += 1; + + return &desktop_file->sections[n]; +} + +static DBusDesktopFileSection* +open_section (DBusDesktopFileParser *parser, + char *name) +{ + DBusDesktopFileSection *section; + + section = new_section (parser->desktop_file, name); + if (section == NULL) + return NULL; + + parser->current_section = parser->desktop_file->n_sections - 1; + _dbus_assert (&parser->desktop_file->sections[parser->current_section] == section); + + return section; +} + +static DBusDesktopFileLine * +new_line (DBusDesktopFileParser *parser) +{ + DBusDesktopFileSection *section; + DBusDesktopFileLine *line; + + section = &parser->desktop_file->sections[parser->current_section]; + + if (section->n_allocated_lines == section->n_lines) + { + if (!grow_lines_in_section (section)) + return NULL; + } + + line = §ion->lines[section->n_lines++]; + + _DBUS_ZERO(*line); + + return line; +} + +static dbus_bool_t +is_blank_line (DBusDesktopFileParser *parser) +{ + int p; + char c; + + p = parser->pos; + + c = _dbus_string_get_byte (&parser->data, p); + + while (c && c != '\n') + { + if (!(c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f')) + return FALSE; + + p++; + c = _dbus_string_get_byte (&parser->data, p); + } + + return TRUE; +} + +static void +parse_comment_or_blank (DBusDesktopFileParser *parser) +{ + int line_end, eol_len; + + if (!_dbus_string_find_eol (&parser->data, parser->pos, &line_end, &eol_len)) + line_end = parser->len; + + if (line_end == parser->len) + parser->pos = parser->len; + else + parser->pos = line_end + eol_len; + + parser->line_num += 1; +} + +static dbus_bool_t +is_valid_section_name (const char *name) +{ + /* 5. Group names may contain all ASCII characters except for control characters and '[' and ']'. */ + + while (*name) + { + if (!((*name >= 'A' && *name <= 'Z') || (*name >= 'a' || *name <= 'z') || + *name == '\n' || *name == '\t')) + return FALSE; + + name++; + } + + return TRUE; +} + +static dbus_bool_t +parse_section_start (DBusDesktopFileParser *parser, DBusError *error) +{ + int line_end, eol_len; + char *section_name; + + _DBUS_ASSERT_ERROR_IS_CLEAR (error); + + if (!_dbus_string_find_eol (&parser->data, parser->pos, &line_end, &eol_len)) + line_end = parser->len; + + if (line_end - parser->pos <= 2 || + _dbus_string_get_byte (&parser->data, line_end - 1) != ']') + { + report_error (parser, "Invalid syntax for section header", DBUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, error); + parser_free (parser); + return FALSE; + } + + section_name = unescape_string (parser, + &parser->data, parser->pos + 1, line_end - 1, + error); + + if (section_name == NULL) + { + parser_free (parser); + return FALSE; + } + + if (!is_valid_section_name (section_name)) + { + report_error (parser, "Invalid characters in section name", DBUS_DESKTOP_PARSE_ERROR_INVALID_CHARS, error); + parser_free (parser); + dbus_free (section_name); + return FALSE; + } + + if (open_section (parser, section_name) == NULL) + { + dbus_free (section_name); + parser_free (parser); + _DBUS_SET_OOM (error); + return FALSE; + } + + if (line_end == parser->len) + parser->pos = parser->len; + else + parser->pos = line_end + eol_len; + + parser->line_num += 1; + + dbus_free (section_name); + + return TRUE; +} + +static dbus_bool_t +parse_key_value (DBusDesktopFileParser *parser, DBusError *error) +{ + int line_end, eol_len; + int key_start, key_end; + int value_start; + int p; + char *value, *tmp; + DBusString key; + DBusDesktopFileLine *line; + + _DBUS_ASSERT_ERROR_IS_CLEAR (error); + + if (!_dbus_string_find_eol (&parser->data, parser->pos, &line_end, &eol_len)) + line_end = parser->len; + + p = parser->pos; + key_start = p; + while (p < line_end && + (valid[_dbus_string_get_byte (&parser->data, p)] & VALID_KEY_CHAR)) + p++; + key_end = p; + + if (key_start == key_end) + { + report_error (parser, "Empty key name", DBUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, error); + parser_free (parser); + return FALSE; + } + + /* We ignore locales for now */ + if (p < line_end && _dbus_string_get_byte (&parser->data, p) == '[') + { + if (line_end == parser->len) + parser->pos = parser->len; + else + parser->pos = line_end + eol_len; + + parser->line_num += 1; + + return TRUE; + } + + /* Skip space before '=' */ + while (p < line_end && _dbus_string_get_byte (&parser->data, p) == ' ') + p++; + + if (p < line_end && _dbus_string_get_byte (&parser->data, p) != '=') + { + report_error (parser, "Invalid characters in key name", DBUS_DESKTOP_PARSE_ERROR_INVALID_CHARS, error); + parser_free (parser); + return FALSE; + } + + if (p == line_end) + { + report_error (parser, "No '=' in key/value pair", DBUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX, error); + parser_free (parser); + return FALSE; + } + + /* Skip the '=' */ + p++; + + /* Skip space after '=' */ + while (p < line_end && _dbus_string_get_byte (&parser->data, p) == ' ') + p++; + + value_start = p; + + value = unescape_string (parser, &parser->data, value_start, line_end, error); + if (value == NULL) + { + parser_free (parser); + return FALSE; + } + + line = new_line (parser); + if (line == NULL) + { + dbus_free (value); + parser_free (parser); + _DBUS_SET_OOM (error); + return FALSE; + } + + if (!_dbus_string_init (&key)) + { + dbus_free (value); + parser_free (parser); + _DBUS_SET_OOM (error); + return FALSE; + } + + if (!_dbus_string_copy_len (&parser->data, key_start, key_end - key_start, + &key, 0)) + { + _dbus_string_free (&key); + dbus_free (value); + parser_free (parser); + _DBUS_SET_OOM (error); + return FALSE; + } + + if (!_dbus_string_steal_data (&key, &tmp)) + { + _dbus_string_free (&key); + dbus_free (value); + parser_free (parser); + _DBUS_SET_OOM (error); + return FALSE; + } + + _dbus_string_free (&key); + + line->key = tmp; + line->value = value; + + if (line_end == parser->len) + parser->pos = parser->len; + else + parser->pos = line_end + eol_len; + + parser->line_num += 1; + + return TRUE; +} + +static void +report_error (DBusDesktopFileParser *parser, + char *message, + const char *error_name, + DBusError *error) +{ + const char *section_name = NULL; + + _DBUS_ASSERT_ERROR_IS_CLEAR (error); + + if (parser->current_section != -1) + section_name = parser->desktop_file->sections[parser->current_section].section_name; + + if (section_name) + dbus_set_error (error, error_name, + "Error in section %s at line %d: %s\n", section_name, parser->line_num, message); + else + dbus_set_error (error, error_name, + "Error at line %d: %s\n", parser->line_num, message); +} + +#if 0 +static void +dump_desktop_file (DBusDesktopFile *file) +{ + int i; + + for (i = 0; i < file->n_sections; i++) + { + int j; + + printf ("[%s]\n", file->sections[i].section_name); + + for (j = 0; j < file->sections[i].n_lines; j++) + { + printf ("%s=%s\n", file->sections[i].lines[j].key, + file->sections[i].lines[j].value); + } + } +} +#endif + +DBusDesktopFile* +_dbus_desktop_file_load (DBusString *filename, + DBusError *error) +{ + DBusString str; + DBusDesktopFileParser parser; + DBusStat sb; + + _DBUS_ASSERT_ERROR_IS_CLEAR (error); + + /* Clearly there's a race here, but it's just to make it unlikely + * that we do something silly, we still handle doing it below. + */ + if (!_dbus_stat (filename, &sb, error)) + return NULL; + + if (sb.size > _DBUS_ONE_KILOBYTE * 128) + { + dbus_set_error (error, DBUS_ERROR_FAILED, + "Desktop file size (%ld bytes) is too large", (long) sb.size); + return NULL; + } + + if (!_dbus_string_init (&str)) + { + _DBUS_SET_OOM (error); + return NULL; + } + + if (!_dbus_file_get_contents (&str, filename, error)) + { + _dbus_string_free (&str); + return NULL; + } + + if (!_dbus_string_validate_utf8 (&str, 0, _dbus_string_get_length (&str))) + { + _dbus_string_free (&str); + dbus_set_error (error, DBUS_ERROR_FAILED, + "invalid UTF-8"); + return NULL; + } + + parser.desktop_file = dbus_new0 (DBusDesktopFile, 1); + if (parser.desktop_file == NULL) + { + _dbus_string_free (&str); + _DBUS_SET_OOM (error); + return NULL; + } + + parser.data = str; + parser.line_num = 1; + parser.pos = 0; + parser.len = _dbus_string_get_length (&parser.data); + parser.current_section = -1; + + while (parser.pos < parser.len) + { + if (_dbus_string_get_byte (&parser.data, parser.pos) == '[') + { + if (!parse_section_start (&parser, error)) + { + return NULL; + } + } + else if (is_blank_line (&parser) || + _dbus_string_get_byte (&parser.data, parser.pos) == '#') + parse_comment_or_blank (&parser); + else + { + if (!parse_key_value (&parser, error)) + { + return NULL; + } + } + } + + _dbus_string_free (&parser.data); + + return parser.desktop_file; +} + +static DBusDesktopFileSection * +lookup_section (DBusDesktopFile *desktop_file, + const char *section_name) +{ + DBusDesktopFileSection *section; + int i; + + if (section_name == NULL) + return NULL; + + for (i = 0; i < desktop_file->n_sections; i ++) + { + section = &desktop_file->sections[i]; + + if (strcmp (section->section_name, section_name) == 0) + return section; + } + + return NULL; +} + +static DBusDesktopFileLine * +lookup_line (DBusDesktopFile *desktop_file, + DBusDesktopFileSection *section, + const char *keyname) +{ + DBusDesktopFileLine *line; + int i; + + for (i = 0; i < section->n_lines; i++) + { + line = §ion->lines[i]; + + if (strcmp (line->key, keyname) == 0) + return line; + } + + return NULL; +} + +dbus_bool_t +_dbus_desktop_file_get_raw (DBusDesktopFile *desktop_file, + const char *section_name, + const char *keyname, + const char **val) +{ + DBusDesktopFileSection *section; + DBusDesktopFileLine *line; + + *val = NULL; + + section = lookup_section (desktop_file, section_name); + + if (!section) + return FALSE; + + line = lookup_line (desktop_file, + section, + keyname); + + if (!line) + return FALSE; + + *val = line->value; + + return TRUE; +} + +dbus_bool_t +_dbus_desktop_file_get_string (DBusDesktopFile *desktop_file, + const char *section, + const char *keyname, + char **val, + DBusError *error) +{ + const char *raw; + + _DBUS_ASSERT_ERROR_IS_CLEAR (error); + + *val = NULL; + + if (!_dbus_desktop_file_get_raw (desktop_file, section, keyname, &raw)) + { + dbus_set_error (error, DBUS_ERROR_FAILED, + "No \"%s\" key in .service file\n", keyname); + return FALSE; + } + + *val = _dbus_strdup (raw); + + if (*val == NULL) + { + _DBUS_SET_OOM (error); + return FALSE; + } + + return TRUE; +} diff --git a/dbus/dbus-desktop-file.h b/dbus/dbus-desktop-file.h new file mode 100644 index 00000000..812d82f3 --- /dev/null +++ b/dbus/dbus-desktop-file.h @@ -0,0 +1,49 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ +/* desktop-file.h .desktop file parser + * + * Copyright (C) 2003 CodeFactory AB + * + * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ +#ifndef BUS_DESKTOP_FILE_H +#define BUS_DESKTOP_FILE_H + +#include +#include + +#define DBUS_DESKTOP_PARSE_ERROR_INVALID_SYNTAX "org.freedesktop.DBus.DesktopParseError.InvalidSyntax" +#define DBUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES "org.freedesktop.DBus.DesktopParseError.InvalidEscapes" +#define DBUS_DESKTOP_PARSE_ERROR_INVALID_CHARS "org.freedesktop.DBus.DesktopParseError.InvalidChars" + +typedef struct DBusDesktopFile DBusDesktopFile; + +DBusDesktopFile *_dbus_desktop_file_load (DBusString *filename, + DBusError *error); +void _dbus_desktop_file_free (DBusDesktopFile *file); + +dbus_bool_t _dbus_desktop_file_get_raw (DBusDesktopFile *desktop_file, + const char *section_name, + const char *keyname, + const char **val); +dbus_bool_t _dbus_desktop_file_get_string (DBusDesktopFile *desktop_file, + const char *section, + const char *keyname, + char **val, + DBusError *error); + +#endif /* BUS_DESKTOP_FILE_H */ -- cgit