diff --git a/server/core/adminusers.c b/server/core/adminusers.c index f551fee0f..3f1472956 100644 --- a/server/core/adminusers.c +++ b/server/core/adminusers.c @@ -153,17 +153,20 @@ char fname[1024], *home, *cpasswd; sprintf(fname, "%s/etc/passwd", home); else sprintf(fname, "/usr/local/skysql/MaxScale/etc/passwd"); + if (users == NULL) { - skygw_log_write( LOGFILE_MESSAGE, "Create initial password file.\n"); + skygw_log_write(LOGFILE_MESSAGE,"Create initial password file."); + if ((users = users_alloc()) == NULL) return ADMIN_ERR_NOMEM; if ((fp = fopen(fname, "w")) == NULL) { - skygw_log_write( LOGFILE_ERROR, - "Unable to create password file %s.\n", - fname); - return ADMIN_ERR_PWDFILEOPEN; + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Unable to create password file %s.", + fname); + return ADMIN_ERR_PWDFILEOPEN; } fclose(fp); } @@ -175,10 +178,10 @@ char fname[1024], *home, *cpasswd; users_add(users, uname, cpasswd); if ((fp = fopen(fname, "a")) == NULL) { - skygw_log_write( LOGFILE_ERROR, - "Unable to append to password file %s.\n", - fname); - return ADMIN_ERR_FILEAPPEND; + skygw_log_write_flush(LOGFILE_ERROR, + "Error : Unable to append to password file %s.", + fname); + return ADMIN_ERR_FILEAPPEND; } fprintf(fp, "%s:%s\n", uname, cpasswd); fclose(fp); @@ -209,18 +212,19 @@ char* admin_remove_user( int n_deleted; if (!admin_search_user(uname)) { - skygw_log_write( - LOGFILE_MESSAGE, - "Couldn't find user %s. Removing user failed", uname); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Couldn't find user %s. Removing user failed", + uname); return ADMIN_ERR_USERNOTFOUND; } if (admin_verify(uname, passwd) == 0) { - skygw_log_write( - LOGFILE_MESSAGE, - "Authentication failed, wrong user/password combination.\n" - "Removing user failed"); - return ADMIN_ERR_AUTHENTICATION; + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Authentication failed, wrong user/password " + "combination. Removing user failed."); + return ADMIN_ERR_AUTHENTICATION; } @@ -228,61 +232,66 @@ char* admin_remove_user( n_deleted = users_delete(users, uname); if (n_deleted == 0) { - skygw_log_write( - LOGFILE_MESSAGE, - "Deleting the only user is forbidden. add new user " - "before deleting the old one.\n"); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Deleting the only user is forbidden. Add new " + "user before deleting the one."); return ADMIN_ERR_DELLASTUSER; } /** * Open passwd file and remove user from the file. */ if ((home = getenv("MAXSCALE_HOME")) != NULL) { - sprintf(fname, "%s/etc/passwd", home); - sprintf(fname_tmp, "%s/etc/passwd_tmp", home); + sprintf(fname, "%s/etc/passwd", home); + sprintf(fname_tmp, "%s/etc/passwd_tmp", home); } else { - sprintf(fname, "/usr/local/skysql/MaxScale/etc/passwd"); - sprintf(fname_tmp, "/usr/local/skysql/MaxScale/etc/passwd_tmp"); + sprintf(fname, "/usr/local/skysql/MaxScale/etc/passwd"); + sprintf(fname_tmp, "/usr/local/skysql/MaxScale/etc/passwd_tmp"); } /** * Rewrite passwd file from memory. */ if ((fp = fopen(fname, "r")) == NULL) { - int err = errno; - skygw_log_write( LOGFILE_ERROR, - "Unable to open password file %s : errno %d.\n" - "Removing user from file failed; it must be done manually.", - fname, - err); - return ADMIN_ERR_PWDFILEOPEN; + int err = errno; + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Unable to open password file %s : errno %d.\n" + "Removing user from file failed; it must be done " + "manually.", + fname, + err); + return ADMIN_ERR_PWDFILEOPEN; } /** * Open temporary passwd file. */ if ((fp_tmp = fopen(fname_tmp, "w")) == NULL) { - int err = errno; - skygw_log_write( LOGFILE_ERROR, - "Unable to open tmp file %s : errno %d.\n" - "Removing user from passwd file failed; " - "it must be done manually.", - fname_tmp, - err); - fclose(fp); - return ADMIN_ERR_TMPFILEOPEN; + int err = errno; + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Unable to open tmp file %s : errno %d.\n" + "Removing user from passwd file failed; it must be done " + "manually.", + fname_tmp, + err); + fclose(fp); + return ADMIN_ERR_TMPFILEOPEN; } - + /** * Scan passwd and copy all but matching lines to temp file. */ if (fgetpos(fp, &rpos) != 0) { int err = errno; - skygw_log_write( LOGFILE_ERROR, - "Unable to process passwd file %s : errno %d.\n" - "Removing user from file failed, and must be done manually.", - fname, - err); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Unable to process passwd file %s : errno %d.\n" + "Removing user from file failed, and must be done " + "manually.", + fname, + err); fclose(fp); unlink(fname_tmp); return ADMIN_ERR_PWDFILEACCESS; @@ -290,28 +299,30 @@ char* admin_remove_user( while (fscanf(fp, "%[^:]:%s\n", fusr, fpwd) == 2) { - /** - * Compare username what was found from passwd file. - * Unmatching lines are copied to tmp file. - */ - if (strncmp(uname, fusr, strlen(uname)+1) != 0) { - fsetpos(fp, &rpos); /** one step back */ - fgets(line, LINELEN, fp); - fputs(line, fp_tmp); - } + /** + * Compare username what was found from passwd file. + * Unmatching lines are copied to tmp file. + */ + if (strncmp(uname, fusr, strlen(uname)+1) != 0) { + fsetpos(fp, &rpos); /** one step back */ + fgets(line, LINELEN, fp); + fputs(line, fp_tmp); + } - if (fgetpos(fp, &rpos) != 0) { - int err = errno; - skygw_log_write( LOGFILE_ERROR, - "Unable to process passwd file %s : errno %d.\n" + if (fgetpos(fp, &rpos) != 0) { + int err = errno; + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Unable to process passwd file %s : " + "errno %d.\n" "Removing user from file failed, and must be " "done manually.", fname, err); - fclose(fp); - unlink(fname_tmp); - return ADMIN_ERR_PWDFILEACCESS; - } + fclose(fp); + unlink(fname_tmp); + return ADMIN_ERR_PWDFILEACCESS; + } } fclose(fp); /** @@ -319,17 +330,18 @@ char* admin_remove_user( */ if (rename(fname_tmp, fname)) { int err = errno; - skygw_log_write( LOGFILE_ERROR, - "Unable to rename new passwd file %s : errno %d.\n" - "Rename it to %s manually.", - fname_tmp, - err, - fname); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Unable to rename new passwd file %s : errno " + "%d.\n" + "Rename it to %s manually.", + fname_tmp, + err, + fname); unlink(fname_tmp); fclose(fp_tmp); return ADMIN_ERR_PWDFILEACCESS; } - fclose(fp_tmp); return ADMIN_SUCCESS; } diff --git a/server/core/config.c b/server/core/config.c index 473fc2f54..e18b59611 100644 --- a/server/core/config.c +++ b/server/core/config.c @@ -178,49 +178,78 @@ int error_count = 0; char *type = config_get_value(obj->parameters, "type"); if (type == NULL) { - skygw_log_write( LOGFILE_ERROR, "Configuration object '%s' has no type\n", obj->object); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Configuration object '%s' has no type.", + obj->object); error_count++; } else if (!strcmp(type, "service")) { - char *router = config_get_value(obj->parameters, "router"); - if (router) - { + char *router = config_get_value(obj->parameters, + "router"); + if (router) + { obj->element = service_alloc(obj->object, router); - char *user = config_get_value(obj->parameters, "user"); - char *auth = config_get_value(obj->parameters, "auth"); + char *user = + config_get_value(obj->parameters, "user"); + char *auth = + config_get_value(obj->parameters, "auth"); if (obj->element && user && auth) serviceSetUser(obj->element, user, auth); } else { obj->element = NULL; - skygw_log_write( LOGFILE_ERROR, "No router defined for service '%s'\n", - obj->object); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : No router defined for service " + "'%s'\n", + obj->object); error_count++; } } else if (!strcmp(type, "server")) { - char *address = config_get_value(obj->parameters, "address"); - char *port = config_get_value(obj->parameters, "port"); - char *protocol = config_get_value(obj->parameters, "protocol"); - char *monuser = config_get_value(obj->parameters, "monitoruser"); - char *monpw = config_get_value(obj->parameters, "monitorpw"); + char *address; + char *port; + char *protocol; + char *monuser; + char *monpw; + + address = config_get_value(obj->parameters, "address"); + port = config_get_value(obj->parameters, "port"); + protocol = config_get_value(obj->parameters, "protocol"); + monuser = config_get_value(obj->parameters,"monitoruser"); + monpw = config_get_value(obj->parameters, "monitorpw"); + if (address && port && protocol) - obj->element = server_alloc(address, protocol, atoi(port)); + obj->element = server_alloc(address, + protocol, + atoi(port)); else { obj->element = NULL; - skygw_log_write(LOGFILE_ERROR, "Server '%s' is missing a required configuration parameter. A server must have address, port and protocol defined.\n", obj->object); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Server '%s' is missing a " + "required configuration parameter. A " + "server must " + "have address, port and protocol " + "defined.", + obj->object); error_count++; } if (obj->element && monuser && monpw) serverAddMonUser(obj->element, monuser, monpw); else - skygw_log_write(LOGFILE_ERROR, "Warning: server '%s' has no valid monitor user defined. The server may not be monitored.\n", obj->object); + skygw_log_write_flush( + LOGFILE_ERROR, + "Warning : server '%s' has no valid " + "monitor user defined. The server may " + "not be monitored.\n", + obj->object); } - obj = obj->next; } @@ -236,8 +265,12 @@ int error_count = 0; ; else if (!strcmp(type, "service")) { - char *servers = config_get_value(obj->parameters, "servers"); - char *roptions = config_get_value(obj->parameters, "router_options"); + char *servers; + char *roptions; + + servers = config_get_value(obj->parameters, "servers"); + roptions = config_get_value(obj->parameters, + "router_options"); if (servers && obj->element) { char *s = strtok(servers, ","); @@ -246,8 +279,13 @@ int error_count = 0; CONFIG_CONTEXT *obj1 = context; while (obj1) { - if (strcmp(s, obj1->object) == 0 && obj->element && obj1->element) - serviceAddBackend(obj->element, obj1->element); + if (strcmp(s, obj1->object) == 0 && + obj->element && obj1->element) + { + serviceAddBackend( + obj->element, + obj1->element); + } obj1 = obj1->next; } s = strtok(NULL, ","); @@ -255,7 +293,12 @@ int error_count = 0; } else if (servers == NULL) { - skygw_log_write(LOGFILE_ERROR, "The service '%s' is missing a definition of the servers that provide the service.\n", obj->object); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : The service '%s' is missing a " + "definition of the servers that provide " + "the service.", + obj->object); } if (roptions && obj->element) { @@ -269,30 +312,49 @@ int error_count = 0; } else if (!strcmp(type, "listener")) { - char *service = config_get_value(obj->parameters, "service"); - char *port = config_get_value(obj->parameters, "port"); - char *protocol = config_get_value(obj->parameters, "protocol"); + char *service; + char *port; + char *protocol; + + service = config_get_value(obj->parameters, "service"); + port = config_get_value(obj->parameters, "port"); + protocol = config_get_value(obj->parameters, "protocol"); + if (service && port && protocol) { CONFIG_CONTEXT *ptr = context; while (ptr && strcmp(ptr->object, service) != 0) ptr = ptr->next; if (ptr && ptr->element) - serviceAddProtocol(ptr->element, protocol, atoi(port)); + serviceAddProtocol(ptr->element, + protocol, + atoi(port)); } else { - skygw_log_write(LOGFILE_ERROR, "Listener '%s' is misisng a required parameter. A Listener must have a service, port and protocol defined.\n", obj->object); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Listener '%s' is misisng a " + "required " + "parameter. A Listener must have a " + "service, port and protocol defined.", + obj->object); error_count++; } } else if (!strcmp(type, "monitor")) { - char *module = config_get_value(obj->parameters, "module"); - char *servers = config_get_value(obj->parameters, "servers"); - char *user = config_get_value(obj->parameters, "user"); - char *passwd = config_get_value(obj->parameters, "passwd"); - if (module) + char *module; + char *servers; + char *user; + char *passwd; + + module = config_get_value(obj->parameters, "module"); + servers = config_get_value(obj->parameters, "servers"); + user = config_get_value(obj->parameters, "user"); + passwd = config_get_value(obj->parameters, "passwd"); + + if (module) { obj->element = monitor_alloc(obj->object, module); if (servers && obj->element) @@ -303,8 +365,13 @@ int error_count = 0; CONFIG_CONTEXT *obj1 = context; while (obj1) { - if (strcmp(s, obj1->object) == 0 && obj->element && obj1->element) - monitorAddServer(obj->element, obj1->element); + if (strcmp(s, obj1->object) == 0 && + obj->element && obj1->element) + { + monitorAddServer( + obj->element, + obj1->element); + } obj1 = obj1->next; } s = strtok(NULL, ","); @@ -312,19 +379,29 @@ int error_count = 0; } if (obj->element && user && passwd) { - monitorAddUser(obj->element, user, passwd); + monitorAddUser(obj->element, + user, + passwd); } } else { obj->element = NULL; - skygw_log_write(LOGFILE_ERROR, "Monitor '%s' is missing a require module parameter.\n", obj->object); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Monitor '%s' is missing a " + "require module parameter.", + obj->object); error_count++; } } else if (strcmp(type, "server") != 0) { - skygw_log_write(LOGFILE_ERROR, "Configuration object '%s' has an invalid type specified", obj->object); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Configuration object '%s' has an " + "invalid type specified.", + obj->object); error_count++; } @@ -333,10 +410,14 @@ int error_count = 0; if (error_count) { - skygw_log_write(LOGFILE_ERROR, "%d errors where encountered processing the configuration file '%s'.\n", error_count, config_file); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : %d errors where encountered processing the " + "configuration file '%s'.", + error_count, + config_file); return 0; } - return 1; } @@ -448,61 +529,112 @@ SERVER *server; { char *type = config_get_value(obj->parameters, "type"); if (type == NULL) - skygw_log_write( LOGFILE_ERROR, "Configuration object %s has no type\n", obj->object); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Configuration object %s has no type.", + obj->object); else if (!strcmp(type, "service")) { - char *router = config_get_value(obj->parameters, "router"); + char *router = config_get_value(obj->parameters, + "router"); if (router) { if ((service = service_find(obj->object)) != NULL) { - char *user = config_get_value(obj->parameters, "user"); - char *auth = config_get_value(obj->parameters, "auth"); + char *user; + char *auth; + + user = config_get_value(obj->parameters, + "user"); + auth = config_get_value(obj->parameters, + "auth"); if (user && auth) - service_update(service, router, user, auth); + service_update(service, router, + user, + auth); obj->element = service; } else { - obj->element = service_alloc(obj->object, router); - char *user = config_get_value(obj->parameters, "user"); - char *auth = config_get_value(obj->parameters, "auth"); + char *user; + char *auth; + + user = config_get_value(obj->parameters, + "user"); + auth = config_get_value(obj->parameters, + "auth"); + obj->element = service_alloc(obj->object, + router); + if (obj->element && user && auth) - serviceSetUser(obj->element, user, auth); + { + serviceSetUser(obj->element, + user, + auth); + } } } else { obj->element = NULL; - skygw_log_write( LOGFILE_ERROR, "No router defined for service '%s'\n", - obj->object); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : No router defined for service " + "'%s'.", + obj->object); } } else if (!strcmp(type, "server")) { - char *address = config_get_value(obj->parameters, "address"); - char *port = config_get_value(obj->parameters, "port"); - char *protocol = config_get_value(obj->parameters, "protocol"); - char *monuser = config_get_value(obj->parameters, "monitoruser"); - char *monpw = config_get_value(obj->parameters, "monitorpw"); - if (address && port && protocol) + char *address; + char *port; + char *protocol; + char *monuser; + char *monpw; + + address = config_get_value(obj->parameters, "address"); + port = config_get_value(obj->parameters, "port"); + protocol = config_get_value(obj->parameters, "protocol"); + monuser = config_get_value(obj->parameters, + "monitoruser"); + monpw = config_get_value(obj->parameters, "monitorpw"); + + if (address && port && protocol) { - if ((server = server_find(address, atoi(port))) != NULL) + if ((server = + server_find(address, atoi(port))) != NULL) { - server_update(server, protocol, monuser, monpw); + server_update(server, + protocol, + monuser, + monpw); obj->element = server; } else { - obj->element = server_alloc(address, protocol, atoi(port)); + obj->element = server_alloc(address, + protocol, + atoi(port)); if (obj->element && monuser && monpw) - serverAddMonUser(obj->element, monuser, monpw); + { + serverAddMonUser(obj->element, + monuser, + monpw); + } } } else - skygw_log_write(LOGFILE_ERROR, "Server '%s' is missing a required configuration parameter. A server must have address, port and protocol defined.\n", obj->object); + { + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Server '%s' is missing a " + "required " + "configuration parameter. A server must " + "have address, port and protocol " + "defined.", + obj->object); + } } - obj = obj->next; } @@ -518,8 +650,12 @@ SERVER *server; ; else if (!strcmp(type, "service")) { - char *servers = config_get_value(obj->parameters, "servers"); - char *roptions = config_get_value(obj->parameters, "router_options"); + char *servers; + char *roptions; + + servers = config_get_value(obj->parameters, "servers"); + roptions = config_get_value(obj->parameters, + "router_options"); if (servers && obj->element) { char *s = strtok(servers, ","); @@ -528,9 +664,16 @@ SERVER *server; CONFIG_CONTEXT *obj1 = context; while (obj1) { - if (strcmp(s, obj1->object) == 0 && obj->element && obj1->element) + if (strcmp(s, obj1->object) == 0 && + obj->element && obj1->element) + { if (!serviceHasBackend(obj->element, obj1->element)) - serviceAddBackend(obj->element, obj1->element); + { + serviceAddBackend( + obj->element, + obj1->element); + } + } obj1 = obj1->next; } s = strtok(NULL, ","); @@ -549,40 +692,89 @@ SERVER *server; } else if (!strcmp(type, "listener")) { - char *service = config_get_value(obj->parameters, "service"); - char *port = config_get_value(obj->parameters, "port"); - char *protocol = config_get_value(obj->parameters, "protocol"); - if (service && port && protocol) + char *service; + char *port; + char *protocol; + + service = config_get_value(obj->parameters, "service"); + port = config_get_value(obj->parameters, "port"); + protocol = config_get_value(obj->parameters, "protocol"); + + if (service && port && protocol) { CONFIG_CONTEXT *ptr = context; while (ptr && strcmp(ptr->object, service) != 0) ptr = ptr->next; - if (ptr && ptr->element && serviceHasProtocol(ptr->element, protocol, atoi(port)) == 0) + + if (ptr && + ptr->element && + serviceHasProtocol(ptr->element, + protocol, + atoi(port)) == 0) { - serviceAddProtocol(ptr->element, protocol, atoi(port)); - serviceStartProtocol(ptr->element, protocol, atoi(port)); + serviceAddProtocol(ptr->element, + protocol, + atoi(port)); + serviceStartProtocol(ptr->element, + protocol, + atoi(port)); } } } - else if (strcmp(type, "server") != 0 && strcmp(type, "monitor") != 0) + else if (strcmp(type, "server") != 0 && + strcmp(type, "monitor") != 0) { - skygw_log_write(LOGFILE_ERROR, "Configuration object %s has an invalid type specified", obj->object); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Configuration object %s has an invalid " + "type specified.", + obj->object); } - obj = obj->next; } - return 1; } static char *service_params[] = - { "type", "router", "router_options", "servers", "user", "auth", NULL }; + { + "type", + "router", + "router_options", + "servers", + "user", + "auth", + NULL + }; + static char *server_params[] = - { "type", "address", "port", "protocol", "monitorpw", "monitoruser", NULL }; + { + "type", + "address", + "port", + "protocol", + "monitorpw", + "monitoruser", + NULL + }; + static char *listener_params[] = - { "type", "service", "protocol", "port", NULL }; + { + "type", + "service", + "protocol", + "port", + NULL + }; + static char *monitor_params[] = - { "type", "module", "servers", "user", "passwd", NULL }; + { + "type", + "module", + "servers", + "user", + "passwd", + NULL + }; /** * Check the configuration objects have valid parameters */ @@ -622,9 +814,14 @@ int i; if (!strcmp(params->name, param_set[i])) found = 1; if (found == 0) - skygw_log_write(LOGFILE_ERROR, - "Unexpected parameter '%s' for object '%s' of type '%s'.\n", - params->name, obj->object, type); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Unexpected parameter " + "'%s' for xobject '%s' of type " + "'%s'.", + params->name, + obj->object, + type); params = params->next; } } diff --git a/server/core/dbusers.c b/server/core/dbusers.c index 48762483e..c40d0b9d3 100644 --- a/server/core/dbusers.c +++ b/server/core/dbusers.c @@ -103,20 +103,28 @@ getUsers(SERVICE *service, struct users *users) serviceGetUser(service, &service_user, &service_passwd); /** multi-thread environment requires that thread init succeeds. */ if (mysql_thread_init()) { - skygw_log_write_flush(LOGFILE_ERROR, "ERROR : mysql_thread_init failed.\n"); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : mysql_thread_init failed."); return -1; } con = mysql_init(NULL); if (con == NULL) { - skygw_log_write( LOGFILE_ERROR, "mysql_init: %s\n", mysql_error(con)); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : mysql_init: %s", + mysql_error(con)); return -1; } if (mysql_options(con, MYSQL_OPT_USE_REMOTE_CONNECTION, NULL)) { - skygw_log_write_flush(LOGFILE_ERROR, "Fatal : failed to set external connection. " - "It is needed for backend server connections. Exiting.\n"); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : failed to set external connection. " + "It is needed for backend server connections. " + "Exiting."); return -1; } /* @@ -126,59 +134,64 @@ getUsers(SERVICE *service, struct users *users) */ server = service->databases; dpwd = decryptPassword(service_passwd); - while (server && mysql_real_connect(con, - server->name, - service_user, - dpwd, - NULL, - server->port, - NULL, - 0) == NULL) + while (server != NULL && mysql_real_connect(con, + server->name, + service_user, + dpwd, + NULL, + server->port, + NULL, + 0) == NULL) { - server = server->nextdb; + server = server->nextdb; } free(dpwd); if (server == NULL) { - skygw_log_write( - LOGFILE_ERROR, - "Unable to get user data from backend database for service " - "%s. Missing server information.", - service->name); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Unable to get user data from backend database " + "for service %s. Missing server information.", + service->name); mysql_close(con); return -1; } if (mysql_query(con, "SELECT user, password FROM mysql.user")) { - skygw_log_write( LOGFILE_ERROR, - "Loading users for service %s encountered error: %s\n", - service->name, mysql_error(con)); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Loading users for service %s encountered " + "error: %s.", + service->name, + mysql_error(con)); mysql_close(con); return -1; } - result = mysql_store_result(con); if (result == NULL) { - skygw_log_write( LOGFILE_ERROR, - "Loading users for service %s encountered error: %s\n", - service->name, mysql_error(con)); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Loading users for service %s encountered " + "error: %s.", + service->name, + mysql_error(con)); mysql_close(con); return -1; } - num_fields = mysql_num_fields(result); while ((row = mysql_fetch_row(result))) { - // we assume here two fields are returned !!! - // now adding to the hastable user and passwd+1 (escaping the first byte that is '*') + /** + * Two fields should be returned. + * user and passwd+1 (escaping the first byte that is '*') are + * added to hashtable. + */ users_add(users, row[0], strlen(row[1]) ? row[1]+1 : row[1]); total_users++; } - mysql_free_result(result); mysql_close(con); mysql_thread_end(); return total_users; - } diff --git a/server/core/dcb.c b/server/core/dcb.c index bb284ad0d..e8222531b 100644 --- a/server/core/dcb.c +++ b/server/core/dcb.c @@ -35,14 +35,18 @@ * 28/06/13 Mark Riddoch Changed the free mechanism ti * introduce a zombie state for the * dcb - * 02/07/2013 Massimiliano Pinto Addition of delayqlock, delayq and authlock - * for handling backend asynchronous protocol connection - * and a generic lock for backend authentication + * 02/07/2013 Massimiliano Pinto Addition of delayqlock, delayq and + * authlock for handling backend + * asynchronous protocol connection + * and a generic lock for backend + * authentication * 16/07/2013 Massimiliano Pinto Added command type for dcb * 23/07/2013 Mark Riddoch Tidy up logging * 02/09/2013 Massimiliano Pinto Added session refcount - * 27/09/2013 Massimiliano Pinto dcb_read returns 0 if ioctl returns no error and 0 bytes to read - * This fixes a bug with many reads from backend + * 27/09/2013 Massimiliano Pinto dcb_read returns 0 if ioctl returns no + * error and 0 bytes to read. + * This fixes a bug with many reads from + * backend * * @endverbatim */ @@ -183,10 +187,10 @@ dcb_add_to_zombieslist(DCB *dcb) if (ptr == dcb) { - skygw_log_write( + skygw_log_write_flush( LOGFILE_ERROR, - "Attempt to add DCB to zombies list " - "when it is already in the list"); + "Error : Attempt to add DCB to zombies " + "list when it is already in the list"); break; } ptr = ptr->memdata.next; @@ -375,9 +379,8 @@ bool succp = false; errno = 0; skygw_log_write_flush( LOGFILE_ERROR, - "%lu [dcb_process_zombies] Failed to close socket " - "%d on dcb %p due error %d, %s.", - pthread_self(), + "Error : Failed to close " + "socket %d on dcb %p due error %d, %s.", dcb->fd, dcb, eno, @@ -428,14 +431,17 @@ int fd; { return NULL; } - if ((funcs = (GWPROTOCOL *)load_module(protocol, MODULE_PROTOCOL)) == NULL) + if ((funcs = (GWPROTOCOL *)load_module(protocol, + MODULE_PROTOCOL)) == NULL) { dcb_set_state(dcb, DCB_STATE_DISCONNECTED, NULL); dcb_final_free(dcb); - skygw_log_write( + skygw_log_write_flush( LOGFILE_ERROR, - "Failed to load protocol module for %s, free dcb %p\n", - protocol, dcb); + "Error : Failed to load protocol module for %s, free " + "dcb %p\n", + protocol, + dcb); return NULL; } memcpy(&(dcb->func), funcs, sizeof(GWPROTOCOL)); @@ -444,20 +450,19 @@ int fd; { skygw_log_write( LOGFILE_TRACE, - "dcb_connect: failed to link to session, the session " - "has been removed."); + "%lu [dcb_connect] Failed to link to session, the " + "session has been removed.", + pthread_self()); dcb_final_free(dcb); return NULL; } - fd = dcb->func.connect(dcb, server, session); if (fd == -1) { skygw_log_write_flush( LOGFILE_ERROR, - "%lu [dcb_connect] Failed to connect to server %s:%d, " - "from backend dcb %p, client dcp %p fd %d\n", - pthread_self(), + "Error : Failed to connect to server %s:%d, " + "from backend dcb %p, client dcp %p fd %d.", server->name, server->port, dcb, @@ -470,7 +475,7 @@ int fd; skygw_log_write_flush( LOGFILE_TRACE, "%lu [dcb_connect] Connected to server %s:%d, " - "from backend dcb %p, client dcp %p fd %d\n", + "from backend dcb %p, client dcp %p fd %d.", pthread_self(), server->name, server->port, @@ -535,17 +540,15 @@ int eno = 0; if (rc == -1) { eno = errno; errno = 0; - skygw_log_write( + skygw_log_write_flush( LOGFILE_ERROR, - "%lu [dcb_read] ioctl FIONREAD for dcb %p fd %d " - "failed. " - "errno %d, %s. dcb->state = %d", - pthread_self(), + "Error : ioctl FIONREAD for dcb %p in " + "state %s fd %d failed due error %d, %s.", dcb, + STRDCBSTATE(dcb->state), dcb->fd, eno, - strerror(eno), - dcb->state); + strerror(eno)); n = -1; goto return_n; } @@ -562,11 +565,10 @@ int eno = 0; * This is a fatal error which should cause shutdown. * vraa : todo shutdown if memory allocation fails. */ - skygw_log_write( + skygw_log_write_flush( LOGFILE_ERROR, - "%lu [dcb_read] Failed to allocate read buffer " - "for dcb %p fd %d, due %d, %d.", - pthread_self(), + "Error : Failed to allocate read buffer " + "for dcb %p fd %d, due %d, %s.", dcb, dcb->fd, eno, @@ -584,12 +586,12 @@ int eno = 0; int eno = errno; errno = 0; - skygw_log_write( + skygw_log_write_flush( LOGFILE_ERROR, - "%lu [dcb_read] Read failed, dcb %p fd %d, due " - "%d, %d.", - pthread_self(), + "Error : Read failed, dcb %p in state %s " + "fd %d, due %d, %s.", dcb, + STRSCBSTATE(dcb->state), dcb->fd, eno, strerror(eno)); @@ -599,10 +601,12 @@ int eno = 0; skygw_log_write( LOGFILE_TRACE, - "%lu [dcb_read] Read %d bytes from dcb %p fd %d", + "%lu [dcb_read] Read %d bytes from dcb %p in state %s " + "fd %d.", pthread_self(), n, dcb, + STRDCBSTATE(dcb->state), dcb->fd); /** Append read data to the gwbuf */ *head = gwbuf_append(*head, buffer); @@ -642,9 +646,11 @@ int w, saved_errno = 0; skygw_log_write( LOGFILE_TRACE, "%lu [dcb_write] Append to writequeue. %d writes " - "buffered for %d", + "buffered for dcb %p in state %s fd %d", pthread_self(), dcb->stats.n_buffered, + dcb, + STRDCBSTATE(dcb->state), dcb->fd); } else @@ -679,7 +685,9 @@ int w, saved_errno = 0; } #endif /* SS_DEBUG */ len = GWBUF_LENGTH(queue); - GW_NOINTR_CALL(w = gw_write(dcb->fd, GWBUF_DATA(queue), len); + GW_NOINTR_CALL(w = gw_write(dcb->fd, + GWBUF_DATA(queue), + len); dcb->stats.n_writes++); saved_errno = errno; errno = 0; @@ -688,10 +696,10 @@ int w, saved_errno = 0; { skygw_log_write_flush( LOGFILE_ERROR, - "%lu [dcb_write] Write to dcb %p fd %d " - "failed due errno %d, %s", - pthread_self(), + "Error : Write to dcb %p in " + "state %s fd %d failed due errno %d, %s", dcb, + STRDCBSTATE(dcb->state), dcb->fd, saved_errno, strerror(saved_errno)); @@ -705,10 +713,12 @@ int w, saved_errno = 0; queue = gwbuf_consume(queue, w); skygw_log_write( LOGFILE_TRACE, - "%lu [dcb_write] Wrote %d Bytes to dcb %p fd %d", + "%lu [dcb_write] Wrote %d Bytes to dcb %p in " + "state %s fd %d", pthread_self(), w, dcb, + STRDCBSTATE(dcb->state), dcb->fd); } /* Buffer the balance of any data */ @@ -722,10 +732,16 @@ int w, saved_errno = 0; if (queue && (saved_errno != EAGAIN || saved_errno != EWOULDBLOCK)) { + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Writing to dcb %p in state %s fd %d " + "failed.", + dcb, + STRDCBSTATE(dcb->state), + dcb->fd); /* We had a real write failure that we must deal with */ return 0; } - return 1; } @@ -757,38 +773,39 @@ int saved_errno = 0; while (dcb->writeq != NULL) { len = GWBUF_LENGTH(dcb->writeq); - GW_NOINTR_CALL(w = gw_write(dcb->fd, GWBUF_DATA(dcb->writeq), len);); + GW_NOINTR_CALL(w = gw_write(dcb->fd, + GWBUF_DATA(dcb->writeq), + len);); saved_errno = errno; errno = 0; if (w < 0) { - skygw_log_write( + skygw_log_write_flush( LOGFILE_ERROR, - "%lu [dcb_drain_writeq] Write to fd %d " - "failed due errno %d, %s", - pthread_self(), + "Error : Write to dcb %p " + "in state fd %d failed due errno %d, %s", + dcb, + STRDCBSTATE(dcb->state), dcb->fd, saved_errno, strerror(saved_errno)); break; } - /* * Pull the number of bytes we have written from * queue with have. */ dcb->writeq = gwbuf_consume(dcb->writeq, w); - if (w < len) - { - /* We didn't write all the data */ - } skygw_log_write( LOGFILE_TRACE, - "%lu [dcb_drain_writeq] Wrote %d Bytes to fd %d", + "%lu [dcb_drain_writeq] Wrote %d Bytes to dcb %p " + "in state %s fd %d", pthread_self(), w, - dcb->fd); + dcb, + STRDCBSTATE(dcb->state), + dcb->fd); n += w; } } @@ -833,11 +850,10 @@ dcb_close(DCB *dcb) dcb, STRDCBSTATE(dcb->state)); } else { - skygw_log_write( - LOGFILE_TRACE, - "%lu [poll_remove_dcb] Removing dcb %p in state %s from " + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Removing dcb %p in state %s from " "poll set failed.", - pthread_self(), dcb, STRDCBSTATE(dcb->state)); } @@ -1160,11 +1176,12 @@ static bool dcb_set_state_nomutex( break; default: - skygw_log_write( + skygw_log_write_flush( LOGFILE_ERROR, - "%lu [dcb_set_state_nomutex] Unknown dcb state %d", - pthread_self(), - dcb->state); + "Error : Unknown dcb state %s for " + "dcb %p", + STRDCBSTATE(dcb->state), + dcb); ss_dassert(false); break; } /* switch (dcb->state) */ diff --git a/server/core/gateway.c b/server/core/gateway.c index c30d82900..d101a971a 100644 --- a/server/core/gateway.c +++ b/server/core/gateway.c @@ -104,14 +104,18 @@ static void libmysqld_done(void); */ static void sighup_handler (int i) { - skygw_log_write( LOGFILE_MESSAGE, "Refreshing configuration following SIGHUP\n"); + skygw_log_write( + LOGFILE_MESSAGE, + "Refreshing configuration following SIGHUP\n"); config_reload(); } static void sigterm_handler (int i) { extern void shutdown_gateway(); - - skygw_log_write( LOGFILE_ERROR, "Signal SIGTERM %i received ...Exiting!\n", i); + + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Signal SIGTERM %i received ...Exiting!", i); shutdown_gateway(); } @@ -120,7 +124,9 @@ sigint_handler (int i) { extern void shutdown_gateway(); - skygw_log_write( LOGFILE_ERROR, "Signal SIGINT %i received ...Exiting!\n", i); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Signal SIGINT %i received ...Exiting!", i); shutdown_gateway(); fprintf(stderr, "Shuting down MaxScale\n"); } @@ -134,7 +140,10 @@ static void signal_set (int sig, void (*handler)(int)) { sigact.sa_handler = handler; GW_NOINTR_CALL(err = sigaction(sig, &sigact, NULL)); if (err < 0) { - skygw_log_write( LOGFILE_ERROR,"sigaction() error %s\n", strerror(errno)); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : sigaction() error %s.", + strerror(errno)); exit(1); } } @@ -248,21 +257,20 @@ fail_accept_errno = 0; */ if (strncmp(argv[n], "-c", 2) == 0) { - int s=2; - - while (argv[n][s] == 0 && s<10) s++; - - if (s==10) { - skygw_log_write( - LOGFILE_ERROR, - "Fatal : missing file name. \n" - "Unable to find a MaxScale configuration file, " - "either install one in /etc/MaxScale.cnf, " - "$MAXSCALE_HOME/etc/MaxScale.cnf " - "or use the -c option with configuration file " - "name. Exiting.\n"); - } - cnf_file = &argv[n][s]; + int s=2; + + while (argv[n][s] == 0 && s<10) s++; + + if (s==10) { + skygw_log_write_flush( + LOGFILE_ERROR, + "Fatal : Unable to find a MaxScale " + "configuration file, either install one in " + "/etc/MaxScale.cnf, " + "$MAXSCALE_HOME/etc/MaxScale.cnf " + "or use the -c option. Exiting."); + } + cnf_file = &argv[n][s]; } } @@ -273,53 +281,61 @@ fail_accept_errno = 0; if (daemon_mode == 1) { if (sigfillset(&sigset) != 0) { - skygw_log_write( - LOGFILE_ERROR, - "sigfillset() error %s\n", - strerror(errno)); - return 1; + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : sigfillset() error %s", + strerror(errno)); + return 1; } if (sigdelset(&sigset, SIGHUP) != 0) { - skygw_log_write( - LOGFILE_ERROR, - "sigdelset(SIGHUP) error %s\n", - strerror(errno)); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : sigdelset(SIGHUP) error %s", + strerror(errno)); } if (sigdelset(&sigset, SIGTERM) != 0) { - skygw_log_write( - LOGFILE_ERROR, - "sigdelset(SIGTERM) error %s\n", - strerror(errno)); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : sigdelset(SIGTERM) error %s", + strerror(errno)); } if (sigprocmask(SIG_SETMASK, &sigset, NULL) != 0) { - skygw_log_write( - LOGFILE_ERROR, - "sigprocmask() error %s\n", - strerror(errno)); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : sigprocmask() error %s", + strerror(errno)); } - gw_daemonize(); } signal_set(SIGHUP, sighup_handler); signal_set(SIGTERM, sigterm_handler); signal_set(SIGINT, sigint_handler); - l = atexit(libmysqld_done); if (l != 0) { - fprintf(stderr, "Couldn't register exit function.\n"); + fprintf(stderr, "Couldn't register exit function.\n"); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Couldn't register exit function."); } if ((home = getenv("MAXSCALE_HOME")) != NULL) { if (access(home, R_OK) != 0) - { - fprintf(stderr,"The configured value of MAXSCALE_HOME '%s' does not exist.\n", + { + fprintf(stderr, + "The configured value of MAXSCALE_HOME '%s' does not " + "exist.\n", + home); + skygw_log_write_flush( + LOGFILE_ERROR, + "Fatal : The configured value of MAXSCALE_HOME '%s' does " + "not exist.", home); exit(1); } @@ -386,8 +402,9 @@ fail_accept_errno = 0; "Fatal : Unable to find a MaxScale configuration " "file, either install one in /etc/MaxScale.cnf, " "$MAXSCALE_HOME/etc/MaxScale.cnf " - "or use the -c option. Exiting.\n"); - fprintf(stderr, "Unable to find configuration file - MaxScale exiting.\n"); + "or use the -c option. Exiting."); + fprintf(stderr, "Unable to find MaxScale configuration file. " + "Exiting.\n"); exit(1); } @@ -404,15 +421,15 @@ fail_accept_errno = 0; if (mysql_library_init(num_elements, server_options, server_groups)) { skygw_log_write_flush( - LOGFILE_ERROR, - "Fatal : mysql_library_init failed, %s. This is mandatory " - "component, required by router services and the MaxScale core, " - "the MaxScale can't continue without it. Exiting.\n" - "%s : %d", - mysql_error(NULL), - __FILE__, - __LINE__); - fprintf(stderr, "Failed to initialise the MySQL library - MaxScale exiting.\n"); + LOGFILE_ERROR, + "Fatal : mysql_library_init failed. It is a " + "mandatory component, required by router services and " + "the MaxScale core. Error %s, %s : %d. Exiting.", + mysql_error(NULL), + __FILE__, + __LINE__); + fprintf(stderr, + "Failed to initialise the MySQL library. Exiting.\n"); exit(1); } libmysqld_started = TRUE; @@ -420,17 +437,21 @@ fail_accept_errno = 0; if (!config_load(cnf_file)) { skygw_log_write_flush( - LOGFILE_ERROR, - "Failed to load MaxScale configuration file %s", cnf_file); - fprintf(stderr, "Fatal error loading the configuration file - MaxScale exiting.\n"); + LOGFILE_ERROR, + "Fatal : Failed to load MaxScale configuration file %s. " + "Exiting.", + cnf_file); + fprintf(stderr, + "Failed to load MaxScale configuration file. " + "Exiting.\n"); exit(1); } - + skygw_log_write( - LOGFILE_MESSAGE, + LOGFILE_MESSAGE, "SkySQL MaxScale (C) SkySQL Ab 2013"); skygw_log_write( - LOGFILE_MESSAGE, + LOGFILE_MESSAGE, "MaxScale is starting, PID %i", getpid()); @@ -442,11 +463,18 @@ fail_accept_errno = 0; n_services = serviceStartAll(); if (n_services == 0) { - skygw_log_write(LOGFILE_ERROR, "Failed to start any services."); - fprintf(stderr, "Failed to start any services - MaxScale exiting.\n"); + skygw_log_write_flush( + LOGFILE_ERROR, + "Fatal : Failed to start any MaxScale services. " + "Exiting."); + fprintf(stderr, + "Failed to start any MaxScale services. Exiting.\n"); exit(1); } - skygw_log_write(LOGFILE_MESSAGE, "Started %d services succesfully.", n_services); + skygw_log_write( + LOGFILE_MESSAGE, + "Started %d services succesfully.", + n_services); /** * Start periodic log flusher thread. @@ -479,7 +507,7 @@ fail_accept_errno = 0; LOGFILE_MESSAGE, "MaxScale shutdown, PID %i\n", getpid()); - + datadir_cleanup(); return 0; @@ -512,5 +540,5 @@ static void log_flush_cb( skygw_log_flush(LOGFILE_TRACE); usleep(timeout_ms*1000); } - skygw_log_write(LOGFILE_MESSAGE, "Finished MaxScale log flusher."); + skygw_log_write(LOGFILE_MESSAGE, "Finished MaxScale log flusher."); } diff --git a/server/core/load_utils.c b/server/core/load_utils.c index 47897d00e..d11baa0b6 100644 --- a/server/core/load_utils.c +++ b/server/core/load_utils.c @@ -17,16 +17,16 @@ */ /** - * @file load_utils.c Utility functions to aid the loading of dynamic modules - * into the gateway + * @file load_utils.c Utility functions to aid the loading of dynamic + * modules into the gateway * * @verbatim * Revision History * * Date Who Description * 13/06/13 Mark Riddoch Initial implementation - * 14/06/13 Mark Riddoch Updated to add call to ModuleInit if one is defined - * in the loaded module. + * 14/06/13 Mark Riddoch Updated to add call to ModuleInit if one is + * defined in the loaded module. * Also updated to call fixed GetModuleObject * * @endverbatim @@ -44,7 +44,11 @@ static MODULES *registered = NULL; static MODULES *find_module(const char *module); -static void register_module(const char *module, const char *type, void *dlhandle, char *version, void *modobj); +static void register_module(const char *module, + const char *type, + void *dlhandle, + char *version, + void *modobj); static void unregister_module(const char *module); /** @@ -81,25 +85,33 @@ MODULES *mod; sprintf(fname, "%s/modules/lib%s.so", home, module); if (access(fname, F_OK) == -1) { - skygw_log_write( LOGFILE_ERROR, - "Unable to find library for module: %s\n", module); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Unable to find library for " + "module: %s.", + module); return NULL; } } if ((dlhandle = dlopen(fname, RTLD_NOW|RTLD_LOCAL)) == NULL) { - skygw_log_write( LOGFILE_ERROR, - "Unable to load library for module: %s, %s\n", module, dlerror()); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Unable to load library for module: " + "%s, %s.", + module, + dlerror()); return NULL; } if ((sym = dlsym(dlhandle, "version")) == NULL) { - skygw_log_write_flush( - LOGFILE_ERROR, - "Version interface not supported by module: %s, %s\n", - module, - dlerror()); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Version interface not supported by " + "module: %s, %s.", + module, + dlerror()); dlclose(dlhandle); return NULL; } @@ -118,21 +130,22 @@ MODULES *mod; if ((sym = dlsym(dlhandle, "GetModuleObject")) == NULL) { skygw_log_write_flush( - LOGFILE_ERROR, - "Expected entry point interface missing from module: " - "%s, %s\n", - module, - dlerror()); + LOGFILE_ERROR, + "Error : Expected entry point interface missing " + "from module: %s, %s.", + module, + dlerror()); dlclose(dlhandle); return NULL; } ep = sym; modobj = ep(); - skygw_log_write_flush(LOGFILE_MESSAGE, - "Loaded module %s: %s\n", - module, - version); + skygw_log_write_flush( + LOGFILE_MESSAGE, + "Loaded module %s: %s.", + module, + version); register_module(module, type, dlhandle, version, modobj); } else diff --git a/server/core/monitor.c b/server/core/monitor.c index e717e500c..61246e6d6 100644 --- a/server/core/monitor.c +++ b/server/core/monitor.c @@ -61,13 +61,15 @@ MONITOR *mon; mon->name = strdup(name); if ((mon->module = load_module(module, MODULE_MONITOR)) == NULL) { - skygw_log_write( LOGFILE_ERROR, "Unable to load monitor module '%s'\n", name); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Unable to load monitor module '%s'.", + name); free(mon->name); free(mon); return NULL; } mon->handle = (*mon->module->startMonitor)(NULL); - spinlock_acquire(&monLock); mon->next = allMonitors; allMonitors = mon; diff --git a/server/core/poll.c b/server/core/poll.c index 337601cd8..d1c9375df 100644 --- a/server/core/poll.c +++ b/server/core/poll.c @@ -115,40 +115,38 @@ poll_add_dcb(DCB *dcb) */ if (dcb_set_state(dcb, new_state, &old_state)) { rc = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, dcb->fd, &ev); - ss_dassert(rc == 0); + + if (rc != 0) { + int eno = errno; + errno = 0; + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Adding dcb %p in state %s " + "to poll set failed. epoll_ctl failed due " + "%d, %s.", + dcb, + STRDCBSTATE(dcb->state), + eno, + strerror(eno)); + } else { + skygw_log_write( + LOGFILE_DEBUG, + "%lu [poll_add_dcb] Added dcb %p in state %s to " + "poll set.", + pthread_self(), + dcb, + STRDCBSTATE(dcb->state)); + } + ss_dassert(rc == 0); /**< trap in debug */ } else { - skygw_log_write( + skygw_log_write_flush( LOGFILE_ERROR, - "%lu [poll_add_dcb] Unable to set new state for dcb %p " + "Error : Unable to set new state for dcb %p " "in state %s. Adding to poll set failed.", - pthread_self(), dcb, STRDCBSTATE(dcb->state)); - goto return_rc; } - if (rc != 0) { - int eno = errno; - errno = 0; - skygw_log_write( - LOGFILE_ERROR, - "%lu [poll_add_dcb] Adding dcb %p to poll set failed. " - "epoll_ctl failed due %d, %s.", - pthread_self(), - dcb, - STRDCBSTATE(dcb->state), - eno, - strerror(eno)); - goto return_rc; - } else { - skygw_log_write( - LOGFILE_DEBUG, - "%lu [poll_add_dcb] Added dcb %p to " - "poll set.", - pthread_self(), - dcb); - } -return_rc: return rc; } @@ -171,6 +169,7 @@ poll_remove_dcb(DCB *dcb) CHK_DCB(dcb); + /** It is possible that dcb has already been removed from the set */ if (dcb->state != DCB_STATE_POLLING) { goto return_rc; } @@ -186,12 +185,11 @@ poll_remove_dcb(DCB *dcb) errno = 0; skygw_log_write_flush( LOGFILE_ERROR, - "%lu [poll_remove_dcb] epoll_ctl failed due %d, %s.", - pthread_self(), + "Error : epoll_ctl failed due %d, %s.", eno, strerror(eno)); } - ss_dassert(rc == 0); + ss_dassert(rc == 0); /**< trap in debug */ } /** * This call was redundant, but the end result is correct. @@ -251,14 +249,12 @@ poll_waitevents(void *arg) while (1) { #if BLOCKINGPOLL - if ((nfds = epoll_wait(epoll_fd, events, MAX_EVENTS, -1)) == -1) - { - } + nfds = epoll_wait(epoll_fd, events, MAX_EVENTS, -1); #else if (!no_op) { skygw_log_write(LOGFILE_DEBUG, - "%lu [poll_waitevents] MaxScale thread %d > " - "epoll_wait <", + "%lu [poll_waitevents] MaxScale thread " + "%d > epoll_wait <", pthread_self(), thread_id); no_op = TRUE; @@ -269,12 +265,13 @@ poll_waitevents(void *arg) { int eno = errno; errno = 0; - skygw_log_write(LOGFILE_DEBUG, - "%lu [poll_waitevents] epoll_wait returned " - "%d, errno %d", - pthread_self(), - nfds, - eno); + skygw_log_write( + LOGFILE_DEBUG, + "%lu [poll_waitevents] epoll_wait returned " + "%d, errno %d", + pthread_self(), + nfds, + eno); no_op = FALSE; } else if (nfds == 0) @@ -342,7 +339,7 @@ poll_waitevents(void *arg) pthread_self(), ev, dcb); - + if (ev & EPOLLERR) { int eno = gw_getsockerrno(dcb->fd); @@ -370,17 +367,11 @@ poll_waitevents(void *arg) } atomic_add(&pollStats.n_error, 1); dcb->func.error(dcb); - if (DCB_ISZOMBIE(dcb)) { - continue; - } } if (ev & EPOLLHUP) { atomic_add(&pollStats.n_hup, 1); dcb->func.hangup(dcb); - if (DCB_ISZOMBIE(dcb)) { - continue; - } } if (ev & EPOLLOUT) { @@ -413,7 +404,8 @@ poll_waitevents(void *arg) "Accept in fd %d", pthread_self(), dcb->fd); - atomic_add(&pollStats.n_accept, 1); + atomic_add( + &pollStats.n_accept, 1); dcb->func.accept(dcb); } else @@ -445,8 +437,8 @@ poll_waitevents(void *arg) if (shutdown) { /** - * Remove this thread from the bitmask of running - * polling threads + * Remove the thread from the bitmask of running + * polling threads. */ bitmask_clear(&poll_mask, thread_id); return; diff --git a/server/core/secrets.c b/server/core/secrets.c index cac28c314..f9da65219 100644 --- a/server/core/secrets.c +++ b/server/core/secrets.c @@ -77,9 +77,8 @@ int len; errno = 0; skygw_log_write_flush( LOGFILE_ERROR, - "%lu [secrets_readKeys] access for secrets file [%s] " - "failed. Error %i, %s\n", - pthread_self(), + "Error : access for secrets file " + "[%s] failed. Error %d, %s.", secret_file, eno, strerror(eno)); @@ -93,9 +92,8 @@ int len; errno = 0; skygw_log_write_flush( LOGFILE_ERROR, - "%lu [secrets_readKeys] Failed opening secret file [%s]." - "Error %i, %s\n", - pthread_self(), + "Error : Failed opening secret " + "file [%s]. Error %d, %s.", secret_file, eno, strerror(eno)); @@ -109,9 +107,8 @@ int len; errno = 0; skygw_log_write_flush( LOGFILE_ERROR, - "%lu [secrets_readKeys] fstat for secret file %s failed." - "Error %i, %s\n", - pthread_self(), + "Error : fstat for secret file %s " + "failed. Error %d, %s.", secret_file, eno, strerror(eno)); @@ -124,9 +121,8 @@ int len; errno = 0; skygw_log_write_flush( LOGFILE_ERROR, - "%lu [secrets_readKeys] Secrets file %s has incorrect " - "size. Error %i, %s\n", - pthread_self(), + "Error : Secrets file %s has " + "incorrect size. Error %d, %s.", secret_file, eno, strerror(eno)); @@ -136,9 +132,8 @@ int len; { skygw_log_write_flush( LOGFILE_ERROR, - "%lu [secrets_readKeys] Ignoring secrets file %s, " - "invalid permissions.", - pthread_self(), + "Error : Ignoring secrets file " + "%s, invalid permissions.", secret_file); return NULL; } @@ -147,8 +142,8 @@ int len; { skygw_log_write_flush( LOGFILE_ERROR, - "%lu [secrets_readKeys] Memory allocation failed for " - "keys structure."); + "Error : Memory allocation failed " + "for key structure."); return NULL; } @@ -165,9 +160,8 @@ int len; free(keys); skygw_log_write_flush( LOGFILE_ERROR, - "%lu [secrets_readKeys] Read from secrets file %s " - "failed. Read %d, expected %d bytes. Error %i, %s\n", - pthread_self(), + "Error : Read from secrets file " + "%s failed. Read %d, expected %d bytes. Error %d, %s.", secret_file, len, sizeof(MAXKEYS), @@ -183,9 +177,8 @@ int len; free(keys); skygw_log_write_flush( LOGFILE_ERROR, - "%lu [secrets_readKeys] Failed closing the secrets " - "file %s. Error %i, %s\n", - pthread_self(), + "Error : Failed closing the " + "secrets file %s. Error %d, %s.", secret_file, eno, strerror(eno)); @@ -212,12 +205,13 @@ MAXKEYS key; /* Open for writing | Create | Truncate the file for writing */ if ((fd = open(secret_file, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR)) < 0) { - skygw_log_write(LOGFILE_ERROR, - "secrets_createKeys, failed opening secret file " - "[%s]. Error %i, %s\n", - secret_file, - errno, - strerror(errno)); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : failed opening secret " + "file [%s]. Error %d, %s.", + secret_file, + errno, + strerror(errno)); return 1; } @@ -228,24 +222,26 @@ MAXKEYS key; /* Write data */ if (write(fd, &key, sizeof(key)) < 0) { - skygw_log_write(LOGFILE_ERROR, - "secrets_createKeys, failed writing into secret " - "file [%s]. Error %i, %s\n", - secret_file, - errno, - strerror(errno)); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : failed writing into " + "secret file [%s]. Error %d, %s.", + secret_file, + errno, + strerror(errno)); return 1; } /* close file */ if (close(fd) < 0) { - skygw_log_write(LOGFILE_ERROR, - "secrets_createKeys, failed closing the secret " - "file [%s]. Error %i, %s\n", - secret_file, - errno, - strerror(errno)); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : failed closing the " + "secret file [%s]. Error %d, %s.", + secret_file, + errno, + strerror(errno)); } chmod(secret_file, S_IRUSR); diff --git a/server/core/server.c b/server/core/server.c index d59a7ca62..05126cba3 100644 --- a/server/core/server.c +++ b/server/core/server.c @@ -309,27 +309,27 @@ server_update(SERVER *server, char *protocol, char *user, char *passwd) { if (!strcmp(server->protocol, protocol)) { - skygw_log_write( - LOGFILE_MESSAGE, - "Update server protocol for server %s to protocol %s", - server->name, - protocol); + skygw_log_write( + LOGFILE_MESSAGE, + "Update server protocol for server %s to protocol %s.", + server->name, + protocol); free(server->protocol); server->protocol = strdup(protocol); } if (user != NULL && passwd != NULL) { - if (strcmp(server->monuser, user) == 0 || - strcmp(server->monpw, passwd) == 0) - { - skygw_log_write( + if (strcmp(server->monuser, user) == 0 || + strcmp(server->monpw, passwd) == 0) + { + skygw_log_write( LOGFILE_MESSAGE, "Update server monitor credentials for server %s", server->name); - free(server->monuser); - free(server->monpw); - serverAddMonUser(server, user, passwd); - } + free(server->monuser); + free(server->monpw); + serverAddMonUser(server, user, passwd); + } } } diff --git a/server/core/service.c b/server/core/service.c index cbb6076da..0bf9a61de 100644 --- a/server/core/service.c +++ b/server/core/service.c @@ -109,23 +109,24 @@ GWPROTOCOL *funcs; return 0; } if (strcmp(port->protocol, "MySQLClient") == 0) { - int loaded = -1; - - loaded = load_mysql_users(service); - + int loaded = load_mysql_users(service); skygw_log_write( LOGFILE_MESSAGE, - "MySQL Users loaded: %i\n", + "MySQL Users loaded: %i.", loaded); } - if ((funcs = (GWPROTOCOL *)load_module(port->protocol, MODULE_PROTOCOL)) == NULL) + if ((funcs = + (GWPROTOCOL *)load_module(port->protocol, MODULE_PROTOCOL)) == NULL) { free(port->listener); port->listener = NULL; - skygw_log_write(LOGFILE_ERROR, - "Unable to load protocol module %s. Listener for service %s not started.", - port->protocol, service->name); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Unable to load protocol module %s. Listener for " + "service %s not started.", + port->protocol, + service->name); return 0; } memcpy(&(port->listener->func), funcs, sizeof(GWPROTOCOL)); @@ -613,21 +614,33 @@ void *router_obj; { if ((router_obj = load_module(router, MODULE_ROUTER)) == NULL) { - skygw_log_write( LOGFILE_ERROR, "Failed to update router for service %s to %s", - service->name, router); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Failed to update router " + "for service %s to %s.", + service->name, + router); } else { - skygw_log_write( LOGFILE_MESSAGE, "Update router for service %s to %s", - service->name, router); + skygw_log_write( + LOGFILE_MESSAGE, + "Update router for service %s to %s.", + service->name, + router); free(service->routerModule); service->routerModule = strdup(router); service->router = router_obj; } } - if (user && (strcmp(service->credentials.name, user) != 0 || strcmp(service->credentials.authdata, auth) != 0)) + if (user && + (strcmp(service->credentials.name, user) != 0 || + strcmp(service->credentials.authdata, auth) != 0)) { - skygw_log_write( LOGFILE_MESSAGE, "Update credentials for service %s", service->name); + skygw_log_write( + LOGFILE_MESSAGE, + "Update credentials for service %s.", + service->name); serviceSetUser(service, user, auth); } } diff --git a/server/core/session.c b/server/core/session.c index cd390bb82..3dc03872f 100644 --- a/server/core/session.c +++ b/server/core/session.c @@ -71,9 +71,8 @@ session_alloc(SERVICE *service, DCB *client) errno = 0; skygw_log_write_flush( LOGFILE_ERROR, - "%lu [session_alloc] Failed to allocate memory for " + "Error : Failed to allocate memory for " "session object due error %d, %s.", - pthread_self(), eno, strerror(eno)); goto return_session; @@ -119,21 +118,21 @@ session_alloc(SERVICE *service, DCB *client) * and should be avoided for the listener session * * Router session creation may create other DCBs that link to the - * session, therefore it is important that the session lock is relinquished - * beforethe router call. + * session, therefore it is important that the session lock is + * relinquished beforethe router call. */ if (client->state != DCB_STATE_LISTENING) { session->router_session = - service->router->newSession(service->router_instance, session); + service->router->newSession(service->router_instance, + session); if (session->router_session == NULL) { client->session = NULL; skygw_log_write_flush( LOGFILE_ERROR, - "%lu [session_alloc] Failed to create router " - "client session. Freeing allocated resources.", - pthread_self()); + "Error : Failed to create router " + "client session. Freeing allocated resources."); free(session); session = NULL; goto return_session; diff --git a/server/core/utils.c b/server/core/utils.c index 49e7c1c42..f4be87363 100644 --- a/server/core/utils.c +++ b/server/core/utils.c @@ -61,15 +61,24 @@ int setnonblocking(int fd) { int fl; if ((fl = fcntl(fd, F_GETFL, 0)) == -1) { - skygw_log_write( LOGFILE_ERROR, "Can't GET fcntl for %i, errno = %d, %s", fd, errno, strerror(errno)); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Can't GET fcntl for %i, errno = %d, %s.", + fd, + errno, + strerror(errno)); return 1; } if (fcntl(fd, F_SETFL, fl | O_NONBLOCK) == -1) { - skygw_log_write( LOGFILE_ERROR, "Can't SET fcntl for %i, errno = %d, %s", fd, errno, strerror(errno)); + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Can't SET fcntl for %i, errno = %d, %s", + fd, + errno, + strerror(errno)); return 1; } - return 0; }