Reindented server/core/load_utils.c
This commit is contained in:
@ -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;
|
||||||
@ -248,7 +250,9 @@ 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);
|
||||||
@ -267,10 +271,16 @@ 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;
|
||||||
@ -314,15 +331,21 @@ 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
|
||||||
@ -419,7 +442,8 @@ 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;
|
||||||
@ -490,7 +514,9 @@ 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,19 +696,20 @@ 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",
|
||||||
|
cfg->feedback_user_info == NULL ? "not_set" : cfg->feedback_user_info);
|
||||||
data_ptr += strlen(data_ptr);
|
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);
|
||||||
@ -682,10 +722,13 @@ module_create_feedback_report(GWBUF **buffer, MODULES *modules, FEEDBACK_CONF *c
|
|||||||
|
|
||||||
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,
|
||||||
|
"module_%s_type\t%s\nmodule_%s_version\t%s\n",
|
||||||
|
ptr->module, ptr->type, ptr->module, ptr->version);
|
||||||
data_ptr += strlen(data_ptr);
|
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,
|
||||||
@ -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;
|
||||||
@ -814,9 +869,12 @@ do_http_post(GWBUF *buffer, void *cfg) {
|
|||||||
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);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user