Run dir (LOCALSTATEDIR/lib/corosync) was hardcoded thru whole codebase.
Totemsrp was trying to create and chdir into it, but also
takes into account environment variable COROSYNC_RUN_DIR creating
inconsistency.
get_run_dir correctly returns COROSYNC_RUN_DIR (when set) or
LOCALSTATEDIR/lib/corosync. This is now used by all functions instead of
hardcoded string.
All occurrences of mkdir/chdir are removed from totemsrp and chdir is
now called in main function.
Signed-off-by: Jan Friesse <jfriesse@xxxxxxxxxx>
---
exec/main.c | 23 +++++++++++++++--------
exec/totemsrp.c | 26 +++-----------------------
exec/util.c | 21 +++++++++++++++++++++
exec/util.h | 5 +++++
exec/votequorum.c | 3 ++-
5 files changed, 46 insertions(+), 32 deletions(-)
diff --git a/exec/main.c b/exec/main.c
index 8cf551e..d630058 100644
--- a/exec/main.c
+++ b/exec/main.c
@@ -193,6 +193,7 @@ void corosync_state_dump (void)
static void corosync_blackbox_write_to_file (void)
{
char fname[PATH_MAX];
+ char fdata_fname[PATH_MAX];
char time_str[PATH_MAX];
struct tm cur_time_tm;
time_t cur_time_t;
@@ -203,17 +204,18 @@ static void corosync_blackbox_write_to_file (void)
strftime(time_str, PATH_MAX, "%Y-%m-%dT%H:%M:%S", &cur_time_tm);
snprintf(fname, PATH_MAX, "%s/fdata-%s-%lld",
- LOCALSTATEDIR "/lib/corosync",
+ get_run_dir(),
time_str,
(long long int)getpid());
if ((res = qb_log_blackbox_write_to_file(fname)) < 0) {
LOGSYS_PERROR(-res, LOGSYS_LEVEL_ERROR, "Can't store blackbox file");
}
- unlink(LOCALSTATEDIR "/lib/corosync/fdata");
- if (symlink(fname, LOCALSTATEDIR "/lib/corosync/fdata") == -1) {
+ snprintf(fdata_fname, sizeof(fdata_fname), "%s/fdata", get_run_dir());
+ unlink(fdata_fname);
+ if (symlink(fname, fdata_fname) == -1) {
log_printf(LOGSYS_LEVEL_ERROR, "Can't create symlink to '%s' for corosync blackbox file '%s'",
- fname, LOCALSTATEDIR "/lib/corosync/fdata");
+ fname, fdata_fname);
}
}
@@ -1085,7 +1087,6 @@ int main (int argc, char **argv, char **envp)
int res, ch;
int background, setprio;
struct stat stat_out;
- char corosync_lib_dir[PATH_MAX];
enum e_corosync_done flock_err;
uint64_t totem_config_warnings;
struct scheduler_pause_timeout_data scheduler_pause_timeout_data;
@@ -1181,10 +1182,16 @@ int main (int argc, char **argv, char **envp)
/*
* Make sure required directory is present
*/
- sprintf (corosync_lib_dir, "%s/lib/corosync", LOCALSTATEDIR);
- res = stat (corosync_lib_dir, &stat_out);
+ res = stat (get_run_dir(), &stat_out);
if ((res == -1) || (res == 0 && !S_ISDIR(stat_out.st_mode))) {
- log_printf (LOGSYS_LEVEL_ERROR, "Required directory not present %s. Please create it.", corosync_lib_dir);
+ log_printf (LOGSYS_LEVEL_ERROR, "Required directory not present %s. Please create it.", get_run_dir());
+ corosync_exit_error (COROSYNC_DONE_DIR_NOT_PRESENT);
+ }
+
+ res = chdir(get_run_dir());
+ if (res == -1) {
+ log_printf (LOGSYS_LEVEL_ERROR, "Cannot chdir to run directory %s. "
+ "Please make sure it has correct context and rights.", get_run_dir());
corosync_exit_error (COROSYNC_DONE_DIR_NOT_PRESENT);
}
diff --git a/exec/totemsrp.c b/exec/totemsrp.c
index 5f5b7be..dc6b209 100644
--- a/exec/totemsrp.c
+++ b/exec/totemsrp.c
@@ -91,6 +91,7 @@
#include "totemnet.h"
#include "cs_queue.h"
+#include "util.h"
#define LOCALHOST_IP inet_addr("127.0.0.1")
#define QUEUE_RTR_ITEMS_SIZE_MAX 16384 /* allow 16384 retransmit items */
@@ -680,8 +681,6 @@ struct message_handlers totemsrp_message_handlers = {
}
};
-static const char *rundir = NULL;
-
#define log_printf(level, format, args...) \
do { \
instance->totemsrp_log_printf ( \
@@ -843,28 +842,12 @@ int totemsrp_initialize (
int waiting_trans_ack))
{
struct totemsrp_instance *instance;
- unsigned int res;
instance = malloc (sizeof (struct totemsrp_instance));
if (instance == NULL) {
goto error_exit;
}
- rundir = getenv ("COROSYNC_RUN_DIR");
- if (rundir == NULL) {
- rundir = LOCALSTATEDIR "/lib/corosync";
- }
-
- res = mkdir (rundir, 0700);
- if (res == -1 && errno != EEXIST) {
- goto error_destroy;
- }
-
- res = chdir (rundir);
- if (res == -1) {
- goto error_destroy;
- }
-
totemsrp_instance_initialize (instance);
instance->totemsrp_waiting_trans_ack_cb_fn = waiting_trans_ack_cb_fn;
@@ -1033,9 +1016,6 @@ int totemsrp_initialize (
*srp_context = instance;
return (0);
-error_destroy:
- free (instance);
-
error_exit:
return (-1);
}
@@ -3321,7 +3301,7 @@ static void memb_ring_id_create_or_load (
char filename[PATH_MAX];
snprintf (filename, sizeof(filename), "%s/ringid_%s",
- rundir, totemip_print (&instance->my_id.addr[0]));
+ get_run_dir(), totemip_print (&instance->my_id.addr[0]));
fd = open (filename, O_RDONLY, 0700);
/*
* If file can be opened and read, read the ring id
@@ -3366,7 +3346,7 @@ static void memb_ring_id_set_and_store (
memcpy (&instance->my_ring_id, ring_id, sizeof (struct memb_ring_id));
snprintf (filename, sizeof(filename), "%s/ringid_%s",
- rundir, totemip_print (&instance->my_id.addr[0]));
+ get_run_dir(), totemip_print (&instance->my_id.addr[0]));
fd = open (filename, O_WRONLY, 0777);
if (fd == -1) {
diff --git a/exec/util.c b/exec/util.c
index 343759d..c7d561e 100644
--- a/exec/util.c
+++ b/exec/util.c
@@ -170,3 +170,24 @@ int cs_name_tisEqual (cs_name_t *str1, char *str2) {
return 0;
}
}
+
+const char *get_run_dir(void)
+{
+ static char path[PATH_MAX] = {'\0'};
+ char *env_run_dir;
+ int res;
+
+ if (path[0] == '\0') {
+ env_run_dir = getenv("COROSYNC_RUN_DIR");
+
+ if (env_run_dir != NULL && env_run_dir[0] != '\0') {
+ res = snprintf(path, PATH_MAX, "%s", getenv("COROSYNC_RUN_DIR"));
+ } else {
+ res = snprintf(path, PATH_MAX, "%s/%s", LOCALSTATEDIR, "lib/corosync");
+ }
+
+ assert(res < PATH_MAX);
+ }
+
+ return (path);
+}
diff --git a/exec/util.h b/exec/util.h
index 58f804b..63caf74 100644
--- a/exec/util.h
+++ b/exec/util.h
@@ -79,4 +79,9 @@ extern int cs_name_tisEqual (cs_name_t *str1, char *str2);
const char * short_service_name_get(uint32_t service_id,
char *buf, size_t buf_size);
+/*
+ * Return run directory (ether COROSYNC_RUN_DIR env or LOCALSTATEDIR/lib/corosync)
+ */
+const char *get_run_dir(void);
+
#endif /* UTIL_H_DEFINED */
diff --git a/exec/votequorum.c b/exec/votequorum.c
index 1e913eb..650e38e 100644
--- a/exec/votequorum.c
+++ b/exec/votequorum.c
@@ -52,6 +52,7 @@
#include <corosync/ipc_votequorum.h>
#include "service.h"
+#include "util.h"
LOGSYS_DECLARE_SUBSYS ("VOTEQ");
@@ -778,7 +779,7 @@ static int load_ev_tracking_barrier(void)
ENTER();
- snprintf(filename, sizeof(filename) - 1, LOCALSTATEDIR "/lib/corosync/ev_tracking");
+ snprintf(filename, sizeof(filename) - 1, "%s/ev_tracking", get_run_dir());
ev_tracking_fd = open(filename, O_RDWR, 0700);
if (ev_tracking_fd != -1) {