ctdb-daemon: Bind to Unix domain socket after PID file creation
[sfrench/samba-autobuild/.git] / ctdb / server / ctdb_daemon.c
index f946ac61da966fcaffdfcc39b171fc248d58f326..a4e4f40555aeb58ac62ff05e78ce986de2c70d9c 100644 (file)
    along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
 
-#include "includes.h"
-#include "db_wrap.h"
-#include "lib/tdb/include/tdb.h"
-#include "lib/util/dlinklist.h"
+#include "replace.h"
 #include "system/network.h"
 #include "system/filesys.h"
 #include "system/wait.h"
-#include "../include/ctdb_client.h"
-#include "../include/ctdb_private.h"
-#include "../common/rb_tree.h"
-#include <sys/socket.h>
+#include "system/time.h"
+
+#include <talloc.h>
+/* Allow use of deprecated function tevent_loop_allow_nesting() */
+#define TEVENT_DEPRECATED
+#include <tevent.h>
+#include <tdb.h>
+
+#include "lib/tdb_wrap/tdb_wrap.h"
+#include "lib/util/dlinklist.h"
+#include "lib/util/debug.h"
+#include "lib/util/samba_util.h"
+#include "lib/util/blocking.h"
+
+#include "ctdb_version.h"
+#include "ctdb_private.h"
+#include "ctdb_client.h"
+
+#include "common/rb_tree.h"
+#include "common/reqid.h"
+#include "common/system.h"
+#include "common/common.h"
+#include "common/logging.h"
+#include "common/pidfile.h"
 
 struct ctdb_client_pid_list {
        struct ctdb_client_pid_list *next, *prev;
@@ -36,27 +53,37 @@ struct ctdb_client_pid_list {
        struct ctdb_client *client;
 };
 
+const char *ctdbd_pidfile = NULL;
+static struct pidfile_context *ctdbd_pidfile_ctx = NULL;
+
 static void daemon_incoming_packet(void *, struct ctdb_req_header *);
 
 static void print_exit_message(void)
 {
-       DEBUG(DEBUG_NOTICE,("CTDB daemon shutting down\n"));
+       if (debug_extra != NULL && debug_extra[0] != '\0') {
+               DEBUG(DEBUG_NOTICE,("CTDB %s shutting down\n", debug_extra));
+       } else {
+               DEBUG(DEBUG_NOTICE,("CTDB daemon shutting down\n"));
+
+               /* Wait a second to allow pending log messages to be flushed */
+               sleep(1);
+       }
 }
 
 
 
-static void ctdb_time_tick(struct event_context *ev, struct timed_event *te, 
+static void ctdb_time_tick(struct tevent_context *ev, struct tevent_timer *te,
                                  struct timeval t, void *private_data)
 {
        struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
 
-       if (getpid() != ctdbd_pid) {
+       if (getpid() != ctdb->ctdbd_pid) {
                return;
        }
 
-       event_add_timed(ctdb->ev, ctdb, 
-                       timeval_current_ofs(1, 0), 
-                       ctdb_time_tick, ctdb);
+       tevent_add_timer(ctdb->ev, ctdb,
+                        timeval_current_ofs(1, 0),
+                        ctdb_time_tick, ctdb);
 }
 
 /* Used to trigger a dummy event once per second, to make
@@ -64,41 +91,16 @@ static void ctdb_time_tick(struct event_context *ev, struct timed_event *te,
  */
 static void ctdb_start_time_tickd(struct ctdb_context *ctdb)
 {
-       event_add_timed(ctdb->ev, ctdb, 
-                       timeval_current_ofs(1, 0), 
-                       ctdb_time_tick, ctdb);
+       tevent_add_timer(ctdb->ev, ctdb,
+                        timeval_current_ofs(1, 0),
+                        ctdb_time_tick, ctdb);
 }
 
-
-/* called when the "startup" event script has finished */
-static void ctdb_start_transport(struct ctdb_context *ctdb)
+static void ctdb_start_periodic_events(struct ctdb_context *ctdb)
 {
-       if (ctdb->methods == NULL) {
-               DEBUG(DEBUG_ALERT,(__location__ " startup event finished but transport is DOWN.\n"));
-               ctdb_fatal(ctdb, "transport is not initialized but startup completed");
-       }
-
-       /* start the transport running */
-       if (ctdb->methods->start(ctdb) != 0) {
-               DEBUG(DEBUG_ALERT,("transport failed to start!\n"));
-               ctdb_fatal(ctdb, "transport failed to start");
-       }
-
-       /* start the recovery daemon process */
-       if (ctdb_start_recoverd(ctdb) != 0) {
-               DEBUG(DEBUG_ALERT,("Failed to start recovery daemon\n"));
-               exit(11);
-       }
-
-       /* Make sure we log something when the daemon terminates */
-       atexit(print_exit_message);
-
        /* start monitoring for connected/disconnected nodes */
        ctdb_start_keepalive(ctdb);
 
-       /* start monitoring for node health */
-       ctdb_start_monitoring(ctdb);
-
        /* start periodic update of tcp tickle lists */
                ctdb_start_tcp_tickle_update(ctdb);
 
@@ -109,7 +111,7 @@ static void ctdb_start_transport(struct ctdb_context *ctdb)
        ctdb_start_time_tickd(ctdb);
 }
 
-static void block_signal(int signum)
+static void ignore_signal(int signum)
 {
        struct sigaction act;
 
@@ -142,18 +144,18 @@ static int daemon_queue_send(struct ctdb_client *client, struct ctdb_req_header
   message handler for when we are in daemon mode. This redirects the message
   to the right client
  */
-static void daemon_message_handler(struct ctdb_context *ctdb, uint64_t srvid, 
-                                   TDB_DATA data, void *private_data)
+static void daemon_message_handler(uint64_t srvid, TDB_DATA data,
+                                  void *private_data)
 {
        struct ctdb_client *client = talloc_get_type(private_data, struct ctdb_client);
-       struct ctdb_req_message *r;
+       struct ctdb_req_message_old *r;
        int len;
 
        /* construct a message to send to the client containing the data */
-       len = offsetof(struct ctdb_req_message, data) + data.dsize;
-       r = ctdbd_allocate_pkt(ctdb, ctdb, CTDB_REQ_MESSAGE, 
-                              len, struct ctdb_req_message);
-       CTDB_NO_MEMORY_VOID(ctdb, r);
+       len = offsetof(struct ctdb_req_message_old, data) + data.dsize;
+       r = ctdbd_allocate_pkt(client->ctdb, client->ctdb, CTDB_REQ_MESSAGE,
+                              len, struct ctdb_req_message_old);
+       CTDB_NO_MEMORY_VOID(client->ctdb, r);
 
        talloc_set_name_const(r, "req_message packet");
 
@@ -172,13 +174,14 @@ static void daemon_message_handler(struct ctdb_context *ctdb, uint64_t srvid,
  */
 int daemon_register_message_handler(struct ctdb_context *ctdb, uint32_t client_id, uint64_t srvid)
 {
-       struct ctdb_client *client = ctdb_reqid_find(ctdb, client_id, struct ctdb_client);
+       struct ctdb_client *client = reqid_find(ctdb->idr, client_id, struct ctdb_client);
        int res;
        if (client == NULL) {
                DEBUG(DEBUG_ERR,("Bad client_id in daemon_request_register_message_handler\n"));
                return -1;
        }
-       res = ctdb_register_message_handler(ctdb, client, srvid, daemon_message_handler, client);
+       res = srvid_register(ctdb->srv, client, srvid, daemon_message_handler,
+                            client);
        if (res != 0) {
                DEBUG(DEBUG_ERR,(__location__ " Failed to register handler %llu in daemon\n", 
                         (unsigned long long)srvid));
@@ -196,12 +199,12 @@ int daemon_register_message_handler(struct ctdb_context *ctdb, uint32_t client_i
  */
 int daemon_deregister_message_handler(struct ctdb_context *ctdb, uint32_t client_id, uint64_t srvid)
 {
-       struct ctdb_client *client = ctdb_reqid_find(ctdb, client_id, struct ctdb_client);
+       struct ctdb_client *client = reqid_find(ctdb->idr, client_id, struct ctdb_client);
        if (client == NULL) {
                DEBUG(DEBUG_ERR,("Bad client_id in daemon_request_deregister_message_handler\n"));
                return -1;
        }
-       return ctdb_deregister_message_handler(ctdb, srvid, client);
+       return srvid_deregister(ctdb->srv, srvid, client);
 }
 
 int daemon_check_srvids(struct ctdb_context *ctdb, TDB_DATA indata,
@@ -226,13 +229,7 @@ int daemon_check_srvids(struct ctdb_context *ctdb, TDB_DATA indata,
                return -1;
        }
        for (i=0; i<num_ids; i++) {
-               struct ctdb_message_list *ml;
-               for (ml=ctdb->message_list; ml; ml=ml->next) {
-                       if (ml->srvid == ids[i]) {
-                               break;
-                       }
-               }
-               if (ml != NULL) {
+               if (srvid_exists(ctdb->srv, ids[i]) == 0) {
                        results[i/8] |= (1 << (i%8));
                }
        }
@@ -249,8 +246,8 @@ static int ctdb_client_destructor(struct ctdb_client *client)
        struct ctdb_db_context *ctdb_db;
 
        ctdb_takeover_client_destructor_hook(client);
-       ctdb_reqid_remove(client->ctdb, client->client_id);
-       CTDB_DECREMENT_STAT(client->ctdb, num_clients);
+       reqid_remove(client->ctdb->idr, client->client_id);
+       client->ctdb->num_clients--;
 
        if (client->num_persistent_updates != 0) {
                DEBUG(DEBUG_ERR,(__location__ " Client disconnecting with %u persistent updates in flight. Starting recovery\n", client->num_persistent_updates));
@@ -262,9 +259,6 @@ static int ctdb_client_destructor(struct ctdb_client *client)
                                  "commit active. Forcing recovery.\n"));
                client->ctdb->recovery_mode = CTDB_RECOVERY_ACTIVE;
 
-               /* legacy trans2 transaction state: */
-               ctdb_db->transaction_active = false;
-
                /*
                 * trans3 transaction state:
                 *
@@ -282,7 +276,7 @@ static int ctdb_client_destructor(struct ctdb_client *client)
   from a local client over the unix domain socket
  */
 static void daemon_request_message_from_client(struct ctdb_client *client, 
-                                              struct ctdb_req_message *c)
+                                              struct ctdb_req_message_old *c)
 {
        TDB_DATA data;
        int res;
@@ -327,7 +321,7 @@ static void daemon_call_from_client_callback(struct ctdb_call_state *state)
 {
        struct daemon_call_state *dstate = talloc_get_type(state->async.private_data, 
                                                           struct daemon_call_state);
-       struct ctdb_reply_call *r;
+       struct ctdb_reply_call_old *r;
        int res;
        uint32_t length;
        struct ctdb_client *client = dstate->client;
@@ -345,7 +339,7 @@ static void daemon_call_from_client_callback(struct ctdb_call_state *state)
                return;
        }
 
-       length = offsetof(struct ctdb_reply_call, data) + dstate->call->reply_data.dsize;
+       length = offsetof(struct ctdb_reply_call_old, data) + dstate->call->reply_data.dsize;
        /* If the client asked for readonly FETCH, we remapped this to 
           FETCH_WITH_HEADER when calling the daemon. So we must
           strip the extra header off the reply data before passing
@@ -357,7 +351,7 @@ static void daemon_call_from_client_callback(struct ctdb_call_state *state)
        }
 
        r = ctdbd_allocate_pkt(client->ctdb, dstate, CTDB_REPLY_CALL, 
-                              length, struct ctdb_reply_call);
+                              length, struct ctdb_reply_call_old);
        if (r == NULL) {
                DEBUG(DEBUG_ERR, (__location__ " Failed to allocate reply_call in ctdb daemon\n"));
                CTDB_DECREMENT_STAT(client->ctdb, pending_calls);
@@ -410,7 +404,7 @@ static void daemon_incoming_packet_wrap(void *p, struct ctdb_req_header *hdr)
                return;
        }
 
-       client = ctdb_reqid_find(w->ctdb, w->client_id, struct ctdb_client);
+       client = reqid_find(w->ctdb->idr, w->client_id, struct ctdb_client);
        if (client == NULL) {
                DEBUG(DEBUG_ERR,(__location__ " Packet for disconnected client %u\n",
                         w->client_id));
@@ -425,7 +419,7 @@ static void daemon_incoming_packet_wrap(void *p, struct ctdb_req_header *hdr)
 
 struct ctdb_deferred_fetch_call {
        struct ctdb_deferred_fetch_call *next, *prev;
-       struct ctdb_req_call *c;
+       struct ctdb_req_call_old *c;
        struct ctdb_daemon_packet_wrap *w;
 };
 
@@ -439,8 +433,9 @@ struct ctdb_deferred_requeue {
 };
 
 /* called from a timer event and starts reprocessing the deferred call.*/
-static void reprocess_deferred_call(struct event_context *ev, struct timed_event *te, 
-                                      struct timeval t, void *private_data)
+static void reprocess_deferred_call(struct tevent_context *ev,
+                                   struct tevent_timer *te,
+                                   struct timeval t, void *private_data)
 {
        struct ctdb_deferred_requeue *dfr = (struct ctdb_deferred_requeue *)private_data;
        struct ctdb_client *client = dfr->client;
@@ -471,7 +466,7 @@ static int deferred_fetch_queue_destructor(struct ctdb_deferred_fetch_queue *dfq
 
                DLIST_REMOVE(dfq->deferred_calls, dfc);
 
-               client = ctdb_reqid_find(dfc->w->ctdb, dfc->w->client_id, struct ctdb_client);
+               client = reqid_find(dfc->w->ctdb->idr, dfc->w->client_id, struct ctdb_client);
                if (client == NULL) {
                        DEBUG(DEBUG_ERR,(__location__ " Packet for disconnected client %u\n",
                                 dfc->w->client_id));
@@ -488,7 +483,8 @@ static int deferred_fetch_queue_destructor(struct ctdb_deferred_fetch_queue *dfq
                dfr->dfc    = talloc_steal(dfr, dfc);
                dfr->client = client;
 
-               event_add_timed(dfc->w->ctdb->ev, client, timeval_zero(), reprocess_deferred_call, dfr);
+               tevent_add_timer(dfc->w->ctdb->ev, client, timeval_zero(),
+                                reprocess_deferred_call, dfr);
        }
 
        return 0;
@@ -512,8 +508,8 @@ static void *insert_dfq_callback(void *parm, void *data)
    free the context and context for all deferred requests to cause them to be
    re-inserted into the event system.
 */
-static void dfq_timeout(struct event_context *ev, struct timed_event *te, 
-                                 struct timeval t, void *private_data)
+static void dfq_timeout(struct tevent_context *ev, struct tevent_timer *te,
+                       struct timeval t, void *private_data)
 {
        talloc_free(private_data);
 }
@@ -528,15 +524,12 @@ static int setup_deferred_fetch_locks(struct ctdb_db_context *ctdb_db, struct ct
        uint32_t *k;
        struct ctdb_deferred_fetch_queue *dfq;
 
-       k = talloc_zero_size(call, ((call->key.dsize + 3) & 0xfffffffc) + 4);
+       k = ctdb_key_to_idkey(call, call->key);
        if (k == NULL) {
                DEBUG(DEBUG_ERR,("Failed to allocate key for deferred fetch\n"));
                return -1;
        }
 
-       k[0] = (call->key.dsize + 3) / 4 + 1;
-       memcpy(&k[1], call->key.dptr, call->key.dsize);
-
        dfq  = talloc(call, struct ctdb_deferred_fetch_queue);
        if (dfq == NULL) {
                DEBUG(DEBUG_ERR,("Failed to allocate key for deferred fetch queue structure\n"));
@@ -551,7 +544,8 @@ static int setup_deferred_fetch_locks(struct ctdb_db_context *ctdb_db, struct ct
 
        /* if the fetch havent completed in 30 seconds, just tear it all down
           and let it try again as the events are reissued */
-       event_add_timed(ctdb_db->ctdb->ev, dfq, timeval_current_ofs(30, 0), dfq_timeout, dfq);
+       tevent_add_timer(ctdb_db->ctdb->ev, dfq, timeval_current_ofs(30, 0),
+                        dfq_timeout, dfq);
 
        talloc_free(k);
        return 0;
@@ -561,21 +555,18 @@ static int setup_deferred_fetch_locks(struct ctdb_db_context *ctdb_db, struct ct
    if it is, make this call deferred to be reprocessed later when
    the in-flight fetch completes.
 */
-static int requeue_duplicate_fetch(struct ctdb_db_context *ctdb_db, struct ctdb_client *client, TDB_DATA key, struct ctdb_req_call *c)
+static int requeue_duplicate_fetch(struct ctdb_db_context *ctdb_db, struct ctdb_client *client, TDB_DATA key, struct ctdb_req_call_old *c)
 {
        uint32_t *k;
        struct ctdb_deferred_fetch_queue *dfq;
        struct ctdb_deferred_fetch_call *dfc;
 
-       k = talloc_zero_size(c, ((key.dsize + 3) & 0xfffffffc) + 4);
+       k = ctdb_key_to_idkey(c, key);
        if (k == NULL) {
                DEBUG(DEBUG_ERR,("Failed to allocate key for deferred fetch\n"));
                return -1;
        }
 
-       k[0] = (key.dsize + 3) / 4 + 1;
-       memcpy(&k[1], key.dptr, key.dsize);
-
        dfq = trbt_lookuparray32(ctdb_db->deferred_fetch, k[0], &k[0]);
        if (dfq == NULL) {
                talloc_free(k);
@@ -602,7 +593,7 @@ static int requeue_duplicate_fetch(struct ctdb_db_context *ctdb_db, struct ctdb_
        dfc->w->ctdb = ctdb_db->ctdb;
        dfc->w->client_id = client->client_id;
 
-       DLIST_ADD_END(dfq->deferred_calls, dfc, NULL);
+       DLIST_ADD_END(dfq->deferred_calls, dfc);
 
        return 0;
 }
@@ -613,7 +604,7 @@ static int requeue_duplicate_fetch(struct ctdb_db_context *ctdb_db, struct ctdb_
   from a local client over the unix domain socket
  */
 static void daemon_request_call_from_client(struct ctdb_client *client, 
-                                           struct ctdb_req_call *c)
+                                           struct ctdb_req_call_old *c)
 {
        struct ctdb_call_state *state;
        struct ctdb_db_context *ctdb_db;
@@ -626,7 +617,7 @@ static void daemon_request_call_from_client(struct ctdb_client *client,
        struct ctdb_daemon_packet_wrap *w;
 
        CTDB_INCREMENT_STAT(ctdb, total_calls);
-       CTDB_DECREMENT_STAT(ctdb, pending_calls);
+       CTDB_INCREMENT_STAT(ctdb, pending_calls);
 
        ctdb_db = find_ctdb_db(client->ctdb, c->db_id);
        if (!ctdb_db) {
@@ -657,7 +648,7 @@ static void daemon_request_call_from_client(struct ctdb_client *client,
 
        ret = ctdb_ltdb_lock_fetch_requeue(ctdb_db, key, &header, 
                                           (struct ctdb_req_header *)c, &data,
-                                          daemon_incoming_packet_wrap, w, True);
+                                          daemon_incoming_packet_wrap, w, true);
        if (ret == -2) {
                /* will retry later */
                CTDB_DECREMENT_STAT(ctdb, pending_calls);
@@ -683,17 +674,18 @@ static void daemon_request_call_from_client(struct ctdb_client *client,
                        if (ret != 0) {
                                DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
                        }
+                       CTDB_DECREMENT_STAT(ctdb, pending_calls);
                        return;
                }
        }
 
-       /* Dont do READONLY if we dont have a tracking database */
+       /* Dont do READONLY if we don't have a tracking database */
        if ((c->flags & CTDB_WANT_READONLY) && !ctdb_db->readonly) {
                c->flags &= ~CTDB_WANT_READONLY;
        }
 
        if (header.flags & CTDB_REC_RO_REVOKE_COMPLETE) {
-               header.flags &= ~(CTDB_REC_RO_HAVE_DELEGATIONS|CTDB_REC_RO_HAVE_READONLY|CTDB_REC_RO_REVOKING_READONLY|CTDB_REC_RO_REVOKE_COMPLETE);
+               header.flags &= ~CTDB_REC_RO_FLAGS;
                CTDB_INCREMENT_STAT(ctdb, total_ro_revokes);
                CTDB_INCREMENT_DB_STAT(ctdb_db, db_ro_revokes);
                if (ctdb_ltdb_store(ctdb_db, key, &header, data) != 0) {
@@ -715,6 +707,7 @@ static void daemon_request_call_from_client(struct ctdb_client *client,
                if (ctdb_add_revoke_deferred_call(ctdb, ctdb_db, key, (struct ctdb_req_header *)c, daemon_incoming_packet, client) != 0) {
                        ctdb_fatal(ctdb, "Failed to add deferred call for revoke child");
                }
+               CTDB_DECREMENT_STAT(ctdb, pending_calls);
                return;
        }
 
@@ -736,6 +729,7 @@ static void daemon_request_call_from_client(struct ctdb_client *client,
                        ctdb_fatal(ctdb, "Failed to add deferred call for revoke child");
                }
 
+               CTDB_DECREMENT_STAT(ctdb, pending_calls);
                return;
        }               
 
@@ -820,7 +814,7 @@ static void daemon_request_call_from_client(struct ctdb_client *client,
 
 
 static void daemon_request_control_from_client(struct ctdb_client *client, 
-                                              struct ctdb_req_control *c);
+                                              struct ctdb_req_control_old *c);
 
 /* data contains a packet from the client */
 static void daemon_incoming_packet(void *p, struct ctdb_req_header *hdr)
@@ -841,7 +835,7 @@ static void daemon_incoming_packet(void *p, struct ctdb_req_header *hdr)
                goto done;
        }
 
-       if (hdr->ctdb_version != CTDB_VERSION) {
+       if (hdr->ctdb_version != CTDB_PROTOCOL) {
                ctdb_set_error(client->ctdb, "Bad CTDB version 0x%x rejected in daemon\n", hdr->ctdb_version);
                goto done;
        }
@@ -849,17 +843,17 @@ static void daemon_incoming_packet(void *p, struct ctdb_req_header *hdr)
        switch (hdr->operation) {
        case CTDB_REQ_CALL:
                CTDB_INCREMENT_STAT(ctdb, client.req_call);
-               daemon_request_call_from_client(client, (struct ctdb_req_call *)hdr);
+               daemon_request_call_from_client(client, (struct ctdb_req_call_old *)hdr);
                break;
 
        case CTDB_REQ_MESSAGE:
                CTDB_INCREMENT_STAT(ctdb, client.req_message);
-               daemon_request_message_from_client(client, (struct ctdb_req_message *)hdr);
+               daemon_request_message_from_client(client, (struct ctdb_req_message_old *)hdr);
                break;
 
        case CTDB_REQ_CONTROL:
                CTDB_INCREMENT_STAT(ctdb, client.req_control);
-               daemon_request_control_from_client(client, (struct ctdb_req_control *)hdr);
+               daemon_request_control_from_client(client, (struct ctdb_req_control_old *)hdr);
                break;
 
        default:
@@ -903,7 +897,7 @@ static void ctdb_daemon_read_cb(uint8_t *data, size_t cnt, void *args)
                return;
        }
 
-       if (hdr->ctdb_version != CTDB_VERSION) {
+       if (hdr->ctdb_version != CTDB_PROTOCOL) {
                ctdb_set_error(client->ctdb, "Bad CTDB version 0x%x rejected in daemon\n", hdr->ctdb_version);
                return;
        }
@@ -927,8 +921,9 @@ static int ctdb_clientpid_destructor(struct ctdb_client_pid_list *client_pid)
 }
 
 
-static void ctdb_accept_client(struct event_context *ev, struct fd_event *fde, 
-                        uint16_t flags, void *private_data)
+static void ctdb_accept_client(struct tevent_context *ev,
+                              struct tevent_fd *fde, uint16_t flags,
+                              void *private_data)
 {
        struct sockaddr_un addr;
        socklen_t len;
@@ -937,6 +932,7 @@ static void ctdb_accept_client(struct event_context *ev, struct fd_event *fde,
        struct ctdb_client *client;
        struct ctdb_client_pid_list *client_pid;
        pid_t peer_pid = 0;
+       int ret;
 
        memset(&addr, 0, sizeof(addr));
        len = sizeof(addr);
@@ -945,7 +941,16 @@ static void ctdb_accept_client(struct event_context *ev, struct fd_event *fde,
                return;
        }
 
-       set_nonblocking(fd);
+       ret = set_blocking(fd, false);
+       if (ret != 0) {
+               DEBUG(DEBUG_ERR,
+                     (__location__
+                      " failed to set socket non-blocking (%s)\n",
+                      strerror(errno)));
+               close(fd);
+               return;
+       }
+
        set_close_on_exec(fd);
 
        DEBUG(DEBUG_DEBUG,(__location__ " Created SOCKET FD:%d to connected child\n", fd));
@@ -957,7 +962,7 @@ static void ctdb_accept_client(struct event_context *ev, struct fd_event *fde,
 
        client->ctdb = ctdb;
        client->fd = fd;
-       client->client_id = ctdb_reqid_new(ctdb, client);
+       client->client_id = reqid_new(ctdb->idr, client);
        client->pid = peer_pid;
 
        client_pid = talloc(client, struct ctdb_client_pid_list);
@@ -979,7 +984,7 @@ static void ctdb_accept_client(struct event_context *ev, struct fd_event *fde,
 
        talloc_set_destructor(client, ctdb_client_destructor);
        talloc_set_destructor(client_pid, ctdb_clientpid_destructor);
-       CTDB_INCREMENT_STAT(ctdb, num_clients);
+       ctdb->num_clients++;
 }
 
 
@@ -991,29 +996,50 @@ static void ctdb_accept_client(struct event_context *ev, struct fd_event *fde,
 static int ux_socket_bind(struct ctdb_context *ctdb)
 {
        struct sockaddr_un addr;
+       int ret;
 
        ctdb->daemon.sd = socket(AF_UNIX, SOCK_STREAM, 0);
        if (ctdb->daemon.sd == -1) {
                return -1;
        }
 
-       set_close_on_exec(ctdb->daemon.sd);
-       set_nonblocking(ctdb->daemon.sd);
-
        memset(&addr, 0, sizeof(addr));
        addr.sun_family = AF_UNIX;
-       strncpy(addr.sun_path, ctdb->daemon.name, sizeof(addr.sun_path));
+       strncpy(addr.sun_path, ctdb->daemon.name, sizeof(addr.sun_path)-1);
+
+       /* First check if an old ctdbd might be running */
+       if (connect(ctdb->daemon.sd,
+                   (struct sockaddr *)&addr, sizeof(addr)) == 0) {
+               DEBUG(DEBUG_CRIT,
+                     ("Something is already listening on ctdb socket '%s'\n",
+                      ctdb->daemon.name));
+               goto failed;
+       }
+
+       /* Remove any old socket */
+       unlink(ctdb->daemon.name);
+
+       set_close_on_exec(ctdb->daemon.sd);
+
+       ret = set_blocking(ctdb->daemon.sd, false);
+       if (ret != 0) {
+               DEBUG(DEBUG_ERR,
+                     (__location__
+                      " failed to set socket non-blocking (%s)\n",
+                      strerror(errno)));
+               goto failed;
+       }
 
        if (bind(ctdb->daemon.sd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
                DEBUG(DEBUG_CRIT,("Unable to bind on ctdb socket '%s'\n", ctdb->daemon.name));
                goto failed;
-       }       
+       }
 
        if (chown(ctdb->daemon.name, geteuid(), getegid()) != 0 ||
            chmod(ctdb->daemon.name, 0700) != 0) {
                DEBUG(DEBUG_CRIT,("Unable to secure ctdb socket '%s', ctdb->daemon.name\n", ctdb->daemon.name));
                goto failed;
-       } 
+       }
 
 
        if (listen(ctdb->daemon.sd, 100) != 0) {
@@ -1021,6 +1047,8 @@ static int ux_socket_bind(struct ctdb_context *ctdb)
                goto failed;
        }
 
+       DEBUG(DEBUG_NOTICE, ("Listening to ctdb socket %s\n",
+                            ctdb->daemon.name));
        return 0;
 
 failed:
@@ -1029,24 +1057,23 @@ failed:
        return -1;      
 }
 
-static void sig_child_handler(struct event_context *ev,
-       struct signal_event *se, int signum, int count,
-       void *dont_care, 
-       void *private_data)
+static void initialise_node_flags (struct ctdb_context *ctdb)
 {
-//     struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
-       int status;
-       pid_t pid = -1;
-
-       while (pid != 0) {
-               pid = waitpid(-1, &status, WNOHANG);
-               if (pid == -1) {
-                       DEBUG(DEBUG_ERR, (__location__ " waitpid() returned error. errno:%d\n", errno));
-                       return;
-               }
-               if (pid > 0) {
-                       DEBUG(DEBUG_DEBUG, ("SIGCHLD from %d\n", (int)pid));
-               }
+       if (ctdb->pnn == -1) {
+               ctdb_fatal(ctdb, "PNN is set to -1 (unknown value)");
+       }
+
+       ctdb->nodes[ctdb->pnn]->flags &= ~NODE_FLAGS_DISCONNECTED;
+
+       /* do we start out in DISABLED mode? */
+       if (ctdb->start_as_disabled != 0) {
+               DEBUG(DEBUG_NOTICE, ("This node is configured to start in DISABLED state\n"));
+               ctdb->nodes[ctdb->pnn]->flags |= NODE_FLAGS_DISABLED;
+       }
+       /* do we start out in STOPPED mode? */
+       if (ctdb->start_as_stopped != 0) {
+               DEBUG(DEBUG_NOTICE, ("This node is configured to start in STOPPED state\n"));
+               ctdb->nodes[ctdb->pnn]->flags |= NODE_FLAGS_STOPPED;
        }
 }
 
@@ -1054,8 +1081,7 @@ static void ctdb_setup_event_callback(struct ctdb_context *ctdb, int status,
                                      void *private_data)
 {
        if (status != 0) {
-               ctdb_fatal(ctdb, "Failed to run setup event\n");
-               return;
+               ctdb_die(ctdb, "Failed to run setup event");
        }
        ctdb_run_notification_script(ctdb, "setup");
 
@@ -1064,85 +1090,240 @@ static void ctdb_setup_event_callback(struct ctdb_context *ctdb, int status,
                                 0, CTDB_CONTROL_STARTUP, 0,
                                 CTDB_CTRL_FLAG_NOREPLY,
                                 tdb_null, NULL, NULL);
+
+       /* Start the recovery daemon */
+       if (ctdb_start_recoverd(ctdb) != 0) {
+               DEBUG(DEBUG_ALERT,("Failed to start recovery daemon\n"));
+               exit(11);
+       }
+
+       ctdb_start_periodic_events(ctdb);
+
+       ctdb_wait_for_first_recovery(ctdb);
+}
+
+static struct timeval tevent_before_wait_ts;
+static struct timeval tevent_after_wait_ts;
+
+static void ctdb_tevent_trace_init(void)
+{
+       struct timeval now;
+
+       now = timeval_current();
+
+       tevent_before_wait_ts = now;
+       tevent_after_wait_ts = now;
+}
+
+static void ctdb_tevent_trace(enum tevent_trace_point tp,
+                             void *private_data)
+{
+       struct timeval diff;
+       struct timeval now;
+       struct ctdb_context *ctdb =
+               talloc_get_type(private_data, struct ctdb_context);
+
+       if (getpid() != ctdb->ctdbd_pid) {
+               return;
+       }
+
+       now = timeval_current();
+
+       switch (tp) {
+       case TEVENT_TRACE_BEFORE_WAIT:
+               diff = timeval_until(&tevent_after_wait_ts, &now);
+               if (diff.tv_sec > 3) {
+                       DEBUG(DEBUG_ERR,
+                             ("Handling event took %ld seconds!\n",
+                              diff.tv_sec));
+               }
+               tevent_before_wait_ts = now;
+               break;
+
+       case TEVENT_TRACE_AFTER_WAIT:
+               diff = timeval_until(&tevent_before_wait_ts, &now);
+               if (diff.tv_sec > 3) {
+                       DEBUG(DEBUG_ERR,
+                             ("No event for %ld seconds!\n",
+                              diff.tv_sec));
+               }
+               tevent_after_wait_ts = now;
+               break;
+
+       default:
+               /* Do nothing for future tevent trace points */ ;
+       }
+}
+
+static void ctdb_remove_pidfile(void)
+{
+       TALLOC_FREE(ctdbd_pidfile_ctx);
+}
+
+static void ctdb_create_pidfile(TALLOC_CTX *mem_ctx)
+{
+       if (ctdbd_pidfile != NULL) {
+               int ret = pidfile_create(mem_ctx, ctdbd_pidfile,
+                                        &ctdbd_pidfile_ctx);
+               if (ret != 0) {
+                       DEBUG(DEBUG_ERR,
+                             ("Failed to create PID file %s\n",
+                              ctdbd_pidfile));
+                       exit(11);
+               }
+
+               DEBUG(DEBUG_NOTICE, ("Created PID file %s\n", ctdbd_pidfile));
+               atexit(ctdb_remove_pidfile);
+       }
+}
+
+static void ctdb_initialise_vnn_map(struct ctdb_context *ctdb)
+{
+       int i, j, count;
+
+       /* initialize the vnn mapping table, skipping any deleted nodes */
+       ctdb->vnn_map = talloc(ctdb, struct ctdb_vnn_map);
+       CTDB_NO_MEMORY_FATAL(ctdb, ctdb->vnn_map);
+
+       count = 0;
+       for (i = 0; i < ctdb->num_nodes; i++) {
+               if ((ctdb->nodes[i]->flags & NODE_FLAGS_DELETED) == 0) {
+                       count++;
+               }
+       }
+
+       ctdb->vnn_map->generation = INVALID_GENERATION;
+       ctdb->vnn_map->size = count;
+       ctdb->vnn_map->map = talloc_array(ctdb->vnn_map, uint32_t, ctdb->vnn_map->size);
+       CTDB_NO_MEMORY_FATAL(ctdb, ctdb->vnn_map->map);
+
+       for(i=0, j=0; i < ctdb->vnn_map->size; i++) {
+               if (ctdb->nodes[i]->flags & NODE_FLAGS_DELETED) {
+                       continue;
+               }
+               ctdb->vnn_map->map[j] = i;
+               j++;
+       }
+}
+
+static void ctdb_set_my_pnn(struct ctdb_context *ctdb)
+{
+       int nodeid;
+
+       if (ctdb->address == NULL) {
+               ctdb_fatal(ctdb,
+                          "Can not determine PNN - node address is not set\n");
+       }
+
+       nodeid = ctdb_ip_to_nodeid(ctdb, ctdb->address);
+       if (nodeid == -1) {
+               ctdb_fatal(ctdb,
+                          "Can not determine PNN - node address not found in node list\n");
+       }
+
+       ctdb->pnn = ctdb->nodes[nodeid]->pnn;
+       DEBUG(DEBUG_NOTICE, ("PNN is %u\n", ctdb->pnn));
 }
 
 /*
   start the protocol going as a daemon
 */
-int ctdb_start_daemon(struct ctdb_context *ctdb, bool do_fork, bool use_syslog, const char *public_address_list)
+int ctdb_start_daemon(struct ctdb_context *ctdb, bool do_fork)
 {
        int res, ret = -1;
-       struct fd_event *fde;
-       const char *domain_socket_name;
-       struct signal_event *se;
-
-       /* get rid of any old sockets */
-       unlink(ctdb->daemon.name);
-
-       /* create a unix domain stream socket to listen to */
-       res = ux_socket_bind(ctdb);
-       if (res!=0) {
-               DEBUG(DEBUG_ALERT,(__location__ " Failed to open CTDB unix domain socket\n"));
-               exit(10);
-       }
+       struct tevent_fd *fde;
 
        if (do_fork && fork()) {
                return 0;
        }
 
-       tdb_reopen_all(False);
+       tdb_reopen_all(false);
 
        if (do_fork) {
-               setsid();
+               if (setsid() == -1) {
+                       ctdb_die(ctdb, "Failed to setsid()\n");
+               }
                close(0);
                if (open("/dev/null", O_RDONLY) != 0) {
                        DEBUG(DEBUG_ALERT,(__location__ " Failed to setup stdin on /dev/null\n"));
                        exit(11);
                }
        }
-       block_signal(SIGPIPE);
+       ignore_signal(SIGPIPE);
+       ignore_signal(SIGUSR1);
 
-       ctdbd_pid = getpid();
-       ctdb->ctdbd_pid = ctdbd_pid;
+       ctdb->ctdbd_pid = getpid();
+       DEBUG(DEBUG_ERR, ("Starting CTDBD (Version %s) as PID: %u\n",
+                         CTDB_VERSION_STRING, ctdb->ctdbd_pid));
+       ctdb_create_pidfile(ctdb);
 
+       /* create a unix domain stream socket to listen to */
+       res = ux_socket_bind(ctdb);
+       if (res!=0) {
+               DEBUG(DEBUG_ALERT,("Cannot continue.  Exiting!\n"));
+               exit(10);
+       }
 
-       DEBUG(DEBUG_ERR, ("Starting CTDBD as pid : %u\n", ctdbd_pid));
+       /* Make sure we log something when the daemon terminates.
+        * This must be the first exit handler to run (so the last to
+        * be registered.
+        */
+       atexit(print_exit_message);
 
        if (ctdb->do_setsched) {
                /* try to set us up as realtime */
-               ctdb_set_scheduler(ctdb);
+               if (!set_scheduler()) {
+                       exit(1);
+               }
+               DEBUG(DEBUG_NOTICE, ("Set real-time scheduler priority\n"));
        }
 
-       /* ensure the socket is deleted on exit of the daemon */
-       domain_socket_name = talloc_strdup(talloc_autofree_context(), ctdb->daemon.name);
-       if (domain_socket_name == NULL) {
-               DEBUG(DEBUG_ALERT,(__location__ " talloc_strdup failed.\n"));
-               exit(12);
+       ctdb->ev = tevent_context_init(NULL);
+       if (ctdb->ev == NULL) {
+               DEBUG(DEBUG_ALERT,("tevent_context_init() failed\n"));
+               exit(1);
        }
-
-       ctdb->ev = event_context_init(NULL);
        tevent_loop_allow_nesting(ctdb->ev);
+       ctdb_tevent_trace_init();
+       tevent_set_trace_callback(ctdb->ev, ctdb_tevent_trace, ctdb);
        ret = ctdb_init_tevent_logging(ctdb);
        if (ret != 0) {
                DEBUG(DEBUG_ALERT,("Failed to initialize TEVENT logging\n"));
                exit(1);
        }
 
+       /* set up a handler to pick up sigchld */
+       if (ctdb_init_sigchld(ctdb) == NULL) {
+               DEBUG(DEBUG_CRIT,("Failed to set up signal handler for SIGCHLD\n"));
+               exit(1);
+       }
+
        ctdb_set_child_logging(ctdb);
 
+       TALLOC_FREE(ctdb->srv);
+       if (srvid_init(ctdb, &ctdb->srv) != 0) {
+               DEBUG(DEBUG_CRIT,("Failed to setup message srvid context\n"));
+               exit(1);
+       }
+
        /* initialize statistics collection */
        ctdb_statistics_init(ctdb);
 
        /* force initial recovery for election */
        ctdb->recovery_mode = CTDB_RECOVERY_ACTIVE;
 
+       ctdb_set_runstate(ctdb, CTDB_RUNSTATE_INIT);
+       ret = ctdb_event_script(ctdb, CTDB_EVENT_INIT);
+       if (ret != 0) {
+               ctdb_die(ctdb, "Failed to run init event\n");
+       }
+       ctdb_run_notification_script(ctdb, "init");
+
        if (strcmp(ctdb->transport, "tcp") == 0) {
-               int ctdb_tcp_init(struct ctdb_context *);
                ret = ctdb_tcp_init(ctdb);
        }
 #ifdef USE_INFINIBAND
        if (strcmp(ctdb->transport, "ib") == 0) {
-               int ctdb_ibw_init(struct ctdb_context *);
                ret = ctdb_ibw_init(ctdb);
        }
 #endif
@@ -1156,68 +1337,59 @@ int ctdb_start_daemon(struct ctdb_context *ctdb, bool do_fork, bool use_syslog,
                ctdb_fatal(ctdb, "transport is unavailable. can not initialize.");
        }
 
-       /* initialise the transport  */
+       /* Initialise the transport.  This sets the node address if it
+        * was not set via the command-line. */
        if (ctdb->methods->initialise(ctdb) != 0) {
                ctdb_fatal(ctdb, "transport failed to initialise");
        }
-       if (public_address_list) {
-               ctdb->public_addresses_file = public_address_list;
+
+       ctdb_set_my_pnn(ctdb);
+
+       initialise_node_flags(ctdb);
+
+       if (ctdb->public_addresses_file) {
                ret = ctdb_set_public_addresses(ctdb, true);
                if (ret == -1) {
                        DEBUG(DEBUG_ALERT,("Unable to setup public address list\n"));
                        exit(1);
                }
-               if (ctdb->do_checkpublicip) {
-                       ctdb_start_monitoring_interfaces(ctdb);
-               }
        }
 
+       ctdb_initialise_vnn_map(ctdb);
 
        /* attach to existing databases */
        if (ctdb_attach_databases(ctdb) != 0) {
                ctdb_fatal(ctdb, "Failed to attach to databases\n");
        }
 
-       ret = ctdb_event_script(ctdb, CTDB_EVENT_INIT);
-       if (ret != 0) {
-               ctdb_fatal(ctdb, "Failed to run init event\n");
-       }
-       ctdb_run_notification_script(ctdb, "init");
-
        /* start frozen, then let the first election sort things out */
-       if (ctdb_blocking_freeze(ctdb)) {
+       if (!ctdb_blocking_freeze(ctdb)) {
                ctdb_fatal(ctdb, "Failed to get initial freeze\n");
        }
 
        /* now start accepting clients, only can do this once frozen */
-       fde = event_add_fd(ctdb->ev, ctdb, ctdb->daemon.sd, 
-                          EVENT_FD_READ,
-                          ctdb_accept_client, ctdb);
-       tevent_fd_set_auto_close(fde);
-
-       /* release any IPs we hold from previous runs of the daemon */
-       if (ctdb->tunable.disable_ip_failover == 0) {
-               ctdb_release_all_ips(ctdb);
+       fde = tevent_add_fd(ctdb->ev, ctdb, ctdb->daemon.sd, TEVENT_FD_READ,
+                           ctdb_accept_client, ctdb);
+       if (fde == NULL) {
+               ctdb_fatal(ctdb, "Failed to add daemon socket to event loop");
        }
+       tevent_fd_set_auto_close(fde);
 
-       /* start the transport going */
-       ctdb_start_transport(ctdb);
-
-       /* set up a handler to pick up sigchld */
-       se = event_add_signal(ctdb->ev, ctdb,
-                                    SIGCHLD, 0,
-                                    sig_child_handler,
-                                    ctdb);
-       if (se == NULL) {
-               DEBUG(DEBUG_CRIT,("Failed to set up signal handler for SIGCHLD\n"));
-               exit(1);
+       /* Start the transport */
+       if (ctdb->methods->start(ctdb) != 0) {
+               DEBUG(DEBUG_ALERT,("transport failed to start!\n"));
+               ctdb_fatal(ctdb, "transport failed to start");
        }
 
+       /* Recovery daemon and timed events are started from the
+        * callback, only after the setup event completes
+        * successfully.
+        */
+       ctdb_set_runstate(ctdb, CTDB_RUNSTATE_SETUP);
        ret = ctdb_event_script_callback(ctdb,
                                         ctdb,
                                         ctdb_setup_event_callback,
                                         ctdb,
-                                        false,
                                         CTDB_EVENT_SETUP,
                                         "%s",
                                         "");
@@ -1226,17 +1398,10 @@ int ctdb_start_daemon(struct ctdb_context *ctdb, bool do_fork, bool use_syslog,
                exit(1);
        }
 
-       if (use_syslog) {
-               if (start_syslog_daemon(ctdb)) {
-                       DEBUG(DEBUG_CRIT, ("Failed to start syslog daemon\n"));
-                       exit(10);
-               }
-       }
+       lockdown_memory(ctdb->valgrinding);
 
-       ctdb_lockdown_memory(ctdb);
-         
        /* go into a wait loop to allow other nodes to complete */
-       event_loop_wait(ctdb->ev);
+       tevent_loop_wait(ctdb->ev);
 
        DEBUG(DEBUG_CRIT,("event_loop_wait() returned. this should not happen\n"));
        exit(1);
@@ -1274,7 +1439,7 @@ struct ctdb_req_header *_ctdb_transport_allocate(struct ctdb_context *ctdb,
        hdr->length       = length;
        hdr->operation    = operation;
        hdr->ctdb_magic   = CTDB_MAGIC;
-       hdr->ctdb_version = CTDB_VERSION;
+       hdr->ctdb_version = CTDB_PROTOCOL;
        hdr->generation   = ctdb->vnn_map->generation;
        hdr->srcnode      = ctdb->pnn;
 
@@ -1284,7 +1449,7 @@ struct ctdb_req_header *_ctdb_transport_allocate(struct ctdb_context *ctdb,
 struct daemon_control_state {
        struct daemon_control_state *next, *prev;
        struct ctdb_client *client;
-       struct ctdb_req_control *c;
+       struct ctdb_req_control_old *c;
        uint32_t reqid;
        struct ctdb_node *node;
 };
@@ -1300,17 +1465,17 @@ static void daemon_control_callback(struct ctdb_context *ctdb,
        struct daemon_control_state *state = talloc_get_type(private_data, 
                                                             struct daemon_control_state);
        struct ctdb_client *client = state->client;
-       struct ctdb_reply_control *r;
+       struct ctdb_reply_control_old *r;
        size_t len;
        int ret;
 
        /* construct a message to send to the client containing the data */
-       len = offsetof(struct ctdb_reply_control, data) + data.dsize;
+       len = offsetof(struct ctdb_reply_control_old, data) + data.dsize;
        if (errormsg) {
                len += strlen(errormsg);
        }
        r = ctdbd_allocate_pkt(ctdb, state, CTDB_REPLY_CONTROL, len, 
-                              struct ctdb_reply_control);
+                              struct ctdb_reply_control_old);
        CTDB_NO_MEMORY_VOID(ctdb, r);
 
        r->hdr.reqid     = state->reqid;
@@ -1358,7 +1523,7 @@ static int daemon_control_destructor(struct daemon_control_state *state)
   from a local client over the unix domain socket
  */
 static void daemon_request_control_from_client(struct ctdb_client *client, 
-                                              struct ctdb_req_control *c)
+                                              struct ctdb_req_control_old *c)
 {
        TDB_DATA data;
        int res;
@@ -1438,18 +1603,14 @@ struct ctdb_local_message {
        TDB_DATA data;
 };
 
-static void ctdb_local_message_trigger(struct event_context *ev, struct timed_event *te, 
+static void ctdb_local_message_trigger(struct tevent_context *ev,
+                                      struct tevent_timer *te,
                                       struct timeval t, void *private_data)
 {
-       struct ctdb_local_message *m = talloc_get_type(private_data, 
-                                                      struct ctdb_local_message);
-       int res;
+       struct ctdb_local_message *m = talloc_get_type(
+               private_data, struct ctdb_local_message);
 
-       res = ctdb_dispatch_message(m->ctdb, m->srvid, m->data);
-       if (res != 0) {
-               DEBUG(DEBUG_ERR, (__location__ " Failed to dispatch message for srvid=%llu\n", 
-                         (unsigned long long)m->srvid));
-       }
+       srvid_dispatch(m->ctdb->srv, m->srvid, CTDB_SRVID_ALL, m->data);
        talloc_free(m);
 }
 
@@ -1469,7 +1630,8 @@ static int ctdb_local_message(struct ctdb_context *ctdb, uint64_t srvid, TDB_DAT
        }
 
        /* this needs to be done as an event to prevent recursion */
-       event_add_timed(ctdb->ev, m, timeval_zero(), ctdb_local_message_trigger, m);
+       tevent_add_timer(ctdb->ev, m, timeval_zero(),
+                        ctdb_local_message_trigger, m);
        return 0;
 }
 
@@ -1479,7 +1641,7 @@ static int ctdb_local_message(struct ctdb_context *ctdb, uint64_t srvid, TDB_DAT
 int ctdb_daemon_send_message(struct ctdb_context *ctdb, uint32_t pnn,
                             uint64_t srvid, TDB_DATA data)
 {
-       struct ctdb_req_message *r;
+       struct ctdb_req_message_old *r;
        int len;
 
        if (ctdb->methods == NULL) {
@@ -1492,9 +1654,9 @@ int ctdb_daemon_send_message(struct ctdb_context *ctdb, uint32_t pnn,
                return ctdb_local_message(ctdb, srvid, data);
        }
 
-       len = offsetof(struct ctdb_req_message, data) + data.dsize;
+       len = offsetof(struct ctdb_req_message_old, data) + data.dsize;
        r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REQ_MESSAGE, len,
-                                   struct ctdb_req_message);
+                                   struct ctdb_req_message_old);
        CTDB_NO_MEMORY(ctdb, r);
 
        r->hdr.destnode  = pnn;
@@ -1534,19 +1696,19 @@ static int ctdb_client_notify_destructor(struct ctdb_client_notify_list *nl)
 
 int32_t ctdb_control_register_notify(struct ctdb_context *ctdb, uint32_t client_id, TDB_DATA indata)
 {
-       struct ctdb_client_notify_register *notify = (struct ctdb_client_notify_register *)indata.dptr;
-        struct ctdb_client *client = ctdb_reqid_find(ctdb, client_id, struct ctdb_client); 
+       struct ctdb_notify_data_old *notify = (struct ctdb_notify_data_old *)indata.dptr;
+        struct ctdb_client *client = reqid_find(ctdb->idr, client_id, struct ctdb_client);
        struct ctdb_client_notify_list *nl;
 
        DEBUG(DEBUG_INFO,("Register srvid %llu for client %d\n", (unsigned long long)notify->srvid, client_id));
 
-       if (indata.dsize < offsetof(struct ctdb_client_notify_register, notify_data)) {
+       if (indata.dsize < offsetof(struct ctdb_notify_data_old, notify_data)) {
                DEBUG(DEBUG_ERR,(__location__ " Too little data in control : %d\n", (int)indata.dsize));
                return -1;
        }
 
-       if (indata.dsize != (notify->len + offsetof(struct ctdb_client_notify_register, notify_data))) {
-               DEBUG(DEBUG_ERR,(__location__ " Wrong amount of data in control. Got %d, expected %d\n", (int)indata.dsize, (int)(notify->len + offsetof(struct ctdb_client_notify_register, notify_data))));
+       if (indata.dsize != (notify->len + offsetof(struct ctdb_notify_data_old, notify_data))) {
+               DEBUG(DEBUG_ERR,(__location__ " Wrong amount of data in control. Got %d, expected %d\n", (int)indata.dsize, (int)(notify->len + offsetof(struct ctdb_notify_data_old, notify_data))));
                return -1;
        }
 
@@ -1571,9 +1733,9 @@ int32_t ctdb_control_register_notify(struct ctdb_context *ctdb, uint32_t client_
        nl->ctdb       = ctdb;
        nl->srvid      = notify->srvid;
        nl->data.dsize = notify->len;
-       nl->data.dptr  = talloc_size(nl, nl->data.dsize);
+       nl->data.dptr  = talloc_memdup(nl, notify->notify_data,
+                                      nl->data.dsize);
        CTDB_NO_MEMORY(ctdb, nl->data.dptr);
-       memcpy(nl->data.dptr, notify->notify_data, nl->data.dsize);
        
        DLIST_ADD(client->notify, nl);
        talloc_set_destructor(nl, ctdb_client_notify_destructor);
@@ -1583,11 +1745,11 @@ int32_t ctdb_control_register_notify(struct ctdb_context *ctdb, uint32_t client_
 
 int32_t ctdb_control_deregister_notify(struct ctdb_context *ctdb, uint32_t client_id, TDB_DATA indata)
 {
-       struct ctdb_client_notify_deregister *notify = (struct ctdb_client_notify_deregister *)indata.dptr;
-        struct ctdb_client *client = ctdb_reqid_find(ctdb, client_id, struct ctdb_client); 
+       uint64_t srvid = *(uint64_t *)indata.dptr;
+        struct ctdb_client *client = reqid_find(ctdb->idr, client_id, struct ctdb_client);
        struct ctdb_client_notify_list *nl;
 
-       DEBUG(DEBUG_INFO,("Deregister srvid %llu for client %d\n", (unsigned long long)notify->srvid, client_id));
+       DEBUG(DEBUG_INFO,("Deregister srvid %llu for client %d\n", (unsigned long long)srvid, client_id));
 
         if (client == NULL) {
                 DEBUG(DEBUG_ERR,(__location__ " Could not find client parent structure. You can not send this control to a remote node\n"));
@@ -1595,12 +1757,12 @@ int32_t ctdb_control_deregister_notify(struct ctdb_context *ctdb, uint32_t clien
         }
 
        for(nl=client->notify; nl; nl=nl->next) {
-               if (nl->srvid == notify->srvid) {
+               if (nl->srvid == srvid) {
                        break;
                }
        }
        if (nl == NULL) {
-                DEBUG(DEBUG_ERR,(__location__ " No notification for srvid:%llu found for this client\n", (unsigned long long)notify->srvid));
+                DEBUG(DEBUG_ERR,(__location__ " No notification for srvid:%llu found for this client\n", (unsigned long long)srvid));
                 return -1;
         }
 
@@ -1649,3 +1811,79 @@ int32_t ctdb_control_process_exists(struct ctdb_context *ctdb, pid_t pid)
 
        return kill(pid, 0);
 }
+
+int ctdb_control_getnodesfile(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata)
+{
+       struct ctdb_node_map_old *node_map = NULL;
+
+       CHECK_CONTROL_DATA_SIZE(0);
+
+       node_map = ctdb_read_nodes_file(ctdb, ctdb->nodes_file);
+       if (node_map == NULL) {
+               DEBUG(DEBUG_ERR, ("Failed to read nodes file\n"));
+               return -1;
+       }
+
+       outdata->dptr  = (unsigned char *)node_map;
+       outdata->dsize = talloc_get_size(outdata->dptr);
+
+       return 0;
+}
+
+void ctdb_shutdown_sequence(struct ctdb_context *ctdb, int exit_code)
+{
+       if (ctdb->runstate == CTDB_RUNSTATE_SHUTDOWN) {
+               DEBUG(DEBUG_NOTICE,("Already shutting down so will not proceed.\n"));
+               return;
+       }
+
+       DEBUG(DEBUG_NOTICE,("Shutdown sequence commencing.\n"));
+       ctdb_set_runstate(ctdb, CTDB_RUNSTATE_SHUTDOWN);
+       ctdb_stop_recoverd(ctdb);
+       ctdb_stop_keepalive(ctdb);
+       ctdb_stop_monitoring(ctdb);
+       ctdb_release_all_ips(ctdb);
+       ctdb_event_script(ctdb, CTDB_EVENT_SHUTDOWN);
+       if (ctdb->methods != NULL && ctdb->methods->shutdown != NULL) {
+               ctdb->methods->shutdown(ctdb);
+       }
+
+       DEBUG(DEBUG_NOTICE,("Shutdown sequence complete, exiting.\n"));
+       exit(exit_code);
+}
+
+/* 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, ...)
+{
+       int ret;
+       va_list ap;
+
+       /* Add extra information so we can identify this in the logs */
+       va_start(ap, fmt);
+       debug_extra = talloc_strdup_append(talloc_vasprintf(NULL, fmt, ap), ":");
+       va_end(ap);
+
+       /* get a new event context */
+       ctdb->ev = tevent_context_init(ctdb);
+       if (ctdb->ev == NULL) {
+               DEBUG(DEBUG_ALERT,("tevent_context_init() failed\n"));
+               exit(1);
+       }
+       tevent_loop_allow_nesting(ctdb->ev);
+
+       /* Connect to main CTDB daemon */
+       ret = ctdb_socket_connect(ctdb);
+       if (ret != 0) {
+               DEBUG(DEBUG_ALERT, (__location__ " Failed to init ctdb client\n"));
+               return -1;
+       }
+
+       ctdb->can_send_controls = true;
+
+       return 0;
+}