src: don't crash on incomplete user-edited config file

If a user created a custom config file and we added new fields in a
newer version, `eg25-manager` will crash by assuming all config files
are complete and up-to-date.

Moreover, creating a custom config, even to change only one option,
required copying a complete default config file and then editing the
relevant field(s). This could lead to issues when upgrading, as default
values of fields unrelated to the user change could be modified and not
applied to the user config.

This patch addresses both these issues by:
* making sure at least one config file exists
* requiring only the "default" config file (the one under `/usr/share`)
  to include all the required fields
* trying to use user config for each field, falling back to the default
  config file if the field isn't present in the user config
* exiting with a meaningful error message in case the default config
  file is missing a required field or section

That way, it will be possible to have a minimal user config file
containing only the field(s) needing a different value than the default
one, falling back to the values in the default config file.

Fixes #23
This commit is contained in:
Arnaud Ferraris
2021-09-29 00:02:41 +02:00
committed by Arnaud Ferraris
parent 55ed2dc39c
commit 4c6625a38d
16 changed files with 159 additions and 158 deletions

View File

@@ -5,6 +5,7 @@
*/
#include "at.h"
#include "config.h"
#include "gpio.h"
#include "manager.h"
@@ -33,6 +34,8 @@
#define EG25_DATADIR "/usr/share/eg25-manager"
#endif
#define POWERON_DELAY_US 100000UL
static gboolean quit_app(struct EG25Manager *manager)
{
int i;
@@ -151,7 +154,7 @@ void modem_reset(struct EG25Manager *manager)
* TODO: Improve ofono plugin and add support for fetching USB ID
*/
if (manager->modem_iface != MODEM_IFACE_MODEMMANAGER)
return;
return;
if (manager->modem_recovery_timer) {
g_source_remove(manager->modem_recovery_timer);
@@ -225,7 +228,7 @@ void modem_resume_post(struct EG25Manager *manager)
at_sequence_resume(manager);
}
static toml_table_t *parse_config_file(char *config_file)
static toml_table_t *parse_config_file(char *config_file, gboolean force_default)
{
toml_table_t *toml_config;
gchar *compatible;
@@ -249,28 +252,26 @@ static toml_table_t *parse_config_file(char *config_file)
} while (pos < len);
for (pos = 0; pos < compat->len; pos++) {
g_autofree gchar *filename = g_strdup_printf(EG25_CONFDIR "/%s.toml", (gchar *)g_ptr_array_index(compat, pos));
g_autofree gchar *filename = NULL;
if (force_default)
filename = g_strdup_printf(EG25_DATADIR "/%s.toml", (gchar *)g_ptr_array_index(compat, pos));
else
filename = g_strdup_printf(EG25_CONFDIR "/%s.toml", (gchar *)g_ptr_array_index(compat, pos));
if (access(filename, F_OK) == 0) {
g_message("Opening config file: %s", filename);
f = fopen(filename, "r");
break;
}
}
if (!f) {
for (pos = 0; pos < compat->len; pos++) {
g_autofree gchar *filename = g_strdup_printf(EG25_DATADIR "/%s.toml", (gchar *)g_ptr_array_index(compat, pos));
if (access(filename, F_OK) == 0) {
g_message("Opening config file: %s", filename);
f = fopen(filename, "r");
break;
}
}
}
}
if (!f)
g_error("unable to find a suitable config file!");
if (!f) {
if (force_default)
g_error("unable to find a suitable config file!");
else
return NULL;
}
toml_config = toml_parse_file(f, error, sizeof(error));
if (!toml_config)
@@ -285,9 +286,8 @@ int main(int argc, char *argv[])
g_autoptr(GError) err = NULL;
struct EG25Manager manager;
gchar *config_file = NULL;
toml_table_t *toml_config;
toml_table_t *toml_manager;
toml_datum_t toml_value;
toml_table_t *toml_config[EG25_CONFIG_COUNT];
toml_table_t *manager_config[EG25_CONFIG_COUNT];
const GOptionEntry options[] = {
{ "config", 'c', 0, G_OPTION_ARG_STRING, &config_file, "Config file to use.", NULL },
{ NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL }
@@ -295,6 +295,7 @@ int main(int argc, char *argv[])
memset(&manager, 0, sizeof(manager));
manager.at_fd = -1;
manager.poweron_delay = POWERON_DELAY_US;
manager.suspend_delay_fd = -1;
manager.suspend_block_fd = -1;
@@ -307,43 +308,46 @@ int main(int argc, char *argv[])
manager.loop = g_main_loop_new(NULL, FALSE);
toml_config = parse_config_file(config_file);
toml_config[EG25_CONFIG_SYS] = parse_config_file(NULL, TRUE);
toml_config[EG25_CONFIG_USER] = parse_config_file(config_file, FALSE);
toml_manager = toml_table_in(toml_config, "manager");
if (toml_manager) {
toml_value = toml_bool_in(toml_manager, "need_libusb");
if (toml_value.ok)
manager.use_libusb = toml_value.u.b;
toml_value = toml_int_in(toml_manager, "usb_vid");
if (toml_value.ok)
manager.usb_vid = toml_value.u.i;
toml_value = toml_int_in(toml_manager, "usb_pid");
if (toml_value.ok)
manager.usb_pid = toml_value.u.i;
toml_value = toml_int_in(toml_manager, "poweron_delay");
if (toml_value.ok) {
if (toml_value.u.i >= 0 && toml_value.u.i <= G_MAXULONG) {
// Safe to cast into gulong
manager.poweron_delay = (gulong) toml_value.u.i;
} else {
// Changed from initialized default value but not in range
g_message("Configured poweron_delay out of range, using default");
}
}
/*
* We need at least one valid config file, and assuming it's
* EG25_CONFIG_SYS will make the rest easier to implement
*/
if (!toml_config[EG25_CONFIG_SYS] && toml_config[EG25_CONFIG_USER]) {
toml_config[EG25_CONFIG_SYS] = toml_config[EG25_CONFIG_USER];
toml_config[EG25_CONFIG_USER] = NULL;
}
at_init(&manager, toml_table_in(toml_config, "at"));
gpio_init(&manager, toml_table_in(toml_config, "gpio"));
if (!toml_config[EG25_CONFIG_SYS])
g_error("Unable to parse config file!");
for (int i = 0; i < EG25_CONFIG_COUNT; i++)
manager_config[i] = toml_config[i] ? toml_table_in(toml_config[i], "manager") : NULL;
if (!manager_config[EG25_CONFIG_SYS])
g_error("Default config file lacks the 'manager' section!");
config_get_bool(manager_config, "need_libusb", &manager.use_libusb);
config_get_uint(manager_config, "usb_vid", &manager.usb_vid);
config_get_uint(manager_config, "usb_pid", &manager.usb_pid);
config_get_uint(manager_config, "poweron_delay", &manager.poweron_delay);
at_init(&manager, toml_config);
gpio_init(&manager, toml_config);
#ifdef HAVE_MMGLIB
mm_iface_init(&manager, toml_table_in(toml_config, "mm-iface"));
mm_iface_init(&manager, toml_config);
#endif
ofono_iface_init(&manager);
suspend_init(&manager, toml_table_in(toml_config, "suspend"));
udev_init(&manager, toml_table_in(toml_config, "udev"));
gnss_init(&manager, toml_table_in(toml_config, "gnss"));
ofono_iface_init(&manager, toml_config);
suspend_init(&manager, toml_config);
udev_init(&manager, toml_config);
gnss_init(&manager, toml_config);
for (int i = 0; i < EG25_CONFIG_COUNT; i++) {
if (toml_config[i])
toml_free(toml_config[i]);
}
g_idle_add(G_SOURCE_FUNC(modem_start), &manager);