s3:messaging: add fds-array to message-backend send function
[obnox/samba/samba-obnox.git] / source3 / lib / messages.c
index 06d05641e5033d17131623eda3979c0535cd2005..0579dbf359944736b80f9d2845db3464c0c02e17 100644 (file)
@@ -51,6 +51,7 @@
 #include "messages.h"
 #include "lib/util/tevent_unix.h"
 #include "lib/background.h"
+#include "lib/messages_dgm.h"
 
 struct messaging_callback {
        struct messaging_callback *prev, *next;
@@ -72,14 +73,14 @@ struct messaging_context {
        struct tevent_req **waiters;
        unsigned num_waiters;
 
-       struct messaging_dgm_context *local;
-
        struct messaging_backend *remote;
-
-       bool *have_context;
 };
 
-static int messaging_context_destructor(struct messaging_context *msg_ctx);
+struct messaging_hdr {
+       int msg_type;
+       struct server_id dst;
+       struct server_id src;
+};
 
 /****************************************************************************
  A useful function for testing the message system.
@@ -202,40 +203,75 @@ bool message_send_all(struct messaging_context *msg_ctx,
        return true;
 }
 
-static void messaging_recv_cb(int msg_type,
-                             struct server_id src, struct server_id dst,
-                             const uint8_t *msg, size_t msg_len,
+static void messaging_recv_cb(const uint8_t *msg, size_t msg_len,
+                             const int *fds, size_t num_fds,
                              void *private_data)
 {
        struct messaging_context *msg_ctx = talloc_get_type_abort(
                private_data, struct messaging_context);
+       const struct messaging_hdr *hdr;
+       struct server_id_buf idbuf;
        struct messaging_rec rec;
+       int64_t fds64[MIN(num_fds, INT8_MAX)];
+       size_t i;
+
+       if (msg_len < sizeof(*hdr)) {
+               for (i=0; i < num_fds; i++) {
+                       close(fds[i]);
+               }
+               DEBUG(1, ("message too short: %u\n", (unsigned)msg_len));
+               return;
+       }
+
+       if (num_fds > INT8_MAX) {
+               for (i=0; i < num_fds; i++) {
+                       close(fds[i]);
+               }
+               DEBUG(1, ("too many fds: %u\n", (unsigned)num_fds));
+               return;
+       }
+
+       for (i=0; i < num_fds; i++) {
+               fds64[i] = fds[i];
+       }
+
+       /*
+        * messages_dgm guarantees alignment, so we can cast here
+        */
+       hdr = (const struct messaging_hdr *)msg;
+
+       DEBUG(10, ("%s: Received message 0x%x len %u (num_fds:%u) from %s\n",
+                  __func__, (unsigned)hdr->msg_type,
+                  (unsigned)(msg_len - sizeof(*hdr)),
+                  (unsigned)num_fds,
+                  server_id_str_buf(hdr->src, &idbuf)));
 
        rec = (struct messaging_rec) {
                .msg_version = MESSAGE_VERSION,
-               .msg_type = msg_type,
-               .src = src,
-               .dest = dst,
-               .buf.data = discard_const_p(uint8, msg),
-               .buf.length = msg_len
+               .msg_type = hdr->msg_type,
+               .src = hdr->src,
+               .dest = hdr->dst,
+               .buf.data = discard_const_p(uint8, msg) + sizeof(*hdr),
+               .buf.length = msg_len - sizeof(*hdr),
+               .num_fds = num_fds,
+               .fds = fds64,
        };
 
        messaging_dispatch_rec(msg_ctx, &rec);
 }
 
+static int messaging_context_destructor(struct messaging_context *ctx)
+{
+       messaging_dgm_destroy();
+       return 0;
+}
+
 struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx, 
                                         struct tevent_context *ev)
 {
        struct messaging_context *ctx;
        NTSTATUS status;
        int ret;
-       static bool have_context = false;
-
-       if (have_context) {
-               DEBUG(0, ("No two messaging contexts per process\n"));
-               return NULL;
-       }
-
 
        if (!(ctx = talloc_zero(mem_ctx, struct messaging_context))) {
                return NULL;
@@ -243,12 +279,12 @@ struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
 
        ctx->id = procid_self();
        ctx->event_ctx = ev;
-       ctx->have_context = &have_context;
 
        sec_init();
 
-       ret = messaging_dgm_init(ctx, ctx->event_ctx, ctx->id,
-                                messaging_recv_cb, ctx, &ctx->local);
+       ret = messaging_dgm_init(ctx->event_ctx, ctx->id,
+                                lp_cache_directory(), sec_initial_uid(),
+                                messaging_recv_cb, ctx);
 
        if (ret != 0) {
                DEBUG(2, ("messaging_dgm_init failed: %s\n", strerror(ret)));
@@ -256,6 +292,8 @@ struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
                return NULL;
        }
 
+       talloc_set_destructor(ctx, messaging_context_destructor);
+
        if (lp_clustering()) {
                status = messaging_ctdbd_init(ctx, ctx, &ctx->remote);
 
@@ -276,19 +314,9 @@ struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
        register_dmalloc_msgs(ctx);
        debug_register_msgs(ctx);
 
-       have_context = true;
-       talloc_set_destructor(ctx, messaging_context_destructor);
-
        return ctx;
 }
 
-static int messaging_context_destructor(struct messaging_context *msg_ctx)
-{
-       SMB_ASSERT(*msg_ctx->have_context);
-       *msg_ctx->have_context = false;
-       return 0;
-}
-
 struct server_id messaging_server_id(const struct messaging_context *msg_ctx)
 {
        return msg_ctx->id;
@@ -302,13 +330,13 @@ NTSTATUS messaging_reinit(struct messaging_context *msg_ctx)
        NTSTATUS status;
        int ret;
 
-       TALLOC_FREE(msg_ctx->local);
+       messaging_dgm_destroy();
 
        msg_ctx->id = procid_self();
 
-       ret = messaging_dgm_init(msg_ctx, msg_ctx->event_ctx,
-                                msg_ctx->id, messaging_recv_cb, msg_ctx,
-                                &msg_ctx->local);
+       ret = messaging_dgm_init(msg_ctx->event_ctx, msg_ctx->id,
+                                lp_cache_directory(), sec_initial_uid(),
+                                messaging_recv_cb, msg_ctx);
        if (ret != 0) {
                DEBUG(0, ("messaging_dgm_init failed: %s\n", strerror(errno)));
                return map_nt_error_from_unix(ret);
@@ -402,13 +430,6 @@ void messaging_deregister(struct messaging_context *ctx, uint32_t msg_type,
        }
 }
 
-static bool messaging_is_self_send(const struct messaging_context *msg_ctx,
-                                  const struct server_id *dst)
-{
-       return ((msg_ctx->id.vnn == dst->vnn) &&
-               (msg_ctx->id.pid == dst->pid));
-}
-
 /*
   Send a message to a particular server
 */
@@ -437,6 +458,8 @@ NTSTATUS messaging_send_iov(struct messaging_context *msg_ctx,
                            const struct iovec *iov, int iovlen)
 {
        int ret;
+       struct messaging_hdr hdr;
+       struct iovec iov2[iovlen+1];
 
        if (server_id_is_disconnected(&server)) {
                return NT_STATUS_INVALID_PARAMETER_MIX;
@@ -445,6 +468,7 @@ NTSTATUS messaging_send_iov(struct messaging_context *msg_ctx,
        if (!procid_is_local(&server)) {
                ret = msg_ctx->remote->send_fn(msg_ctx->id, server,
                                               msg_type, iov, iovlen,
+                                              NULL, 0,
                                               msg_ctx->remote);
                if (ret != 0) {
                        return map_nt_error_from_unix(ret);
@@ -452,28 +476,42 @@ NTSTATUS messaging_send_iov(struct messaging_context *msg_ctx,
                return NT_STATUS_OK;
        }
 
-       if (messaging_is_self_send(msg_ctx, &server)) {
+       if (server_id_same_process(&msg_ctx->id, &server)) {
                struct messaging_rec rec;
                uint8_t *buf;
 
+               /*
+                * Self-send, directly dispatch
+                */
+
                buf = iov_buf(talloc_tos(), iov, iovlen);
                if (buf == NULL) {
                        return NT_STATUS_NO_MEMORY;
                }
 
-               rec.msg_version = MESSAGE_VERSION;
-               rec.msg_type = msg_type & MSG_TYPE_MASK;
-               rec.dest = server;
-               rec.src = msg_ctx->id;
-               rec.buf = data_blob_const(buf, talloc_get_size(buf));
+               rec = (struct messaging_rec) {
+                       .msg_version = MESSAGE_VERSION,
+                       .msg_type = msg_type & MSG_TYPE_MASK,
+                       .dest = server,
+                       .src = msg_ctx->id,
+                       .buf = data_blob_const(buf, talloc_get_size(buf)),
+               };
+
                messaging_dispatch_rec(msg_ctx, &rec);
                TALLOC_FREE(buf);
                return NT_STATUS_OK;
        }
 
+       hdr = (struct messaging_hdr) {
+               .msg_type = msg_type,
+               .dst = server,
+               .src = msg_ctx->id
+       };
+       iov2[0] = (struct iovec){ .iov_base = &hdr, .iov_len = sizeof(hdr) };
+       memcpy(&iov2[1], iov, iovlen * sizeof(*iov));
+
        become_root();
-       ret = messaging_dgm_send(msg_ctx->local, msg_ctx->id, server, msg_type,
-                                iov, iovlen);
+       ret = messaging_dgm_send(server.pid, iov2, iovlen+1, NULL, 0);
        unbecome_root();
 
        if (ret != 0) {
@@ -486,9 +524,10 @@ 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;
        }
@@ -498,6 +537,13 @@ 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_array(result, int64_t, result->num_fds);
+               memcpy(result->fds, rec->fds, fds_size);
+       }
+
        return result;
 }
 
@@ -542,7 +588,7 @@ struct tevent_req *messaging_filtered_read_send(
        tevent_req_defer_callback(req, state->ev);
 
        state->tevent_handle = messaging_dgm_register_tevent_context(
-               state, msg_ctx->local, ev);
+               state, ev);
        if (tevent_req_nomem(state, req)) {
                return tevent_req_post(req, ev);
        }
@@ -677,6 +723,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;
 }
 
@@ -810,6 +860,7 @@ void messaging_dispatch_rec(struct messaging_context *msg_ctx,
 {
        struct messaging_callback *cb, *next;
        unsigned i;
+       size_t j;
 
        for (cb = msg_ctx->callbacks; cb != NULL; cb = next) {
                next = cb->next;
@@ -817,7 +868,17 @@ void messaging_dispatch_rec(struct messaging_context *msg_ctx,
                        continue;
                }
 
-               if (messaging_is_self_send(msg_ctx, &rec->dest)) {
+               /*
+                * 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;
+
+               if (server_id_same_process(&rec->src, &rec->dest)) {
                        /*
                         * This is a self-send. We are called here from
                         * messaging_send(), and we don't want to directly
@@ -844,6 +905,12 @@ void messaging_dispatch_rec(struct messaging_context *msg_ctx,
        }
 
        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;
        }
 
@@ -874,10 +941,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;
        }
+
+       /*
+        * 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);
@@ -901,11 +984,9 @@ 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);
        int ret;
 
-       ret = messaging_dgm_wipe(msg_ctx->local);
+       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",
@@ -939,9 +1020,9 @@ int messaging_cleanup(struct messaging_context *msg_ctx, pid_t pid)
        int ret;
 
        if (pid == 0) {
-               ret = messaging_dgm_wipe(msg_ctx->local);
+               ret = messaging_dgm_wipe();
        } else {
-               ret = messaging_dgm_cleanup(msg_ctx->local, pid);
+               ret = messaging_dgm_cleanup(pid);
        }
 
        return ret;