diff --git a/src/swmod.c b/src/swmod.c index 4528f5bfac54c807c19576da63a8bf094486c4d2..36a5b0ab095aae6166aee9c65e1fc7279be1f79c 100644 --- a/src/swmod.c +++ b/src/swmod.c @@ -36,7 +36,7 @@ #include "swmod_lxc.h" #endif -ExecEnv environments[MAX_ENV] = {0}; +static ExecEnv_t g_environments[MAX_ENV] = {0}; ExecUnit exec_units[MAX_ENV] = {0}; enum { @@ -85,14 +85,12 @@ static const struct blobmsg_policy du_uninstall_policy[__DU_UNINSTALL_MAX] = { static void populate_environments(void) { - memset(environments, '\0', sizeof(environments)); - /* Host system */ - populate_host_system_environment(); + populate_host_system_environment(&g_environments[0]); /* Linux containers */ #ifdef SWMOD_LXC - populate_lxc_environment(); + populate_lxc_environment(&g_environments); #endif } @@ -113,20 +111,20 @@ swmod_environment(struct ubus_context *ctx, struct ubus_object *obj, a = blobmsg_open_array(&bb, "environment"); for (i = 0; i < MAX_ENV; i++) { - if (!environments[i].exists) - continue; + if (!g_environments[i].exists) + break; t = blobmsg_open_table(&bb, ""); - blobmsg_add_string(&bb, "name", environments[i].name); - blobmsg_add_string(&bb, "status", environments[i].status); - blobmsg_add_string(&bb, "type", environments[i].type); - blobmsg_add_string(&bb, "vendor", environments[i].vendor); - blobmsg_add_string(&bb, "version", environments[i].version); - blobmsg_add_u64(&bb, "allocated_disk_space", environments[i].allocated_disk_space); - blobmsg_add_u64(&bb, "available_disk_space", environments[i].available_disk_space); - blobmsg_add_u64(&bb, "allocated_memory", environments[i].allocated_memory); - blobmsg_add_u64(&bb, "available_memory", environments[i].available_memory); + blobmsg_add_string(&bb, "name", g_environments[i].name); + blobmsg_add_string(&bb, "status", g_environments[i].status); + blobmsg_add_string(&bb, "type", g_environments[i].type); + blobmsg_add_string(&bb, "vendor", g_environments[i].vendor); + blobmsg_add_string(&bb, "version", g_environments[i].version); + blobmsg_add_u64(&bb, "allocated_disk_space", g_environments[i].allocated_disk_space); + blobmsg_add_u64(&bb, "available_disk_space", g_environments[i].available_disk_space); + blobmsg_add_u64(&bb, "allocated_memory", g_environments[i].allocated_memory); + blobmsg_add_u64(&bb, "available_memory", g_environments[i].available_memory); blobmsg_close_table(&bb, t); } @@ -536,6 +534,7 @@ static struct ubus_object swmod_object = { static int swmod_pre_init(void) { + memset(&g_environments, 0, sizeof(ExecEnv_t) * MAX_ENV); return synchronize_deployment_units_with_map_du_file(); } diff --git a/src/swmod.h b/src/swmod.h index ce442380b97015a9c13071231e046e83dc19065b..af0c6f80c1b020aeea6068d5e4ee6a98bca6dbc8 100644 --- a/src/swmod.h +++ b/src/swmod.h @@ -40,7 +40,7 @@ typedef struct { unsigned long available_disk_space; //AvailableDiskSpace unsigned long allocated_memory; //AllocatedMemory unsigned long available_memory; //AvailableMemory -} ExecEnv; +} ExecEnv_t; typedef struct { bool exists; @@ -57,7 +57,6 @@ typedef struct { int memory_space[MAX_EU]; //MemoryInUse } ExecUnit; -extern ExecEnv environments[MAX_ENV]; extern ExecUnit exec_units[MAX_ENV]; #endif //SWMOD_H diff --git a/src/swmod_host.c b/src/swmod_host.c index 26bd617d0ae92a8804b500141d82736c45825c26..33ec87c02c543bebc3fca387493d30a7cb6a25f6 100644 --- a/src/swmod_host.c +++ b/src/swmod_host.c @@ -30,24 +30,21 @@ #include "swmod_host.h" #include "swmod.h" -static char host_hash[64] = {0}; - -void populate_host_system_environment(void) +void populate_host_system_environment(ExecEnv_t *environments) { + struct utsname utsname; + struct sysinfo sinfo; + environments[0].exists = true; swmod_strncpy(environments[0].name, HOST_SYSTEM, 14); swmod_strncpy(environments[0].status, "Up", 3); - struct utsname utsname; - if (uname(&utsname) >= 0) { swmod_strncpy(environments[0].type, utsname.sysname, 64); swmod_strncpy(environments[0].vendor, utsname.nodename, 128); swmod_strncpy(environments[0].version, utsname.release, 16); } - struct sysinfo sinfo; - if (sysinfo(&sinfo) == 0) { environments[0].allocated_disk_space = 0; // TODO environments[0].available_disk_space = 0; // TODO @@ -56,14 +53,14 @@ void populate_host_system_environment(void) } } -void populate_host_system_deployment_unit(void) +//void populate_host_system_deployment_unit(void) +void populate_opkg_inventory(unsigned host_index, char *opkg_path, char *db_file) { - time_t tm = get_file_mtime(OPKG_INFO_PATH); - unsigned int um = 0; - sscanf(host_hash, "%u", &um); - if (tm == um) + if (opkg_path == NULL || db_file == NULL) { + PRINT_WARNING("Invalid opkg root(%s) or db_file(%s)", opkg_path, db_file); return; + } if (swmod_uci_init(SWMOD_PATH)) return; @@ -72,18 +69,18 @@ void populate_host_system_deployment_unit(void) char pname[256] = {0}; FILE *log; - swmod_uci_foreach_section_safe(SWMOD_MAP_DU, "deployment", stmp, s) { + swmod_uci_foreach_section_safe(db_file, "deployment", stmp, s) { const char *map_du_name = swmod_uci_get_value_by_section(s, "name"); - snprintf(pname, sizeof(pname), OPKG_INFO_PATH"%s.control", map_du_name); + snprintf(pname, sizeof(pname), "%s/%s.control", db_file, map_du_name); /* Check package name exists, if no => remove the section */ if (!file_exists(pname)) - swmod_uci_delete_by_section(s, NULL, NULL); + swmod_uci_set_value_by_section(s, "Status", "Uninstalled"); } - if ((log = popen("opkg list", "r"))) { + if ((log = popen("opkg list-installed", "r"))) { char line[256] = {0}; while (fgets(line, sizeof(line), log) != NULL) { @@ -98,7 +95,7 @@ void populate_host_system_deployment_unit(void) if (check_section_exist_by_option("map_du", "deployment", "name", name)) continue; - struct uci_section *new_s = swmod_uci_add_section(SWMOD_MAP_DU, "deployment"); + struct uci_section *new_s = swmod_uci_add_section(db_file, "deployment"); /* Generate UUID */ char *uuid = generate_uuid(); @@ -110,25 +107,21 @@ void populate_host_system_deployment_unit(void) swmod_uci_set_value_by_section(new_s, "version", version); swmod_uci_set_value_by_section(new_s, "uuid", uuid); swmod_uci_set_value_by_section(new_s, "duid", duid); - swmod_uci_set_value_by_section(new_s, "environment", HOST_SYSTEM); + swmod_uci_set_value_by_section(new_s, "environment", host_index); /* Freed all allocated memory */ FREE(uuid); FREE(duid); /* Get Description from package_name.control and Set it to map_du file */ - get_description_from_package(new_s, name); - - /* Get Config from package_name.list and Set it to map_du file */ - get_config_from_package(new_s, name); + update_opkg_info(opkg_path, name, new_s); } } pclose(log); } - swmod_uci_fini(SWMOD_MAP_DU); + swmod_uci_fini(db_file); - snprintf(host_hash, sizeof(host_hash), "%u", (unsigned int)tm); } void populate_host_system_execution_unit(void) @@ -253,6 +246,45 @@ void populate_host_system_execution_unit(void) swmod_free_data_from_list(&proc_list); } +void update_opkg_info(char *opkg_path, char *pkg_name, struct uci_section *s) +{ + /* Get Description from package_name.control */ + char pkg_path[128] = {0}; + char line[256] = {0}; + char *spch = NULL; + FILE *fp = NULL; + + snprintf(pkg_path, sizeof(pkg_path), "%s/%s.control", opkg_path, pkg_name); + fp = fopen(pkg_path, "r"); + if (fp != NULL) { + while (fgets(line, sizeof(line), fp) != NULL) { + + if ((spch = strstr(line, "Description:"))) { + remove_new_line(spch); + swmod_uci_set_value_by_section(s, "description", spch+14); + break; + } + } + + fclose(fp); + } + + snprintf(pkg_path, sizeof(pkg_path), "%s/%s.list", opkg_path, pkg_name); + fp = fopen(pkg_path, "r"); + if (fp != NULL) { + while (fgets(line, sizeof(line), fp) != NULL) { + + if ((spch = strstr(line, "/etc/config/"))) { + remove_new_line(spch); + swmod_uci_set_value_by_section(s, "config", spch+12); + break; + } + } + + fclose(fp); + } +} + void get_description_from_package(struct uci_section *s, char *pkg_name) { /* Get Description from package_name.control */ diff --git a/src/swmod_host.h b/src/swmod_host.h index 4c4f1049a09b5305a346129f7b4c16a2655cb997..b9cef7298a77ec11d884fd8601a23592fbbd8dbf 100644 --- a/src/swmod_host.h +++ b/src/swmod_host.h @@ -22,8 +22,9 @@ #ifndef HOST_H #define HOST_H +#include "swmod.h" -void populate_host_system_environment(void); +void populate_host_system_environment(ExecEnv_t *env); void populate_host_system_deployment_unit(void); void populate_host_system_execution_unit(void); diff --git a/src/swmod_lxc.c b/src/swmod_lxc.c index 2e1ec2b3d96a7ad80674904b06fa8f7736ff2c42..96ea50086b72bb544dd8ac7d20b86896754121e7 100644 --- a/src/swmod_lxc.c +++ b/src/swmod_lxc.c @@ -138,7 +138,25 @@ static int lxc_attach_run_env_func(void *args) return 0; } -void populate_lxc_environment(void) +int get_updated_env_index(ExecEnv_t *environments, const char *name) +{ + int i, index = MAX_ENV - 1; + + for (i = 0; i < MAX_ENV; i++) { + if (environments[i].exists == false) { + index = i; + break; + } + + if (strcmp(environments[i].name, name) == 0) { + index = i; + break; + } + } + return index; +} + +void populate_lxc_environment(ExecEnv_t *environments[MAX_ENV]) { const char *lxcpath = NULL; @@ -152,11 +170,12 @@ void populate_lxc_environment(void) for (i = 0; i < lxc_nbr; i++) { struct lxc_container *ct = clist[i]; + int index = get_updated_env_index(environments, ct->name); - environments[i+1].exists = true; - swmod_strncpy(environments[i+1].name, ct->name, 32); - swmod_strncpy(environments[i+1].status, (ct->is_running(ct)) ? "Up" : "Disabled", 9); - swmod_strncpy(environments[i+1].type, "Linux Container", 16); + environments[index].exists = true; + swmod_strncpy(environments[index].name, ct->name, 32); + swmod_strncpy(environments[index].status, (ct->is_running(ct)) ? "Up" : "Disabled", 9); + swmod_strncpy(environments[index].type, "Linux Container", 16); if (!ct->is_running(ct)) { PRINT_INFO("lxc container not running"); @@ -172,14 +191,14 @@ void populate_lxc_environment(void) * alloc_mem=<ENV_ALLOCATED_MEMORY> avail_mem=<ENV_AVAILABLE_MEMORY> */ sscanf(lxc_attach_result, "type=%64s vendor=%128s version=%16s alloc_mem=%lu avail_mem=%lu", - environments[i+1].type, - environments[i+1].vendor, - environments[i+1].version, - &(environments[i+1].allocated_memory), - &(environments[i+1].available_memory)); - - environments[i+1].allocated_disk_space = 0; // TODO - environments[i+1].available_disk_space = 0; // TODO + environments[index].type, + environments[index].vendor, + environments[index].version, + &(environments[index].allocated_memory), + &(environments[index].available_memory)); + + environments[index].allocated_disk_space = 0; // TODO + environments[index].available_disk_space = 0; // TODO } lxc_container_put(ct); diff --git a/src/swmod_lxc.h b/src/swmod_lxc.h index 00b6bb04d6c5c978096f87b6f5d8ff0044c3d5c7..f5ec733c018d1a677939f525d497569464dc76d1 100644 --- a/src/swmod_lxc.h +++ b/src/swmod_lxc.h @@ -24,10 +24,11 @@ #define LXC_H #include <stdbool.h> +#include "swmod.h" +void populate_lxc_environment(ExecEnv_t *env[MAX_ENV]); bool lxc_is_supported(const char **lxcpath); const char *get_lxc_path_from_config(void); -void populate_lxc_environment(void); void populate_lxc_deployment_execution_units(int action); int swmod_lxc_install_update_remove_package(const char *package_path, const char *environment, int action); diff --git a/src/tools.c b/src/tools.c index 0d1ab451f2b0a36175412c904f401676c85ad391..d20d50cb95687b1670ef0bc1ba1f0fba53e8b76d 100644 --- a/src/tools.c +++ b/src/tools.c @@ -199,7 +199,9 @@ char *generate_duid(bool sysnchronise, int number) int synchronize_deployment_units_with_map_du_file(void) { /* Host System */ - populate_host_system_deployment_unit(); + //populate_host_system_deployment_unit(); + populate_opkg_inventory(1, OPKG_INFO_PATH, SWMOD_MAP_DU); +void populate_opkg_inventory(unsigned host_index, char *opkg_path, char *db_file) /* Linux Container */ #ifdef SWMOD_LXC