diff --git a/log_manager/log_manager.cc b/log_manager/log_manager.cc index e1bf72cf7..2ab79f5c5 100644 --- a/log_manager/log_manager.cc +++ b/log_manager/log_manager.cc @@ -1375,6 +1375,7 @@ static bool fnames_conf_init( fn->fn_chk_top = CHK_NUM_FNAMES; fn->fn_chk_tail = CHK_NUM_FNAMES; #endif + optind = 1; /**state != DCB_STATE_ALLOC && + dcb->state != DCB_STATE_POLLING && + dcb->state != DCB_STATE_LISTENING)) + { return 0; } + spinlock_acquire(&dcb->writeqlock); if (dcb->writeq != NULL) diff --git a/server/core/gateway.c b/server/core/gateway.c index 6c5174054..8ce999fd7 100644 --- a/server/core/gateway.c +++ b/server/core/gateway.c @@ -98,6 +98,11 @@ static bool do_exit = FALSE; */ static bool libmysqld_started = FALSE; +/** + * If MaxScale is started to run in daemon process the value is true. + */ +static bool daemon_mode = true; + static void log_flush_shutdown(void); static void log_flush_cb(void* arg); static void libmysqld_done(void); @@ -206,42 +211,6 @@ static void libmysqld_done(void) } } -#if 0 -static char* set_home_and_variables( - int argc, - char** argv) -{ - int i; - int n; - char* home = NULL; - bool home_set = FALSE; - - for (i=1; i 0 && access(&argv[n][j], R_OK) == 0) { - home = strdup(&argv[n][j]); - goto return_home; - } - } - } - if ((home = getenv("MAXSCALE_HOME")) != NULL) - { - sprintf(mysql_home, "%s/mysql", home); - setenv("MYSQL_HOME", mysql_home, 1); - } - -return_home: - return home; - -} -#endif static void write_footer(void) { @@ -337,10 +306,10 @@ return_succp: * @param do_stderr - in, use * is printing to stderr enabled * - * @param logerr - in, use + * @param logstr - in, use * string to be printed to log * - * @param fprerr - in, use + * @param fprstr - in, use * string to be printed to stderr * * @param eno - in, use @@ -350,29 +319,29 @@ return_succp: * */ static void print_log_n_stderr( - bool do_log, /**/etc + * + * is resolved in the following order: + * 1. from '-c ' command-line argument + * 2. from MAXSCALE_HOME environment variable + * 3. /etc/ if MaxScale.cnf is found from there + * 4. current working directory if MaxScale.cnf is found from there + * * vraa 25.11.13 * */ @@ -412,28 +464,22 @@ int main(int argc, char **argv) int n_threads; /**]"; + print_log_n_stderr(true, true, logerr, logerr, 0); + } + break; + + case 'm': + { + char* ldlib_env = getenv("LD_LIBRARY_PATH"); + char* new_env; + char* abs_path = (char*)malloc(PATH_MAX+1); + + if (abs_path == NULL) + { + succp = false; + break; + } + + if (realpath(optarg, abs_path) == NULL) + { + int eno = errno; + errno = 0; + + fprintf(stderr, + "*\n* Error : Failed to read the " + "library directory %s. %s.\n*\n", + optarg, + strerror(eno)); + + skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Failed to read the " + "library directory %s, due " + "to %d, %s.", + optarg, + eno, + strerror(eno)); + succp = false; + } + else + { + new_env = (char*)malloc(strlen(ldlib_env)+ + 1+ + strlen(abs_path)+ + 1); + if (new_env == NULL) + { + succp = false; + break; + } + sprintf(new_env, "%s:%s", ldlib_env, abs_path); + setenv("LD_LIBRARY_PATH", new_env, 1); + free(new_env); + ss_dfprintf(stderr, + "Set LD_LIBRARY_PATH=%s\n", + getenv("LD_LIBRARY_PATH")); + + } + free(abs_path); + } + break; + + default: + fprintf(stderr, + "*\n* Usage : maxscale [-d] [-c ] [-m ]\n*\n"); + succp = false; + break; + } + + if (!succp) + { + rc = 1; + goto return_main; } } - if (!daemon_mode) { fprintf(stderr, "Info : MaxScale will be run in the terminal process.\n See " - "the log from the following log files.\n\n"); + "the log from the following log files : \n\n"); } else { @@ -515,7 +640,7 @@ int main(int argc, char **argv) fprintf(stderr, "Info : MaxScale will be run in a daemon process.\n\tSee " - "the log from the following log files.\n\n"); + "the log from the following log files : \n\n"); r = sigfillset(&sigset); /*r=1;/**/ @@ -620,121 +745,96 @@ int main(int argc, char **argv) if (l != 0) { char* fprerr = "Failed to register exit function for\n* " "embedded MySQL library.\n* Exiting."; - char* logerr = "Failed to register exit function libmysql_done for MaxScale. " - "Exiting."; - print_log_n_stderr(true, true, logerr, fprerr, 0); + char* logerr = "Failed to register exit function libmysql_done " + "for MaxScale. Exiting."; + print_log_n_stderr(true, true, logerr, fprerr, 0); rc = 1; goto return_main; } - home = getenv("MAXSCALE_HOME"); - /*home=NULL; /**/ - if (home != NULL) + /** + * 1. if home dir wasn't specified in the command-line argument, + * read env. variable MAXSCALE_HOME. + */ + if (home_dir == NULL) { - int r = access(home, R_OK); - int eno = 0; - /*r=1; /**/ - if (r != 0) + char* tmp = getenv("MAXSCALE_HOME"); + cnf_file = get_config_filename(&home_dir, tmp); + + if (!daemon_mode) { - eno = errno; - errno = 0; - if (!daemon_mode) - { - fprintf(stderr, - "*\n* Error : Failed to read the " - "value of\n* MAXSCALE_HOME, %s.\n* " - "Exiting.\n*\n", - home); - } - skygw_log_write_flush( - LOGFILE_ERROR, - "Error : Failed to read the " - "value of MAXSCALE_HOME, %s, due " - "to %d, %s. Exiting.", - home, - eno, - strerror(eno)); - rc = 1; - goto return_main; + fprintf(stderr, "Found MAXSCALE_HOME = %s\n", tmp); } - sprintf(mysql_home, "%s/mysql", home); - setenv("MYSQL_HOME", mysql_home, 1); - /** - * 2. Resolve config file location from $MAXSCALE_HOME/etc. - */ - /*cnf_file=NULL; /**/ - if (cnf_file == NULL) { - int r; - int eno = 0; - - sprintf(buf, "%s/etc/MaxScale.cnf", home); - r = access(buf, R_OK); - /*r=1; /**/ - if (r != 0) - { - eno = errno; - errno = 0; - if (!daemon_mode) - { - fprintf(stderr, - "*\n* Error : Failed to read the " - "configuration \n* file %s.\n* " - "Exiting.\n*\n", - buf); - } - skygw_log_write_flush( - LOGFILE_ERROR, - "Error : Failed to read the " - "configuration \nfile %s due to %d, %s.\n" - "Exiting.", - buf, - eno, - strerror(eno)); - rc = 1; - goto return_main; - } - cnf_file = buf; + if (tmp != NULL && cnf_file == NULL) + { + char* logerr = "Unable to locate MaxScale.cnf from " + "directory pointed to by MAXSCALE_HOME. " + "Exiting."; + print_log_n_stderr(true, true, logerr, logerr, 0); } } /** - * If not done yet, - * 3. Resolve config file location from /etc/MaxScale. + * 2. if home dir wasn't specified in MAXSCALE_HOME, + * try access /etc/MaxScale.cnf. */ - if (cnf_file == NULL && - access("/etc/MaxScale.cnf", R_OK) == 0) + if (home_dir == NULL) { - cnf_file = "/etc/MaxScale.cnf"; + char* tmp = "/etc/MaxScale"; + cnf_file = get_config_filename(&home_dir, tmp); } - /* + /** + * 3. if /etc/MaxScale.cnf didn't exist or wasn't accessible, home + * isn't specified. Thus, try to access $PWD/MaxScale.cnf . + */ + if (home_dir == NULL) + { + char* tmp = getenv("PWD"); + cnf_file = get_config_filename(&home_dir, tmp); + } + + if (home_dir != NULL) + { + ss_dassert(cnf_file != NULL); + sprintf(mysql_home, "%s/mysql", home_dir); + setenv("MYSQL_HOME", mysql_home, 1); + } + else + { + char* logstr = "MaxScale couldn't find home directory including " + "readable configuration file.\n*\n* Set home directory as " + "follows:\n* - specify it as a \'-c \' " + "command-line argument,\n* - set it to MAXSCALE_HOME " + "environment " + "variable, or\n* - create directory /etc/MaxScale and " + "copy MaxScale.cnf there, or\n* - change to MaxScale " + "home directory, and ensure that MaxScale.cnf is " + "located in etc/ directory.\n*\n* Exiting."; + ss_dassert(cnf_file == NULL); + print_log_n_stderr(true, true, logstr, logstr, 0); + rc = 1; + goto return_main; + } + /** * Set a data directory for the mysqld library, we use * a unique directory name to avoid clauses if multiple * instances of the gateway are beign run on the same * machine. */ - if (home) - { - sprintf(datadir, "%s/data%d", home, getpid()); - mkdir(datadir, 0777); - } - else - { - sprintf(datadir, "/tmp/MaxScale/data%d", getpid()); - mkdir("/tmp/MaxScale", 0777); - mkdir(datadir, 0777); - } + sprintf(datadir, "%s/data%d", home_dir, getpid()); + mkdir(datadir, 0777); - /* + /** + * Init Log Manager for MaxScale. * If $MAXSCALE_HOME is set then write the logs into $MAXSCALE_HOME/log. * The skygw_logmanager_init expects to take arguments as passed to main * and proesses them with getopt, therefore we need to give it a dummy * argv[0] */ - if (home) { char buf[1024]; char *argv[8]; - sprintf(buf, "%s/log", home); + sprintf(buf, "%s/log", home_dir); mkdir(buf, 0777); argv[0] = "MaxScale"; argv[1] = "-j"; @@ -746,17 +846,7 @@ int main(int argc, char **argv) argv[7] = NULL; skygw_logmanager_init(7, argv); } - - if (cnf_file == NULL) { - char* logerr = "Failed to find or read the configuration " - "file MaxScale.cnf.\n Either install one in /etc/, " - "$MAXSCALE_HOME/etc/ , or specify it by using " - "the -c option. Exiting."; - print_log_n_stderr(true, !daemon_mode, logerr, logerr, 0); - rc = 1; - goto return_main; - } - + /* Update the server options */ for (i = 0; server_options[i]; i++) { @@ -857,7 +947,9 @@ int main(int argc, char **argv) thread_wait(threads[n]); } free(threads); - + free(home_dir); + free(cnf_file); + /** * Wait the flush thread. */ diff --git a/utils/skygw_types.h b/utils/skygw_types.h index ec1f89686..05830fdd6 100644 --- a/utils/skygw_types.h +++ b/utils/skygw_types.h @@ -36,4 +36,9 @@ #define UINTLEN(i) (i<10 ? 1 : (i<100 ? 2 : (i<1000 ? 3 : CALCLEN(i)))) +#if defined(__USE_POSIX) +# define PATH_MAX _POSIX_PATH_MAX +#else +# define PATH_MAX 256 +#endif #endif /* SKYGW_TYPES_H */