dlm: master - dlm_controld: clean up default defines and their usage

David Teigland teigland at fedoraproject.org
Wed Oct 5 21:48:48 UTC 2011


Gitweb:        http://git.fedorahosted.org/git/dlm.git?p=dlm.git;a=commitdiff;h=bc70e884fdfdbed3cf51091f506be415716982da
Commit:        bc70e884fdfdbed3cf51091f506be415716982da
Parent:        3ee4f78d0039e1dc67382cfc4a2ada0ae2c1851f
Author:        David Teigland <teigland at redhat.com>
AuthorDate:    Wed Oct 5 15:33:02 2011 -0500
Committer:     David Teigland <teigland at redhat.com>
CommitterDate: Wed Oct 5 16:27:47 2011 -0500

dlm_controld: clean up default defines and their usage

Signed-off-by: David Teigland <teigland at redhat.com>
---
 dlm_controld/config.c     |    8 ++--
 dlm_controld/dlm_daemon.h |   35 ++++++++++++--------
 dlm_controld/logging.c    |    3 +-
 dlm_controld/main.c       |   76 +++++++++++++++++++++++++++++++-------------
 4 files changed, 80 insertions(+), 42 deletions(-)

diff --git a/dlm_controld/config.c b/dlm_controld/config.c
index 40b0c87..6c94fca 100644
--- a/dlm_controld/config.c
+++ b/dlm_controld/config.c
@@ -46,18 +46,18 @@ void setup_config(int update)
 	xmlNode *root;
 	xmlChar *str;
 
-	if (!path_exists(DLM_CONFIG_FILE))
+	if (!path_exists(CONF_FILE_PATH))
 		return;
 
-	doc = xmlParseFile(DLM_CONFIG_FILE);
+	doc = xmlParseFile(CONF_FILE_PATH);
 	if (!doc) {
-		log_error("xml parse error %d %s", errno, DLM_CONFIG_FILE);
+		log_error("xml parse error %d %s", errno, CONF_FILE_PATH);
 		return;
 	}
 
 	root = xmlDocGetRootElement(doc);
 	if (!root) {
-		log_error("xml root error %d %s", errno, DLM_CONFIG_FILE);
+		log_error("xml root error %d %s", errno, CONF_FILE_PATH);
 		xmlFreeDoc(doc);
 		return;
 	}
diff --git a/dlm_controld/dlm_daemon.h b/dlm_controld/dlm_daemon.h
index 9627118..9c180d7 100644
--- a/dlm_controld/dlm_daemon.h
+++ b/dlm_controld/dlm_daemon.h
@@ -43,20 +43,27 @@
 #include "rbtree.h"
 #include "linux_endian.h"
 
-/* TODO: cleanup */
-#define CLUSTERVARLIB "/var/lib/cluster"
-#define CLUSTERVARRUN "/var/run/cluster"
-#define LOGDIR "/var/log/cluster"
-#define SYSLOGFACILITY LOG_LOCAL4
-#define SYSLOGLEVEL LOG_INFO
-#define LOCKFILE_NAME CLUSTERVARRUN "/dlm_controld.pid"
-#define DAEMON_NAME "dlm_controld"
-#define DEFAULT_LOG_MODE LOG_MODE_OUTPUT_FILE|LOG_MODE_OUTPUT_SYSLOG
-#define DEFAULT_SYSLOG_FACILITY SYSLOGFACILITY
-#define DEFAULT_SYSLOG_PRIORITY SYSLOGLEVEL
-#define DEFAULT_LOGFILE_PRIORITY LOG_INFO /* ? */
-#define DEFAULT_LOGFILE LOGDIR "/" DAEMON_NAME ".log"
-#define DLM_CONFIG_FILE "/etc/dlm.conf"
+/* TODO: get CONFDIR, LOGDIR, RUNDIR from build */
+
+#define RUNDIR                   "/var/run/cluster"
+#define LOGDIR                   "/var/log/cluster"
+#define CONFDIR                  "/etc"
+
+#define RUN_FILE_NAME            "dlm_controld.pid"
+#define LOG_FILE_NAME            "dlm_controld.log"
+#define CONF_FILE_NAME           "dlm.conf"
+
+#define RUN_FILE_PATH            RUNDIR "/" RUN_FILE_NAME
+#define LOG_FILE_PATH            LOGDIR "/" LOG_FILE_NAME
+#define CONF_FILE_PATH           CONFDIR "/" CONF_FILE_NAME
+
+#define DEFAULT_LOG_MODE         LOG_MODE_OUTPUT_FILE | LOG_MODE_OUTPUT_SYSLOG
+#define DEFAULT_SYSLOG_FACILITY  LOG_LOCAL4
+#define DEFAULT_SYSLOG_PRIORITY  LOG_INFO
+#define DEFAULT_LOGFILE_PRIORITY LOG_INFO
+#define DEFAULT_LOGFILE          LOG_FILE_PATH
+
+#define DAEMON_NAME              "dlm_controld"
 
 
 /* DLM_LOCKSPACE_LEN: maximum lockspace name length, from linux/dlmconstants.h.
diff --git a/dlm_controld/logging.c b/dlm_controld/logging.c
index e6e82b7..cb3c7fb 100644
--- a/dlm_controld/logging.c
+++ b/dlm_controld/logging.c
@@ -30,7 +30,8 @@ void init_logging(void)
 
 void setup_logging(void)
 {
-	/* TODO */
+
+	/* TODO: look for settings for each of these in dlm.conf */
 	/*
 	ccs_read_logging(ccs_handle, DAEMON_NAME,
 			 &cfgd_debug_logfile, &log_mode,
diff --git a/dlm_controld/main.c b/dlm_controld/main.c
index 0fa10fb..2be088a 100644
--- a/dlm_controld/main.c
+++ b/dlm_controld/main.c
@@ -989,20 +989,29 @@ static void loop(void)
 		log_error("abandoned lockspace %s", ls->name);
 }
 
-static void lockfile(void)
+static int lockfile(const char *dir, const char *name)
 {
-	int fd, error;
+	char path[PATH_MAX];
+	char buf[16];
 	struct flock lock;
-	char buf[33];
+	mode_t old_umask;
+	int fd, rv;
 
-	memset(buf, 0, 33);
+	old_umask = umask(0022);
+	rv = mkdir(dir, 0777);
+	if (rv < 0 && errno != EEXIST) {
+		umask(old_umask);
+		return rv;
+	}
+	umask(old_umask);
 
-	fd = open(LOCKFILE_NAME, O_CREAT|O_WRONLY,
-		  S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
+	snprintf(path, PATH_MAX, "%s/%s", dir, name);
+
+	fd = open(path, O_CREAT|O_WRONLY|O_CLOEXEC, 0666);
 	if (fd < 0) {
-		fprintf(stderr, "cannot open/create lock file %s\n",
-			LOCKFILE_NAME);
-		exit(EXIT_FAILURE);
+		log_error("lockfile open error %s: %s",
+			  path, strerror(errno));
+		return -1;
 	}
 
 	lock.l_type = F_WRLCK;
@@ -1010,25 +1019,43 @@ static void lockfile(void)
 	lock.l_whence = SEEK_SET;
 	lock.l_len = 0;
 
-	error = fcntl(fd, F_SETLK, &lock);
-	if (error) {
-		fprintf(stderr, "dlm_controld is already running\n");
-		exit(EXIT_FAILURE);
+	rv = fcntl(fd, F_SETLK, &lock);
+	if (rv < 0) {
+		log_error("lockfile setlk error %s: %s",
+			  path, strerror(errno));
+		goto fail;
 	}
 
-	error = ftruncate(fd, 0);
-	if (error) {
-		fprintf(stderr, "cannot clear lock file %s\n", LOCKFILE_NAME);
-		exit(EXIT_FAILURE);
+	rv = ftruncate(fd, 0);
+	if (rv < 0) {
+		log_error("lockfile truncate error %s: %s",
+			  path, strerror(errno));
+		goto fail;
 	}
 
-	sprintf(buf, "%d\n", getpid());
+	memset(buf, 0, sizeof(buf));
+	snprintf(buf, sizeof(buf), "%d\n", getpid());
 
-	error = write(fd, buf, strlen(buf));
-	if (error <= 0) {
-		fprintf(stderr, "cannot write lock file %s\n", LOCKFILE_NAME);
-		exit(EXIT_FAILURE);
+	rv = write(fd, buf, strlen(buf));
+	if (rv <= 0) {
+		log_error("lockfile write error %s: %s",
+			  path, strerror(errno));
+		goto fail;
 	}
+
+	return fd;
+ fail:
+	close(fd);
+	return -1;
+}
+
+static void unlink_lockfile(int fd, const char *dir, const char *name)
+{
+	char path[PATH_MAX];
+
+	snprintf(path, PATH_MAX, "%s/%s", dir, name);
+	unlink(path);
+	close(fd);
 }
 
 static void print_usage(void)
@@ -1200,6 +1227,8 @@ static void set_scheduler(void)
 
 int main(int argc, char **argv)
 {
+	int fd;
+
 	INIT_LIST_HEAD(&lockspaces);
 	INIT_LIST_HEAD(&fs_register_list);
 
@@ -1211,7 +1240,7 @@ int main(int argc, char **argv)
 			exit(EXIT_FAILURE);
 		}
 	}
-	lockfile();
+	fd = lockfile(RUNDIR, RUN_FILE_NAME);
 	init_logging();
 	log_level(NULL, LOG_INFO, "dlm_controld %s started", RELEASE_VERSION);
 	signal(SIGTERM, sigterm_handler);
@@ -1219,6 +1248,7 @@ int main(int argc, char **argv)
 
 	loop();
 
+	unlink_lockfile(fd, RUNDIR, RUN_FILE_NAME);
 	return 0;
 }
 


More information about the cluster-commits mailing list