util: ctdb_fork() closes all sockets opened by the main daemon
authorMartin Schwenke <martin@meltin.net>
Tue, 2 Oct 2012 01:51:24 +0000 (11:51 +1000)
committerMartin Schwenke <martin@meltin.net>
Fri, 5 Oct 2012 01:56:12 +0000 (11:56 +1000)
Do some other hosuekeeping including stopping tevent.

Pair-programmed-with: Amitay Isaacs <amitay@gmail.com>
Signed-off-by: Martin Schwenke <martin@meltin.net>
(This used to be ctdb commit 212298279557a2833ef0f81809b4a5cdac72ca02)

ctdb/client/ctdb_client.c
ctdb/common/ctdb_fork.c

index 9162a476fa5f37d8067e239ecce31221620005dd..d7c30316ec20bb469eb49f5e329b49e0e12cff31 100644 (file)
@@ -4091,9 +4091,11 @@ int ctdb_ctrl_recd_ping(struct ctdb_context *ctdb)
        return 0;
 }
 
-/* when forking the main daemon and the child process needs to connect back
- * to the daemon as a client process, this function can be used to change
- * the ctdb context from daemon into client mode
+/* When forking the main daemon and the child process needs to connect
+ * back to the daemon as a client process, this function can be used
+ * to change the ctdb context from daemon into client mode.  The child
+ * process must be created using ctdb_fork() and not fork() -
+ * ctdb_fork() does some necessary housekeeping.
  */
 int switch_from_server_to_client(struct ctdb_context *ctdb, const char *fmt, ...)
 {
@@ -4105,25 +4107,11 @@ int switch_from_server_to_client(struct ctdb_context *ctdb, const char *fmt, ...
        debug_extra = talloc_strdup_append(talloc_vasprintf(NULL, fmt, ap), ":");
        va_end(ap);
 
-       /* shutdown the transport */
-       if (ctdb->methods) {
-               ctdb->methods->shutdown(ctdb);
-       }
-
        /* get a new event context */
-       talloc_free(ctdb->ev);
        ctdb->ev = event_context_init(ctdb);
        tevent_loop_allow_nesting(ctdb->ev);
 
-       close(ctdb->daemon.sd);
-       ctdb->daemon.sd = -1;
-
-       /* the client does not need to be realtime */
-       if (ctdb->do_setsched) {
-               ctdb_restore_scheduler(ctdb);
-       }
-
-       /* initialise ctdb */
+       /* Connect to main CTDB daemon */
        ret = ctdb_socket_connect(ctdb);
        if (ret != 0) {
                DEBUG(DEBUG_ALERT, (__location__ " Failed to init ctdb client\n"));
index 81055c5fc92fba5b922b7ec7e50b9ec3caf4080a..24534e66c0ae477840a26c0034032ebc8bf58e9d 100644 (file)
@@ -37,6 +37,24 @@ pid_t ctdb_fork(struct ctdb_context *ctdb)
                return -1;
        }
        if (pid == 0) {
+               /* Close the Unix Domain socket and the TCP socket.
+                * This ensures that none of the child processes will
+                * look like the main daemon when it is not running.
+                * tevent needs to be stopped before closing sockets.
+                */
+               if (ctdb->ev != NULL) {
+                       talloc_free(ctdb->ev);
+                       ctdb->ev = NULL;
+               }
+               if (ctdb->daemon.sd != -1) {
+                       close(ctdb->daemon.sd);
+                       ctdb->daemon.sd = -1;
+               }
+               if (ctdb->methods != NULL) {
+                       ctdb->methods->shutdown(ctdb);
+               }
+
+               /* The child does not need to be realtime */
                if (ctdb->do_setsched) {
                        ctdb_restore_scheduler(ctdb);
                }