From 89b08c0f0ba951ed385ffb6190a98465717f8b25 Mon Sep 17 00:00:00 2001 From: Martin Schwenke Date: Sat, 7 Mar 2015 07:22:32 +1100 Subject: ctdb-common: New function ctdb_set_helper() Signed-off-by: Martin Schwenke Reviewed-by: Volker Lendecke --- ctdb/common/ctdb_util.c | 60 +++++++++++++++++++++++++++++++++++++++++++++ ctdb/include/ctdb_private.h | 2 ++ 2 files changed, 62 insertions(+) diff --git a/ctdb/common/ctdb_util.c b/ctdb/common/ctdb_util.c index 137e0a8a09..bd68c55416 100644 --- a/ctdb/common/ctdb_util.c +++ b/ctdb/common/ctdb_util.c @@ -64,6 +64,66 @@ void ctdb_die(struct ctdb_context *ctdb, const char *msg) exit(1); } +/* Set the path of a helper program from envvar, falling back to + * dir/file if envvar unset. type is a string to print in log + * messages. helper is assumed to point to a statically allocated + * array of size bytes, initialised to "". If file is NULL don't fall + * back if envvar is unset. If dir is NULL and envvar is unset (but + * file is not NULL) then this is an error. Returns true if helper is + * set, either previously or this time. */ +bool ctdb_set_helper(const char *type, char *helper, size_t size, + const char *envvar, + const char *dir, const char *file) +{ + const char *t; + struct stat st; + + if (helper[0] != '\0') { + /* Already set */ + return true; + } + + t = getenv(envvar); + if (t != NULL) { + if (strlen(t) >= size) { + DEBUG(DEBUG_ERR, + ("Unable to set %s - path too long\n", type)); + return false; + } + + strncpy(helper, t, size); + } else if (file == NULL) { + return false; + } else if (dir == NULL) { + DEBUG(DEBUG_ERR, + ("Unable to set %s - dir is NULL\n", type)); + return false; + } else { + if (snprintf(helper, size, "%s/%s", dir, file) >= size) { + DEBUG(DEBUG_ERR, + ("Unable to set %s - path too long\n", type)); + return false; + } + } + + if (stat(helper, &st) != 0) { + DEBUG(DEBUG_ERR, + ("Unable to set %s \"%s\" - %s\n", + type, helper, strerror(errno))); + return false; + } + if (!(st.st_mode & S_IXUSR)) { + DEBUG(DEBUG_ERR, + ("Unable to set %s \"%s\" - not executable\n", + type, helper)); + return false; + } + + DEBUG(DEBUG_NOTICE, + ("Set %s to \"%s\"\n", type, helper)); + return true; +} + /* Invoke an external program to do some sort of tracing on the CTDB * process. This might block for a little while. The external * program is specified by the environment variable diff --git a/ctdb/include/ctdb_private.h b/ctdb/include/ctdb_private.h index 7005fd802f..ed4d612620 100644 --- a/ctdb/include/ctdb_private.h +++ b/ctdb/include/ctdb_private.h @@ -722,6 +722,8 @@ struct ctdb_fetch_handle { void ctdb_set_error(struct ctdb_context *ctdb, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3); void ctdb_fatal(struct ctdb_context *ctdb, const char *msg); void ctdb_die(struct ctdb_context *ctdb, const char *msg); +bool ctdb_set_helper(const char *type, char *helper, size_t size, + const char *envvar, const char *dir, const char *file); void ctdb_external_trace(void); bool ctdb_same_address(struct ctdb_address *a1, struct ctdb_address *a2); int ctdb_parse_address(struct ctdb_context *ctdb, -- cgit