1
0
mirror of https://github.com/samba-team/samba.git synced 2025-08-26 01:49:31 +03:00

ctdb-cluster-mutex: Separate out command and file handling

This code is difficult to read and there really is no common code
between the 2 cases.  For example, there is no need to split a
filename into words.  Separating each of the 2 cases into its own
function makes the logic much easier to understand.

BUG: https://bugzilla.samba.org/show_bug.cgi?id=13800

Signed-off-by: Martin Schwenke <martin@meltin.net>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>

Autobuild-User(master): Amitay Isaacs <amitay@samba.org>
Autobuild-Date(master): Mon Feb 25 03:40:16 CET 2019 on sn-devel-144

(cherry picked from commit c93430fe8f)
(cherry picked from commit d5131afc533102ed5adfb147bf1a316e51810729)
This commit is contained in:
Martin Schwenke
2019-01-21 12:16:43 +11:00
committed by Karolin Seeger
parent 65c3c5801f
commit eb16d3b7bc

View File

@ -118,72 +118,101 @@ static void cluster_mutex_handler(struct tevent_context *ev,
static char cluster_mutex_helper[PATH_MAX+1] = "";
static bool cluster_mutex_helper_args(TALLOC_CTX *mem_ctx,
const char *argstring, char ***argv)
static bool cluster_mutex_helper_args_file(TALLOC_CTX *mem_ctx,
const char *argstring,
char ***argv)
{
int nargs, i, ret, n;
bool is_command = false;
bool ok;
char **args = NULL;
ok = ctdb_set_helper("cluster mutex helper",
cluster_mutex_helper,
sizeof(cluster_mutex_helper),
"CTDB_CLUSTER_MUTEX_HELPER",
CTDB_HELPER_BINDIR,
"ctdb_mutex_fcntl_helper");
if (! ok) {
DBG_ERR("ctdb exiting with error: "
"Unable to set cluster mutex helper\n");
exit(1);
}
/* Array includes default helper, file and NULL */
args = talloc_array(mem_ctx, char *, 3);
if (args == NULL) {
DBG_ERR("Memory allocation error\n");
return false;
}
args[0] = cluster_mutex_helper;
args[1] = talloc_strdup(args, argstring);
if (args[1] == NULL) {
DBG_ERR("Memory allocation error\n");
return false;
}
args[2] = NULL;
*argv = args;
return true;
}
static bool cluster_mutex_helper_args_cmd(TALLOC_CTX *mem_ctx,
const char *argstring,
char ***argv)
{
int i, ret, n;
char **args = NULL;
char *strv = NULL;
char *t = NULL;
if (argstring != NULL && argstring[0] == '!') {
/* This is actually a full command */
is_command = true;
t = discard_const(&argstring[1]);
} else {
is_command = false;
t = discard_const(argstring);
}
ret = strv_split(mem_ctx, &strv, t, " \t");
ret = strv_split(mem_ctx, &strv, argstring, " \t");
if (ret != 0) {
DEBUG(DEBUG_ERR,
("Unable to parse mutex helper string \"%s\" (%s)\n",
argstring, strerror(ret)));
D_ERR("Unable to parse mutex helper command \"%s\" (%s)\n",
argstring,
strerror(ret));
return false;
}
n = strv_count(strv);
args = talloc_array(mem_ctx, char *, n + (is_command ? 1 : 2));
/* Extra slot for NULL */
args = talloc_array(mem_ctx, char *, n + 1);
if (args == NULL) {
DEBUG(DEBUG_ERR,(__location__ " out of memory\n"));
DBG_ERR("Memory allocation error\n");
return false;
}
nargs = 0;
if (! is_command) {
if (!ctdb_set_helper("cluster mutex helper",
cluster_mutex_helper,
sizeof(cluster_mutex_helper),
"CTDB_CLUSTER_MUTEX_HELPER",
CTDB_HELPER_BINDIR,
"ctdb_mutex_fcntl_helper")) {
DEBUG(DEBUG_ERR,("ctdb exiting with error: %s\n",
__location__
" Unable to set cluster mutex helper\n"));
exit(1);
}
args[nargs++] = cluster_mutex_helper;
}
talloc_steal(args, strv);
t = NULL;
for (i = 0; i < n; i++) {
/* Don't copy, just keep cmd_args around */
for (i = 0 ; i < n; i++) {
t = strv_next(strv, t);
args[nargs++] = t;
args[i] = t;
}
/* Make sure last argument is NULL */
args[nargs] = NULL;
args[n] = NULL;
*argv = args;
return true;
}
static bool cluster_mutex_helper_args(TALLOC_CTX *mem_ctx,
const char *argstring,
char ***argv)
{
bool ok;
if (argstring != NULL && argstring[0] == '!') {
ok = cluster_mutex_helper_args_cmd(mem_ctx, &argstring[1], argv);
} else {
ok = cluster_mutex_helper_args_file(mem_ctx, argstring, argv);
}
return ok;
}
struct ctdb_cluster_mutex_handle *
ctdb_cluster_mutex(TALLOC_CTX *mem_ctx,
struct ctdb_context *ctdb,