monitor-config-store: Add way to define config store loading policy

This adds a way to define a way, at the system level, to define a policy
of how monitor configuration files are loaded.

The intended use case is to e.g. either prefer system level monitor
configurations before user levels, or only allow system level
configurations.

Examples:

Prefer system over user level configurations:

    <monitors version="2">
      <policy>
        <stores>
          <store>system</store>
          <store>user</store>
        </stores>
      </policy>
      <configuration>
        ...
      </configuration>
    </monitors>

Only allow system level configurations:

    <monitors version="2">
      <policy>
        <stores>
          <store>system</store>
        </stores>
      </policy>
      <configuration>
        ...
      </configuration>
    </monitors>

Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/2030>
This commit is contained in:
Jonas Ådahl
2022-01-17 11:45:53 +01:00
committed by Marge Bot
parent 6c4380ed41
commit c611b64c53
13 changed files with 452 additions and 95 deletions

View File

@ -29,6 +29,8 @@ typedef struct _MetaMonitorConfigManager MetaMonitorConfigManager;
typedef struct _MetaMonitorConfigStore MetaMonitorConfigStore;
typedef struct _MetaMonitorsConfig MetaMonitorsConfig;
typedef enum _MetaMonitorsConfigFlag MetaMonitorsConfigFlag;
typedef struct _MetaMonitor MetaMonitor;
typedef struct _MetaMonitorNormal MetaMonitorNormal;
typedef struct _MetaMonitorTiled MetaMonitorTiled;

View File

@ -51,12 +51,12 @@ typedef struct _MetaMonitorsConfigKey
GList *monitor_specs;
} MetaMonitorsConfigKey;
typedef enum _MetaMonitorsConfigFlag
enum _MetaMonitorsConfigFlag
{
META_MONITORS_CONFIG_FLAG_NONE = 0,
META_MONITORS_CONFIG_FLAG_MIGRATED = (1 << 0),
META_MONITORS_CONFIG_FLAG_SYSTEM_CONFIG = (1 << 1),
} MetaMonitorsConfigFlag;
};
struct _MetaMonitorsConfig
{

View File

@ -120,6 +120,9 @@ struct _MetaMonitorConfigStore
GFile *user_file;
GFile *custom_read_file;
GFile *custom_write_file;
gboolean has_stores_policy;
GList *stores_policy;
};
#define META_MONITOR_CONFIG_STORE_ERROR (meta_monitor_config_store_error_quark ())
@ -162,12 +165,18 @@ typedef enum
STATE_MONITOR_MODE_FLAG,
STATE_MONITOR_UNDERSCANNING,
STATE_DISABLED,
STATE_POLICY,
STATE_STORES,
STATE_STORE,
} ParserState;
typedef struct
{
ParserState state;
MetaMonitorConfigStore *config_store;
GFile *file;
GHashTable *pending_configs;
ParserState monitor_spec_parent_state;
@ -180,6 +189,10 @@ typedef struct
MetaMonitorConfig *current_monitor_config;
MetaLogicalMonitorConfig *current_logical_monitor_config;
GList *current_disabled_monitor_specs;
gboolean seen_policy;
gboolean seen_stores;
MetaConfigStore pending_store;
GList *stores;
ParserState unknown_state_root;
int unknown_level;
@ -268,16 +281,31 @@ handle_start_element (GMarkupParseContext *context,
case STATE_MONITORS:
{
if (!g_str_equal (element_name, "configuration"))
if (g_str_equal (element_name, "configuration"))
{
parser->state = STATE_CONFIGURATION;
parser->current_was_migrated = FALSE;
}
else if (g_str_equal (element_name, "policy"))
{
if (parser->seen_policy)
{
g_set_error (error,
G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
"Multiple policy definitions");
return;
}
parser->seen_policy = TRUE;
parser->state = STATE_POLICY;
}
else
{
enter_unknown_element (parser, element_name,
"monitors", STATE_MONITORS);
return;
}
parser->state = STATE_CONFIGURATION;
parser->current_was_migrated = FALSE;
return;
}
@ -523,6 +551,59 @@ handle_start_element (GMarkupParseContext *context,
return;
}
case STATE_POLICY:
{
if (!(parser->extra_config_flags &
META_MONITORS_CONFIG_FLAG_SYSTEM_CONFIG))
{
g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
"Policy can only be defined in system level configurations");
return;
}
if (g_str_equal (element_name, "stores"))
{
if (parser->seen_stores)
{
g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
"Multiple stores elements under policy");
return;
}
parser->seen_stores = TRUE;
parser->state = STATE_STORES;
}
else
{
enter_unknown_element (parser, element_name,
"policy", STATE_POLICY);
}
return;
}
case STATE_STORES:
{
if (g_str_equal (element_name, "store"))
{
parser->state = STATE_STORE;
}
else
{
enter_unknown_element (parser, element_name,
"stores", STATE_STORES);
}
return;
}
case STATE_STORE:
{
g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
"Invalid store sub element '%s'", element_name);
return;
}
}
}
@ -819,13 +900,65 @@ handle_end_element (GMarkupParseContext *context,
return;
}
g_hash_table_replace (parser->config_store->configs,
g_hash_table_replace (parser->pending_configs,
config->key, config);
parser->state = STATE_MONITORS;
return;
}
case STATE_STORE:
g_assert (g_str_equal (element_name, "store"));
if (parser->pending_store == -1)
{
g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
"Got an empty store");
return;
}
if (g_list_find (parser->stores,
GINT_TO_POINTER (parser->pending_store)))
{
g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
"Multiple identical stores in policy");
return;
}
parser->stores =
g_list_append (parser->stores,
GINT_TO_POINTER (parser->pending_store));
parser->pending_store = -1;
parser->state = STATE_STORES;
return;
case STATE_STORES:
g_assert (g_str_equal (element_name, "stores"));
if (parser->config_store->has_stores_policy)
{
g_warning ("Ignoring stores policy from '%s', "
"it has already been configured",
g_file_peek_path (parser->file));
g_clear_pointer (&parser->stores, g_list_free);
}
else
{
parser->config_store->stores_policy =
g_steal_pointer (&parser->stores);
parser->config_store->has_stores_policy = TRUE;
}
parser->state = STATE_POLICY;
return;
case STATE_POLICY:
g_assert (g_str_equal (element_name, "policy"));
parser->state = STATE_MONITORS;
return;
case STATE_UNKNOWN:
{
parser->unknown_level--;
@ -970,6 +1103,8 @@ handle_text (GMarkupParseContext *context,
case STATE_MONITOR_MODE:
case STATE_TRANSFORM:
case STATE_DISABLED:
case STATE_POLICY:
case STATE_STORES:
{
if (!is_all_whitespace (text, text_len))
g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
@ -1120,6 +1255,36 @@ handle_text (GMarkupParseContext *context,
error);
return;
}
case STATE_STORE:
{
MetaConfigStore store;
if (parser->pending_store != -1)
{
g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
"Multiple store strings");
return;
}
if (text_equals (text, text_len, "system"))
{
store = META_CONFIG_STORE_SYSTEM;
}
else if (text_equals (text, text_len, "user"))
{
store = META_CONFIG_STORE_USER;
}
else
{
g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
"Invalid store %.*s", (int) text_len, text);
return;
}
parser->pending_store = store;
return;
}
}
}
@ -1133,6 +1298,7 @@ static gboolean
read_config_file (MetaMonitorConfigStore *config_store,
GFile *file,
MetaMonitorsConfigFlag extra_config_flags,
GHashTable **out_configs,
GError **error)
{
char *buffer;
@ -1145,9 +1311,15 @@ read_config_file (MetaMonitorConfigStore *config_store,
parser = (ConfigParser) {
.state = STATE_INITIAL,
.file = file,
.config_store = config_store,
.pending_configs = g_hash_table_new_full (meta_monitors_config_key_hash,
meta_monitors_config_key_equal,
NULL,
g_object_unref),
.extra_config_flags = extra_config_flags,
.unknown_state_root = -1,
.pending_store = -1,
};
parse_context = g_markup_parse_context_new (&config_parser,
@ -1165,9 +1337,13 @@ read_config_file (MetaMonitorConfigStore *config_store,
meta_monitor_config_free);
g_clear_pointer (&parser.current_logical_monitor_config,
meta_logical_monitor_config_free);
g_list_free (parser.stores);
g_hash_table_unref (parser.pending_configs);
return FALSE;
}
*out_configs = g_steal_pointer (&parser.pending_configs);
g_markup_parse_context_free (parse_context);
g_free (buffer);
@ -1526,23 +1702,34 @@ meta_monitor_config_store_remove (MetaMonitorConfigStore *config_store,
}
gboolean
meta_monitor_config_store_set_custom (MetaMonitorConfigStore *config_store,
const char *read_path,
const char *write_path,
GError **error)
meta_monitor_config_store_set_custom (MetaMonitorConfigStore *config_store,
const char *read_path,
const char *write_path,
MetaMonitorsConfigFlag config_flags,
GError **error)
{
GHashTable *new_configs = NULL;
g_clear_object (&config_store->custom_read_file);
g_clear_object (&config_store->custom_write_file);
g_hash_table_remove_all (config_store->configs);
config_store->custom_read_file = g_file_new_for_path (read_path);
if (write_path)
config_store->custom_write_file = g_file_new_for_path (write_path);
return read_config_file (config_store,
config_store->custom_read_file,
META_MONITORS_CONFIG_FLAG_NONE,
error);
g_clear_pointer (&config_store->stores_policy, g_list_free);
config_store->has_stores_policy = FALSE;
if (!read_config_file (config_store,
config_store->custom_read_file,
config_flags,
&new_configs,
error))
return FALSE;
g_clear_pointer (&config_store->configs, g_hash_table_unref);
config_store->configs = g_steal_pointer (&new_configs);
return TRUE;
}
int
@ -1551,6 +1738,12 @@ meta_monitor_config_store_get_config_count (MetaMonitorConfigStore *config_store
return (int) g_hash_table_size (config_store->configs);
}
GList *
meta_monitor_config_store_get_stores_policy (MetaMonitorConfigStore *config_store)
{
return config_store->stores_policy;
}
MetaMonitorManager *
meta_monitor_config_store_get_monitor_manager (MetaMonitorConfigStore *config_store)
{
@ -1569,75 +1762,8 @@ static void
meta_monitor_config_store_constructed (GObject *object)
{
MetaMonitorConfigStore *config_store = META_MONITOR_CONFIG_STORE (object);
const char * const *system_dirs;
char *user_file_path;
GError *error = NULL;
for (system_dirs = g_get_system_config_dirs ();
system_dirs && *system_dirs;
system_dirs++)
{
g_autofree char *system_file_path = NULL;
system_file_path = g_build_filename (*system_dirs, "monitors.xml", NULL);
if (g_file_test (system_file_path, G_FILE_TEST_EXISTS))
{
g_autoptr (GFile) system_file = NULL;
system_file = g_file_new_for_path (system_file_path);
if (!read_config_file (config_store,
system_file,
META_MONITORS_CONFIG_FLAG_SYSTEM_CONFIG,
&error))
{
if (g_error_matches (error,
META_MONITOR_CONFIG_STORE_ERROR,
META_MONITOR_CONFIG_STORE_ERROR_NEEDS_MIGRATION))
g_warning ("System monitor configuration file (%s) is "
"incompatible; ask your administrator to migrate "
"the system monitor configuration.",
system_file_path);
else
g_warning ("Failed to read monitors config file '%s': %s",
system_file_path, error->message);
g_clear_error (&error);
}
}
}
user_file_path = g_build_filename (g_get_user_config_dir (),
"monitors.xml",
NULL);
config_store->user_file = g_file_new_for_path (user_file_path);
if (g_file_test (user_file_path, G_FILE_TEST_EXISTS))
{
if (!read_config_file (config_store,
config_store->user_file,
META_MONITORS_CONFIG_FLAG_NONE,
&error))
{
if (error->domain == META_MONITOR_CONFIG_STORE_ERROR &&
error->code == META_MONITOR_CONFIG_STORE_ERROR_NEEDS_MIGRATION)
{
g_clear_error (&error);
if (!meta_migrate_old_user_monitors_config (config_store, &error))
{
g_warning ("Failed to migrate old monitors config file: %s",
error->message);
g_error_free (error);
}
}
else
{
g_warning ("Failed to read monitors config file '%s': %s",
user_file_path, error->message);
g_error_free (error);
}
}
}
g_free (user_file_path);
meta_monitor_config_store_reset (config_store);
G_OBJECT_CLASS (meta_monitor_config_store_parent_class)->constructed (object);
}
@ -1660,6 +1786,7 @@ meta_monitor_config_store_dispose (GObject *object)
g_clear_object (&config_store->user_file);
g_clear_object (&config_store->custom_read_file);
g_clear_object (&config_store->custom_write_file);
g_clear_pointer (&config_store->stores_policy, g_list_free);
G_OBJECT_CLASS (meta_monitor_config_store_parent_class)->dispose (object);
}
@ -1730,3 +1857,133 @@ meta_monitor_config_store_class_init (MetaMonitorConfigStoreClass *klass)
g_object_class_install_properties (object_class, PROP_LAST, obj_props);
}
static void
replace_configs (MetaMonitorConfigStore *config_store,
GHashTable *configs)
{
GHashTableIter iter;
MetaMonitorsConfigKey *key;
MetaMonitorsConfig *config;
g_hash_table_iter_init (&iter, configs);
while (g_hash_table_iter_next (&iter,
(gpointer *) &key,
(gpointer *) &config))
{
g_hash_table_iter_steal (&iter);
g_hash_table_replace (config_store->configs, key, config);
}
}
void
meta_monitor_config_store_reset (MetaMonitorConfigStore *config_store)
{
g_autoptr (GHashTable) system_configs = NULL;
g_autoptr (GHashTable) user_configs = NULL;
const char * const *system_dirs;
char *user_file_path;
GError *error = NULL;
g_clear_object (&config_store->user_file);
g_clear_object (&config_store->custom_read_file);
g_clear_object (&config_store->custom_write_file);
g_hash_table_remove_all (config_store->configs);
for (system_dirs = g_get_system_config_dirs ();
system_dirs && *system_dirs;
system_dirs++)
{
g_autofree char *system_file_path = NULL;
system_file_path = g_build_filename (*system_dirs, "monitors.xml", NULL);
if (g_file_test (system_file_path, G_FILE_TEST_EXISTS))
{
g_autoptr (GFile) system_file = NULL;
system_file = g_file_new_for_path (system_file_path);
if (!read_config_file (config_store,
system_file,
META_MONITORS_CONFIG_FLAG_SYSTEM_CONFIG,
&system_configs,
&error))
{
if (g_error_matches (error,
META_MONITOR_CONFIG_STORE_ERROR,
META_MONITOR_CONFIG_STORE_ERROR_NEEDS_MIGRATION))
g_warning ("System monitor configuration file (%s) is "
"incompatible; ask your administrator to migrate "
"the system monitor configuration.",
system_file_path);
else
g_warning ("Failed to read monitors config file '%s': %s",
system_file_path, error->message);
g_clear_error (&error);
}
}
}
user_file_path = g_build_filename (g_get_user_config_dir (),
"monitors.xml",
NULL);
config_store->user_file = g_file_new_for_path (user_file_path);
if (g_file_test (user_file_path, G_FILE_TEST_EXISTS))
{
if (!read_config_file (config_store,
config_store->user_file,
META_MONITORS_CONFIG_FLAG_NONE,
&user_configs,
&error))
{
if (error->domain == META_MONITOR_CONFIG_STORE_ERROR &&
error->code == META_MONITOR_CONFIG_STORE_ERROR_NEEDS_MIGRATION)
{
g_clear_error (&error);
if (!meta_migrate_old_user_monitors_config (config_store, &error))
{
g_warning ("Failed to migrate old monitors config file: %s",
error->message);
g_error_free (error);
}
}
else
{
g_warning ("Failed to read monitors config file '%s': %s",
user_file_path, error->message);
g_error_free (error);
}
}
}
if (config_store->has_stores_policy)
{
GList *l;
for (l = g_list_last (config_store->stores_policy); l; l = l->prev)
{
MetaConfigStore store = GPOINTER_TO_INT (l->data);
switch (store)
{
case META_CONFIG_STORE_SYSTEM:
if (system_configs)
replace_configs (config_store, system_configs);
break;
case META_CONFIG_STORE_USER:
if (user_configs)
replace_configs (config_store, user_configs);
}
}
}
else
{
if (system_configs)
replace_configs (config_store, system_configs);
if (user_configs)
replace_configs (config_store, user_configs);
}
g_free (user_file_path);
}

View File

@ -26,6 +26,12 @@
#include "backends/meta-monitor-config-manager.h"
typedef enum _MetaConfigStore
{
META_CONFIG_STORE_SYSTEM,
META_CONFIG_STORE_USER,
} MetaConfigStore;
#define META_TYPE_MONITOR_CONFIG_STORE (meta_monitor_config_store_get_type ())
G_DECLARE_FINAL_TYPE (MetaMonitorConfigStore, meta_monitor_config_store,
META, MONITOR_CONFIG_STORE, GObject)
@ -46,10 +52,14 @@ void meta_monitor_config_store_remove (MetaMonitorConfigStore *config_store,
MetaMonitorsConfig *config);
META_EXPORT_TEST
gboolean meta_monitor_config_store_set_custom (MetaMonitorConfigStore *config_store,
const char *read_path,
const char *write_path,
GError **error);
gboolean meta_monitor_config_store_set_custom (MetaMonitorConfigStore *config_store,
const char *read_path,
const char *write_path,
MetaMonitorsConfigFlag flags,
GError **error);
META_EXPORT_TEST
GList * meta_monitor_config_store_get_stores_policy (MetaMonitorConfigStore *config_store);
META_EXPORT_TEST
int meta_monitor_config_store_get_config_count (MetaMonitorConfigStore *config_store);
@ -57,4 +67,7 @@ int meta_monitor_config_store_get_config_count (MetaMonitorConfigStore *config_s
META_EXPORT_TEST
MetaMonitorManager * meta_monitor_config_store_get_monitor_manager (MetaMonitorConfigStore *config_store);
META_EXPORT_TEST
void meta_monitor_config_store_reset (MetaMonitorConfigStore *config_store);
#endif /* META_MONITOR_CONFIG_STORE_H */