From mboxrd@z Thu Jan 1 00:00:00 1970 From: Alexander Aring Date: Wed, 31 Mar 2021 16:55:26 -0400 Subject: [Cluster-devel] [PATCH dlm-tool] dlm_controld: create var parent directories Message-ID: <20210331205526.351253-1-aahringo@redhat.com> List-Id: To: cluster-devel.redhat.com MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit This patch creates /var/log/dlm_controld and /var/run/dlm_controld and it's parents if not exists before. In case of logging there was a likely issue no log file is created when /var/log/dlm_controld didn't exists before starting dlm_controld. Reported-by: Bob Peterson --- dlm_controld/dlm_daemon.h | 8 ++++++-- dlm_controld/logging.c | 24 ++++++++++++++++++++++++ dlm_controld/main.c | 20 ++++++++++++++++---- 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/dlm_controld/dlm_daemon.h b/dlm_controld/dlm_daemon.h index 45b295ea..436fc910 100644 --- a/dlm_controld/dlm_daemon.h +++ b/dlm_controld/dlm_daemon.h @@ -65,8 +65,12 @@ /* TODO: get CONFDIR, LOGDIR, RUNDIR from build */ -#define RUNDIR "/var/run/dlm_controld" -#define LOGDIR "/var/log/dlm_controld" +#define SYS_VARDIR "/var" +#define SYS_RUNDIR SYS_VARDIR "/run" +#define SYS_LOGDIR SYS_VARDIR "/log" + +#define RUNDIR SYS_RUNDIR "/dlm_controld" +#define LOGDIR SYS_LOGDIR "/dlm_controld" #define CONFDIR "/etc/dlm" #define RUN_FILE_NAME "dlm_controld.pid" diff --git a/dlm_controld/logging.c b/dlm_controld/logging.c index 4aa3406c..d48b8aeb 100644 --- a/dlm_controld/logging.c +++ b/dlm_controld/logging.c @@ -16,6 +16,9 @@ static FILE *logfile_fp; void init_logging(void) { + mode_t old_umask; + int rv; + syslog_facility = DEFAULT_SYSLOG_FACILITY; syslog_priority = DEFAULT_SYSLOG_PRIORITY; logfile_priority = DEFAULT_LOGFILE_PRIORITY; @@ -28,6 +31,26 @@ void init_logging(void) logfile_priority = LOG_DEBUG; if (logfile[0]) { + old_umask = umask(0077); + rv = mkdir(SYS_VARDIR, 0700); + if (rv < 0 && errno != EEXIST) { + umask(old_umask); + goto skip_logfile; + } + + rv = mkdir(SYS_LOGDIR, 0700); + if (rv < 0 && errno != EEXIST) { + umask(old_umask); + goto skip_logfile; + } + + rv = mkdir(LOGDIR, 0700); + if (rv < 0 && errno != EEXIST) { + umask(old_umask); + goto skip_logfile; + } + umask(old_umask); + logfile_fp = fopen(logfile, "a+"); if (logfile_fp != NULL) { int fd = fileno(logfile_fp); @@ -35,6 +58,7 @@ void init_logging(void) } } +skip_logfile: openlog(DAEMON_NAME, LOG_CONS | LOG_PID, syslog_facility); } diff --git a/dlm_controld/main.c b/dlm_controld/main.c index c35756d4..504cafa1 100644 --- a/dlm_controld/main.c +++ b/dlm_controld/main.c @@ -1598,7 +1598,7 @@ static int loop(void) return rv; } -static int lockfile(const char *dir, const char *name) +static int lockfile(const char *name) { char path[PATH_MAX]; char buf[16]; @@ -1607,14 +1607,26 @@ static int lockfile(const char *dir, const char *name) int fd, rv; old_umask = umask(0022); - rv = mkdir(dir, 0775); + rv = mkdir(SYS_VARDIR, 0775); + if (rv < 0 && errno != EEXIST) { + umask(old_umask); + return rv; + } + + rv = mkdir(SYS_RUNDIR, 0775); + if (rv < 0 && errno != EEXIST) { + umask(old_umask); + return rv; + } + + rv = mkdir(RUNDIR, 0775); if (rv < 0 && errno != EEXIST) { umask(old_umask); return rv; } umask(old_umask); - snprintf(path, PATH_MAX, "%s/%s", dir, name); + snprintf(path, PATH_MAX, "%s/%s", RUNDIR, name); fd = open(path, O_CREAT|O_WRONLY|O_CLOEXEC, 0644); if (fd < 0) { @@ -2125,7 +2137,7 @@ int main(int argc, char **argv) init_logging(); - fd = lockfile(RUNDIR, RUN_FILE_NAME); + fd = lockfile(RUN_FILE_NAME); if (fd < 0) return 1; -- 2.26.3