ctdb-daemon: Rename struct ctdb_req_control to ctdb_req_control_old
[obnox/samba/samba-obnox.git] / ctdb / client / ctdb_client.c
index 1d4374d3b1b843e8ce65e2bd425569e374b646fb..684bb17788051694eed8c0f93c84e8447f7ff62e 100644 (file)
    along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
 
-#include "includes.h"
-#include "lib/tdb_wrap/tdb_wrap.h"
-#include "tdb.h"
-#include "lib/util/dlinklist.h"
+#include "replace.h"
 #include "system/network.h"
 #include "system/filesys.h"
 #include "system/locale.h"
-#include <stdlib.h>
-#include "../include/ctdb_private.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/time.h"
+#include "lib/util/debug.h"
+#include "lib/util/samba_util.h"
+
+#include "ctdb_logging.h"
+#include "ctdb_private.h"
+#include "ctdb_client.h"
+
+#include "common/reqid.h"
+#include "common/system.h"
+#include "common/common.h"
 
 /*
   allocate a packet for use in client<->daemon communication
@@ -150,10 +164,10 @@ static int ctdb_client_queue_pkt(struct ctdb_context *ctdb, struct ctdb_req_head
 */
 static void ctdb_client_reply_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
 {
-       struct ctdb_reply_call *c = (struct ctdb_reply_call *)hdr;
+       struct ctdb_reply_call_old *c = (struct ctdb_reply_call_old *)hdr;
        struct ctdb_client_call_state *state;
 
-       state = ctdb_reqid_find(ctdb, hdr->reqid, struct ctdb_client_call_state);
+       state = reqid_find(ctdb->idr, hdr->reqid, struct ctdb_client_call_state);
        if (state == NULL) {
                DEBUG(DEBUG_ERR,(__location__ " reqid %u not found\n", hdr->reqid));
                return;
@@ -181,7 +195,7 @@ static void ctdb_client_reply_call(struct ctdb_context *ctdb, struct ctdb_req_he
 void ctdb_request_message(struct ctdb_context *ctdb,
                          struct ctdb_req_header *hdr)
 {
-       struct ctdb_req_message *c = (struct ctdb_req_message *)hdr;
+       struct ctdb_req_message_old *c = (struct ctdb_req_message_old *)hdr;
        TDB_DATA data;
 
        data.dsize = c->datalen;
@@ -313,7 +327,7 @@ int ctdb_call_recv(struct ctdb_client_call_state *state, struct ctdb_call *call)
        }
 
        while (state->state < CTDB_CALL_DONE) {
-               event_loop_once(state->ctdb_db->ctdb->ev);
+               tevent_loop_once(state->ctdb_db->ctdb->ev);
        }
        if (state->state != CTDB_CALL_DONE) {
                DEBUG(DEBUG_ERR,(__location__ " ctdb_call_recv failed\n"));
@@ -344,7 +358,7 @@ int ctdb_call_recv(struct ctdb_client_call_state *state, struct ctdb_call *call)
 */
 static int ctdb_client_call_destructor(struct ctdb_client_call_state *state)   
 {
-       ctdb_reqid_remove(state->ctdb_db->ctdb, state->reqid);
+       reqid_remove(state->ctdb_db->ctdb->idr, state->reqid);
        return 0;
 }
 
@@ -397,7 +411,7 @@ struct ctdb_client_call_state *ctdb_call_send(struct ctdb_db_context *ctdb_db,
        TDB_DATA data;
        int ret;
        size_t len;
-       struct ctdb_req_call *c;
+       struct ctdb_req_call_old *c;
 
        /* if the domain socket is not yet open, open it */
        if (ctdb->daemon.sd==-1) {
@@ -437,14 +451,14 @@ struct ctdb_client_call_state *ctdb_call_send(struct ctdb_db_context *ctdb_db,
                return NULL;
        }
 
-       len = offsetof(struct ctdb_req_call, data) + call->key.dsize + call->call_data.dsize;
-       c = ctdbd_allocate_pkt(ctdb, state, CTDB_REQ_CALL, len, struct ctdb_req_call);
+       len = offsetof(struct ctdb_req_call_old, data) + call->key.dsize + call->call_data.dsize;
+       c = ctdbd_allocate_pkt(ctdb, state, CTDB_REQ_CALL, len, struct ctdb_req_call_old);
        if (c == NULL) {
                DEBUG(DEBUG_ERR, (__location__ " failed to allocate packet\n"));
                return NULL;
        }
 
-       state->reqid     = ctdb_reqid_new(ctdb, state);
+       state->reqid     = reqid_new(ctdb->idr, state);
        state->ctdb_db = ctdb_db;
        talloc_set_destructor(state, ctdb_client_call_destructor);
 
@@ -575,12 +589,12 @@ int ctdb_client_check_message_handlers(struct ctdb_context *ctdb, uint64_t *ids,
 int ctdb_client_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, res;
 
-       len = offsetof(struct ctdb_req_message, data) + data.dsize;
+       len = offsetof(struct ctdb_req_message_old, data) + data.dsize;
        r = ctdbd_allocate_pkt(ctdb, ctdb, CTDB_REQ_MESSAGE, 
-                              len, struct ctdb_req_message);
+                              len, struct ctdb_req_message_old);
        CTDB_NO_MEMORY(ctdb, r);
 
        r->hdr.destnode  = pnn;
@@ -945,8 +959,9 @@ int ctdb_fetch(struct ctdb_db_context *ctdb_db, TALLOC_CTX *mem_ctx,
    called when a control completes or timesout to invoke the callback
    function the user provided
 */
-static void invoke_control_callback(struct event_context *ev, struct timed_event *te, 
-       struct timeval t, void *private_data)
+static void invoke_control_callback(struct tevent_context *ev,
+                                   struct tevent_timer *te,
+                                   struct timeval t, void *private_data)
 {
        struct ctdb_client_control_state *state;
        TALLOC_CTX *tmp_ctx = talloc_new(NULL);
@@ -978,7 +993,7 @@ static void ctdb_client_reply_control(struct ctdb_context *ctdb,
        struct ctdb_reply_control *c = (struct ctdb_reply_control *)hdr;
        struct ctdb_client_control_state *state;
 
-       state = ctdb_reqid_find(ctdb, hdr->reqid, struct ctdb_client_control_state);
+       state = reqid_find(ctdb->idr, hdr->reqid, struct ctdb_client_control_state);
        if (state == NULL) {
                DEBUG(DEBUG_ERR,(__location__ " reqid %u not found\n", hdr->reqid));
                return;
@@ -1010,7 +1025,8 @@ static void ctdb_client_reply_control(struct ctdb_context *ctdb,
           and call the callback.
        */
        if (state->async.fn) {
-               event_add_timed(ctdb->ev, state, timeval_zero(), invoke_control_callback, state);
+               tevent_add_timer(ctdb->ev, state, timeval_zero(),
+                                invoke_control_callback, state);
        }
 }
 
@@ -1020,14 +1036,15 @@ static void ctdb_client_reply_control(struct ctdb_context *ctdb,
 */
 static int ctdb_client_control_destructor(struct ctdb_client_control_state *state)
 {
-       ctdb_reqid_remove(state->ctdb, state->reqid);
+       reqid_remove(state->ctdb->idr, state->reqid);
        return 0;
 }
 
 
 /* time out handler for ctdb_control */
-static void control_timeout_func(struct event_context *ev, struct timed_event *te, 
-       struct timeval t, void *private_data)
+static void control_timeout_func(struct tevent_context *ev,
+                                struct tevent_timer *te,
+                                struct timeval t, void *private_data)
 {
        struct ctdb_client_control_state *state = talloc_get_type(private_data, struct ctdb_client_control_state);
 
@@ -1041,7 +1058,8 @@ static void control_timeout_func(struct event_context *ev, struct timed_event *t
           and call the callback.
        */
        if (state->async.fn) {
-               event_add_timed(state->ctdb->ev, state, timeval_zero(), invoke_control_callback, state);
+               tevent_add_timer(state->ctdb->ev, state, timeval_zero(),
+                                invoke_control_callback, state);
        }
 }
 
@@ -1055,7 +1073,7 @@ struct ctdb_client_control_state *ctdb_control_send(struct ctdb_context *ctdb,
 {
        struct ctdb_client_control_state *state;
        size_t len;
-       struct ctdb_req_control *c;
+       struct ctdb_req_control_old *c;
        int ret;
 
        if (errormsg) {
@@ -1071,15 +1089,15 @@ struct ctdb_client_control_state *ctdb_control_send(struct ctdb_context *ctdb,
        CTDB_NO_MEMORY_NULL(ctdb, state);
 
        state->ctdb       = ctdb;
-       state->reqid      = ctdb_reqid_new(ctdb, state);
+       state->reqid      = reqid_new(ctdb->idr, state);
        state->state      = CTDB_CONTROL_WAIT;
        state->errormsg   = NULL;
 
        talloc_set_destructor(state, ctdb_client_control_destructor);
 
-       len = offsetof(struct ctdb_req_control, data) + data.dsize;
+       len = offsetof(struct ctdb_req_control_old, data) + data.dsize;
        c = ctdbd_allocate_pkt(ctdb, state, CTDB_REQ_CONTROL, 
-                              len, struct ctdb_req_control);
+                              len, struct ctdb_req_control_old);
        state->c            = c;        
        CTDB_NO_MEMORY_NULL(ctdb, c);
        c->hdr.reqid        = state->reqid;
@@ -1095,7 +1113,8 @@ struct ctdb_client_control_state *ctdb_control_send(struct ctdb_context *ctdb,
 
        /* timeout */
        if (timeout && !timeval_is_zero(timeout)) {
-               event_add_timed(ctdb->ev, state, *timeout, control_timeout_func, state);
+               tevent_add_timer(ctdb->ev, state, *timeout,
+                                control_timeout_func, state);
        }
 
        ret = ctdb_client_queue_pkt(ctdb, &(c->hdr));
@@ -1140,7 +1159,7 @@ int ctdb_control_recv(struct ctdb_context *ctdb,
           completes.
        */
        while (state->state == CTDB_CONTROL_WAIT) {
-               event_loop_once(ctdb->ev);
+               tevent_loop_once(ctdb->ev);
        }
 
        if (state->state != CTDB_CONTROL_DONE) {
@@ -2291,7 +2310,7 @@ static int ctdb_traverse_ext(struct ctdb_db_context *ctdb_db,
        }
 
        while (!state.done) {
-               event_loop_once(ctdb_db->ctdb->ev);
+               tevent_loop_once(ctdb_db->ctdb->ev);
        }
 
        ret = ctdb_client_remove_message_handler(ctdb_db->ctdb, srvid, &state);
@@ -3302,7 +3321,7 @@ int ctdb_ctrl_get_server_id_list(struct ctdb_context *ctdb,
   NOTE: In current code the daemon does not fork. This is for testing purposes only
   and to simplify the code.
 */
-struct ctdb_context *ctdb_init(struct event_context *ev)
+struct ctdb_context *ctdb_init(struct tevent_context *ev)
 {
        int ret;
        struct ctdb_context *ctdb;
@@ -3313,10 +3332,13 @@ struct ctdb_context *ctdb_init(struct event_context *ev)
                return NULL;
        }
        ctdb->ev  = ev;
-       ctdb->idr = idr_init(ctdb);
        /* Wrap early to exercise code. */
-       ctdb->lastid = INT_MAX-200;
-       CTDB_NO_MEMORY_NULL(ctdb, ctdb->idr);
+       ret = reqid_init(ctdb, INT_MAX-200, &ctdb->idr);
+       if (ret != 0) {
+               DEBUG(DEBUG_ERR, ("reqid_init failed (%s)\n", strerror(ret)));
+               talloc_free(ctdb);
+               return NULL;
+       }
 
        ret = srvid_init(ctdb, &ctdb->srv);
        if (ret != 0) {
@@ -3502,7 +3524,7 @@ void ctdb_client_async_add(struct client_async_data *data, struct ctdb_client_co
 int ctdb_client_async_wait(struct ctdb_context *ctdb, struct client_async_data *data)
 {
        while (data->count > 0) {
-               event_loop_once(ctdb->ev);
+               tevent_loop_once(ctdb->ev);
        }
        if (data->fail_count != 0) {
                if (!data->dont_log_errors) {
@@ -4101,7 +4123,7 @@ struct ctdb_transaction_handle {
 static int ctdb_transaction_destructor(struct ctdb_transaction_handle *h)
 {
        g_lock_unlock(h, h->g_lock_db, h->lock_name, h->reqid);
-       ctdb_reqid_remove(h->ctdb_db->ctdb, h->reqid);
+       reqid_remove(h->ctdb_db->ctdb->idr, h->reqid);
        return 0;
 }
 
@@ -4149,7 +4171,7 @@ struct ctdb_transaction_handle *ctdb_transaction_start(struct ctdb_db_context *c
                return NULL;
        }
 
-       h->reqid = ctdb_reqid_new(h->ctdb_db->ctdb, h);
+       h->reqid = reqid_new(h->ctdb_db->ctdb->idr, h);
 
        if (!g_lock_lock(h, h->g_lock_db, h->lock_name, h->reqid)) {
                DEBUG(DEBUG_ERR, (__location__ " Error locking g_lock.tdb\n"));
@@ -4412,7 +4434,7 @@ int switch_from_server_to_client(struct ctdb_context *ctdb, const char *fmt, ...
        va_end(ap);
 
        /* get a new event context */
-       ctdb->ev = event_context_init(ctdb);
+       ctdb->ev = tevent_context_init(ctdb);
        tevent_loop_allow_nesting(ctdb->ev);
 
        /* Connect to main CTDB daemon */
@@ -4430,10 +4452,11 @@ int switch_from_server_to_client(struct ctdb_context *ctdb, const char *fmt, ...
 /*
   get the status of running the monitor eventscripts: NULL means never run.
  */
-int ctdb_ctrl_getscriptstatus(struct ctdb_context *ctdb, 
-               struct timeval timeout, uint32_t destnode, 
-               TALLOC_CTX *mem_ctx, enum ctdb_eventscript_call type,
-               struct ctdb_scripts_wire **scripts)
+int ctdb_ctrl_getscriptstatus(struct ctdb_context *ctdb,
+                             struct timeval timeout, uint32_t destnode,
+                             TALLOC_CTX *mem_ctx,
+                             enum ctdb_event type,
+                             struct ctdb_script_list_old **scripts)
 {
        int ret;
        TDB_DATA outdata, indata;
@@ -4454,10 +4477,10 @@ int ctdb_ctrl_getscriptstatus(struct ctdb_context *ctdb,
        if (outdata.dsize == 0) {
                *scripts = NULL;
        } else {
-               *scripts = (struct ctdb_scripts_wire *)talloc_memdup(mem_ctx, outdata.dptr, outdata.dsize);
+               *scripts = (struct ctdb_script_list_old *)talloc_memdup(mem_ctx, outdata.dptr, outdata.dsize);
                talloc_free(outdata.dptr);
        }
-                   
+
        return 0;
 }