lib: Fix CID 1272858 Copy-paste error
[samba.git] / source3 / lib / messages.c
index 065782ac1b49095b64e9c547cc6e3eebee632d16..78ff7217d5252dd92efedf88e30f6e9d090b9c87 100644 (file)
 #include "messages.h"
 #include "lib/util/tevent_unix.h"
 #include "lib/background.h"
+#include "lib/messages_dgm.h"
+#include "lib/util/iov_buf.h"
+#include "lib/util/server_id_db.h"
+#include "lib/messages_dgm_ref.h"
+#include "lib/messages_util.h"
 
 struct messaging_callback {
        struct messaging_callback *prev, *next;
-       uint32 msg_type;
+       uint32_t msg_type;
        void (*fn)(struct messaging_context *msg, void *private_data, 
                   uint32_t msg_type, 
                   struct server_id server_id, DATA_BLOB *data);
        void *private_data;
 };
 
+struct messaging_context {
+       struct server_id id;
+       struct tevent_context *event_ctx;
+       struct messaging_callback *callbacks;
+
+       struct tevent_req **new_waiters;
+       unsigned num_new_waiters;
+
+       struct tevent_req **waiters;
+       unsigned num_waiters;
+
+       void *msg_dgm_ref;
+       struct messaging_backend *remote;
+
+       struct server_id_db *names_db;
+};
+
+static void messaging_dispatch_rec(struct messaging_context *msg_ctx,
+                                  struct messaging_rec *rec);
+
 /****************************************************************************
  A useful function for testing the message system.
 ****************************************************************************/
@@ -71,17 +96,12 @@ static void ping_message(struct messaging_context *msg_ctx,
                         struct server_id src,
                         DATA_BLOB *data)
 {
-       const char *msg = "none";
-       char *free_me = NULL;
+       struct server_id_buf idbuf;
+
+       DEBUG(1, ("INFO: Received PING message from PID %s [%.*s]\n",
+                 server_id_str_buf(src, &idbuf), (int)data->length,
+                 data->data ? (char *)data->data : ""));
 
-       if (data->data != NULL) {
-               free_me = talloc_strndup(talloc_tos(), (char *)data->data,
-                                        data->length);
-               msg = free_me;
-       }
-       DEBUG(1,("INFO: Received PING message from PID %s [%s]\n",
-                procid_str_static(&src), msg));
-       TALLOC_FREE(free_me);
        messaging_send(msg_ctx, src, MSG_PONG, data);
 }
 
@@ -95,7 +115,7 @@ static void ping_message(struct messaging_context *msg_ctx,
 struct msg_all {
        struct messaging_context *msg_ctx;
        int msg_type;
-       uint32 msg_flag;
+       uint32_t msg_flag;
        const void *buf;
        size_t len;
        int n_sent;
@@ -124,13 +144,15 @@ static int traverse_fn(struct db_record *rec, const struct server_id *id,
                                    (const uint8_t *)msg_all->buf, msg_all->len);
 
        if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
+               struct server_id_buf idbuf;
 
                /*
                 * If the pid was not found delete the entry from
                 * serverid.tdb
                 */
 
-               DEBUG(2, ("pid %s doesn't exist\n", procid_str_static(id)));
+               DEBUG(2, ("pid %s doesn't exist\n",
+                         server_id_str_buf(*id, &idbuf)));
 
                dbwrap_record_delete(rec);
        }
@@ -185,11 +207,95 @@ bool message_send_all(struct messaging_context *msg_ctx,
        return true;
 }
 
+static void messaging_recv_cb(const uint8_t *msg, size_t msg_len,
+                             int *fds, size_t num_fds,
+                             void *private_data)
+{
+       struct messaging_context *msg_ctx = talloc_get_type_abort(
+               private_data, struct messaging_context);
+       struct server_id_buf idbuf;
+       struct messaging_rec rec;
+       int64_t fds64[MIN(num_fds, INT8_MAX)];
+       size_t i;
+
+       if (msg_len < MESSAGE_HDR_LENGTH) {
+               DEBUG(1, ("message too short: %u\n", (unsigned)msg_len));
+               goto close_fail;
+       }
+
+       if (num_fds > INT8_MAX) {
+               DEBUG(1, ("too many fds: %u\n", (unsigned)num_fds));
+               goto close_fail;
+       }
+
+       /*
+        * "consume" the fds by copying them and setting
+        * the original variable to -1
+        */
+       for (i=0; i < num_fds; i++) {
+               fds64[i] = fds[i];
+               fds[i] = -1;
+       }
+
+       rec = (struct messaging_rec) {
+               .msg_version = MESSAGE_VERSION,
+               .buf.data = discard_const_p(uint8_t, msg) + MESSAGE_HDR_LENGTH,
+               .buf.length = msg_len - MESSAGE_HDR_LENGTH,
+               .num_fds = num_fds,
+               .fds = fds64,
+       };
+
+       message_hdr_get(&rec.msg_type, &rec.src, &rec.dest, msg);
+
+       DEBUG(10, ("%s: Received message 0x%x len %u (num_fds:%u) from %s\n",
+                  __func__, (unsigned)rec.msg_type,
+                  (unsigned)rec.buf.length,
+                  (unsigned)num_fds,
+                  server_id_str_buf(rec.src, &idbuf)));
+
+       messaging_dispatch_rec(msg_ctx, &rec);
+       return;
+
+close_fail:
+       for (i=0; i < num_fds; i++) {
+               close(fds[i]);
+       }
+}
+
+static int messaging_context_destructor(struct messaging_context *ctx)
+{
+       unsigned i;
+
+       for (i=0; i<ctx->num_new_waiters; i++) {
+               if (ctx->new_waiters[i] != NULL) {
+                       tevent_req_set_cleanup_fn(ctx->new_waiters[i], NULL);
+                       ctx->new_waiters[i] = NULL;
+               }
+       }
+       for (i=0; i<ctx->num_waiters; i++) {
+               if (ctx->waiters[i] != NULL) {
+                       tevent_req_set_cleanup_fn(ctx->waiters[i], NULL);
+                       ctx->waiters[i] = NULL;
+               }
+       }
+
+       return 0;
+}
+
+static const char *private_path(const char *name)
+{
+       return talloc_asprintf(talloc_tos(), "%s/%s", lp_private_dir(), name);
+}
+
 struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx, 
                                         struct tevent_context *ev)
 {
        struct messaging_context *ctx;
        NTSTATUS status;
+       int ret;
+       const char *lck_path;
+       const char *priv_path;
+       bool ok;
 
        if (!(ctx = talloc_zero(mem_ctx, struct messaging_context))) {
                return NULL;
@@ -198,15 +304,46 @@ struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
        ctx->id = procid_self();
        ctx->event_ctx = ev;
 
-       status = messaging_dgm_init(ctx, ctx, &ctx->local);
+       sec_init();
+
+       lck_path = lock_path("msg");
+       if (lck_path == NULL) {
+               TALLOC_FREE(ctx);
+               return NULL;
+       }
+
+       ok = directory_create_or_exist_strict(lck_path, sec_initial_uid(),
+                                             0755);
+       if (!ok) {
+               DEBUG(10, ("%s: Could not create lock directory: %s\n",
+                          __func__, strerror(errno)));
+               TALLOC_FREE(ctx);
+               return NULL;
+       }
+
+       priv_path = private_path("sock");
 
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(2, ("messaging_dgm_init failed: %s\n",
-                         nt_errstr(status)));
+       ok = directory_create_or_exist_strict(priv_path, sec_initial_uid(),
+                                             0700);
+       if (!ok) {
+               DEBUG(10, ("%s: Could not create msg directory: %s\n",
+                          __func__, strerror(errno)));
                TALLOC_FREE(ctx);
                return NULL;
        }
 
+       ctx->msg_dgm_ref = messaging_dgm_ref(
+               ctx, ctx->event_ctx, ctx->id.unique_id,
+               priv_path, lck_path, messaging_recv_cb, ctx, &ret);
+
+       if (ctx->msg_dgm_ref == NULL) {
+               DEBUG(2, ("messaging_dgm_ref failed: %s\n", strerror(ret)));
+               TALLOC_FREE(ctx);
+               return NULL;
+       }
+
+       talloc_set_destructor(ctx, messaging_context_destructor);
+
        if (lp_clustering()) {
                status = messaging_ctdbd_init(ctx, ctx, &ctx->remote);
 
@@ -219,6 +356,15 @@ struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
        }
        ctx->id.vnn = get_my_vnn();
 
+       ctx->names_db = server_id_db_init(
+               ctx, ctx->id, lp_lock_directory(), 0,
+               TDB_INCOMPATIBLE_HASH|TDB_CLEAR_IF_FIRST);
+       if (ctx->names_db == NULL) {
+               DEBUG(10, ("%s: server_id_db_init failed\n", __func__));
+               TALLOC_FREE(ctx);
+               return NULL;
+       }
+
        messaging_register(ctx, NULL, MSG_PING, ping_message);
 
        /* Register some debugging related messages */
@@ -241,16 +387,20 @@ struct server_id messaging_server_id(const struct messaging_context *msg_ctx)
 NTSTATUS messaging_reinit(struct messaging_context *msg_ctx)
 {
        NTSTATUS status;
+       int ret;
 
-       TALLOC_FREE(msg_ctx->local);
+       TALLOC_FREE(msg_ctx->msg_dgm_ref);
 
        msg_ctx->id = procid_self();
 
-       status = messaging_dgm_init(msg_ctx, msg_ctx, &msg_ctx->local);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0, ("messaging_dgm_init failed: %s\n",
-                         nt_errstr(status)));
-               return status;
+       msg_ctx->msg_dgm_ref = messaging_dgm_ref(
+               msg_ctx, msg_ctx->event_ctx, msg_ctx->id.unique_id,
+               private_path("sock"), lock_path("msg"),
+               messaging_recv_cb, msg_ctx, &ret);
+
+       if (msg_ctx->msg_dgm_ref == NULL) {
+               DEBUG(2, ("messaging_dgm_ref failed: %s\n", strerror(ret)));
+               return map_nt_error_from_unix(ret);
        }
 
        TALLOC_FREE(msg_ctx->remote);
@@ -266,6 +416,8 @@ NTSTATUS messaging_reinit(struct messaging_context *msg_ctx)
                }
        }
 
+       server_id_db_reinit(msg_ctx->names_db, msg_ctx->id);
+
        return NT_STATUS_OK;
 }
 
@@ -341,15 +493,6 @@ void messaging_deregister(struct messaging_context *ctx, uint32_t msg_type,
        }
 }
 
-struct messaging_selfsend_state {
-       struct messaging_context *msg;
-       struct messaging_rec rec;
-};
-
-static void messaging_trigger_self(struct tevent_context *ev,
-                                  struct tevent_immediate *im,
-                                  void *private_data);
-
 /*
   Send a message to a particular server
 */
@@ -357,59 +500,12 @@ NTSTATUS messaging_send(struct messaging_context *msg_ctx,
                        struct server_id server, uint32_t msg_type,
                        const DATA_BLOB *data)
 {
-       if (server_id_is_disconnected(&server)) {
-               return NT_STATUS_INVALID_PARAMETER_MIX;
-       }
-
-       if (!procid_is_local(&server)) {
-               return msg_ctx->remote->send_fn(msg_ctx, server,
-                                               msg_type, data,
-                                               msg_ctx->remote);
-       }
-
-       if (server_id_equal(&msg_ctx->id, &server)) {
-               struct messaging_selfsend_state *state;
-               struct tevent_immediate *im;
-
-               state = talloc_pooled_object(
-                       msg_ctx, struct messaging_selfsend_state,
-                       1, data->length);
-               if (state == NULL) {
-                       return NT_STATUS_NO_MEMORY;
-               }
-               state->msg = msg_ctx;
-               state->rec.msg_version = MESSAGE_VERSION;
-               state->rec.msg_type = msg_type & MSG_TYPE_MASK;
-               state->rec.dest = server;
-               state->rec.src = msg_ctx->id;
-
-               /* Can't fail, it's a pooled_object */
-               state->rec.buf = data_blob_talloc(
-                       state, data->data, data->length);
-
-               im = tevent_create_immediate(state);
-               if (im == NULL) {
-                       TALLOC_FREE(state);
-                       return NT_STATUS_NO_MEMORY;
-               }
-
-               tevent_schedule_immediate(im, msg_ctx->event_ctx,
-                                         messaging_trigger_self, state);
-               return NT_STATUS_OK;
-       }
+       struct iovec iov;
 
-       return msg_ctx->local->send_fn(msg_ctx, server, msg_type, data,
-                                      msg_ctx->local);
-}
+       iov.iov_base = data->data;
+       iov.iov_len = data->length;
 
-static void messaging_trigger_self(struct tevent_context *ev,
-                                  struct tevent_immediate *im,
-                                  void *private_data)
-{
-       struct messaging_selfsend_state *state = talloc_get_type_abort(
-               private_data, struct messaging_selfsend_state);
-       messaging_dispatch_rec(state->msg, &state->rec);
-       TALLOC_FREE(state);
+       return messaging_send_iov(msg_ctx, server, msg_type, &iov, 1, NULL, 0);
 }
 
 NTSTATUS messaging_send_buf(struct messaging_context *msg_ctx,
@@ -420,32 +516,70 @@ NTSTATUS messaging_send_buf(struct messaging_context *msg_ctx,
        return messaging_send(msg_ctx, server, msg_type, &blob);
 }
 
-NTSTATUS messaging_send_iov(struct messaging_context *msg_ctx,
-                           struct server_id server, uint32_t msg_type,
-                           const struct iovec *iov, int iovlen)
+NTSTATUS messaging_send_iov_from(struct messaging_context *msg_ctx,
+                                struct server_id src, struct server_id dst,
+                                uint32_t msg_type,
+                                const struct iovec *iov, int iovlen,
+                                const int *fds, size_t num_fds)
 {
-       uint8_t *buf;
-       NTSTATUS status;
+       int ret;
+       uint8_t hdr[MESSAGE_HDR_LENGTH];
+       struct iovec iov2[iovlen+1];
 
-       buf = iov_buf(talloc_tos(), iov, iovlen);
-       if (buf == NULL) {
-               return NT_STATUS_NO_MEMORY;
+       if (server_id_is_disconnected(&dst)) {
+               return NT_STATUS_INVALID_PARAMETER_MIX;
        }
 
-       status = messaging_send_buf(msg_ctx, server, msg_type,
-                                   buf, talloc_get_size(buf));
+       if (num_fds > INT8_MAX) {
+               return NT_STATUS_INVALID_PARAMETER_MIX;
+       }
+
+       if (!procid_is_local(&dst)) {
+               if (num_fds > 0) {
+                       return NT_STATUS_NOT_SUPPORTED;
+               }
 
-       TALLOC_FREE(buf);
-       return status;
+               ret = msg_ctx->remote->send_fn(src, dst,
+                                              msg_type, iov, iovlen,
+                                              NULL, 0,
+                                              msg_ctx->remote);
+               if (ret != 0) {
+                       return map_nt_error_from_unix(ret);
+               }
+               return NT_STATUS_OK;
+       }
+
+       message_hdr_put(hdr, msg_type, src, dst);
+       iov2[0] = (struct iovec){ .iov_base = hdr, .iov_len = sizeof(hdr) };
+       memcpy(&iov2[1], iov, iovlen * sizeof(*iov));
+
+       become_root();
+       ret = messaging_dgm_send(dst.pid, iov2, iovlen+1, fds, num_fds);
+       unbecome_root();
+
+       if (ret != 0) {
+               return map_nt_error_from_unix(ret);
+       }
+       return NT_STATUS_OK;
+}
+
+NTSTATUS messaging_send_iov(struct messaging_context *msg_ctx,
+                           struct server_id dst, uint32_t msg_type,
+                           const struct iovec *iov, int iovlen,
+                           const int *fds, size_t num_fds)
+{
+       return messaging_send_iov_from(msg_ctx, msg_ctx->id, dst, msg_type,
+                                      iov, iovlen, fds, num_fds);
 }
 
 static struct messaging_rec *messaging_rec_dup(TALLOC_CTX *mem_ctx,
                                               struct messaging_rec *rec)
 {
        struct messaging_rec *result;
+       size_t fds_size = sizeof(int64_t) * rec->num_fds;
 
-       result = talloc_pooled_object(mem_ctx, struct messaging_rec,
-                                     1, rec->buf.length);
+       result = talloc_pooled_object(mem_ctx, struct messaging_rec, 2,
+                                     rec->buf.length + fds_size);
        if (result == NULL) {
                return NULL;
        }
@@ -455,12 +589,19 @@ static struct messaging_rec *messaging_rec_dup(TALLOC_CTX *mem_ctx,
 
        result->buf.data = talloc_memdup(result, rec->buf.data,
                                         rec->buf.length);
+
+       result->fds = NULL;
+       if (result->num_fds > 0) {
+               result->fds = talloc_memdup(result, rec->fds, fds_size);
+       }
+
        return result;
 }
 
 struct messaging_filtered_read_state {
        struct tevent_context *ev;
        struct messaging_context *msg_ctx;
+       void *tevent_handle;
 
        bool (*filter)(struct messaging_rec *rec, void *private_data);
        void *private_data;
@@ -491,6 +632,26 @@ struct tevent_req *messaging_filtered_read_send(
        state->filter = filter;
        state->private_data = private_data;
 
+       /*
+        * We have to defer the callback here, as we might be called from
+        * within a different tevent_context than state->ev
+        */
+       tevent_req_defer_callback(req, state->ev);
+
+       state->tevent_handle = messaging_dgm_register_tevent_context(
+               state, ev);
+       if (tevent_req_nomem(state, req)) {
+               return tevent_req_post(req, ev);
+       }
+
+       /*
+        * We add ourselves to the "new_waiters" array, not the "waiters"
+        * array. If we are called from within messaging_read_done,
+        * messaging_dispatch_rec will be in an active for-loop on
+        * "waiters". We must be careful not to mess with this array, because
+        * it could mean that a single event is being delivered twice.
+        */
+
        new_waiters_len = talloc_array_length(msg_ctx->new_waiters);
 
        if (new_waiters_len == msg_ctx->num_new_waiters) {
@@ -521,6 +682,16 @@ static void messaging_filtered_read_cleanup(struct tevent_req *req,
 
        tevent_req_set_cleanup_fn(req, NULL);
 
+       TALLOC_FREE(state->tevent_handle);
+
+       /*
+        * Just set the [new_]waiters entry to NULL, be careful not to mess
+        * with the other "waiters" array contents. We are often called from
+        * within "messaging_dispatch_rec", which loops over
+        * "waiters". Messing with the "waiters" array will mess up that
+        * for-loop.
+        */
+
        for (i=0; i<msg_ctx->num_waiters; i++) {
                if (msg_ctx->waiters[i] == req) {
                        msg_ctx->waiters[i] = NULL;
@@ -603,6 +774,10 @@ static bool messaging_read_filter(struct messaging_rec *rec,
        struct messaging_read_state *state = talloc_get_type_abort(
                private_data, struct messaging_read_state);
 
+       if (rec->num_fds != 0) {
+               return false;
+       }
+
        return rec->msg_type == state->msg_type;
 }
 
@@ -638,6 +813,87 @@ int messaging_read_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
        return 0;
 }
 
+struct messaging_handler_state {
+       struct tevent_context *ev;
+       struct messaging_context *msg_ctx;
+       uint32_t msg_type;
+       bool (*handler)(struct messaging_context *msg_ctx,
+                       struct messaging_rec **rec, void *private_data);
+       void *private_data;
+};
+
+static void messaging_handler_got_msg(struct tevent_req *subreq);
+
+struct tevent_req *messaging_handler_send(
+       TALLOC_CTX *mem_ctx, struct tevent_context *ev,
+       struct messaging_context *msg_ctx, uint32_t msg_type,
+       bool (*handler)(struct messaging_context *msg_ctx,
+                       struct messaging_rec **rec, void *private_data),
+       void *private_data)
+{
+       struct tevent_req *req, *subreq;
+       struct messaging_handler_state *state;
+
+       req = tevent_req_create(mem_ctx, &state,
+                               struct messaging_handler_state);
+       if (req == NULL) {
+               return NULL;
+       }
+       state->ev = ev;
+       state->msg_ctx = msg_ctx;
+       state->msg_type = msg_type;
+       state->handler = handler;
+       state->private_data = private_data;
+
+       subreq = messaging_read_send(state, state->ev, state->msg_ctx,
+                                    state->msg_type);
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_set_callback(subreq, messaging_handler_got_msg, req);
+       return req;
+}
+
+static void messaging_handler_got_msg(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct messaging_handler_state *state = tevent_req_data(
+               req, struct messaging_handler_state);
+       struct messaging_rec *rec;
+       int ret;
+       bool ok;
+
+       ret = messaging_read_recv(subreq, state, &rec);
+       TALLOC_FREE(subreq);
+       if (tevent_req_error(req, ret)) {
+               return;
+       }
+
+       subreq = messaging_read_send(state, state->ev, state->msg_ctx,
+                                    state->msg_type);
+       if (tevent_req_nomem(subreq, req)) {
+               return;
+       }
+       tevent_req_set_callback(subreq, messaging_handler_got_msg, req);
+
+       ok = state->handler(state->msg_ctx, &rec, state->private_data);
+       TALLOC_FREE(rec);
+       if (ok) {
+               /*
+                * Next round
+                */
+               return;
+       }
+       TALLOC_FREE(subreq);
+       tevent_req_done(req);
+}
+
+int messaging_handler_recv(struct tevent_req *req)
+{
+       return tevent_req_simple_recv_unix(req);
+}
+
 static bool messaging_append_new_waiters(struct messaging_context *msg_ctx)
 {
        if (msg_ctx->num_new_waiters == 0) {
@@ -669,26 +925,47 @@ static bool messaging_append_new_waiters(struct messaging_context *msg_ctx)
 /*
   Dispatch one messaging_rec
 */
-void messaging_dispatch_rec(struct messaging_context *msg_ctx,
-                           struct messaging_rec *rec)
+static void messaging_dispatch_rec(struct messaging_context *msg_ctx,
+                                  struct messaging_rec *rec)
 {
        struct messaging_callback *cb, *next;
        unsigned i;
+       size_t j;
 
        for (cb = msg_ctx->callbacks; cb != NULL; cb = next) {
                next = cb->next;
-               if (cb->msg_type == rec->msg_type) {
-                       cb->fn(msg_ctx, cb->private_data, rec->msg_type,
-                              rec->src, &rec->buf);
-                       /* we continue looking for matching messages
-                          after finding one. This matters for
-                          subsystems like the internal notify code
-                          which register more than one handler for
-                          the same message type */
+               if (cb->msg_type != rec->msg_type) {
+                       continue;
                }
+
+               /*
+                * the old style callbacks don't support fd passing
+                */
+               for (j=0; j < rec->num_fds; j++) {
+                       int fd = rec->fds[j];
+                       close(fd);
+               }
+               rec->num_fds = 0;
+               rec->fds = NULL;
+
+               cb->fn(msg_ctx, cb->private_data, rec->msg_type,
+                      rec->src, &rec->buf);
+
+               /*
+                * we continue looking for matching messages after finding
+                * one. This matters for subsystems like the internal notify
+                * code which register more than one handler for the same
+                * message type
+                */
        }
 
        if (!messaging_append_new_waiters(msg_ctx)) {
+               for (j=0; j < rec->num_fds; j++) {
+                       int fd = rec->fds[j];
+                       close(fd);
+               }
+               rec->num_fds = 0;
+               rec->fds = NULL;
                return;
        }
 
@@ -705,7 +982,7 @@ void messaging_dispatch_rec(struct messaging_context *msg_ctx,
                         * to keep the order of waiters, as
                         * other code may depend on this.
                         */
-                       if (i <  msg_ctx->num_waiters - 1) {
+                       if (i < msg_ctx->num_waiters - 1) {
                                memmove(&msg_ctx->waiters[i],
                                        &msg_ctx->waiters[i+1],
                                        sizeof(struct tevent_req *) *
@@ -719,11 +996,26 @@ void messaging_dispatch_rec(struct messaging_context *msg_ctx,
                        req, struct messaging_filtered_read_state);
                if (state->filter(rec, state->private_data)) {
                        messaging_filtered_read_done(req, rec);
+
+                       /*
+                        * Only the first one gets the fd-array
+                        */
+                       rec->num_fds = 0;
+                       rec->fds = NULL;
                }
 
                i += 1;
        }
-       return;
+
+       /*
+        * If the fd-array isn't used, just close it.
+        */
+       for (j=0; j < rec->num_fds; j++) {
+               int fd = rec->fds[j];
+               close(fd);
+       }
+       rec->num_fds = 0;
+       rec->fds = NULL;
 }
 
 static int mess_parent_dgm_cleanup(void *private_data);
@@ -747,12 +1039,11 @@ bool messaging_parent_dgm_cleanup_init(struct messaging_context *msg)
 
 static int mess_parent_dgm_cleanup(void *private_data)
 {
-       struct messaging_context *msg_ctx = talloc_get_type_abort(
-               private_data, struct messaging_context);
-       NTSTATUS status;
+       int ret;
 
-       status = messaging_dgm_wipe(msg_ctx);
-       DEBUG(10, ("messaging_dgm_wipe returned %s\n", nt_errstr(status)));
+       ret = messaging_dgm_wipe();
+       DEBUG(10, ("messaging_dgm_wipe returned %s\n",
+                  ret ? strerror(ret) : "ok"));
        return lp_parm_int(-1, "messaging", "messaging dgm cleanup interval",
                           60*15);
 }
@@ -775,8 +1066,33 @@ static void mess_parent_dgm_cleanup_done(struct tevent_req *req)
                mess_parent_dgm_cleanup, msg);
        if (req == NULL) {
                DEBUG(1, ("background_job_send failed\n"));
+               return;
        }
        tevent_req_set_callback(req, mess_parent_dgm_cleanup_done, msg);
 }
 
+int messaging_cleanup(struct messaging_context *msg_ctx, pid_t pid)
+{
+       int ret;
+
+       if (pid == 0) {
+               ret = messaging_dgm_wipe();
+       } else {
+               ret = messaging_dgm_cleanup(pid);
+       }
+
+       return ret;
+}
+
+struct tevent_context *messaging_tevent_context(
+       struct messaging_context *msg_ctx)
+{
+       return msg_ctx->event_ctx;
+}
+
+struct server_id_db *messaging_names_db(struct messaging_context *msg_ctx)
+{
+       return msg_ctx->names_db;
+}
+
 /** @} **/