From edd9a518b910425f7679563c0a1d2b5307723b3b Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Wed, 15 Feb 2017 14:13:05 +0000 Subject: [PATCH 7/9] bus_config_parser_get_watched_dirs: Turn into a helper function This means we can test it more easily. At the moment it just contains service directories, because this config file is so cut-down that it doesn't have any config.d directories. Bug: https://bugs.freedesktop.org/show_bug.cgi?id=99825 Signed-off-by: Simon McVittie --- bus/bus.c | 30 +-------------------- bus/config-parser.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++ bus/config-parser.h | 2 ++ 3 files changed, 79 insertions(+), 29 deletions(-) diff --git a/bus/bus.c b/bus/bus.c index 70f1d40b..b0138664 100644 --- a/bus/bus.c +++ b/bus/bus.c @@ -670,32 +670,6 @@ process_config_every_time (BusContext *context, return retval; } -static dbus_bool_t -list_concat_new (DBusList **a, - DBusList **b, - DBusList **result) -{ - DBusList *link; - - *result = NULL; - - for (link = _dbus_list_get_first_link (a); link; link = _dbus_list_get_next_link (a, link)) - { - if (!_dbus_list_append (result, link->data)) - goto oom; - } - for (link = _dbus_list_get_first_link (b); link; link = _dbus_list_get_next_link (b, link)) - { - if (!_dbus_list_append (result, link->data)) - goto oom; - } - - return TRUE; -oom: - _dbus_list_clear (result); - return FALSE; -} - static void raise_file_descriptor_limit (BusContext *context) { @@ -756,9 +730,7 @@ process_config_postinit (BusContext *context, /* We need to monitor both the configuration directories and directories * containing .service files. */ - if (!list_concat_new (bus_config_parser_get_conf_dirs (parser), - bus_config_parser_get_service_dirs (parser), - &watched_dirs)) + if (!bus_config_parser_get_watched_dirs (parser, &watched_dirs)) { BUS_SET_OOM (error); return FALSE; diff --git a/bus/config-parser.c b/bus/config-parser.c index 5275f8f1..c1e2ff35 100644 --- a/bus/config-parser.c +++ b/bus/config-parser.c @@ -2789,6 +2789,46 @@ bus_config_parser_steal_service_context_table (BusConfigParser *parser) return table; } +/* + * Return a list of the directories that should be watched with inotify, + * as strings. The list might be empty and is in arbitrary order. + * + * The list must be empty on entry. On success, the links are owned by the + * caller and must be freed, but the data in each link remains owned by + * the BusConfigParser and must not be freed: in GObject-Introspection + * notation, it is (transfer container). + */ +dbus_bool_t +bus_config_parser_get_watched_dirs (BusConfigParser *parser, + DBusList **watched_dirs) +{ + DBusList *link; + + _dbus_assert (*watched_dirs == NULL); + + for (link = _dbus_list_get_first_link (&parser->conf_dirs); + link != NULL; + link = _dbus_list_get_next_link (&parser->conf_dirs, link)) + { + if (!_dbus_list_append (watched_dirs, link->data)) + goto oom; + } + + for (link = _dbus_list_get_first_link (&parser->service_dirs); + link != NULL; + link = _dbus_list_get_next_link (&parser->service_dirs, link)) + { + if (!_dbus_list_append (watched_dirs, link->data)) + goto oom; + } + + return TRUE; + +oom: + _dbus_list_clear (watched_dirs); + return FALSE; +} + #ifdef DBUS_ENABLE_EMBEDDED_TESTS #include @@ -3399,6 +3439,7 @@ test_default_session_servicedirs (const DBusString *test_base_dir) BusConfigParser *parser = NULL; DBusError error = DBUS_ERROR_INIT; DBusList **dirs; + DBusList *watched_dirs = NULL; DBusList *link; DBusString tmp; DBusString full_path; @@ -3534,12 +3575,47 @@ test_default_session_servicedirs (const DBusString *test_base_dir) goto out; } + if (!bus_config_parser_get_watched_dirs (parser, &watched_dirs)) + _dbus_assert_not_reached ("out of memory"); + + for (link = _dbus_list_get_first_link (&watched_dirs), i = 0; + link != NULL; + link = _dbus_list_get_next_link (&watched_dirs, link), i++) + { + printf (" watched service dir: '%s'\n", (const char *) link->data); + printf (" current standard service dir: '%s'\n", + test_session_service_dir_matches[i]); + + if (test_session_service_dir_matches[i] == NULL) + { + printf ("more directories parsed than in match set\n"); + goto out; + } + + if (strcmp (test_session_service_dir_matches[i], + (const char *) link->data) != 0) + { + printf ("'%s' directory does not match '%s' in the match set\n", + (const char *) link->data, + test_session_service_dir_matches[i]); + goto out; + } + } + + if (test_session_service_dir_matches[i] != NULL) + { + printf ("extra data %s in the match set was not matched\n", + test_session_service_dir_matches[i]); + goto out; + } + ret = TRUE; out: if (parser != NULL) bus_config_parser_unref (parser); + _dbus_list_clear (&watched_dirs); _dbus_string_free (&full_path); _dbus_string_free (&install_root_based); _dbus_string_free (&progs); diff --git a/bus/config-parser.h b/bus/config-parser.h index ba5bf749..2361e22f 100644 --- a/bus/config-parser.h +++ b/bus/config-parser.h @@ -73,6 +73,8 @@ DBusList** bus_config_parser_get_conf_dirs (BusConfigParser *parser); BusPolicy* bus_config_parser_steal_policy (BusConfigParser *parser); void bus_config_parser_get_limits (BusConfigParser *parser, BusLimits *limits); +dbus_bool_t bus_config_parser_get_watched_dirs (BusConfigParser *parser, + DBusList **watched_dirs); DBusHashTable* bus_config_parser_steal_service_context_table (BusConfigParser *parser); -- 2.11.0