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