summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Schwenke <martin@meltin.net>2015-03-07 07:22:32 +1100
committerVolker Lendecke <vl@samba.org>2015-03-10 15:29:06 +0100
commit89b08c0f0ba951ed385ffb6190a98465717f8b25 (patch)
tree81abb0657326d2992e34ccf9f6e73cb3c339b8b9
parentc6cb2d650872a8c37820b65bb2d5882207263e88 (diff)
downloadsamba-89b08c0f0ba951ed385ffb6190a98465717f8b25.tar.gz
samba-89b08c0f0ba951ed385ffb6190a98465717f8b25.tar.xz
samba-89b08c0f0ba951ed385ffb6190a98465717f8b25.zip
ctdb-common: New function ctdb_set_helper()
Signed-off-by: Martin Schwenke <martin@meltin.net> Reviewed-by: Volker Lendecke <vl@samba.org>
-rw-r--r--ctdb/common/ctdb_util.c60
-rw-r--r--ctdb/include/ctdb_private.h2
2 files changed, 62 insertions, 0 deletions
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,