33e0f7e1bc2d8065d57daff7ea4bbddebcb28578
[obnox/samba-ctdb.git] / source3 / lib / ctdbd_conn.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba internal messaging functions
4    Copyright (C) 2007 by Volker Lendecke
5    Copyright (C) 2007 by Andrew Tridgell
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22
23 #ifdef CLUSTER_SUPPORT
24
25 #include "librpc/gen_ndr/messaging.h"
26 #include "librpc/gen_ndr/ndr_messaging.h"
27
28 /* paths to these include files come from --with-ctdb= in configure */
29 #include "ctdb.h"
30 #include "ctdb_private.h"
31
32 struct ctdbd_connection {
33         struct messaging_context *msg_ctx;
34         uint32 reqid;
35         uint32 our_vnn;
36         uint64 rand_srvid;
37         struct packet_context *pkt;
38         struct fd_event *fde;
39         
40         void (*release_ip_handler)(const char *ip_addr, void *private_data);
41         void *release_ip_priv;
42 };
43
44 static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
45                               uint32_t vnn, uint32 opcode, 
46                               uint64_t srvid, uint32_t flags, TDB_DATA data, 
47                               TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
48                               int *cstatus);
49
50 /*
51  * exit on fatal communications errors with the ctdbd daemon
52  */
53 static void cluster_fatal(const char *why)
54 {
55         DEBUG(0,("cluster fatal event: %s - exiting immediately\n", why));
56         /* we don't use smb_panic() as we don't want to delay to write
57            a core file. We need to release this process id immediately
58            so that someone else can take over without getting sharing
59            violations */
60         _exit(1);
61 }
62
63 /*
64  *
65  */
66 static void ctdb_packet_dump(struct ctdb_req_header *hdr)
67 {
68         if (DEBUGLEVEL < 10) {
69                 return;
70         }
71         DEBUGADD(10, ("len=%d, magic=%x, vers=%d, gen=%d, op=%d, reqid=%d\n",
72                       (int)hdr->length, (int)hdr->ctdb_magic,
73                       (int)hdr->ctdb_version, (int)hdr->generation,
74                       (int)hdr->operation, (int)hdr->reqid));
75 }
76
77 /*
78  * Register a srvid with ctdbd
79  */
80 static NTSTATUS register_with_ctdbd(struct ctdbd_connection *conn,
81                                     uint64_t srvid)
82 {
83
84         int cstatus;
85         return ctdbd_control(conn, CTDB_CURRENT_NODE,
86                              CTDB_CONTROL_REGISTER_SRVID, srvid, 0,
87                              tdb_null, NULL, NULL, &cstatus);
88 }
89
90 /*
91  * get our vnn from the cluster
92  */
93 static NTSTATUS get_cluster_vnn(struct ctdbd_connection *conn, uint32 *vnn)
94 {
95         int32_t cstatus=-1;
96         NTSTATUS status;
97         status = ctdbd_control(conn,
98                                CTDB_CURRENT_NODE, CTDB_CONTROL_GET_PNN, 0, 0,
99                                tdb_null, NULL, NULL, &cstatus);
100         if (!NT_STATUS_IS_OK(status)) {
101                 cluster_fatal("ctdbd_control failed\n");
102         }
103         *vnn = (uint32_t)cstatus;
104         return status;
105 }
106
107 /*
108  * Are we active (i.e. not banned or stopped?)
109  */
110 static bool ctdbd_working(struct ctdbd_connection *conn, uint32_t vnn)
111 {
112         int32_t cstatus=-1;
113         NTSTATUS status;
114         TDB_DATA outdata;
115         struct ctdb_node_map *m;
116         uint32_t failure_flags;
117         bool ret = false;
118         int i;
119
120         status = ctdbd_control(conn, CTDB_CURRENT_NODE,
121                                CTDB_CONTROL_GET_NODEMAP, 0, 0,
122                                tdb_null, talloc_tos(), &outdata, &cstatus);
123         if (!NT_STATUS_IS_OK(status)) {
124                 cluster_fatal("ctdbd_control failed\n");
125         }
126         if ((cstatus != 0) || (outdata.dptr == NULL)) {
127                 DEBUG(2, ("Received invalid ctdb data\n"));
128                 return false;
129         }
130
131         m = (struct ctdb_node_map *)outdata.dptr;
132
133         for (i=0; i<m->num; i++) {
134                 if (vnn == m->nodes[i].pnn) {
135                         break;
136                 }
137         }
138
139         if (i == m->num) {
140                 DEBUG(2, ("Did not find ourselves (node %d) in nodemap\n",
141                           (int)vnn));
142                 goto fail;
143         }
144
145         failure_flags = NODE_FLAGS_BANNED | NODE_FLAGS_DISCONNECTED
146                 | NODE_FLAGS_PERMANENTLY_DISABLED | NODE_FLAGS_STOPPED;
147
148         if ((m->nodes[i].flags & failure_flags) != 0) {
149                 DEBUG(2, ("Node has status %x, not active\n",
150                           (int)m->nodes[i].flags));
151                 goto fail;
152         }
153
154         ret = true;
155 fail:
156         TALLOC_FREE(outdata.dptr);
157         return ret;;
158 }
159
160 uint32 ctdbd_vnn(const struct ctdbd_connection *conn)
161 {
162         return conn->our_vnn;
163 }
164
165 /*
166  * Get us a ctdb connection
167  */
168
169 static NTSTATUS ctdbd_connect(TALLOC_CTX *mem_ctx,
170                               struct packet_context **presult)
171 {
172         struct packet_context *result;
173         const char *sockname = lp_ctdbd_socket();
174         struct sockaddr_un addr;
175         int fd;
176
177         if (!sockname || !*sockname) {
178                 sockname = CTDB_PATH;
179         }
180
181         fd = socket(AF_UNIX, SOCK_STREAM, 0);
182         if (fd == -1) {
183                 DEBUG(3, ("Could not create socket: %s\n", strerror(errno)));
184                 return map_nt_error_from_unix(errno);
185         }
186
187         ZERO_STRUCT(addr);
188         addr.sun_family = AF_UNIX;
189         strncpy(addr.sun_path, sockname, sizeof(addr.sun_path));
190
191         if (sys_connect(fd, (struct sockaddr *)&addr) == -1) {
192                 DEBUG(1, ("connect(%s) failed: %s\n", sockname,
193                           strerror(errno)));
194                 close(fd);
195                 return map_nt_error_from_unix(errno);
196         }
197
198         if (!(result = packet_init(mem_ctx, fd))) {
199                 close(fd);
200                 return NT_STATUS_NO_MEMORY;
201         }
202
203         *presult = result;
204         return NT_STATUS_OK;
205 }
206
207 /*
208  * Do we have a complete ctdb packet in the queue?
209  */
210
211 static bool ctdb_req_complete(const uint8_t *buf, size_t available,
212                               size_t *length,
213                               void *private_data)
214 {
215         uint32 msglen;
216
217         if (available < sizeof(msglen)) {
218                 return False;
219         }
220
221         msglen = *((uint32 *)buf);
222
223         DEBUG(10, ("msglen = %d\n", msglen));
224
225         if (msglen < sizeof(struct ctdb_req_header)) {
226                 DEBUG(0, ("Got invalid msglen: %d, expected at least %d for "
227                           "the req_header\n", (int)msglen,
228                           (int)sizeof(struct ctdb_req_header)));
229                 cluster_fatal("ctdbd protocol error\n");
230         }
231
232         if (available < msglen) {
233                 return false;
234         }
235
236         *length = msglen;
237         return true;
238 }
239
240 /*
241  * State necessary to defer an incoming message while we are waiting for a
242  * ctdb reply.
243  */
244
245 struct deferred_msg_state {
246         struct messaging_context *msg_ctx;
247         struct messaging_rec *rec;
248 };
249
250 /*
251  * Timed event handler for the deferred message
252  */
253
254 static void deferred_message_dispatch(struct event_context *event_ctx,
255                                       struct timed_event *te,
256                                       struct timeval now,
257                                       void *private_data)
258 {
259         struct deferred_msg_state *state = talloc_get_type_abort(
260                 private_data, struct deferred_msg_state);
261
262         messaging_dispatch_rec(state->msg_ctx, state->rec);
263         TALLOC_FREE(state);
264         TALLOC_FREE(te);
265 }
266
267 struct req_pull_state {
268         TALLOC_CTX *mem_ctx;
269         DATA_BLOB req;
270 };
271
272 /*
273  * Pull a ctdb request out of the incoming packet queue
274  */
275
276 static NTSTATUS ctdb_req_pull(uint8_t *buf, size_t length,
277                               void *private_data)
278 {
279         struct req_pull_state *state = (struct req_pull_state *)private_data;
280
281         state->req.data = talloc_move(state->mem_ctx, &buf);
282         state->req.length = length;
283         return NT_STATUS_OK;
284 }
285
286 /*
287  * Fetch a messaging_rec from an incoming ctdb style message
288  */
289
290 static struct messaging_rec *ctdb_pull_messaging_rec(TALLOC_CTX *mem_ctx,
291                                                      size_t overall_length,
292                                                      struct ctdb_req_message *msg)
293 {
294         struct messaging_rec *result;
295         DATA_BLOB blob;
296         enum ndr_err_code ndr_err;
297
298         if ((overall_length < offsetof(struct ctdb_req_message, data))
299             || (overall_length
300                 < offsetof(struct ctdb_req_message, data) + msg->datalen)) {
301
302                 cluster_fatal("got invalid msg length");
303         }
304
305         if (!(result = TALLOC_P(mem_ctx, struct messaging_rec))) {
306                 DEBUG(0, ("talloc failed\n"));
307                 return NULL;
308         }
309
310         blob = data_blob_const(msg->data, msg->datalen);
311
312         ndr_err = ndr_pull_struct_blob(
313                 &blob, result, NULL, result,
314                 (ndr_pull_flags_fn_t)ndr_pull_messaging_rec);
315
316         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
317                 DEBUG(0, ("ndr_pull_struct_blob failed: %s\n",
318                           ndr_errstr(ndr_err)));
319                 TALLOC_FREE(result);
320                 return NULL;
321         }
322
323         if (DEBUGLEVEL >= 10) {
324                 DEBUG(10, ("ctdb_pull_messaging_rec:\n"));
325                 NDR_PRINT_DEBUG(messaging_rec, result);
326         }
327
328         return result;
329 }
330
331 static NTSTATUS ctdb_packet_fd_read_sync(struct packet_context *ctx)
332 {
333         struct timeval timeout;
334         struct timeval *ptimeout;
335
336         timeout = timeval_set(lp_ctdb_timeout(), 0);
337         ptimeout = (timeout.tv_sec != 0) ? &timeout : NULL;
338
339         return packet_fd_read_sync(ctx, ptimeout);
340 }
341
342 /*
343  * Read a full ctdbd request. If we have a messaging context, defer incoming
344  * messages that might come in between.
345  */
346
347 static NTSTATUS ctdb_read_req(struct ctdbd_connection *conn, uint32 reqid,
348                               TALLOC_CTX *mem_ctx, void *result)
349 {
350         struct ctdb_req_header *hdr;
351         struct req_pull_state state;
352         NTSTATUS status;
353
354  again:
355
356         status = ctdb_packet_fd_read_sync(conn->pkt);
357
358         if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_BUSY)) {
359                 /* EAGAIN */
360                 goto again;
361         } else if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
362                 /* EAGAIN */
363                 goto again;
364         }
365
366         if (!NT_STATUS_IS_OK(status)) {
367                 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status)));
368                 cluster_fatal("ctdbd died\n");
369         }
370
371  next_pkt:
372
373         ZERO_STRUCT(state);
374         state.mem_ctx = mem_ctx;
375
376         if (!packet_handler(conn->pkt, ctdb_req_complete, ctdb_req_pull,
377                             &state, &status)) {
378                 /*
379                  * Not enough data
380                  */
381                 DEBUG(10, ("not enough data from ctdb socket, retrying\n"));
382                 goto again;
383         }
384
385         if (!NT_STATUS_IS_OK(status)) {
386                 DEBUG(0, ("Could not read packet: %s\n", nt_errstr(status)));
387                 cluster_fatal("ctdbd died\n");
388         }
389
390         hdr = (struct ctdb_req_header *)state.req.data;
391
392         DEBUG(10, ("Received ctdb packet\n"));
393         ctdb_packet_dump(hdr);
394
395         if (hdr->operation == CTDB_REQ_MESSAGE) {
396                 struct timed_event *evt;
397                 struct deferred_msg_state *msg_state;
398                 struct ctdb_req_message *msg = (struct ctdb_req_message *)hdr;
399
400                 if (conn->msg_ctx == NULL) {
401                         DEBUG(1, ("Got a message without having a msg ctx, "
402                                   "dropping msg %llu\n",
403                                   (long long unsigned)msg->srvid));
404                         goto next_pkt;
405                 }
406                 
407                 if ((conn->release_ip_handler != NULL)
408                     && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
409                         /* must be dispatched immediately */
410                         DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
411                         conn->release_ip_handler((const char *)msg->data,
412                                                  conn->release_ip_priv);
413                         TALLOC_FREE(hdr);
414                         goto next_pkt;
415                 }
416
417                 if ((msg->srvid == CTDB_SRVID_RECONFIGURE)
418                     || (msg->srvid == CTDB_SRVID_SAMBA_NOTIFY)) {
419
420                         DEBUG(1, ("ctdb_read_req: Got %s message\n",
421                                   (msg->srvid == CTDB_SRVID_RECONFIGURE)
422                                   ? "cluster reconfigure" : "SAMBA_NOTIFY"));
423
424                         messaging_send(conn->msg_ctx, procid_self(),
425                                        MSG_SMB_BRL_VALIDATE, &data_blob_null);
426                         messaging_send(conn->msg_ctx, procid_self(),
427                                        MSG_DBWRAP_G_LOCK_RETRY,
428                                        &data_blob_null);
429                         TALLOC_FREE(hdr);
430                         goto next_pkt;
431                 }
432
433                 if (!(msg_state = TALLOC_P(NULL, struct deferred_msg_state))) {
434                         DEBUG(0, ("talloc failed\n"));
435                         TALLOC_FREE(hdr);
436                         goto next_pkt;
437                 }
438
439                 if (!(msg_state->rec = ctdb_pull_messaging_rec(
440                               msg_state, state.req.length, msg))) {
441                         DEBUG(0, ("ctdbd_pull_messaging_rec failed\n"));
442                         TALLOC_FREE(msg_state);
443                         TALLOC_FREE(hdr);
444                         goto next_pkt;
445                 }
446
447                 TALLOC_FREE(hdr);
448
449                 msg_state->msg_ctx = conn->msg_ctx;
450                 
451                 /*
452                  * We're waiting for a call reply, but an async message has
453                  * crossed. Defer dispatching to the toplevel event loop.
454                  */
455                 evt = event_add_timed(conn->msg_ctx->event_ctx,
456                                       conn->msg_ctx->event_ctx,
457                                       timeval_zero(),
458                                       deferred_message_dispatch,
459                                       msg_state);
460                 if (evt == NULL) {
461                         DEBUG(0, ("event_add_timed failed\n"));
462                         TALLOC_FREE(msg_state);
463                         TALLOC_FREE(hdr);
464                         goto next_pkt;
465                 }
466                 
467                 goto next_pkt;
468         }
469
470         if (hdr->reqid != reqid) {
471                 /* we got the wrong reply */
472                 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
473                          "been %u\n", hdr->reqid, reqid));
474                 TALLOC_FREE(hdr);
475                 goto again;
476         }
477
478         *((void **)result) = talloc_move(mem_ctx, &hdr);
479
480         return NT_STATUS_OK;
481 }
482
483 /*
484  * Get us a ctdbd connection
485  */
486
487 NTSTATUS ctdbd_init_connection(TALLOC_CTX *mem_ctx,
488                                struct ctdbd_connection **pconn)
489 {
490         struct ctdbd_connection *conn;
491         NTSTATUS status;
492
493         if (!(conn = TALLOC_ZERO_P(mem_ctx, struct ctdbd_connection))) {
494                 DEBUG(0, ("talloc failed\n"));
495                 return NT_STATUS_NO_MEMORY;
496         }
497
498         status = ctdbd_connect(conn, &conn->pkt);
499
500         if (!NT_STATUS_IS_OK(status)) {
501                 DEBUG(10, ("ctdbd_connect failed: %s\n", nt_errstr(status)));
502                 goto fail;
503         }
504
505         status = get_cluster_vnn(conn, &conn->our_vnn);
506
507         if (!NT_STATUS_IS_OK(status)) {
508                 DEBUG(10, ("get_cluster_vnn failed: %s\n", nt_errstr(status)));
509                 goto fail;
510         }
511
512         if (!ctdbd_working(conn, conn->our_vnn)) {
513                 DEBUG(2, ("Node is not working, can not connect\n"));
514                 status = NT_STATUS_INTERNAL_DB_ERROR;
515                 goto fail;
516         }
517
518         generate_random_buffer((unsigned char *)&conn->rand_srvid,
519                                sizeof(conn->rand_srvid));
520
521         status = register_with_ctdbd(conn, conn->rand_srvid);
522
523         if (!NT_STATUS_IS_OK(status)) {
524                 DEBUG(5, ("Could not register random srvid: %s\n",
525                           nt_errstr(status)));
526                 goto fail;
527         }
528
529         *pconn = conn;
530         return NT_STATUS_OK;
531
532  fail:
533         TALLOC_FREE(conn);
534         return status;
535 }
536
537 /*
538  * Get us a ctdbd connection and register us as a process
539  */
540
541 NTSTATUS ctdbd_messaging_connection(TALLOC_CTX *mem_ctx,
542                                     struct ctdbd_connection **pconn)
543 {
544         struct ctdbd_connection *conn;
545         NTSTATUS status;
546
547         status = ctdbd_init_connection(mem_ctx, &conn);
548
549         if (!NT_STATUS_IS_OK(status)) {
550                 return status;
551         }
552
553         status = register_with_ctdbd(conn, (uint64_t)sys_getpid());
554         if (!NT_STATUS_IS_OK(status)) {
555                 goto fail;
556         }
557
558         status = register_with_ctdbd(conn, MSG_SRVID_SAMBA);
559         if (!NT_STATUS_IS_OK(status)) {
560                 goto fail;
561         }
562
563         status = register_with_ctdbd(conn, CTDB_SRVID_SAMBA_NOTIFY);
564         if (!NT_STATUS_IS_OK(status)) {
565                 goto fail;
566         }
567
568         *pconn = conn;
569         return NT_STATUS_OK;
570
571  fail:
572         TALLOC_FREE(conn);
573         return status;
574 }
575
576 struct messaging_context *ctdb_conn_msg_ctx(struct ctdbd_connection *conn)
577 {
578         return conn->msg_ctx;
579 }
580
581 int ctdbd_conn_get_fd(struct ctdbd_connection *conn)
582 {
583         return packet_get_fd(conn->pkt);
584 }
585
586 /*
587  * Packet handler to receive and handle a ctdb message
588  */
589 static NTSTATUS ctdb_handle_message(uint8_t *buf, size_t length,
590                                     void *private_data)
591 {
592         struct ctdbd_connection *conn = talloc_get_type_abort(
593                 private_data, struct ctdbd_connection);
594         struct ctdb_req_message *msg;
595         struct messaging_rec *msg_rec;
596
597         msg = (struct ctdb_req_message *)buf;
598
599         if (msg->hdr.operation != CTDB_REQ_MESSAGE) {
600                 DEBUG(0, ("Received async msg of type %u, discarding\n",
601                           msg->hdr.operation));
602                 TALLOC_FREE(buf);
603                 return NT_STATUS_INVALID_PARAMETER;
604         }
605
606         if ((conn->release_ip_handler != NULL)
607             && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
608                 /* must be dispatched immediately */
609                 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
610                 conn->release_ip_handler((const char *)msg->data,
611                                          conn->release_ip_priv);
612                 TALLOC_FREE(buf);
613                 return NT_STATUS_OK;
614         }
615
616         SMB_ASSERT(conn->msg_ctx != NULL);
617
618         if ((msg->srvid == CTDB_SRVID_RECONFIGURE)
619             || (msg->srvid == CTDB_SRVID_SAMBA_NOTIFY)){
620                 DEBUG(0,("Got cluster reconfigure message\n"));
621                 /*
622                  * when the cluster is reconfigured or someone of the
623                  * family has passed away (SAMBA_NOTIFY), we need to
624                  * clean the brl database
625                  */
626                 messaging_send(conn->msg_ctx, procid_self(),
627                                MSG_SMB_BRL_VALIDATE, &data_blob_null);
628
629                 messaging_send(conn->msg_ctx, procid_self(),
630                                MSG_DBWRAP_G_LOCK_RETRY,
631                                &data_blob_null);
632
633                 TALLOC_FREE(buf);
634
635                 return NT_STATUS_OK;
636         }
637
638         /* only messages to our pid or the broadcast are valid here */
639         if (msg->srvid != sys_getpid() && msg->srvid != MSG_SRVID_SAMBA) {
640                 DEBUG(0,("Got unexpected message with srvid=%llu\n", 
641                          (unsigned long long)msg->srvid));
642                 TALLOC_FREE(buf);
643                 return NT_STATUS_OK;
644         }
645
646         if (!(msg_rec = ctdb_pull_messaging_rec(NULL, length, msg))) {
647                 DEBUG(10, ("ctdb_pull_messaging_rec failed\n"));
648                 TALLOC_FREE(buf);
649                 return NT_STATUS_NO_MEMORY;
650         }
651
652         messaging_dispatch_rec(conn->msg_ctx, msg_rec);
653
654         TALLOC_FREE(msg_rec);
655         TALLOC_FREE(buf);
656         return NT_STATUS_OK;
657 }
658
659 /*
660  * The ctdbd socket is readable asynchronuously
661  */
662
663 static void ctdbd_socket_handler(struct event_context *event_ctx,
664                                  struct fd_event *event,
665                                  uint16 flags,
666                                  void *private_data)
667 {
668         struct ctdbd_connection *conn = talloc_get_type_abort(
669                 private_data, struct ctdbd_connection);
670
671         NTSTATUS status;
672
673         status = packet_fd_read(conn->pkt);
674
675         if (!NT_STATUS_IS_OK(status)) {
676                 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status)));
677                 cluster_fatal("ctdbd died\n");
678         }
679
680         while (packet_handler(conn->pkt, ctdb_req_complete,
681                               ctdb_handle_message, conn, &status)) {
682                 if (!NT_STATUS_IS_OK(status)) {
683                         DEBUG(10, ("could not handle incoming message: %s\n",
684                                    nt_errstr(status)));
685                 }
686         }
687 }
688
689 /*
690  * Prepare a ctdbd connection to receive messages
691  */
692
693 NTSTATUS ctdbd_register_msg_ctx(struct ctdbd_connection *conn,
694                                 struct messaging_context *msg_ctx)
695 {
696         SMB_ASSERT(conn->msg_ctx == NULL);
697         SMB_ASSERT(conn->fde == NULL);
698
699         if (!(conn->fde = event_add_fd(msg_ctx->event_ctx, conn,
700                                        packet_get_fd(conn->pkt),
701                                        EVENT_FD_READ,
702                                        ctdbd_socket_handler,
703                                        conn))) {
704                 DEBUG(0, ("event_add_fd failed\n"));
705                 return NT_STATUS_NO_MEMORY;
706         }
707
708         conn->msg_ctx = msg_ctx;
709
710         return NT_STATUS_OK;
711 }
712
713 /*
714  * Send a messaging message across a ctdbd
715  */
716
717 NTSTATUS ctdbd_messaging_send(struct ctdbd_connection *conn,
718                               uint32 dst_vnn, uint64 dst_srvid,
719                               struct messaging_rec *msg)
720 {
721         struct ctdb_req_message r;
722         TALLOC_CTX *mem_ctx;
723         DATA_BLOB blob;
724         NTSTATUS status;
725         enum ndr_err_code ndr_err;
726
727         if (!(mem_ctx = talloc_init("ctdbd_messaging_send"))) {
728                 DEBUG(0, ("talloc failed\n"));
729                 return NT_STATUS_NO_MEMORY;
730         }
731
732         ndr_err = ndr_push_struct_blob(
733                 &blob, mem_ctx, NULL, msg,
734                 (ndr_push_flags_fn_t)ndr_push_messaging_rec);
735
736         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
737                 DEBUG(0, ("ndr_push_struct_blob failed: %s\n",
738                           ndr_errstr(ndr_err)));
739                 status = ndr_map_error2ntstatus(ndr_err);
740                 goto fail;
741         }
742
743         r.hdr.length = offsetof(struct ctdb_req_message, data) + blob.length;
744         r.hdr.ctdb_magic = CTDB_MAGIC;
745         r.hdr.ctdb_version = CTDB_VERSION;
746         r.hdr.generation = 1;
747         r.hdr.operation  = CTDB_REQ_MESSAGE;
748         r.hdr.destnode   = dst_vnn;
749         r.hdr.srcnode    = conn->our_vnn;
750         r.hdr.reqid      = 0;
751         r.srvid          = dst_srvid;
752         r.datalen        = blob.length;
753
754         DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
755         ctdb_packet_dump(&r.hdr);
756
757         status = packet_send(
758                 conn->pkt, 2,
759                 data_blob_const(&r, offsetof(struct ctdb_req_message, data)),
760                 blob);
761
762         if (!NT_STATUS_IS_OK(status)) {
763                 DEBUG(0, ("packet_send failed: %s\n", nt_errstr(status)));
764                 goto fail;
765         }
766
767         status = packet_flush(conn->pkt);
768
769         if (!NT_STATUS_IS_OK(status)) {
770                 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
771                 cluster_fatal("cluster dispatch daemon msg write error\n");
772         }
773
774         status = NT_STATUS_OK;
775  fail:
776         TALLOC_FREE(mem_ctx);
777         return status;
778 }
779
780 /*
781  * send/recv a generic ctdb control message
782  */
783 static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
784                               uint32_t vnn, uint32 opcode, 
785                               uint64_t srvid, uint32_t flags, 
786                               TDB_DATA data, 
787                               TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
788                               int *cstatus)
789 {
790         struct ctdb_req_control req;
791         struct ctdb_reply_control *reply = NULL;
792         struct ctdbd_connection *new_conn = NULL;
793         NTSTATUS status;
794
795         /* the samba3 ctdb code can't handle NOREPLY yet */
796         flags &= ~CTDB_CTRL_FLAG_NOREPLY;
797
798         if (conn == NULL) {
799                 status = ctdbd_init_connection(NULL, &new_conn);
800
801                 if (!NT_STATUS_IS_OK(status)) {
802                         DEBUG(10, ("Could not init temp connection: %s\n",
803                                    nt_errstr(status)));
804                         goto fail;
805                 }
806
807                 conn = new_conn;
808         }
809
810         ZERO_STRUCT(req);
811         req.hdr.length = offsetof(struct ctdb_req_control, data) + data.dsize;
812         req.hdr.ctdb_magic   = CTDB_MAGIC;
813         req.hdr.ctdb_version = CTDB_VERSION;
814         req.hdr.operation    = CTDB_REQ_CONTROL;
815         req.hdr.reqid        = ++conn->reqid;
816         req.hdr.destnode     = vnn;
817         req.opcode           = opcode;
818         req.srvid            = srvid;
819         req.datalen          = data.dsize;
820
821         DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
822         ctdb_packet_dump(&req.hdr);
823
824         status = packet_send(
825                 conn->pkt, 2,
826                 data_blob_const(&req, offsetof(struct ctdb_req_control, data)),
827                 data_blob_const(data.dptr, data.dsize));
828
829         if (!NT_STATUS_IS_OK(status)) {
830                 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
831                 goto fail;
832         }
833
834         status = packet_flush(conn->pkt);
835
836         if (!NT_STATUS_IS_OK(status)) {
837                 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
838                 cluster_fatal("cluster dispatch daemon control write error\n");
839         }
840
841         if (flags & CTDB_CTRL_FLAG_NOREPLY) {
842                 TALLOC_FREE(new_conn);
843                 if (cstatus) {
844                         *cstatus = 0;
845                 }
846                 return NT_STATUS_OK;
847         }
848
849         status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
850
851         if (!NT_STATUS_IS_OK(status)) {
852                 DEBUG(10, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
853                 goto fail;
854         }
855
856         if (reply->hdr.operation != CTDB_REPLY_CONTROL) {
857                 DEBUG(0, ("received invalid reply\n"));
858                 goto fail;
859         }
860
861         if (outdata) {
862                 if (!(outdata->dptr = (uint8 *)talloc_memdup(
863                               mem_ctx, reply->data, reply->datalen))) {
864                         TALLOC_FREE(reply);
865                         return NT_STATUS_NO_MEMORY;
866                 }
867                 outdata->dsize = reply->datalen;
868         }
869         if (cstatus) {
870                 (*cstatus) = reply->status;
871         }
872
873         status = NT_STATUS_OK;
874
875  fail:
876         TALLOC_FREE(new_conn);
877         TALLOC_FREE(reply);
878         return status;
879 }
880
881 /*
882  * see if a remote process exists
883  */
884 bool ctdbd_process_exists(struct ctdbd_connection *conn, uint32 vnn, pid_t pid)
885 {
886         NTSTATUS status;
887         TDB_DATA data;
888         int32_t cstatus;
889
890         data.dptr = (uint8_t*)&pid;
891         data.dsize = sizeof(pid);
892
893         status = ctdbd_control(conn, vnn, CTDB_CONTROL_PROCESS_EXISTS, 0, 0,
894                                data, NULL, NULL, &cstatus);
895         if (!NT_STATUS_IS_OK(status)) {
896                 DEBUG(0, (__location__ " ctdb_control for process_exists "
897                           "failed\n"));
898                 return False;
899         }
900
901         return cstatus == 0;
902 }
903
904 /*
905  * Get a db path
906  */
907 char *ctdbd_dbpath(struct ctdbd_connection *conn,
908                    TALLOC_CTX *mem_ctx, uint32_t db_id)
909 {
910         NTSTATUS status;
911         TDB_DATA data;
912         int32_t cstatus;
913
914         data.dptr = (uint8_t*)&db_id;
915         data.dsize = sizeof(db_id);
916
917         status = ctdbd_control(conn, CTDB_CURRENT_NODE,
918                                CTDB_CONTROL_GETDBPATH, 0, 0, data, 
919                                mem_ctx, &data, &cstatus);
920         if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
921                 DEBUG(0,(__location__ " ctdb_control for getdbpath failed\n"));
922                 return NULL;
923         }
924
925         return (char *)data.dptr;
926 }
927
928 /*
929  * attach to a ctdb database
930  */
931 NTSTATUS ctdbd_db_attach(struct ctdbd_connection *conn,
932                          const char *name, uint32_t *db_id, int tdb_flags)
933 {
934         NTSTATUS status;
935         TDB_DATA data;
936         int32_t cstatus;
937         bool persistent = (tdb_flags & TDB_CLEAR_IF_FIRST) == 0;
938
939         data.dptr = (uint8_t*)name;
940         data.dsize = strlen(name)+1;
941
942         status = ctdbd_control(conn, CTDB_CURRENT_NODE,
943                                persistent
944                                ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
945                                : CTDB_CONTROL_DB_ATTACH,
946                                tdb_flags, 0, data, NULL, &data, &cstatus);
947         if (!NT_STATUS_IS_OK(status)) {
948                 DEBUG(0, (__location__ " ctdb_control for db_attach "
949                           "failed: %s\n", nt_errstr(status)));
950                 return status;
951         }
952
953         if (cstatus != 0 || data.dsize != sizeof(uint32_t)) {
954                 DEBUG(0,(__location__ " ctdb_control for db_attach failed\n"));
955                 return NT_STATUS_INTERNAL_ERROR;
956         }
957
958         *db_id = *(uint32_t *)data.dptr;
959         talloc_free(data.dptr);
960
961         if (!(tdb_flags & TDB_SEQNUM)) {
962                 return NT_STATUS_OK;
963         }
964
965         data.dptr = (uint8_t *)db_id;
966         data.dsize = sizeof(*db_id);
967
968         status = ctdbd_control(conn, CTDB_CURRENT_NODE,
969                                CTDB_CONTROL_ENABLE_SEQNUM, 0, 0, data, 
970                                NULL, NULL, &cstatus);
971         if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
972                 DEBUG(0,(__location__ " ctdb_control for enable seqnum "
973                          "failed\n"));
974                 return NT_STATUS_IS_OK(status) ? NT_STATUS_INTERNAL_ERROR :
975                         status;
976         }
977
978         return NT_STATUS_OK;
979 }
980
981 /*
982  * force the migration of a record to this node
983  */
984 NTSTATUS ctdbd_migrate(struct ctdbd_connection *conn, uint32 db_id,
985                        TDB_DATA key)
986 {
987         struct ctdb_req_call req;
988         struct ctdb_reply_call *reply;
989         NTSTATUS status;
990
991         ZERO_STRUCT(req);
992         
993         req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
994         req.hdr.ctdb_magic   = CTDB_MAGIC;
995         req.hdr.ctdb_version = CTDB_VERSION;
996         req.hdr.operation    = CTDB_REQ_CALL;
997         req.hdr.reqid        = ++conn->reqid;
998         req.flags            = CTDB_IMMEDIATE_MIGRATION;
999         req.callid           = CTDB_NULL_FUNC;
1000         req.db_id            = db_id;
1001         req.keylen           = key.dsize;
1002
1003         DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
1004         ctdb_packet_dump(&req.hdr);
1005
1006         status = packet_send(
1007                 conn->pkt, 2,
1008                 data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
1009                 data_blob_const(key.dptr, key.dsize));
1010
1011         if (!NT_STATUS_IS_OK(status)) {
1012                 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
1013                 return status;
1014         }
1015
1016         status = packet_flush(conn->pkt);
1017
1018         if (!NT_STATUS_IS_OK(status)) {
1019                 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
1020                 cluster_fatal("cluster dispatch daemon control write error\n");
1021         }
1022
1023         status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
1024
1025         if (!NT_STATUS_IS_OK(status)) {
1026                 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
1027                 goto fail;
1028         }
1029
1030         if (reply->hdr.operation != CTDB_REPLY_CALL) {
1031                 DEBUG(0, ("received invalid reply\n"));
1032                 status = NT_STATUS_INTERNAL_ERROR;
1033                 goto fail;
1034         }
1035
1036         status = NT_STATUS_OK;
1037  fail:
1038
1039         TALLOC_FREE(reply);
1040         return status;
1041 }
1042
1043 /*
1044  * remotely fetch a record without locking it or forcing a migration
1045  */
1046 NTSTATUS ctdbd_fetch(struct ctdbd_connection *conn, uint32 db_id,
1047                      TDB_DATA key, TALLOC_CTX *mem_ctx, TDB_DATA *data)
1048 {
1049         struct ctdb_req_call req;
1050         struct ctdb_reply_call *reply;
1051         NTSTATUS status;
1052
1053         ZERO_STRUCT(req);
1054         
1055         req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
1056         req.hdr.ctdb_magic   = CTDB_MAGIC;
1057         req.hdr.ctdb_version = CTDB_VERSION;
1058         req.hdr.operation    = CTDB_REQ_CALL;
1059         req.hdr.reqid        = ++conn->reqid;
1060         req.flags            = 0;
1061         req.callid           = CTDB_FETCH_FUNC;
1062         req.db_id            = db_id;
1063         req.keylen           = key.dsize;
1064
1065         status = packet_send(
1066                 conn->pkt, 2,
1067                 data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
1068                 data_blob_const(key.dptr, key.dsize));
1069
1070         if (!NT_STATUS_IS_OK(status)) {
1071                 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
1072                 return status;
1073         }
1074
1075         status = packet_flush(conn->pkt);
1076
1077         if (!NT_STATUS_IS_OK(status)) {
1078                 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
1079                 cluster_fatal("cluster dispatch daemon control write error\n");
1080         }
1081
1082         status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
1083
1084         if (!NT_STATUS_IS_OK(status)) {
1085                 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
1086                 goto fail;
1087         }
1088
1089         if (reply->hdr.operation != CTDB_REPLY_CALL) {
1090                 DEBUG(0, ("received invalid reply\n"));
1091                 status = NT_STATUS_INTERNAL_ERROR;
1092                 goto fail;
1093         }
1094
1095         data->dsize = reply->datalen;
1096         if (data->dsize == 0) {
1097                 data->dptr = NULL;
1098                 goto done;
1099         }
1100
1101         data->dptr = (uint8 *)talloc_memdup(mem_ctx, &reply->data[0],
1102                                             reply->datalen);
1103         if (data->dptr == NULL) {
1104                 DEBUG(0, ("talloc failed\n"));
1105                 status = NT_STATUS_NO_MEMORY;
1106                 goto fail;
1107         }
1108
1109  done:
1110         status = NT_STATUS_OK;
1111  fail:
1112         TALLOC_FREE(reply);
1113         return status;
1114 }
1115
1116 struct ctdbd_traverse_state {
1117         void (*fn)(TDB_DATA key, TDB_DATA data, void *private_data);
1118         void *private_data;
1119 };
1120
1121 /*
1122  * Handle a traverse record coming in on the ctdbd connection
1123  */
1124
1125 static NTSTATUS ctdb_traverse_handler(uint8_t *buf, size_t length,
1126                                       void *private_data)
1127 {
1128         struct ctdbd_traverse_state *state =
1129                 (struct ctdbd_traverse_state *)private_data;
1130
1131         struct ctdb_req_message *m;
1132         struct ctdb_rec_data *d;
1133         TDB_DATA key, data;
1134
1135         m = (struct ctdb_req_message *)buf;
1136
1137         if (length < sizeof(*m) || m->hdr.length != length) {
1138                 DEBUG(0, ("Got invalid message of length %d\n", (int)length));
1139                 TALLOC_FREE(buf);
1140                 return NT_STATUS_UNEXPECTED_IO_ERROR;
1141         }
1142
1143         d = (struct ctdb_rec_data *)&m->data[0];
1144         if (m->datalen < sizeof(uint32_t) || m->datalen != d->length) {
1145                 DEBUG(0, ("Got invalid traverse data of length %d\n",
1146                           (int)m->datalen));
1147                 TALLOC_FREE(buf);
1148                 return NT_STATUS_UNEXPECTED_IO_ERROR;
1149         }
1150
1151         key.dsize = d->keylen;
1152         key.dptr  = &d->data[0];
1153         data.dsize = d->datalen;
1154         data.dptr = &d->data[d->keylen];                
1155
1156         if (key.dsize == 0 && data.dsize == 0) {
1157                 /* end of traverse */
1158                 return NT_STATUS_END_OF_FILE;
1159         }
1160
1161         if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
1162                 DEBUG(0, ("Got invalid ltdb header length %d\n",
1163                           (int)data.dsize));
1164                 TALLOC_FREE(buf);
1165                 return NT_STATUS_UNEXPECTED_IO_ERROR;
1166         }
1167         data.dsize -= sizeof(struct ctdb_ltdb_header);
1168         data.dptr += sizeof(struct ctdb_ltdb_header);
1169
1170         if (state->fn) {
1171                 state->fn(key, data, state->private_data);
1172         }
1173
1174         TALLOC_FREE(buf);
1175         return NT_STATUS_OK;
1176 }
1177
1178 /*
1179   Traverse a ctdb database. This uses a kind-of hackish way to open a second
1180   connection to ctdbd to avoid the hairy recursive and async problems with
1181   everything in-line.
1182 */
1183
1184 NTSTATUS ctdbd_traverse(uint32 db_id,
1185                         void (*fn)(TDB_DATA key, TDB_DATA data,
1186                                    void *private_data),
1187                         void *private_data)
1188 {
1189         struct ctdbd_connection *conn;
1190         NTSTATUS status;
1191
1192         TDB_DATA data;
1193         struct ctdb_traverse_start t;
1194         int cstatus;
1195         struct ctdbd_traverse_state state;
1196
1197         status = ctdbd_init_connection(NULL, &conn);
1198         if (!NT_STATUS_IS_OK(status)) {
1199                 DEBUG(0, ("ctdbd_init_connection failed: %s\n",
1200                           nt_errstr(status)));
1201                 return status;
1202         }
1203
1204         t.db_id = db_id;
1205         t.srvid = conn->rand_srvid;
1206         t.reqid = ++conn->reqid;
1207
1208         data.dptr = (uint8_t *)&t;
1209         data.dsize = sizeof(t);
1210
1211         status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1212                                CTDB_CONTROL_TRAVERSE_START, conn->rand_srvid, 0,
1213                                data, NULL, NULL, &cstatus);
1214
1215         if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
1216
1217                 DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status),
1218                          cstatus));
1219
1220                 if (NT_STATUS_IS_OK(status)) {
1221                         /*
1222                          * We need a mapping here
1223                          */
1224                         status = NT_STATUS_UNSUCCESSFUL;
1225                 }
1226                 goto done;
1227         }
1228
1229         state.fn = fn;
1230         state.private_data = private_data;
1231
1232         while (True) {
1233
1234                 status = NT_STATUS_OK;
1235
1236                 if (packet_handler(conn->pkt, ctdb_req_complete,
1237                                    ctdb_traverse_handler, &state, &status)) {
1238
1239                         if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
1240                                 status = NT_STATUS_OK;
1241                                 break;
1242                         }
1243
1244                         /*
1245                          * There might be more in the queue
1246                          */
1247                         continue;
1248                 }
1249
1250                 if (!NT_STATUS_IS_OK(status)) {
1251                         break;
1252                 }
1253
1254                 status = ctdb_packet_fd_read_sync(conn->pkt);
1255
1256                 if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1257                         /*
1258                          * There might be more in the queue
1259                          */
1260                         continue;
1261                 }
1262
1263                 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
1264                         status = NT_STATUS_OK;
1265                         break;
1266                 }
1267
1268                 if (!NT_STATUS_IS_OK(status)) {
1269                         DEBUG(0, ("packet_fd_read_sync failed: %s\n", nt_errstr(status)));
1270                         cluster_fatal("ctdbd died\n");
1271                 }
1272         }
1273
1274  done:
1275         TALLOC_FREE(conn);
1276         return status;
1277 }
1278
1279 /*
1280    This is used to canonicalize a ctdb_sock_addr structure.
1281 */
1282 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage *in,
1283                                       struct sockaddr_storage *out)
1284 {
1285         memcpy(out, in, sizeof (*out));
1286
1287 #ifdef HAVE_IPV6
1288         if (in->ss_family == AF_INET6) {
1289                 const char prefix[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
1290                 const struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)in;
1291                 struct sockaddr_in *out4 = (struct sockaddr_in *)out;
1292                 if (memcmp(&in6->sin6_addr, prefix, 12) == 0) {
1293                         memset(out, 0, sizeof(*out));
1294 #ifdef HAVE_SOCK_SIN_LEN
1295                         out4->sin_len = sizeof(*out);
1296 #endif
1297                         out4->sin_family = AF_INET;
1298                         out4->sin_port   = in6->sin6_port;
1299                         memcpy(&out4->sin_addr, &in6->sin6_addr.s6_addr32[3], 4);
1300                 }
1301         }
1302 #endif
1303 }
1304
1305 /*
1306  * Register us as a server for a particular tcp connection
1307  */
1308
1309 NTSTATUS ctdbd_register_ips(struct ctdbd_connection *conn,
1310                             const struct sockaddr_storage *_server,
1311                             const struct sockaddr_storage *_client,
1312                             void (*release_ip_handler)(const char *ip_addr,
1313                                                        void *private_data),
1314                             void *private_data)
1315 {
1316         /*
1317          * we still use ctdb_control_tcp for ipv4
1318          * because we want to work against older ctdb
1319          * versions at runtime
1320          */
1321         struct ctdb_control_tcp p4;
1322 #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1323         struct ctdb_control_tcp_addr p;
1324 #endif
1325         TDB_DATA data;
1326         NTSTATUS status;
1327         struct sockaddr_storage client;
1328         struct sockaddr_storage server;
1329
1330         /*
1331          * Only one connection so far
1332          */
1333         SMB_ASSERT(conn->release_ip_handler == NULL);
1334
1335         smbd_ctdb_canonicalize_ip(_client, &client);
1336         smbd_ctdb_canonicalize_ip(_server, &server);
1337
1338         switch (client.ss_family) {
1339         case AF_INET:
1340                 p4.dest = *(struct sockaddr_in *)&server;
1341                 p4.src = *(struct sockaddr_in *)&client;
1342                 data.dptr = (uint8_t *)&p4;
1343                 data.dsize = sizeof(p4);
1344                 break;
1345 #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1346         case AF_INET6:
1347                 p.dest.ip6 = *(struct sockaddr_in6 *)&server;
1348                 p.src.ip6 = *(struct sockaddr_in6 *)&client;
1349                 data.dptr = (uint8_t *)&p;
1350                 data.dsize = sizeof(p);
1351                 break;
1352 #endif
1353         default:
1354                 return NT_STATUS_INTERNAL_ERROR;
1355         }
1356
1357         conn->release_ip_handler = release_ip_handler;
1358
1359         /*
1360          * We want to be told about IP releases
1361          */
1362
1363         status = register_with_ctdbd(conn, CTDB_SRVID_RELEASE_IP);
1364         if (!NT_STATUS_IS_OK(status)) {
1365                 return status;
1366         }
1367
1368         /*
1369          * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1370          * can send an extra ack to trigger a reset for our client, so it
1371          * immediately reconnects
1372          */
1373         return ctdbd_control(conn, CTDB_CURRENT_NODE, 
1374                              CTDB_CONTROL_TCP_CLIENT, 0,
1375                              CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL, NULL);
1376 }
1377
1378 /*
1379  * We want to handle reconfigure events
1380  */
1381 NTSTATUS ctdbd_register_reconfigure(struct ctdbd_connection *conn)
1382 {
1383         return register_with_ctdbd(conn, CTDB_SRVID_RECONFIGURE);
1384 }
1385
1386 /*
1387   call a control on the local node
1388  */
1389 NTSTATUS ctdbd_control_local(struct ctdbd_connection *conn, uint32 opcode, 
1390                              uint64_t srvid, uint32_t flags, TDB_DATA data, 
1391                              TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
1392                              int *cstatus)
1393 {
1394         return ctdbd_control(conn, CTDB_CURRENT_NODE, opcode, srvid, flags, data, mem_ctx, outdata, cstatus);
1395 }
1396
1397 NTSTATUS ctdb_watch_us(struct ctdbd_connection *conn)
1398 {
1399         struct ctdb_client_notify_register reg_data;
1400         size_t struct_len;
1401         NTSTATUS status;
1402         int cstatus;
1403
1404         reg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1405         reg_data.len = 1;
1406         reg_data.notify_data[0] = 0;
1407
1408         struct_len = offsetof(struct ctdb_client_notify_register,
1409                               notify_data) + reg_data.len;
1410
1411         status = ctdbd_control_local(
1412                 conn, CTDB_CONTROL_REGISTER_NOTIFY, conn->rand_srvid, 0,
1413                 make_tdb_data((uint8_t *)&reg_data, struct_len),
1414                 NULL, NULL, &cstatus);
1415         if (!NT_STATUS_IS_OK(status)) {
1416                 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1417                           nt_errstr(status)));
1418         }
1419         return status;
1420 }
1421
1422 NTSTATUS ctdb_unwatch(struct ctdbd_connection *conn)
1423 {
1424         struct ctdb_client_notify_deregister dereg_data;
1425         NTSTATUS status;
1426         int cstatus;
1427
1428         dereg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1429
1430         status = ctdbd_control_local(
1431                 conn, CTDB_CONTROL_DEREGISTER_NOTIFY, conn->rand_srvid, 0,
1432                 make_tdb_data((uint8_t *)&dereg_data, sizeof(dereg_data)),
1433                 NULL, NULL, &cstatus);
1434         if (!NT_STATUS_IS_OK(status)) {
1435                 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1436                           nt_errstr(status)));
1437         }
1438         return status;
1439 }
1440
1441 #else
1442
1443 NTSTATUS ctdbd_init_connection(TALLOC_CTX *mem_ctx,
1444                                struct ctdbd_connection **pconn)
1445 {
1446         return NT_STATUS_NOT_IMPLEMENTED;
1447 }
1448
1449 #endif