lib: Add messaging_send_all
[samba.git] / source3 / lib / messages.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba internal messaging functions
4    Copyright (C) Andrew Tridgell 2000
5    Copyright (C) 2001 by Martin Pool
6    Copyright (C) 2002 by Jeremy Allison
7    Copyright (C) 2007 by Volker Lendecke
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 /**
24   @defgroup messages Internal messaging framework
25   @{
26   @file messages.c
27
28   @brief  Module for internal messaging between Samba daemons. 
29
30    The idea is that if a part of Samba wants to do communication with
31    another Samba process then it will do a message_register() of a
32    dispatch function, and use message_send_pid() to send messages to
33    that process.
34
35    The dispatch function is given the pid of the sender, and it can
36    use that to reply by message_send_pid().  See ping_message() for a
37    simple example.
38
39    @caution Dispatch functions must be able to cope with incoming
40    messages on an *odd* byte boundary.
41
42    This system doesn't have any inherent size limitations but is not
43    very efficient for large messages or when messages are sent in very
44    quick succession.
45
46 */
47
48 #include "includes.h"
49 #include "lib/util/server_id.h"
50 #include "dbwrap/dbwrap.h"
51 #include "serverid.h"
52 #include "messages.h"
53 #include "lib/util/tevent_unix.h"
54 #include "lib/background.h"
55 #include "lib/messages_dgm.h"
56 #include "lib/util/iov_buf.h"
57 #include "lib/util/server_id_db.h"
58 #include "lib/messages_dgm_ref.h"
59 #include "lib/messages_ctdb.h"
60 #include "lib/messages_ctdb_ref.h"
61 #include "lib/messages_util.h"
62 #include "cluster_support.h"
63 #include "ctdbd_conn.h"
64 #include "ctdb_srvids.h"
65
66 #ifdef CLUSTER_SUPPORT
67 #include "ctdb_protocol.h"
68 #endif
69
70 struct messaging_callback {
71         struct messaging_callback *prev, *next;
72         uint32_t msg_type;
73         void (*fn)(struct messaging_context *msg, void *private_data, 
74                    uint32_t msg_type, 
75                    struct server_id server_id, DATA_BLOB *data);
76         void *private_data;
77 };
78
79 struct messaging_registered_ev {
80         struct tevent_context *ev;
81         struct tevent_immediate *im;
82         size_t refcount;
83 };
84
85 struct messaging_context {
86         struct server_id id;
87         struct tevent_context *event_ctx;
88         struct messaging_callback *callbacks;
89
90         struct messaging_rec *posted_msgs;
91
92         struct messaging_registered_ev *event_contexts;
93
94         struct tevent_req **new_waiters;
95         size_t num_new_waiters;
96
97         struct tevent_req **waiters;
98         size_t num_waiters;
99
100         void *msg_dgm_ref;
101         void *msg_ctdb_ref;
102
103         struct server_id_db *names_db;
104 };
105
106 static struct messaging_rec *messaging_rec_dup(TALLOC_CTX *mem_ctx,
107                                                struct messaging_rec *rec);
108 static bool messaging_dispatch_classic(struct messaging_context *msg_ctx,
109                                        struct messaging_rec *rec);
110 static bool messaging_dispatch_waiters(struct messaging_context *msg_ctx,
111                                        struct tevent_context *ev,
112                                        struct messaging_rec *rec);
113 static void messaging_dispatch_rec(struct messaging_context *msg_ctx,
114                                    struct tevent_context *ev,
115                                    struct messaging_rec *rec);
116
117 /****************************************************************************
118  A useful function for testing the message system.
119 ****************************************************************************/
120
121 static void ping_message(struct messaging_context *msg_ctx,
122                          void *private_data,
123                          uint32_t msg_type,
124                          struct server_id src,
125                          DATA_BLOB *data)
126 {
127         struct server_id_buf idbuf;
128
129         DEBUG(1, ("INFO: Received PING message from PID %s [%.*s]\n",
130                   server_id_str_buf(src, &idbuf), (int)data->length,
131                   data->data ? (char *)data->data : ""));
132
133         messaging_send(msg_ctx, src, MSG_PONG, data);
134 }
135
136 struct messaging_rec *messaging_rec_create(
137         TALLOC_CTX *mem_ctx, struct server_id src, struct server_id dst,
138         uint32_t msg_type, const struct iovec *iov, int iovlen,
139         const int *fds, size_t num_fds)
140 {
141         ssize_t buflen;
142         uint8_t *buf;
143         struct messaging_rec *result;
144
145         if (num_fds > INT8_MAX) {
146                 return NULL;
147         }
148
149         buflen = iov_buflen(iov, iovlen);
150         if (buflen == -1) {
151                 return NULL;
152         }
153         buf = talloc_array(mem_ctx, uint8_t, buflen);
154         if (buf == NULL) {
155                 return NULL;
156         }
157         iov_buf(iov, iovlen, buf, buflen);
158
159         {
160                 struct messaging_rec rec;
161                 int64_t fds64[num_fds];
162                 size_t i;
163
164                 for (i=0; i<num_fds; i++) {
165                         fds64[i] = fds[i];
166                 }
167
168                 rec = (struct messaging_rec) {
169                         .msg_version = MESSAGE_VERSION, .msg_type = msg_type,
170                         .src = src, .dest = dst,
171                         .buf.data = buf, .buf.length = buflen,
172                         .num_fds = num_fds, .fds = fds64,
173                 };
174
175                 result = messaging_rec_dup(mem_ctx, &rec);
176         }
177
178         TALLOC_FREE(buf);
179
180         return result;
181 }
182
183 static bool messaging_register_event_context(struct messaging_context *ctx,
184                                              struct tevent_context *ev)
185 {
186         size_t i, num_event_contexts;
187         struct messaging_registered_ev *free_reg = NULL;
188         struct messaging_registered_ev *tmp;
189
190         num_event_contexts = talloc_array_length(ctx->event_contexts);
191
192         for (i=0; i<num_event_contexts; i++) {
193                 struct messaging_registered_ev *reg = &ctx->event_contexts[i];
194
195                 if (reg->ev == ev) {
196                         reg->refcount += 1;
197                         return true;
198                 }
199                 if (reg->refcount == 0) {
200                         if (reg->ev != NULL) {
201                                 abort();
202                         }
203                         free_reg = reg;
204                 }
205         }
206
207         if (free_reg == NULL) {
208                 tmp = talloc_realloc(ctx, ctx->event_contexts,
209                                      struct messaging_registered_ev,
210                                      num_event_contexts+1);
211                 if (tmp == NULL) {
212                         return false;
213                 }
214                 ctx->event_contexts = tmp;
215
216                 free_reg = &ctx->event_contexts[num_event_contexts];
217         }
218
219         *free_reg = (struct messaging_registered_ev) { .ev = ev, .refcount = 1 };
220
221         return true;
222 }
223
224 static bool messaging_deregister_event_context(struct messaging_context *ctx,
225                                                struct tevent_context *ev)
226 {
227         size_t i, num_event_contexts;
228
229         num_event_contexts = talloc_array_length(ctx->event_contexts);
230
231         for (i=0; i<num_event_contexts; i++) {
232                 struct messaging_registered_ev *reg = &ctx->event_contexts[i];
233
234                 if (reg->ev == ev) {
235                         if (reg->refcount == 0) {
236                                 return false;
237                         }
238                         reg->refcount -= 1;
239
240                         if (reg->refcount == 0) {
241                                 /*
242                                  * Not strictly necessary, just
243                                  * paranoia
244                                  */
245                                 reg->ev = NULL;
246
247                                 /*
248                                  * Do not talloc_free(reg->im),
249                                  * recycle immediates events.
250                                  */
251                         }
252                         return true;
253                 }
254         }
255         return false;
256 }
257
258 static void messaging_post_main_event_context(struct tevent_context *ev,
259                                               struct tevent_immediate *im,
260                                               void *private_data)
261 {
262         struct messaging_context *ctx = talloc_get_type_abort(
263                 private_data, struct messaging_context);
264
265         while (ctx->posted_msgs != NULL) {
266                 struct messaging_rec *rec = ctx->posted_msgs;
267                 bool consumed;
268
269                 DLIST_REMOVE(ctx->posted_msgs, rec);
270
271                 consumed = messaging_dispatch_classic(ctx, rec);
272                 if (!consumed) {
273                         consumed = messaging_dispatch_waiters(
274                                 ctx, ctx->event_ctx, rec);
275                 }
276
277                 if (!consumed) {
278                         uint8_t i;
279
280                         for (i=0; i<rec->num_fds; i++) {
281                                 close(rec->fds[i]);
282                         }
283                 }
284
285                 TALLOC_FREE(rec);
286         }
287 }
288
289 static void messaging_post_sub_event_context(struct tevent_context *ev,
290                                              struct tevent_immediate *im,
291                                              void *private_data)
292 {
293         struct messaging_context *ctx = talloc_get_type_abort(
294                 private_data, struct messaging_context);
295         struct messaging_rec *rec, *next;
296
297         for (rec = ctx->posted_msgs; rec != NULL; rec = next) {
298                 bool consumed;
299
300                 next = rec->next;
301
302                 consumed = messaging_dispatch_waiters(ctx, ev, rec);
303                 if (consumed) {
304                         DLIST_REMOVE(ctx->posted_msgs, rec);
305                         TALLOC_FREE(rec);
306                 }
307         }
308 }
309
310 static bool messaging_alert_event_contexts(struct messaging_context *ctx)
311 {
312         size_t i, num_event_contexts;
313
314         num_event_contexts = talloc_array_length(ctx->event_contexts);
315
316         for (i=0; i<num_event_contexts; i++) {
317                 struct messaging_registered_ev *reg = &ctx->event_contexts[i];
318
319                 if (reg->refcount == 0) {
320                         continue;
321                 }
322
323                 if (reg->im == NULL) {
324                         reg->im = tevent_create_immediate(
325                                 ctx->event_contexts);
326                 }
327                 if (reg->im == NULL) {
328                         DBG_WARNING("Could not create immediate\n");
329                         continue;
330                 }
331
332                 /*
333                  * We depend on schedule_immediate to work
334                  * multiple times. Might be a bit inefficient,
335                  * but this needs to be proven in tests. The
336                  * alternatively would be to track whether the
337                  * immediate has already been scheduled. For
338                  * now, avoid that complexity here.
339                  */
340
341                 if (reg->ev == ctx->event_ctx) {
342                         tevent_schedule_immediate(
343                                 reg->im, reg->ev,
344                                 messaging_post_main_event_context,
345                                 ctx);
346                 } else {
347                         tevent_schedule_immediate(
348                                 reg->im, reg->ev,
349                                 messaging_post_sub_event_context,
350                                 ctx);
351                 }
352
353         }
354         return true;
355 }
356
357 static void messaging_recv_cb(struct tevent_context *ev,
358                               const uint8_t *msg, size_t msg_len,
359                               int *fds, size_t num_fds,
360                               void *private_data)
361 {
362         struct messaging_context *msg_ctx = talloc_get_type_abort(
363                 private_data, struct messaging_context);
364         struct server_id_buf idbuf;
365         struct messaging_rec rec;
366         int64_t fds64[MIN(num_fds, INT8_MAX)];
367         size_t i;
368
369         if (msg_len < MESSAGE_HDR_LENGTH) {
370                 DBG_WARNING("message too short: %zu\n", msg_len);
371                 goto close_fail;
372         }
373
374         if (num_fds > INT8_MAX) {
375                 DBG_WARNING("too many fds: %zu\n", num_fds);
376                 goto close_fail;
377         }
378
379         /*
380          * "consume" the fds by copying them and setting
381          * the original variable to -1
382          */
383         for (i=0; i < num_fds; i++) {
384                 fds64[i] = fds[i];
385                 fds[i] = -1;
386         }
387
388         rec = (struct messaging_rec) {
389                 .msg_version = MESSAGE_VERSION,
390                 .buf.data = discard_const_p(uint8_t, msg) + MESSAGE_HDR_LENGTH,
391                 .buf.length = msg_len - MESSAGE_HDR_LENGTH,
392                 .num_fds = num_fds,
393                 .fds = fds64,
394         };
395
396         message_hdr_get(&rec.msg_type, &rec.src, &rec.dest, msg);
397
398         DBG_DEBUG("Received message 0x%x len %zu (num_fds:%zu) from %s\n",
399                   (unsigned)rec.msg_type, rec.buf.length, num_fds,
400                   server_id_str_buf(rec.src, &idbuf));
401
402         messaging_dispatch_rec(msg_ctx, ev, &rec);
403         return;
404
405 close_fail:
406         for (i=0; i < num_fds; i++) {
407                 close(fds[i]);
408         }
409 }
410
411 static int messaging_context_destructor(struct messaging_context *ctx)
412 {
413         size_t i;
414
415         for (i=0; i<ctx->num_new_waiters; i++) {
416                 if (ctx->new_waiters[i] != NULL) {
417                         tevent_req_set_cleanup_fn(ctx->new_waiters[i], NULL);
418                         ctx->new_waiters[i] = NULL;
419                 }
420         }
421         for (i=0; i<ctx->num_waiters; i++) {
422                 if (ctx->waiters[i] != NULL) {
423                         tevent_req_set_cleanup_fn(ctx->waiters[i], NULL);
424                         ctx->waiters[i] = NULL;
425                 }
426         }
427
428         /*
429          * The immediates from messaging_alert_event_contexts
430          * reference "ctx". Don't let them outlive the
431          * messaging_context we're destroying here.
432          */
433         TALLOC_FREE(ctx->event_contexts);
434
435         return 0;
436 }
437
438 static const char *private_path(const char *name)
439 {
440         return talloc_asprintf(talloc_tos(), "%s/%s", lp_private_dir(), name);
441 }
442
443 static NTSTATUS messaging_init_internal(TALLOC_CTX *mem_ctx,
444                                         struct tevent_context *ev,
445                                         struct messaging_context **pmsg_ctx)
446 {
447         TALLOC_CTX *frame;
448         struct messaging_context *ctx;
449         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
450         int ret;
451         const char *lck_path;
452         const char *priv_path;
453         bool ok;
454
455         lck_path = lock_path("msg.lock");
456         if (lck_path == NULL) {
457                 return NT_STATUS_NO_MEMORY;
458         }
459
460         ok = directory_create_or_exist_strict(lck_path,
461                                               sec_initial_uid(),
462                                               0755);
463         if (!ok) {
464                 DBG_DEBUG("Could not create lock directory: %s\n",
465                           strerror(errno));
466                 return NT_STATUS_ACCESS_DENIED;
467         }
468
469         priv_path = private_path("msg.sock");
470         if (priv_path == NULL) {
471                 return NT_STATUS_NO_MEMORY;
472         }
473
474         ok = directory_create_or_exist_strict(priv_path, sec_initial_uid(),
475                                               0700);
476         if (!ok) {
477                 DBG_DEBUG("Could not create msg directory: %s\n",
478                           strerror(errno));
479                 return NT_STATUS_ACCESS_DENIED;
480         }
481
482         frame = talloc_stackframe();
483         if (frame == NULL) {
484                 return NT_STATUS_NO_MEMORY;
485         }
486
487         ctx = talloc_zero(frame, struct messaging_context);
488         if (ctx == NULL) {
489                 status = NT_STATUS_NO_MEMORY;
490                 goto done;
491         }
492
493         ctx->id = (struct server_id) {
494                 .pid = getpid(), .vnn = NONCLUSTER_VNN
495         };
496
497         ctx->event_ctx = ev;
498
499         ok = messaging_register_event_context(ctx, ev);
500         if (!ok) {
501                 status = NT_STATUS_NO_MEMORY;
502                 goto done;
503         }
504
505         sec_init();
506
507         ctx->msg_dgm_ref = messaging_dgm_ref(ctx,
508                                              ctx->event_ctx,
509                                              &ctx->id.unique_id,
510                                              priv_path,
511                                              lck_path,
512                                              messaging_recv_cb,
513                                              ctx,
514                                              &ret);
515         if (ctx->msg_dgm_ref == NULL) {
516                 DEBUG(2, ("messaging_dgm_ref failed: %s\n", strerror(ret)));
517                 status = map_nt_error_from_unix(ret);
518                 goto done;
519         }
520         talloc_set_destructor(ctx, messaging_context_destructor);
521
522 #ifdef CLUSTER_SUPPORT
523         if (lp_clustering()) {
524                 ctx->msg_ctdb_ref = messaging_ctdb_ref(
525                         ctx, ctx->event_ctx,
526                         lp_ctdbd_socket(), lp_ctdb_timeout(),
527                         ctx->id.unique_id, messaging_recv_cb, ctx, &ret);
528                 if (ctx->msg_ctdb_ref == NULL) {
529                         DBG_NOTICE("messaging_ctdb_ref failed: %s\n",
530                                    strerror(ret));
531                         status = map_nt_error_from_unix(ret);
532                         goto done;
533                 }
534         }
535 #endif
536
537         ctx->id.vnn = get_my_vnn();
538
539         ctx->names_db = server_id_db_init(ctx,
540                                           ctx->id,
541                                           lp_lock_directory(),
542                                           0,
543                                           TDB_INCOMPATIBLE_HASH|TDB_CLEAR_IF_FIRST);
544         if (ctx->names_db == NULL) {
545                 DBG_DEBUG("server_id_db_init failed\n");
546                 status = NT_STATUS_NO_MEMORY;
547                 goto done;
548         }
549
550         messaging_register(ctx, NULL, MSG_PING, ping_message);
551
552         /* Register some debugging related messages */
553
554         register_msg_pool_usage(ctx);
555         register_dmalloc_msgs(ctx);
556         debug_register_msgs(ctx);
557
558         {
559                 struct server_id_buf tmp;
560                 DBG_DEBUG("my id: %s\n", server_id_str_buf(ctx->id, &tmp));
561         }
562
563         *pmsg_ctx = talloc_steal(mem_ctx, ctx);
564
565         status = NT_STATUS_OK;
566 done:
567         TALLOC_FREE(frame);
568
569         return status;
570 }
571
572 struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
573                                          struct tevent_context *ev)
574 {
575         struct messaging_context *ctx = NULL;
576         NTSTATUS status;
577
578         status = messaging_init_internal(mem_ctx,
579                                          ev,
580                                          &ctx);
581         if (!NT_STATUS_IS_OK(status)) {
582                 return NULL;
583         }
584
585         return ctx;
586 }
587
588 NTSTATUS messaging_init_client(TALLOC_CTX *mem_ctx,
589                                struct tevent_context *ev,
590                                struct messaging_context **pmsg_ctx)
591 {
592         return messaging_init_internal(mem_ctx,
593                                         ev,
594                                         pmsg_ctx);
595 }
596
597 struct server_id messaging_server_id(const struct messaging_context *msg_ctx)
598 {
599         return msg_ctx->id;
600 }
601
602 /*
603  * re-init after a fork
604  */
605 NTSTATUS messaging_reinit(struct messaging_context *msg_ctx)
606 {
607         int ret;
608         char *lck_path;
609
610         TALLOC_FREE(msg_ctx->msg_dgm_ref);
611         TALLOC_FREE(msg_ctx->msg_ctdb_ref);
612
613         msg_ctx->id = (struct server_id) {
614                 .pid = getpid(), .vnn = msg_ctx->id.vnn
615         };
616
617         lck_path = lock_path("msg.lock");
618         if (lck_path == NULL) {
619                 return NT_STATUS_NO_MEMORY;
620         }
621
622         msg_ctx->msg_dgm_ref = messaging_dgm_ref(
623                 msg_ctx, msg_ctx->event_ctx, &msg_ctx->id.unique_id,
624                 private_path("msg.sock"), lck_path,
625                 messaging_recv_cb, msg_ctx, &ret);
626
627         if (msg_ctx->msg_dgm_ref == NULL) {
628                 DEBUG(2, ("messaging_dgm_ref failed: %s\n", strerror(ret)));
629                 return map_nt_error_from_unix(ret);
630         }
631
632         if (lp_clustering()) {
633                 msg_ctx->msg_ctdb_ref = messaging_ctdb_ref(
634                         msg_ctx, msg_ctx->event_ctx,
635                         lp_ctdbd_socket(), lp_ctdb_timeout(),
636                         msg_ctx->id.unique_id, messaging_recv_cb, msg_ctx,
637                         &ret);
638                 if (msg_ctx->msg_ctdb_ref == NULL) {
639                         DBG_NOTICE("messaging_ctdb_ref failed: %s\n",
640                                    strerror(ret));
641                         return map_nt_error_from_unix(ret);
642                 }
643         }
644
645         server_id_db_reinit(msg_ctx->names_db, msg_ctx->id);
646
647         return NT_STATUS_OK;
648 }
649
650
651 /*
652  * Register a dispatch function for a particular message type. Allow multiple
653  * registrants
654 */
655 NTSTATUS messaging_register(struct messaging_context *msg_ctx,
656                             void *private_data,
657                             uint32_t msg_type,
658                             void (*fn)(struct messaging_context *msg,
659                                        void *private_data, 
660                                        uint32_t msg_type, 
661                                        struct server_id server_id,
662                                        DATA_BLOB *data))
663 {
664         struct messaging_callback *cb;
665
666         DEBUG(5, ("Registering messaging pointer for type %u - "
667                   "private_data=%p\n",
668                   (unsigned)msg_type, private_data));
669
670         /*
671          * Only one callback per type
672          */
673
674         for (cb = msg_ctx->callbacks; cb != NULL; cb = cb->next) {
675                 /* we allow a second registration of the same message
676                    type if it has a different private pointer. This is
677                    needed in, for example, the internal notify code,
678                    which creates a new notify context for each tree
679                    connect, and expects to receive messages to each of
680                    them. */
681                 if (cb->msg_type == msg_type && private_data == cb->private_data) {
682                         DEBUG(5,("Overriding messaging pointer for type %u - private_data=%p\n",
683                                   (unsigned)msg_type, private_data));
684                         cb->fn = fn;
685                         cb->private_data = private_data;
686                         return NT_STATUS_OK;
687                 }
688         }
689
690         if (!(cb = talloc(msg_ctx, struct messaging_callback))) {
691                 return NT_STATUS_NO_MEMORY;
692         }
693
694         cb->msg_type = msg_type;
695         cb->fn = fn;
696         cb->private_data = private_data;
697
698         DLIST_ADD(msg_ctx->callbacks, cb);
699         return NT_STATUS_OK;
700 }
701
702 /*
703   De-register the function for a particular message type.
704 */
705 void messaging_deregister(struct messaging_context *ctx, uint32_t msg_type,
706                           void *private_data)
707 {
708         struct messaging_callback *cb, *next;
709
710         for (cb = ctx->callbacks; cb; cb = next) {
711                 next = cb->next;
712                 if ((cb->msg_type == msg_type)
713                     && (cb->private_data == private_data)) {
714                         DEBUG(5,("Deregistering messaging pointer for type %u - private_data=%p\n",
715                                   (unsigned)msg_type, private_data));
716                         DLIST_REMOVE(ctx->callbacks, cb);
717                         TALLOC_FREE(cb);
718                 }
719         }
720 }
721
722 /*
723   Send a message to a particular server
724 */
725 NTSTATUS messaging_send(struct messaging_context *msg_ctx,
726                         struct server_id server, uint32_t msg_type,
727                         const DATA_BLOB *data)
728 {
729         struct iovec iov = {0};
730
731         if (data != NULL) {
732                 iov.iov_base = data->data;
733                 iov.iov_len = data->length;
734         };
735
736         return messaging_send_iov(msg_ctx, server, msg_type, &iov, 1, NULL, 0);
737 }
738
739 NTSTATUS messaging_send_buf(struct messaging_context *msg_ctx,
740                             struct server_id server, uint32_t msg_type,
741                             const uint8_t *buf, size_t len)
742 {
743         DATA_BLOB blob = data_blob_const(buf, len);
744         return messaging_send(msg_ctx, server, msg_type, &blob);
745 }
746
747 static int messaging_post_self(struct messaging_context *msg_ctx,
748                                struct server_id src, struct server_id dst,
749                                uint32_t msg_type,
750                                const struct iovec *iov, int iovlen,
751                                const int *fds, size_t num_fds)
752 {
753         struct messaging_rec *rec;
754         bool ok;
755
756         rec = messaging_rec_create(
757                 msg_ctx, src, dst, msg_type, iov, iovlen, fds, num_fds);
758         if (rec == NULL) {
759                 return ENOMEM;
760         }
761
762         ok = messaging_alert_event_contexts(msg_ctx);
763         if (!ok) {
764                 TALLOC_FREE(rec);
765                 return ENOMEM;
766         }
767
768         DLIST_ADD_END(msg_ctx->posted_msgs, rec);
769
770         return 0;
771 }
772
773 int messaging_send_iov_from(struct messaging_context *msg_ctx,
774                             struct server_id src, struct server_id dst,
775                             uint32_t msg_type,
776                             const struct iovec *iov, int iovlen,
777                             const int *fds, size_t num_fds)
778 {
779         int ret;
780         uint8_t hdr[MESSAGE_HDR_LENGTH];
781         struct iovec iov2[iovlen+1];
782
783         if (server_id_is_disconnected(&dst)) {
784                 return EINVAL;
785         }
786
787         if (num_fds > INT8_MAX) {
788                 return EINVAL;
789         }
790
791         if (server_id_equal(&dst, &msg_ctx->id)) {
792                 ret = messaging_post_self(msg_ctx, src, dst, msg_type,
793                                           iov, iovlen, fds, num_fds);
794                 return ret;
795         }
796
797         message_hdr_put(hdr, msg_type, src, dst);
798         iov2[0] = (struct iovec){ .iov_base = hdr, .iov_len = sizeof(hdr) };
799         memcpy(&iov2[1], iov, iovlen * sizeof(*iov));
800
801         if (dst.vnn != msg_ctx->id.vnn) {
802                 if (num_fds > 0) {
803                         return ENOSYS;
804                 }
805
806                 ret = messaging_ctdb_send(dst.vnn, dst.pid, iov2, iovlen+1);
807                 return ret;
808         }
809
810         ret = messaging_dgm_send(dst.pid, iov2, iovlen+1, fds, num_fds);
811
812         if (ret == EACCES) {
813                 become_root();
814                 ret = messaging_dgm_send(dst.pid, iov2, iovlen+1,
815                                          fds, num_fds);
816                 unbecome_root();
817         }
818
819         if (ret == ECONNREFUSED) {
820                 /*
821                  * Linux returns this when a socket exists in the file
822                  * system without a listening process. This is not
823                  * documented in susv4 or the linux manpages, but it's
824                  * easily testable. For the higher levels this is the
825                  * same as "destination does not exist"
826                  */
827                 ret = ENOENT;
828         }
829
830         return ret;
831 }
832
833 NTSTATUS messaging_send_iov(struct messaging_context *msg_ctx,
834                             struct server_id dst, uint32_t msg_type,
835                             const struct iovec *iov, int iovlen,
836                             const int *fds, size_t num_fds)
837 {
838         int ret;
839
840         ret = messaging_send_iov_from(msg_ctx, msg_ctx->id, dst, msg_type,
841                                       iov, iovlen, fds, num_fds);
842         if (ret != 0) {
843                 return map_nt_error_from_unix(ret);
844         }
845         return NT_STATUS_OK;
846 }
847
848 struct send_all_state {
849         struct messaging_context *msg_ctx;
850         int msg_type;
851         const void *buf;
852         size_t len;
853 };
854
855 static int send_all_fn(pid_t pid, void *private_data)
856 {
857         struct send_all_state *state = private_data;
858         NTSTATUS status;
859
860         status = messaging_send_buf(state->msg_ctx, pid_to_procid(pid),
861                                     state->msg_type, state->buf, state->len);
862         if (!NT_STATUS_IS_OK(status)) {
863                 DBG_WARNING("messaging_send_buf to %ju failed: %s\n",
864                             (uintmax_t)pid, nt_errstr(status));
865         }
866
867         return 0;
868 }
869
870 void messaging_send_all(struct messaging_context *msg_ctx,
871                         int msg_type, const void *buf, size_t len)
872 {
873         struct send_all_state state = {
874                 .msg_ctx = msg_ctx, .msg_type = msg_type,
875                 .buf = buf, .len = len
876         };
877         int ret;
878
879 #ifdef CLUSTER_SUPPORT
880         if (lp_clustering()) {
881                 struct ctdbd_connection *conn = messaging_ctdb_connection();
882                 uint8_t msghdr[MESSAGE_HDR_LENGTH];
883                 struct iovec iov[] = {
884                         { .iov_base = msghdr,
885                           .iov_len = sizeof(msghdr) },
886                         { .iov_base = discard_const_p(void, buf),
887                           .iov_len = len }
888                 };
889
890                 message_hdr_put(msghdr, msg_type, messaging_server_id(msg_ctx),
891                                 (struct server_id) {0});
892
893                 ret = ctdbd_messaging_send_iov(
894                         conn, CTDB_BROADCAST_CONNECTED,
895                         CTDB_SRVID_SAMBA_PROCESS,
896                         iov, ARRAY_SIZE(iov));
897                 if (ret != 0) {
898                         DBG_WARNING("ctdbd_messaging_send_iov failed: %s\n",
899                                     strerror(ret));
900                 }
901
902                 return;
903         }
904 #endif
905
906         ret = messaging_dgm_forall(send_all_fn, &state);
907         if (ret != 0) {
908                 DBG_WARNING("messaging_dgm_forall failed: %s\n",
909                             strerror(ret));
910         }
911 }
912
913 static struct messaging_rec *messaging_rec_dup(TALLOC_CTX *mem_ctx,
914                                                struct messaging_rec *rec)
915 {
916         struct messaging_rec *result;
917         size_t fds_size = sizeof(int64_t) * rec->num_fds;
918         size_t payload_len;
919
920         payload_len = rec->buf.length + fds_size;
921         if (payload_len < rec->buf.length) {
922                 /* overflow */
923                 return NULL;
924         }
925
926         result = talloc_pooled_object(mem_ctx, struct messaging_rec, 2,
927                                       payload_len);
928         if (result == NULL) {
929                 return NULL;
930         }
931         *result = *rec;
932
933         /* Doesn't fail, see talloc_pooled_object */
934
935         result->buf.data = talloc_memdup(result, rec->buf.data,
936                                          rec->buf.length);
937
938         result->fds = NULL;
939         if (result->num_fds > 0) {
940                 result->fds = talloc_memdup(result, rec->fds, fds_size);
941         }
942
943         return result;
944 }
945
946 struct messaging_filtered_read_state {
947         struct tevent_context *ev;
948         struct messaging_context *msg_ctx;
949         struct messaging_dgm_fde *fde;
950         struct messaging_ctdb_fde *cluster_fde;
951
952         bool (*filter)(struct messaging_rec *rec, void *private_data);
953         void *private_data;
954
955         struct messaging_rec *rec;
956 };
957
958 static void messaging_filtered_read_cleanup(struct tevent_req *req,
959                                             enum tevent_req_state req_state);
960
961 struct tevent_req *messaging_filtered_read_send(
962         TALLOC_CTX *mem_ctx, struct tevent_context *ev,
963         struct messaging_context *msg_ctx,
964         bool (*filter)(struct messaging_rec *rec, void *private_data),
965         void *private_data)
966 {
967         struct tevent_req *req;
968         struct messaging_filtered_read_state *state;
969         size_t new_waiters_len;
970         bool ok;
971
972         req = tevent_req_create(mem_ctx, &state,
973                                 struct messaging_filtered_read_state);
974         if (req == NULL) {
975                 return NULL;
976         }
977         state->ev = ev;
978         state->msg_ctx = msg_ctx;
979         state->filter = filter;
980         state->private_data = private_data;
981
982         /*
983          * We have to defer the callback here, as we might be called from
984          * within a different tevent_context than state->ev
985          */
986         tevent_req_defer_callback(req, state->ev);
987
988         state->fde = messaging_dgm_register_tevent_context(state, ev);
989         if (tevent_req_nomem(state->fde, req)) {
990                 return tevent_req_post(req, ev);
991         }
992
993         if (lp_clustering()) {
994                 state->cluster_fde =
995                         messaging_ctdb_register_tevent_context(state, ev);
996                 if (tevent_req_nomem(state->cluster_fde, req)) {
997                         return tevent_req_post(req, ev);
998                 }
999         }
1000
1001         /*
1002          * We add ourselves to the "new_waiters" array, not the "waiters"
1003          * array. If we are called from within messaging_read_done,
1004          * messaging_dispatch_rec will be in an active for-loop on
1005          * "waiters". We must be careful not to mess with this array, because
1006          * it could mean that a single event is being delivered twice.
1007          */
1008
1009         new_waiters_len = talloc_array_length(msg_ctx->new_waiters);
1010
1011         if (new_waiters_len == msg_ctx->num_new_waiters) {
1012                 struct tevent_req **tmp;
1013
1014                 tmp = talloc_realloc(msg_ctx, msg_ctx->new_waiters,
1015                                      struct tevent_req *, new_waiters_len+1);
1016                 if (tevent_req_nomem(tmp, req)) {
1017                         return tevent_req_post(req, ev);
1018                 }
1019                 msg_ctx->new_waiters = tmp;
1020         }
1021
1022         msg_ctx->new_waiters[msg_ctx->num_new_waiters] = req;
1023         msg_ctx->num_new_waiters += 1;
1024         tevent_req_set_cleanup_fn(req, messaging_filtered_read_cleanup);
1025
1026         ok = messaging_register_event_context(msg_ctx, ev);
1027         if (!ok) {
1028                 tevent_req_oom(req);
1029                 return tevent_req_post(req, ev);
1030         }
1031
1032         return req;
1033 }
1034
1035 static void messaging_filtered_read_cleanup(struct tevent_req *req,
1036                                             enum tevent_req_state req_state)
1037 {
1038         struct messaging_filtered_read_state *state = tevent_req_data(
1039                 req, struct messaging_filtered_read_state);
1040         struct messaging_context *msg_ctx = state->msg_ctx;
1041         size_t i;
1042         bool ok;
1043
1044         tevent_req_set_cleanup_fn(req, NULL);
1045
1046         TALLOC_FREE(state->fde);
1047         TALLOC_FREE(state->cluster_fde);
1048
1049         ok = messaging_deregister_event_context(msg_ctx, state->ev);
1050         if (!ok) {
1051                 abort();
1052         }
1053
1054         /*
1055          * Just set the [new_]waiters entry to NULL, be careful not to mess
1056          * with the other "waiters" array contents. We are often called from
1057          * within "messaging_dispatch_rec", which loops over
1058          * "waiters". Messing with the "waiters" array will mess up that
1059          * for-loop.
1060          */
1061
1062         for (i=0; i<msg_ctx->num_waiters; i++) {
1063                 if (msg_ctx->waiters[i] == req) {
1064                         msg_ctx->waiters[i] = NULL;
1065                         return;
1066                 }
1067         }
1068
1069         for (i=0; i<msg_ctx->num_new_waiters; i++) {
1070                 if (msg_ctx->new_waiters[i] == req) {
1071                         msg_ctx->new_waiters[i] = NULL;
1072                         return;
1073                 }
1074         }
1075 }
1076
1077 static void messaging_filtered_read_done(struct tevent_req *req,
1078                                          struct messaging_rec *rec)
1079 {
1080         struct messaging_filtered_read_state *state = tevent_req_data(
1081                 req, struct messaging_filtered_read_state);
1082
1083         state->rec = messaging_rec_dup(state, rec);
1084         if (tevent_req_nomem(state->rec, req)) {
1085                 return;
1086         }
1087         tevent_req_done(req);
1088 }
1089
1090 int messaging_filtered_read_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1091                                  struct messaging_rec **presult)
1092 {
1093         struct messaging_filtered_read_state *state = tevent_req_data(
1094                 req, struct messaging_filtered_read_state);
1095         int err;
1096
1097         if (tevent_req_is_unix_error(req, &err)) {
1098                 tevent_req_received(req);
1099                 return err;
1100         }
1101         if (presult != NULL) {
1102                 *presult = talloc_move(mem_ctx, &state->rec);
1103         }
1104         return 0;
1105 }
1106
1107 struct messaging_read_state {
1108         uint32_t msg_type;
1109         struct messaging_rec *rec;
1110 };
1111
1112 static bool messaging_read_filter(struct messaging_rec *rec,
1113                                   void *private_data);
1114 static void messaging_read_done(struct tevent_req *subreq);
1115
1116 struct tevent_req *messaging_read_send(TALLOC_CTX *mem_ctx,
1117                                        struct tevent_context *ev,
1118                                        struct messaging_context *msg,
1119                                        uint32_t msg_type)
1120 {
1121         struct tevent_req *req, *subreq;
1122         struct messaging_read_state *state;
1123
1124         req = tevent_req_create(mem_ctx, &state,
1125                                 struct messaging_read_state);
1126         if (req == NULL) {
1127                 return NULL;
1128         }
1129         state->msg_type = msg_type;
1130
1131         subreq = messaging_filtered_read_send(state, ev, msg,
1132                                               messaging_read_filter, state);
1133         if (tevent_req_nomem(subreq, req)) {
1134                 return tevent_req_post(req, ev);
1135         }
1136         tevent_req_set_callback(subreq, messaging_read_done, req);
1137         return req;
1138 }
1139
1140 static bool messaging_read_filter(struct messaging_rec *rec,
1141                                   void *private_data)
1142 {
1143         struct messaging_read_state *state = talloc_get_type_abort(
1144                 private_data, struct messaging_read_state);
1145
1146         if (rec->num_fds != 0) {
1147                 return false;
1148         }
1149
1150         return rec->msg_type == state->msg_type;
1151 }
1152
1153 static void messaging_read_done(struct tevent_req *subreq)
1154 {
1155         struct tevent_req *req = tevent_req_callback_data(
1156                 subreq, struct tevent_req);
1157         struct messaging_read_state *state = tevent_req_data(
1158                 req, struct messaging_read_state);
1159         int ret;
1160
1161         ret = messaging_filtered_read_recv(subreq, state, &state->rec);
1162         TALLOC_FREE(subreq);
1163         if (tevent_req_error(req, ret)) {
1164                 return;
1165         }
1166         tevent_req_done(req);
1167 }
1168
1169 int messaging_read_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1170                         struct messaging_rec **presult)
1171 {
1172         struct messaging_read_state *state = tevent_req_data(
1173                 req, struct messaging_read_state);
1174         int err;
1175
1176         if (tevent_req_is_unix_error(req, &err)) {
1177                 return err;
1178         }
1179         if (presult != NULL) {
1180                 *presult = talloc_move(mem_ctx, &state->rec);
1181         }
1182         return 0;
1183 }
1184
1185 static bool messaging_append_new_waiters(struct messaging_context *msg_ctx)
1186 {
1187         if (msg_ctx->num_new_waiters == 0) {
1188                 return true;
1189         }
1190
1191         if (talloc_array_length(msg_ctx->waiters) <
1192             (msg_ctx->num_waiters + msg_ctx->num_new_waiters)) {
1193                 struct tevent_req **tmp;
1194                 tmp = talloc_realloc(
1195                         msg_ctx, msg_ctx->waiters, struct tevent_req *,
1196                         msg_ctx->num_waiters + msg_ctx->num_new_waiters);
1197                 if (tmp == NULL) {
1198                         DEBUG(1, ("%s: talloc failed\n", __func__));
1199                         return false;
1200                 }
1201                 msg_ctx->waiters = tmp;
1202         }
1203
1204         memcpy(&msg_ctx->waiters[msg_ctx->num_waiters], msg_ctx->new_waiters,
1205                sizeof(struct tevent_req *) * msg_ctx->num_new_waiters);
1206
1207         msg_ctx->num_waiters += msg_ctx->num_new_waiters;
1208         msg_ctx->num_new_waiters = 0;
1209
1210         return true;
1211 }
1212
1213 static bool messaging_dispatch_classic(struct messaging_context *msg_ctx,
1214                                        struct messaging_rec *rec)
1215 {
1216         struct messaging_callback *cb, *next;
1217
1218         for (cb = msg_ctx->callbacks; cb != NULL; cb = next) {
1219                 size_t j;
1220
1221                 next = cb->next;
1222                 if (cb->msg_type != rec->msg_type) {
1223                         continue;
1224                 }
1225
1226                 /*
1227                  * the old style callbacks don't support fd passing
1228                  */
1229                 for (j=0; j < rec->num_fds; j++) {
1230                         int fd = rec->fds[j];
1231                         close(fd);
1232                 }
1233                 rec->num_fds = 0;
1234                 rec->fds = NULL;
1235
1236                 cb->fn(msg_ctx, cb->private_data, rec->msg_type,
1237                        rec->src, &rec->buf);
1238
1239                 return true;
1240         }
1241
1242         return false;
1243 }
1244
1245 static bool messaging_dispatch_waiters(struct messaging_context *msg_ctx,
1246                                        struct tevent_context *ev,
1247                                        struct messaging_rec *rec)
1248 {
1249         size_t i;
1250
1251         if (!messaging_append_new_waiters(msg_ctx)) {
1252                 return false;
1253         }
1254
1255         i = 0;
1256         while (i < msg_ctx->num_waiters) {
1257                 struct tevent_req *req;
1258                 struct messaging_filtered_read_state *state;
1259
1260                 req = msg_ctx->waiters[i];
1261                 if (req == NULL) {
1262                         /*
1263                          * This got cleaned up. In the meantime,
1264                          * move everything down one. We need
1265                          * to keep the order of waiters, as
1266                          * other code may depend on this.
1267                          */
1268                         if (i < msg_ctx->num_waiters - 1) {
1269                                 memmove(&msg_ctx->waiters[i],
1270                                         &msg_ctx->waiters[i+1],
1271                                         sizeof(struct tevent_req *) *
1272                                             (msg_ctx->num_waiters - i - 1));
1273                         }
1274                         msg_ctx->num_waiters -= 1;
1275                         continue;
1276                 }
1277
1278                 state = tevent_req_data(
1279                         req, struct messaging_filtered_read_state);
1280                 if ((ev == state->ev) &&
1281                     state->filter(rec, state->private_data)) {
1282                         messaging_filtered_read_done(req, rec);
1283                         return true;
1284                 }
1285
1286                 i += 1;
1287         }
1288
1289         return false;
1290 }
1291
1292 /*
1293   Dispatch one messaging_rec
1294 */
1295 static void messaging_dispatch_rec(struct messaging_context *msg_ctx,
1296                                    struct tevent_context *ev,
1297                                    struct messaging_rec *rec)
1298 {
1299         bool consumed;
1300         size_t i;
1301
1302         if (ev == msg_ctx->event_ctx) {
1303                 consumed = messaging_dispatch_classic(msg_ctx, rec);
1304                 if (consumed) {
1305                         return;
1306                 }
1307         }
1308
1309         consumed = messaging_dispatch_waiters(msg_ctx, ev, rec);
1310         if (consumed) {
1311                 return;
1312         }
1313
1314         if (ev != msg_ctx->event_ctx) {
1315                 struct iovec iov;
1316                 int fds[rec->num_fds];
1317                 int ret;
1318
1319                 /*
1320                  * We've been listening on a nested event
1321                  * context. Messages need to be handled in the main
1322                  * event context, so post to ourselves
1323                  */
1324
1325                 iov.iov_base = rec->buf.data;
1326                 iov.iov_len = rec->buf.length;
1327
1328                 for (i=0; i<rec->num_fds; i++) {
1329                         fds[i] = rec->fds[i];
1330                 }
1331
1332                 ret = messaging_post_self(
1333                         msg_ctx, rec->src, rec->dest, rec->msg_type,
1334                         &iov, 1, fds, rec->num_fds);
1335                 if (ret == 0) {
1336                         return;
1337                 }
1338         }
1339
1340         /*
1341          * If the fd-array isn't used, just close it.
1342          */
1343         for (i=0; i < rec->num_fds; i++) {
1344                 int fd = rec->fds[i];
1345                 close(fd);
1346         }
1347         rec->num_fds = 0;
1348         rec->fds = NULL;
1349 }
1350
1351 static int mess_parent_dgm_cleanup(void *private_data);
1352 static void mess_parent_dgm_cleanup_done(struct tevent_req *req);
1353
1354 bool messaging_parent_dgm_cleanup_init(struct messaging_context *msg)
1355 {
1356         struct tevent_req *req;
1357
1358         req = background_job_send(
1359                 msg, msg->event_ctx, msg, NULL, 0,
1360                 lp_parm_int(-1, "messaging", "messaging dgm cleanup interval",
1361                             60*15),
1362                 mess_parent_dgm_cleanup, msg);
1363         if (req == NULL) {
1364                 DBG_WARNING("background_job_send failed\n");
1365                 return false;
1366         }
1367         tevent_req_set_callback(req, mess_parent_dgm_cleanup_done, msg);
1368         return true;
1369 }
1370
1371 static int mess_parent_dgm_cleanup(void *private_data)
1372 {
1373         int ret;
1374
1375         ret = messaging_dgm_wipe();
1376         DEBUG(10, ("messaging_dgm_wipe returned %s\n",
1377                    ret ? strerror(ret) : "ok"));
1378         return lp_parm_int(-1, "messaging", "messaging dgm cleanup interval",
1379                            60*15);
1380 }
1381
1382 static void mess_parent_dgm_cleanup_done(struct tevent_req *req)
1383 {
1384         struct messaging_context *msg = tevent_req_callback_data(
1385                 req, struct messaging_context);
1386         NTSTATUS status;
1387
1388         status = background_job_recv(req);
1389         TALLOC_FREE(req);
1390         DEBUG(1, ("messaging dgm cleanup job ended with %s\n",
1391                   nt_errstr(status)));
1392
1393         req = background_job_send(
1394                 msg, msg->event_ctx, msg, NULL, 0,
1395                 lp_parm_int(-1, "messaging", "messaging dgm cleanup interval",
1396                             60*15),
1397                 mess_parent_dgm_cleanup, msg);
1398         if (req == NULL) {
1399                 DEBUG(1, ("background_job_send failed\n"));
1400                 return;
1401         }
1402         tevent_req_set_callback(req, mess_parent_dgm_cleanup_done, msg);
1403 }
1404
1405 int messaging_cleanup(struct messaging_context *msg_ctx, pid_t pid)
1406 {
1407         int ret;
1408
1409         if (pid == 0) {
1410                 ret = messaging_dgm_wipe();
1411         } else {
1412                 ret = messaging_dgm_cleanup(pid);
1413         }
1414
1415         return ret;
1416 }
1417
1418 struct tevent_context *messaging_tevent_context(
1419         struct messaging_context *msg_ctx)
1420 {
1421         return msg_ctx->event_ctx;
1422 }
1423
1424 struct server_id_db *messaging_names_db(struct messaging_context *msg_ctx)
1425 {
1426         return msg_ctx->names_db;
1427 }
1428
1429 /** @} **/