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