Partial merge of MXS-70

This commit is contained in:
Markus Makela
2015-08-10 17:05:52 +03:00
parent 7cfb83554f
commit 0abcf349ff
5 changed files with 132 additions and 55 deletions

View File

@ -2480,3 +2480,12 @@ void config_add_param(CONFIG_CONTEXT* obj, char* key,char* value)
nptr->next = obj->parameters; nptr->next = obj->parameters;
obj->parameters = nptr; obj->parameters = nptr;
} }
/**
* Return the pointer to the global options for MaxScale.
* @return Pointer to the GATEWAY_CONF structure. This is a static structure and
* should not be modified.
*/
GATEWAY_CONF* config_get_global_options()
{
return &gateway;
}

View File

@ -36,7 +36,7 @@
* Put example code behind SS_DEBUG macros. * Put example code behind SS_DEBUG macros.
* 05/02/14 Mark Riddoch Addition of version string * 05/02/14 Mark Riddoch Addition of version string
* 29/06/14 Massimiliano Pinto Addition of pidfile * 29/06/14 Massimiliano Pinto Addition of pidfile
* * 10/08/15 Markus Makela Added configurable directory locations
* @endverbatim * @endverbatim
*/ */
#define _XOPEN_SOURCE 700 #define _XOPEN_SOURCE 700
@ -178,7 +178,7 @@ static struct option long_options[] = {
static int cnf_preparser(void* data, const char* section, const char* name, const char* value); static int cnf_preparser(void* data, const char* section, const char* name, const char* value);
static void log_flush_shutdown(void); static void log_flush_shutdown(void);
static void log_flush_cb(void* arg); static void log_flush_cb(void* arg);
static int write_pid_file(char *); /* write MaxScale pidfile */ static int write_pid_file(); /* write MaxScale pidfile */
static void unlink_pidfile(void); /* remove pidfile */ static void unlink_pidfile(void); /* remove pidfile */
static void libmysqld_done(void); static void libmysqld_done(void);
static bool file_write_header(FILE* outfile); static bool file_write_header(FILE* outfile);
@ -978,7 +978,7 @@ return_cnf_file_buf:
static void usage(void) static void usage(void)
{ {
fprintf(stderr, fprintf(stderr,
"\nUsage : %s [OPTION]...\n\n" "\nUsage : %s [OPTION]...\n\n"
" -d, --nodaemon enable running in terminal process (default:disabled)\n" " -d, --nodaemon enable running in terminal process (default:disabled)\n"
" -f, --config=FILE relative|absolute pathname of MaxScale configuration file\n" " -f, --config=FILE relative|absolute pathname of MaxScale configuration file\n"
" (default:/etc/maxscale.cnf)\n" " (default:/etc/maxscale.cnf)\n"
@ -993,6 +993,8 @@ static void usage(void)
" (default: /etc/)\n" " (default: /etc/)\n"
" -D, --datadir=PATH path to data directory, stored embedded mysql tables\n" " -D, --datadir=PATH path to data directory, stored embedded mysql tables\n"
" (default: /var/cache/maxscale)\n" " (default: /var/cache/maxscale)\n"
" -N, --language=PATH apth to errmsg.sys file\n"
" (default: /var/lib/maxscale)\n"
" -P, --piddir=PATH path to PID file directory\n" " -P, --piddir=PATH path to PID file directory\n"
" (default: /var/run/maxscale)\n" " (default: /var/run/maxscale)\n"
" -U, --user=USER run MaxScale as another user.\n" " -U, --user=USER run MaxScale as another user.\n"
@ -1049,7 +1051,6 @@ int main(int argc, char **argv)
char mysql_home[PATH_MAX+1]; char mysql_home[PATH_MAX+1];
char datadir_arg[10+PATH_MAX+1]; /*< '--datadir=' + PATH_MAX */ char datadir_arg[10+PATH_MAX+1]; /*< '--datadir=' + PATH_MAX */
char language_arg[11+PATH_MAX+1]; /*< '--language=' + PATH_MAX */ char language_arg[11+PATH_MAX+1]; /*< '--language=' + PATH_MAX */
char* home_dir = NULL; /*< home dir, to be freed */
char* cnf_file_path = NULL; /*< conf file, to be freed */ char* cnf_file_path = NULL; /*< conf file, to be freed */
char* cnf_file_arg = NULL; /*< conf filename from cmd-line arg */ char* cnf_file_arg = NULL; /*< conf filename from cmd-line arg */
void* log_flush_thr = NULL; void* log_flush_thr = NULL;
@ -1057,8 +1058,8 @@ int main(int argc, char **argv)
char* tmp_var; char* tmp_var;
int option_index; int option_index;
int logtofile = 0; /* Use shared memory or file */ int logtofile = 0; /* Use shared memory or file */
int syslog_enabled = 0; /** Log to syslog */ int *syslog_enabled = &config_get_global_options()->syslog; /** Log to syslog */
int maxscalelog_enabled = 1; /** Log with MaxScale */ int *maxscalelog_enabled = &config_get_global_options()->maxlog; /** Log with MaxScale */
ssize_t log_flush_timeout_ms = 0; ssize_t log_flush_timeout_ms = 0;
sigset_t sigset; sigset_t sigset;
sigset_t sigpipe_mask; sigset_t sigpipe_mask;
@ -1068,7 +1069,8 @@ int main(int argc, char **argv)
write_footer, write_footer,
NULL}; NULL};
*syslog_enabled = 0;
*maxscalelog_enabled = 1;
sigemptyset(&sigpipe_mask); sigemptyset(&sigpipe_mask);
sigaddset(&sigpipe_mask, SIGPIPE); sigaddset(&sigpipe_mask, SIGPIPE);
@ -1160,20 +1162,20 @@ int main(int argc, char **argv)
if(handle_path_arg(&tmp_path,optarg,NULL,true,false)) if(handle_path_arg(&tmp_path,optarg,NULL,true,false))
{ {
logdir = tmp_path; set_logdir(tmp_path);
} }
break; break;
case 'N': case 'N':
if(handle_path_arg(&tmp_path,optarg,NULL,true,false)) if(handle_path_arg(&tmp_path,optarg,NULL,true,false))
{ {
langdir = tmp_path; set_langdir(tmp_path);
} }
break; break;
case 'P': case 'P':
if(handle_path_arg(&tmp_path,optarg,NULL,true,true)) if(handle_path_arg(&tmp_path,optarg,NULL,true,true))
{ {
piddir = tmp_path; set_piddir(tmp_path);
} }
break; break;
case 'D': case 'D':
@ -1184,7 +1186,7 @@ int main(int argc, char **argv)
case 'C': case 'C':
if(handle_path_arg(&tmp_path,optarg,NULL,true,false)) if(handle_path_arg(&tmp_path,optarg,NULL,true,false))
{ {
configdir = tmp_path; set_configdir(tmp_path);
} }
break; break;
case 'B': case 'B':
@ -1206,11 +1208,11 @@ int main(int argc, char **argv)
{ {
tok++; tok++;
if(tok) if(tok)
maxscalelog_enabled = config_truth_value(tok); *maxscalelog_enabled = config_truth_value(tok);
} }
else else
{ {
maxscalelog_enabled = config_truth_value(optarg); *maxscalelog_enabled = config_truth_value(optarg);
} }
} }
break; break;
@ -1221,11 +1223,11 @@ int main(int argc, char **argv)
{ {
tok++; tok++;
if(tok) if(tok)
syslog_enabled = config_truth_value(tok); *syslog_enabled = config_truth_value(tok);
} }
else else
{ {
syslog_enabled = config_truth_value(optarg); *syslog_enabled = config_truth_value(optarg);
} }
} }
break; break;
@ -1539,7 +1541,7 @@ int main(int argc, char **argv)
* read accessibility. * read accessibility.
*/ */
char pathbuf[PATH_MAX+1]; char pathbuf[PATH_MAX+1];
snprintf(pathbuf,PATH_MAX,"%s",configdir ? configdir:default_configdir); snprintf(pathbuf,PATH_MAX,"%s",get_configdir());
if(pathbuf[strlen(pathbuf)-1] != '/') if(pathbuf[strlen(pathbuf)-1] != '/')
strcat(pathbuf,"/"); strcat(pathbuf,"/");
@ -1569,34 +1571,29 @@ int main(int argc, char **argv)
char *argv[8]; char *argv[8];
bool succp; bool succp;
/** Use default log directory /var/log/maxscale/ */ if(mkdir(get_logdir(),0777) != 0 && errno != EEXIST)
if(logdir == NULL)
{ {
if(mkdir(default_logdir,0777) != 0 && errno != EEXIST) fprintf(stderr,
{ "Error: Cannot create log directory: %s\n",
fprintf(stderr, default_logdir);
"Error: Cannot create log directory: %s\n", goto return_main;
default_logdir);
goto return_main;
}
logdir = strdup(default_logdir);
} }
argv[0] = "MaxScale"; argv[0] = "MaxScale";
argv[1] = "-j"; argv[1] = "-j";
argv[2] = logdir; argv[2] = get_logdir();
if(!syslog_enabled) if(!(*syslog_enabled))
{ {
printf("Syslog logging is disabled.\n"); printf("Syslog logging is disabled.\n");
} }
if(!maxscalelog_enabled) if(!(*maxscalelog_enabled))
{ {
printf("MaxScale logging is disabled.\n"); printf("MaxScale logging is disabled.\n");
} }
logmanager_enable_syslog(syslog_enabled); logmanager_enable_syslog(*syslog_enabled);
logmanager_enable_maxscalelog(maxscalelog_enabled); logmanager_enable_maxscalelog(*maxscalelog_enabled);
if (logtofile) if (logtofile)
{ {
@ -1662,8 +1659,8 @@ int main(int argc, char **argv)
"Module directory : %s\n" "Module directory : %s\n"
"Service cache : %s\n\n", "Service cache : %s\n\n",
cnf_file_path, cnf_file_path,
logdir, get_logdir(),
datadir, get_datadir(),
get_libdir(), get_libdir(),
get_cachedir()); get_cachedir());
} }
@ -1677,12 +1674,12 @@ int main(int argc, char **argv)
(skygw_log_write_flush( (skygw_log_write_flush(
LOGFILE_MESSAGE, LOGFILE_MESSAGE,
"Log directory: %s/", "Log directory: %s/",
logdir))); get_logdir())));
LOGIF(LM, LOGIF(LM,
(skygw_log_write_flush( (skygw_log_write_flush(
LOGFILE_MESSAGE, LOGFILE_MESSAGE,
"Data directory: %s", "Data directory: %s",
datadir))); get_datadir())));
LOGIF(LM, LOGIF(LM,
(skygw_log_write_flush(LOGFILE_MESSAGE, (skygw_log_write_flush(LOGFILE_MESSAGE,
"Module directory: %s", "Module directory: %s",
@ -1705,7 +1702,7 @@ int main(int argc, char **argv)
snprintf(language_arg, snprintf(language_arg,
11+PATH_MAX+1, 11+PATH_MAX+1,
"--language=%s", "--language=%s",
langdir?langdir:default_langdir); get_langdir());
server_options[i] = language_arg; server_options[i] = language_arg;
} }
} }
@ -1735,7 +1732,7 @@ int main(int argc, char **argv)
"exactly with that of the errmsg.sys " "exactly with that of the errmsg.sys "
"file.\n*\n", "file.\n*\n",
mysql_error(NULL), mysql_error(NULL),
home_dir); get_langdir());
} }
else else
{ {
@ -1784,7 +1781,7 @@ int main(int argc, char **argv)
getpid()))); getpid())));
/* Write process pid into MaxScale pidfile */ /* Write process pid into MaxScale pidfile */
write_pid_file(home_dir); write_pid_file();
/* Init MaxScale poll system */ /* Init MaxScale poll system */
poll_init(); poll_init();
@ -1894,8 +1891,6 @@ int main(int argc, char **argv)
return_main: return_main:
if (threads) if (threads)
free(threads); free(threads);
if (home_dir)
free(home_dir);
if (cnf_file_path) if (cnf_file_path)
free(cnf_file_path); free(cnf_file_path);
@ -1977,11 +1972,11 @@ static void unlink_pidfile(void)
* *
*/ */
static int write_pid_file(char *home_dir) { static int write_pid_file() {
int fd = -1; int fd = -1;
snprintf(pidfile, PATH_MAX, "%s/maxscale.pid",piddir?piddir:default_piddir); snprintf(pidfile, PATH_MAX, "%s/maxscale.pid",get_piddir());
fd = open(pidfile, O_WRONLY | O_CREAT | O_TRUNC, 0777); fd = open(pidfile, O_WRONLY | O_CREAT | O_TRUNC, 0777);
if (fd == -1) { if (fd == -1) {
@ -2043,7 +2038,7 @@ bool handle_path_arg(char** dest, char* path, char* arg, bool rd, bool wr)
} }
else else
{ {
fprintf(stderr,"%s\n",errstr); print_log_n_stderr(true,true,errstr,errstr,0);
free(errstr); free(errstr);
errstr = NULL; errstr = NULL;
} }
@ -2062,9 +2057,7 @@ bool handle_path_arg(char** dest, char* path, char* arg, bool rd, bool wr)
*/ */
static int cnf_preparser(void* data, const char* section, const char* name, const char* value) static int cnf_preparser(void* data, const char* section, const char* name, const char* value)
{ {
GATEWAY_CONF* cnf = config_get_global_options();
char pathbuffer[PATH_MAX];
char* errstr;
char *tmp; char *tmp;
/** These are read from the configuration file. These will not override /** These are read from the configuration file. These will not override
* command line parameters but will override default values. */ * command line parameters but will override default values. */
@ -2072,8 +2065,13 @@ static int cnf_preparser(void* data, const char* section, const char* name, cons
{ {
if(strcmp(name, "logdir") == 0) if(strcmp(name, "logdir") == 0)
{ {
if(logdir == NULL) if(strcmp(get_logdir(),default_logdir) == 0)
handle_path_arg(&logdir,(char*)value,NULL,true,true); {
if(handle_path_arg(&tmp,(char*)value,NULL,true,true))
{
set_logdir(tmp);
}
}
} }
else if(strcmp(name, "libdir") == 0) else if(strcmp(name, "libdir") == 0)
{ {
@ -2085,14 +2083,18 @@ static int cnf_preparser(void* data, const char* section, const char* name, cons
} }
else if(strcmp(name, "piddir") == 0) else if(strcmp(name, "piddir") == 0)
{ {
if(piddir == NULL) if(strcmp(get_piddir(),default_piddir) == 0)
handle_path_arg(&piddir,(char*)value,NULL,true,true); {
if(handle_path_arg(&tmp,(char*)value,NULL,true,true))
{
set_piddir(tmp);
}
}
} }
else if(strcmp(name, "datadir") == 0) else if(strcmp(name, "datadir") == 0)
{ {
if(!datadir_defined) if(!datadir_defined)
{ {
char* tmp;
if(handle_path_arg(&tmp,(char*)value,NULL,true,false)) if(handle_path_arg(&tmp,(char*)value,NULL,true,false))
{ {
sprintf(datadir,"%s",tmp); sprintf(datadir,"%s",tmp);
@ -2111,8 +2113,21 @@ static int cnf_preparser(void* data, const char* section, const char* name, cons
} }
else if(strcmp(name, "language") == 0) else if(strcmp(name, "language") == 0)
{ {
if(langdir == NULL) if(strcmp(get_langdir(),default_langdir) == 0)
handle_path_arg((char**)&langdir,(char*)value,NULL,true,false); {
if(handle_path_arg((char**)&tmp,(char*)value,NULL,true,false))
{
set_langdir(tmp);
}
}
}
else if(strcmp(name, "syslog") == 0)
{
cnf->syslog = config_truth_value((char*)value);
}
else if(strcmp(name, "maxlog") == 0)
{
cnf->maxlog = config_truth_value((char*)value);
} }
} }

View File

@ -1,5 +1,26 @@
#include <gwdirs.h> #include <gwdirs.h>
void set_configdir(char* str)
{
free(configdir);
configdir = str;
}
void set_logdir(char* str)
{
free(logdir);
logdir = str;
}
void set_langdir(char* str)
{
free(langdir);
langdir = str;
}
void set_piddir(char* str)
{
free(piddir);
piddir = str;
}
/** /**
* Get the directory with all the modules. * Get the directory with all the modules.
* @return The module directory * @return The module directory
@ -46,3 +67,23 @@ void set_datadir(char* param)
free(maxscaledatadir); free(maxscaledatadir);
maxscaledatadir = param; maxscaledatadir = param;
} }
char* get_configdir()
{
return configdir?configdir:(char*)default_configdir;
}
char* get_piddir()
{
return piddir?piddir:(char*)default_piddir;
}
char* get_logdir()
{
return logdir?logdir:(char*)default_logdir;
}
char* get_langdir()
{
return langdir?langdir:(char*)default_langdir;
}

View File

@ -42,10 +42,19 @@ static char* cachedir = NULL;
static char* maxscaledatadir = NULL; static char* maxscaledatadir = NULL;
static char* langdir = NULL; static char* langdir = NULL;
static char* piddir = NULL; static char* piddir = NULL;
void set_libdir(char* param); void set_libdir(char* param);
void set_datadir(char* param); void set_datadir(char* param);
void set_cachedir(char* param); void set_cachedir(char* param);
void set_configdir(char* param);
void set_logdir(char* param);
void set_langdir(char* param);
void set_piddir(char* param);
char* get_libdir(); char* get_libdir();
char* get_datadir(); char* get_datadir();
char* get_cachedir(); char* get_cachedir();
char* get_configdir();
char* get_piddir();
char* get_logdir();
char* get_langdir();
#endif #endif

View File

@ -104,6 +104,8 @@ typedef struct {
unsigned long id; /**< MaxScale ID */ unsigned long id; /**< MaxScale ID */
unsigned int n_nbpoll; /**< Tune number of non-blocking polls */ unsigned int n_nbpoll; /**< Tune number of non-blocking polls */
unsigned int pollsleep; /**< Wait time in blocking polls */ unsigned int pollsleep; /**< Wait time in blocking polls */
int syslog; /*< Log to syslog */
int maxlog; /*< Log to MaxScale's own logs */
} GATEWAY_CONF; } GATEWAY_CONF;
extern int config_load(char *); extern int config_load(char *);
@ -143,4 +145,5 @@ bool config_get_valtarget(
void config_enable_feedback_task(void); void config_enable_feedback_task(void);
void config_disable_feedback_task(void); void config_disable_feedback_task(void);
unsigned long config_get_gateway_id(void); unsigned long config_get_gateway_id(void);
GATEWAY_CONF* config_get_global_options();
#endif #endif