Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Abort if mce-conf subsystem is used without successful initialization
Now that failing mce_conf_init() causes mce to exit, we can remove
repetitive error handling that deals with NULL keyfile pointers.
  • Loading branch information
spiiroin committed Feb 7, 2013
1 parent 5032b36 commit 64abe96
Showing 1 changed file with 28 additions and 87 deletions.
115 changes: 28 additions & 87 deletions mce-conf.c
Expand Up @@ -30,6 +30,28 @@
/** Pointer to the keyfile structure where config values are read from */
static gpointer keyfile = NULL;

/** Internal helper for insuring valid keyfile pointer is available
*
* @param keyfilepointer custom key file, or NULL to use the default one
*
* @returns non-null keyfile pointer, or aborts
*/
static gpointer mce_conf_get_keyfile(gpointer keyfileptr)
{
if( !keyfileptr && !(keyfileptr = keyfile) ) {
/* Earlier it was possible to have mce running with NULL
* keyfile. Now the only reasons that might happen are:
* 1) mce_conf_init() was not called yet
* 2) mce_conf_init() has failed
* 3) mce_conf_exit() has already been called
* i.e. critical logic errors somewhere */
mce_log(LL_CRIT, "mce config subsystem used without "
"properly initializing it");
mce_abort();
}
return keyfileptr;
}

/**
* Get a boolean configuration value
*
Expand All @@ -45,19 +67,7 @@ gboolean mce_conf_get_bool(const gchar *group, const gchar *key,
gboolean tmp = FALSE;
GError *error = NULL;

if (keyfileptr == NULL) {
if (keyfile == NULL) {
mce_log(LL_ERR,
"Could not get config key %s/%s; "
"no configuration file initialised; "
"defaulting to `%d'",
group, key, defaultval);
tmp = defaultval;
goto EXIT;
} else {
keyfileptr = keyfile;
}
}
keyfileptr = mce_conf_get_keyfile(keyfileptr);

tmp = g_key_file_get_boolean(keyfileptr, group, key, &error);

Expand All @@ -71,7 +81,6 @@ gboolean mce_conf_get_bool(const gchar *group, const gchar *key,

g_clear_error(&error);

EXIT:
return tmp;
}

Expand All @@ -90,19 +99,7 @@ gint mce_conf_get_int(const gchar *group, const gchar *key,
gint tmp = -1;
GError *error = NULL;

if (keyfileptr == NULL) {
if (keyfile == NULL) {
mce_log(LL_ERR,
"Could not get config key %s/%s; "
"no configuration file initialised; "
"defaulting to `%d'",
group, key, defaultval);
tmp = defaultval;
goto EXIT;
} else {
keyfileptr = keyfile;
}
}
keyfileptr = mce_conf_get_keyfile(keyfileptr);

tmp = g_key_file_get_integer(keyfileptr, group, key, &error);

Expand All @@ -116,7 +113,6 @@ gint mce_conf_get_int(const gchar *group, const gchar *key,

g_clear_error(&error);

EXIT:
return tmp;
}

Expand All @@ -135,19 +131,7 @@ gint *mce_conf_get_int_list(const gchar *group, const gchar *key,
gint *tmp = NULL;
GError *error = NULL;

if (keyfileptr == NULL) {
if (keyfile == NULL) {
mce_log(LL_ERR,
"Could not get config key %s/%s; "
"no configuration file initialised",
group, key);
if( length )
*length = 0;
goto EXIT;
} else {
keyfileptr = keyfile;
}
}
keyfileptr = mce_conf_get_keyfile(keyfileptr);

tmp = g_key_file_get_integer_list(keyfileptr, group, key,
length, &error);
Expand All @@ -162,7 +146,6 @@ gint *mce_conf_get_int_list(const gchar *group, const gchar *key,

g_clear_error(&error);

EXIT:
return tmp;
}

Expand All @@ -181,22 +164,7 @@ gchar *mce_conf_get_string(const gchar *group, const gchar *key,
gchar *tmp = NULL;
GError *error = NULL;

if (keyfileptr == NULL) {
if (keyfile == NULL) {
mce_log(LL_ERR,
"Could not get config key %s/%s; "
"no configuration file initialised; "
"defaulting to `%s'",
group, key, defaultval);

if (defaultval != NULL)
tmp = g_strdup(defaultval);

goto EXIT;
} else {
keyfileptr = keyfile;
}
}
keyfileptr = mce_conf_get_keyfile(keyfileptr);

tmp = g_key_file_get_string(keyfileptr, group, key, &error);

Expand All @@ -214,7 +182,6 @@ gchar *mce_conf_get_string(const gchar *group, const gchar *key,

g_clear_error(&error);

EXIT:
return tmp;
}

Expand All @@ -233,19 +200,7 @@ gchar **mce_conf_get_string_list(const gchar *group, const gchar *key,
gchar **tmp = NULL;
GError *error = NULL;

if (keyfileptr == NULL) {
if (keyfile == NULL) {
mce_log(LL_ERR,
"Could not get config key %s/%s; "
"no configuration file initialised",
group, key);
if( length )
*length = 0;
goto EXIT;
} else {
keyfileptr = keyfile;
}
}
keyfileptr = mce_conf_get_keyfile(keyfileptr);

tmp = g_key_file_get_string_list(keyfileptr, group, key,
length, &error);
Expand All @@ -260,7 +215,6 @@ gchar **mce_conf_get_string_list(const gchar *group, const gchar *key,

g_clear_error(&error);

EXIT:
return tmp;
}

Expand All @@ -270,19 +224,7 @@ gchar **mce_conf_get_keys(const gchar *group, gsize *length,
gchar **tmp = NULL;
GError *error = NULL;

if (keyfileptr == NULL) {
if (keyfile == NULL) {
mce_log(LL_ERR,
"Could not get config keys %s; "
"no configuration file initialised",
group);
if( length )
*length = 0;
goto EXIT;
} else {
keyfileptr = keyfile;
}
}
keyfileptr = mce_conf_get_keyfile(keyfileptr);

tmp = g_key_file_get_keys(keyfileptr, group, length, &error);

Expand All @@ -296,7 +238,6 @@ gchar **mce_conf_get_keys(const gchar *group, gsize *length,

g_clear_error(&error);

EXIT:
return tmp;
}

Expand Down

0 comments on commit 64abe96

Please sign in to comment.