summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAjay Panicker <apanicke@google.com>2016-05-12 11:03:39 -0700
committerAjay Panicker <apanicke@google.com>2016-05-13 17:40:34 +0000
commit9eb556804ae753b6fabc66329952dae596f378b7 (patch)
tree0ca006dbdccee884d8de59d50572776039c9ca26
parentf56308e42dc38699111b416fe68da300bf41dfbf (diff)
downloadandroid-system-bt-9eb556804ae753b6fabc66329952dae596f378b7.tar.gz
android-system-bt-9eb556804ae753b6fabc66329952dae596f378b7.tar.xz
Add config logging, make parsing stricter, and move sync
The functions that parse the config file will report failure if the config file is improperly formatted or is missing the necessary Adapter section. Also moved sync to sync the filesystem after the config file is saved. Bug: 27354612 Change-Id: Ia75ff628d83bdc933c4bc16761d6c55eef19b757
-rw-r--r--btif/src/btif_config.c33
-rw-r--r--osi/src/config.c16
2 files changed, 41 insertions, 8 deletions
diff --git a/btif/src/btif_config.c b/btif/src/btif_config.c
index 09e0e2d..eceb964 100644
--- a/btif/src/btif_config.c
+++ b/btif/src/btif_config.c
@@ -54,6 +54,7 @@
#define INFO_SECTION "Info"
#define FILE_TIMESTAMP "TimeCreated"
+#define FILE_SOURCE "FileSource"
#define TIME_STRING_LENGTH sizeof("YYYY-MM-DD HH:MM:SS")
static const char* TIME_STRING_FORMAT = "%Y-%m-%d %H:%M:%S";
@@ -75,6 +76,7 @@ static bool is_factory_reset(void);
static void delete_config_files(void);
static void btif_config_remove_unpaired(config_t *config);
static void btif_config_remove_restricted(config_t *config);
+static config_t *btif_config_open(const char* filename);
static enum ConfigSource {
NOT_LOADED,
@@ -139,24 +141,33 @@ static future_t *init(void) {
if (is_factory_reset())
delete_config_files();
- config = config_new(CONFIG_FILE_PATH);
+ const char *file_source = NULL;
+
+ config = btif_config_open(CONFIG_FILE_PATH);
btif_config_source = ORIGINAL;
if (!config) {
LOG_WARN("%s unable to load config file: %s; using backup.",
__func__, CONFIG_FILE_PATH);
- config = config_new(CONFIG_BACKUP_PATH);
+ config = btif_config_open(CONFIG_BACKUP_PATH);
btif_config_source = BACKUP;
+ file_source = "Backup";
}
if (!config) {
LOG_WARN("%s unable to load backup; attempting to transcode legacy file.", __func__);
config = btif_config_transcode(CONFIG_LEGACY_FILE_PATH);
btif_config_source = LEGACY;
+ file_source = "Legacy";
}
if (!config) {
LOG_ERROR("%s unable to transcode legacy file; creating empty config.", __func__);
config = config_new_empty();
btif_config_source = NEW_FILE;
+ file_source = "Empty";
}
+
+ if (file_source != NULL)
+ config_set_string(config, INFO_SECTION, FILE_SOURCE, file_source);
+
if (!config) {
LOG_ERROR("%s unable to allocate a config object.", __func__);
goto error;
@@ -202,6 +213,20 @@ error:
return future_new_immediate(FUTURE_FAIL);
}
+static config_t *btif_config_open(const char *filename) {
+ config_t *config = config_new(filename);
+ if (!config)
+ return NULL;
+
+ if (!config_has_section(config, "Adapter")) {
+ LOG_ERROR(LOG_TAG, "Config is missing adapter section");
+ config_free(config);
+ return NULL;
+ }
+
+ return config;
+}
+
static future_t *shut_down(void) {
btif_config_flush();
return future_new_immediate(FUTURE_SUCCESS);
@@ -464,11 +489,11 @@ static void btif_config_write(UNUSED_ATTR UINT16 event, UNUSED_ATTR char *p_para
pthread_mutex_lock(&lock);
rename(CONFIG_FILE_PATH, CONFIG_BACKUP_PATH);
- sync();
config_t *config_paired = config_new_clone(config);
btif_config_remove_unpaired(config_paired);
config_save(config_paired, CONFIG_FILE_PATH);
config_free(config_paired);
+ sync();
pthread_mutex_unlock(&lock);
}
@@ -530,6 +555,8 @@ void btif_debug_config_dump(int fd) {
dprintf(fd, " Devices loaded: %d\n", btif_config_devices_loaded);
dprintf(fd, " File created/tagged: %s\n", btif_config_time_created);
+ dprintf(fd, " File source: %s\n", config_get_string(config, INFO_SECTION,
+ FILE_SOURCE, "Original"));
}
static void btif_config_remove_restricted(config_t* config) {
diff --git a/osi/src/config.c b/osi/src/config.c
index 6c76ae1..9683de2 100644
--- a/osi/src/config.c
+++ b/osi/src/config.c
@@ -50,7 +50,7 @@ struct config_t {
// Empty definition; this type is aliased to list_node_t.
struct config_section_iter_t {};
-static void config_parse(FILE *fp, config_t *config);
+static bool config_parse(FILE *fp, config_t *config);
static section_t *section_new(const char *name);
static void section_free(void *ptr);
@@ -89,7 +89,12 @@ config_t *config_new(const char *filename) {
config_free(config);
return NULL;
}
- config_parse(fp, config);
+
+ if (!config_parse(fp, config)) {
+ config_free(config);
+ config = NULL;
+ }
+
fclose(fp);
return config;
}
@@ -352,7 +357,7 @@ static char *trim(char *str) {
return str;
}
-static void config_parse(FILE *fp, config_t *config) {
+static bool config_parse(FILE *fp, config_t *config) {
assert(fp != NULL);
assert(config != NULL);
@@ -373,7 +378,7 @@ static void config_parse(FILE *fp, config_t *config) {
size_t len = strlen(line_ptr);
if (line_ptr[len - 1] != ']') {
LOG_DEBUG(LOG_TAG, "%s unterminated section name on line %d.", __func__, line_num);
- continue;
+ return false;
}
strncpy(section, line_ptr + 1, len - 2);
section[len - 2] = '\0';
@@ -381,13 +386,14 @@ static void config_parse(FILE *fp, config_t *config) {
char *split = strchr(line_ptr, '=');
if (!split) {
LOG_DEBUG(LOG_TAG, "%s no key/value separator found on line %d.", __func__, line_num);
- continue;
+ return false;
}
*split = '\0';
config_set_string(config, section, trim(line_ptr), trim(split + 1));
}
}
+ return true;
}
static section_t *section_new(const char *name) {