common: Refactor code to keep track of child processes
authorAmitay Isaacs <amitay@gmail.com>
Tue, 19 Nov 2013 05:13:20 +0000 (16:13 +1100)
committerAmitay Isaacs <amitay@gmail.com>
Tue, 19 Nov 2013 05:47:31 +0000 (16:47 +1100)
This code can then be used to track child processes created with vfork().

Signed-off-by: Amitay Isaacs <amitay@gmail.com>
common/ctdb_fork.c
include/ctdb_private.h

index d372ae0443279d4555d6ce7012180cf102b9d813..1d7d9aa278dd1ce1d27d8f14449805fcd24bec8c 100644 (file)
@@ -45,6 +45,19 @@ bool ctdb_is_child_process(void)
        return is_child;
 }
 
+void ctdb_track_child(struct ctdb_context *ctdb, pid_t pid)
+{
+       char *process;
+
+       /* Only CTDB main daemon should track child processes */
+       if (getpid() != ctdb->ctdbd_pid) {
+               return;
+       }
+
+       process = talloc_asprintf(ctdb->child_processes, "process:%d", (int)pid);
+       trbt_insert32(ctdb->child_processes, pid, process);
+}
+
 /*
  * This function forks a child process and drops the realtime 
  * scheduler for the child process.
@@ -52,7 +65,6 @@ bool ctdb_is_child_process(void)
 pid_t ctdb_fork_no_free_ringbuffer(struct ctdb_context *ctdb)
 {
        pid_t pid;
-       char *process;
 
        pid = fork();
        if (pid == -1) {
@@ -87,13 +99,7 @@ pid_t ctdb_fork_no_free_ringbuffer(struct ctdb_context *ctdb)
                return 0;
        }
 
-       if (getpid() != ctdb->ctdbd_pid) {
-               return pid;
-       }
-
-       process = talloc_asprintf(ctdb->child_processes, "process:%d", (int)pid);
-       trbt_insert32(ctdb->child_processes, pid, process);
-
+       ctdb_track_child(ctdb, pid);
        return pid;
 }
 
index 8135112f62c78567523f62e999aefc3bf22dd722..71c9f139b1f1fba6c778c2c73e09c1287b634b14 100644 (file)
@@ -1084,6 +1084,7 @@ void ctdb_set_scheduler(struct ctdb_context *ctdb);
 void ctdb_restore_scheduler(struct ctdb_context *ctdb);
 
 struct tevent_signal *ctdb_init_sigchld(struct ctdb_context *ctdb);
+void ctdb_track_child(struct ctdb_context *ctdb, pid_t pid);
 pid_t ctdb_fork(struct ctdb_context *ctdb);
 pid_t ctdb_fork_no_free_ringbuffer(struct ctdb_context *ctdb);
 void ctdb_set_child_info(TALLOC_CTX *mem_ctx, const char *child_name_fmt, ...);