Skip to content
Snippets Groups Projects
Commit 3bfc97cd authored by Amin Ben Ramdhane's avatar Amin Ben Ramdhane
Browse files

Clean up

parent ea25f283
No related branches found
No related tags found
No related merge requests found
......@@ -11,7 +11,7 @@ PROG_LDFLAGS += -luci -lubus -lubox -ljson-c -lblobmsg_json -luuid
all: ${PROG}
${PROG}: $(OBJS)
$(CC) $(PROG_LDFLAGS) -o $@ $^
$(CC) $(PROG_CFLAGS) -o $@ $^ $(PROG_LDFLAGS)
clean:
rm -f *.o $(PROG)
......@@ -20,32 +20,31 @@
* 02110-1301 USA
*/
#include "common.h"
static struct uci_context *uci_ctx_opkg = NULL;
static struct uci_context *uci_ctx_swmod = NULL;
int opkg_uci_init(void)
int swmod_uci_init(void)
{
uci_ctx_opkg = uci_alloc_context();
if (!uci_ctx_opkg) {
uci_ctx_swmod = uci_alloc_context();
if (!uci_ctx_swmod) {
return -1;
}
uci_add_delta_path(uci_ctx_opkg, uci_ctx_opkg->savedir);
uci_set_savedir(uci_ctx_opkg, OPKG_SAVEDIR_PATH);
uci_set_confdir(uci_ctx_opkg, OPKG_CONFDIR_PATH);
uci_add_delta_path(uci_ctx_swmod, uci_ctx_swmod->savedir);
uci_set_savedir(uci_ctx_swmod, SWMOD_PATH);
uci_set_confdir(uci_ctx_swmod, SWMOD_PATH);
return 0;
}
int opkg_uci_fini(void)
int swmod_uci_fini(void)
{
if (uci_ctx_opkg) {
uci_free_context(uci_ctx_opkg);
if (uci_ctx_swmod) {
uci_free_context(uci_ctx_swmod);
}
return 0;
}
static bool opkg_uci_validate_section(const char *str)
static bool swmod_uci_validate_section(const char *str)
{
if (!*str)
return false;
......@@ -61,7 +60,7 @@ static bool opkg_uci_validate_section(const char *str)
return true;
}
static int opkg_uci_init_ptr(struct uci_ptr *ptr, char *package, char *section, char *option, char *value)
static int swmod_uci_init_ptr(struct uci_ptr *ptr, char *package, char *section, char *option, char *value)
{
memset(ptr, 0, sizeof(struct uci_ptr));
......@@ -88,7 +87,7 @@ static int opkg_uci_init_ptr(struct uci_ptr *ptr, char *package, char *section,
}
lastval:
if (ptr->section && !opkg_uci_validate_section(ptr->section))
if (ptr->section && !swmod_uci_validate_section(ptr->section))
ptr->flags |= UCI_LOOKUP_EXTENDED;
return 0;
......@@ -97,7 +96,7 @@ error:
return -1;
}
struct uci_section *opkg_uci_walk_section(char *package, char *section_type, struct uci_section *prev_section)
struct uci_section *swmod_uci_walk_section(char *package, char *section_type, struct uci_section *prev_section)
{
struct uci_ptr ptr;
struct uci_element *e;
......@@ -113,10 +112,10 @@ struct uci_section *opkg_uci_walk_section(char *package, char *section_type, str
return next_section;
}
else {
if (opkg_uci_init_ptr(&ptr, package, NULL, NULL, NULL)) {
if (swmod_uci_init_ptr(&ptr, package, NULL, NULL, NULL)) {
return NULL;
}
if (uci_lookup_ptr(uci_ctx_opkg, &ptr, NULL, true) != UCI_OK) {
if (uci_lookup_ptr(uci_ctx_swmod, &ptr, NULL, true) != UCI_OK) {
return NULL;
}
if (ptr.p->sections.next == &ptr.p->sections)
......@@ -135,10 +134,10 @@ struct uci_section *opkg_uci_walk_section(char *package, char *section_type, str
shead = &prev_section->package->sections;
}
else {
if (opkg_uci_init_ptr(&ptr, package, NULL, NULL, NULL)) {
if (swmod_uci_init_ptr(&ptr, package, NULL, NULL, NULL)) {
return NULL;
}
if (uci_lookup_ptr(uci_ctx_opkg, &ptr, NULL, true) != UCI_OK) {
if (uci_lookup_ptr(uci_ctx_swmod, &ptr, NULL, true) != UCI_OK) {
return NULL;
}
ul = &ptr.p->sections;
......@@ -156,7 +155,7 @@ struct uci_section *opkg_uci_walk_section(char *package, char *section_type, str
return NULL;
}
void opkg_uci_print_list(struct uci_list *uh, char **val, char *delimiter)
void swmod_uci_print_list(struct uci_list *uh, char **val, char *delimiter)
{
struct uci_element *e;
static char buffer[512];
......@@ -174,7 +173,7 @@ void opkg_uci_print_list(struct uci_list *uh, char **val, char *delimiter)
*val = buf;
}
struct uci_element *opkg_uci_lookup_list(struct uci_list *list, const char *name)
struct uci_element *swmod_uci_lookup_list(struct uci_list *list, const char *name)
{
struct uci_element *e;
......@@ -185,7 +184,7 @@ struct uci_element *opkg_uci_lookup_list(struct uci_list *list, const char *name
return NULL;
}
int opkg_uci_lookup_ptr_by_section(struct uci_ptr *ptr, struct uci_section *section, char *option, char *value)
int swmod_uci_lookup_ptr_by_section(struct uci_ptr *ptr, struct uci_section *section, char *option, char *value)
{
struct uci_element *e;
memset(ptr, 0, sizeof(struct uci_ptr));
......@@ -200,7 +199,7 @@ int opkg_uci_lookup_ptr_by_section(struct uci_ptr *ptr, struct uci_section *sect
ptr->s = section;
if (ptr->option) {
e = opkg_uci_lookup_list(&ptr->s->options, ptr->option);
e = swmod_uci_lookup_list(&ptr->s->options, ptr->option);
if (!e)
return UCI_OK;
ptr->o = uci_to_option(e);
......@@ -217,19 +216,19 @@ int opkg_uci_lookup_ptr_by_section(struct uci_ptr *ptr, struct uci_section *sect
return UCI_OK;
}
char *opkg_uci_get_value_by_section(struct uci_section *section, char *option)
char *swmod_uci_get_value_by_section(struct uci_section *section, char *option)
{
struct uci_ptr ptr;
char *val = "";
if (opkg_uci_lookup_ptr_by_section(&ptr, section, option, NULL) != UCI_OK)
if (swmod_uci_lookup_ptr_by_section(&ptr, section, option, NULL) != UCI_OK)
return val;
if (!ptr.o)
return val;
if(ptr.o->type == UCI_TYPE_LIST) {
opkg_uci_print_list(&ptr.o->v.list, &val, " ");
swmod_uci_print_list(&ptr.o->v.list, &val, " ");
return val;
}
......@@ -239,7 +238,7 @@ char *opkg_uci_get_value_by_section(struct uci_section *section, char *option)
return val;
}
char *opkg_uci_set_value_by_section(struct uci_section *section, char *option, char *value)
char *swmod_uci_set_value_by_section(struct uci_section *section, char *option, char *value)
{
struct uci_ptr ptr;
int ret = UCI_OK;
......@@ -247,13 +246,13 @@ char *opkg_uci_set_value_by_section(struct uci_section *section, char *option, c
if (section == NULL)
return "";
if (opkg_uci_lookup_ptr_by_section(&ptr, section, option, value) != UCI_OK)
if (swmod_uci_lookup_ptr_by_section(&ptr, section, option, value) != UCI_OK)
return "";
uci_set(uci_ctx_opkg, &ptr);
uci_set(uci_ctx_swmod, &ptr);
if (ret == UCI_OK)
ret = uci_save(uci_ctx_opkg, ptr.p);
ret = uci_save(uci_ctx_swmod, ptr.p);
if (ptr.o && ptr.o->v.string)
return ptr.o->v.string;
......@@ -261,35 +260,35 @@ char *opkg_uci_set_value_by_section(struct uci_section *section, char *option, c
return "";
}
struct uci_section *opkg_uci_add_section(char *package, char *section_type)
struct uci_section *swmod_uci_add_section(char *package, char *section_type)
{
struct uci_ptr ptr;
struct uci_section *s = NULL;
if (opkg_uci_init_ptr(&ptr, package, NULL, NULL, NULL))
if (swmod_uci_init_ptr(&ptr, package, NULL, NULL, NULL))
return NULL;
if (uci_lookup_ptr(uci_ctx_opkg, &ptr, NULL, true) != UCI_OK)
if (uci_lookup_ptr(uci_ctx_swmod, &ptr, NULL, true) != UCI_OK)
return NULL;
int ret = uci_add_section(uci_ctx_opkg, ptr.p, section_type, &s);
int ret = uci_add_section(uci_ctx_swmod, ptr.p, section_type, &s);
if (ret != UCI_OK)
return NULL;
return s;
}
int opkg_uci_delete_by_section(struct uci_section *section, char *option, char *value)
int swmod_uci_delete_by_section(struct uci_section *section, char *option, char *value)
{
struct uci_ptr ptr = {0};
if (section == NULL)
return -1;
if (opkg_uci_lookup_ptr_by_section(&ptr, section, option, value) != UCI_OK)
if (swmod_uci_lookup_ptr_by_section(&ptr, section, option, value) != UCI_OK)
return -1;
if (uci_delete(uci_ctx_opkg, &ptr) != UCI_OK)
if (uci_delete(uci_ctx_swmod, &ptr) != UCI_OK)
return -1;
return 0;
......@@ -306,7 +305,7 @@ void remove_newline(char *buf)
char *get_package_name(char *full_name)
{
static char name[32] = "";
strncpy(name, full_name, sizeof(name));
swmod_strncpy(name, full_name, sizeof(name));
char *dot = strchr(name, '.');
if (dot)
*dot = '\0';
......@@ -323,7 +322,7 @@ int isfileexist(const char *filepath)
int islxcrunning(char *lxc_name)
{
char command[256], state[16];
char command[256] = {0}, state[16] = {0};
FILE *in;
if (isfileexist(LXC_INFO)) {
......@@ -343,7 +342,7 @@ int islxcrunning(char *lxc_name)
char *convert_url(char *url, char *username, char *password)
{
static char buf[256] = "";
static char buf[256]= {0};
if (strncmp(url, "http://", 7) == 0)
snprintf(buf, sizeof(buf), "http://%s:%s@%s", username, password, url+7);
......@@ -364,51 +363,50 @@ char *generate_uuid(void)
char *generate_duid(bool sysnchronise, int number)
{
char buf[36] = "0123456789abcdefghijklmnopqrstuvwxyz", euid[64] = "", euid_num[8] = "";
char buf[36] = "0123456789abcdefghijklmnopqrstuvwxyz", euid[16] = {0}, euid_num[8] = {0};
int i;
srand(time(NULL));
if (sysnchronise) {
for (i = 0; i < 59; i++)
for (i = 0; i < 3; i++)
euid[i] = buf[rand() % 35];
euid[59] = '\0';
sprintf(euid_num, "%04d", number);
strcat(euid, euid_num);
strncat(euid, euid_num, 4);
} else {
for (i = 0; i < 62; i++)
for (i = 0; i < 7; i++)
euid[i] = buf[rand() % 35];
euid[63] = '\0';
}
euid[7] = '\0';
return strdup(euid);
}
int synchronize_deployment_units_with_map_du_file(void)
{
struct uci_section *s = NULL, *stmp = NULL;
char path[256], line[256], name[64], version[64], command[256];
char path[256] = {0}, line[256] = {0}, name[64] = {0}, version[64] = {0}, command[512] = {0};
FILE *log;
DIR *dir;
struct dirent *ent;
int found, incr = 0;
opkg_uci_foreach_section_safe("map_du", "deployment", stmp, s) {
char *map_du_name = opkg_uci_get_value_by_section(s, "name");
char *map_du_env = opkg_uci_get_value_by_section(s, "environment");
swmod_uci_foreach_section_safe("map_du", "deployment", stmp, s) {
char *map_du_name = swmod_uci_get_value_by_section(s, "name");
char *map_du_env = swmod_uci_get_value_by_section(s, "environment");
if (strcmp(map_du_env, "OpenWRT_Linux") == 0)
snprintf(path, sizeof(path), "%s/%s.control", OPKG_INFO_PATH, map_du_name);
else
snprintf(path, sizeof(path), "%s/%s/rootfs/%s/%s.control", LXC_PATH, map_du_env, OPKG_INFO_PATH, map_du_name);
if (!isfileexist(path))
opkg_uci_delete_by_section(s, NULL, NULL);
swmod_uci_delete_by_section(s, NULL, NULL);
}
if ((log = popen("opkg list", "r"))) {
while(fgets(line, sizeof(line), log) != NULL) {
found = 0;
if (sscanf(line, "%63s - %63s", name, version)) {
opkg_uci_foreach_section("map_du", "deployment", s) {
char *map_name = opkg_uci_get_value_by_section(s, "name");
char *map_env = opkg_uci_get_value_by_section(s, "environment");
swmod_uci_foreach_section("map_du", "deployment", s) {
char *map_name = swmod_uci_get_value_by_section(s, "name");
char *map_env = swmod_uci_get_value_by_section(s, "environment");
if ((strcmp(map_name, name) == 0) && (strcmp(map_env, "OpenWRT_Linux") == 0)) {
found = 1;
break;
......@@ -417,15 +415,15 @@ int synchronize_deployment_units_with_map_du_file(void)
if (found)
continue;
struct uci_section *new_s = opkg_uci_add_section("map_du", "deployment");
struct uci_section *new_s = swmod_uci_add_section("map_du", "deployment");
char *uuid = generate_uuid();
char *duid = generate_duid(true, incr);
incr++;
opkg_uci_set_value_by_section(new_s, "name", name);
opkg_uci_set_value_by_section(new_s, "version", version);
opkg_uci_set_value_by_section(new_s, "uuid", uuid);
opkg_uci_set_value_by_section(new_s, "duid", duid);
opkg_uci_set_value_by_section(new_s, "environment", "OpenWRT_Linux");
swmod_uci_set_value_by_section(new_s, "name", name);
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", "OpenWRT_Linux");
FREE(uuid);
FREE(duid);
}
......@@ -442,9 +440,9 @@ int synchronize_deployment_units_with_map_du_file(void)
while(fgets(line, sizeof(line), log) != NULL) {
found = 0;
if (sscanf(line, "%63s - %63s", name, version)) {
opkg_uci_foreach_section("map_du", "deployment", s) {
char *map_name = opkg_uci_get_value_by_section(s, "name");
char *map_env = opkg_uci_get_value_by_section(s, "environment");
swmod_uci_foreach_section("map_du", "deployment", s) {
char *map_name = swmod_uci_get_value_by_section(s, "name");
char *map_env = swmod_uci_get_value_by_section(s, "environment");
if ((strcmp(map_name, name) == 0) && (strcmp(map_env, ent->d_name) == 0)) {
found = 1;
break;
......@@ -453,15 +451,15 @@ int synchronize_deployment_units_with_map_du_file(void)
if (found)
continue;
struct uci_section *new_s = opkg_uci_add_section("map_du", "deployment");
struct uci_section *new_s = swmod_uci_add_section("map_du", "deployment");
char *uuid = generate_uuid();
char *duid = generate_duid(true, incr);
incr++;
opkg_uci_set_value_by_section(new_s, "name", name);
opkg_uci_set_value_by_section(new_s, "version", version);
opkg_uci_set_value_by_section(new_s, "uuid", uuid);
opkg_uci_set_value_by_section(new_s, "duid", duid);
opkg_uci_set_value_by_section(new_s, "environment", ent->d_name);
swmod_uci_set_value_by_section(new_s, "name", name);
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", ent->d_name);
FREE(uuid);
FREE(duid);
}
......@@ -474,7 +472,7 @@ int synchronize_deployment_units_with_map_du_file(void)
return 0;
}
void opkg_add_data_to_list(struct list_head *dup_list, char *cmd, int pid, unsigned int vsize)
void swmod_add_data_to_list(struct list_head *dup_list, char *cmd, int pid, unsigned int vsize)
{
struct process_res *proc;
proc = calloc(1, sizeof(struct process_res));
......@@ -484,18 +482,18 @@ void opkg_add_data_to_list(struct list_head *dup_list, char *cmd, int pid, unsig
proc->vsize= vsize;
}
static void opkg_delete_data_from_list(struct process_res *proc)
static void swmod_delete_data_from_list(struct process_res *proc)
{
list_del(&proc->list);
FREE(proc->cmd);
FREE(proc);
}
void opkg_free_data_from_list(struct list_head *dup_list)
void swmod_free_data_from_list(struct list_head *dup_list)
{
struct process_res *proc;
while (dup_list->next != dup_list) {
proc = list_entry(dup_list->next, struct process_res, list);
opkg_delete_data_from_list(proc);
swmod_delete_data_from_list(proc);
}
}
......@@ -33,9 +33,8 @@
#include <uci.h>
#include <libubox/list.h>
#define OPKG_SAVEDIR_PATH "/etc/swmod"
#define OPKG_CONFDIR_PATH "/etc/swmod"
#define OPKG_INFO_PATH "/usr/lib/swmod/info"
#define SWMOD_PATH "/etc/swmod"
#define OPKG_INFO_PATH "/usr/lib/opkg/info"
#define DEPLOYMENT_UNIT_PATH "/etc/swmod/map_du"
#define PROC_PATH "/proc"
#define LXC_PATH "/srv/lxc"
......@@ -53,6 +52,12 @@
#define FREE(x) do { if(x) {free(x); x = NULL;} } while (0)
#endif
#define swmod_strncpy(dst, src, size) \
do { \
strncpy(dst, src, size - 1);\
dst[size-1] = '\0';\
} while(0)
struct process_res
{
struct list_head list;
......@@ -61,13 +66,13 @@ struct process_res
unsigned int vsize;
};
int opkg_uci_init(void);
int opkg_uci_fini(void);
struct uci_section *opkg_uci_walk_section(char *package, char *section_type, struct uci_section *prev_section);
struct uci_section *opkg_uci_add_section(char *package, char *section_type);
int opkg_uci_delete_by_section(struct uci_section *section, char *option, char *value);
char *opkg_uci_get_value_by_section(struct uci_section *section, char *option);
char *opkg_uci_set_value_by_section(struct uci_section *section, char *option, char *value);
int swmod_uci_init(void);
int swmod_uci_fini(void);
struct uci_section *swmod_uci_walk_section(char *package, char *section_type, struct uci_section *prev_section);
struct uci_section *swmod_uci_add_section(char *package, char *section_type);
int swmod_uci_delete_by_section(struct uci_section *section, char *option, char *value);
char *swmod_uci_get_value_by_section(struct uci_section *section, char *option);
char *swmod_uci_set_value_by_section(struct uci_section *section, char *option, char *value);
char *get_package_name(char *full_name);
int isfileexist(const char *filepath);
int synchronize_deployment_units_with_map_du_file(void);
......@@ -76,19 +81,19 @@ char *generate_uuid(void);
char *generate_duid(bool sysnchronise, int number);
void remove_newline(char *buf);
int islxcrunning(char *lxc_name);
void opkg_add_data_to_list(struct list_head *dup_list, char *cmd, int pid, unsigned int vsize);
void opkg_free_data_from_list(struct list_head *dup_list);
void swmod_add_data_to_list(struct list_head *dup_list, char *cmd, int pid, unsigned int vsize);
void swmod_free_data_from_list(struct list_head *dup_list);
#define opkg_uci_foreach_section(package, section_type, section) \
for (section = opkg_uci_walk_section(package, section_type, NULL); \
#define swmod_uci_foreach_section(package, section_type, section) \
for (section = swmod_uci_walk_section(package, section_type, NULL); \
section != NULL; \
section = opkg_uci_walk_section(package, section_type, section))
section = swmod_uci_walk_section(package, section_type, section))
#define opkg_uci_foreach_section_safe(package, section_type, _tmp, section) \
for(section = opkg_uci_walk_section(package, section_type, NULL), \
_tmp = (section) ? opkg_uci_walk_section(package, section_type, section) : NULL; \
#define swmod_uci_foreach_section_safe(package, section_type, _tmp, section) \
for(section = swmod_uci_walk_section(package, section_type, NULL), \
_tmp = (section) ? swmod_uci_walk_section(package, section_type, section) : NULL; \
section != NULL; \
section = _tmp, _tmp = (section) ? opkg_uci_walk_section(package, section_type, section) : NULL)
section = _tmp, _tmp = (section) ? swmod_uci_walk_section(package, section_type, section) : NULL)
#endif //__COMMON_H
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment