ctdb-tool: Allow passing multiple command-line arguments to helper
authorAmitay Isaacs <amitay@gmail.com>
Mon, 21 Nov 2016 03:36:04 +0000 (14:36 +1100)
committerAmitay Isaacs <amitay@samba.org>
Sun, 18 Dec 2016 13:23:22 +0000 (14:23 +0100)
Signed-off-by: Amitay Isaacs <amitay@gmail.com>
Reviewed-by: Martin Schwenke <martin@meltin.net>
ctdb/tools/ctdb.c

index 5f172effcb64686bdf8826220975b74c65c9a92b..dffc103dbe12543062887c0ab7b0111ec8db43bf 100644 (file)
@@ -645,21 +645,36 @@ static int str_to_data(const char *str, size_t len, TALLOC_CTX *mem_ctx,
        return ret;
 }
 
-static int run_helper(const char *command, const char *path, const char *arg1)
+static int run_helper(TALLOC_CTX *mem_ctx, const char *command,
+                     const char *path, int argc, const char **argv)
 {
        pid_t pid;
        int save_errno, status, ret;
+       const char **new_argv;
+       int i;
+
+       new_argv = talloc_array(mem_ctx, const char *, argc + 2);
+       if (new_argv == NULL) {
+               return ENOMEM;
+       }
+
+       new_argv[0] = path;
+       for (i=0; i<argc; i++) {
+               new_argv[i+1] = argv[i];
+       }
+       new_argv[argc+1] = NULL;
 
        pid = fork();
        if (pid < 0) {
                save_errno = errno;
+               talloc_free(new_argv);
                fprintf(stderr, "Failed to fork %s (%s) - %s\n",
                        command, path, strerror(save_errno));
                return save_errno;
        }
 
        if (pid == 0) {
-               ret = execl(path, path, arg1, NULL);
+               ret = execv(path, discard_const(new_argv));
                if (ret == -1) {
                        _exit(errno);
                }
@@ -667,6 +682,8 @@ static int run_helper(const char *command, const char *path, const char *arg1)
                _exit(ENOEXEC);
        }
 
+       talloc_free(new_argv);
+
        ret = waitpid(pid, &status, 0);
        if (ret == -1) {
                save_errno = errno;
@@ -2278,7 +2295,7 @@ static int control_lvs(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
                return 1;
        }
 
-       return run_helper("LVS helper", lvs_helper, argv[0]);
+       return run_helper(mem_ctx, "LVS helper", lvs_helper, argc, argv);
 }
 
 static int control_disable_monitor(TALLOC_CTX *mem_ctx,
@@ -4884,13 +4901,15 @@ static int control_natgw(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
                return 1;
        }
 
-       return run_helper("NAT gateway helper", natgw_helper, argv[0]);
+       return run_helper(mem_ctx, "NAT gateway helper", natgw_helper,
+                         argc, argv);
 }
 
 static int control_natgwlist(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
                             int argc, const char **argv)
 {
        char *t, *natgw_helper = NULL;
+       const char *cmd_argv[] = { "natgwlist", NULL };
 
        if (argc != 0) {
                usage("natgwlist");
@@ -4909,7 +4928,8 @@ static int control_natgwlist(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
                return 1;
        }
 
-       return run_helper("NAT gateway helper", natgw_helper, "natgwlist");
+       return run_helper(mem_ctx, "NAT gateway helper", natgw_helper,
+                         1, cmd_argv);
 }
 
 /*