Skip to content

Commit

Permalink
config: logDir add dyn support
Browse files Browse the repository at this point in the history
Signed-off-by: Xiubo Li <[email protected]>
  • Loading branch information
lxbsz committed Nov 30, 2018
1 parent 12fc101 commit 33f9de0
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 30 deletions.
6 changes: 1 addition & 5 deletions daemon/gluster-blockd.c
Original file line number Diff line number Diff line change
Expand Up @@ -437,17 +437,13 @@ main (int argc, char **argv)
int errnosv = 0;


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, "%s", "glusterBlockSetupConfig() failed");
return -1;
Expand Down
5 changes: 4 additions & 1 deletion systemd/gluster-blockd.sysconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -21,3 +20,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-blockd"
2 changes: 1 addition & 1 deletion utils/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ libgb_la_CFLAGS = $(GFAPI_CFLAGS) -DDATADIR=\"$(localstatedir)\" \
-DCONFDIR=\"$(GLUSTER_BLOCKD_WORKDIR)\" \
-I$(top_builddir)/ -I$(top_builddir)/rpc/rpcl

libgb_la_LIBADD = $(GFAPI_LIBS)
libgb_la_LIBADD = $(PTHREAD) $(GFAPI_LIBS)

libgb_ladir = $(includedir)/gluster-block/utils

Expand Down
70 changes: 59 additions & 11 deletions utils/dyn-config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -195,6 +204,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);
}

Expand Down Expand Up @@ -415,7 +425,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;

Expand All @@ -426,11 +436,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);
}

static int
glusterBlockLoadConfig(gbConfig *cfg, bool reloading)
glusterBlockLoadConfig(gbConfig *cfg, bool getLogDir)
{
ssize_t len = 0;
char *buf;
Expand All @@ -443,12 +453,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) {
fprintf(stderr, "Alloc GB config failed for configPath: %s!\n", configPath);
return NULL;
}

if (GB_STRDUP(cfg->configPath, configPath) < 0) {
fprintf(stderr, "failed to copy configPath: %s\n", configPath);
goto freeConfig;
}

if (glusterBlockLoadConfig(cfg, true)) {
fprintf(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) {
fprintf(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)
{
Expand Down Expand Up @@ -505,7 +556,7 @@ glusterBlockDynConfigStart(void *arg)

/* Try to reload the config file */
if (event->mask & IN_MODIFY || event->mask & IN_IGNORED) {
glusterBlockLoadConfig(cfg, true);
glusterBlockLoadConfig(cfg, false);
}

p += sizeof(struct inotify_event) + event->len;
Expand All @@ -516,16 +567,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!\n", configPath);
return NULL;
Expand Down
59 changes: 49 additions & 10 deletions utils/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@

# include <dirent.h>
# include <sys/stat.h>
# include <pthread.h>

# 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
};

const char *argp_program_version = "" \
Expand Down Expand Up @@ -251,24 +253,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\n", logDir);
return EXIT_FAILURE;
ret = EXIT_FAILURE;
goto unlock;
}

/* set logfile paths */
LOCK(gbConf.lock);
snprintf(gbConf.logDir, PATH_MAX,
"%s", logDir);
snprintf(gbConf.daemonLogFile, PATH_MAX,
Expand All @@ -283,10 +304,28 @@ initLogging(void)
"%s/cmd_history.log", logDir);

if(!glusterBlockLogdirCreate()) {
return EXIT_FAILURE;
ret = EXIT_FAILURE;
goto unlock;
}

return 0;
unlock:
UNLOCK(gbConf.lock);
GB_FREE(tmpLogDir);
return ret;
}


int
initLogging(void)
{
return initLogDirAndFiles(NULL);
}


bool
glusterBlockSetLogDir(char *logDir)
{
return initLogDirAndFiles(logDir);
}


Expand Down
9 changes: 7 additions & 2 deletions utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -575,6 +575,7 @@ typedef struct gbConfig {

bool isDynamic;
char *GB_LOG_LEVEL;
char *GB_LOG_DIR;
ssize_t GB_GLFS_LRU_COUNT;
} gbConfig;

Expand All @@ -598,6 +599,8 @@ void logTimeNow(char* buf, size_t bufSize);

void fetchGlfsVolServerFromEnv(void);

bool glusterBlockSetLogDir(char *logDir);

int initLogging(void);

int gbRunnerExitStatus(int exitStatus);
Expand All @@ -621,8 +624,10 @@ char* gbStrcpy(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 */

0 comments on commit 33f9de0

Please sign in to comment.