ctdb-daemon: Replace ctdb_fork_with_logging with ctdb_vfork_with_logging (part 2)
authorAmitay Isaacs <amitay@gmail.com>
Wed, 18 Dec 2013 03:07:57 +0000 (14:07 +1100)
committerMartin Schwenke <martin@meltin.net>
Thu, 16 Jan 2014 01:11:38 +0000 (12:11 +1100)
Use ctdb_event_helper to run debug-hung-script.sh.

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

index 2c5a61271512329c0a3531ee8e5500432c37b80b..2928c11abac21f138deedca2bbb3b9ab74581a57 100644 (file)
@@ -602,44 +602,46 @@ static void ctdb_run_debug_hung_script(struct ctdb_context *ctdb, struct debug_h
        int fd[2];
        struct tevent_timer *ttimer;
        struct tevent_fd *tfd;
+       const char **argv;
+       int i;
 
-       if (pipe(fd) < 0) {
-               DEBUG(DEBUG_ERR,("Failed to create pipe fd for debug hung script\n"));
+       if (helper_prog == NULL) {
                return;
        }
 
-       if (!ctdb_fork_with_logging(ctdb, ctdb, "Hung script", NULL, NULL, &pid)) {
-               DEBUG(DEBUG_ERR,("Failed to fork a child process with logging to track hung event script\n"));
-               close(fd[0]);
-               close(fd[1]);
-               return;
-       }
-       if (pid == -1) {
-               DEBUG(DEBUG_ERR,("Fork for debug script failed : %s\n",
-                                strerror(errno)));
-               close(fd[0]);
-               close(fd[1]);
+       if (pipe(fd) < 0) {
+               DEBUG(DEBUG_ERR,("Failed to create pipe fd for debug hung script\n"));
                return;
        }
-       if (pid == 0) {
-               char *buf;
 
-               ctdb_set_process_name("ctdb_debug_hung_script");
-               if (getenv("CTDB_DEBUG_HUNG_SCRIPT") != NULL) {
-                       debug_hung_script = getenv("CTDB_DEBUG_HUNG_SCRIPT");
-               }
+       argv = talloc_array(state, const char *, 5);
 
-               close(fd[0]);
+       argv[0] = talloc_asprintf(argv, "%d", fd[1]);
+       argv[1] = talloc_strdup(argv, debug_hung_script);
+       argv[2] = talloc_asprintf(argv, "%d", state->child);
+       argv[3] = talloc_strdup(argv, ctdb_eventscript_call_names[state->call]);
+       argv[4] = NULL;
+
+       for (i=0; i<4; i++) {
+               if (argv[i] == NULL) {
+                       close(fd[0]);
+                       close(fd[1]);
+                       talloc_free(argv);
+                       return;
+               }
+       }
 
-               buf = talloc_asprintf(NULL, "%s %d %s",
-                                     debug_hung_script, state->child,
-                                     ctdb_eventscript_call_names[state->call]);
-               system(buf);
-               talloc_free(buf);
 
-               _exit(0);
+       if (!ctdb_vfork_with_logging(state, ctdb, "Hung-script",
+                                    helper_prog, 5, argv, NULL, NULL, &pid)) {
+               DEBUG(DEBUG_ERR,("Failed to fork a child to track hung event script\n"));
+               talloc_free(argv);
+               close(fd[0]);
+               close(fd[1]);
+               return;
        }
 
+       talloc_free(argv);
        close(fd[1]);
 
        ttimer = tevent_add_timer(ctdb->ev, state,