diff --git a/src/swmod_lxc.c b/src/swmod_lxc.c index 8c9f11aa1aae819103745c109795f78866d24049..589adc07931869f4e0190f8b8b5447a665bcf51e 100644 --- a/src/swmod_lxc.c +++ b/src/swmod_lxc.c @@ -510,7 +510,6 @@ static int lxc_attach_service_state_func(void *args) int swmod_set_ee_state(ExecEnv_t *env, char *state) { - int timeout; int ret = LXC_SUCCESS; struct lxc_container *ct = NULL; @@ -538,8 +537,7 @@ int swmod_set_ee_state(ExecEnv_t *env, char *state) switch (req_state) { case LXC_START: - if (!get_autoboot_from_config_file(ct->name)) - set_autoboot_to_config_file(ct->name); + set_autoboot_to_config_file(ct->name, true); if (ct->is_running(ct)) { need_du_sync = true; @@ -554,10 +552,7 @@ int swmod_set_ee_state(ExecEnv_t *env, char *state) } break; case LXC_STOP: - timeout = get_timeout_from_config_file(ct->name); - - if (get_autoboot_from_config_file(ct->name)) - delete_autoboot_from_config_file(ct->name); + set_autoboot_to_config_file(ct->name, false); if (!ct->is_running(ct)) { ret = LXC_NOT_RUNNING; @@ -572,6 +567,7 @@ int swmod_set_ee_state(ExecEnv_t *env, char *state) } } + int timeout = get_timeout_from_config_file(ct->name); ret = ct->shutdown(ct, timeout) ? LXC_SUCCESS : LXC_FAILURE; if (ret == LXC_FAILURE) { PRINT_DEBUG("Going to force stop [%s]", ct->name); diff --git a/src/tools.c b/src/tools.c index c6eb0beb161cae1cf75447c2a8b32be477a81c95..80a5ecb2da194cfb40d48039a3ebacf8ddfb2586 100644 --- a/src/tools.c +++ b/src/tools.c @@ -253,56 +253,58 @@ int run_cmd(const char *cmd, char *output, size_t out_len) return ret; } -void delete_autoboot_from_config_file(const char *env) +void set_autoboot_to_config_file(const char *env, bool enable) { + bool found = false; + if (0 != swmod_uci_init(STD_UCI_PATH)) return; - struct uci_section *s = NULL, *stmp = NULL; - swmod_uci_foreach_section_safe(LXC_AUTOBOOT_FILE, "container", stmp, s) { + struct uci_section *s = NULL; + swmod_uci_foreach_section(LXC_AUTOBOOT_FILE, "container", s) { const char *name = swmod_uci_get_value_by_section(s, "name"); - - /* Check name exists, if yes => remove the section */ if (strcmp(name, env) == 0) { - swmod_uci_delete_by_section(s, NULL, NULL); + found = true; break; } } - swmod_uci_fini(LXC_AUTOBOOT_FILE); -} - -void set_autoboot_to_config_file(const char *env) -{ - if (0 != swmod_uci_init(STD_UCI_PATH)) - return; - - struct uci_section *new_s = swmod_uci_add_section(LXC_AUTOBOOT_FILE, "container"); + if (!found) { + s = swmod_uci_add_section(LXC_AUTOBOOT_FILE, "container"); + swmod_uci_set_value_by_section(s, "name", env); + swmod_uci_set_value_by_section(s, "timeout", "300"); + } - swmod_uci_set_value_by_section(new_s, "name", env); - swmod_uci_set_value_by_section(new_s, "timeout", "300"); + swmod_uci_set_value_by_section(s, "enabled", enable ? "1" : "0"); swmod_uci_fini(LXC_AUTOBOOT_FILE); } bool get_autoboot_from_config_file(const char *env) { - bool found = false; + bool enabled = false; if (swmod_uci_init(STD_UCI_PATH) != 0) - return found; + return enabled; struct uci_section *s = NULL; swmod_uci_foreach_section(LXC_AUTOBOOT_FILE, "container", s) { const char *name = swmod_uci_get_value_by_section(s, "name"); if (strcmp(name, env) == 0) { - found = true; + const char *enable = swmod_uci_get_value_by_section(s, "enabled"); + if (enable != NULL && enable[0] != '\0') { + enabled = atoi(enable) ? true : false; + } else { + /* assume enabled by default for compatibility with older configs, + * when enabled option is not present in config file */ + enabled = true; + } break; } } swmod_uci_fini(LXC_AUTOBOOT_FILE); - return found; + return enabled; } bool present_in_buffer_list(struct list_head *plist, char *entry) diff --git a/src/tools.h b/src/tools.h index cb3440387088d2686a80537f624eb0aa3baad406..c9d7cc9b206e99e2680b08f994a58bb8fd5234fb 100644 --- a/src/tools.h +++ b/src/tools.h @@ -83,8 +83,7 @@ void update_eu_from_blob(ExecEnv_t *env, struct blob_attr *msg); int ubus_call_service_state(char *service, bool state); bool dir_exist(const char *dir); bool get_autoboot_from_config_file(const char *env); -void set_autoboot_to_config_file(const char *env); -void delete_autoboot_from_config_file(const char *env); +void set_autoboot_to_config_file(const char *env, bool enable); int get_timeout_from_config_file(const char *env); void swmod_add_eu_in_list(struct list_head *eu_list, ExecUnit *eu); void swmod_delete_eu_list(struct list_head *eu_list);