diff --git a/daemon/gluster-blockd.c b/daemon/gluster-blockd.c index 8fff46f..1780dd7 100644 --- a/daemon/gluster-blockd.c +++ b/daemon/gluster-blockd.c @@ -587,17 +587,13 @@ main (int argc, char **argv) int wstatus; - if (pthread_mutex_init(&gbConf.lock, NULL) < 0) { - exit(EXIT_FAILURE); - } - if(initLogging()) { exit(EXIT_FAILURE); } fetchGlfsVolServerFromEnv(); - gbCfg = glusterBlockSetupConfig(NULL); + gbCfg = glusterBlockSetupConfig(); if (!gbCfg) { LOG("mgmt", GB_LOG_ERROR, "glusterBlockSetupConfig() failed"); exit(EXIT_FAILURE); diff --git a/systemd/gluster-blockd.sysconfig b/systemd/gluster-blockd.sysconfig index cc065cd..676ad52 100644 --- a/systemd/gluster-blockd.sysconfig +++ b/systemd/gluster-blockd.sysconfig @@ -9,7 +9,6 @@ # least recently used object. #GB_GLFS_LRU_COUNT=5 - # Supported loglevels [ NONE, CRIT, ERROR, WARNING, INFO, DEBUG, TRACE ] # And the default logging level is INFO, if you want to change the # default level, uncomment it and set your level: @@ -25,3 +24,7 @@ # Expert use only, just incase if we have any extra args to pass for daemon #GB_EXTRA_ARGS="" + +# Supported setting the log directory path from the sysconfig +# default path is /var/log/gluster-blockd, uncomment it and set your path: +#GB_LOG_DIR="/var/log/gluster-block" diff --git a/utils/Makefile.am b/utils/Makefile.am index 83b63fd..f52a6f2 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -9,7 +9,7 @@ libgb_la_CFLAGS = $(GFAPI_CFLAGS) $(TIRPC_CFLAGS) \ -DCONFDIR=\"$(GLUSTER_BLOCKD_WORKDIR)\" \ -I$(top_builddir)/ -I$(top_builddir)/rpc/rpcl -libgb_la_LIBADD = $(GFAPI_LIBS) $(TIRPC_LIBS) +libgb_la_LIBADD = $(GFAPI_LIBS) $(TIRPC_LIBS) $(PTHREAD) libgb_la_LDFLAGS = -lpthread diff --git a/utils/dyn-config.c b/utils/dyn-config.c index f223a70..0499003 100644 --- a/utils/dyn-config.c +++ b/utils/dyn-config.c @@ -167,11 +167,20 @@ glusterBlockGetOption(const char *key) } while (0); static void -glusterBlockConfSetOptions(gbConfig *cfg, bool reloading) +glusterBlockConfSetOptions(gbConfig *cfg, bool getLogDir) { unsigned int logLevel; + /* set logdir option */ + GB_PARSE_CFG_STR(cfg, GB_LOG_DIR, GB_LOGDIR_DEF); + if (getLogDir) { + return; + } + if (cfg->GB_LOG_DIR) { + glusterBlockSetLogDir(cfg->GB_LOG_DIR); + } + /* set logLevel option */ GB_PARSE_CFG_STR(cfg, GB_LOG_LEVEL, "INFO"); if (cfg->GB_LOG_LEVEL) { @@ -207,6 +216,7 @@ glusterBlockConfFreeStrKeys(gbConfig *cfg) * For example: * GB_FREE_CFG_STR_KEY(cfg, 'STR KEY'); */ + GB_FREE_CFG_STR_KEY(cfg, GB_LOG_DIR); GB_FREE_CFG_STR_KEY(cfg, GB_LOG_LEVEL); } @@ -437,7 +447,7 @@ glusterBlockParseOption(char **cur, const char *end) } static void -glusterBlockParseOptions(gbConfig *cfg, char *buf, int len, bool reloading) +glusterBlockParseOptions(gbConfig *cfg, char *buf, int len, bool getLogDir) { char *cur = buf, *end = buf + len; @@ -448,11 +458,11 @@ glusterBlockParseOptions(gbConfig *cfg, char *buf, int len, bool reloading) } /* parse the options from gb_options[] to struct gbConfig */ - glusterBlockConfSetOptions(cfg, reloading); + glusterBlockConfSetOptions(cfg, getLogDir); } int -glusterBlockLoadConfig(gbConfig *cfg, bool reloading) +glusterBlockLoadConfig(gbConfig *cfg, bool getLogDir) { ssize_t len = 0; char *buf; @@ -465,12 +475,53 @@ glusterBlockLoadConfig(gbConfig *cfg, bool reloading) return -1; } - glusterBlockParseOptions(cfg, buf, len, reloading); + glusterBlockParseOptions(cfg, buf, len, getLogDir); GB_FREE(buf); return 0; } + +char * +glusterBlockDynConfigGetLogDir(void) +{ + gbConfig *cfg = NULL; + char *logDir = NULL; + char *configPath = GB_DEF_CONFIGPATH; + int ret; + + + if (GB_ALLOC(cfg) < 0) { + MSG(stderr, "Alloc GB config failed for configPath: %s!\n", configPath); + return NULL; + } + + if (GB_STRDUP(cfg->configPath, configPath) < 0) { + MSG(stderr, "failed to copy configPath: %s\n", configPath); + goto freeConfig; + } + + if (glusterBlockLoadConfig(cfg, true)) { + MSG(stderr, "Loading GB config failed for configPath: %s!\n", configPath); + goto freeConfigPath; + } + + if (cfg->GB_LOG_DIR) { + if (GB_STRDUP(logDir, cfg->GB_LOG_DIR) < 0) { + MSG(stderr, "failed to copy logDir: %s\n", cfg->GB_LOG_DIR); + logDir = NULL; + } + } + +freeConfigPath: + GB_FREE(cfg->configPath); +freeConfig: + GB_FREE(cfg); + + return logDir; +} + + static void * glusterBlockDynConfigStart(void *arg) { @@ -538,7 +589,7 @@ glusterBlockDynConfigStart(void *arg) /* Try to reload the config file */ if (event->mask & IN_MODIFY) { - glusterBlockLoadConfig(cfg, true); + glusterBlockLoadConfig(cfg, false); } } } @@ -548,16 +599,13 @@ glusterBlockDynConfigStart(void *arg) } gbConfig * -glusterBlockSetupConfig(const char *configPath) +glusterBlockSetupConfig(void) { gbConfig *cfg = NULL; + char *configPath = GB_DEF_CONFIGPATH; int ret; - if (!configPath) { - configPath = GB_DEF_CONFIGPATH; - } - if (GB_ALLOC(cfg) < 0) { LOG("mgmt", GB_LOG_ERROR, "Alloc GB config failed for configPath: %s!", configPath); return NULL; diff --git a/utils/lru.c b/utils/lru.c index 0ddcee1..efb4b93 100644 --- a/utils/lru.c +++ b/utils/lru.c @@ -8,12 +8,14 @@ cases as published by the Free Software Foundation. */ +# include # include "lru.h" # include "utils.h" -static struct list_head Cache; +static LIST_HEAD(Cache); static int lruCount; +static pthread_mutex_t lru_lock = PTHREAD_MUTEX_INITIALIZER; typedef struct Entry { char volume[255]; @@ -22,6 +24,31 @@ typedef struct Entry { struct list_head list; } Entry; + +void +glusterBlockUpdateLruLogdir(const char *logPath) +{ + struct list_head *pos, *q; + Entry *tmp; + + + LOCK(lru_lock); + if (!logPath || list_empty(&Cache)) { + UNLOCK(lru_lock); + return; + } + + list_for_each_safe(pos, q, &Cache){ + tmp = list_entry(pos, Entry, list); + if (glfs_set_logging(tmp->glfs, logPath, GFAPI_LOG_LEVEL)) { + LOG("mgmt", GB_LOG_WARNING, "glfs_set_logging(%s, %d) on %s failed[%s]", + logPath, GFAPI_LOG_LEVEL, tmp->volume, strerror(errno)); + } + } + UNLOCK(lru_lock); +} + + int glusterBlockSetLruCount(const size_t lruCount) { @@ -52,6 +79,7 @@ releaseColdEntry(void) struct list_head *pos, *q = &Cache; + LOCK(lru_lock); list_for_each_prev(pos, q) { tmp = list_entry(pos, Entry, list); list_del(pos); @@ -62,6 +90,7 @@ releaseColdEntry(void) break; } + UNLOCK(lru_lock); } @@ -83,7 +112,9 @@ appendNewEntry(const char *volname, glfs_t *fs) GB_STRCPYSTATIC(tmp->volume, volname); tmp->glfs = fs; + LOCK(lru_lock); list_add(&(tmp->list), &Cache); + UNLOCK(lru_lock); lruCount++; UNLOCK(gbConf.lock); @@ -117,13 +148,16 @@ queryCache(const char *volname) struct list_head *pos, *q, *r = &Cache; + LOCK(lru_lock); list_for_each_safe(pos, q, r){ tmp = list_entry(pos, Entry, list); if (!strcmp(tmp->volume, volname)) { boostEntryWarmness(volname); + UNLOCK(lru_lock); return tmp->glfs; } } + UNLOCK(lru_lock); return NULL; } diff --git a/utils/lru.h b/utils/lru.h index 75eca15..0a55e32 100644 --- a/utils/lru.h +++ b/utils/lru.h @@ -32,5 +32,7 @@ appendNewEntry(const char *volname, glfs_t *glfs); int glusterBlockSetLruCount(const size_t lruCount); +void +glusterBlockUpdateLruLogdir(const char *logDir); # endif /* _LRU_H */ diff --git a/utils/utils.c b/utils/utils.c index fe60915..c7ab4be 100644 --- a/utils/utils.c +++ b/utils/utils.c @@ -13,15 +13,17 @@ # include # include # include +# include # include "utils.h" # include "lru.h" # include "config.h" struct gbConf gbConf = { + .lock = PTHREAD_MUTEX_INITIALIZER, .glfsLruCount = LRU_COUNT_DEF, .logLevel = GB_LOG_INFO, - .logDir = GB_LOGDIR, + .logDir = GB_LOGDIR_DEF, .cliTimeout = CLI_TIMEOUT_DEF }; @@ -278,24 +280,43 @@ void fetchGlfsVolServerFromEnv() LOG("mgmt", GB_LOG_INFO, "Block Hosting Volfile Server Set to: %s", gbConf.volServer); } - -int -initLogging(void) +static int +initLogDirAndFiles(char *newLogDir) { char *logDir = NULL; + char *tmpLogDir = NULL; + int ret = 0; + + + /* + * The priority of the logdir setting is: + * 1, /etc/sysconfig/gluster-blockd config file + * 2, "GB_LOGDIR" from the ENV setting + * 3, default as GB_LOGDIR_DEF + */ + if (newLogDir) { + logDir = newLogDir; + } else { + logDir = getenv("GB_LOGDIR"); + tmpLogDir = glusterBlockDynConfigGetLogDir(); + if (tmpLogDir) { + logDir = tmpLogDir; + } - logDir = getenv("GB_LOGDIR"); - if (!logDir) { - logDir = GB_LOGDIR; + if (!logDir) { + logDir = GB_LOGDIR_DEF; + } } if (strlen(logDir) > PATH_MAX - GB_MAX_LOGFILENAME) { fprintf(stderr, "strlen of logDir Path > PATH_MAX: %s", logDir); return EXIT_FAILURE; + goto out; } /* set logfile paths */ + LOCK(gbConf.lock); snprintf(gbConf.logDir, PATH_MAX, "%s", logDir); snprintf(gbConf.daemonLogFile, PATH_MAX, @@ -310,10 +331,31 @@ initLogging(void) "%s/cmd_history.log", logDir); if(!glusterBlockLogdirCreate()) { - return EXIT_FAILURE; + ret = EXIT_FAILURE; + goto unlock; } - return 0; + glusterBlockUpdateLruLogdir(gbConf.gfapiLogFile); + + unlock: + UNLOCK(gbConf.lock); +out: + GB_FREE(tmpLogDir); + return ret; +} + + +int +initLogging(void) +{ + return initLogDirAndFiles(NULL); +} + + +bool +glusterBlockSetLogDir(char *logDir) +{ + return initLogDirAndFiles(logDir); } diff --git a/utils/utils.h b/utils/utils.h index b77bd69..ecc2cfe 100644 --- a/utils/utils.h +++ b/utils/utils.h @@ -29,7 +29,7 @@ # include "list.h" -# define GB_LOGDIR DATADIR "/log/gluster-block" +# define GB_LOGDIR_DEF DATADIR "/log/gluster-block" # define GB_INFODIR DATADIR "/run" # define GB_LOCK_FILE GB_INFODIR "/gluster-blockd.lock" @@ -603,6 +603,7 @@ typedef struct gbConfig { bool isDynamic; char *GB_LOG_LEVEL; + char *GB_LOG_DIR; ssize_t GB_GLFS_LRU_COUNT; ssize_t GB_CLI_TIMEOUT; /* seconds */ } gbConfig; @@ -634,6 +635,8 @@ void logTimeNow(char* buf, size_t bufSize); void fetchGlfsVolServerFromEnv(void); +bool glusterBlockSetLogDir(char *logDir); + int initLogging(void); int gbRunnerExitStatus(int exitStatus); @@ -662,8 +665,10 @@ char *gbStrcat(char *dest, const char *src, size_t destbytes, void gbFree(void *ptrptr); +char *glusterBlockDynConfigGetLogDir(void); + void glusterBlockDestroyConfig(struct gbConfig *cfg); -gbConfig *glusterBlockSetupConfig(const char *path); +gbConfig *glusterBlockSetupConfig(void); #endif /* _UTILS_H */