Skip to content

Commit

Permalink
udisksconfigmanager: Always read the config file on getting list of m…
Browse files Browse the repository at this point in the history
…odules

Some of the configuration options don't need to be read right after
daemon startup. This brings flexibility to tests where any changes
to the config file will be reflected immediately on subsequent module
enablement calls.
  • Loading branch information
tbzatek committed May 22, 2020
1 parent 1385cc0 commit 447aef5
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 112 deletions.
3 changes: 3 additions & 0 deletions data/org.freedesktop.UDisks2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@
Loads and activates modules. Modules that have been already loaded are not reinitialized on subsequent calls to this method and are skipped.
In case any new module is getting activated by this method call a <literal>add</literal> uevent is trigerred on all exported objects.
This takes in account an optional explicit list of modules to load as specified in the <filename>/etc/udisks2/udisks2.conf</filename>
config file. If unspecified all available modules will be loaded.
Modules cannot be deactivated at the moment. This method call never fails even if no module has been activated
and by nature it cannot report any particular module initialization failures. Clients have no way of finding that
a particular module is available.
Expand Down
208 changes: 103 additions & 105 deletions src/udisksconfigmanager.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ struct _UDisksConfigManager {
gboolean uninstalled;

UDisksModuleLoadPreference load_preference;
GList *modules;

const gchar *encryption;
gchar *config_dir;
Expand Down Expand Up @@ -90,10 +89,8 @@ udisks_config_manager_get_property (GObject *object,
}

static const gchar *
get_encryption_config (const GValue *value)
get_encryption_config (const gchar *encryption)
{
const gchar *encryption = g_value_get_string (value);

if (g_strcmp0 (encryption, UDISKS_ENCRYPTION_LUKS1) == 0)
{
return UDISKS_ENCRYPTION_LUKS1;
Expand All @@ -104,8 +101,7 @@ get_encryption_config (const GValue *value)
}
else
{
udisks_warning ("Unknown value used for 'encryption': %s"
"; defaulting to '%s'",
udisks_warning ("Unknown value used for 'encryption': %s; defaulting to '%s'",
encryption, UDISKS_ENCRYPTION_DEFAULT);
return UDISKS_ENCRYPTION_DEFAULT;
}
Expand All @@ -130,7 +126,7 @@ udisks_config_manager_set_property (GObject *object,
break;

case PROP_ENCRYPTION:
manager->encryption = get_encryption_config (value);
manager->encryption = get_encryption_config (g_value_get_string (value));
break;

default:
Expand All @@ -140,9 +136,11 @@ udisks_config_manager_set_property (GObject *object,
}

static void
udisks_config_manager_constructed (GObject *object)
parse_config_file (UDisksConfigManager *manager,
UDisksModuleLoadPreference *out_load_preference,
const gchar **out_encryption,
GList **out_modules)
{
UDisksConfigManager *manager = UDISKS_CONFIG_MANAGER (object);
GKeyFile *config_file;
gchar *conf_filename;
gchar *load_preference;
Expand All @@ -151,19 +149,6 @@ udisks_config_manager_constructed (GObject *object)
gchar **modules;
gchar **modules_tmp;

/* Build a path to the config directory */
manager->config_dir = g_build_path (G_DIR_SEPARATOR_S,
manager->uninstalled ? BUILD_DIR : PACKAGE_SYSCONF_DIR,
manager->uninstalled ? "udisks" : PROJECT_SYSCONF_DIR,
NULL);

/* Make sure the config dir exists, UDisksLinuxDrive may store some data there */
if (g_mkdir_with_parents (manager->config_dir, 0755) != 0)
{
/* don't abort the daemon, the config dir may point to a readonly filesystem */
udisks_warning ("Error creating directory %s: %m", manager->config_dir);
}

/* Get modules and means of loading */
conf_filename = g_build_filename (G_DIR_SEPARATOR_S,
manager->config_dir,
Expand All @@ -175,99 +160,85 @@ udisks_config_manager_constructed (GObject *object)
/* Load config */
config_file = g_key_file_new ();
g_key_file_set_list_separator (config_file, ',');
if (g_key_file_load_from_file (config_file,
conf_filename,
G_KEY_FILE_NONE,
NULL))
if (g_key_file_load_from_file (config_file, conf_filename, G_KEY_FILE_NONE, NULL))
{
modules = g_key_file_get_string_list (config_file,
MODULES_GROUP_NAME,
MODULES_KEY,
NULL,
NULL);
/* Read the list of modules to load. */
if (modules)
{
modules_tmp = modules;
for (module_i = *modules_tmp; module_i; module_i = *++modules_tmp)
manager->modules = g_list_append (manager->modules,
g_strdup (g_strstrip (module_i)));
g_strfreev (modules);
}
else
if (out_modules != NULL)
{
udisks_debug ("No 'modules' found in configuration file");
modules = g_key_file_get_string_list (config_file, MODULES_GROUP_NAME, MODULES_KEY, NULL, NULL);
/* Read the list of modules to load. */
if (modules)
{
modules_tmp = modules;
for (module_i = *modules_tmp; module_i; module_i = *++modules_tmp)
*out_modules = g_list_append (*out_modules, g_strdup (g_strstrip (module_i)));
g_strfreev (modules);
}
}

/* Read the load preference configuration option. */
load_preference = g_key_file_get_string (config_file,
MODULES_GROUP_NAME,
MODULES_LOAD_PREFERENCE_KEY,
NULL);
if (load_preference)
if (out_load_preference != NULL)
{
/* Check the key value */
if (g_ascii_strcasecmp (load_preference, "ondemand") == 0)
{
manager->load_preference = UDISKS_MODULE_LOAD_ONDEMAND;
}
else if (g_ascii_strcasecmp (load_preference, "onstartup") == 0)
{
manager->load_preference = UDISKS_MODULE_LOAD_ONSTARTUP;
}
else
/* Read the load preference configuration option. */
load_preference = g_key_file_get_string (config_file, MODULES_GROUP_NAME, MODULES_LOAD_PREFERENCE_KEY, NULL);
if (load_preference)
{
udisks_warning ("Unknown value used for 'modules_load_preference': %s"
"; defaulting to 'ondemand'",
load_preference);
/* Check the key value */
if (g_ascii_strcasecmp (load_preference, "ondemand") == 0)
{
*out_load_preference = UDISKS_MODULE_LOAD_ONDEMAND;
}
else if (g_ascii_strcasecmp (load_preference, "onstartup") == 0)
{
*out_load_preference = UDISKS_MODULE_LOAD_ONSTARTUP;
}
else
{
udisks_warning ("Unknown value used for 'modules_load_preference': %s; defaulting to 'ondemand'",
load_preference);
}

g_free (load_preference);
}

g_free (load_preference);
}
else
{
udisks_debug ("No 'modules_load_preference' found in configuration file");
}

/* Read the load preference configuration option. */
encryption = g_key_file_get_string (config_file,
DEFAULTS_GROUP_NAME,
DEFAULTS_ENCRYPTION_KEY,
NULL);
if (encryption)
if (out_encryption != NULL)
{
/* Check the key value */
if (g_ascii_strcasecmp (encryption, UDISKS_ENCRYPTION_LUKS1) == 0)
{
manager->encryption = UDISKS_ENCRYPTION_LUKS1;
}
else if (g_ascii_strcasecmp (encryption, UDISKS_ENCRYPTION_LUKS2) == 0)
/* Read the load preference configuration option. */
encryption = g_key_file_get_string (config_file, DEFAULTS_GROUP_NAME, DEFAULTS_ENCRYPTION_KEY, NULL);
if (encryption)
{
manager->encryption = UDISKS_ENCRYPTION_LUKS2;
*out_encryption = get_encryption_config (encryption);
g_free (encryption);
}
else
{
udisks_warning ("Unknown value used for 'encryption': %s"
"; defaulting to '%s'",
encryption, manager->encryption);
}

g_free (encryption);
}
else
{
udisks_debug ("No 'encryption' found in configuration file");
}

}
else
{
udisks_warning ("Can't load configuration file %s", conf_filename);
}


g_key_file_free (config_file);
g_free (conf_filename);
}

static void
udisks_config_manager_constructed (GObject *object)
{
UDisksConfigManager *manager = UDISKS_CONFIG_MANAGER (object);

/* Build a path to the config directory */
manager->config_dir = g_build_path (G_DIR_SEPARATOR_S,
manager->uninstalled ? BUILD_DIR : PACKAGE_SYSCONF_DIR,
manager->uninstalled ? "udisks" : PROJECT_SYSCONF_DIR,
NULL);

/* Make sure the config dir exists, UDisksLinuxDrive may store some data there */
if (g_mkdir_with_parents (manager->config_dir, 0755) != 0)
{
/* don't abort the daemon, the config dir may point to a readonly filesystem */
udisks_warning ("Error creating directory %s: %m", manager->config_dir);
}

parse_config_file (manager, &manager->load_preference, &manager->encryption, NULL);

if (G_OBJECT_CLASS (udisks_config_manager_parent_class))
G_OBJECT_CLASS (udisks_config_manager_parent_class)->constructed (object);
Expand All @@ -285,11 +256,6 @@ udisks_config_manager_finalize (GObject *object)
{
UDisksConfigManager *manager = UDISKS_CONFIG_MANAGER (object);

if (manager->modules)
{
g_list_free_full (manager->modules, (GDestroyNotify) g_free);
manager->modules = NULL;
}
g_free (manager->config_dir);

if (G_OBJECT_CLASS (udisks_config_manager_parent_class))
Expand Down Expand Up @@ -362,7 +328,6 @@ udisks_config_manager_class_init (UDisksConfigManagerClass *klass)
static void
udisks_config_manager_init (UDisksConfigManager *manager)
{
manager->modules = NULL; /* NULL == '*' */
manager->load_preference = UDISKS_MODULE_LOAD_ONDEMAND;
manager->encryption = UDISKS_ENCRYPTION_DEFAULT;
}
Expand Down Expand Up @@ -390,20 +355,53 @@ udisks_config_manager_get_uninstalled (UDisksConfigManager *manager)
return manager->uninstalled;
}

const GList *
/**
* udisks_config_manager_get_modules:
* @manager: A #UDisksConfigManager.
*
* Reads the udisks2.conf file and retrieves a list of module names to load.
* A special '*' placeholder may be present as a first item as specified
* in the config file.
*
* Returns: (transfer full) (nullable) (element-type gchar*): A list of strings
* or %NULL if no specific configuration has been found in the config file.
* Free the elements with g_free().
*/
GList *
udisks_config_manager_get_modules (UDisksConfigManager *manager)
{
GList *modules = NULL;

g_return_val_if_fail (UDISKS_IS_CONFIG_MANAGER (manager), NULL);
return manager->modules;

parse_config_file (manager, NULL, NULL, &modules);
return modules;
}

/**
* udisks_config_manager_get_modules_all:
* @manager: A #UDisksConfigManager.
*
* Reads the udisks2.conf file and returns whether to load all modules or not.
* This corresponds to a special '*' placeholder in the config file.
*
* Returns: %TRUE when the daemon runs from a source tree, %FALSE otherwise.
*/
gboolean
udisks_config_manager_get_modules_all (UDisksConfigManager *manager)
{
GList *modules = NULL;
gboolean ret;

g_return_val_if_fail (UDISKS_IS_CONFIG_MANAGER (manager), FALSE);
return ! manager->modules
|| (g_strcmp0 (manager->modules->data, "*") == 0
&& g_list_length (manager->modules) == 1);

parse_config_file (manager, NULL, NULL, &modules);

ret = !modules || (g_strcmp0 (modules->data, "*") == 0 && g_list_length (modules) == 1);

g_list_free_full (modules, (GDestroyNotify) g_free);

return ret;
}

UDisksModuleLoadPreference
Expand Down
6 changes: 3 additions & 3 deletions src/udisksconfigmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ G_BEGIN_DECLS
*/
typedef enum
{
UDISKS_MODULE_LOAD_ONDEMAND,
UDISKS_MODULE_LOAD_ONSTARTUP
UDISKS_MODULE_LOAD_ONDEMAND,
UDISKS_MODULE_LOAD_ONSTARTUP
} UDisksModuleLoadPreference;

#define UDISKS_ENCRYPTION_LUKS1 "luks1"
Expand All @@ -55,7 +55,7 @@ UDisksConfigManager *udisks_config_manager_new_uninstalled (void);

gboolean udisks_config_manager_get_uninstalled (UDisksConfigManager *manager);

const GList *udisks_config_manager_get_modules (UDisksConfigManager *manager);
GList *udisks_config_manager_get_modules (UDisksConfigManager *manager);
gboolean udisks_config_manager_get_modules_all (UDisksConfigManager *manager);
UDisksModuleLoadPreference
udisks_config_manager_get_load_preference (UDisksConfigManager *manager);
Expand Down
11 changes: 7 additions & 4 deletions src/udisksmodulemanager.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ get_modules_list (UDisksModuleManager *manager)
UDisksConfigManager *config_manager;
GDir *dir;
GError *error = NULL;
const GList *modules_i = NULL;
GList *modules_list = NULL;
const gchar *dent;
gchar *module_dir;
Expand Down Expand Up @@ -246,14 +245,18 @@ get_modules_list (UDisksModuleManager *manager)
}
else
{
GList *configured_modules;
GList *modules_i;

/* Load only those modules which are specified in config file. */
for (modules_i = udisks_config_manager_get_modules (config_manager);
modules_i;
modules_i = modules_i->next)
configured_modules = udisks_config_manager_get_modules (config_manager);
for (modules_i = configured_modules; modules_i; modules_i = modules_i->next)
{
pth = get_module_sopath_for_name (manager, modules_i->data);
modules_list = g_list_append (modules_list, pth);
}

g_list_free_full (configured_modules, (GDestroyNotify) g_free);
}

g_dir_close (dir);
Expand Down

0 comments on commit 447aef5

Please sign in to comment.