From dc4ded50dd6b3feb7f164c7ec070ae8b0facb1da Mon Sep 17 00:00:00 2001 From: Markus Makela Date: Wed, 30 Sep 2015 12:39:24 +0300 Subject: [PATCH 01/15] Simplified the configuration file template. --- server/maxscale_template.cnf | 394 ++++++----------------------------- 1 file changed, 67 insertions(+), 327 deletions(-) diff --git a/server/maxscale_template.cnf b/server/maxscale_template.cnf index 0928a750d..5824a4892 100644 --- a/server/maxscale_template.cnf +++ b/server/maxscale_template.cnf @@ -1,329 +1,17 @@ -## Example MaxScale.cnf configuration file -# # Global parameters # -# Number of worker threads in MaxScale +# Set the number of threads to a value +# equal to the number of CPU cores. # -# threads= -# -# Enabled logfiles. The message log is enabled by default and -# the error log is always enabled. -# -# log_messages=<1|0> -# log_trace=<1|0> -# log_debug=<1|0> -## Example: [maxscale] threads=4 -## Define a monitor that can be used to determine the state and role of -# the servers. +# Server definitions # -# Currently valid options for all monitors are: +# Set the address of the server to the network +# address of a MySQL server. # -# module=[mysqlmon|galeramon] -# -# List of server names which are being monitored -# -# servers=,,..., -# -# Username for monitor queries, need slave replication and slave client privileges -# Password in plain text format, and monitor's sampling interval in milliseconds. -# -# user= -# passwd= -# monitor_interval= (default 10000) -# -# Timeouts for monitor operations in backend servers - optional. -# -# backend_connect_timeout= -# backend_write_timeout= -# backend_read_timeout= -# -## MySQL monitor-specific options: -# -# Enable detection of replication slaves lag via replication_heartbeat -# table - optional. -# -# detect_replication_lag=[1|0] (default 0) -# -# Allow previous master to be available even in case of stopped or misconfigured -# replication - optional. -# -# detect_stale_master=[1|0] (default 0) -# -## Galera monitor-specific options: -# -# If disable_master_failback is not set, recovery of previously failed master -# causes mastership to be switched back to it. Enabling the option prevents it. -# -# disable_master_failback=[0|1] (default 0) -# -## Examples: - -[MySQL Monitor] -type=monitor -module=mysqlmon -servers=server1,server2,server3 -user=myuser -passwd=mypwd -monitor_interval=10000 -#backend_connect_timeout= -#backend_read_timeout= -#backend_write_timeout= -#detect_replication_lag= -#detect_stale_master= - -[Galera Monitor] -type=monitor -module=galeramon -servers=server1,server2,server3 -user=myuser -passwd=mypwd -monitor_interval=10000 -#disable_master_failback= - -## Filter definition -# -# Type specifies the section -# -# type=filter -# -# Module specifies which module implements the filter function -# -# module=[qlafilter|regexfilter|topfilter|teefilter] -# -# Options specify the log file for Query Log Filter -# -# options= -# -# Match and replace are used in regexfilter -# -# match=fetch -# replace=select -# -# Count and filebase are used with topfilter to specify how many top queries are -# listed and where. -# -# count= -# filebase= -# -# Match and service are used by tee filter to specify what queries should be -# duplicated and where the copy should be routed. -# -# match=insert.*HighScore.*values -# service=Cassandra -# -## Examples: - -[qla] -type=filter -module=qlafilter -options=/tmp/QueryLog - -[fetch] -type=filter -module=regexfilter -match=fetch -replace=select - -[hint] -type=filter -module=hintfilter - - -## A series of service definition -# -# Name of router module, currently valid options are -# -# router=[readconnroute|readwritesplit|debugcli|CLI] -# -# List of server names for use of service - mandatory for readconnroute, -# readwritesplit, and debugcli -# -# servers=,,..., -# -# Username to fetch password information with and password in plaintext -# format - for readconnroute and readwritesplit -# -# user= -# passwd= -# -# flag for enabling the use of root user - for readconnroute and -# readwritesplite - optional. -# -# enable_root_user=[0|1] (default 0) -# -# Version string to be used in server handshake. Default value is that of -# MariaDB embedded library's - for readconnroute and readwritesplite - optional. -# -# version_string= -# -# Filters specify the filters through which the query is transferred and the -# order of their appearance on the list corresponds the order they are -# used. Values refer to names of filters configured in this file - for -# readconnroute and readwritesplit - optional. -# -# filters= -# -## Read Connection Router specific router options. -# -# router_options specify the role in which the selected server must be. -# -# router_options=[master|slave|synced] -# -## Read/Write Split Router specific options. -# -# use_sql_variables_in specifies where sql variable modifications are -# routed - optional. -# -# use_sql_variables_in=[master|all] (default all) -# -# router_options=slave_selection_criteria specifies the selection criteria for -# slaves both in new session creation and when route target is selected - optional. -# -# router_options= -# slave_selection_criteria=[LEAST_CURRENT_OPERATIONS|LEAST_BEHIND_MASTER] -# -# router_options=max_sescmd_history specifies a limit on the number of 'session commands' -# a single session can execute. Please refer to the configuration guide for more details - optional. -# -# router_options= -# max_sescmd_history=2500 -# -# max_slave_connections specifies how many slaves a router session can -# connect to - optional. -# -# max_slave_connections= -# -# max_slave_replication_lag specifies how much a slave is allowed to be behind -# the master and still become chosen routing target - optional, requires that -# monitor has detect_replication_lag=1 . -# -# max_slave_replication_lag= -# -# Valid router modules currently are: -# readwritesplit, readconnroute, debugcli and CLI -# -## Examples: - -[Read Connection Router] -type=service -router=readconnroute -servers=server1,server2,server3 -user=myuser -passwd=mypwd -router_options=slave - -[RW Split Router] -type=service -router=readwritesplit -servers=server1,server2,server3 -user=myuser -passwd=mypwd -max_slave_connections=100% -#use_sql_variables_in=master -#max_slave_replication_lag=21 -#filters=hint|fetch|qla -#router_options=slave_selection_criteria=LEAST_CURRENT_OPERATIONS - -# Uncomment this to disable the saving of session modifying comments. Some scripting -# languages use connection pooling and will use the same session. MaxScale sees them -# as the same session and stores them for the slave recovery process. -#router_options=disable_sescmd_history=true,disable_slave_recovery=true - -# This will allow the master server to be used for read queries. By default -# MaxScale will only use the master for write queries. -#router_options=master_accept_reads=true - - -[Debug Interface] -type=service -router=debugcli - -[CLI] -type=service -router=cli - -## Listener definitions for the services -# -# Type specifies section as listener one -# -# type=listener -# -# Service links the section to one of the service names used in this configuration -# -# service= -# -# Protocol is client protocol library name. -# -# protocol=[MySQLClient|telnetd|HTTPD|maxscaled] -# -# Port and address specify which port the service listens and the address limits -# listening to a specific network interface only. Address is optional. -# -# port= -# address=
-# -# Socket is alternative for address. The specified socket path must be writable -# by the Unix user MaxScale runs as. -# -# socket= -# -## Examples: - -[Read Connection Listener] -type=listener -service=Read Connection Router -protocol=MySQLClient -address=192.168.100.102 -port=4008 -socket=/var/lib/maxscale/readconn.sock - -[RW Split Listener] -type=listener -service=RW Split Router -protocol=MySQLClient -port=4006 -#socket=/var/lib/maxscale/rwsplit.sock - -[Debug Listener] -type=listener -service=Debug Interface -protocol=telnetd -address=127.0.0.1 -port=4442 - -[CLI Listener] -type=listener -service=CLI -protocol=maxscaled -port=6603 - -## Definition of the servers -# -# Type specifies the section as server one -# -# type=server -# -# The IP address or hostname of the machine running the database server that is -# being defined. MaxScale will use this address to connect to the backend -# database server. -# -# address= -# -# The port on which the database listens for incoming connections. MaxScale -# will use this port to connect to the database server. -# -# port= -# -# The name for the protocol module to use to connect MaxScale to the database. -# Currently the only backend protocol supported is the MySQLBackend module. -# -# protocol=MySQLBackend -# -## Examples: [server1] type=server @@ -331,14 +19,66 @@ address=127.0.0.1 port=3306 protocol=MySQLBackend -[server2] -type=server -address=127.0.0.1 -port=3306 -protocol=MySQLBackend +# Monitor for the servers +# +# This will keep MaxScale aware of the state of the servers. +# -[server3] -type=server -address=127.0.0.1 -port=3306 -protocol=MySQLBackend +[MySQL Monitor] +type=monitor +module=mysqlmon +servers=server1 +user=myuser +passwd=mypwd +monitor_interval=10000 + +# Service definitions +# +# Service Definition for a read-only service and +# a read/write splitting service. +# + +[Read-Only Service] +type=service +router=readconnroute +servers=server1 +user=myuser +passwd=mypwd +router_options=slave + +[Read-Write Service] +type=service +router=readwritesplit +servers=server1 +user=myuser +passwd=mypwd +max_slave_connections=100% + +# This service enables the use of the MaxAdmin interface +[MaxAdmin Service] +type=service +router=cli + +# Listener definitions for the services +# +# These listeners represent the ports the +# services will listen on. +# + +[Read-Only Listener] +type=listener +service=Read-Only Service +protocol=MySQLClient +port=4008 + +[Read-Write Listener] +type=listener +service=Read-Write Service +protocol=MySQLClient +port=4006 + +[MaxAdmin Listener] +type=listener +service=MaxAdmin Service +protocol=maxscaled +port=6603 From eeefd7362387076f29f1c0209c6a58839c781f92 Mon Sep 17 00:00:00 2001 From: Markus Makela Date: Tue, 20 Oct 2015 16:20:54 +0300 Subject: [PATCH 02/15] Changed sprintf format to use %02hhx instead of %02x. --- server/modules/routing/binlog/blr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/modules/routing/binlog/blr.c b/server/modules/routing/binlog/blr.c index e5bfbd23e..93a5aa32a 100644 --- a/server/modules/routing/binlog/blr.c +++ b/server/modules/routing/binlog/blr.c @@ -247,7 +247,7 @@ char *defuuid; { my_uuid(defuuid); if ((inst->uuid = (char *)malloc(38)) != NULL) - sprintf(inst->uuid, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", + sprintf(inst->uuid, "%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx", defuuid[0], defuuid[1], defuuid[2], defuuid[3], defuuid[4], defuuid[5], defuuid[6], defuuid[7], defuuid[8], defuuid[9], defuuid[10], defuuid[11], From 04a1f9bd3d8a485844f9e7c2693d4f9be67e4ad3 Mon Sep 17 00:00:00 2001 From: Markus Makela Date: Tue, 20 Oct 2015 22:45:29 +0300 Subject: [PATCH 03/15] Error messages in getDatabases are now unique. --- server/core/dbusers.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/server/core/dbusers.c b/server/core/dbusers.c index e95ebf70c..dae524798 100644 --- a/server/core/dbusers.c +++ b/server/core/dbusers.c @@ -483,7 +483,7 @@ getDatabases(SERVICE *service, MYSQL *con) LOGIF(LE, (skygw_log_write_flush( LOGFILE_ERROR, "Error : Loading database names for service %s encountered " - "error: %s.", + "error when querying database privileges: %s.", service->name, mysql_error(con)))); return -1; @@ -495,7 +495,7 @@ getDatabases(SERVICE *service, MYSQL *con) LOGIF(LE, (skygw_log_write_flush( LOGFILE_ERROR, "Error : Loading database names for service %s encountered " - "error: %s.", + "error when storing result set of database privilege query: %s.", service->name, mysql_error(con)))); return -1; @@ -527,7 +527,7 @@ getDatabases(SERVICE *service, MYSQL *con) LOGIF(LE, (skygw_log_write_flush( LOGFILE_ERROR, "Error : Loading database names for service %s encountered " - "error: %s.", + "error when executing SHOW DATABASES query: %s.", service->name, mysql_error(con)))); @@ -540,7 +540,7 @@ getDatabases(SERVICE *service, MYSQL *con) LOGIF(LE, (skygw_log_write_flush( LOGFILE_ERROR, "Error : Loading database names for service %s encountered " - "error: %s.", + "error when storing the result set: %s.", service->name, mysql_error(con)))); From add1927781ada63b6a489b234808b6927dbfcf86 Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Wed, 21 Oct 2015 09:06:18 +0300 Subject: [PATCH 04/15] Reformatted to make line-length < 110. --- server/modules/routing/binlog/blr.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/server/modules/routing/binlog/blr.c b/server/modules/routing/binlog/blr.c index 93a5aa32a..6a57fb742 100644 --- a/server/modules/routing/binlog/blr.c +++ b/server/modules/routing/binlog/blr.c @@ -247,11 +247,16 @@ char *defuuid; { my_uuid(defuuid); if ((inst->uuid = (char *)malloc(38)) != NULL) - sprintf(inst->uuid, "%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx", - defuuid[0], defuuid[1], defuuid[2], defuuid[3], - defuuid[4], defuuid[5], defuuid[6], defuuid[7], - defuuid[8], defuuid[9], defuuid[10], defuuid[11], - defuuid[12], defuuid[13], defuuid[14], defuuid[15]); + sprintf(inst->uuid, + "%02hhx%02hhx%02hhx%02hhx-" + "%02hhx%02hhx-" + "%02hhx%02hhx-" + "%02hhx%02hhx-" + "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx", + defuuid[0], defuuid[1], defuuid[2], defuuid[3], + defuuid[4], defuuid[5], defuuid[6], defuuid[7], + defuuid[8], defuuid[9], defuuid[10], defuuid[11], + defuuid[12], defuuid[13], defuuid[14], defuuid[15]); } /* From 76f4f4fe890e854e5d4356d3622f517b4ffabd5d Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Wed, 21 Oct 2015 11:03:11 +0300 Subject: [PATCH 05/15] Indentation and whitespace fixes. Straightforward indentation and whitespace modifications. This is the first one in a series of commits that will bring log manager in line with the coding style. --- log_manager/log_manager.cc | 4046 ++++++++++++++++++------------------ log_manager/log_manager.h | 42 +- 2 files changed, 2044 insertions(+), 2044 deletions(-) diff --git a/log_manager/log_manager.cc b/log_manager/log_manager.cc index 157c9ea04..d35ddead9 100644 --- a/log_manager/log_manager.cc +++ b/log_manager/log_manager.cc @@ -116,19 +116,19 @@ static bool fatal_error = false; /** Writer thread structure */ struct filewriter_st { #if defined(SS_DEBUG) - skygw_chk_t fwr_chk_top; + skygw_chk_t fwr_chk_top; #endif - flat_obj_state_t fwr_state; - logmanager_t* fwr_logmgr; - /** Physical files */ - skygw_file_t* fwr_file[LOGFILE_LAST+1]; - /** fwr_logmes is for messages from log clients */ - skygw_message_t* fwr_logmes; - /** fwr_clientmes is for messages to log clients */ - skygw_message_t* fwr_clientmes; - skygw_thread_t* fwr_thread; + flat_obj_state_t fwr_state; + logmanager_t* fwr_logmgr; + /** Physical files */ + skygw_file_t* fwr_file[LOGFILE_LAST+1]; + /** fwr_logmes is for messages from log clients */ + skygw_message_t* fwr_logmes; + /** fwr_clientmes is for messages to log clients */ + skygw_message_t* fwr_clientmes; + skygw_thread_t* fwr_thread; #if defined(SS_DEBUG) - skygw_chk_t fwr_chk_tail; + skygw_chk_t fwr_chk_tail; #endif }; @@ -138,19 +138,19 @@ struct filewriter_st { */ typedef struct blockbuf_st { #if defined(SS_DEBUG) - skygw_chk_t bb_chk_top; + skygw_chk_t bb_chk_top; #endif - logfile_id_t bb_fileid; - blockbuf_state_t bb_state; /**State of the block buffer*/ - simple_mutex_t bb_mutex; /**< bb_buf_used, bb_isfull */ - int bb_refcount; /**< protected by list mutex. #of clients */ + logfile_id_t bb_fileid; + blockbuf_state_t bb_state; /**State of the block buffer*/ + simple_mutex_t bb_mutex; /**< bb_buf_used, bb_isfull */ + int bb_refcount; /**< protected by list mutex. #of clients */ // int bb_blankcount; /**< # of blanks used btw strings */ - size_t bb_buf_size; - size_t bb_buf_left; - size_t bb_buf_used; - char bb_buf[MAX_LOGSTRLEN]; + size_t bb_buf_size; + size_t bb_buf_left; + size_t bb_buf_used; + char bb_buf[MAX_LOGSTRLEN]; #if defined(SS_DEBUG) - skygw_chk_t bb_chk_tail; + skygw_chk_t bb_chk_tail; #endif } blockbuf_t; @@ -160,75 +160,75 @@ typedef struct blockbuf_st { */ struct logfile_st { #if defined(SS_DEBUG) - skygw_chk_t lf_chk_top; + skygw_chk_t lf_chk_top; #endif - flat_obj_state_t lf_state; - bool lf_init_started; - bool lf_enabled; - bool lf_store_shmem; - bool lf_write_syslog; - logmanager_t* lf_lmgr; - /** fwr_logmes is for messages from log clients */ - skygw_message_t* lf_logmes; - logfile_id_t lf_id; - char* lf_filepath; /**< path to file used for logging */ - char* lf_linkpath; /**< path to symlink file. */ - char* lf_name_prefix; - char* lf_name_suffix; - int lf_name_seqno; - char* lf_full_file_name; /**< complete log file name */ - char* lf_full_link_name; /**< complete symlink name */ - int lf_nfiles_max; - size_t lf_file_size; - /** list of block-sized log buffers */ - mlist_t lf_blockbuf_list; - size_t lf_buf_size; - bool lf_flushflag; - bool lf_rotateflag; - int lf_spinlock; /**< lf_flushflag & lf_rotateflag */ - int lf_npending_writes; + flat_obj_state_t lf_state; + bool lf_init_started; + bool lf_enabled; + bool lf_store_shmem; + bool lf_write_syslog; + logmanager_t* lf_lmgr; + /** fwr_logmes is for messages from log clients */ + skygw_message_t* lf_logmes; + logfile_id_t lf_id; + char* lf_filepath; /**< path to file used for logging */ + char* lf_linkpath; /**< path to symlink file. */ + char* lf_name_prefix; + char* lf_name_suffix; + int lf_name_seqno; + char* lf_full_file_name; /**< complete log file name */ + char* lf_full_link_name; /**< complete symlink name */ + int lf_nfiles_max; + size_t lf_file_size; + /** list of block-sized log buffers */ + mlist_t lf_blockbuf_list; + size_t lf_buf_size; + bool lf_flushflag; + bool lf_rotateflag; + int lf_spinlock; /**< lf_flushflag & lf_rotateflag */ + int lf_npending_writes; #if defined(SS_DEBUG) - skygw_chk_t lf_chk_tail; + skygw_chk_t lf_chk_tail; #endif }; struct fnames_conf_st { #if defined(SS_DEBUG) - skygw_chk_t fn_chk_top; + skygw_chk_t fn_chk_top; #endif - flat_obj_state_t fn_state; - char* fn_debug_prefix; - char* fn_debug_suffix; - char* fn_trace_prefix; - char* fn_trace_suffix; - char* fn_msg_prefix; - char* fn_msg_suffix; - char* fn_err_prefix; - char* fn_err_suffix; - char* fn_logpath; + flat_obj_state_t fn_state; + char* fn_debug_prefix; + char* fn_debug_suffix; + char* fn_trace_prefix; + char* fn_trace_suffix; + char* fn_msg_prefix; + char* fn_msg_suffix; + char* fn_err_prefix; + char* fn_err_suffix; + char* fn_logpath; #if defined(SS_DEBUG) - skygw_chk_t fn_chk_tail; + skygw_chk_t fn_chk_tail; #endif }; struct logmanager_st { #if defined(SS_DEBUG) - skygw_chk_t lm_chk_top; + skygw_chk_t lm_chk_top; #endif - bool lm_enabled; - int lm_enabled_logfiles; - simple_mutex_t lm_mutex; - size_t lm_nlinks; - /** fwr_logmes is for messages from log clients */ - skygw_message_t* lm_logmes; - /** fwr_clientmes is for messages to log clients */ - skygw_message_t* lm_clientmes; - fnames_conf_t lm_fnames_conf; - logfile_t lm_logfile[LOGFILE_LAST+1]; - filewriter_t lm_filewriter; + bool lm_enabled; + int lm_enabled_logfiles; + simple_mutex_t lm_mutex; + size_t lm_nlinks; + /** fwr_logmes is for messages from log clients */ + skygw_message_t* lm_logmes; + /** fwr_clientmes is for messages to log clients */ + skygw_message_t* lm_clientmes; + fnames_conf_t lm_fnames_conf; + logfile_t lm_logfile[LOGFILE_LAST+1]; + filewriter_t lm_filewriter; #if defined(SS_DEBUG) - skygw_chk_t lm_chk_tail; + skygw_chk_t lm_chk_tail; #endif }; @@ -239,19 +239,19 @@ struct logmanager_st { typedef struct strpart_st strpart_t; struct strpart_st { - char* sp_string; - strpart_t* sp_next; + char* sp_string; + strpart_t* sp_next; }; /** Static function declarations */ static bool logfiles_init(logmanager_t* lmgr); static bool logfile_init( - logfile_t* logfile, - logfile_id_t logfile_id, - logmanager_t* logmanager, - bool store_shmem, - bool write_syslog); + logfile_t* logfile, + logfile_id_t logfile_id, + logmanager_t* logmanager, + bool store_shmem, + bool write_syslog); static void logfile_done(logfile_t* logfile); static void logfile_free_memory(logfile_t* lf); static void logfile_flush(logfile_t* lf); @@ -261,10 +261,10 @@ static bool logfile_open_file(filewriter_t* fw, logfile_t* lf); static char* form_full_file_name(strpart_t* parts, logfile_t* lf, int seqnoidx); static bool filewriter_init( - logmanager_t* logmanager, - filewriter_t* fw, - skygw_message_t* clientmes, - skygw_message_t* logmes); + logmanager_t* logmanager, + filewriter_t* fw, + skygw_message_t* clientmes, + skygw_message_t* logmes); static void filewriter_done(filewriter_t* filewriter); static bool fnames_conf_init(fnames_conf_t* fn, int argc, char* argv[]); static void fnames_conf_done(fnames_conf_t* fn); @@ -279,22 +279,22 @@ static bool logmanager_init_nomutex(int argc, char* argv[]); static void logmanager_done_nomutex(void); static int logmanager_write_log( - logfile_id_t id, - bool flush, - bool use_valist, - bool spread_down, - bool rotate, - size_t len, - const char* str, - va_list valist); + logfile_id_t id, + bool flush, + bool use_valist, + bool spread_down, + bool rotate, + size_t len, + const char* str, + va_list valist); static blockbuf_t* blockbuf_init(logfile_id_t id); static void blockbuf_node_done(void* bb_data); static char* blockbuf_get_writepos( - blockbuf_t** p_bb, - logfile_id_t id, - size_t str_len, - bool flush); + blockbuf_t** p_bb, + logfile_id_t id, + size_t str_len, + bool flush); static void blockbuf_register(blockbuf_t* bb); static void blockbuf_unregister(blockbuf_t* bb); @@ -302,9 +302,9 @@ static bool logfile_set_enabled(logfile_id_t id, bool val); static char* add_slash(char* str); static bool check_file_and_path( - char* filename, - bool* writable, - bool do_log); + char* filename, + bool* writable, + bool do_log); static bool file_is_symlink(char* filename); static int skygw_log_disable_raw(logfile_id_t id, bool emergency); /*< no locking */ @@ -314,7 +314,7 @@ bool thr_flushall_check(); const char* get_suffix_default(void) { - return ".log"; + return ".log"; } const char* get_debug_prefix_default(void) @@ -324,7 +324,7 @@ const char* get_debug_prefix_default(void) const char* get_debug_suffix_default(void) { - return get_suffix_default(); + return get_suffix_default(); } const char* get_trace_prefix_default(void) @@ -334,7 +334,7 @@ const char* get_trace_prefix_default(void) const char* get_trace_suffix_default(void) { - return get_suffix_default(); + return get_suffix_default(); } const char* get_msg_prefix_default(void) @@ -344,7 +344,7 @@ const char* get_msg_prefix_default(void) const char* get_msg_suffix_default(void) { - return get_suffix_default(); + return get_suffix_default(); } const char* get_err_prefix_default(void) @@ -354,123 +354,123 @@ const char* get_err_prefix_default(void) const char* get_err_suffix_default(void) { - return get_suffix_default(); + return get_suffix_default(); } const char* get_logpath_default(void) { - return "/var/log/maxscale"; + return "/var/log/maxscale"; } static bool logmanager_init_nomutex( - int argc, - char* argv[]) + int argc, + char* argv[]) { - fnames_conf_t* fn; - filewriter_t* fw; - int err; - bool succp = false; + fnames_conf_t* fn; + filewriter_t* fw; + int err; + bool succp = false; - lm = (logmanager_t *)calloc(1, sizeof(logmanager_t)); + lm = (logmanager_t *)calloc(1, sizeof(logmanager_t)); - if (lm == NULL) - { - err = 1; - goto return_succp; - } + if (lm == NULL) + { + err = 1; + goto return_succp; + } #if defined(SS_DEBUG) - lm->lm_chk_top = CHK_NUM_LOGMANAGER; - lm->lm_chk_tail = CHK_NUM_LOGMANAGER; - write_index = 0; - block_start_index = 0; - prevval = -1; - simple_mutex_init(&msg_mutex, "Message mutex"); + lm->lm_chk_top = CHK_NUM_LOGMANAGER; + lm->lm_chk_tail = CHK_NUM_LOGMANAGER; + write_index = 0; + block_start_index = 0; + prevval = -1; + simple_mutex_init(&msg_mutex, "Message mutex"); #endif - lm->lm_clientmes = skygw_message_init(); - lm->lm_logmes = skygw_message_init(); + lm->lm_clientmes = skygw_message_init(); + lm->lm_logmes = skygw_message_init(); - if (lm->lm_clientmes == NULL || - lm->lm_logmes == NULL) - { - err = 1; - goto return_succp; - } + if (lm->lm_clientmes == NULL || + lm->lm_logmes == NULL) + { + err = 1; + goto return_succp; + } - lm->lm_enabled_logfiles |= LOGFILE_ERROR; - lm->lm_enabled_logfiles |= LOGFILE_MESSAGE; + lm->lm_enabled_logfiles |= LOGFILE_ERROR; + lm->lm_enabled_logfiles |= LOGFILE_MESSAGE; #if defined(SS_DEBUG) - lm->lm_enabled_logfiles |= LOGFILE_TRACE; - lm->lm_enabled_logfiles |= LOGFILE_DEBUG; + lm->lm_enabled_logfiles |= LOGFILE_TRACE; + lm->lm_enabled_logfiles |= LOGFILE_DEBUG; #endif - fn = &lm->lm_fnames_conf; - fw = &lm->lm_filewriter; - fn->fn_state = UNINIT; - fw->fwr_state = UNINIT; + fn = &lm->lm_fnames_conf; + fw = &lm->lm_filewriter; + fn->fn_state = UNINIT; + fw->fwr_state = UNINIT; - if(!do_syslog) - { - free(syslog_id_str); - syslog_id_str = NULL; - } + if(!do_syslog) + { + free(syslog_id_str); + syslog_id_str = NULL; + } - /** Initialize configuration including log file naming info */ - if (!fnames_conf_init(fn, argc, argv)) - { - err = 1; - goto return_succp; - } + /** Initialize configuration including log file naming info */ + if (!fnames_conf_init(fn, argc, argv)) + { + err = 1; + goto return_succp; + } - /** Initialize logfiles */ - if(!logfiles_init(lm)) - { - err = 1; - goto return_succp; - } + /** Initialize logfiles */ + if(!logfiles_init(lm)) + { + err = 1; + goto return_succp; + } - /** - * Set global variable - */ - lm_enabled_logfiles_bitmask = lm->lm_enabled_logfiles; + /** + * Set global variable + */ + lm_enabled_logfiles_bitmask = lm->lm_enabled_logfiles; - /** - * Initialize filewriter data and open the log file - * for each log file type. - */ - if (!filewriter_init(lm, fw, lm->lm_clientmes, lm->lm_logmes)) - { - err = 1; - goto return_succp; - } + /** + * Initialize filewriter data and open the log file + * for each log file type. + */ + if (!filewriter_init(lm, fw, lm->lm_clientmes, lm->lm_logmes)) + { + err = 1; + goto return_succp; + } - /** Initialize and start filewriter thread */ - fw->fwr_thread = skygw_thread_init("filewriter thr", - thr_filewriter_fun, - (void *)fw); + /** Initialize and start filewriter thread */ + fw->fwr_thread = skygw_thread_init("filewriter thr", + thr_filewriter_fun, + (void *)fw); - if (fw->fwr_thread == NULL) - { - err = 1; - goto return_succp; - } + if (fw->fwr_thread == NULL) + { + err = 1; + goto return_succp; + } - if ((err = skygw_thread_start(fw->fwr_thread)) != 0) - { - goto return_succp; - } - /** Wait message from filewriter_thr */ - skygw_message_wait(fw->fwr_clientmes); + if ((err = skygw_thread_start(fw->fwr_thread)) != 0) + { + goto return_succp; + } + /** Wait message from filewriter_thr */ + skygw_message_wait(fw->fwr_clientmes); - succp = true; - lm->lm_enabled = true; + succp = true; + lm->lm_enabled = true; return_succp: - if (err != 0) - { - /** This releases memory of all created objects */ - logmanager_done_nomutex(); - fprintf(stderr, "*\n* Error : Initializing log manager failed.\n*\n"); - } - return succp; + if (err != 0) + { + /** This releases memory of all created objects */ + logmanager_done_nomutex(); + fprintf(stderr, "*\n* Error : Initializing log manager failed.\n*\n"); + } + return succp; } @@ -479,32 +479,32 @@ return_succp: * Initializes log managing routines in MariaDB Corporation MaxScale. * * Parameters: - * @param argc number of arguments in argv array + * @param argc number of arguments in argv array * - * @param argv arguments array + * @param argv arguments array * * @return true if succeed, otherwise false * */ bool skygw_logmanager_init( - int argc, - char* argv[]) + int argc, + char* argv[]) { - bool succp = false; + bool succp = false; - acquire_lock(&lmlock); + acquire_lock(&lmlock); - if (lm != NULL) { - succp = true; - goto return_succp; - } + if (lm != NULL) { + succp = true; + goto return_succp; + } - succp = logmanager_init_nomutex(argc, argv); + succp = logmanager_init_nomutex(argc, argv); return_succp: - release_lock(&lmlock); + release_lock(&lmlock); - return succp; + return succp; } /** @@ -515,44 +515,44 @@ return_succp: */ static void logmanager_done_nomutex(void) { - int i; - logfile_t* lf; - filewriter_t* fwr; + int i; + logfile_t* lf; + filewriter_t* fwr; - fwr = &lm->lm_filewriter; + fwr = &lm->lm_filewriter; - if (fwr->fwr_state == RUN) { - CHK_FILEWRITER(fwr); - /** Inform filewriter thread and wait until it has stopped. */ - skygw_thread_set_exitflag(fwr->fwr_thread, - fwr->fwr_logmes, - fwr->fwr_clientmes); + if (fwr->fwr_state == RUN) { + CHK_FILEWRITER(fwr); + /** Inform filewriter thread and wait until it has stopped. */ + skygw_thread_set_exitflag(fwr->fwr_thread, + fwr->fwr_logmes, + fwr->fwr_clientmes); - /** Free thread memory */ - skygw_thread_done(fwr->fwr_thread); - } + /** Free thread memory */ + skygw_thread_done(fwr->fwr_thread); + } - /** Free filewriter memory. */ - filewriter_done(fwr); + /** Free filewriter memory. */ + filewriter_done(fwr); - for (i=LOGFILE_FIRST; i<=LOGFILE_LAST; i++) { - lf = logmanager_get_logfile(lm, (logfile_id_t)i); - /** Release logfile memory */ - logfile_done(lf); - } + for (i=LOGFILE_FIRST; i<=LOGFILE_LAST; i++) { + lf = logmanager_get_logfile(lm, (logfile_id_t)i); + /** Release logfile memory */ + logfile_done(lf); + } - if (syslog_id_str) - { - closelog(); - } - /** Release messages and finally logmanager memory */ - fnames_conf_done(&lm->lm_fnames_conf); - skygw_message_done(lm->lm_clientmes); - skygw_message_done(lm->lm_logmes); + if (syslog_id_str) + { + closelog(); + } + /** Release messages and finally logmanager memory */ + fnames_conf_done(&lm->lm_fnames_conf); + skygw_message_done(lm->lm_clientmes); + skygw_message_done(lm->lm_logmes); - /** Set global pointer NULL to prevent access to freed data. */ - free(lm); - lm = NULL; + /** Set global pointer NULL to prevent access to freed data. */ + free(lm); + lm = NULL; } @@ -561,7 +561,7 @@ static void logmanager_done_nomutex(void) */ void skygw_logmanager_exit(void) { - skygw_logmanager_done(); + skygw_logmanager_done(); } /** @@ -572,49 +572,49 @@ void skygw_logmanager_exit(void) */ void skygw_logmanager_done(void) { + acquire_lock(&lmlock); + + if (lm == NULL) { + release_lock(&lmlock); + return; + } + CHK_LOGMANAGER(lm); + /** Mark logmanager unavailable */ + lm->lm_enabled = false; + + /** Wait until all users have left or someone shuts down + * logmanager between lock release and acquire. + */ + while(lm != NULL && lm->lm_nlinks != 0) { + release_lock(&lmlock); + pthread_yield(); acquire_lock(&lmlock); + } - if (lm == NULL) { - release_lock(&lmlock); - return; - } - CHK_LOGMANAGER(lm); - /** Mark logmanager unavailable */ - lm->lm_enabled = false; - - /** Wait until all users have left or someone shuts down - * logmanager between lock release and acquire. - */ - while(lm != NULL && lm->lm_nlinks != 0) { - release_lock(&lmlock); - pthread_yield(); - acquire_lock(&lmlock); - } - - /** Logmanager was already shut down. Return successfully. */ - if (lm == NULL) { - goto return_void; - } - ss_dassert(lm->lm_nlinks == 0); - logmanager_done_nomutex(); + /** Logmanager was already shut down. Return successfully. */ + if (lm == NULL) { + goto return_void; + } + ss_dassert(lm->lm_nlinks == 0); + logmanager_done_nomutex(); return_void: - release_lock(&lmlock); + release_lock(&lmlock); } static logfile_t* logmanager_get_logfile( - logmanager_t* lmgr, - logfile_id_t id) + logmanager_t* lmgr, + logfile_id_t id) { - logfile_t* lf; - CHK_LOGMANAGER(lmgr); - ss_dassert(id >= LOGFILE_FIRST && id <= LOGFILE_LAST); - lf = &lmgr->lm_logfile[id]; + logfile_t* lf; + CHK_LOGMANAGER(lmgr); + ss_dassert(id >= LOGFILE_FIRST && id <= LOGFILE_LAST); + lf = &lmgr->lm_logfile[id]; - if (lf->lf_state == RUN) { - CHK_LOGFILE(lf); - } - return lf; + if (lf->lf_state == RUN) { + CHK_LOGFILE(lf); + } + return lf; } @@ -623,332 +623,332 @@ static logfile_t* logmanager_get_logfile( * * Parameters: * - * @param id logfile object identifier - * @param flush indicates whether log string must be written to disk - * immediately - * @param use_valist does write involve formatting of the string and use of - * valist argument - * @param spread_down if true, log string is spread to all logs having - * larger id - * @param rotate if set, closes currently open log file and opens a - * new one - * @param str_len length of formatted string - * @param str string to be written to log - * @param valist variable-length argument list for formatting the string + * @param id logfile object identifier + * @param flush indicates whether log string must be written to disk + * immediately + * @param use_valist does write involve formatting of the string and use of + * valist argument + * @param spread_down if true, log string is spread to all logs having + * larger id + * @param rotate if set, closes currently open log file and opens a + * new one + * @param str_len length of formatted string + * @param str string to be written to log + * @param valist variable-length argument list for formatting the string * * @return 0 if succeed, -1 otherwise * */ static int logmanager_write_log( - logfile_id_t id, - bool flush, - bool use_valist, - bool spread_down, - bool rotate, - size_t str_len, - const char* str, - va_list valist) + logfile_id_t id, + bool flush, + bool use_valist, + bool spread_down, + bool rotate, + size_t str_len, + const char* str, + va_list valist) { - logfile_t* lf; - char* wp; - int err = 0; - blockbuf_t* bb; - blockbuf_t* bb_c; - size_t timestamp_len; - int i; + logfile_t* lf; + char* wp; + int err = 0; + blockbuf_t* bb; + blockbuf_t* bb_c; + size_t timestamp_len; + int i; - CHK_LOGMANAGER(lm); - - if (id < LOGFILE_FIRST || id > LOGFILE_LAST) { - const char* errstr = "Invalid logfile id argument."; - /** - * invalid id, since we don't have logfile yet. - */ - err = logmanager_write_log(LOGFILE_ERROR, - true, - false, - false, - false, - strlen(errstr)+1, - errstr, - valist); - if (err != 0) { - fprintf(stderr, - "Writing to logfile %s failed.\n", - STRLOGID(LOGFILE_ERROR)); - } - err = -1; - ss_dassert(false); - goto return_err; - } - lf = &lm->lm_logfile[id]; - CHK_LOGFILE(lf); + CHK_LOGMANAGER(lm); + if (id < LOGFILE_FIRST || id > LOGFILE_LAST) { + const char* errstr = "Invalid logfile id argument."; /** - * When string pointer is NULL, operation is either flush or rotate. + * invalid id, since we don't have logfile yet. */ - if (str == NULL) - { - if (flush) - { - logfile_flush(lf); /*< wakes up file writer */ - } - else if (rotate) - { - logfile_rotate(lf); /*< wakes up file writer */ - } + err = logmanager_write_log(LOGFILE_ERROR, + true, + false, + false, + false, + strlen(errstr)+1, + errstr, + valist); + if (err != 0) { + fprintf(stderr, + "Writing to logfile %s failed.\n", + STRLOGID(LOGFILE_ERROR)); + } + err = -1; + ss_dassert(false); + goto return_err; + } + lf = &lm->lm_logfile[id]; + CHK_LOGFILE(lf); + + /** + * When string pointer is NULL, operation is either flush or rotate. + */ + if (str == NULL) + { + if (flush) + { + logfile_flush(lf); /*< wakes up file writer */ + } + else if (rotate) + { + logfile_rotate(lf); /*< wakes up file writer */ + } + } + else + { + /** Length of string that will be written, limited by bufsize */ + size_t safe_str_len; + /** Length of session id */ + size_t sesid_str_len; + size_t cmplen = 0; + /** + * 2 braces, 2 spaces and terminating char + * If session id is stored to tls_log_info structure, allocate + * room for session id too. + */ + if (id == LOGFILE_TRACE && tls_log_info.li_sesid != 0) + { + sesid_str_len = 5*sizeof(char)+get_decimal_len(tls_log_info.li_sesid); } else - { - /** Length of string that will be written, limited by bufsize */ - size_t safe_str_len; - /** Length of session id */ - size_t sesid_str_len; - size_t cmplen = 0; - /** - * 2 braces, 2 spaces and terminating char - * If session id is stored to tls_log_info structure, allocate - * room for session id too. - */ - if (id == LOGFILE_TRACE && tls_log_info.li_sesid != 0) - { - sesid_str_len = 5*sizeof(char)+get_decimal_len(tls_log_info.li_sesid); - } - else - { - sesid_str_len = 0; - } - if(highprec) - timestamp_len = get_timestamp_len_hp(); - else - timestamp_len = get_timestamp_len(); - cmplen = sesid_str_len > 0 ? sesid_str_len - sizeof(char) : 0; + { + sesid_str_len = 0; + } + if(highprec) + timestamp_len = get_timestamp_len_hp(); + else + timestamp_len = get_timestamp_len(); + cmplen = sesid_str_len > 0 ? sesid_str_len - sizeof(char) : 0; - /** Find out how much can be safely written with current block size */ - if (timestamp_len-sizeof(char)+cmplen+str_len > lf->lf_buf_size) - { - safe_str_len = lf->lf_buf_size; - } - else - { - safe_str_len = timestamp_len-sizeof(char)+cmplen+str_len; - } - /** - * Seek write position and register to block buffer. - * Then print formatted string to write position. - */ + /** Find out how much can be safely written with current block size */ + if (timestamp_len-sizeof(char)+cmplen+str_len > lf->lf_buf_size) + { + safe_str_len = lf->lf_buf_size; + } + else + { + safe_str_len = timestamp_len-sizeof(char)+cmplen+str_len; + } + /** + * Seek write position and register to block buffer. + * Then print formatted string to write position. + */ #if defined (SS_LOG_DEBUG) - { - char *copy,*tok; - int tokval; + { + char *copy,*tok; + int tokval; - simple_mutex_lock(&msg_mutex,true); - copy = strdup(str); - tok = strtok(copy,"|"); - tok = strtok(NULL,"|"); + simple_mutex_lock(&msg_mutex,true); + copy = strdup(str); + tok = strtok(copy,"|"); + tok = strtok(NULL,"|"); - if(strstr(str,"message|") && tok) - { - tokval = atoi(tok); + if(strstr(str,"message|") && tok) + { + tokval = atoi(tok); - if(prevval > 0) - { - ss_dassert(tokval == (prevval + 1)); - } - prevval = tokval; - } - free(copy); - simple_mutex_unlock(&msg_mutex); - } + if(prevval > 0) + { + ss_dassert(tokval == (prevval + 1)); + } + prevval = tokval; + } + free(copy); + simple_mutex_unlock(&msg_mutex); + } #endif - /** Book space for log string from buffer */ - if(do_maxscalelog) - { - wp = blockbuf_get_writepos(&bb, - id, - safe_str_len, - flush); - } - else - { - wp = (char*)malloc(sizeof(char)*(timestamp_len-sizeof(char)+cmplen+str_len + 1)); - } + /** Book space for log string from buffer */ + if(do_maxscalelog) + { + wp = blockbuf_get_writepos(&bb, + id, + safe_str_len, + flush); + } + else + { + wp = (char*)malloc(sizeof(char)*(timestamp_len-sizeof(char)+cmplen+str_len + 1)); + } if(wp == NULL) return -1; #if defined (SS_LOG_DEBUG) - { - sprintf(wp,"[msg:%d]",atomic_add(&write_index,1)); - safe_str_len -= strlen(wp); - wp += strlen(wp); - } + { + sprintf(wp,"[msg:%d]",atomic_add(&write_index,1)); + safe_str_len -= strlen(wp); + wp += strlen(wp); + } #endif - /** - * Write timestamp with at most characters - * to wp. - * Returned timestamp_len doesn't include terminating null. - */ - if(highprec) - timestamp_len = snprint_timestamp_hp(wp, timestamp_len); - else - timestamp_len = snprint_timestamp(wp, timestamp_len); - if (sesid_str_len != 0) - { - /** - * Write session id - */ - snprintf(wp+timestamp_len, - sesid_str_len, - "[%lu] ", - tls_log_info.li_sesid); - sesid_str_len -= 1; /*< don't calculate terminating char anymore */ - } - /** - * Write next string to overwrite terminating null character - * of the timestamp string. - */ - if (use_valist) { - vsnprintf(wp+timestamp_len+sesid_str_len, - safe_str_len-timestamp_len-sesid_str_len, - str, - valist); - } else { - snprintf(wp+timestamp_len+sesid_str_len, - safe_str_len-timestamp_len-sesid_str_len, - "%s", - str); - } + /** + * Write timestamp with at most characters + * to wp. + * Returned timestamp_len doesn't include terminating null. + */ + if(highprec) + timestamp_len = snprint_timestamp_hp(wp, timestamp_len); + else + timestamp_len = snprint_timestamp(wp, timestamp_len); + if (sesid_str_len != 0) + { + /** + * Write session id + */ + snprintf(wp+timestamp_len, + sesid_str_len, + "[%lu] ", + tls_log_info.li_sesid); + sesid_str_len -= 1; /*< don't calculate terminating char anymore */ + } + /** + * Write next string to overwrite terminating null character + * of the timestamp string. + */ + if (use_valist) { + vsnprintf(wp+timestamp_len+sesid_str_len, + safe_str_len-timestamp_len-sesid_str_len, + str, + valist); + } else { + snprintf(wp+timestamp_len+sesid_str_len, + safe_str_len-timestamp_len-sesid_str_len, + "%s", + str); + } - /** write to syslog */ - if (lf->lf_write_syslog) + /** write to syslog */ + if (lf->lf_write_syslog) + { + switch(id) { + case LOGFILE_ERROR: + syslog(LOG_ERR, "%s", wp+timestamp_len); + break; + + case LOGFILE_MESSAGE: + syslog(LOG_NOTICE, "%s", wp+timestamp_len); + break; + + default: + break; + } + } + /** remove double line feed */ + if (wp[safe_str_len-2] == '\n') + { + wp[safe_str_len-2]=' '; + } + wp[safe_str_len-1] = '\n'; + + if(do_maxscalelog) + { + blockbuf_unregister(bb); + } + else + { + free(wp); + } + /** + * disable because cross-blockbuffer locking either causes deadlock + * or run out of memory blocks. + */ + if (spread_down && false) { + /** + * Write to target log. If spread_down == true, then + * write also to all logs with greater logfile id. + * LOGFILE_ERROR = 1, + * LOGFILE_MESSAGE = 2, + * LOGFILE_TRACE = 4, + * LOGFILE_DEBUG = 8 + * + * So everything written to error log will appear in + * message, trace and debuglog. Messages will be + * written in trace and debug log. + */ + for (i=(id<<1); i<=LOGFILE_LAST; i<<=1) + { + /** pointer to write buffer of larger-id log */ + char* wp_c; + + /**< Check if particular log is enabled */ + if (!(lm->lm_enabled_logfiles & i)) { - switch(id) { - case LOGFILE_ERROR: - syslog(LOG_ERR, "%s", wp+timestamp_len); - break; - - case LOGFILE_MESSAGE: - syslog(LOG_NOTICE, "%s", wp+timestamp_len); - break; - - default: - break; - } + continue; } + /** + * Seek write position and register to block + * buffer. Then print formatted string to + * write position. + */ + wp_c = blockbuf_get_writepos( + &bb_c, + (logfile_id_t)i, + timestamp_len-1+str_len, + flush); + /** + * Copy original string from block buffer to + * other logs' block buffers. + */ + snprintf(wp_c, timestamp_len+str_len, "%s", wp); + /** remove double line feed */ - if (wp[safe_str_len-2] == '\n') - { - wp[safe_str_len-2]=' '; - } - wp[safe_str_len-1] = '\n'; - - if(do_maxscalelog) + if (wp_c[timestamp_len-1+str_len-2] == '\n') { - blockbuf_unregister(bb); + wp_c[timestamp_len-1+str_len-2]=' '; } - else - { - free(wp); - } - /** - * disable because cross-blockbuffer locking either causes deadlock - * or run out of memory blocks. - */ - if (spread_down && false) { - /** - * Write to target log. If spread_down == true, then - * write also to all logs with greater logfile id. - * LOGFILE_ERROR = 1, - * LOGFILE_MESSAGE = 2, - * LOGFILE_TRACE = 4, - * LOGFILE_DEBUG = 8 - * - * So everything written to error log will appear in - * message, trace and debuglog. Messages will be - * written in trace and debug log. - */ - for (i=(id<<1); i<=LOGFILE_LAST; i<<=1) - { - /** pointer to write buffer of larger-id log */ - char* wp_c; + wp_c[timestamp_len-1+str_len-1]='\n'; - /**< Check if particular log is enabled */ - if (!(lm->lm_enabled_logfiles & i)) - { - continue; - } - /** - * Seek write position and register to block - * buffer. Then print formatted string to - * write position. - */ - wp_c = blockbuf_get_writepos( - &bb_c, - (logfile_id_t)i, - timestamp_len-1+str_len, - flush); - /** - * Copy original string from block buffer to - * other logs' block buffers. - */ - snprintf(wp_c, timestamp_len+str_len, "%s", wp); - - /** remove double line feed */ - if (wp_c[timestamp_len-1+str_len-2] == '\n') - { - wp_c[timestamp_len-1+str_len-2]=' '; - } - wp_c[timestamp_len-1+str_len-1]='\n'; - - /** lock-free unregistration, includes flush if - * bb_state == BB_FULL */ - blockbuf_unregister(bb_c); - } - } /* if (spread_down) */ - } /* if (str == NULL) */ + /** lock-free unregistration, includes flush if + * bb_state == BB_FULL */ + blockbuf_unregister(bb_c); + } + } /* if (spread_down) */ + } /* if (str == NULL) */ return_err: - return err; + return err; } /** * Register writer to a block buffer. When reference counter is non-zero the * flusher thread doesn't write the block to disk. * - * @param bb block buffer + * @param bb block buffer */ static void blockbuf_register( - blockbuf_t* bb) + blockbuf_t* bb) { - CHK_BLOCKBUF(bb); - ss_dassert(bb->bb_refcount >= 0); - atomic_add(&bb->bb_refcount, 1); + CHK_BLOCKBUF(bb); + ss_dassert(bb->bb_refcount >= 0); + atomic_add(&bb->bb_refcount, 1); } /** * Unregister writer from block buffer. If the buffer got filled up and there * are no other registered writers anymore, notify the flusher thread. * - * @param bb block buffer + * @param bb block buffer */ static void blockbuf_unregister( - blockbuf_t* bb) + blockbuf_t* bb) { - logfile_t* lf; + logfile_t* lf; - CHK_BLOCKBUF(bb); - ss_dassert(bb->bb_refcount >= 1); - lf = &lm->lm_logfile[bb->bb_fileid]; - CHK_LOGFILE(lf); - /** - * if this is the last client in a full buffer, send write request. - */ - if (atomic_add(&bb->bb_refcount, -1) == 1 && bb->bb_state == BB_FULL) { - skygw_message_send(lf->lf_logmes); - } - ss_dassert(bb->bb_refcount >= 0); + CHK_BLOCKBUF(bb); + ss_dassert(bb->bb_refcount >= 1); + lf = &lm->lm_logfile[bb->bb_fileid]; + CHK_LOGFILE(lf); + /** + * if this is the last client in a full buffer, send write request. + */ + if (atomic_add(&bb->bb_refcount, -1) == 1 && bb->bb_state == BB_FULL) { + skygw_message_send(lf->lf_logmes); + } + ss_dassert(bb->bb_refcount >= 0); } @@ -972,253 +972,253 @@ static void blockbuf_unregister( * */ static char* blockbuf_get_writepos( - blockbuf_t** p_bb, - logfile_id_t id, - size_t str_len, - bool flush) + blockbuf_t** p_bb, + logfile_id_t id, + size_t str_len, + bool flush) { - logfile_t* lf; - mlist_t* bb_list; - char* pos = NULL; - mlist_node_t* node; - blockbuf_t* bb; - ss_debug(bool succp;) + logfile_t* lf; + mlist_t* bb_list; + char* pos = NULL; + mlist_node_t* node; + blockbuf_t* bb; + ss_debug(bool succp;) CHK_LOGMANAGER(lm); - lf = &lm->lm_logfile[id]; - CHK_LOGFILE(lf); - bb_list = &lf->lf_blockbuf_list; + lf = &lm->lm_logfile[id]; + CHK_LOGFILE(lf); + bb_list = &lf->lf_blockbuf_list; - /** Lock list */ - simple_mutex_lock(&bb_list->mlist_mutex, true); - CHK_MLIST(bb_list); + /** Lock list */ + simple_mutex_lock(&bb_list->mlist_mutex, true); + CHK_MLIST(bb_list); - if (bb_list->mlist_nodecount > 0) { - /** - * At least block buffer exists on the list. - */ - node = bb_list->mlist_first; + if (bb_list->mlist_nodecount > 0) { + /** + * At least block buffer exists on the list. + */ + node = bb_list->mlist_first; - /** Loop over blockbuf list to find write position */ - while (true) { - CHK_MLIST_NODE(node); + /** Loop over blockbuf list to find write position */ + while (true) { + CHK_MLIST_NODE(node); - /** Unlock list */ - simple_mutex_unlock(&bb_list->mlist_mutex); - - bb = (blockbuf_t *)node->mlnode_data; - CHK_BLOCKBUF(bb); - - /** Lock buffer */ - simple_mutex_lock(&bb->bb_mutex, true); - - if (bb->bb_state == BB_FULL || bb->bb_buf_left < str_len) { - /** - * This block buffer is too full. - * Send flush request to file writer thread. This causes - * flushing all buffers, and (eventually) frees buffer space. - */ - blockbuf_register(bb); - - bb->bb_state = BB_FULL; - - blockbuf_unregister(bb); - - /** Unlock buffer */ - simple_mutex_unlock(&bb->bb_mutex); - - /** Lock list */ - simple_mutex_lock(&bb_list->mlist_mutex, true); - - - /** - * If next node exists move forward. Else check if there is - * space for a new block buffer on the list. - */ - if (node != bb_list->mlist_last) { - node = node->mlnode_next; - continue; - } - /** - * All buffers on the list are full. - */ - if (bb_list->mlist_nodecount < - bb_list->mlist_nodecount_max) - { - /** - * New node is created - */ - if((bb = blockbuf_init(id)) == NULL) - return NULL; - - CHK_BLOCKBUF(bb); - - /** - * Increase version to odd to mark list update active - * update. - */ - bb_list->mlist_versno += 1; - ss_dassert(bb_list->mlist_versno%2 == 1); - - ss_debug(succp =) - mlist_add_data_nomutex(bb_list, bb); - ss_dassert(succp); - - /** - * Increase version to even to mark completion of update. - */ - bb_list->mlist_versno += 1; - ss_dassert(bb_list->mlist_versno%2 == 0); - } else { - /** - * List and buffers are full. - * Reset to the beginning of the list, and wait until - * there is a block buffer with enough space. - */ - simple_mutex_unlock(&bb_list->mlist_mutex); - simple_mutex_lock(&bb_list->mlist_mutex, true); - - node = bb_list->mlist_first; - continue; - } - - }else if(bb->bb_state == BB_CLEARED){ - - /** - *Move the cleared buffer to the end of the list if it is the first one in the list - */ - - simple_mutex_unlock(&bb->bb_mutex); - simple_mutex_lock(&bb_list->mlist_mutex, true); - - if(node == bb_list->mlist_first) - { - - if(bb_list->mlist_nodecount > 1 && - node != bb_list->mlist_last){ - bb_list->mlist_last->mlnode_next = bb_list->mlist_first; - bb_list->mlist_first = bb_list->mlist_first->mlnode_next; - bb_list->mlist_last->mlnode_next->mlnode_next = NULL; - bb_list->mlist_last = bb_list->mlist_last->mlnode_next; - } - - ss_dassert(node == bb_list->mlist_last); - - simple_mutex_unlock(&bb_list->mlist_mutex); - simple_mutex_lock(&bb->bb_mutex, true); - - bb->bb_state = BB_READY; - - simple_mutex_unlock(&bb->bb_mutex); - simple_mutex_lock(&bb_list->mlist_mutex, true); - node = bb_list->mlist_first; - } - else - { - if(node->mlnode_next){ - node = node->mlnode_next; - }else{ - node = bb_list->mlist_first; - } - continue; - } - - }else if (bb->bb_state == BB_READY){ - /** - * There is space for new log string. - */ - break; - } - } /** while (true) */ - } else { - - /** - * Create the first block buffer to logfile's blockbuf list. - */ - - if((bb = blockbuf_init(id)) == NULL) - return NULL; + /** Unlock list */ + simple_mutex_unlock(&bb_list->mlist_mutex); + bb = (blockbuf_t *)node->mlnode_data; CHK_BLOCKBUF(bb); /** Lock buffer */ simple_mutex_lock(&bb->bb_mutex, true); - /** - * Increase version to odd to mark list update active update. - */ - bb_list->mlist_versno += 1; - ss_dassert(bb_list->mlist_versno%2 == 1); - ss_debug(succp =)mlist_add_data_nomutex(bb_list, bb); - ss_dassert(succp); + if (bb->bb_state == BB_FULL || bb->bb_buf_left < str_len) { + /** + * This block buffer is too full. + * Send flush request to file writer thread. This causes + * flushing all buffers, and (eventually) frees buffer space. + */ + blockbuf_register(bb); - /** - * Increase version to even to mark completion of update. - */ - bb_list->mlist_versno += 1; - ss_dassert(bb_list->mlist_versno%2 == 0); + bb->bb_state = BB_FULL; - /** Unlock list */ - simple_mutex_unlock(&bb_list->mlist_mutex); - } /* if (bb_list->mlist_nodecount > 0) */ + blockbuf_unregister(bb); - ss_dassert(pos == NULL); - ss_dassert(!(bb->bb_state == BB_FULL || bb->bb_buf_left < str_len)); - ss_dassert(bb_list->mlist_nodecount <= bb_list->mlist_nodecount_max); + /** Unlock buffer */ + simple_mutex_unlock(&bb->bb_mutex); + + /** Lock list */ + simple_mutex_lock(&bb_list->mlist_mutex, true); + + + /** + * If next node exists move forward. Else check if there is + * space for a new block buffer on the list. + */ + if (node != bb_list->mlist_last) { + node = node->mlnode_next; + continue; + } + /** + * All buffers on the list are full. + */ + if (bb_list->mlist_nodecount < + bb_list->mlist_nodecount_max) + { + /** + * New node is created + */ + if((bb = blockbuf_init(id)) == NULL) + return NULL; + + CHK_BLOCKBUF(bb); + + /** + * Increase version to odd to mark list update active + * update. + */ + bb_list->mlist_versno += 1; + ss_dassert(bb_list->mlist_versno%2 == 1); + + ss_debug(succp =) + mlist_add_data_nomutex(bb_list, bb); + ss_dassert(succp); + + /** + * Increase version to even to mark completion of update. + */ + bb_list->mlist_versno += 1; + ss_dassert(bb_list->mlist_versno%2 == 0); + } else { + /** + * List and buffers are full. + * Reset to the beginning of the list, and wait until + * there is a block buffer with enough space. + */ + simple_mutex_unlock(&bb_list->mlist_mutex); + simple_mutex_lock(&bb_list->mlist_mutex, true); + + node = bb_list->mlist_first; + continue; + } + + }else if(bb->bb_state == BB_CLEARED){ + + /** + *Move the cleared buffer to the end of the list if it is the first one in the list + */ + + simple_mutex_unlock(&bb->bb_mutex); + simple_mutex_lock(&bb_list->mlist_mutex, true); + + if(node == bb_list->mlist_first) + { + + if(bb_list->mlist_nodecount > 1 && + node != bb_list->mlist_last){ + bb_list->mlist_last->mlnode_next = bb_list->mlist_first; + bb_list->mlist_first = bb_list->mlist_first->mlnode_next; + bb_list->mlist_last->mlnode_next->mlnode_next = NULL; + bb_list->mlist_last = bb_list->mlist_last->mlnode_next; + } + + ss_dassert(node == bb_list->mlist_last); + + simple_mutex_unlock(&bb_list->mlist_mutex); + simple_mutex_lock(&bb->bb_mutex, true); + + bb->bb_state = BB_READY; + + simple_mutex_unlock(&bb->bb_mutex); + simple_mutex_lock(&bb_list->mlist_mutex, true); + node = bb_list->mlist_first; + } + else + { + if(node->mlnode_next){ + node = node->mlnode_next; + }else{ + node = bb_list->mlist_first; + } + continue; + } + + }else if (bb->bb_state == BB_READY){ + /** + * There is space for new log string. + */ + break; + } + } /** while (true) */ + } else { /** - * Registration to blockbuf adds reference for the write operation. - * It indicates that client has allocated space from the buffer, - * but not written yet. As long as refcount > 0 buffer can't be - * written to disk. + * Create the first block buffer to logfile's blockbuf list. */ - blockbuf_register(bb); - *p_bb = bb; + + if((bb = blockbuf_init(id)) == NULL) + return NULL; + + CHK_BLOCKBUF(bb); + + /** Lock buffer */ + simple_mutex_lock(&bb->bb_mutex, true); /** - * At this point list mutex is held and bb points to a node with - * enough space available. pos is not yet set. + * Increase version to odd to mark list update active update. */ - pos = &bb->bb_buf[bb->bb_buf_used]; - bb->bb_buf_used += str_len; - bb->bb_buf_left -= str_len; + bb_list->mlist_versno += 1; + ss_dassert(bb_list->mlist_versno%2 == 1); - ss_dassert(pos >= &bb->bb_buf[0] && - pos <= &bb->bb_buf[MAX_LOGSTRLEN-str_len]); + ss_debug(succp =)mlist_add_data_nomutex(bb_list, bb); + ss_dassert(succp); - /** read checkmark */ - /** TODO: add buffer overflow checkmark + /** + * Increase version to even to mark completion of update. + */ + bb_list->mlist_versno += 1; + ss_dassert(bb_list->mlist_versno%2 == 0); + + /** Unlock list */ + simple_mutex_unlock(&bb_list->mlist_mutex); + } /* if (bb_list->mlist_nodecount > 0) */ + + ss_dassert(pos == NULL); + ss_dassert(!(bb->bb_state == BB_FULL || bb->bb_buf_left < str_len)); + ss_dassert(bb_list->mlist_nodecount <= bb_list->mlist_nodecount_max); + + /** + * Registration to blockbuf adds reference for the write operation. + * It indicates that client has allocated space from the buffer, + * but not written yet. As long as refcount > 0 buffer can't be + * written to disk. + */ + blockbuf_register(bb); + *p_bb = bb; + /** + * At this point list mutex is held and bb points to a node with + * enough space available. pos is not yet set. + */ + pos = &bb->bb_buf[bb->bb_buf_used]; + bb->bb_buf_used += str_len; + bb->bb_buf_left -= str_len; + + ss_dassert(pos >= &bb->bb_buf[0] && + pos <= &bb->bb_buf[MAX_LOGSTRLEN-str_len]); + + /** read checkmark */ + /** TODO: add buffer overflow checkmark chk_val = (int)bb->bb_buf[bb->bb_buf_used-count_len]; ss_dassert(chk_val == bb->bb_strcount); - */ + */ - /** TODO : write next checkmark + /** TODO : write next checkmark bb->bb_strcount += 1; memcpy(&bb->bb_buf[bb->bb_buf_used], &bb->bb_strcount, count_len); bb->bb_buf_used += count_len; bb->bb_buf_left -= count_len; - */ + */ - /** - * If flush flag is set, set buffer full. As a consequence, no-one - * can write to it before it is flushed to disk. - */ - bb->bb_state = (flush == true ? BB_FULL : bb->bb_state); + /** + * If flush flag is set, set buffer full. As a consequence, no-one + * can write to it before it is flushed to disk. + */ + bb->bb_state = (flush == true ? BB_FULL : bb->bb_state); - /** Unlock buffer */ - simple_mutex_unlock(&bb->bb_mutex); - return pos; + /** Unlock buffer */ + simple_mutex_unlock(&bb->bb_mutex); + return pos; } static void blockbuf_node_done( - void* bb_data) + void* bb_data) { - blockbuf_t* bb = (blockbuf_t *)bb_data; - simple_mutex_done(&bb->bb_mutex); + blockbuf_t* bb = (blockbuf_t *)bb_data; + simple_mutex_done(&bb->bb_mutex); } static blockbuf_t* blockbuf_init( - logfile_id_t id) + logfile_id_t id) { blockbuf_t* bb; @@ -1248,111 +1248,111 @@ static blockbuf_t* blockbuf_init( int skygw_log_enable( - logfile_id_t id) + logfile_id_t id) { - bool err = 0; + bool err = 0; - if (!logmanager_register(true)) - { - err = -1; - goto return_err; - } - CHK_LOGMANAGER(lm); + if (!logmanager_register(true)) + { + err = -1; + goto return_err; + } + CHK_LOGMANAGER(lm); - if (logfile_set_enabled(id, true)) { - lm->lm_enabled_logfiles |= id; - /** - * Set global variable - */ - lm_enabled_logfiles_bitmask = lm->lm_enabled_logfiles; - } + if (logfile_set_enabled(id, true)) { + lm->lm_enabled_logfiles |= id; + /** + * Set global variable + */ + lm_enabled_logfiles_bitmask = lm->lm_enabled_logfiles; + } - logmanager_unregister(); + logmanager_unregister(); return_err: - return err; + return err; } int skygw_log_disable( - logfile_id_t id) /*< no locking */ + logfile_id_t id) /*< no locking */ { - int rc; + int rc; - rc = skygw_log_disable_raw(id, false); + rc = skygw_log_disable_raw(id, false); - return rc; + return rc; } static int skygw_log_disable_raw( - logfile_id_t id, - bool emergency) /*< no locking */ + logfile_id_t id, + bool emergency) /*< no locking */ { - bool err = 0; + bool err = 0; - if (!logmanager_register(true)) - { - err = -1; - goto return_err; - } - CHK_LOGMANAGER(lm); + if (!logmanager_register(true)) + { + err = -1; + goto return_err; + } + CHK_LOGMANAGER(lm); - if (emergency || logfile_set_enabled(id, false)) - { - lm->lm_enabled_logfiles &= ~id; - /** - * Set global variable - */ - lm_enabled_logfiles_bitmask = lm->lm_enabled_logfiles; - } + if (emergency || logfile_set_enabled(id, false)) + { + lm->lm_enabled_logfiles &= ~id; + /** + * Set global variable + */ + lm_enabled_logfiles_bitmask = lm->lm_enabled_logfiles; + } - logmanager_unregister(); + logmanager_unregister(); return_err: - return err; + return err; } static bool logfile_set_enabled( - logfile_id_t id, - bool val) + logfile_id_t id, + bool val) { - char* logstr; - va_list notused; - bool oldval; - bool succp = false; - int err = 0; - logfile_t* lf; + char* logstr; + va_list notused; + bool oldval; + bool succp = false; + int err = 0; + logfile_t* lf; - CHK_LOGMANAGER(lm); + CHK_LOGMANAGER(lm); - if (id < LOGFILE_FIRST || id > LOGFILE_LAST) { - const char* errstr = "Invalid logfile id argument."; - /** - * invalid id, since we don't have logfile yet. - */ - err = logmanager_write_log(LOGFILE_ERROR, - true, - false, - false, - false, - strlen(errstr)+1, - errstr, - notused); - if (err != 0) { - fprintf(stderr, - "* Writing to logfile %s failed.\n", - STRLOGID(LOGFILE_ERROR)); - } - ss_dassert(false); - goto return_succp; + if (id < LOGFILE_FIRST || id > LOGFILE_LAST) { + const char* errstr = "Invalid logfile id argument."; + /** + * invalid id, since we don't have logfile yet. + */ + err = logmanager_write_log(LOGFILE_ERROR, + true, + false, + false, + false, + strlen(errstr)+1, + errstr, + notused); + if (err != 0) { + fprintf(stderr, + "* Writing to logfile %s failed.\n", + STRLOGID(LOGFILE_ERROR)); + } + ss_dassert(false); + goto return_succp; + } + lf = &lm->lm_logfile[id]; + CHK_LOGFILE(lf); + if(use_stdout == 0) + { + if (val) { + logstr = strdup("---\tLogging to file is enabled\t--"); + } else { + logstr = strdup("---\tLogging to file is disabled\t--"); } - lf = &lm->lm_logfile[id]; - CHK_LOGFILE(lf); - if(use_stdout == 0) - { - if (val) { - logstr = strdup("---\tLogging to file is enabled\t--"); - } else { - logstr = strdup("---\tLogging to file is disabled\t--"); - } oldval = lf->lf_enabled; lf->lf_enabled = val; @@ -1360,23 +1360,23 @@ static bool logfile_set_enabled( true, false, false, - false, + false, strlen(logstr)+1, logstr, notused); free(logstr); - } - if (err != 0) { - lf->lf_enabled = oldval; - fprintf(stderr, - "logfile_set_enabled failed. Writing notification to logfile %s " - "failed.\n ", - STRLOGID(id)); - goto return_succp; - } - succp = true; + } + if (err != 0) { + lf->lf_enabled = oldval; + fprintf(stderr, + "logfile_set_enabled failed. Writing notification to logfile %s " + "failed.\n ", + STRLOGID(id)); + goto return_succp; + } + succp = true; return_succp: - return succp; + return succp; } void skygw_log_set_augmentation(int bits) @@ -1500,53 +1500,53 @@ static int log_write(logfile_id_t id, } int skygw_log_write_context_flush( - logfile_id_t id, - const char* file, - int line, - const char* function, - const char* str, - ...) + logfile_id_t id, + const char* file, + int line, + const char* function, + const char* str, + ...) { - int err = 0; - va_list valist; + int err = 0; + va_list valist; + + /** + * Find out the length of log string (to be formatted str). + */ + va_start(valist, str); + int len = vsnprintf(NULL, 0, str, valist); + va_end(valist); + + if (len >= 0) { + char message[len + 1]; - /** - * Find out the length of log string (to be formatted str). - */ va_start(valist, str); - int len = vsnprintf(NULL, 0, str, valist); + int len2 = vsnprintf(message, sizeof(message), str, valist); va_end(valist); + assert(len2 == len); - if (len >= 0) { - char message[len + 1]; + const bool flush = true; - va_start(valist, str); - int len2 = vsnprintf(message, sizeof(message), str, valist); - va_end(valist); - assert(len2 == len); + err = log_write(id, file, line, function, len2, message, flush); - const bool flush = true; - - err = log_write(id, file, line, function, len2, message, flush); - - if (err != 0) - { - fprintf(stderr, "skygw_log_write_flush failed.\n"); - } + if (err != 0) + { + fprintf(stderr, "skygw_log_write_flush failed.\n"); } + } - return err; + return err; } int skygw_log_write_context( - logfile_id_t id, - const char* file, - int line, - const char* function, - const char* str, - ...) + logfile_id_t id, + const char* file, + int line, + const char* function, + const char* str, + ...) { int err = 0; va_list valist; @@ -1580,28 +1580,28 @@ int skygw_log_write_context( int skygw_log_flush( - logfile_id_t id) + logfile_id_t id) { - int err = 0; - va_list valist; /**< Dummy, must be present but it is not processed */ + int err = 0; + va_list valist; /**< Dummy, must be present but it is not processed */ - if (!logmanager_register(false)) { - ss_dfprintf(stderr, - "Can't register to logmanager, nothing to flush\n"); - goto return_err; - } - CHK_LOGMANAGER(lm); - err = logmanager_write_log(id, true, false, false, false, 0, NULL, valist); + if (!logmanager_register(false)) { + ss_dfprintf(stderr, + "Can't register to logmanager, nothing to flush\n"); + goto return_err; + } + CHK_LOGMANAGER(lm); + err = logmanager_write_log(id, true, false, false, false, 0, NULL, valist); - if (err != 0) { - fprintf(stderr, "skygw_log_flush failed.\n"); - goto return_unregister; - } + if (err != 0) { + fprintf(stderr, "skygw_log_flush failed.\n"); + goto return_unregister; + } return_unregister: - logmanager_unregister(); + logmanager_unregister(); return_err: - return err; + return err; } /** @@ -1609,50 +1609,50 @@ return_err: * its name. */ int skygw_log_rotate( - logfile_id_t id) + logfile_id_t id) { - int err = 0; - logfile_t* lf; - va_list valist; /**< Dummy, must be present but it is not processed */ + int err = 0; + logfile_t* lf; + va_list valist; /**< Dummy, must be present but it is not processed */ - if (!logmanager_register(false)) - { - ss_dfprintf(stderr, - "Can't register to logmanager, rotating failed\n"); - goto return_err; - } - CHK_LOGMANAGER(lm); - lf = &lm->lm_logfile[id]; + if (!logmanager_register(false)) + { + ss_dfprintf(stderr, + "Can't register to logmanager, rotating failed\n"); + goto return_err; + } + CHK_LOGMANAGER(lm); + lf = &lm->lm_logfile[id]; - LOGIF(LM, (skygw_log_write( - LOGFILE_MESSAGE, - "Log rotation is called for %s.", - lf->lf_full_file_name))); + LOGIF(LM, (skygw_log_write( + LOGFILE_MESSAGE, + "Log rotation is called for %s.", + lf->lf_full_file_name))); - err = logmanager_write_log(id, false, false, false, true, 0, NULL, valist); + err = logmanager_write_log(id, false, false, false, true, 0, NULL, valist); - if (err != 0) - { - LOGIF(LE, (skygw_log_write( - LOGFILE_ERROR, - "Log file rotation failed for file %s.", - lf->lf_full_file_name))); + if (err != 0) + { + LOGIF(LE, (skygw_log_write( + LOGFILE_ERROR, + "Log file rotation failed for file %s.", + lf->lf_full_file_name))); - fprintf(stderr, "skygw_log_rotate failed.\n"); - goto return_unregister; - } + fprintf(stderr, "skygw_log_rotate failed.\n"); + goto return_unregister; + } return_unregister: - LOGIF(LM, (skygw_log_write_flush( - LOGFILE_MESSAGE, - "File %s use for log writing..", - lf->lf_full_file_name))); + LOGIF(LM, (skygw_log_write_flush( + LOGFILE_MESSAGE, + "File %s use for log writing..", + lf->lf_full_file_name))); - logmanager_unregister(); + logmanager_unregister(); return_err: - return err; + return err; } @@ -1672,54 +1672,54 @@ return_err: * */ static bool logmanager_register( - bool writep) + bool writep) { - bool succp = true; + bool succp = true; - acquire_lock(&lmlock); + acquire_lock(&lmlock); - if (lm == NULL || !lm->lm_enabled) { - /** - * Flush succeeds if logmanager is shut or shutting down. - * returning false so that flusher doesn't access logmanager - * and its members which would probabaly lead to NULL pointer - * reference. - */ - if (!writep || fatal_error) { - succp = false; - goto return_succp; - } - - ss_dassert(lm == NULL || (lm != NULL && !lm->lm_enabled)); - - /** - * Wait until logmanager shut down has completed. - * logmanager is enabled if someone already restarted - * it between latest lock release, and acquire. - */ - while (lm != NULL && !lm->lm_enabled) { - release_lock(&lmlock); - pthread_yield(); - acquire_lock(&lmlock); - } - - if (lm == NULL) { - succp = logmanager_init_nomutex(0, NULL); - } - } - /** if logmanager existed or was succesfully restarted, increase link */ - if (succp) { - lm->lm_nlinks += 1; + if (lm == NULL || !lm->lm_enabled) { + /** + * Flush succeeds if logmanager is shut or shutting down. + * returning false so that flusher doesn't access logmanager + * and its members which would probabaly lead to NULL pointer + * reference. + */ + if (!writep || fatal_error) { + succp = false; + goto return_succp; } - return_succp: + ss_dassert(lm == NULL || (lm != NULL && !lm->lm_enabled)); - if(!succp) - { - fatal_error = true; + /** + * Wait until logmanager shut down has completed. + * logmanager is enabled if someone already restarted + * it between latest lock release, and acquire. + */ + while (lm != NULL && !lm->lm_enabled) { + release_lock(&lmlock); + pthread_yield(); + acquire_lock(&lmlock); } - release_lock(&lmlock); - return succp; + + if (lm == NULL) { + succp = logmanager_init_nomutex(0, NULL); + } + } + /** if logmanager existed or was succesfully restarted, increase link */ + if (succp) { + lm->lm_nlinks += 1; + } + +return_succp: + + if(!succp) + { + fatal_error = true; + } + release_lock(&lmlock); + return succp; } /** @@ -1737,12 +1737,12 @@ static bool logmanager_register( */ static void logmanager_unregister(void) { - acquire_lock(&lmlock); + acquire_lock(&lmlock); - lm->lm_nlinks -= 1; - ss_dassert(lm->lm_nlinks >= 0); + lm->lm_nlinks -= 1; + ss_dassert(lm->lm_nlinks >= 0); - release_lock(&lmlock); + release_lock(&lmlock); } @@ -1767,232 +1767,232 @@ static void logmanager_unregister(void) * */ static bool fnames_conf_init( - fnames_conf_t* fn, - int argc, - char* argv[]) + fnames_conf_t* fn, + int argc, + char* argv[]) { - int opt; - bool succp = false; - const char* argstr = - "-h - help\n" - "-a ............(\"skygw_debug\")\n" - "-b ............(\".log\")\n" - "-c ............(\"skygw_trace\")\n" - "-d ............(\".log\")\n" - "-e ............(\"skygw_msg\")\n" - "-f ............(\".log\")\n" - "-g ............(\"skygw_err\")\n" - "-i ............(\".log\")\n" - "-j ............(\"/tmp\")\n" - "-l .......(no default)\n" - "-m ............(argv[0])\n" - "-s .......(no default)\n" - "-o .......(write logs to stdout)\n"; + int opt; + bool succp = false; + const char* argstr = + "-h - help\n" + "-a ............(\"skygw_debug\")\n" + "-b ............(\".log\")\n" + "-c ............(\"skygw_trace\")\n" + "-d ............(\".log\")\n" + "-e ............(\"skygw_msg\")\n" + "-f ............(\".log\")\n" + "-g ............(\"skygw_err\")\n" + "-i ............(\".log\")\n" + "-j ............(\"/tmp\")\n" + "-l .......(no default)\n" + "-m ............(argv[0])\n" + "-s .......(no default)\n" + "-o .......(write logs to stdout)\n"; - /** - * When init_started is set, clean must be done for it. - */ - fn->fn_state = INIT; + /** + * When init_started is set, clean must be done for it. + */ + fn->fn_state = INIT; #if defined(SS_DEBUG) - fn->fn_chk_top = CHK_NUM_FNAMES; - fn->fn_chk_tail = CHK_NUM_FNAMES; + fn->fn_chk_top = CHK_NUM_FNAMES; + fn->fn_chk_tail = CHK_NUM_FNAMES; #endif - optind = 1; /**fn_debug_prefix = strndup(optarg, MAX_PREFIXLEN); - break; + optind = 1; /**fn_debug_prefix = strndup(optarg, MAX_PREFIXLEN); + break; - case 'b': - fn->fn_debug_suffix = strndup(optarg, MAX_SUFFIXLEN); - break; - case 'c': - fn->fn_trace_prefix = strndup(optarg, MAX_PREFIXLEN); - break; + case 'b': + fn->fn_debug_suffix = strndup(optarg, MAX_SUFFIXLEN); + break; + case 'c': + fn->fn_trace_prefix = strndup(optarg, MAX_PREFIXLEN); + break; - case 'd': - fn->fn_trace_suffix = strndup(optarg, MAX_SUFFIXLEN); - break; + case 'd': + fn->fn_trace_suffix = strndup(optarg, MAX_SUFFIXLEN); + break; - case 'e': - fn->fn_msg_prefix = strndup(optarg, MAX_PREFIXLEN); - break; + case 'e': + fn->fn_msg_prefix = strndup(optarg, MAX_PREFIXLEN); + break; - case 'f': - fn->fn_msg_suffix = strndup(optarg, MAX_SUFFIXLEN); - break; + case 'f': + fn->fn_msg_suffix = strndup(optarg, MAX_SUFFIXLEN); + break; - case 'g': - fn->fn_err_prefix = strndup(optarg, MAX_PREFIXLEN); - break; + case 'g': + fn->fn_err_prefix = strndup(optarg, MAX_PREFIXLEN); + break; - case 'i': - fn->fn_err_suffix = strndup(optarg, MAX_SUFFIXLEN); - break; + case 'i': + fn->fn_err_suffix = strndup(optarg, MAX_SUFFIXLEN); + break; - case 'j': - fn->fn_logpath = strndup(optarg, MAX_PATHLEN); - break; + case 'j': + fn->fn_logpath = strndup(optarg, MAX_PATHLEN); + break; - case 'l': - /** record list of log file ids for syslogged */ - if(do_syslog) - { - if (syslog_id_str != NULL) - { - free (syslog_id_str); - } - syslog_id_str = optarg; - } - break; + case 'l': + /** record list of log file ids for syslogged */ + if(do_syslog) + { + if (syslog_id_str != NULL) + { + free (syslog_id_str); + } + syslog_id_str = optarg; + } + break; - case 'm': - /** - * Identity string for syslog printing, needs '-l' - * to be effective. - */ - syslog_ident_str = optarg; - break; + case 'm': + /** + * Identity string for syslog printing, needs '-l' + * to be effective. + */ + syslog_ident_str = optarg; + break; - case 's': - /** record list of log file ids for later use */ - shmem_id_str = optarg; - break; - case 'h': - default: - fprintf(stderr, - "\nSupported arguments are (default)\n%s\n", - argstr); - goto return_conf_init; - } /** switch (opt) */ - } - /** If log file name is not specified in call arguments, use default. */ - fn->fn_debug_prefix = (fn->fn_debug_prefix == NULL) ? - strdup(get_debug_prefix_default()) : fn->fn_debug_prefix; - fn->fn_debug_suffix = (fn->fn_debug_suffix == NULL) ? - strdup(get_debug_suffix_default()) : fn->fn_debug_suffix; - fn->fn_trace_prefix = (fn->fn_trace_prefix == NULL) ? - strdup(get_trace_prefix_default()) : fn->fn_trace_prefix; - fn->fn_trace_suffix = (fn->fn_trace_suffix == NULL) ? - strdup(get_trace_suffix_default()) : fn->fn_trace_suffix; - fn->fn_msg_prefix = (fn->fn_msg_prefix == NULL) ? - strdup(get_msg_prefix_default()) : fn->fn_msg_prefix; - fn->fn_msg_suffix = (fn->fn_msg_suffix == NULL) ? - strdup(get_msg_suffix_default()) : fn->fn_msg_suffix; - fn->fn_err_prefix = (fn->fn_err_prefix == NULL) ? - strdup(get_err_prefix_default()) : fn->fn_err_prefix; - fn->fn_err_suffix = (fn->fn_err_suffix == NULL) ? - strdup(get_err_suffix_default()) : fn->fn_err_suffix; - fn->fn_logpath = (fn->fn_logpath == NULL) ? - strdup(get_logpath_default()) : fn->fn_logpath; + case 's': + /** record list of log file ids for later use */ + shmem_id_str = optarg; + break; + case 'h': + default: + fprintf(stderr, + "\nSupported arguments are (default)\n%s\n", + argstr); + goto return_conf_init; + } /** switch (opt) */ + } + /** If log file name is not specified in call arguments, use default. */ + fn->fn_debug_prefix = (fn->fn_debug_prefix == NULL) ? + strdup(get_debug_prefix_default()) : fn->fn_debug_prefix; + fn->fn_debug_suffix = (fn->fn_debug_suffix == NULL) ? + strdup(get_debug_suffix_default()) : fn->fn_debug_suffix; + fn->fn_trace_prefix = (fn->fn_trace_prefix == NULL) ? + strdup(get_trace_prefix_default()) : fn->fn_trace_prefix; + fn->fn_trace_suffix = (fn->fn_trace_suffix == NULL) ? + strdup(get_trace_suffix_default()) : fn->fn_trace_suffix; + fn->fn_msg_prefix = (fn->fn_msg_prefix == NULL) ? + strdup(get_msg_prefix_default()) : fn->fn_msg_prefix; + fn->fn_msg_suffix = (fn->fn_msg_suffix == NULL) ? + strdup(get_msg_suffix_default()) : fn->fn_msg_suffix; + fn->fn_err_prefix = (fn->fn_err_prefix == NULL) ? + strdup(get_err_prefix_default()) : fn->fn_err_prefix; + fn->fn_err_suffix = (fn->fn_err_suffix == NULL) ? + strdup(get_err_suffix_default()) : fn->fn_err_suffix; + fn->fn_logpath = (fn->fn_logpath == NULL) ? + strdup(get_logpath_default()) : fn->fn_logpath; - /** Set identity string for syslog if it is not set in config.*/ - if(do_syslog) - { + /** Set identity string for syslog if it is not set in config.*/ + if(do_syslog) + { syslog_ident_str = (syslog_ident_str == NULL ? (argv == NULL ? strdup(program_invocation_short_name) : strdup(*argv)) : syslog_ident_str); - } - /* ss_dfprintf(stderr, "\n\n\tCommand line : "); - for (i=0; ifn_logpath, - fn->fn_err_prefix, - fn->fn_err_suffix, - fn->fn_logpath, - fn->fn_msg_prefix, - fn->fn_msg_suffix, - fn->fn_logpath, - fn->fn_trace_prefix, - fn->fn_trace_suffix, - fn->fn_logpath, - fn->fn_debug_prefix, - fn->fn_debug_suffix); + fprintf(stderr, + "Error log :\t%s/%s1%s\n" + "Message log :\t%s/%s1%s\n" + "Trace log :\t%s/%s1%s\n" + "Debug log :\t%s/%s1%s\n\n", + fn->fn_logpath, + fn->fn_err_prefix, + fn->fn_err_suffix, + fn->fn_logpath, + fn->fn_msg_prefix, + fn->fn_msg_suffix, + fn->fn_logpath, + fn->fn_trace_prefix, + fn->fn_trace_suffix, + fn->fn_logpath, + fn->fn_debug_prefix, + fn->fn_debug_suffix); #endif - succp = true; - fn->fn_state = RUN; - CHK_FNAMES_CONF(fn); + succp = true; + fn->fn_state = RUN; + CHK_FNAMES_CONF(fn); return_conf_init: - if (!succp) { - fnames_conf_done(fn); - } - ss_dassert(fn->fn_state == RUN || fn->fn_state == DONE); - return succp; + if (!succp) { + fnames_conf_done(fn); + } + ss_dassert(fn->fn_state == RUN || fn->fn_state == DONE); + return succp; } static char* fname_conf_get_prefix( - fnames_conf_t* fn, - logfile_id_t id) + fnames_conf_t* fn, + logfile_id_t id) { - CHK_FNAMES_CONF(fn); - ss_dassert(id >= LOGFILE_FIRST && id <= LOGFILE_LAST); + CHK_FNAMES_CONF(fn); + ss_dassert(id >= LOGFILE_FIRST && id <= LOGFILE_LAST); - switch (id) { - case LOGFILE_DEBUG: - return strdup(fn->fn_debug_prefix); - break; + switch (id) { + case LOGFILE_DEBUG: + return strdup(fn->fn_debug_prefix); + break; - case LOGFILE_TRACE: - return strdup(fn->fn_trace_prefix); - break; + case LOGFILE_TRACE: + return strdup(fn->fn_trace_prefix); + break; - case LOGFILE_MESSAGE: - return strdup(fn->fn_msg_prefix); - break; + case LOGFILE_MESSAGE: + return strdup(fn->fn_msg_prefix); + break; - case LOGFILE_ERROR: - return strdup(fn->fn_err_prefix); - break; + case LOGFILE_ERROR: + return strdup(fn->fn_err_prefix); + break; - default: - return NULL; - } + default: + return NULL; + } } static char* fname_conf_get_suffix( - fnames_conf_t* fn, - logfile_id_t id) + fnames_conf_t* fn, + logfile_id_t id) { - CHK_FNAMES_CONF(fn); - ss_dassert(id >= LOGFILE_FIRST && id <= LOGFILE_LAST); + CHK_FNAMES_CONF(fn); + ss_dassert(id >= LOGFILE_FIRST && id <= LOGFILE_LAST); - switch (id) { - case LOGFILE_DEBUG: - return strdup(fn->fn_debug_suffix); - break; + switch (id) { + case LOGFILE_DEBUG: + return strdup(fn->fn_debug_suffix); + break; - case LOGFILE_TRACE: - return strdup(fn->fn_trace_suffix); - break; + case LOGFILE_TRACE: + return strdup(fn->fn_trace_suffix); + break; - case LOGFILE_MESSAGE: - return strdup(fn->fn_msg_suffix); - break; + case LOGFILE_MESSAGE: + return strdup(fn->fn_msg_suffix); + break; - case LOGFILE_ERROR: - return strdup(fn->fn_err_suffix); - break; + case LOGFILE_ERROR: + return strdup(fn->fn_err_suffix); + break; - default: - return NULL; - } + default: + return NULL; + } } @@ -2001,9 +2001,9 @@ static char* fname_conf_get_suffix( * * * Parameters: - * @param lm Log manager pointer + * @param lm Log manager pointer * - * @return succp true if succeed, otherwise false. + * @return succp true if succeed, otherwise false. * * * @details If logfile is supposed to be located to shared memory @@ -2015,230 +2015,230 @@ static char* fname_conf_get_suffix( * */ static bool logfiles_init( - logmanager_t* lm) + logmanager_t* lm) { - bool succp = true; - int lid = LOGFILE_FIRST; - int i = 0; - bool store_shmem; - bool write_syslog; + bool succp = true; + int lid = LOGFILE_FIRST; + int i = 0; + bool store_shmem; + bool write_syslog; - /** Open syslog immediately. Print pid of loggind process. */ - if (syslog_id_str != NULL) + /** Open syslog immediately. Print pid of loggind process. */ + if (syslog_id_str != NULL) + { + openlog(syslog_ident_str, LOG_PID | LOG_NDELAY, LOG_USER); + } + /** + * Initialize log files, pass softlink flag if necessary. + */ + while (lid<=LOGFILE_LAST && succp) { + /** + * Check if the file is stored in shared memory. If so, + * a symbolic link will be created to log directory. + */ + if (shmem_id_str != NULL && + strcasestr(shmem_id_str, + STRLOGID(logfile_id_t(lid))) != NULL) { - openlog(syslog_ident_str, LOG_PID | LOG_NDELAY, LOG_USER); + store_shmem = true; + } + else + { + store_shmem = false; } /** - * Initialize log files, pass softlink flag if necessary. + * Check if file is also written to syslog. */ - while (lid<=LOGFILE_LAST && succp) { - /** - * Check if the file is stored in shared memory. If so, - * a symbolic link will be created to log directory. - */ - if (shmem_id_str != NULL && - strcasestr(shmem_id_str, - STRLOGID(logfile_id_t(lid))) != NULL) - { - store_shmem = true; - } - else - { - store_shmem = false; - } - /** - * Check if file is also written to syslog. - */ - if (syslog_id_str != NULL && - strcasestr(syslog_id_str, - STRLOGID(logfile_id_t(lid))) != NULL) - { - write_syslog = true; - } - else - { - write_syslog = false; - } - succp = logfile_init(&lm->lm_logfile[lid], - (logfile_id_t)lid, - lm, - store_shmem, - write_syslog); - - if (!succp) { - fprintf(stderr, "*\n* Error : Initializing log files failed.\n"); - break; - } - lid <<= 1; - i += 1; + if (syslog_id_str != NULL && + strcasestr(syslog_id_str, + STRLOGID(logfile_id_t(lid))) != NULL) + { + write_syslog = true; } - return succp; + else + { + write_syslog = false; + } + succp = logfile_init(&lm->lm_logfile[lid], + (logfile_id_t)lid, + lm, + store_shmem, + write_syslog); + + if (!succp) { + fprintf(stderr, "*\n* Error : Initializing log files failed.\n"); + break; + } + lid <<= 1; + i += 1; + } + return succp; } static void logfile_flush( - logfile_t* lf) + logfile_t* lf) { - CHK_LOGFILE(lf); - acquire_lock(&lf->lf_spinlock); - lf->lf_flushflag = true; - release_lock(&lf->lf_spinlock); - skygw_message_send(lf->lf_logmes); + CHK_LOGFILE(lf); + acquire_lock(&lf->lf_spinlock); + lf->lf_flushflag = true; + release_lock(&lf->lf_spinlock); + skygw_message_send(lf->lf_logmes); } /** * Set rotate flag for a log file and wake up the writer thread which then * performs the actual rotation task. * - * @param lf logfile pointer + * @param lf logfile pointer */ static void logfile_rotate( - logfile_t* lf) + logfile_t* lf) { - CHK_LOGFILE(lf); - acquire_lock(&lf->lf_spinlock); - lf->lf_rotateflag = true; - release_lock(&lf->lf_spinlock); - skygw_message_send(lf->lf_logmes); + CHK_LOGFILE(lf); + acquire_lock(&lf->lf_spinlock); + lf->lf_rotateflag = true; + release_lock(&lf->lf_spinlock); + skygw_message_send(lf->lf_logmes); } /** * Forms complete path name for logfile and tests that the file doesn't conflict * with any existing file and it is writable. * - * @param lf logfile pointer + * @param lf logfile pointer * * @return true if succeed, false if failed * - * @note Log file openings are not TOCTOU-safe. It is not likely that + * @note Log file openings are not TOCTOU-safe. It is not likely that * multiple copies of same files are opened in parallel but it is possible by * using log manager in parallel with multiple processes and by configuring * log manager to use same directories among those processes. */ static bool logfile_create( - logfile_t* lf) + logfile_t* lf) { - bool namecreatefail; - bool nameconflicts; - bool store_shmem; - bool writable; - bool succp; - strpart_t spart[3]; /*< string parts of which the file is composed of */ + bool namecreatefail; + bool nameconflicts; + bool store_shmem; + bool writable; + bool succp; + strpart_t spart[3]; /*< string parts of which the file is composed of */ - /** - * sparts is an array but next pointers are used to walk through - * the list of string parts. - */ - spart[0].sp_next = &spart[1]; - spart[1].sp_next = &spart[2]; - spart[2].sp_next = NULL; + /** + * sparts is an array but next pointers are used to walk through + * the list of string parts. + */ + spart[0].sp_next = &spart[1]; + spart[1].sp_next = &spart[2]; + spart[2].sp_next = NULL; - spart[1].sp_string = lf->lf_name_prefix; - spart[2].sp_string = lf->lf_name_suffix; + spart[1].sp_string = lf->lf_name_prefix; + spart[2].sp_string = lf->lf_name_suffix; - store_shmem = lf->lf_store_shmem; + store_shmem = lf->lf_store_shmem; - do { - namecreatefail = false; - nameconflicts = false; + do { + namecreatefail = false; + nameconflicts = false; - spart[0].sp_string = lf->lf_filepath; - /** - * Create name for log file. Seqno is added between prefix & - * suffix (index == 2) - */ - lf->lf_full_file_name = - form_full_file_name(spart, lf, 2); + spart[0].sp_string = lf->lf_filepath; + /** + * Create name for log file. Seqno is added between prefix & + * suffix (index == 2) + */ + lf->lf_full_file_name = + form_full_file_name(spart, lf, 2); - if (store_shmem) - { - spart[0].sp_string = lf->lf_linkpath; - /** - * Create name for link file - */ - lf->lf_full_link_name = form_full_file_name(spart,lf,2); - } - /** - * At least one of the files couldn't be created. Increase - * sequence number and retry until succeeds. - */ - if (lf->lf_full_file_name == NULL || - (store_shmem && lf->lf_full_link_name == NULL)) - { - namecreatefail = true; - goto file_create_fail; - } + if (store_shmem) + { + spart[0].sp_string = lf->lf_linkpath; + /** + * Create name for link file + */ + lf->lf_full_link_name = form_full_file_name(spart,lf,2); + } + /** + * At least one of the files couldn't be created. Increase + * sequence number and retry until succeeds. + */ + if (lf->lf_full_file_name == NULL || + (store_shmem && lf->lf_full_link_name == NULL)) + { + namecreatefail = true; + goto file_create_fail; + } - /** - * If file exists but is different type, create fails and - * new, increased sequence number is added to file name. - */ - if (check_file_and_path(lf->lf_full_file_name, &writable, true)) - { - /** Found similarly named file which isn't writable */ - if (!writable || file_is_symlink(lf->lf_full_file_name)) - { - nameconflicts = true; - goto file_create_fail; - } - } - else - { - /** - * Opening the file failed for some other reason than - * existing non-writable file. Shut down. - */ - if (!writable) - { - succp = false; - goto return_succp; - } - } + /** + * If file exists but is different type, create fails and + * new, increased sequence number is added to file name. + */ + if (check_file_and_path(lf->lf_full_file_name, &writable, true)) + { + /** Found similarly named file which isn't writable */ + if (!writable || file_is_symlink(lf->lf_full_file_name)) + { + nameconflicts = true; + goto file_create_fail; + } + } + else + { + /** + * Opening the file failed for some other reason than + * existing non-writable file. Shut down. + */ + if (!writable) + { + succp = false; + goto return_succp; + } + } - if (store_shmem) - { - if (check_file_and_path(lf->lf_full_link_name, &writable, true)) - { - /** Found similarly named link which isn't writable */ - if (!writable) - { - nameconflicts = true; - } - } - else - { - /** - * Opening the file failed for some other reason than - * existing non-writable file. Shut down. - */ - if (!writable) - { - succp = false; - goto return_succp; - } - } - } -file_create_fail: - if (namecreatefail || nameconflicts) - { - lf->lf_name_seqno += 1; + if (store_shmem) + { + if (check_file_and_path(lf->lf_full_link_name, &writable, true)) + { + /** Found similarly named link which isn't writable */ + if (!writable) + { + nameconflicts = true; + } + } + else + { + /** + * Opening the file failed for some other reason than + * existing non-writable file. Shut down. + */ + if (!writable) + { + succp = false; + goto return_succp; + } + } + } + file_create_fail: + if (namecreatefail || nameconflicts) + { + lf->lf_name_seqno += 1; - if (lf->lf_full_file_name != NULL) - { - free(lf->lf_full_file_name); - lf->lf_full_file_name = NULL; - } - if (lf->lf_full_link_name != NULL) - { - free(lf->lf_full_link_name); - lf->lf_full_link_name = NULL; - } - } - } while (namecreatefail || nameconflicts); + if (lf->lf_full_file_name != NULL) + { + free(lf->lf_full_file_name); + lf->lf_full_file_name = NULL; + } + if (lf->lf_full_link_name != NULL) + { + free(lf->lf_full_link_name); + lf->lf_full_link_name = NULL; + } + } + } while (namecreatefail || nameconflicts); - succp = true; + succp = true; return_succp: - return succp; + return succp; } /** @@ -2246,84 +2246,84 @@ return_succp: * and file descriptor are stored to skygw_file_t struct which is stored in * filewriter strcuture passed as parameter. * - * @param fw filewriter pointer - * @param lf logfile pointer + * @param fw filewriter pointer + * @param lf logfile pointer * * @return true if succeed; the resulting skygw_file_t is written in filewriter, * false if failed. * */ static bool logfile_open_file( - filewriter_t* fw, - logfile_t* lf) + filewriter_t* fw, + logfile_t* lf) { - bool succp; - char* start_msg_str; - int err; + bool succp; + char* start_msg_str; + int err; - if(use_stdout) + if(use_stdout) + { + fw->fwr_file[lf->lf_id] = skygw_file_alloc ( + lf->lf_full_file_name); + fw->fwr_file[lf->lf_id]->sf_file = stdout; + } + else if (lf->lf_store_shmem) + { + /** Create symlink pointing to log file */ + fw->fwr_file[lf->lf_id] = skygw_file_init( + lf->lf_full_file_name, + lf->lf_full_link_name); + } + else + { + /** Create normal disk-resident log file */ + fw->fwr_file[lf->lf_id] = skygw_file_init( + lf->lf_full_file_name, + NULL); + } + + if (fw->fwr_file[lf->lf_id] == NULL) + { + fprintf(stderr, + "Error : opening logfile %s failed.\n", + lf->lf_full_file_name); + succp = false; + goto return_succp; + } + + if(use_stdout == 0) + { + if (lf->lf_enabled) { - fw->fwr_file[lf->lf_id] = skygw_file_alloc ( - lf->lf_full_file_name); - fw->fwr_file[lf->lf_id]->sf_file = stdout; + start_msg_str = strdup("---\tLogging is enabled.\n"); } - else if (lf->lf_store_shmem) - { - /** Create symlink pointing to log file */ - fw->fwr_file[lf->lf_id] = skygw_file_init( - lf->lf_full_file_name, - lf->lf_full_link_name); - } - else - { - /** Create normal disk-resident log file */ - fw->fwr_file[lf->lf_id] = skygw_file_init( - lf->lf_full_file_name, - NULL); - } - - if (fw->fwr_file[lf->lf_id] == NULL) - { - fprintf(stderr, - "Error : opening logfile %s failed.\n", - lf->lf_full_file_name); - succp = false; - goto return_succp; - } - - if(use_stdout == 0) + else { - if (lf->lf_enabled) - { - start_msg_str = strdup("---\tLogging is enabled.\n"); - } - else - { - start_msg_str = strdup("---\tLogging is disabled.\n"); - } - err = skygw_file_write(fw->fwr_file[lf->lf_id], - (void *)start_msg_str, - strlen(start_msg_str), - true); - - if (err != 0) - { - char errbuf[STRERROR_BUFLEN]; - fprintf(stderr, - "Error : writing to file %s failed due to %d, %s. " - "Exiting MaxScale.\n", - lf->lf_full_file_name, - err, - strerror_r(err, errbuf, sizeof(errbuf))); - succp = false; - goto return_succp; - } - free(start_msg_str); + start_msg_str = strdup("---\tLogging is disabled.\n"); } - succp = true; + err = skygw_file_write(fw->fwr_file[lf->lf_id], + (void *)start_msg_str, + strlen(start_msg_str), + true); + + if (err != 0) + { + char errbuf[STRERROR_BUFLEN]; + fprintf(stderr, + "Error : writing to file %s failed due to %d, %s. " + "Exiting MaxScale.\n", + lf->lf_full_file_name, + err, + strerror_r(err, errbuf, sizeof(errbuf))); + succp = false; + goto return_succp; + } + free(start_msg_str); + } + succp = true; return_succp: - return succp; + return succp; } @@ -2343,100 +2343,100 @@ return_succp: * */ static char* form_full_file_name( - strpart_t* parts, - logfile_t* lf, - int seqnoidx) + strpart_t* parts, + logfile_t* lf, + int seqnoidx) { - int i; - int seqno; - size_t s; - size_t fnlen; - char* filename = NULL; - char* seqnostr = NULL; - strpart_t* p; + int i; + int seqno; + size_t s; + size_t fnlen; + char* filename = NULL; + char* seqnostr = NULL; + strpart_t* p; - if (lf->lf_name_seqno != -1) + if (lf->lf_name_seqno != -1) + { + int file_sn; + int link_sn = 0; + char* tmp = parts[0].sp_string; + + file_sn = find_last_seqno(parts, lf->lf_name_seqno, seqnoidx); + + if (lf->lf_linkpath != NULL) { - int file_sn; - int link_sn = 0; - char* tmp = parts[0].sp_string; - - file_sn = find_last_seqno(parts, lf->lf_name_seqno, seqnoidx); - - if (lf->lf_linkpath != NULL) - { - tmp = parts[0].sp_string; - parts[0].sp_string = lf->lf_linkpath; - link_sn = find_last_seqno(parts, lf->lf_name_seqno, seqnoidx); - parts[0].sp_string = tmp; - } - lf->lf_name_seqno = MAX(file_sn, link_sn); - - seqno = lf->lf_name_seqno; - s = UINTLEN(seqno); - seqnostr = (char *)malloc((int)s+1); - } - else - { - /** - * These magic numbers are needed to indicate this and - * in subroutines that sequence number is not used. - */ - s = 0; - seqnoidx = -1; - seqno = lf->lf_name_seqno; + tmp = parts[0].sp_string; + parts[0].sp_string = lf->lf_linkpath; + link_sn = find_last_seqno(parts, lf->lf_name_seqno, seqnoidx); + parts[0].sp_string = tmp; } + lf->lf_name_seqno = MAX(file_sn, link_sn); - if (parts == NULL || parts->sp_string == NULL) { - goto return_filename; - } + seqno = lf->lf_name_seqno; + s = UINTLEN(seqno); + seqnostr = (char *)malloc((int)s+1); + } + else + { /** - * Length of path, file name, separating slash, sequence number and - * terminating character. + * These magic numbers are needed to indicate this and + * in subroutines that sequence number is not used. */ - fnlen = sizeof('/') + s + sizeof('\0'); - p = parts; - /** Calculate the combined length of all parts put together */ - while (p->sp_string != NULL) + s = 0; + seqnoidx = -1; + seqno = lf->lf_name_seqno; + } + + if (parts == NULL || parts->sp_string == NULL) { + goto return_filename; + } + /** + * Length of path, file name, separating slash, sequence number and + * terminating character. + */ + fnlen = sizeof('/') + s + sizeof('\0'); + p = parts; + /** Calculate the combined length of all parts put together */ + while (p->sp_string != NULL) + { + fnlen += strnlen(p->sp_string, NAME_MAX); + + if (p->sp_next == NULL) { - fnlen += strnlen(p->sp_string, NAME_MAX); - - if (p->sp_next == NULL) - { - break; - } - p = p->sp_next; + break; } + p = p->sp_next; + } - if (fnlen > NAME_MAX) - { - fprintf(stderr, "Error : Too long file name= %d.\n", (int)fnlen); - goto return_filename; - } - filename = (char*)calloc(1, fnlen); + if (fnlen > NAME_MAX) + { + fprintf(stderr, "Error : Too long file name= %d.\n", (int)fnlen); + goto return_filename; + } + filename = (char*)calloc(1, fnlen); - if (seqnostr != NULL) - { - snprintf(seqnostr, s+1, "%d", seqno); - } + if (seqnostr != NULL) + { + snprintf(seqnostr, s+1, "%d", seqno); + } - for (i=0, p=parts; p->sp_string != NULL; i++, p=p->sp_next) + for (i=0, p=parts; p->sp_string != NULL; i++, p=p->sp_next) + { + if (seqnostr != NULL && i == seqnoidx) { - if (seqnostr != NULL && i == seqnoidx) - { - strcat(filename, seqnostr); /*< add sequence number */ - } - strcat(filename, p->sp_string); - - if (p->sp_next == NULL) - { - break; - } + strcat(filename, seqnostr); /*< add sequence number */ } + strcat(filename, p->sp_string); + + if (p->sp_next == NULL) + { + break; + } + } return_filename: - if (seqnostr != NULL) free(seqnostr); - return filename; + if (seqnostr != NULL) free(seqnostr); + return filename; } /** @@ -2455,19 +2455,19 @@ return_filename: * */ static char* add_slash( - char* str) + char* str) { - char* p = str; - size_t plen = strlen(p); + char* p = str; + size_t plen = strlen(p); - /** Add slash if missing */ - if (p[plen-1] != '/') - { - str = (char *)malloc(plen+2); - snprintf(str, plen+2, "%s/", p); - free(p); - } - return str; + /** Add slash if missing */ + if (p[plen-1] != '/') + { + str = (char *)malloc(plen+2); + snprintf(str, plen+2, "%s/", p); + free(p); + } + return str; } @@ -2476,110 +2476,110 @@ static char* add_slash( * check if they are accessible and writable. * * Parameters: - * @param filename file to be checked + * @param filename file to be checked * - * @param writable flag indicating whether file was found writable or not + * @param writable flag indicating whether file was found writable or not * if writable is NULL, check is skipped. * * @return true & writable if file exists and it is writable, - * true & not writable if file exists but it can't be written, - * false & writable if file doesn't exist but directory could be written, and - * false & not writable if directory can't be written. + * true & not writable if file exists but it can't be written, + * false & writable if file doesn't exist but directory could be written, and + * false & not writable if directory can't be written. * * @details Note, that a space character is written to the end of file. * TODO: recall what was the reason for not succeeding with simply * calling access, and fstat. vraa 26.11.13 */ static bool check_file_and_path( - char* filename, - bool* writable, - bool do_log) + char* filename, + bool* writable, + bool do_log) { - bool exists; + bool exists; - if (filename == NULL) - { - exists = false; + if (filename == NULL) + { + exists = false; - if (writable) - { - *writable = false; - } - } - else - { - if(access(filename,F_OK) == 0) - { + if (writable) + { + *writable = false; + } + } + else + { + if(access(filename,F_OK) == 0) + { - exists = true; + exists = true; - if(access(filename,W_OK) == 0) - { - if(writable) - { - *writable = true; - } - } - else - { - - if (do_log && file_is_symlink(filename)) - { - char errbuf[STRERROR_BUFLEN]; - fprintf(stderr, - "*\n* Error : Can't access " - "file pointed to by %s due " - "to %s.\n", - filename, - strerror_r(errno, errbuf, sizeof(errbuf))); - } - else if (do_log) - { - char errbuf[STRERROR_BUFLEN]; - fprintf(stderr, - "*\n* Error : Can't access %s due " - "to %s.\n", - filename, - strerror_r(errno, errbuf, sizeof(errbuf))); - } - - if(writable) - { - *writable = false; - } - } - - } - else - { - exists = false; + if(access(filename,W_OK) == 0) + { if(writable) - { + { *writable = true; - } - } - } - return exists; + } + } + else + { + + if (do_log && file_is_symlink(filename)) + { + char errbuf[STRERROR_BUFLEN]; + fprintf(stderr, + "*\n* Error : Can't access " + "file pointed to by %s due " + "to %s.\n", + filename, + strerror_r(errno, errbuf, sizeof(errbuf))); + } + else if (do_log) + { + char errbuf[STRERROR_BUFLEN]; + fprintf(stderr, + "*\n* Error : Can't access %s due " + "to %s.\n", + filename, + strerror_r(errno, errbuf, sizeof(errbuf))); + } + + if(writable) + { + *writable = false; + } + } + + } + else + { + exists = false; + if(writable) + { + *writable = true; + } + } + } + return exists; } static bool file_is_symlink( - char* filename) + char* filename) { - int rc; - bool succp = false; - struct stat b; + int rc; + bool succp = false; + struct stat b; - if (filename != NULL) - { - rc = lstat(filename, &b); + if (filename != NULL) + { + rc = lstat(filename, &b); - if (rc != -1 && S_ISLNK(b.st_mode)) { - succp = true; - } + if (rc != -1 && S_ISLNK(b.st_mode)) { + succp = true; } - return succp; + } + return succp; } @@ -2589,127 +2589,127 @@ static bool file_is_symlink( * link name. Create block buffer for logfile. * * Parameters: - * @param logfile log file - * @param logfile_id identifier for log file - * @param logmanager log manager pointer - * @param store_shmem flag to indicate whether log is physically written to shmem - * @param write_syslog flag to indicate whether log is also written to syslog + * @param logfile log file + * @param logfile_id identifier for log file + * @param logmanager log manager pointer + * @param store_shmem flag to indicate whether log is physically written to shmem + * @param write_syslog flag to indicate whether log is also written to syslog * * @return true if succeed, false otherwise */ static bool logfile_init( - logfile_t* logfile, - logfile_id_t logfile_id, - logmanager_t* logmanager, - bool store_shmem, - bool write_syslog) + logfile_t* logfile, + logfile_id_t logfile_id, + logmanager_t* logmanager, + bool store_shmem, + bool write_syslog) { - bool succp = false; - fnames_conf_t* fn = &logmanager->lm_fnames_conf; - logfile->lf_state = INIT; + bool succp = false; + fnames_conf_t* fn = &logmanager->lm_fnames_conf; + logfile->lf_state = INIT; #if defined(SS_DEBUG) - logfile->lf_chk_top = CHK_NUM_LOGFILE; - logfile->lf_chk_tail = CHK_NUM_LOGFILE; + logfile->lf_chk_top = CHK_NUM_LOGFILE; + logfile->lf_chk_tail = CHK_NUM_LOGFILE; #endif - logfile->lf_logmes = logmanager->lm_logmes; - logfile->lf_id = logfile_id; - logfile->lf_name_prefix = fname_conf_get_prefix(fn, logfile_id); - logfile->lf_name_suffix = fname_conf_get_suffix(fn, logfile_id); - logfile->lf_npending_writes = 0; - logfile->lf_name_seqno = 1; - logfile->lf_lmgr = logmanager; - logfile->lf_flushflag = false; - logfile->lf_rotateflag= false; - logfile->lf_spinlock = 0; - logfile->lf_store_shmem = store_shmem; - logfile->lf_write_syslog = write_syslog; - logfile->lf_buf_size = MAX_LOGSTRLEN; - logfile->lf_enabled = logmanager->lm_enabled_logfiles & logfile_id; - /** - * If file is stored in shared memory in /dev/shm, a link - * pointing to shm file is created and located to the file - * directory. - */ - if (store_shmem) - { - char* c; - pid_t pid = getpid(); - int len = strlen(shm_pathname_prefix)+ - + strlen("maxscale.") + - get_decimal_len((size_t)pid) + 1; + logfile->lf_logmes = logmanager->lm_logmes; + logfile->lf_id = logfile_id; + logfile->lf_name_prefix = fname_conf_get_prefix(fn, logfile_id); + logfile->lf_name_suffix = fname_conf_get_suffix(fn, logfile_id); + logfile->lf_npending_writes = 0; + logfile->lf_name_seqno = 1; + logfile->lf_lmgr = logmanager; + logfile->lf_flushflag = false; + logfile->lf_rotateflag= false; + logfile->lf_spinlock = 0; + logfile->lf_store_shmem = store_shmem; + logfile->lf_write_syslog = write_syslog; + logfile->lf_buf_size = MAX_LOGSTRLEN; + logfile->lf_enabled = logmanager->lm_enabled_logfiles & logfile_id; + /** + * If file is stored in shared memory in /dev/shm, a link + * pointing to shm file is created and located to the file + * directory. + */ + if (store_shmem) + { + char* c; + pid_t pid = getpid(); + int len = strlen(shm_pathname_prefix)+ + + strlen("maxscale.") + + get_decimal_len((size_t)pid) + 1; - c = (char *)calloc(len, sizeof(char)); + c = (char *)calloc(len, sizeof(char)); - if (c == NULL) - { - succp = false; - goto return_with_succp; - } - sprintf(c, "%smaxscale.%d", shm_pathname_prefix, pid); - logfile->lf_filepath = c; - - if (mkdir(c, S_IRWXU | S_IRWXG) != 0 && - errno != EEXIST) - { - succp = false; - goto return_with_succp; - } - logfile->lf_linkpath = strdup(fn->fn_logpath); - logfile->lf_linkpath = add_slash(logfile->lf_linkpath); - } - else - { - logfile->lf_filepath = strdup(fn->fn_logpath); - } - logfile->lf_filepath = add_slash(logfile->lf_filepath); - - if (!(succp = logfile_create(logfile))) - { - goto return_with_succp; - } - /** - * Create a block buffer list for log file. Clients' writes go to buffers - * from where separate log flusher thread writes them to disk. - */ - if (mlist_init(&logfile->lf_blockbuf_list, - NULL, - strdup("logfile block buffer list"), - blockbuf_node_done, - MAXNBLOCKBUFS) == NULL) + if (c == NULL) { - ss_dfprintf(stderr, - "*\n* Error : Initializing buffers for log files " - "failed."); - logfile_free_memory(logfile); - goto return_with_succp; + succp = false; + goto return_with_succp; } + sprintf(c, "%smaxscale.%d", shm_pathname_prefix, pid); + logfile->lf_filepath = c; + + if (mkdir(c, S_IRWXU | S_IRWXG) != 0 && + errno != EEXIST) + { + succp = false; + goto return_with_succp; + } + logfile->lf_linkpath = strdup(fn->fn_logpath); + logfile->lf_linkpath = add_slash(logfile->lf_linkpath); + } + else + { + logfile->lf_filepath = strdup(fn->fn_logpath); + } + logfile->lf_filepath = add_slash(logfile->lf_filepath); + + if (!(succp = logfile_create(logfile))) + { + goto return_with_succp; + } + /** + * Create a block buffer list for log file. Clients' writes go to buffers + * from where separate log flusher thread writes them to disk. + */ + if (mlist_init(&logfile->lf_blockbuf_list, + NULL, + strdup("logfile block buffer list"), + blockbuf_node_done, + MAXNBLOCKBUFS) == NULL) + { + ss_dfprintf(stderr, + "*\n* Error : Initializing buffers for log files " + "failed."); + logfile_free_memory(logfile); + goto return_with_succp; + } #if defined(SS_DEBUG) - if (store_shmem && !use_stdout) - { - fprintf(stderr, "%s\t: %s->%s\n", - STRLOGNAME(logfile_id), - logfile->lf_full_link_name, - logfile->lf_full_file_name); - } - else if(!use_stdout) - { - fprintf(stderr, "%s\t: %s\n", - STRLOGNAME(logfile_id), - logfile->lf_full_file_name); - } + if (store_shmem && !use_stdout) + { + fprintf(stderr, "%s\t: %s->%s\n", + STRLOGNAME(logfile_id), + logfile->lf_full_link_name, + logfile->lf_full_file_name); + } + else if(!use_stdout) + { + fprintf(stderr, "%s\t: %s\n", + STRLOGNAME(logfile_id), + logfile->lf_full_file_name); + } #endif - succp = true; - logfile->lf_state = RUN; - CHK_LOGFILE(logfile); + succp = true; + logfile->lf_state = RUN; + CHK_LOGFILE(logfile); return_with_succp: - if (!succp) - { - logfile_done(logfile); - } - ss_dassert(logfile->lf_state == RUN || logfile->lf_state == DONE); - return succp; + if (!succp) + { + logfile_done(logfile); + } + ss_dassert(logfile->lf_state == RUN || logfile->lf_state == DONE); + return succp; } /** @@ -2732,131 +2732,131 @@ return_with_succp: * */ static void logfile_done( - logfile_t* lf) + logfile_t* lf) { - switch(lf->lf_state) { - case RUN: - CHK_LOGFILE(lf); - ss_dassert(lf->lf_npending_writes == 0); - /** fallthrough */ - case INIT: - /** Test if list is initialized before freeing it */ - if (lf->lf_blockbuf_list.mlist_versno != 0) - { - mlist_done(&lf->lf_blockbuf_list); - } - logfile_free_memory(lf); - lf->lf_state = DONE; - /** fallthrough */ - case DONE: - case UNINIT: - default: - break; + switch(lf->lf_state) { + case RUN: + CHK_LOGFILE(lf); + ss_dassert(lf->lf_npending_writes == 0); + /** fallthrough */ + case INIT: + /** Test if list is initialized before freeing it */ + if (lf->lf_blockbuf_list.mlist_versno != 0) + { + mlist_done(&lf->lf_blockbuf_list); } + logfile_free_memory(lf); + lf->lf_state = DONE; + /** fallthrough */ + case DONE: + case UNINIT: + default: + break; + } } static void logfile_free_memory( - logfile_t* lf) + logfile_t* lf) { - if (lf->lf_filepath != NULL) free(lf->lf_filepath); - if (lf->lf_linkpath != NULL) free(lf->lf_linkpath); - if (lf->lf_name_prefix != NULL) free(lf->lf_name_prefix); - if (lf->lf_name_suffix != NULL) free(lf->lf_name_suffix); - if (lf->lf_full_link_name != NULL) free(lf->lf_full_link_name); - if (lf->lf_full_file_name != NULL) free(lf->lf_full_file_name); + if (lf->lf_filepath != NULL) free(lf->lf_filepath); + if (lf->lf_linkpath != NULL) free(lf->lf_linkpath); + if (lf->lf_name_prefix != NULL) free(lf->lf_name_prefix); + if (lf->lf_name_suffix != NULL) free(lf->lf_name_suffix); + if (lf->lf_full_link_name != NULL) free(lf->lf_full_link_name); + if (lf->lf_full_file_name != NULL) free(lf->lf_full_file_name); } /** * @node Initialize filewriter data and open the log file for each log file type. * - * @param logmanager Log manager struct - * @param fw File writer struct - * @param clientmes Messaging from file writer to log manager - * @param logmes Messaging from loggers to file writer thread + * @param logmanager Log manager struct + * @param fw File writer struct + * @param clientmes Messaging from file writer to log manager + * @param logmes Messaging from loggers to file writer thread * * @return true if succeed, false if failed * */ static bool filewriter_init( - logmanager_t* logmanager, - filewriter_t* fw, - skygw_message_t* clientmes, - skygw_message_t* logmes) + logmanager_t* logmanager, + filewriter_t* fw, + skygw_message_t* clientmes, + skygw_message_t* logmes) { - bool succp = false; - logfile_t* lf; - logfile_id_t id; - int i; + bool succp = false; + logfile_t* lf; + logfile_id_t id; + int i; - CHK_LOGMANAGER(logmanager); + CHK_LOGMANAGER(logmanager); - fw->fwr_state = INIT; + fw->fwr_state = INIT; #if defined(SS_DEBUG) - fw->fwr_chk_top = CHK_NUM_FILEWRITER; - fw->fwr_chk_tail = CHK_NUM_FILEWRITER; + fw->fwr_chk_top = CHK_NUM_FILEWRITER; + fw->fwr_chk_tail = CHK_NUM_FILEWRITER; #endif - fw->fwr_logmgr = logmanager; - /** Message from filewriter to clients */ - fw->fwr_logmes = logmes; - /** Message from clients to filewriter */ - fw->fwr_clientmes = clientmes; + fw->fwr_logmgr = logmanager; + /** Message from filewriter to clients */ + fw->fwr_logmes = logmes; + /** Message from clients to filewriter */ + fw->fwr_clientmes = clientmes; - if (fw->fwr_logmes == NULL || fw->fwr_clientmes == NULL) { - goto return_succp; + if (fw->fwr_logmes == NULL || fw->fwr_clientmes == NULL) { + goto return_succp; + } + + for (i=LOGFILE_FIRST; i<=LOGFILE_LAST; i <<= 1) + { + id = (logfile_id_t)i; + lf = logmanager_get_logfile(logmanager, id); + + if (!(succp = logfile_open_file(fw, lf))) + { + fprintf(stderr, + "Error : opening log file %s failed. Exiting " + "MaxScale\n", + lf->lf_full_file_name); + goto return_succp; } - - for (i=LOGFILE_FIRST; i<=LOGFILE_LAST; i <<= 1) - { - id = (logfile_id_t)i; - lf = logmanager_get_logfile(logmanager, id); - - if (!(succp = logfile_open_file(fw, lf))) - { - fprintf(stderr, - "Error : opening log file %s failed. Exiting " - "MaxScale\n", - lf->lf_full_file_name); - goto return_succp; - } - } /*< for */ - fw->fwr_state = RUN; - CHK_FILEWRITER(fw); - succp = true; + } /*< for */ + fw->fwr_state = RUN; + CHK_FILEWRITER(fw); + succp = true; return_succp: - if (!succp) { - filewriter_done(fw); - } - ss_dassert(fw->fwr_state == RUN || fw->fwr_state == DONE); - return succp; + if (!succp) { + filewriter_done(fw); + } + ss_dassert(fw->fwr_state == RUN || fw->fwr_state == DONE); + return succp; } static void filewriter_done( filewriter_t* fw) { - int i; - logfile_id_t id; + int i; + logfile_id_t id; - switch(fw->fwr_state) { - case RUN: - CHK_FILEWRITER(fw); - case INIT: - fw->fwr_logmes = NULL; - fw->fwr_clientmes = NULL; - for (i=LOGFILE_FIRST; i<=LOGFILE_LAST; i++) - { - id = (logfile_id_t)i; - if(use_stdout) - skygw_file_free(fw->fwr_file[id]); - else - skygw_file_close(fw->fwr_file[id], true); - } - fw->fwr_state = DONE; - case DONE: - case UNINIT: - default: - break; + switch(fw->fwr_state) { + case RUN: + CHK_FILEWRITER(fw); + case INIT: + fw->fwr_logmes = NULL; + fw->fwr_clientmes = NULL; + for (i=LOGFILE_FIRST; i<=LOGFILE_LAST; i++) + { + id = (logfile_id_t)i; + if(use_stdout) + skygw_file_free(fw->fwr_file[id]); + else + skygw_file_close(fw->fwr_file[id], true); } + fw->fwr_state = DONE; + case DONE: + case UNINIT: + default: + break; + } } @@ -2904,335 +2904,335 @@ static void filewriter_done( * during the read. */ static void* thr_filewriter_fun( - void* data) + void* data) { - skygw_thread_t* thr; - filewriter_t* fwr; - skygw_file_t* file; - logfile_t* lf; + skygw_thread_t* thr; + filewriter_t* fwr; + skygw_file_t* file; + logfile_t* lf; - mlist_t* bb_list; - blockbuf_t* bb; - mlist_node_t* node; - int i; - blockbuf_state_t flush_blockbuf; /**< flush single block buffer. */ - bool flush_logfile; /**< flush logfile */ - bool do_flushall = false; - bool rotate_logfile; /*< close current and open new file */ - size_t vn1; - size_t vn2; + mlist_t* bb_list; + blockbuf_t* bb; + mlist_node_t* node; + int i; + blockbuf_state_t flush_blockbuf; /**< flush single block buffer. */ + bool flush_logfile; /**< flush logfile */ + bool do_flushall = false; + bool rotate_logfile; /*< close current and open new file */ + size_t vn1; + size_t vn2; - thr = (skygw_thread_t *)data; - fwr = (filewriter_t *)skygw_thread_get_data(thr); - flushall_logfiles(false); + thr = (skygw_thread_t *)data; + fwr = (filewriter_t *)skygw_thread_get_data(thr); + flushall_logfiles(false); - CHK_FILEWRITER(fwr); - ss_debug(skygw_thread_set_state(thr, THR_RUNNING)); + CHK_FILEWRITER(fwr); + ss_debug(skygw_thread_set_state(thr, THR_RUNNING)); - /** Inform log manager about the state. */ - skygw_message_send(fwr->fwr_clientmes); + /** Inform log manager about the state. */ + skygw_message_send(fwr->fwr_clientmes); - while(!skygw_thread_must_exit(thr)) { - /** - * Wait until new log arrival message appears. - * Reset message to avoid redundant calls. - */ - skygw_message_wait(fwr->fwr_logmes); - if(skygw_thread_must_exit(thr)){ - flushall_logfiles(true); - } + while(!skygw_thread_must_exit(thr)) { + /** + * Wait until new log arrival message appears. + * Reset message to avoid redundant calls. + */ + skygw_message_wait(fwr->fwr_logmes); + if(skygw_thread_must_exit(thr)){ + flushall_logfiles(true); + } - /** Process all logfiles which have buffered writes. */ - for (i=LOGFILE_FIRST; i<=LOGFILE_LAST; i <<= 1) - { - retry_flush_on_exit: - /** - * Get file pointer of current logfile. - */ + /** Process all logfiles which have buffered writes. */ + for (i=LOGFILE_FIRST; i<=LOGFILE_LAST; i <<= 1) + { + retry_flush_on_exit: + /** + * Get file pointer of current logfile. + */ - do_flushall = thr_flushall_check(); - file = fwr->fwr_file[i]; - lf = &lm->lm_logfile[(logfile_id_t)i]; + do_flushall = thr_flushall_check(); + file = fwr->fwr_file[i]; + lf = &lm->lm_logfile[(logfile_id_t)i]; - /** - * read and reset logfile's flush- and rotateflag - */ - acquire_lock(&lf->lf_spinlock); - flush_logfile = lf->lf_flushflag; - rotate_logfile = lf->lf_rotateflag; - lf->lf_flushflag = false; - lf->lf_rotateflag = false; - release_lock(&lf->lf_spinlock); - /** - * Log rotation : - * Close current, and open a new file for the log. - */ - if (rotate_logfile) - { - bool succp; + /** + * read and reset logfile's flush- and rotateflag + */ + acquire_lock(&lf->lf_spinlock); + flush_logfile = lf->lf_flushflag; + rotate_logfile = lf->lf_rotateflag; + lf->lf_flushflag = false; + lf->lf_rotateflag = false; + release_lock(&lf->lf_spinlock); + /** + * Log rotation : + * Close current, and open a new file for the log. + */ + if (rotate_logfile) + { + bool succp; - lf->lf_name_seqno += 1; /*< new sequence number */ + lf->lf_name_seqno += 1; /*< new sequence number */ - if (!(succp = logfile_create(lf))) - { - lf->lf_name_seqno -= 1; /*< restore */ - } - else if ((succp = logfile_open_file(fwr, lf))) - { - if(use_stdout) - skygw_file_free (file); - else - skygw_file_close(file, false); /*< close old file */ - } + if (!(succp = logfile_create(lf))) + { + lf->lf_name_seqno -= 1; /*< restore */ + } + else if ((succp = logfile_open_file(fwr, lf))) + { + if(use_stdout) + skygw_file_free (file); + else + skygw_file_close(file, false); /*< close old file */ + } - if (!succp) - { - LOGIF(LE, (skygw_log_write( - LOGFILE_ERROR, - "Error : Log rotation failed. " - "Creating replacement file %s " - "failed. Continuing " - "logging to existing file.", - lf->lf_full_file_name))); - } - continue; - } - /** - * get logfile's block buffer list - */ - bb_list = &lf->lf_blockbuf_list; + if (!succp) + { + LOGIF(LE, (skygw_log_write( + LOGFILE_ERROR, + "Error : Log rotation failed. " + "Creating replacement file %s " + "failed. Continuing " + "logging to existing file.", + lf->lf_full_file_name))); + } + continue; + } + /** + * get logfile's block buffer list + */ + bb_list = &lf->lf_blockbuf_list; #if defined(SS_DEBUG) - simple_mutex_lock(&bb_list->mlist_mutex, true); - CHK_MLIST(bb_list); - simple_mutex_unlock(&bb_list->mlist_mutex); + simple_mutex_lock(&bb_list->mlist_mutex, true); + CHK_MLIST(bb_list); + simple_mutex_unlock(&bb_list->mlist_mutex); #endif - node = bb_list->mlist_first; + node = bb_list->mlist_first; - while (node != NULL) - { - int err = 0; + while (node != NULL) + { + int err = 0; - CHK_MLIST_NODE(node); - bb = (blockbuf_t *)node->mlnode_data; - CHK_BLOCKBUF(bb); + CHK_MLIST_NODE(node); + bb = (blockbuf_t *)node->mlnode_data; + CHK_BLOCKBUF(bb); - /** Lock block buffer */ - simple_mutex_lock(&bb->bb_mutex, true); + /** Lock block buffer */ + simple_mutex_lock(&bb->bb_mutex, true); - flush_blockbuf = bb->bb_state; + flush_blockbuf = bb->bb_state; - if (bb->bb_buf_used != 0 && - (flush_blockbuf == BB_FULL || - flush_logfile || - do_flushall)) - { - /** - * buffer is at least half-full - * -> write to disk - */ - while(bb->bb_refcount > 0) - { - simple_mutex_unlock( - &bb->bb_mutex); - simple_mutex_lock( - &bb->bb_mutex, - true); - } - err = skygw_file_write( - file, - (void *)bb->bb_buf, - bb->bb_buf_used, - (flush_logfile || - do_flushall)); - if (err) - { - char errbuf[STRERROR_BUFLEN]; - fprintf(stderr, - "Error : Write to %s log " - ": %s failed due to %d, " - "%s. Disabling the log.", - STRLOGNAME((logfile_id_t)i), - lf->lf_full_file_name, - err, - strerror_r(err, errbuf, sizeof(errbuf))); - /** Force log off */ - skygw_log_disable_raw((logfile_id_t)i, true); - } - /** - * Reset buffer's counters and mark - * not full. - */ - bb->bb_buf_left = bb->bb_buf_size; - bb->bb_buf_used = 0; - memset(bb->bb_buf, 0, bb->bb_buf_size); - bb->bb_state = BB_CLEARED; + if (bb->bb_buf_used != 0 && + (flush_blockbuf == BB_FULL || + flush_logfile || + do_flushall)) + { + /** + * buffer is at least half-full + * -> write to disk + */ + while(bb->bb_refcount > 0) + { + simple_mutex_unlock( + &bb->bb_mutex); + simple_mutex_lock( + &bb->bb_mutex, + true); + } + err = skygw_file_write( + file, + (void *)bb->bb_buf, + bb->bb_buf_used, + (flush_logfile || + do_flushall)); + if (err) + { + char errbuf[STRERROR_BUFLEN]; + fprintf(stderr, + "Error : Write to %s log " + ": %s failed due to %d, " + "%s. Disabling the log.", + STRLOGNAME((logfile_id_t)i), + lf->lf_full_file_name, + err, + strerror_r(err, errbuf, sizeof(errbuf))); + /** Force log off */ + skygw_log_disable_raw((logfile_id_t)i, true); + } + /** + * Reset buffer's counters and mark + * not full. + */ + bb->bb_buf_left = bb->bb_buf_size; + bb->bb_buf_used = 0; + memset(bb->bb_buf, 0, bb->bb_buf_size); + bb->bb_state = BB_CLEARED; #if defined(SS_LOG_DEBUG) - sprintf(bb->bb_buf,"[block:%d]",atomic_add(&block_start_index,1)); - bb->bb_buf_used += strlen(bb->bb_buf); - bb->bb_buf_left -= strlen(bb->bb_buf); + sprintf(bb->bb_buf,"[block:%d]",atomic_add(&block_start_index,1)); + bb->bb_buf_used += strlen(bb->bb_buf); + bb->bb_buf_left -= strlen(bb->bb_buf); #endif - } - /** Release lock to block buffer */ - simple_mutex_unlock(&bb->bb_mutex); + } + /** Release lock to block buffer */ + simple_mutex_unlock(&bb->bb_mutex); - /** Consistent lock-free read on the list */ - do { - while ((vn1 = bb_list->mlist_versno)%2 - != 0); - node = node->mlnode_next; - vn2 = bb_list->mlist_versno; - } while (vn1 != vn2 && node); + /** Consistent lock-free read on the list */ + do { + while ((vn1 = bb_list->mlist_versno)%2 + != 0); + node = node->mlnode_next; + vn2 = bb_list->mlist_versno; + } while (vn1 != vn2 && node); - } /* while (node != NULL) */ + } /* while (node != NULL) */ - /** - * Writer's exit flag was set after checking it. - * Loop is restarted to ensure that all logfiles are - * flushed. - */ + /** + * Writer's exit flag was set after checking it. + * Loop is restarted to ensure that all logfiles are + * flushed. + */ - if(flushall_started_flag){ - flushall_started_flag = false; - flushall_done_flag = true; - i = LOGFILE_FIRST; - goto retry_flush_on_exit; - } + if(flushall_started_flag){ + flushall_started_flag = false; + flushall_done_flag = true; + i = LOGFILE_FIRST; + goto retry_flush_on_exit; + } - if (!thr_flushall_check() && skygw_thread_must_exit(thr)) - { - flushall_logfiles(true); - i = LOGFILE_FIRST; - goto retry_flush_on_exit; - } - }/* for */ + if (!thr_flushall_check() && skygw_thread_must_exit(thr)) + { + flushall_logfiles(true); + i = LOGFILE_FIRST; + goto retry_flush_on_exit; + } + }/* for */ - if(flushall_done_flag){ - flushall_done_flag = false; - flushall_logfiles(false); - skygw_message_send(fwr->fwr_clientmes); - } + if(flushall_done_flag){ + flushall_done_flag = false; + flushall_logfiles(false); + skygw_message_send(fwr->fwr_clientmes); + } - } /* while (!skygw_thread_must_exit) */ + } /* while (!skygw_thread_must_exit) */ - ss_debug(skygw_thread_set_state(thr, THR_STOPPED)); - /** Inform log manager that file writer thread has stopped. */ - skygw_message_send(fwr->fwr_clientmes); - return NULL; + ss_debug(skygw_thread_set_state(thr, THR_STOPPED)); + /** Inform log manager that file writer thread has stopped. */ + skygw_message_send(fwr->fwr_clientmes); + return NULL; } static void fnames_conf_done( - fnames_conf_t* fn) + fnames_conf_t* fn) { - switch (fn->fn_state) { - case RUN: - CHK_FNAMES_CONF(fn); - case INIT: - fnames_conf_free_memory(fn); - fn->fn_state = DONE; - case DONE: - case UNINIT: - default: - break; - } + switch (fn->fn_state) { + case RUN: + CHK_FNAMES_CONF(fn); + case INIT: + fnames_conf_free_memory(fn); + fn->fn_state = DONE; + case DONE: + case UNINIT: + default: + break; + } } static void fnames_conf_free_memory( - fnames_conf_t* fn) + fnames_conf_t* fn) { - if (fn->fn_debug_prefix != NULL) free(fn->fn_debug_prefix); - if (fn->fn_debug_suffix!= NULL) free(fn->fn_debug_suffix); - if (fn->fn_trace_prefix != NULL) free(fn->fn_trace_prefix); - if (fn->fn_trace_suffix!= NULL) free(fn->fn_trace_suffix); - if (fn->fn_msg_prefix != NULL) free(fn->fn_msg_prefix); - if (fn->fn_msg_suffix != NULL) free(fn->fn_msg_suffix); - if (fn->fn_err_prefix != NULL) free(fn->fn_err_prefix); - if (fn->fn_err_suffix != NULL) free(fn->fn_err_suffix); - if (fn->fn_logpath != NULL) free(fn->fn_logpath); + if (fn->fn_debug_prefix != NULL) free(fn->fn_debug_prefix); + if (fn->fn_debug_suffix!= NULL) free(fn->fn_debug_suffix); + if (fn->fn_trace_prefix != NULL) free(fn->fn_trace_prefix); + if (fn->fn_trace_suffix!= NULL) free(fn->fn_trace_suffix); + if (fn->fn_msg_prefix != NULL) free(fn->fn_msg_prefix); + if (fn->fn_msg_suffix != NULL) free(fn->fn_msg_suffix); + if (fn->fn_err_prefix != NULL) free(fn->fn_err_prefix); + if (fn->fn_err_suffix != NULL) free(fn->fn_err_suffix); + if (fn->fn_logpath != NULL) free(fn->fn_logpath); } /** * Find the file with biggest sequence number from given directory and return * the sequence number. * - * @param parts string parts of which the file name is composed of - * @param seqno the sequence number to start with, if seqno is -1 just return + * @param parts string parts of which the file name is composed of + * @param seqno the sequence number to start with, if seqno is -1 just return * * @return the biggest sequence number used */ static int find_last_seqno( - strpart_t* parts, - int seqno, - int seqnoidx) + strpart_t* parts, + int seqno, + int seqnoidx) { - strpart_t* p; - char* snstr; - int snstrlen; + strpart_t* p; + char* snstr; + int snstrlen; - if (seqno == -1) - { - return seqno; - } - snstrlen = UINTLEN(INT_MAX); - snstr = (char *)calloc(1, snstrlen); - p = parts; + if (seqno == -1) + { + return seqno; + } + snstrlen = UINTLEN(INT_MAX); + snstr = (char *)calloc(1, snstrlen); + p = parts; - while (true) - { - int i; - char filename[NAME_MAX] = {0}; - /** Form name with next seqno */ - snprintf(snstr, snstrlen, "%d", seqno+1); + while (true) + { + int i; + char filename[NAME_MAX] = {0}; + /** Form name with next seqno */ + snprintf(snstr, snstrlen, "%d", seqno+1); - for (i=0, p=parts; p->sp_string != NULL; i++, p=p->sp_next) - { - if (snstr != NULL && i == seqnoidx) - { - strncat(filename, snstr, NAME_MAX - 1); /*< add sequence number */ - } - strncat(filename, p->sp_string, NAME_MAX - 1); + for (i=0, p=parts; p->sp_string != NULL; i++, p=p->sp_next) + { + if (snstr != NULL && i == seqnoidx) + { + strncat(filename, snstr, NAME_MAX - 1); /*< add sequence number */ + } + strncat(filename, p->sp_string, NAME_MAX - 1); - if (p->sp_next == NULL) - { - break; - } - } + if (p->sp_next == NULL) + { + break; + } + } - if (check_file_and_path(filename, NULL, false)) - { - seqno++; - } - else - { - break; - } - } - free(snstr); + if (check_file_and_path(filename, NULL, false)) + { + seqno++; + } + else + { + break; + } + } + free(snstr); - return seqno; + return seqno; } bool thr_flushall_check() { - bool rval = false; - simple_mutex_lock(&lm->lm_mutex,true); - rval = flushall_flag; - if(rval && !flushall_started_flag && !flushall_done_flag){ - flushall_started_flag = true; - } - simple_mutex_unlock(&lm->lm_mutex); - return rval; + bool rval = false; + simple_mutex_lock(&lm->lm_mutex,true); + rval = flushall_flag; + if(rval && !flushall_started_flag && !flushall_done_flag){ + flushall_started_flag = true; + } + simple_mutex_unlock(&lm->lm_mutex); + return rval; } void flushall_logfiles(bool flush) { - simple_mutex_lock(&lm->lm_mutex,true); - flushall_flag = flush; - simple_mutex_unlock(&lm->lm_mutex); + simple_mutex_lock(&lm->lm_mutex,true); + flushall_flag = flush; + simple_mutex_unlock(&lm->lm_mutex); } /** @@ -3240,7 +3240,7 @@ void flushall_logfiles(bool flush) */ void skygw_log_sync_all(void) { - if(!use_stdout)skygw_log_write(LOGFILE_TRACE,"Starting log flushing to disk."); + if(!use_stdout)skygw_log_write(LOGFILE_TRACE,"Starting log flushing to disk."); /** If initialization of the log manager has not been done, lm pointer can be * NULL. */ @@ -3258,7 +3258,7 @@ void skygw_log_sync_all(void) */ void skygw_set_highp(int val) { - highprec = val; + highprec = val; } diff --git a/log_manager/log_manager.h b/log_manager/log_manager.h index dfc83cb8b..150fff2d6 100644 --- a/log_manager/log_manager.h +++ b/log_manager/log_manager.h @@ -31,9 +31,9 @@ typedef struct fnames_conf_st fnames_conf_t; typedef struct logmanager_st logmanager_t; typedef enum { - BB_READY = 0x00, - BB_FULL, - BB_CLEARED + BB_READY = 0x00, + BB_FULL, + BB_CLEARED } blockbuf_state_t; typedef enum { @@ -54,8 +54,8 @@ typedef enum { FILEWRITER_INIT, FILEWRITER_RUN, FILEWRITER_DONE } */ typedef struct log_info_st { - size_t li_sesid; - int li_enabled_logs; + size_t li_sesid; + int li_enabled_logs; } log_info_t; #define LE LOGFILE_ERROR @@ -67,36 +67,36 @@ typedef struct log_info_st * Check if specified log type is enabled in general or if it is enabled * for the current session. */ -#define LOG_IS_ENABLED(id) (((lm_enabled_logfiles_bitmask & id) || \ - (log_ses_count[id] > 0 && \ - tls_log_info.li_enabled_logs & id)) ? true : false) +#define LOG_IS_ENABLED(id) (((lm_enabled_logfiles_bitmask & id) || \ + (log_ses_count[id] > 0 && \ + tls_log_info.li_enabled_logs & id)) ? true : false) -#define LOG_MAY_BE_ENABLED(id) (((lm_enabled_logfiles_bitmask & id) || \ - log_ses_count[id] > 0) ? true : false) +#define LOG_MAY_BE_ENABLED(id) (((lm_enabled_logfiles_bitmask & id) || \ + log_ses_count[id] > 0) ? true : false) /** * Execute the given command if specified log is enabled in general or * if there is at least one session for whom the log is enabled. */ -#define LOGIF_MAYBE(id,cmd) if (LOG_MAY_BE_ENABLED(id)) \ - { \ - cmd; \ - } +#define LOGIF_MAYBE(id,cmd) if (LOG_MAY_BE_ENABLED(id)) \ + { \ + cmd; \ + } /** * Execute the given command if specified log is enabled in general or * if the log is enabled for the current session. */ -#define LOGIF(id,cmd) if (LOG_IS_ENABLED(id)) \ - { \ - cmd; \ - } +#define LOGIF(id,cmd) if (LOG_IS_ENABLED(id)) \ + { \ + cmd; \ + } #if !defined(LOGIF) #define LOGIF(id,cmd) if (lm_enabled_logfiles_bitmask & id) \ - { \ - cmd; \ - } + { \ + cmd; \ + } #endif /** From f47b6324a1b5a7d5ef13c61ff522a8f56d87ffc7 Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Wed, 21 Oct 2015 13:17:50 +0300 Subject: [PATCH 06/15] Allman indentation style applied. --- log_manager/log_manager.cc | 252 ++++++++++++++++++++++++++----------- log_manager/log_manager.h | 24 +++- 2 files changed, 197 insertions(+), 79 deletions(-) diff --git a/log_manager/log_manager.cc b/log_manager/log_manager.cc index d35ddead9..7cff07d7e 100644 --- a/log_manager/log_manager.cc +++ b/log_manager/log_manager.cc @@ -114,7 +114,8 @@ static int log_augmentation = default_log_augmentation; static bool fatal_error = false; /** Writer thread structure */ -struct filewriter_st { +struct filewriter_st +{ #if defined(SS_DEBUG) skygw_chk_t fwr_chk_top; #endif @@ -136,7 +137,8 @@ struct filewriter_st { * Log client's string is copied to block-sized log buffer, which is passed * to file writer thread. */ -typedef struct blockbuf_st { +typedef struct blockbuf_st +{ #if defined(SS_DEBUG) skygw_chk_t bb_chk_top; #endif @@ -158,7 +160,8 @@ typedef struct blockbuf_st { * logfile object corresponds to physical file(s) where * certain log is written. */ -struct logfile_st { +struct logfile_st +{ #if defined(SS_DEBUG) skygw_chk_t lf_chk_top; #endif @@ -193,7 +196,8 @@ struct logfile_st { }; -struct fnames_conf_st { +struct fnames_conf_st +{ #if defined(SS_DEBUG) skygw_chk_t fn_chk_top; #endif @@ -212,7 +216,8 @@ struct fnames_conf_st { #endif }; -struct logmanager_st { +struct logmanager_st +{ #if defined(SS_DEBUG) skygw_chk_t lm_chk_top; #endif @@ -238,7 +243,8 @@ struct logmanager_st { */ typedef struct strpart_st strpart_t; -struct strpart_st { +struct strpart_st +{ char* sp_string; strpart_t* sp_next; }; @@ -494,7 +500,8 @@ bool skygw_logmanager_init( acquire_lock(&lmlock); - if (lm != NULL) { + if (lm != NULL) + { succp = true; goto return_succp; } @@ -521,7 +528,8 @@ static void logmanager_done_nomutex(void) fwr = &lm->lm_filewriter; - if (fwr->fwr_state == RUN) { + if (fwr->fwr_state == RUN) + { CHK_FILEWRITER(fwr); /** Inform filewriter thread and wait until it has stopped. */ skygw_thread_set_exitflag(fwr->fwr_thread, @@ -535,7 +543,8 @@ static void logmanager_done_nomutex(void) /** Free filewriter memory. */ filewriter_done(fwr); - for (i=LOGFILE_FIRST; i<=LOGFILE_LAST; i++) { + for (i=LOGFILE_FIRST; i<=LOGFILE_LAST; i++) + { lf = logmanager_get_logfile(lm, (logfile_id_t)i); /** Release logfile memory */ logfile_done(lf); @@ -574,7 +583,8 @@ void skygw_logmanager_done(void) { acquire_lock(&lmlock); - if (lm == NULL) { + if (lm == NULL) + { release_lock(&lmlock); return; } @@ -585,14 +595,16 @@ void skygw_logmanager_done(void) /** Wait until all users have left or someone shuts down * logmanager between lock release and acquire. */ - while(lm != NULL && lm->lm_nlinks != 0) { + while(lm != NULL && lm->lm_nlinks != 0) + { release_lock(&lmlock); pthread_yield(); acquire_lock(&lmlock); } /** Logmanager was already shut down. Return successfully. */ - if (lm == NULL) { + if (lm == NULL) + { goto return_void; } ss_dassert(lm->lm_nlinks == 0); @@ -611,7 +623,8 @@ static logfile_t* logmanager_get_logfile( ss_dassert(id >= LOGFILE_FIRST && id <= LOGFILE_LAST); lf = &lmgr->lm_logfile[id]; - if (lf->lf_state == RUN) { + if (lf->lf_state == RUN) + { CHK_LOGFILE(lf); } return lf; @@ -659,7 +672,8 @@ static int logmanager_write_log( CHK_LOGMANAGER(lm); - if (id < LOGFILE_FIRST || id > LOGFILE_LAST) { + if (id < LOGFILE_FIRST || id > LOGFILE_LAST) + { const char* errstr = "Invalid logfile id argument."; /** * invalid id, since we don't have logfile yet. @@ -672,7 +686,8 @@ static int logmanager_write_log( strlen(errstr)+1, errstr, valist); - if (err != 0) { + if (err != 0) + { fprintf(stderr, "Writing to logfile %s failed.\n", STRLOGID(LOGFILE_ERROR)); @@ -719,9 +734,13 @@ static int logmanager_write_log( sesid_str_len = 0; } if(highprec) + { timestamp_len = get_timestamp_len_hp(); + } else + { timestamp_len = get_timestamp_len(); + } cmplen = sesid_str_len > 0 ? sesid_str_len - sizeof(char) : 0; /** Find out how much can be safely written with current block size */ @@ -776,7 +795,9 @@ static int logmanager_write_log( } if(wp == NULL) + { return -1; + } #if defined (SS_LOG_DEBUG) { @@ -791,9 +812,13 @@ static int logmanager_write_log( * Returned timestamp_len doesn't include terminating null. */ if(highprec) + { timestamp_len = snprint_timestamp_hp(wp, timestamp_len); + } else + { timestamp_len = snprint_timestamp(wp, timestamp_len); + } if (sesid_str_len != 0) { /** @@ -809,12 +834,15 @@ static int logmanager_write_log( * Write next string to overwrite terminating null character * of the timestamp string. */ - if (use_valist) { + if (use_valist) + { vsnprintf(wp+timestamp_len+sesid_str_len, safe_str_len-timestamp_len-sesid_str_len, str, valist); - } else { + } + else + { snprintf(wp+timestamp_len+sesid_str_len, safe_str_len-timestamp_len-sesid_str_len, "%s", @@ -824,7 +852,8 @@ static int logmanager_write_log( /** write to syslog */ if (lf->lf_write_syslog) { - switch(id) { + switch(id) + { case LOGFILE_ERROR: syslog(LOG_ERR, "%s", wp+timestamp_len); break; @@ -856,7 +885,8 @@ static int logmanager_write_log( * disable because cross-blockbuffer locking either causes deadlock * or run out of memory blocks. */ - if (spread_down && false) { + if (spread_down && false) + { /** * Write to target log. If spread_down == true, then * write also to all logs with greater logfile id. @@ -945,7 +975,8 @@ static void blockbuf_unregister( /** * if this is the last client in a full buffer, send write request. */ - if (atomic_add(&bb->bb_refcount, -1) == 1 && bb->bb_state == BB_FULL) { + if (atomic_add(&bb->bb_refcount, -1) == 1 && bb->bb_state == BB_FULL) + { skygw_message_send(lf->lf_logmes); } ss_dassert(bb->bb_refcount >= 0); @@ -993,14 +1024,16 @@ static char* blockbuf_get_writepos( simple_mutex_lock(&bb_list->mlist_mutex, true); CHK_MLIST(bb_list); - if (bb_list->mlist_nodecount > 0) { + if (bb_list->mlist_nodecount > 0) + { /** * At least block buffer exists on the list. */ node = bb_list->mlist_first; /** Loop over blockbuf list to find write position */ - while (true) { + while (true) + { CHK_MLIST_NODE(node); /** Unlock list */ @@ -1012,7 +1045,8 @@ static char* blockbuf_get_writepos( /** Lock buffer */ simple_mutex_lock(&bb->bb_mutex, true); - if (bb->bb_state == BB_FULL || bb->bb_buf_left < str_len) { + if (bb->bb_state == BB_FULL || bb->bb_buf_left < str_len) + { /** * This block buffer is too full. * Send flush request to file writer thread. This causes @@ -1035,7 +1069,8 @@ static char* blockbuf_get_writepos( * If next node exists move forward. Else check if there is * space for a new block buffer on the list. */ - if (node != bb_list->mlist_last) { + if (node != bb_list->mlist_last) + { node = node->mlnode_next; continue; } @@ -1069,7 +1104,9 @@ static char* blockbuf_get_writepos( */ bb_list->mlist_versno += 1; ss_dassert(bb_list->mlist_versno%2 == 0); - } else { + } + else + { /** * List and buffers are full. * Reset to the beginning of the list, and wait until @@ -1082,7 +1119,9 @@ static char* blockbuf_get_writepos( continue; } - }else if(bb->bb_state == BB_CLEARED){ + } + else if(bb->bb_state == BB_CLEARED) + { /** *Move the cleared buffer to the end of the list if it is the first one in the list @@ -1115,29 +1154,38 @@ static char* blockbuf_get_writepos( } else { - if(node->mlnode_next){ + if(node->mlnode_next) + { node = node->mlnode_next; - }else{ + } + else + { node = bb_list->mlist_first; } continue; } - }else if (bb->bb_state == BB_READY){ + } + else if (bb->bb_state == BB_READY) + { /** * There is space for new log string. */ break; } } /** while (true) */ - } else { + } + else + { /** * Create the first block buffer to logfile's blockbuf list. */ if((bb = blockbuf_init(id)) == NULL) + { return NULL; + } CHK_BLOCKBUF(bb); @@ -1259,7 +1307,8 @@ int skygw_log_enable( } CHK_LOGMANAGER(lm); - if (logfile_set_enabled(id, true)) { + if (logfile_set_enabled(id, true)) + { lm->lm_enabled_logfiles |= id; /** * Set global variable @@ -1323,7 +1372,8 @@ static bool logfile_set_enabled( CHK_LOGMANAGER(lm); - if (id < LOGFILE_FIRST || id > LOGFILE_LAST) { + if (id < LOGFILE_FIRST || id > LOGFILE_LAST) + { const char* errstr = "Invalid logfile id argument."; /** * invalid id, since we don't have logfile yet. @@ -1336,7 +1386,8 @@ static bool logfile_set_enabled( strlen(errstr)+1, errstr, notused); - if (err != 0) { + if (err != 0) + { fprintf(stderr, "* Writing to logfile %s failed.\n", STRLOGID(LOGFILE_ERROR)); @@ -1348,9 +1399,12 @@ static bool logfile_set_enabled( CHK_LOGFILE(lf); if(use_stdout == 0) { - if (val) { + if (val) + { logstr = strdup("---\tLogging to file is enabled\t--"); - } else { + } + else + { logstr = strdup("---\tLogging to file is disabled\t--"); } @@ -1366,7 +1420,8 @@ static bool logfile_set_enabled( notused); free(logstr); } - if (err != 0) { + if (err != 0) + { lf->lf_enabled = oldval; fprintf(stderr, "logfile_set_enabled failed. Writing notification to logfile %s " @@ -1517,7 +1572,8 @@ int skygw_log_write_context_flush( int len = vsnprintf(NULL, 0, str, valist); va_end(valist); - if (len >= 0) { + if (len >= 0) + { char message[len + 1]; va_start(valist, str); @@ -1558,7 +1614,8 @@ int skygw_log_write_context( int len = vsnprintf(NULL, 0, str, valist); va_end(valist); - if (len >= 0) { + if (len >= 0) + { char message[len + 1]; va_start(valist, str); @@ -1570,7 +1627,8 @@ int skygw_log_write_context( err = log_write(id, file, line, function, len2, message, flush); - if (err != 0) { + if (err != 0) + { fprintf(stderr, "skygw_log_write failed.\n"); } } @@ -1585,7 +1643,8 @@ int skygw_log_flush( int err = 0; va_list valist; /**< Dummy, must be present but it is not processed */ - if (!logmanager_register(false)) { + if (!logmanager_register(false)) + { ss_dfprintf(stderr, "Can't register to logmanager, nothing to flush\n"); goto return_err; @@ -1593,7 +1652,8 @@ int skygw_log_flush( CHK_LOGMANAGER(lm); err = logmanager_write_log(id, true, false, false, false, 0, NULL, valist); - if (err != 0) { + if (err != 0) + { fprintf(stderr, "skygw_log_flush failed.\n"); goto return_unregister; } @@ -1678,14 +1738,16 @@ static bool logmanager_register( acquire_lock(&lmlock); - if (lm == NULL || !lm->lm_enabled) { + if (lm == NULL || !lm->lm_enabled) + { /** * Flush succeeds if logmanager is shut or shutting down. * returning false so that flusher doesn't access logmanager * and its members which would probabaly lead to NULL pointer * reference. */ - if (!writep || fatal_error) { + if (!writep || fatal_error) + { succp = false; goto return_succp; } @@ -1697,18 +1759,21 @@ static bool logmanager_register( * logmanager is enabled if someone already restarted * it between latest lock release, and acquire. */ - while (lm != NULL && !lm->lm_enabled) { + while (lm != NULL && !lm->lm_enabled) + { release_lock(&lmlock); pthread_yield(); acquire_lock(&lmlock); } - if (lm == NULL) { + if (lm == NULL) + { succp = logmanager_init_nomutex(0, NULL); } } /** if logmanager existed or was succesfully restarted, increase link */ - if (succp) { + if (succp) + { lm->lm_nlinks += 1; } @@ -1800,7 +1865,8 @@ static bool fnames_conf_init( optind = 1; /**fn_state == RUN || fn->fn_state == DONE); @@ -1944,7 +2012,8 @@ static char* fname_conf_get_prefix( CHK_FNAMES_CONF(fn); ss_dassert(id >= LOGFILE_FIRST && id <= LOGFILE_LAST); - switch (id) { + switch (id) + { case LOGFILE_DEBUG: return strdup(fn->fn_debug_prefix); break; @@ -1973,7 +2042,8 @@ static char* fname_conf_get_suffix( CHK_FNAMES_CONF(fn); ss_dassert(id >= LOGFILE_FIRST && id <= LOGFILE_LAST); - switch (id) { + switch (id) + { case LOGFILE_DEBUG: return strdup(fn->fn_debug_suffix); break; @@ -2031,7 +2101,8 @@ static bool logfiles_init( /** * Initialize log files, pass softlink flag if necessary. */ - while (lid<=LOGFILE_LAST && succp) { + while (lid<=LOGFILE_LAST && succp) + { /** * Check if the file is stored in shared memory. If so, * a symbolic link will be created to log directory. @@ -2059,16 +2130,19 @@ static bool logfiles_init( { write_syslog = false; } + succp = logfile_init(&lm->lm_logfile[lid], (logfile_id_t)lid, lm, store_shmem, write_syslog); - if (!succp) { + if (!succp) + { fprintf(stderr, "*\n* Error : Initializing log files failed.\n"); break; } + lid <<= 1; i += 1; } @@ -2137,7 +2211,8 @@ static bool logfile_create( store_shmem = lf->lf_store_shmem; - do { + do + { namecreatefail = false; nameconflicts = false; @@ -2233,7 +2308,8 @@ static bool logfile_create( lf->lf_full_link_name = NULL; } } - } while (namecreatefail || nameconflicts); + } + while (namecreatefail || nameconflicts); succp = true; @@ -2318,8 +2394,10 @@ static bool logfile_open_file( succp = false; goto return_succp; } + free(start_msg_str); } + succp = true; return_succp: @@ -2387,7 +2465,8 @@ static char* form_full_file_name( seqno = lf->lf_name_seqno; } - if (parts == NULL || parts->sp_string == NULL) { + if (parts == NULL || parts->sp_string == NULL) + { goto return_filename; } /** @@ -2435,7 +2514,11 @@ static char* form_full_file_name( } return_filename: - if (seqnostr != NULL) free(seqnostr); + if (seqnostr != NULL) + { + free(seqnostr); + } + return filename; } @@ -2575,7 +2658,8 @@ static bool file_is_symlink( { rc = lstat(filename, &b); - if (rc != -1 && S_ISLNK(b.st_mode)) { + if (rc != -1 && S_ISLNK(b.st_mode)) + { succp = true; } } @@ -2734,7 +2818,8 @@ return_with_succp: static void logfile_done( logfile_t* lf) { - switch(lf->lf_state) { + switch(lf->lf_state) + { case RUN: CHK_LOGFILE(lf); ss_dassert(lf->lf_npending_writes == 0); @@ -2801,7 +2886,8 @@ static bool filewriter_init( /** Message from clients to filewriter */ fw->fwr_clientmes = clientmes; - if (fw->fwr_logmes == NULL || fw->fwr_clientmes == NULL) { + if (fw->fwr_logmes == NULL || fw->fwr_clientmes == NULL) + { goto return_succp; } @@ -2824,7 +2910,8 @@ static bool filewriter_init( succp = true; return_succp: - if (!succp) { + if (!succp) + { filewriter_done(fw); } ss_dassert(fw->fwr_state == RUN || fw->fwr_state == DONE); @@ -2837,7 +2924,8 @@ static void filewriter_done( int i; logfile_id_t id; - switch(fw->fwr_state) { + switch(fw->fwr_state) + { case RUN: CHK_FILEWRITER(fw); case INIT: @@ -2932,13 +3020,15 @@ static void* thr_filewriter_fun( /** Inform log manager about the state. */ skygw_message_send(fwr->fwr_clientmes); - while(!skygw_thread_must_exit(thr)) { + while(!skygw_thread_must_exit(thr)) + { /** * Wait until new log arrival message appears. * Reset message to avoid redundant calls. */ skygw_message_wait(fwr->fwr_logmes); - if(skygw_thread_must_exit(thr)){ + if(skygw_thread_must_exit(thr)) + { flushall_logfiles(true); } @@ -2982,9 +3072,13 @@ static void* thr_filewriter_fun( else if ((succp = logfile_open_file(fwr, lf))) { if(use_stdout) + { skygw_file_free (file); + } else + { skygw_file_close(file, false); /*< close old file */ + } } if (!succp) @@ -3079,12 +3173,17 @@ static void* thr_filewriter_fun( simple_mutex_unlock(&bb->bb_mutex); /** Consistent lock-free read on the list */ - do { + do + { while ((vn1 = bb_list->mlist_versno)%2 - != 0); + != 0) + { + continue; + } node = node->mlnode_next; vn2 = bb_list->mlist_versno; - } while (vn1 != vn2 && node); + } + while (vn1 != vn2 && node); } /* while (node != NULL) */ @@ -3094,7 +3193,8 @@ static void* thr_filewriter_fun( * flushed. */ - if(flushall_started_flag){ + if(flushall_started_flag) + { flushall_started_flag = false; flushall_done_flag = true; i = LOGFILE_FIRST; @@ -3109,7 +3209,8 @@ static void* thr_filewriter_fun( } }/* for */ - if(flushall_done_flag){ + if(flushall_done_flag) + { flushall_done_flag = false; flushall_logfiles(false); skygw_message_send(fwr->fwr_clientmes); @@ -3127,7 +3228,8 @@ static void* thr_filewriter_fun( static void fnames_conf_done( fnames_conf_t* fn) { - switch (fn->fn_state) { + switch (fn->fn_state) + { case RUN: CHK_FNAMES_CONF(fn); case INIT: @@ -3221,7 +3323,8 @@ bool thr_flushall_check() bool rval = false; simple_mutex_lock(&lm->lm_mutex,true); rval = flushall_flag; - if(rval && !flushall_started_flag && !flushall_done_flag){ + if(rval && !flushall_started_flag && !flushall_done_flag) + { flushall_started_flag = true; } simple_mutex_unlock(&lm->lm_mutex); @@ -3240,7 +3343,10 @@ void flushall_logfiles(bool flush) */ void skygw_log_sync_all(void) { - if(!use_stdout)skygw_log_write(LOGFILE_TRACE,"Starting log flushing to disk."); + if(!use_stdout) + { + skygw_log_write(LOGFILE_TRACE,"Starting log flushing to disk."); + } /** If initialization of the log manager has not been done, lm pointer can be * NULL. */ diff --git a/log_manager/log_manager.h b/log_manager/log_manager.h index 150fff2d6..9c3d97200 100644 --- a/log_manager/log_manager.h +++ b/log_manager/log_manager.h @@ -30,13 +30,15 @@ typedef struct logfile_st logfile_t; typedef struct fnames_conf_st fnames_conf_t; typedef struct logmanager_st logmanager_t; -typedef enum { +typedef enum +{ BB_READY = 0x00, BB_FULL, BB_CLEARED } blockbuf_state_t; -typedef enum { +typedef enum +{ LOGFILE_ERROR = 1, LOGFILE_FIRST = LOGFILE_ERROR, LOGFILE_MESSAGE = 2, @@ -46,13 +48,17 @@ typedef enum { } logfile_id_t; -typedef enum { FILEWRITER_INIT, FILEWRITER_RUN, FILEWRITER_DONE } - filewriter_state_t; +typedef enum +{ + FILEWRITER_INIT, + FILEWRITER_RUN, + FILEWRITER_DONE +} filewriter_state_t; /** * Thread-specific logging information. */ -typedef struct log_info_st +typedef struct log_info { size_t li_sesid; int li_enabled_logs; @@ -106,7 +112,13 @@ typedef struct log_info_st * RUN Struct is valid for run-time checking. * DONE means that possible memory allocations have been released. */ -typedef enum { UNINIT = 0, INIT, RUN, DONE } flat_obj_state_t; +typedef enum +{ + UNINIT = 0, + INIT, + RUN, + DONE +} flat_obj_state_t; /** * LOG_AUGMENT_WITH_FUNCTION Each logged line is suffixed with [function-name]. From ba23fc6fa21e1b29cc486a0a75ac9cd2dd7274a4 Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Wed, 21 Oct 2015 13:28:04 +0300 Subject: [PATCH 07/15] Structures renamed according to style guide. Structures renamed according to style guide and forward declaration moved from header (where they are not needed) to implementation. --- log_manager/log_manager.cc | 26 ++++++++++++++++---------- log_manager/log_manager.h | 5 ----- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/log_manager/log_manager.cc b/log_manager/log_manager.cc index 7cff07d7e..3cf1c5265 100644 --- a/log_manager/log_manager.cc +++ b/log_manager/log_manager.cc @@ -93,6 +93,13 @@ char* shmem_id_str = NULL; char* syslog_id_str = strdup("LOGFILE_ERROR"); char* syslog_ident_str = NULL; +/** Forward declarations + */ +typedef struct filewriter filewriter_t; +typedef struct logfile logfile_t; +typedef struct fnames_conf fnames_conf_t; +typedef struct logmanager logmanager_t; + /** * Global log manager pointer and lock variable. * lmlock protects logmanager access. @@ -114,7 +121,7 @@ static int log_augmentation = default_log_augmentation; static bool fatal_error = false; /** Writer thread structure */ -struct filewriter_st +struct filewriter { #if defined(SS_DEBUG) skygw_chk_t fwr_chk_top; @@ -137,7 +144,7 @@ struct filewriter_st * Log client's string is copied to block-sized log buffer, which is passed * to file writer thread. */ -typedef struct blockbuf_st +typedef struct blockbuf { #if defined(SS_DEBUG) skygw_chk_t bb_chk_top; @@ -160,7 +167,7 @@ typedef struct blockbuf_st * logfile object corresponds to physical file(s) where * certain log is written. */ -struct logfile_st +struct logfile { #if defined(SS_DEBUG) skygw_chk_t lf_chk_top; @@ -196,7 +203,7 @@ struct logfile_st }; -struct fnames_conf_st +struct fnames_conf { #if defined(SS_DEBUG) skygw_chk_t fn_chk_top; @@ -216,7 +223,7 @@ struct fnames_conf_st #endif }; -struct logmanager_st +struct logmanager { #if defined(SS_DEBUG) skygw_chk_t lm_chk_top; @@ -241,13 +248,12 @@ struct logmanager_st * Type definition for string part. It is used in forming the log file name * from string parts provided by the client of log manager, as arguments. */ -typedef struct strpart_st strpart_t; -struct strpart_st +typedef struct strpart { - char* sp_string; - strpart_t* sp_next; -}; + char* sp_string; + struct strpart* sp_next; +} strpart_t; /** Static function declarations */ diff --git a/log_manager/log_manager.h b/log_manager/log_manager.h index 9c3d97200..32576a586 100644 --- a/log_manager/log_manager.h +++ b/log_manager/log_manager.h @@ -25,11 +25,6 @@ #define STRERROR_BUFLEN 512 #endif -typedef struct filewriter_st filewriter_t; -typedef struct logfile_st logfile_t; -typedef struct fnames_conf_st fnames_conf_t; -typedef struct logmanager_st logmanager_t; - typedef enum { BB_READY = 0x00, From 18181e8cb9c1e818c15e232a98de74f676179bd5 Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Wed, 21 Oct 2015 15:35:33 +0300 Subject: [PATCH 08/15] All keywords now followed by space. Plus some missing Allman identation fixes. --- log_manager/log_manager.cc | 82 ++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 38 deletions(-) diff --git a/log_manager/log_manager.cc b/log_manager/log_manager.cc index 3cf1c5265..7de032a4f 100644 --- a/log_manager/log_manager.cc +++ b/log_manager/log_manager.cc @@ -419,7 +419,7 @@ static bool logmanager_init_nomutex( fn->fn_state = UNINIT; fw->fwr_state = UNINIT; - if(!do_syslog) + if (!do_syslog) { free(syslog_id_str); syslog_id_str = NULL; @@ -433,7 +433,7 @@ static bool logmanager_init_nomutex( } /** Initialize logfiles */ - if(!logfiles_init(lm)) + if (!logfiles_init(lm)) { err = 1; goto return_succp; @@ -601,7 +601,7 @@ void skygw_logmanager_done(void) /** Wait until all users have left or someone shuts down * logmanager between lock release and acquire. */ - while(lm != NULL && lm->lm_nlinks != 0) + while (lm != NULL && lm->lm_nlinks != 0) { release_lock(&lmlock); pthread_yield(); @@ -739,7 +739,7 @@ static int logmanager_write_log( { sesid_str_len = 0; } - if(highprec) + if (highprec) { timestamp_len = get_timestamp_len_hp(); } @@ -773,11 +773,11 @@ static int logmanager_write_log( tok = strtok(copy,"|"); tok = strtok(NULL,"|"); - if(strstr(str,"message|") && tok) + if (strstr(str,"message|") && tok) { tokval = atoi(tok); - if(prevval > 0) + if (prevval > 0) { ss_dassert(tokval == (prevval + 1)); } @@ -788,7 +788,7 @@ static int logmanager_write_log( } #endif /** Book space for log string from buffer */ - if(do_maxscalelog) + if (do_maxscalelog) { wp = blockbuf_get_writepos(&bb, id, @@ -800,7 +800,7 @@ static int logmanager_write_log( wp = (char*)malloc(sizeof(char)*(timestamp_len-sizeof(char)+cmplen+str_len + 1)); } - if(wp == NULL) + if (wp == NULL) { return -1; } @@ -817,7 +817,7 @@ static int logmanager_write_log( * to wp. * Returned timestamp_len doesn't include terminating null. */ - if(highprec) + if (highprec) { timestamp_len = snprint_timestamp_hp(wp, timestamp_len); } @@ -879,7 +879,7 @@ static int logmanager_write_log( } wp[safe_str_len-1] = '\n'; - if(do_maxscalelog) + if (do_maxscalelog) { blockbuf_unregister(bb); } @@ -1089,8 +1089,10 @@ static char* blockbuf_get_writepos( /** * New node is created */ - if((bb = blockbuf_init(id)) == NULL) + if ((bb = blockbuf_init(id)) == NULL) + { return NULL; + } CHK_BLOCKBUF(bb); @@ -1126,7 +1128,7 @@ static char* blockbuf_get_writepos( } } - else if(bb->bb_state == BB_CLEARED) + else if (bb->bb_state == BB_CLEARED) { /** @@ -1136,10 +1138,10 @@ static char* blockbuf_get_writepos( simple_mutex_unlock(&bb->bb_mutex); simple_mutex_lock(&bb_list->mlist_mutex, true); - if(node == bb_list->mlist_first) + if (node == bb_list->mlist_first) { - if(bb_list->mlist_nodecount > 1 && + if (bb_list->mlist_nodecount > 1 && node != bb_list->mlist_last){ bb_list->mlist_last->mlnode_next = bb_list->mlist_first; bb_list->mlist_first = bb_list->mlist_first->mlnode_next; @@ -1160,7 +1162,7 @@ static char* blockbuf_get_writepos( } else { - if(node->mlnode_next) + if (node->mlnode_next) { node = node->mlnode_next; } @@ -1188,7 +1190,7 @@ static char* blockbuf_get_writepos( * Create the first block buffer to logfile's blockbuf list. */ - if((bb = blockbuf_init(id)) == NULL) + if ((bb = blockbuf_init(id)) == NULL) { return NULL; } @@ -1403,7 +1405,7 @@ static bool logfile_set_enabled( } lf = &lm->lm_logfile[id]; CHK_LOGFILE(lf); - if(use_stdout == 0) + if (use_stdout == 0) { if (val) { @@ -1785,7 +1787,7 @@ static bool logmanager_register( return_succp: - if(!succp) + if (!succp) { fatal_error = true; } @@ -1913,7 +1915,7 @@ static bool fnames_conf_init( case 'l': /** record list of log file ids for syslogged */ - if(do_syslog) + if (do_syslog) { if (syslog_id_str != NULL) { @@ -1964,7 +1966,7 @@ static bool fnames_conf_init( strdup(get_logpath_default()) : fn->fn_logpath; /** Set identity string for syslog if it is not set in config.*/ - if(do_syslog) + if (do_syslog) { syslog_ident_str = (syslog_ident_str == NULL ? @@ -2343,7 +2345,7 @@ static bool logfile_open_file( char* start_msg_str; int err; - if(use_stdout) + if (use_stdout) { fw->fwr_file[lf->lf_id] = skygw_file_alloc ( lf->lf_full_file_name); @@ -2373,7 +2375,7 @@ static bool logfile_open_file( goto return_succp; } - if(use_stdout == 0) + if (use_stdout == 0) { if (lf->lf_enabled) { @@ -2597,14 +2599,14 @@ static bool check_file_and_path( } else { - if(access(filename,F_OK) == 0) + if (access(filename,F_OK) == 0) { exists = true; - if(access(filename,W_OK) == 0) + if (access(filename,W_OK) == 0) { - if(writable) + if (writable) { *writable = true; } @@ -2632,7 +2634,7 @@ static bool check_file_and_path( strerror_r(errno, errbuf, sizeof(errbuf))); } - if(writable) + if (writable) { *writable = false; } @@ -2642,7 +2644,7 @@ static bool check_file_and_path( else { exists = false; - if(writable) + if (writable) { *writable = true; } @@ -2782,7 +2784,7 @@ static bool logfile_init( logfile->lf_full_link_name, logfile->lf_full_file_name); } - else if(!use_stdout) + else if (!use_stdout) { fprintf(stderr, "%s\t: %s\n", STRLOGNAME(logfile_id), @@ -2940,10 +2942,14 @@ static void filewriter_done( for (i=LOGFILE_FIRST; i<=LOGFILE_LAST; i++) { id = (logfile_id_t)i; - if(use_stdout) + if (use_stdout) + { skygw_file_free(fw->fwr_file[id]); + } else + { skygw_file_close(fw->fwr_file[id], true); + } } fw->fwr_state = DONE; case DONE: @@ -3026,14 +3032,14 @@ static void* thr_filewriter_fun( /** Inform log manager about the state. */ skygw_message_send(fwr->fwr_clientmes); - while(!skygw_thread_must_exit(thr)) + while (!skygw_thread_must_exit(thr)) { /** * Wait until new log arrival message appears. * Reset message to avoid redundant calls. */ skygw_message_wait(fwr->fwr_logmes); - if(skygw_thread_must_exit(thr)) + if (skygw_thread_must_exit(thr)) { flushall_logfiles(true); } @@ -3077,7 +3083,7 @@ static void* thr_filewriter_fun( } else if ((succp = logfile_open_file(fwr, lf))) { - if(use_stdout) + if (use_stdout) { skygw_file_free (file); } @@ -3132,7 +3138,7 @@ static void* thr_filewriter_fun( * buffer is at least half-full * -> write to disk */ - while(bb->bb_refcount > 0) + while (bb->bb_refcount > 0) { simple_mutex_unlock( &bb->bb_mutex); @@ -3199,7 +3205,7 @@ static void* thr_filewriter_fun( * flushed. */ - if(flushall_started_flag) + if (flushall_started_flag) { flushall_started_flag = false; flushall_done_flag = true; @@ -3215,7 +3221,7 @@ static void* thr_filewriter_fun( } }/* for */ - if(flushall_done_flag) + if (flushall_done_flag) { flushall_done_flag = false; flushall_logfiles(false); @@ -3329,7 +3335,7 @@ bool thr_flushall_check() bool rval = false; simple_mutex_lock(&lm->lm_mutex,true); rval = flushall_flag; - if(rval && !flushall_started_flag && !flushall_done_flag) + if (rval && !flushall_started_flag && !flushall_done_flag) { flushall_started_flag = true; } @@ -3349,14 +3355,14 @@ void flushall_logfiles(bool flush) */ void skygw_log_sync_all(void) { - if(!use_stdout) + if (!use_stdout) { skygw_log_write(LOGFILE_TRACE,"Starting log flushing to disk."); } /** If initialization of the log manager has not been done, lm pointer can be * NULL. */ - if(lm) + if (lm) { flushall_logfiles(true); skygw_message_send(lm->lm_logmes); From cb468be04fe0b99b6ecfcaa95c5fdff0fe5c7088 Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Wed, 21 Oct 2015 15:47:04 +0300 Subject: [PATCH 09/15] Function declarations changed to common Maxscale style. --- log_manager/log_manager.cc | 257 ++++++++++++++++--------------------- 1 file changed, 108 insertions(+), 149 deletions(-) diff --git a/log_manager/log_manager.cc b/log_manager/log_manager.cc index 7de032a4f..55725c5de 100644 --- a/log_manager/log_manager.cc +++ b/log_manager/log_manager.cc @@ -258,12 +258,11 @@ typedef struct strpart /** Static function declarations */ static bool logfiles_init(logmanager_t* lmgr); -static bool logfile_init( - logfile_t* logfile, - logfile_id_t logfile_id, - logmanager_t* logmanager, - bool store_shmem, - bool write_syslog); +static bool logfile_init(logfile_t* logfile, + logfile_id_t logfile_id, + logmanager_t* logmanager, + bool store_shmem, + bool write_syslog); static void logfile_done(logfile_t* logfile); static void logfile_free_memory(logfile_t* lf); static void logfile_flush(logfile_t* lf); @@ -272,53 +271,49 @@ static bool logfile_create(logfile_t* lf); static bool logfile_open_file(filewriter_t* fw, logfile_t* lf); static char* form_full_file_name(strpart_t* parts, logfile_t* lf, int seqnoidx); -static bool filewriter_init( - logmanager_t* logmanager, - filewriter_t* fw, - skygw_message_t* clientmes, - skygw_message_t* logmes); -static void filewriter_done(filewriter_t* filewriter); -static bool fnames_conf_init(fnames_conf_t* fn, int argc, char* argv[]); -static void fnames_conf_done(fnames_conf_t* fn); -static void fnames_conf_free_memory(fnames_conf_t* fn); -static char* fname_conf_get_prefix(fnames_conf_t* fn, logfile_id_t id); -static char* fname_conf_get_suffix(fnames_conf_t* fn, logfile_id_t id); -static void* thr_filewriter_fun(void* data); +static bool filewriter_init(logmanager_t* logmanager, + filewriter_t* fw, + skygw_message_t* clientmes, + skygw_message_t* logmes); +static void filewriter_done(filewriter_t* filewriter); +static bool fnames_conf_init(fnames_conf_t* fn, int argc, char* argv[]); +static void fnames_conf_done(fnames_conf_t* fn); +static void fnames_conf_free_memory(fnames_conf_t* fn); +static char* fname_conf_get_prefix(fnames_conf_t* fn, logfile_id_t id); +static char* fname_conf_get_suffix(fnames_conf_t* fn, logfile_id_t id); +static void* thr_filewriter_fun(void* data); static logfile_t* logmanager_get_logfile(logmanager_t* lm, logfile_id_t id); static bool logmanager_register(bool writep); static void logmanager_unregister(void); static bool logmanager_init_nomutex(int argc, char* argv[]); static void logmanager_done_nomutex(void); -static int logmanager_write_log( - logfile_id_t id, - bool flush, - bool use_valist, - bool spread_down, - bool rotate, - size_t len, - const char* str, - va_list valist); +static int logmanager_write_log(logfile_id_t id, + bool flush, + bool use_valist, + bool spread_down, + bool rotate, + size_t len, + const char* str, + va_list valist); static blockbuf_t* blockbuf_init(logfile_id_t id); -static void blockbuf_node_done(void* bb_data); -static char* blockbuf_get_writepos( - blockbuf_t** p_bb, - logfile_id_t id, - size_t str_len, - bool flush); +static void blockbuf_node_done(void* bb_data); +static char* blockbuf_get_writepos(blockbuf_t** p_bb, + logfile_id_t id, + size_t str_len, + bool flush); -static void blockbuf_register(blockbuf_t* bb); -static void blockbuf_unregister(blockbuf_t* bb); -static bool logfile_set_enabled(logfile_id_t id, bool val); +static void blockbuf_register(blockbuf_t* bb); +static void blockbuf_unregister(blockbuf_t* bb); +static bool logfile_set_enabled(logfile_id_t id, bool val); static char* add_slash(char* str); -static bool check_file_and_path( - char* filename, - bool* writable, - bool do_log); +static bool check_file_and_path(char* filename, + bool* writable, + bool do_log); -static bool file_is_symlink(char* filename); +static bool file_is_symlink(char* filename); static int skygw_log_disable_raw(logfile_id_t id, bool emergency); /*< no locking */ static int find_last_seqno(strpart_t* parts, int seqno, int seqnoidx); void flushall_logfiles(bool flush); @@ -374,9 +369,7 @@ const char* get_logpath_default(void) return "/var/log/maxscale"; } -static bool logmanager_init_nomutex( - int argc, - char* argv[]) +static bool logmanager_init_nomutex(int argc, char* argv[]) { fnames_conf_t* fn; filewriter_t* fw; @@ -498,9 +491,7 @@ return_succp: * @return true if succeed, otherwise false * */ -bool skygw_logmanager_init( - int argc, - char* argv[]) +bool skygw_logmanager_init(int argc, char* argv[]) { bool succp = false; @@ -658,15 +649,14 @@ static logfile_t* logmanager_get_logfile( * @return 0 if succeed, -1 otherwise * */ -static int logmanager_write_log( - logfile_id_t id, - bool flush, - bool use_valist, - bool spread_down, - bool rotate, - size_t str_len, - const char* str, - va_list valist) +static int logmanager_write_log(logfile_id_t id, + bool flush, + bool use_valist, + bool spread_down, + bool rotate, + size_t str_len, + const char* str, + va_list valist) { logfile_t* lf; char* wp; @@ -955,8 +945,7 @@ return_err: * * @param bb block buffer */ -static void blockbuf_register( - blockbuf_t* bb) +static void blockbuf_register(blockbuf_t* bb) { CHK_BLOCKBUF(bb); ss_dassert(bb->bb_refcount >= 0); @@ -969,8 +958,7 @@ static void blockbuf_register( * * @param bb block buffer */ -static void blockbuf_unregister( - blockbuf_t* bb) +static void blockbuf_unregister(blockbuf_t* bb) { logfile_t* lf; @@ -1008,11 +996,10 @@ static void blockbuf_unregister( * mutex. * */ -static char* blockbuf_get_writepos( - blockbuf_t** p_bb, - logfile_id_t id, - size_t str_len, - bool flush) +static char* blockbuf_get_writepos(blockbuf_t** p_bb, + logfile_id_t id, + size_t str_len, + bool flush) { logfile_t* lf; mlist_t* bb_list; @@ -1265,16 +1252,14 @@ static char* blockbuf_get_writepos( return pos; } -static void blockbuf_node_done( - void* bb_data) +static void blockbuf_node_done(void* bb_data) { blockbuf_t* bb = (blockbuf_t *)bb_data; simple_mutex_done(&bb->bb_mutex); } -static blockbuf_t* blockbuf_init( - logfile_id_t id) +static blockbuf_t* blockbuf_init(logfile_id_t id) { blockbuf_t* bb; @@ -1303,8 +1288,7 @@ static blockbuf_t* blockbuf_init( } -int skygw_log_enable( - logfile_id_t id) +int skygw_log_enable(logfile_id_t id) { bool err = 0; @@ -1329,8 +1313,7 @@ return_err: return err; } -int skygw_log_disable( - logfile_id_t id) /*< no locking */ +int skygw_log_disable(logfile_id_t id) /*< no locking */ { int rc; @@ -1562,13 +1545,12 @@ static int log_write(logfile_id_t id, return rv; } -int skygw_log_write_context_flush( - logfile_id_t id, - const char* file, - int line, - const char* function, - const char* str, - ...) +int skygw_log_write_context_flush(logfile_id_t id, + const char* file, + int line, + const char* function, + const char* str, + ...) { int err = 0; va_list valist; @@ -1604,13 +1586,12 @@ int skygw_log_write_context_flush( -int skygw_log_write_context( - logfile_id_t id, - const char* file, - int line, - const char* function, - const char* str, - ...) +int skygw_log_write_context(logfile_id_t id, + const char* file, + int line, + const char* function, + const char* str, + ...) { int err = 0; va_list valist; @@ -1645,8 +1626,7 @@ int skygw_log_write_context( } -int skygw_log_flush( - logfile_id_t id) +int skygw_log_flush(logfile_id_t id) { int err = 0; va_list valist; /**< Dummy, must be present but it is not processed */ @@ -1676,8 +1656,7 @@ return_err: * Replace current logfile with new file with increased sequence number on * its name. */ -int skygw_log_rotate( - logfile_id_t id) +int skygw_log_rotate(logfile_id_t id) { int err = 0; logfile_t* lf; @@ -1739,8 +1718,7 @@ return_err: * @details Link count modify is protected by mutex. * */ -static bool logmanager_register( - bool writep) +static bool logmanager_register(bool writep) { bool succp = true; @@ -1839,10 +1817,9 @@ static void logmanager_unregister(void) * @details Note that input parameter lenghts are checked here. * */ -static bool fnames_conf_init( - fnames_conf_t* fn, - int argc, - char* argv[]) +static bool fnames_conf_init(fnames_conf_t* fn, + int argc, + char* argv[]) { int opt; bool succp = false; @@ -2013,9 +1990,8 @@ return_conf_init: } -static char* fname_conf_get_prefix( - fnames_conf_t* fn, - logfile_id_t id) +static char* fname_conf_get_prefix(fnames_conf_t* fn, + logfile_id_t id) { CHK_FNAMES_CONF(fn); ss_dassert(id >= LOGFILE_FIRST && id <= LOGFILE_LAST); @@ -2043,9 +2019,8 @@ static char* fname_conf_get_prefix( } } -static char* fname_conf_get_suffix( - fnames_conf_t* fn, - logfile_id_t id) +static char* fname_conf_get_suffix(fnames_conf_t* fn, + logfile_id_t id) { CHK_FNAMES_CONF(fn); ss_dassert(id >= LOGFILE_FIRST && id <= LOGFILE_LAST); @@ -2157,8 +2132,7 @@ static bool logfiles_init( return succp; } -static void logfile_flush( - logfile_t* lf) +static void logfile_flush(logfile_t* lf) { CHK_LOGFILE(lf); acquire_lock(&lf->lf_spinlock); @@ -2173,8 +2147,7 @@ static void logfile_flush( * * @param lf logfile pointer */ -static void logfile_rotate( - logfile_t* lf) +static void logfile_rotate(logfile_t* lf) { CHK_LOGFILE(lf); acquire_lock(&lf->lf_spinlock); @@ -2196,8 +2169,7 @@ static void logfile_rotate( * using log manager in parallel with multiple processes and by configuring * log manager to use same directories among those processes. */ -static bool logfile_create( - logfile_t* lf) +static bool logfile_create(logfile_t* lf) { bool namecreatefail; bool nameconflicts; @@ -2337,9 +2309,8 @@ return_succp: * false if failed. * */ -static bool logfile_open_file( - filewriter_t* fw, - logfile_t* lf) +static bool logfile_open_file(filewriter_t* fw, + logfile_t* lf) { bool succp; char* start_msg_str; @@ -2428,10 +2399,9 @@ return_succp: * @return Pointer to filename, of NULL if failed. * */ -static char* form_full_file_name( - strpart_t* parts, - logfile_t* lf, - int seqnoidx) +static char* form_full_file_name(strpart_t* parts, + logfile_t* lf, + int seqnoidx) { int i; int seqno; @@ -2545,8 +2515,7 @@ return_filename: * @details (write detailed description here) * */ -static char* add_slash( - char* str) +static char* add_slash(char* str) { char* p = str; size_t plen = strlen(p); @@ -2581,10 +2550,9 @@ static char* add_slash( * TODO: recall what was the reason for not succeeding with simply * calling access, and fstat. vraa 26.11.13 */ -static bool check_file_and_path( - char* filename, - bool* writable, - bool do_log) +static bool check_file_and_path(char* filename, + bool* writable, + bool do_log) { bool exists; @@ -2655,8 +2623,7 @@ static bool check_file_and_path( -static bool file_is_symlink( - char* filename) +static bool file_is_symlink(char* filename) { int rc; bool succp = false; @@ -2689,12 +2656,11 @@ static bool file_is_symlink( * * @return true if succeed, false otherwise */ -static bool logfile_init( - logfile_t* logfile, - logfile_id_t logfile_id, - logmanager_t* logmanager, - bool store_shmem, - bool write_syslog) +static bool logfile_init(logfile_t* logfile, + logfile_id_t logfile_id, + logmanager_t* logmanager, + bool store_shmem, + bool write_syslog) { bool succp = false; fnames_conf_t* fn = &logmanager->lm_fnames_conf; @@ -2823,8 +2789,7 @@ return_with_succp: * flag read/write. Lock would be held during log write operation by clients. * */ -static void logfile_done( - logfile_t* lf) +static void logfile_done(logfile_t* lf) { switch(lf->lf_state) { @@ -2870,11 +2835,10 @@ static void logfile_free_memory( * @return true if succeed, false if failed * */ -static bool filewriter_init( - logmanager_t* logmanager, - filewriter_t* fw, - skygw_message_t* clientmes, - skygw_message_t* logmes) +static bool filewriter_init(logmanager_t* logmanager, + filewriter_t* fw, + skygw_message_t* clientmes, + skygw_message_t* logmes) { bool succp = false; logfile_t* lf; @@ -2926,8 +2890,7 @@ return_succp: return succp; } -static void filewriter_done( - filewriter_t* fw) +static void filewriter_done(filewriter_t* fw) { int i; logfile_id_t id; @@ -3003,8 +2966,7 @@ static void filewriter_done( * version is even and read is consistent only if version hasn't changed * during the read. */ -static void* thr_filewriter_fun( - void* data) +static void* thr_filewriter_fun(void* data) { skygw_thread_t* thr; filewriter_t* fwr; @@ -3237,8 +3199,7 @@ static void* thr_filewriter_fun( } -static void fnames_conf_done( - fnames_conf_t* fn) +static void fnames_conf_done(fnames_conf_t* fn) { switch (fn->fn_state) { @@ -3255,8 +3216,7 @@ static void fnames_conf_done( } -static void fnames_conf_free_memory( - fnames_conf_t* fn) +static void fnames_conf_free_memory(fnames_conf_t* fn) { if (fn->fn_debug_prefix != NULL) free(fn->fn_debug_prefix); if (fn->fn_debug_suffix!= NULL) free(fn->fn_debug_suffix); @@ -3278,10 +3238,9 @@ static void fnames_conf_free_memory( * * @return the biggest sequence number used */ -static int find_last_seqno( - strpart_t* parts, - int seqno, - int seqnoidx) +static int find_last_seqno(strpart_t* parts, + int seqno, + int seqnoidx) { strpart_t* p; char* snstr; From 20364a132bc27d7df8d8e28b804ca13076e88066 Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Thu, 22 Oct 2015 10:55:20 +0300 Subject: [PATCH 10/15] Whitespace changes. All binary operators surrounded by a space. --- log_manager/log_manager.cc | 114 ++++++++++++++++++------------------- 1 file changed, 57 insertions(+), 57 deletions(-) diff --git a/log_manager/log_manager.cc b/log_manager/log_manager.cc index 55725c5de..2588d3ce3 100644 --- a/log_manager/log_manager.cc +++ b/log_manager/log_manager.cc @@ -129,7 +129,7 @@ struct filewriter flat_obj_state_t fwr_state; logmanager_t* fwr_logmgr; /** Physical files */ - skygw_file_t* fwr_file[LOGFILE_LAST+1]; + skygw_file_t* fwr_file[LOGFILE_LAST + 1]; /** fwr_logmes is for messages from log clients */ skygw_message_t* fwr_logmes; /** fwr_clientmes is for messages to log clients */ @@ -237,7 +237,7 @@ struct logmanager /** fwr_clientmes is for messages to log clients */ skygw_message_t* lm_clientmes; fnames_conf_t lm_fnames_conf; - logfile_t lm_logfile[LOGFILE_LAST+1]; + logfile_t lm_logfile[LOGFILE_LAST + 1]; filewriter_t lm_filewriter; #if defined(SS_DEBUG) skygw_chk_t lm_chk_tail; @@ -540,7 +540,7 @@ static void logmanager_done_nomutex(void) /** Free filewriter memory. */ filewriter_done(fwr); - for (i=LOGFILE_FIRST; i<=LOGFILE_LAST; i++) + for (i = LOGFILE_FIRST; i <= LOGFILE_LAST; i++) { lf = logmanager_get_logfile(lm, (logfile_id_t)i); /** Release logfile memory */ @@ -679,7 +679,7 @@ static int logmanager_write_log(logfile_id_t id, false, false, false, - strlen(errstr)+1, + strlen(errstr) + 1, errstr, valist); if (err != 0) @@ -723,7 +723,7 @@ static int logmanager_write_log(logfile_id_t id, */ if (id == LOGFILE_TRACE && tls_log_info.li_sesid != 0) { - sesid_str_len = 5*sizeof(char)+get_decimal_len(tls_log_info.li_sesid); + sesid_str_len = 5 * sizeof(char) + get_decimal_len(tls_log_info.li_sesid); } else { @@ -740,13 +740,13 @@ static int logmanager_write_log(logfile_id_t id, cmplen = sesid_str_len > 0 ? sesid_str_len - sizeof(char) : 0; /** Find out how much can be safely written with current block size */ - if (timestamp_len-sizeof(char)+cmplen+str_len > lf->lf_buf_size) + if (timestamp_len - sizeof(char) + cmplen + str_len > lf->lf_buf_size) { safe_str_len = lf->lf_buf_size; } else { - safe_str_len = timestamp_len-sizeof(char)+cmplen+str_len; + safe_str_len = timestamp_len - sizeof(char) + cmplen + str_len; } /** * Seek write position and register to block buffer. @@ -755,15 +755,15 @@ static int logmanager_write_log(logfile_id_t id, #if defined (SS_LOG_DEBUG) { - char *copy,*tok; + char *copy, *tok; int tokval; - simple_mutex_lock(&msg_mutex,true); + simple_mutex_lock(&msg_mutex, true); copy = strdup(str); - tok = strtok(copy,"|"); - tok = strtok(NULL,"|"); + tok = strtok(copy, "|"); + tok = strtok(NULL, "|"); - if (strstr(str,"message|") && tok) + if (strstr(str, "message|") && tok) { tokval = atoi(tok); @@ -787,7 +787,7 @@ static int logmanager_write_log(logfile_id_t id, } else { - wp = (char*)malloc(sizeof(char)*(timestamp_len-sizeof(char)+cmplen+str_len + 1)); + wp = (char*)malloc(sizeof(char) * (timestamp_len - sizeof(char) + cmplen + str_len + 1)); } if (wp == NULL) @@ -797,7 +797,7 @@ static int logmanager_write_log(logfile_id_t id, #if defined (SS_LOG_DEBUG) { - sprintf(wp,"[msg:%d]",atomic_add(&write_index,1)); + sprintf(wp, "[msg:%d]", atomic_add(&write_index, 1)); safe_str_len -= strlen(wp); wp += strlen(wp); } @@ -820,7 +820,7 @@ static int logmanager_write_log(logfile_id_t id, /** * Write session id */ - snprintf(wp+timestamp_len, + snprintf(wp + timestamp_len, sesid_str_len, "[%lu] ", tls_log_info.li_sesid); @@ -832,14 +832,14 @@ static int logmanager_write_log(logfile_id_t id, */ if (use_valist) { - vsnprintf(wp+timestamp_len+sesid_str_len, + vsnprintf(wp + timestamp_len + sesid_str_len, safe_str_len-timestamp_len-sesid_str_len, str, valist); } else { - snprintf(wp+timestamp_len+sesid_str_len, + snprintf(wp + timestamp_len + sesid_str_len, safe_str_len-timestamp_len-sesid_str_len, "%s", str); @@ -851,11 +851,11 @@ static int logmanager_write_log(logfile_id_t id, switch(id) { case LOGFILE_ERROR: - syslog(LOG_ERR, "%s", wp+timestamp_len); + syslog(LOG_ERR, "%s", wp + timestamp_len); break; case LOGFILE_MESSAGE: - syslog(LOG_NOTICE, "%s", wp+timestamp_len); + syslog(LOG_NOTICE, "%s", wp + timestamp_len); break; default: @@ -863,11 +863,11 @@ static int logmanager_write_log(logfile_id_t id, } } /** remove double line feed */ - if (wp[safe_str_len-2] == '\n') + if (wp[safe_str_len - 2] == '\n') { - wp[safe_str_len-2]=' '; + wp[safe_str_len - 2] = ' '; } - wp[safe_str_len-1] = '\n'; + wp[safe_str_len - 1] = '\n'; if (do_maxscalelog) { @@ -895,7 +895,7 @@ static int logmanager_write_log(logfile_id_t id, * message, trace and debuglog. Messages will be * written in trace and debug log. */ - for (i=(id<<1); i<=LOGFILE_LAST; i<<=1) + for (i = (id << 1); i <= LOGFILE_LAST; i <<= 1) { /** pointer to write buffer of larger-id log */ char* wp_c; @@ -913,20 +913,20 @@ static int logmanager_write_log(logfile_id_t id, wp_c = blockbuf_get_writepos( &bb_c, (logfile_id_t)i, - timestamp_len-1+str_len, + timestamp_len - 1 + str_len, flush); /** * Copy original string from block buffer to * other logs' block buffers. */ - snprintf(wp_c, timestamp_len+str_len, "%s", wp); + snprintf(wp_c, timestamp_len + str_len, "%s", wp); /** remove double line feed */ - if (wp_c[timestamp_len-1+str_len-2] == '\n') + if (wp_c[timestamp_len - 1 + str_len - 2] == '\n') { - wp_c[timestamp_len-1+str_len-2]=' '; + wp_c[timestamp_len - 1 + str_len - 2] = ' '; } - wp_c[timestamp_len-1+str_len-1]='\n'; + wp_c[timestamp_len - 1 + str_len - 1] = '\n'; /** lock-free unregistration, includes flush if * bb_state == BB_FULL */ @@ -1088,7 +1088,7 @@ static char* blockbuf_get_writepos(blockbuf_t** p_bb, * update. */ bb_list->mlist_versno += 1; - ss_dassert(bb_list->mlist_versno%2 == 1); + ss_dassert(bb_list->mlist_versno % 2 == 1); ss_debug(succp =) mlist_add_data_nomutex(bb_list, bb); @@ -1098,7 +1098,7 @@ static char* blockbuf_get_writepos(blockbuf_t** p_bb, * Increase version to even to mark completion of update. */ bb_list->mlist_versno += 1; - ss_dassert(bb_list->mlist_versno%2 == 0); + ss_dassert(bb_list->mlist_versno % 2 == 0); } else { @@ -1129,7 +1129,8 @@ static char* blockbuf_get_writepos(blockbuf_t** p_bb, { if (bb_list->mlist_nodecount > 1 && - node != bb_list->mlist_last){ + node != bb_list->mlist_last) + { bb_list->mlist_last->mlnode_next = bb_list->mlist_first; bb_list->mlist_first = bb_list->mlist_first->mlnode_next; bb_list->mlist_last->mlnode_next->mlnode_next = NULL; @@ -1190,7 +1191,7 @@ static char* blockbuf_get_writepos(blockbuf_t** p_bb, * Increase version to odd to mark list update active update. */ bb_list->mlist_versno += 1; - ss_dassert(bb_list->mlist_versno%2 == 1); + ss_dassert(bb_list->mlist_versno % 2 == 1); ss_debug(succp =)mlist_add_data_nomutex(bb_list, bb); ss_dassert(succp); @@ -1199,7 +1200,7 @@ static char* blockbuf_get_writepos(blockbuf_t** p_bb, * Increase version to even to mark completion of update. */ bb_list->mlist_versno += 1; - ss_dassert(bb_list->mlist_versno%2 == 0); + ss_dassert(bb_list->mlist_versno % 2 == 0); /** Unlock list */ simple_mutex_unlock(&bb_list->mlist_mutex); @@ -1374,7 +1375,7 @@ static bool logfile_set_enabled( false, false, false, - strlen(errstr)+1, + strlen(errstr) + 1, errstr, notused); if (err != 0) @@ -1406,7 +1407,7 @@ static bool logfile_set_enabled( false, false, false, - strlen(logstr)+1, + strlen(logstr) + 1, logstr, notused); free(logstr); @@ -1952,7 +1953,7 @@ static bool fnames_conf_init(fnames_conf_t* fn, syslog_ident_str); } /* ss_dfprintf(stderr, "\n\n\tCommand line : "); - for (i=0; ilf_full_link_name = form_full_file_name(spart,lf,2); + lf->lf_full_link_name = form_full_file_name(spart, lf, 2); } /** * At least one of the files couldn't be created. Increase @@ -2430,7 +2431,7 @@ static char* form_full_file_name(strpart_t* parts, seqno = lf->lf_name_seqno; s = UINTLEN(seqno); - seqnostr = (char *)malloc((int)s+1); + seqnostr = (char *)malloc((int)s + 1); } else { @@ -2474,10 +2475,10 @@ static char* form_full_file_name(strpart_t* parts, if (seqnostr != NULL) { - snprintf(seqnostr, s+1, "%d", seqno); + snprintf(seqnostr, s + 1, "%d", seqno); } - for (i=0, p=parts; p->sp_string != NULL; i++, p=p->sp_next) + for (i = 0, p = parts; p->sp_string != NULL; i++, p = p->sp_next) { if (seqnostr != NULL && i == seqnoidx) { @@ -2523,8 +2524,8 @@ static char* add_slash(char* str) /** Add slash if missing */ if (p[plen-1] != '/') { - str = (char *)malloc(plen+2); - snprintf(str, plen+2, "%s/", p); + str = (char *)malloc(plen + 2); + snprintf(str, plen + 2, "%s/", p); free(p); } return str; @@ -2567,12 +2568,12 @@ static bool check_file_and_path(char* filename, } else { - if (access(filename,F_OK) == 0) + if (access(filename, F_OK) == 0) { exists = true; - if (access(filename,W_OK) == 0) + if (access(filename, W_OK) == 0) { if (writable) { @@ -2677,7 +2678,7 @@ static bool logfile_init(logfile_t* logfile, logfile->lf_name_seqno = 1; logfile->lf_lmgr = logmanager; logfile->lf_flushflag = false; - logfile->lf_rotateflag= false; + logfile->lf_rotateflag = false; logfile->lf_spinlock = 0; logfile->lf_store_shmem = store_shmem; logfile->lf_write_syslog = write_syslog; @@ -2692,7 +2693,7 @@ static bool logfile_init(logfile_t* logfile, { char* c; pid_t pid = getpid(); - int len = strlen(shm_pathname_prefix)+ + int len = strlen(shm_pathname_prefix) + strlen("maxscale.") + get_decimal_len((size_t)pid) + 1; @@ -2863,7 +2864,7 @@ static bool filewriter_init(logmanager_t* logmanager, goto return_succp; } - for (i=LOGFILE_FIRST; i<=LOGFILE_LAST; i <<= 1) + for (i = LOGFILE_FIRST; i <= LOGFILE_LAST; i <<= 1) { id = (logfile_id_t)i; lf = logmanager_get_logfile(logmanager, id); @@ -2902,7 +2903,7 @@ static void filewriter_done(filewriter_t* fw) case INIT: fw->fwr_logmes = NULL; fw->fwr_clientmes = NULL; - for (i=LOGFILE_FIRST; i<=LOGFILE_LAST; i++) + for (i = LOGFILE_FIRST; i <= LOGFILE_LAST; i++) { id = (logfile_id_t)i; if (use_stdout) @@ -3007,7 +3008,7 @@ static void* thr_filewriter_fun(void* data) } /** Process all logfiles which have buffered writes. */ - for (i=LOGFILE_FIRST; i<=LOGFILE_LAST; i <<= 1) + for (i = LOGFILE_FIRST; i <= LOGFILE_LAST; i <<= 1) { retry_flush_on_exit: /** @@ -3137,7 +3138,7 @@ static void* thr_filewriter_fun(void* data) memset(bb->bb_buf, 0, bb->bb_buf_size); bb->bb_state = BB_CLEARED; #if defined(SS_LOG_DEBUG) - sprintf(bb->bb_buf,"[block:%d]",atomic_add(&block_start_index,1)); + sprintf(bb->bb_buf,"[block:%d]", atomic_add(&block_start_index, 1)); bb->bb_buf_used += strlen(bb->bb_buf); bb->bb_buf_left -= strlen(bb->bb_buf); #endif @@ -3149,8 +3150,7 @@ static void* thr_filewriter_fun(void* data) /** Consistent lock-free read on the list */ do { - while ((vn1 = bb_list->mlist_versno)%2 - != 0) + while ((vn1 = bb_list->mlist_versno) % 2 != 0) { continue; } @@ -3259,9 +3259,9 @@ static int find_last_seqno(strpart_t* parts, int i; char filename[NAME_MAX] = {0}; /** Form name with next seqno */ - snprintf(snstr, snstrlen, "%d", seqno+1); + snprintf(snstr, snstrlen, "%d", seqno + 1); - for (i=0, p=parts; p->sp_string != NULL; i++, p=p->sp_next) + for (i = 0, p = parts; p->sp_string != NULL; i++, p = p->sp_next) { if (snstr != NULL && i == seqnoidx) { @@ -3292,7 +3292,7 @@ static int find_last_seqno(strpart_t* parts, bool thr_flushall_check() { bool rval = false; - simple_mutex_lock(&lm->lm_mutex,true); + simple_mutex_lock(&lm->lm_mutex, true); rval = flushall_flag; if (rval && !flushall_started_flag && !flushall_done_flag) { @@ -3304,7 +3304,7 @@ bool thr_flushall_check() void flushall_logfiles(bool flush) { - simple_mutex_lock(&lm->lm_mutex,true); + simple_mutex_lock(&lm->lm_mutex, true); flushall_flag = flush; simple_mutex_unlock(&lm->lm_mutex); } From d93d01b116a96013c0dfdc846a0b72f90a9b2d48 Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Thu, 22 Oct 2015 11:23:25 +0300 Subject: [PATCH 11/15] General tidying General tidying and correction of missed points during earlier cleanup steps. --- log_manager/log_manager.cc | 178 +++++++++++++------------------------ 1 file changed, 62 insertions(+), 116 deletions(-) diff --git a/log_manager/log_manager.cc b/log_manager/log_manager.cc index 2588d3ce3..c6efe0887 100644 --- a/log_manager/log_manager.cc +++ b/log_manager/log_manager.cc @@ -394,8 +394,7 @@ static bool logmanager_init_nomutex(int argc, char* argv[]) lm->lm_clientmes = skygw_message_init(); lm->lm_logmes = skygw_message_init(); - if (lm->lm_clientmes == NULL || - lm->lm_logmes == NULL) + if (lm->lm_clientmes == NULL || lm->lm_logmes == NULL) { err = 1; goto return_succp; @@ -448,9 +447,7 @@ static bool logmanager_init_nomutex(int argc, char* argv[]) } /** Initialize and start filewriter thread */ - fw->fwr_thread = skygw_thread_init("filewriter thr", - thr_filewriter_fun, - (void *)fw); + fw->fwr_thread = skygw_thread_init("filewriter thr", thr_filewriter_fun, (void *)fw); if (fw->fwr_thread == NULL) { @@ -493,7 +490,7 @@ return_succp: */ bool skygw_logmanager_init(int argc, char* argv[]) { - bool succp = false; + bool succp = false; acquire_lock(&lmlock); @@ -529,9 +526,7 @@ static void logmanager_done_nomutex(void) { CHK_FILEWRITER(fwr); /** Inform filewriter thread and wait until it has stopped. */ - skygw_thread_set_exitflag(fwr->fwr_thread, - fwr->fwr_logmes, - fwr->fwr_clientmes); + skygw_thread_set_exitflag(fwr->fwr_thread, fwr->fwr_logmes, fwr->fwr_clientmes); /** Free thread memory */ skygw_thread_done(fwr->fwr_thread); @@ -611,9 +606,7 @@ return_void: release_lock(&lmlock); } -static logfile_t* logmanager_get_logfile( - logmanager_t* lmgr, - logfile_id_t id) +static logfile_t* logmanager_get_logfile(logmanager_t* lmgr, logfile_id_t id) { logfile_t* lf; CHK_LOGMANAGER(lmgr); @@ -780,10 +773,7 @@ static int logmanager_write_log(logfile_id_t id, /** Book space for log string from buffer */ if (do_maxscalelog) { - wp = blockbuf_get_writepos(&bb, - id, - safe_str_len, - flush); + wp = blockbuf_get_writepos(&bb, id, safe_str_len, flush); } else { @@ -820,10 +810,7 @@ static int logmanager_write_log(logfile_id_t id, /** * Write session id */ - snprintf(wp + timestamp_len, - sesid_str_len, - "[%lu] ", - tls_log_info.li_sesid); + snprintf(wp + timestamp_len, sesid_str_len, "[%lu] ", tls_log_info.li_sesid); sesid_str_len -= 1; /*< don't calculate terminating char anymore */ } /** @@ -833,7 +820,7 @@ static int logmanager_write_log(logfile_id_t id, if (use_valist) { vsnprintf(wp + timestamp_len + sesid_str_len, - safe_str_len-timestamp_len-sesid_str_len, + safe_str_len - timestamp_len - sesid_str_len, str, valist); } @@ -848,7 +835,7 @@ static int logmanager_write_log(logfile_id_t id, /** write to syslog */ if (lf->lf_write_syslog) { - switch(id) + switch (id) { case LOGFILE_ERROR: syslog(LOG_ERR, "%s", wp + timestamp_len); @@ -878,7 +865,7 @@ static int logmanager_write_log(logfile_id_t id, free(wp); } /** - * disable because cross-blockbuffer locking either causes deadlock + * disabled because cross-blockbuffer locking either causes deadlock * or run out of memory blocks. */ if (spread_down && false) @@ -910,11 +897,10 @@ static int logmanager_write_log(logfile_id_t id, * buffer. Then print formatted string to * write position. */ - wp_c = blockbuf_get_writepos( - &bb_c, - (logfile_id_t)i, - timestamp_len - 1 + str_len, - flush); + wp_c = blockbuf_get_writepos(&bb_c, + (logfile_id_t)i, + timestamp_len - 1 + str_len, + flush); /** * Copy original string from block buffer to * other logs' block buffers. @@ -1006,9 +992,11 @@ static char* blockbuf_get_writepos(blockbuf_t** p_bb, char* pos = NULL; mlist_node_t* node; blockbuf_t* bb; - ss_debug(bool succp;) +#if defined(SS_DEBUG) + bool succp; +#endif - CHK_LOGMANAGER(lm); + CHK_LOGMANAGER(lm); lf = &lm->lm_logfile[id]; CHK_LOGFILE(lf); bb_list = &lf->lf_blockbuf_list; @@ -1070,8 +1058,7 @@ static char* blockbuf_get_writepos(blockbuf_t** p_bb, /** * All buffers on the list are full. */ - if (bb_list->mlist_nodecount < - bb_list->mlist_nodecount_max) + if (bb_list->mlist_nodecount < bb_list->mlist_nodecount_max) { /** * New node is created @@ -1090,8 +1077,7 @@ static char* blockbuf_get_writepos(blockbuf_t** p_bb, bb_list->mlist_versno += 1; ss_dassert(bb_list->mlist_versno % 2 == 1); - ss_debug(succp =) - mlist_add_data_nomutex(bb_list, bb); + ss_debug(succp =) mlist_add_data_nomutex(bb_list, bb); ss_dassert(succp); /** @@ -1128,8 +1114,7 @@ static char* blockbuf_get_writepos(blockbuf_t** p_bb, if (node == bb_list->mlist_first) { - if (bb_list->mlist_nodecount > 1 && - node != bb_list->mlist_last) + if ((bb_list->mlist_nodecount > 1) && (node != bb_list->mlist_last)) { bb_list->mlist_last->mlnode_next = bb_list->mlist_first; bb_list->mlist_first = bb_list->mlist_first->mlnode_next; @@ -1193,7 +1178,7 @@ static char* blockbuf_get_writepos(blockbuf_t** p_bb, bb_list->mlist_versno += 1; ss_dassert(bb_list->mlist_versno % 2 == 1); - ss_debug(succp =)mlist_add_data_nomutex(bb_list, bb); + ss_debug(succp =) mlist_add_data_nomutex(bb_list, bb); ss_dassert(succp); /** @@ -1226,8 +1211,7 @@ static char* blockbuf_get_writepos(blockbuf_t** p_bb, bb->bb_buf_used += str_len; bb->bb_buf_left -= str_len; - ss_dassert(pos >= &bb->bb_buf[0] && - pos <= &bb->bb_buf[MAX_LOGSTRLEN-str_len]); + ss_dassert((pos >= &bb->bb_buf[0]) && (pos <= &bb->bb_buf[MAX_LOGSTRLEN-str_len])); /** read checkmark */ /** TODO: add buffer overflow checkmark @@ -1323,9 +1307,7 @@ int skygw_log_disable(logfile_id_t id) /*< no locking */ return rc; } -static int skygw_log_disable_raw( - logfile_id_t id, - bool emergency) /*< no locking */ +static int skygw_log_disable_raw(logfile_id_t id, bool emergency) /*< no locking */ { bool err = 0; @@ -1351,9 +1333,7 @@ return_err: } -static bool logfile_set_enabled( - logfile_id_t id, - bool val) +static bool logfile_set_enabled(logfile_id_t id, bool val) { char* logstr; va_list notused; @@ -1665,36 +1645,32 @@ int skygw_log_rotate(logfile_id_t id) if (!logmanager_register(false)) { - ss_dfprintf(stderr, - "Can't register to logmanager, rotating failed\n"); + ss_dfprintf(stderr, "Can't register to logmanager, rotating failed\n"); goto return_err; } CHK_LOGMANAGER(lm); lf = &lm->lm_logfile[id]; - LOGIF(LM, (skygw_log_write( - LOGFILE_MESSAGE, - "Log rotation is called for %s.", - lf->lf_full_file_name))); + LOGIF(LM, (skygw_log_write(LOGFILE_MESSAGE, + "Log rotation is called for %s.", + lf->lf_full_file_name))); err = logmanager_write_log(id, false, false, false, true, 0, NULL, valist); if (err != 0) { - LOGIF(LE, (skygw_log_write( - LOGFILE_ERROR, - "Log file rotation failed for file %s.", - lf->lf_full_file_name))); + LOGIF(LE, (skygw_log_write(LOGFILE_ERROR, + "Log file rotation failed for file %s.", + lf->lf_full_file_name))); fprintf(stderr, "skygw_log_rotate failed.\n"); goto return_unregister; } return_unregister: - LOGIF(LM, (skygw_log_write_flush( - LOGFILE_MESSAGE, - "File %s use for log writing..", - lf->lf_full_file_name))); + LOGIF(LM, (skygw_log_write_flush(LOGFILE_MESSAGE, + "File %s use for log writing..", + lf->lf_full_file_name))); logmanager_unregister(); @@ -1917,9 +1893,7 @@ static bool fnames_conf_init(fnames_conf_t* fn, break; case 'h': default: - fprintf(stderr, - "\nSupported arguments are (default)\n%s\n", - argstr); + fprintf(stderr, "\nSupported arguments are (default)\n%s\n", argstr); goto return_conf_init; } /** switch (opt) */ } @@ -1948,8 +1922,7 @@ static bool fnames_conf_init(fnames_conf_t* fn, { syslog_ident_str = (syslog_ident_str == NULL ? - (argv == NULL ? strdup(program_invocation_short_name) : - strdup(*argv)) : + (argv == NULL ? strdup(program_invocation_short_name) : strdup(*argv)) : syslog_ident_str); } /* ss_dfprintf(stderr, "\n\n\tCommand line : "); @@ -1991,8 +1964,7 @@ return_conf_init: } -static char* fname_conf_get_prefix(fnames_conf_t* fn, - logfile_id_t id) +static char* fname_conf_get_prefix(fnames_conf_t* fn, logfile_id_t id) { CHK_FNAMES_CONF(fn); ss_dassert(id >= LOGFILE_FIRST && id <= LOGFILE_LAST); @@ -2020,8 +1992,7 @@ static char* fname_conf_get_prefix(fnames_conf_t* fn, } } -static char* fname_conf_get_suffix(fnames_conf_t* fn, - logfile_id_t id) +static char* fname_conf_get_suffix(fnames_conf_t* fn, logfile_id_t id) { CHK_FNAMES_CONF(fn); ss_dassert(id >= LOGFILE_FIRST && id <= LOGFILE_LAST); @@ -2068,8 +2039,7 @@ static char* fname_conf_get_suffix(fnames_conf_t* fn, * that writing it to disk slows execution down remarkably. * */ -static bool logfiles_init( - logmanager_t* lm) +static bool logfiles_init(logmanager_t* lm) { bool succp = true; int lid = LOGFILE_FIRST; @@ -2092,8 +2062,7 @@ static bool logfiles_init( * a symbolic link will be created to log directory. */ if (shmem_id_str != NULL && - strcasestr(shmem_id_str, - STRLOGID(logfile_id_t(lid))) != NULL) + strcasestr(shmem_id_str, STRLOGID(logfile_id_t(lid))) != NULL) { store_shmem = true; } @@ -2105,8 +2074,7 @@ static bool logfiles_init( * Check if file is also written to syslog. */ if (syslog_id_str != NULL && - strcasestr(syslog_id_str, - STRLOGID(logfile_id_t(lid))) != NULL) + strcasestr(syslog_id_str, STRLOGID(logfile_id_t(lid))) != NULL) { write_syslog = true; } @@ -2202,8 +2170,7 @@ static bool logfile_create(logfile_t* lf) * Create name for log file. Seqno is added between prefix & * suffix (index == 2) */ - lf->lf_full_file_name = - form_full_file_name(spart, lf, 2); + lf->lf_full_file_name = form_full_file_name(spart, lf, 2); if (store_shmem) { @@ -2310,8 +2277,7 @@ return_succp: * false if failed. * */ -static bool logfile_open_file(filewriter_t* fw, - logfile_t* lf) +static bool logfile_open_file(filewriter_t* fw, logfile_t* lf) { bool succp; char* start_msg_str; @@ -2319,30 +2285,23 @@ static bool logfile_open_file(filewriter_t* fw, if (use_stdout) { - fw->fwr_file[lf->lf_id] = skygw_file_alloc ( - lf->lf_full_file_name); + fw->fwr_file[lf->lf_id] = skygw_file_alloc(lf->lf_full_file_name); fw->fwr_file[lf->lf_id]->sf_file = stdout; } else if (lf->lf_store_shmem) { /** Create symlink pointing to log file */ - fw->fwr_file[lf->lf_id] = skygw_file_init( - lf->lf_full_file_name, - lf->lf_full_link_name); + fw->fwr_file[lf->lf_id] = skygw_file_init(lf->lf_full_file_name, lf->lf_full_link_name); } else { /** Create normal disk-resident log file */ - fw->fwr_file[lf->lf_id] = skygw_file_init( - lf->lf_full_file_name, - NULL); + fw->fwr_file[lf->lf_id] = skygw_file_init(lf->lf_full_file_name, NULL); } if (fw->fwr_file[lf->lf_id] == NULL) { - fprintf(stderr, - "Error : opening logfile %s failed.\n", - lf->lf_full_file_name); + fprintf(stderr, "Error : opening logfile %s failed.\n", lf->lf_full_file_name); succp = false; goto return_succp; } @@ -2400,9 +2359,7 @@ return_succp: * @return Pointer to filename, of NULL if failed. * */ -static char* form_full_file_name(strpart_t* parts, - logfile_t* lf, - int seqnoidx) +static char* form_full_file_name(strpart_t* parts, logfile_t* lf, int seqnoidx) { int i; int seqno; @@ -2522,7 +2479,7 @@ static char* add_slash(char* str) size_t plen = strlen(p); /** Add slash if missing */ - if (p[plen-1] != '/') + if (p[plen - 1] != '/') { str = (char *)malloc(plen + 2); snprintf(str, plen + 2, "%s/", p); @@ -2551,9 +2508,7 @@ static char* add_slash(char* str) * TODO: recall what was the reason for not succeeding with simply * calling access, and fstat. vraa 26.11.13 */ -static bool check_file_and_path(char* filename, - bool* writable, - bool do_log) +static bool check_file_and_path(char* filename, bool* writable, bool do_log) { bool exists; @@ -2792,7 +2747,7 @@ return_with_succp: */ static void logfile_done(logfile_t* lf) { - switch(lf->lf_state) + switch (lf->lf_state) { case RUN: CHK_LOGFILE(lf); @@ -2896,7 +2851,7 @@ static void filewriter_done(filewriter_t* fw) int i; logfile_id_t id; - switch(fw->fwr_state) + switch (fw->fwr_state) { case RUN: CHK_FILEWRITER(fw); @@ -2978,9 +2933,9 @@ static void* thr_filewriter_fun(void* data) blockbuf_t* bb; mlist_node_t* node; int i; - blockbuf_state_t flush_blockbuf; /**< flush single block buffer. */ + blockbuf_state_t flush_blockbuf; /**< flush single block buffer. */ bool flush_logfile; /**< flush logfile */ - bool do_flushall = false; + bool do_flushall = false; bool rotate_logfile; /*< close current and open new file */ size_t vn1; size_t vn2; @@ -3015,8 +2970,6 @@ static void* thr_filewriter_fun(void* data) * Get file pointer of current logfile. */ - - do_flushall = thr_flushall_check(); file = fwr->fwr_file[i]; lf = &lm->lm_logfile[(logfile_id_t)i]; @@ -3093,9 +3046,7 @@ static void* thr_filewriter_fun(void* data) flush_blockbuf = bb->bb_state; if (bb->bb_buf_used != 0 && - (flush_blockbuf == BB_FULL || - flush_logfile || - do_flushall)) + ((flush_blockbuf == BB_FULL) || flush_logfile || do_flushall)) { /** * buffer is at least half-full @@ -3103,18 +3054,13 @@ static void* thr_filewriter_fun(void* data) */ while (bb->bb_refcount > 0) { - simple_mutex_unlock( - &bb->bb_mutex); - simple_mutex_lock( - &bb->bb_mutex, - true); + simple_mutex_unlock(&bb->bb_mutex); + simple_mutex_lock(&bb->bb_mutex, true); } - err = skygw_file_write( - file, - (void *)bb->bb_buf, - bb->bb_buf_used, - (flush_logfile || - do_flushall)); + err = skygw_file_write(file, + (void *)bb->bb_buf, + bb->bb_buf_used, + (flush_logfile || do_flushall)); if (err) { char errbuf[STRERROR_BUFLEN]; From 37e4d5f3cbae298e33786c3a3dc80c9230530c34 Mon Sep 17 00:00:00 2001 From: Markus Makela Date: Thu, 22 Oct 2015 15:24:16 +0300 Subject: [PATCH 12/15] Updated configuration template with links to documentation. --- server/maxscale_template.cnf | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/server/maxscale_template.cnf b/server/maxscale_template.cnf index 5824a4892..e439ebe13 100644 --- a/server/maxscale_template.cnf +++ b/server/maxscale_template.cnf @@ -1,8 +1,11 @@ +# MaxScale documentation on GitHub: +# https://github.com/mariadb-corporation/MaxScale/blob/master/Documentation/Documentation-Contents.md + # Global parameters # -# Set the number of threads to a value -# equal to the number of CPU cores. -# +# Set the number of threads to a value equal to the number of CPU cores. +# Complete list of configuration options: +# https://github.com/mariadb-corporation/MaxScale/blob/master/Documentation/Getting-Started/Configuration-Guide.md [maxscale] threads=4 @@ -22,7 +25,8 @@ protocol=MySQLBackend # Monitor for the servers # # This will keep MaxScale aware of the state of the servers. -# +# MySQL Monitor documentation: +# https://github.com/mariadb-corporation/MaxScale/blob/master/Documentation/Monitors/MySQL-Monitor.md [MySQL Monitor] type=monitor @@ -38,6 +42,9 @@ monitor_interval=10000 # a read/write splitting service. # +# ReadConnRoute documentation: +# https://github.com/mariadb-corporation/MaxScale/blob/master/Documentation/Routers/ReadConnRoute.md + [Read-Only Service] type=service router=readconnroute @@ -46,6 +53,9 @@ user=myuser passwd=mypwd router_options=slave +# ReadWriteSplit documentation: +# https://github.com/mariadb-corporation/MaxScale/blob/master/Documentation/Routers/ReadWriteSplit.md + [Read-Write Service] type=service router=readwritesplit @@ -55,6 +65,9 @@ passwd=mypwd max_slave_connections=100% # This service enables the use of the MaxAdmin interface +# MaxScale administration guide: +# https://github.com/mariadb-corporation/MaxScale/blob/master/Documentation/Reference/MaxAdmin.md + [MaxAdmin Service] type=service router=cli From de7fbe3271acd782f8eb020363b43be234ccdc19 Mon Sep 17 00:00:00 2001 From: Markus Makela Date: Thu, 15 Oct 2015 22:23:59 +0300 Subject: [PATCH 13/15] Cleaned up the readwritesplit documentation. --- Documentation/Routers/ReadWriteSplit.md | 61 ++++++++++++++++++++----- 1 file changed, 49 insertions(+), 12 deletions(-) diff --git a/Documentation/Routers/ReadWriteSplit.md b/Documentation/Routers/ReadWriteSplit.md index ec89db65f..c6e5f456f 100644 --- a/Documentation/Routers/ReadWriteSplit.md +++ b/Documentation/Routers/ReadWriteSplit.md @@ -16,22 +16,31 @@ The configuration consists of mandatory and optional parameters. ## Mandatory parameters +### `type` + **`type`** specifies the type of service. For **readwritesplit** module the type is `router`: type=router +### `router` + **`router`** specifies the router module to be used. For **readwritesplit** the value is `readwritesplit`: router=readwritesplit +### `servers` + **`servers`** provides a list of servers, which must include one master and available slaves: servers=server1,server2,server3 **NOTE: Each server on the list must have its own section in the configuration file where it is defined.** +### `user` **`user`** is the username the router session uses for accessing backends in order to load the content of the `mysql.user` table (and `mysql.db` and database names as well) and optionally for creating, and using `maxscale_schema.replication_heartbeat` table. +### `passwd` + **`passwd`** specifies corresponding password for the user. Syntax for user and passwd is: ``` @@ -41,10 +50,13 @@ passwd= ## Optional parameters +### `max_slave_connections` + **`max_slave_connections`** sets the maximum number of slaves a router session uses at any moment. Default value is `1`. max_slave_connections= +### `max_slave_replication_lag` **`max_slave_replication_lag`** specifies how many seconds a slave is allowed to be behind the master. If the lag is bigger than configured value a slave can't be used for routing. max_slave_replication_lag= @@ -52,16 +64,8 @@ passwd= This applies to Master/Slave replication with MySQL monitor and `detect_replication_lag=1` options set. Please note max_slave_replication_lag must be greater than monitor interval. -**`router_options`** may include multiple **readwritesplit**-specific options. Values are either singular or parameter-value pairs. Currently available is a single option which specifies the criteria used in slave selection both in initialization of router session and per each query. Note that due to the current monitor implementation, the value specified here should be ** + 1. - options=slave_selection_criteria= - -where ** is one of the following: - -* `LEAST_GLOBAL_CONNECTIONS`, the slave with least connections in total -* `LEAST_ROUTER_CONNECTIONS`, the slave with least connections from this router -* `LEAST_BEHIND_MASTER`, the slave with smallest replication lag -* `LEAST_CURRENT_OPERATIONS` (default), the slave with least active operations +### `use_sql_variables_in` **`use_sql_variables_in`** specifies where should queries, which read session variable, be routed. The syntax for `use_sql_variable_in` is: @@ -73,6 +77,35 @@ When value all is used, queries reading session variables can be routed to any a In above-mentioned case the user-defined variable would only be updated in the master where query would be routed due to `INSERT` statement. +## Router options + +**`router_options`** may include multiple **readwritesplit**-specific options. All the options are parameter-value pairs. All parameters listed in this section must be configured as a value in `router_options`. + +Multiple options can be defined as a comma-separated list of parameter-value pairs. + +``` +router_options=