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