Reindented server/core/load_utils.c

This commit is contained in:
Johan Wikman
2015-11-30 14:04:07 +02:00
parent 3189a47fc5
commit 60fb05ea40

View File

@ -64,7 +64,8 @@ static void unregister_module(const char *module);
int module_create_feedback_report(GWBUF **buffer, MODULES *modules, FEEDBACK_CONF *cfg); int module_create_feedback_report(GWBUF **buffer, MODULES *modules, FEEDBACK_CONF *cfg);
int do_http_post(GWBUF *buffer, void *cfg); int do_http_post(GWBUF *buffer, void *cfg);
struct MemoryStruct { struct MemoryStruct
{
char *data; char *data;
size_t size; size_t size;
}; };
@ -86,7 +87,8 @@ WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
struct MemoryStruct *mem = (struct MemoryStruct *)userp; struct MemoryStruct *mem = (struct MemoryStruct *)userp;
mem->data = realloc(mem->data, mem->size + realsize + 1); mem->data = realloc(mem->data, mem->size + realsize + 1);
if(mem->data == NULL) { if (mem->data == NULL)
{
/* out of memory! */ /* out of memory! */
MXS_ERROR("Error in module_feedback_send(), not enough memory for realloc"); MXS_ERROR("Error in module_feedback_send(), not enough memory for realloc");
return 0; return 0;
@ -111,13 +113,13 @@ WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
void * void *
load_module(const char *module, const char *type) load_module(const char *module, const char *type)
{ {
char *home, *version; char *home, *version;
char fname[MAXPATHLEN+1]; char fname[MAXPATHLEN+1];
void *dlhandle, *sym; void *dlhandle, *sym;
char *(*ver)(); char *(*ver)();
void *(*ep)(), *modobj; void *(*ep)(), *modobj;
MODULES *mod; MODULES *mod;
MODULE_INFO *mod_info = NULL; MODULE_INFO *mod_info = NULL;
if ((mod = find_module(module)) == NULL) if ((mod = find_module(module)) == NULL)
{ {
@ -244,11 +246,13 @@ MODULE_INFO *mod_info = NULL;
void void
unload_module(const char *module) unload_module(const char *module)
{ {
MODULES *mod = find_module(module); MODULES *mod = find_module(module);
void *handle; void *handle;
if (!mod) if (!mod)
{
return; return;
}
handle = mod->handle; handle = mod->handle;
unregister_module(module); unregister_module(module);
dlclose(handle); dlclose(handle);
@ -264,13 +268,19 @@ void *handle;
static MODULES * static MODULES *
find_module(const char *module) find_module(const char *module)
{ {
MODULES *mod = registered; MODULES *mod = registered;
while (mod) while (mod)
{
if (strcmp(mod->module, module) == 0) if (strcmp(mod->module, module) == 0)
{
return mod; return mod;
}
else else
{
mod = mod->next; mod = mod->next;
}
}
return NULL; return NULL;
} }
@ -286,12 +296,19 @@ MODULES *mod = registered;
* @param mod_info The module information * @param mod_info The module information
*/ */
static void static void
register_module(const char *module, const char *type, void *dlhandle, char *version, void *modobj, MODULE_INFO *mod_info) register_module(const char *module,
const char *type,
void *dlhandle,
char *version,
void *modobj,
MODULE_INFO *mod_info)
{ {
MODULES *mod; MODULES *mod;
if ((mod = malloc(sizeof(MODULES))) == NULL) if ((mod = malloc(sizeof(MODULES))) == NULL)
{
return; return;
}
mod->module = strdup(module); mod->module = strdup(module);
mod->type = strdup(type); mod->type = strdup(type);
mod->handle = dlhandle; mod->handle = dlhandle;
@ -310,19 +327,25 @@ MODULES *mod;
static void static void
unregister_module(const char *module) unregister_module(const char *module)
{ {
MODULES *mod = find_module(module); MODULES *mod = find_module(module);
MODULES *ptr; MODULES *ptr;
if (!mod) if (!mod)
{
return; // Module not found return; // Module not found
}
if (registered == mod) if (registered == mod)
{
registered = mod->next; registered = mod->next;
}
else else
{ {
ptr = registered; ptr = registered;
while (ptr && ptr->next != mod) while (ptr && ptr->next != mod)
{
ptr = ptr->next; ptr = ptr->next;
} }
}
/*< /*<
* The module is now not in the linked list and all * The module is now not in the linked list and all
@ -358,7 +381,7 @@ unload_all_modules()
void void
printModules() printModules()
{ {
MODULES *ptr = registered; MODULES *ptr = registered;
printf("%-15s | %-11s | Version\n", "Module Name", "Module Type"); printf("%-15s | %-11s | Version\n", "Module Name", "Module Type");
printf("-----------------------------------------------------\n"); printf("-----------------------------------------------------\n");
@ -377,7 +400,7 @@ MODULES *ptr = registered;
void void
dprintAllModules(DCB *dcb) dprintAllModules(DCB *dcb)
{ {
MODULES *ptr = registered; MODULES *ptr = registered;
dcb_printf(dcb, "Modules.\n"); dcb_printf(dcb, "Modules.\n");
dcb_printf(dcb, "----------------+-------------+---------+-------+-------------------------\n"); dcb_printf(dcb, "----------------+-------------+---------+-------+-------------------------\n");
@ -415,11 +438,12 @@ MODULES *ptr = registered;
void void
moduleShowFeedbackReport(DCB *dcb) moduleShowFeedbackReport(DCB *dcb)
{ {
GWBUF *buffer; GWBUF *buffer;
MODULES *modules_list = registered; MODULES *modules_list = registered;
FEEDBACK_CONF *feedback_config = config_get_feedback_data(); FEEDBACK_CONF *feedback_config = config_get_feedback_data();
if (!module_create_feedback_report(&buffer, modules_list, feedback_config)) { if (!module_create_feedback_report(&buffer, modules_list, feedback_config))
{
MXS_ERROR("Error in module_create_feedback_report(): gwbuf_alloc() failed to allocate memory"); MXS_ERROR("Error in module_create_feedback_report(): gwbuf_alloc() failed to allocate memory");
return; return;
@ -438,11 +462,11 @@ FEEDBACK_CONF *feedback_config = config_get_feedback_data();
static RESULT_ROW * static RESULT_ROW *
moduleRowCallback(RESULTSET *set, void *data) moduleRowCallback(RESULTSET *set, void *data)
{ {
int *rowno = (int *)data; int *rowno = (int *)data;
int i = 0;; int i = 0;;
char *stat, buf[20]; char *stat, buf[20];
RESULT_ROW *row; RESULT_ROW *row;
MODULES *ptr; MODULES *ptr;
ptr = registered; ptr = registered;
while (i < *rowno && ptr) while (i < *rowno && ptr)
@ -486,11 +510,13 @@ MODULES *ptr;
RESULTSET * RESULTSET *
moduleGetList() moduleGetList()
{ {
RESULTSET *set; RESULTSET *set;
int *data; int *data;
if ((data = (int *)malloc(sizeof(int))) == NULL) if ((data = (int *)malloc(sizeof(int))) == NULL)
{
return NULL; return NULL;
}
*data = 0; *data = 0;
if ((set = resultset_create(moduleRowCallback, data)) == NULL) if ((set = resultset_create(moduleRowCallback, data)) == NULL)
{ {
@ -512,7 +538,8 @@ int *data;
* @param data The configuration details of notification service * @param data The configuration details of notification service
*/ */
void void
module_feedback_send(void* data) { module_feedback_send(void* data)
{
MODULES *modules_list = registered; MODULES *modules_list = registered;
CURL *curl = NULL; CURL *curl = NULL;
CURLcode res; CURLcode res;
@ -538,7 +565,10 @@ module_feedback_send(void* data) {
/* Configuration check */ /* Configuration check */
if (feedback_config->feedback_enable == 0 || feedback_config->feedback_url == NULL || feedback_config->feedback_user_info == NULL) { if (feedback_config->feedback_enable == 0 ||
feedback_config->feedback_url == NULL ||
feedback_config->feedback_user_info == NULL)
{
MXS_ERROR("Error in module_feedback_send(): some mandatory parameters are not set" MXS_ERROR("Error in module_feedback_send(): some mandatory parameters are not set"
" feedback_enable=%u, feedback_url=%s, feedback_user_info=%s", " feedback_enable=%u, feedback_url=%s, feedback_user_info=%s",
feedback_config->feedback_enable, feedback_config->feedback_enable,
@ -557,7 +587,8 @@ module_feedback_send(void* data) {
* If it's done in that time interval, it will be skipped * If it's done in that time interval, it will be skipped
*/ */
if (hour > 4 || hour < 2) { if (hour > 4 || hour < 2)
{
/* It's not the rigt time, mark it as to be done and return */ /* It's not the rigt time, mark it as to be done and return */
feedback_config->feedback_last_action = _NOTIFICATION_SEND_PENDING; feedback_config->feedback_last_action = _NOTIFICATION_SEND_PENDING;
@ -569,7 +600,8 @@ module_feedback_send(void* data) {
} }
/* Time to run the task: if a previous run was succesfull skip next runs */ /* Time to run the task: if a previous run was succesfull skip next runs */
if (feedback_config->feedback_last_action == _NOTIFICATION_SEND_OK) { if (feedback_config->feedback_last_action == _NOTIFICATION_SEND_OK)
{
/* task was done before, return */ /* task was done before, return */
MXS_INFO("module_feedback_send(): execution skipped because of previous " MXS_INFO("module_feedback_send(): execution skipped because of previous "
@ -582,7 +614,8 @@ module_feedback_send(void* data) {
MXS_INFO("module_feedback_send(): task now runs: hour is [%d], last_action [%d]", MXS_INFO("module_feedback_send(): task now runs: hour is [%d], last_action [%d]",
hour, feedback_config->feedback_last_action); hour, feedback_config->feedback_last_action);
if (!module_create_feedback_report(&buffer, modules_list, feedback_config)) { if (!module_create_feedback_report(&buffer, modules_list, feedback_config))
{
MXS_ERROR("Error in module_create_feedback_report(): gwbuf_alloc() failed to allocate memory"); MXS_ERROR("Error in module_create_feedback_report(): gwbuf_alloc() failed to allocate memory");
feedback_config->feedback_last_action = _NOTIFICATION_SEND_ERROR; feedback_config->feedback_last_action = _NOTIFICATION_SEND_ERROR;
@ -593,9 +626,12 @@ module_feedback_send(void* data) {
/* try sending data via http/https post */ /* try sending data via http/https post */
http_send = do_http_post(buffer, feedback_config); http_send = do_http_post(buffer, feedback_config);
if (http_send == 0) { if (http_send == 0)
{
feedback_config->feedback_last_action = _NOTIFICATION_SEND_OK; feedback_config->feedback_last_action = _NOTIFICATION_SEND_OK;
} else { }
else
{
feedback_config->feedback_last_action = _NOTIFICATION_SEND_ERROR; feedback_config->feedback_last_action = _NOTIFICATION_SEND_ERROR;
MXS_INFO("Error in module_create_feedback_report(): do_http_post ret_code is %d", http_send); MXS_INFO("Error in module_create_feedback_report(): do_http_post ret_code is %d", http_send);
@ -622,8 +658,8 @@ module_feedback_send(void* data) {
*/ */
int int
module_create_feedback_report(GWBUF **buffer, MODULES *modules, FEEDBACK_CONF *cfg) { module_create_feedback_report(GWBUF **buffer, MODULES *modules, FEEDBACK_CONF *cfg)
{
MODULES *ptr = modules; MODULES *ptr = modules;
int n_mod = 0; int n_mod = 0;
char *data_ptr=NULL; char *data_ptr=NULL;
@ -631,8 +667,11 @@ module_create_feedback_report(GWBUF **buffer, MODULES *modules, FEEDBACK_CONF *c
time_t now; time_t now;
struct tm *now_tm; struct tm *now_tm;
int report_max_bytes=0; int report_max_bytes=0;
if(buffer == NULL)
if (buffer == NULL)
{
return 0; return 0;
}
now = time(NULL); now = time(NULL);
@ -657,42 +696,46 @@ module_create_feedback_report(GWBUF **buffer, MODULES *modules, FEEDBACK_CONF *c
report_max_bytes = ((n_mod * 4) + 7) * (_NOTIFICATION_REPORT_ROW_LEN + 1); report_max_bytes = ((n_mod * 4) + 7) * (_NOTIFICATION_REPORT_ROW_LEN + 1);
*buffer = gwbuf_alloc(report_max_bytes); *buffer = gwbuf_alloc(report_max_bytes);
if (*buffer == NULL) { if (*buffer == NULL)
{
return 0; return 0;
} }
/* encode MAC-sha1 to HEX */ /* encode MAC-sha1 to HEX */
gw_bin2hex(hex_setup_info, cfg->mac_sha1, SHA_DIGEST_LENGTH); gw_bin2hex(hex_setup_info, cfg->mac_sha1, SHA_DIGEST_LENGTH);
data_ptr = (char *)GWBUF_DATA(*buffer); data_ptr = (char *)GWBUF_DATA(*buffer);
snprintf(data_ptr, _NOTIFICATION_REPORT_ROW_LEN, "FEEDBACK_SERVER_UID\t%s\n", hex_setup_info); snprintf(data_ptr, _NOTIFICATION_REPORT_ROW_LEN, "FEEDBACK_SERVER_UID\t%s\n", hex_setup_info);
data_ptr+=strlen(data_ptr); data_ptr += strlen(data_ptr);
snprintf(data_ptr, _NOTIFICATION_REPORT_ROW_LEN, "FEEDBACK_USER_INFO\t%s\n", cfg->feedback_user_info == NULL ? "not_set" : cfg->feedback_user_info); snprintf(data_ptr, _NOTIFICATION_REPORT_ROW_LEN, "FEEDBACK_USER_INFO\t%s\n",
data_ptr+=strlen(data_ptr); cfg->feedback_user_info == NULL ? "not_set" : cfg->feedback_user_info);
data_ptr += strlen(data_ptr);
snprintf(data_ptr, _NOTIFICATION_REPORT_ROW_LEN, "VERSION\t%s\n", MAXSCALE_VERSION); snprintf(data_ptr, _NOTIFICATION_REPORT_ROW_LEN, "VERSION\t%s\n", MAXSCALE_VERSION);
data_ptr+=strlen(data_ptr); data_ptr += strlen(data_ptr);
snprintf(data_ptr, _NOTIFICATION_REPORT_ROW_LEN * 2, "NOW\t%lu\nPRODUCT\t%s\n", now, "maxscale"); snprintf(data_ptr, _NOTIFICATION_REPORT_ROW_LEN * 2, "NOW\t%lu\nPRODUCT\t%s\n", now, "maxscale");
data_ptr+=strlen(data_ptr); data_ptr += strlen(data_ptr);
snprintf(data_ptr, _NOTIFICATION_REPORT_ROW_LEN, "Uname_sysname\t%s\n", cfg->sysname); snprintf(data_ptr, _NOTIFICATION_REPORT_ROW_LEN, "Uname_sysname\t%s\n", cfg->sysname);
data_ptr+=strlen(data_ptr); data_ptr += strlen(data_ptr);
snprintf(data_ptr, _NOTIFICATION_REPORT_ROW_LEN, "Uname_distribution\t%s\n", cfg->release_info); snprintf(data_ptr, _NOTIFICATION_REPORT_ROW_LEN, "Uname_distribution\t%s\n", cfg->release_info);
data_ptr+=strlen(data_ptr); data_ptr += strlen(data_ptr);
while (ptr) while (ptr)
{ {
snprintf(data_ptr, _NOTIFICATION_REPORT_ROW_LEN * 2, "module_%s_type\t%s\nmodule_%s_version\t%s\n", ptr->module, ptr->type, ptr->module, ptr->version); snprintf(data_ptr, _NOTIFICATION_REPORT_ROW_LEN * 2,
data_ptr+=strlen(data_ptr); "module_%s_type\t%s\nmodule_%s_version\t%s\n",
ptr->module, ptr->type, ptr->module, ptr->version);
data_ptr += strlen(data_ptr);
if (ptr->info) { if (ptr->info)
{
snprintf(data_ptr, _NOTIFICATION_REPORT_ROW_LEN, "module_%s_api\t%d.%d.%d\n", snprintf(data_ptr, _NOTIFICATION_REPORT_ROW_LEN, "module_%s_api\t%d.%d.%d\n",
ptr->module, ptr->module,
ptr->info->api_version.major, ptr->info->api_version.major,
ptr->info->api_version.minor, ptr->info->api_version.minor,
ptr->info->api_version.patch); ptr->info->api_version.patch);
data_ptr+=strlen(data_ptr); data_ptr += strlen(data_ptr);
snprintf(data_ptr, _NOTIFICATION_REPORT_ROW_LEN, "module_%s_releasestatus\t%s\n", snprintf(data_ptr, _NOTIFICATION_REPORT_ROW_LEN, "module_%s_releasestatus\t%s\n",
ptr->module, ptr->module,
ptr->info->status == MODULE_IN_DEVELOPMENT ptr->info->status == MODULE_IN_DEVELOPMENT
@ -705,7 +748,7 @@ module_create_feedback_report(GWBUF **buffer, MODULES *modules, FEEDBACK_CONF *c
? "GA" ? "GA"
: (ptr->info->status == MODULE_EXPERIMENTAL : (ptr->info->status == MODULE_EXPERIMENTAL
? "Experimental" : "Unknown"))))); ? "Experimental" : "Unknown")))));
data_ptr+=strlen(data_ptr); data_ptr += strlen(data_ptr);
} }
ptr = ptr->next; ptr = ptr->next;
} }
@ -721,7 +764,8 @@ module_create_feedback_report(GWBUF **buffer, MODULES *modules, FEEDBACK_CONF *c
* @return 0 on success, != 0 on failure * @return 0 on success, != 0 on failure
*/ */
int int
do_http_post(GWBUF *buffer, void *cfg) { do_http_post(GWBUF *buffer, void *cfg)
{
CURL *curl = NULL; CURL *curl = NULL;
CURLcode res; CURLcode res;
struct curl_httppost *formpost=NULL; struct curl_httppost *formpost=NULL;
@ -779,31 +823,42 @@ do_http_post(GWBUF *buffer, void *cfg) {
res = curl_easy_perform(curl); res = curl_easy_perform(curl);
/* Check for errors */ /* Check for errors */
if(res != CURLE_OK) { if (res != CURLE_OK)
{
ret_code = 2; ret_code = 2;
MXS_ERROR("do_http_post(), curl call for [%s] failed due: %s, %s", MXS_ERROR("do_http_post(), curl call for [%s] failed due: %s, %s",
feedback_config->feedback_url, feedback_config->feedback_url,
curl_easy_strerror(res), curl_easy_strerror(res),
error_message); error_message);
goto cleanup; goto cleanup;
} else { }
else
{
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
} }
if (http_code == 302) { if (http_code == 302)
{
char *from = strstr(chunk.data, "<h1>ok</h1>"); char *from = strstr(chunk.data, "<h1>ok</h1>");
if (from) { if (from)
{
ret_code = 0; ret_code = 0;
} else { }
else
{
ret_code = 3; ret_code = 3;
goto cleanup; goto cleanup;
} }
} else { }
else
{
MXS_ERROR("do_http_post(), Bad HTTP Code from remote server: %lu", http_code); MXS_ERROR("do_http_post(), Bad HTTP Code from remote server: %lu", http_code);
ret_code = 4; ret_code = 4;
goto cleanup; goto cleanup;
} }
} else { }
else
{
MXS_ERROR("do_http_post(), curl object not initialized"); MXS_ERROR("do_http_post(), curl object not initialized");
ret_code = 1; ret_code = 1;
goto cleanup; goto cleanup;
@ -811,12 +866,15 @@ do_http_post(GWBUF *buffer, void *cfg) {
MXS_INFO("do_http_post() ret_code [%d], HTTP code [%ld]", MXS_INFO("do_http_post() ret_code [%d], HTTP code [%ld]",
ret_code, http_code); ret_code, http_code);
cleanup: cleanup:
if (chunk.data) if (chunk.data)
{
free(chunk.data); free(chunk.data);
}
if (curl) { if (curl)
{
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
curl_formfree(formpost); curl_formfree(formpost);
} }