The branch, master has been updated
       via  c2f66e7 messaging3: Include messages_dgm.h only in messages.c
       via  121d3dd messaging3: Move messaging_hdr handling to messages.c.
       via  bff27ee messaging3: Only store the pid in messaging_dgm_context
       via  6e63420 messaging3: Save 48 bytes .text
       via  90a2246 messaging3: Remove one-context protection from messages.c
       via  d22bb34 messaging3: Protect messaging_dgm against multiple contexts
       via  a1529c3 messaging3: Add messages_dgm.h
       via  274998f messaging3: Pass on msg_type unmasked
       via  ea5280e messaging3: I don't see 2 versions running concurrently...
       via  a0efa09 messaging3: Avoid "enum messaging_type" in messages_dgm
       via  bab01d1 messaging3: Pass dir_owner to messaging_dgm_init()
       via  f408a42 messaging3: Pass cache_dir to messaging_dgm_init()
       via  99e288c messaging3: Move [un]become_root() calls out of 
messaging_dgm_send()
       via  7c6a6cd messaging3: Move sec_init() call out of messaging_dgm_init()
       via  3aa3c6e messaging3: Directly refer to messaging_dgm in messages.c
       via  28db680 smbd: Use messaging_cleanup()
       via  32d45cf messaging3: Add messaging_cleanup
       via  dcd3e2f messaging_dgm: Remove unused "messaging_context"
       via  7cb94e6 messaging3: Explicitly pass server_id to messaging_dgm_init
       via  706ba59 messaging3: Explicitly pass tevent_context to 
messaging_dgm_init
       via  371e999 messaging_dgm: Receive through a cb function
      from  42e1556 smbd: Add debugs to smbXsrv_open.c

http://gitweb.samba.org/?p=samba.git;a=shortlog;h=master


- Log -----------------------------------------------------------------
commit c2f66e71aaf6f31f177ea53686cf30438e292b23
Author: Volker Lendecke <v...@samba.org>
Date:   Sun Jul 27 12:31:21 2014 +0200

    messaging3: Include messages_dgm.h only in messages.c
    
    Signed-off-by: Volker Lendecke <v...@samba.org>
    Reviewed-by: Jeremy Allison <j...@samba.org>
    
    Autobuild-User(master): Jeremy Allison <j...@samba.org>
    Autobuild-Date(master): Tue Aug 12 02:21:32 CEST 2014 on sn-devel-104

commit 121d3ddce66053c8e00983d95f2689a64baf06c8
Author: Volker Lendecke <v...@samba.org>
Date:   Sun Jul 27 12:29:26 2014 +0200

    messaging3: Move messaging_hdr handling to messages.c.
    
    This makes messages_dgm a simple byte-transport across processes that
    knows almost nothing about server_id etc.
    
    Signed-off-by: Volker Lendecke <v...@samba.org>
    Reviewed-by: Jeremy Allison <j...@samba.org>

commit bff27ee7837861d8c3fd8fef7ed7411944be575b
Author: Volker Lendecke <v...@samba.org>
Date:   Fri Jul 25 11:03:11 2014 +0000

    messaging3: Only store the pid in messaging_dgm_context
    
    That's all we need here
    
    Signed-off-by: Volker Lendecke <v...@samba.org>
    Reviewed-by: Jeremy Allison <j...@samba.org>

commit 6e6342055a74e5cc4a630821c2e0ac207a5b44ae
Author: Volker Lendecke <v...@samba.org>
Date:   Fri Jul 25 10:42:19 2014 +0000

    messaging3: Save 48 bytes .text

commit 90a2246aac4a441ffc2ff6c5165c6de4c2c4123f
Author: Volker Lendecke <v...@samba.org>
Date:   Sat Aug 9 17:18:04 2014 +0200

    messaging3: Remove one-context protection from messages.c
    
    messages_dgm.c takes care of it.
    
    Signed-off-by: Volker Lendecke <v...@samba.org>
    Reviewed-by: Jeremy Allison <j...@samba.org>

commit d22bb34b7308794b721af7c62f3682b37d997f6b
Author: Volker Lendecke <v...@samba.org>
Date:   Sun Jul 20 14:51:47 2014 +0200

    messaging3: Protect messaging_dgm against multiple contexts
    
    We can't rely on posix locking within a process
    
    Signed-off-by: Volker Lendecke <v...@samba.org>
    Reviewed-by: Jeremy Allison <j...@samba.org>

commit a1529c3302656fad3daf1247141a987d36801066
Author: Volker Lendecke <v...@samba.org>
Date:   Thu Jul 17 11:57:51 2014 +0000

    messaging3: Add messages_dgm.h
    
    Signed-off-by: Volker Lendecke <v...@samba.org>
    Reviewed-by: Jeremy Allison <j...@samba.org>

commit 274998fb89e9225db5549e8a6500cbe982e84c42
Author: Volker Lendecke <v...@samba.org>
Date:   Thu Jul 17 11:56:18 2014 +0000

    messaging3: Pass on msg_type unmasked
    
    Signed-off-by: Volker Lendecke <v...@samba.org>
    Reviewed-by: Jeremy Allison <j...@samba.org>

commit ea5280e856b46bfef4de950a795daa26bd777387
Author: Volker Lendecke <v...@samba.org>
Date:   Thu Jul 17 11:55:33 2014 +0000

    messaging3: I don't see 2 versions running concurrently...
    
    Signed-off-by: Volker Lendecke <v...@samba.org>
    Reviewed-by: Jeremy Allison <j...@samba.org>

commit a0efa091464930e21a0dfcd398f582c32492acf5
Author: Volker Lendecke <v...@samba.org>
Date:   Thu Jul 17 11:51:13 2014 +0000

    messaging3: Avoid "enum messaging_type" in messages_dgm
    
    Signed-off-by: Volker Lendecke <v...@samba.org>
    Reviewed-by: Jeremy Allison <j...@samba.org>

commit bab01d1a89c51a1c9ff1bc80bcabf840d5f0308f
Author: Volker Lendecke <v...@samba.org>
Date:   Thu Jul 17 11:47:32 2014 +0000

    messaging3: Pass dir_owner to messaging_dgm_init()

commit f408a428962e6b7ccbbe580ac853cbba283b3c8f
Author: Volker Lendecke <v...@samba.org>
Date:   Thu Jul 17 11:38:36 2014 +0000

    messaging3: Pass cache_dir to messaging_dgm_init()
    
    Signed-off-by: Volker Lendecke <v...@samba.org>
    Reviewed-by: Jeremy Allison <j...@samba.org>

commit 99e288c22b8b1136ec639145f156527a44811e64
Author: Volker Lendecke <v...@samba.org>
Date:   Thu Jul 17 11:34:24 2014 +0000

    messaging3: Move [un]become_root() calls out of messaging_dgm_send()
    
    Signed-off-by: Volker Lendecke <v...@samba.org>
    Reviewed-by: Jeremy Allison <j...@samba.org>

commit 7c6a6cd5c8d4e561cb11bf37267295abaf9ed9d6
Author: Volker Lendecke <v...@samba.org>
Date:   Thu Jul 17 11:33:26 2014 +0000

    messaging3: Move sec_init() call out of messaging_dgm_init()
    
    Signed-off-by: Volker Lendecke <v...@samba.org>
    Reviewed-by: Jeremy Allison <j...@samba.org>

commit 3aa3c6ed012dd344cbc91ac49870c6adc4c3d891
Author: Volker Lendecke <v...@samba.org>
Date:   Thu Jul 17 11:23:46 2014 +0000

    messaging3: Directly refer to messaging_dgm in messages.c
    
    This removes the messaging_backend abstraction layer from messages_dgm.c. 
That
    layer was introduced for ctdb and is still used there. But as the 
messaging_dgm
    interface is very slim anyway, I don't think directly calling it is too bad.
    
    Why this commit? It is another step towards making messages_dgm
    independent of messages.[ch], thus it might become usable in other
    contexts like ctdb and source4
    
    Signed-off-by: Volker Lendecke <v...@samba.org>
    Reviewed-by: Jeremy Allison <j...@samba.org>

commit 28db680a0df9ce4329668d50ce7ba6d5a7c29761
Author: Volker Lendecke <v...@samba.org>
Date:   Thu Jul 17 11:05:02 2014 +0000

    smbd: Use messaging_cleanup()
    
    Signed-off-by: Volker Lendecke <v...@samba.org>
    Reviewed-by: Jeremy Allison <j...@samba.org>

commit 32d45cf2f3a0d709f35560f9cc119438a0a5b397
Author: Volker Lendecke <v...@samba.org>
Date:   Thu Jul 17 11:01:00 2014 +0000

    messaging3: Add messaging_cleanup
    
    Rename smbcontrol's dgm-cleanup to msg-cleanup. We haven't published
    this UI yet :-)
    
    Signed-off-by: Volker Lendecke <v...@samba.org>
    Reviewed-by: Jeremy Allison <j...@samba.org>

commit dcd3e2f2dc43875aec0448d74b2e040290338426
Author: Volker Lendecke <v...@samba.org>
Date:   Thu Jul 17 09:58:50 2014 +0000

    messaging_dgm: Remove unused "messaging_context"
    
    Signed-off-by: Volker Lendecke <v...@samba.org>
    Reviewed-by: Jeremy Allison <j...@samba.org>

commit 7cb94e686075b9815ca645af4f82d094f91c8573
Author: Volker Lendecke <v...@samba.org>
Date:   Thu Jul 17 09:56:39 2014 +0000

    messaging3: Explicitly pass server_id to messaging_dgm_init
    
    Signed-off-by: Volker Lendecke <v...@samba.org>
    Reviewed-by: Jeremy Allison <j...@samba.org>

commit 706ba593d672775c28b6dfa4bd65fdd1e5a6fb4a
Author: Volker Lendecke <v...@samba.org>
Date:   Thu Jul 17 09:52:28 2014 +0000

    messaging3: Explicitly pass tevent_context to messaging_dgm_init
    
    One dependency less on messaging_context()
    
    Signed-off-by: Volker Lendecke <v...@samba.org>
    Reviewed-by: Jeremy Allison <j...@samba.org>

commit 371e99959cf8c02b3a4e7b6352ed83f1d8b9200d
Author: Volker Lendecke <v...@samba.org>
Date:   Thu Jul 17 09:44:41 2014 +0000

    messaging_dgm: Receive through a cb function
    
    This avoids calling messaging_dispatch_rec directly from messaging_dgm.c
    
    Signed-off-by: Volker Lendecke <v...@samba.org>
    Reviewed-by: Jeremy Allison <j...@samba.org>

-----------------------------------------------------------------------

Summary of changes:
 source3/include/messages.h |   13 +---
 source3/lib/messages.c     |  109 +++++++++++++++++++--------
 source3/lib/messages_dgm.c |  179 +++++++++++++++-----------------------------
 source3/lib/messages_dgm.h |   42 ++++++++++
 source3/smbd/server.c      |    4 +-
 source3/utils/smbcontrol.c |   10 +--
 6 files changed, 188 insertions(+), 169 deletions(-)
 create mode 100644 source3/lib/messages_dgm.h


Changeset truncated at 500 lines:

diff --git a/source3/include/messages.h b/source3/include/messages.h
index b718dd7..7543301 100644
--- a/source3/include/messages.h
+++ b/source3/include/messages.h
@@ -73,15 +73,6 @@ struct messaging_backend {
        void *private_data;
 };
 
-int messaging_dgm_init(struct messaging_context *msg_ctx,
-                      TALLOC_CTX *mem_ctx,
-                      struct messaging_backend **presult);
-int messaging_dgm_cleanup(struct messaging_context *msg_ctx, pid_t pid);
-int messaging_dgm_wipe(struct messaging_context *msg_ctx);
-void *messaging_dgm_register_tevent_context(TALLOC_CTX *mem_ctx,
-                                           struct messaging_context *msg_ctx,
-                                           struct tevent_context *ev);
-
 NTSTATUS messaging_ctdbd_init(struct messaging_context *msg_ctx,
                              TALLOC_CTX *mem_ctx,
                              struct messaging_backend **presult);
@@ -97,8 +88,6 @@ struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
 struct server_id messaging_server_id(const struct messaging_context *msg_ctx);
 struct tevent_context *messaging_tevent_context(
        struct messaging_context *msg_ctx);
-struct messaging_backend *messaging_local_backend(
-       struct messaging_context *msg_ctx);
 
 /*
  * re-init after a fork
@@ -143,6 +132,8 @@ struct tevent_req *messaging_read_send(TALLOC_CTX *mem_ctx,
 int messaging_read_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
                        struct messaging_rec **presult);
 
+int messaging_cleanup(struct messaging_context *msg_ctx, pid_t pid);
+
 bool messaging_parent_dgm_cleanup_init(struct messaging_context *msg);
 
 #include "librpc/gen_ndr/ndr_messaging.h"
diff --git a/source3/lib/messages.c b/source3/lib/messages.c
index bbc5183..25d3f01 100644
--- a/source3/lib/messages.c
+++ b/source3/lib/messages.c
@@ -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,13 +73,16 @@ struct messaging_context {
        struct tevent_req **waiters;
        unsigned num_waiters;
 
-       struct messaging_backend *local;
-       struct messaging_backend *remote;
+       struct messaging_dgm_context *local;
 
-       bool *have_context;
+       struct messaging_backend *remote;
 };
 
-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.
@@ -201,19 +205,47 @@ bool message_send_all(struct messaging_context *msg_ctx,
        return true;
 }
 
+static void messaging_recv_cb(const uint8_t *msg, size_t msg_len,
+                             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;
+
+       if (msg_len < sizeof(*hdr)) {
+               DEBUG(1, ("message too short: %u\n", (unsigned)msg_len));
+               return;
+       }
+
+       /*
+        * messages_dgm guarantees alignment, so we can cast here
+        */
+       hdr = (const struct messaging_hdr *)msg;
+
+       DEBUG(10, ("%s: Received message 0x%x len %u from %s\n", __func__,
+                  (unsigned)hdr->msg_type, (unsigned)(msg_len - sizeof(*hdr)),
+                  server_id_str_buf(hdr->src, &idbuf)));
+
+       rec = (struct messaging_rec) {
+               .msg_version = MESSAGE_VERSION,
+               .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)
+       };
+
+       messaging_dispatch_rec(msg_ctx, &rec);
+}
+
 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;
@@ -221,9 +253,12 @@ struct messaging_context *messaging_init(TALLOC_CTX 
*mem_ctx,
 
        ctx->id = procid_self();
        ctx->event_ctx = ev;
-       ctx->have_context = &have_context;
 
-       ret = messaging_dgm_init(ctx, ctx, &ctx->local);
+       sec_init();
+
+       ret = messaging_dgm_init(ctx, ctx->event_ctx, ctx->id,
+                                lp_cache_directory(), sec_initial_uid(),
+                                messaging_recv_cb, ctx, &ctx->local);
 
        if (ret != 0) {
                DEBUG(2, ("messaging_dgm_init failed: %s\n", strerror(ret)));
@@ -251,19 +286,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;
@@ -281,7 +306,10 @@ NTSTATUS messaging_reinit(struct messaging_context 
*msg_ctx)
 
        msg_ctx->id = procid_self();
 
-       ret = messaging_dgm_init(msg_ctx, msg_ctx, &msg_ctx->local);
+       ret = messaging_dgm_init(msg_ctx, msg_ctx->event_ctx, msg_ctx->id,
+                                lp_cache_directory(), sec_initial_uid(),
+                                messaging_recv_cb, msg_ctx,
+                                &msg_ctx->local);
        if (ret != 0) {
                DEBUG(0, ("messaging_dgm_init failed: %s\n", strerror(errno)));
                return map_nt_error_from_unix(ret);
@@ -410,6 +438,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;
@@ -444,8 +474,18 @@ NTSTATUS messaging_send_iov(struct messaging_context 
*msg_ctx,
                return NT_STATUS_OK;
        }
 
-       ret = msg_ctx->local->send_fn(msg_ctx->id, server, msg_type,
-                                     iov, iovlen, msg_ctx->local);
+       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, server.pid, iov2, iovlen+1);
+       unbecome_root();
+
        if (ret != 0) {
                return map_nt_error_from_unix(ret);
        }
@@ -512,7 +552,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, ev);
+               state, msg_ctx->local, ev);
        if (tevent_req_nomem(state, req)) {
                return tevent_req_post(req, ev);
        }
@@ -875,7 +915,7 @@ static int mess_parent_dgm_cleanup(void *private_data)
                private_data, struct messaging_context);
        int ret;
 
-       ret = messaging_dgm_wipe(msg_ctx);
+       ret = messaging_dgm_wipe(msg_ctx->local);
        DEBUG(10, ("messaging_dgm_wipe returned %s\n",
                   ret ? strerror(ret) : "ok"));
        return lp_parm_int(-1, "messaging", "messaging dgm cleanup interval",
@@ -904,10 +944,17 @@ static void mess_parent_dgm_cleanup_done(struct 
tevent_req *req)
        tevent_req_set_callback(req, mess_parent_dgm_cleanup_done, msg);
 }
 
-struct messaging_backend *messaging_local_backend(
-       struct messaging_context *msg_ctx)
+int messaging_cleanup(struct messaging_context *msg_ctx, pid_t pid)
 {
-       return msg_ctx->local;
+       int ret;
+
+       if (pid == 0) {
+               ret = messaging_dgm_wipe(msg_ctx->local);
+       } else {
+               ret = messaging_dgm_cleanup(msg_ctx->local, pid);
+       }
+
+       return ret;
 }
 
 struct tevent_context *messaging_tevent_context(
diff --git a/source3/lib/messages_dgm.c b/source3/lib/messages_dgm.c
index df34d53..2c43ec3 100644
--- a/source3/lib/messages_dgm.c
+++ b/source3/lib/messages_dgm.c
@@ -22,32 +22,27 @@
 #include "lib/util/debug.h"
 #include "lib/unix_msg/unix_msg.h"
 #include "system/filesys.h"
-#include "messages.h"
+#include "lib/messages_dgm.h"
 #include "lib/param/param.h"
 #include "poll_funcs/poll_funcs_tevent.h"
 #include "unix_msg/unix_msg.h"
-#include "librpc/gen_ndr/messaging.h"
 
 struct messaging_dgm_context {
-       struct messaging_context *msg_ctx;
+       pid_t pid;
        struct poll_funcs *msg_callbacks;
        void *tevent_handle;
        struct unix_msg_ctx *dgm_ctx;
        char *cache_dir;
        int lockfile_fd;
-};
 
-struct messaging_dgm_hdr {
-       uint32_t msg_version;
-       enum messaging_type msg_type;
-       struct server_id dst;
-       struct server_id src;
+       void (*recv_cb)(const uint8_t *msg,
+                       size_t msg_len,
+                       void *private_data);
+       void *recv_cb_private_data;
+
+       bool *have_dgm_context;
 };
 
-static int messaging_dgm_send(struct server_id src,
-                             struct server_id pid, int msg_type,
-                             const struct iovec *iov, int iovlen,
-                             struct messaging_backend *backend);
 static void messaging_dgm_recv(struct unix_msg_ctx *ctx,
                               uint8_t *msg, size_t msg_len,
                               void *private_data);
@@ -55,14 +50,15 @@ static void messaging_dgm_recv(struct unix_msg_ctx *ctx,
 static int messaging_dgm_context_destructor(struct messaging_dgm_context *c);
 
 static int messaging_dgm_lockfile_create(TALLOC_CTX *tmp_ctx,
-                                        const char *cache_dir, pid_t pid,
+                                        const char *cache_dir,
+                                        uid_t dir_owner, pid_t pid,
                                         int *plockfile_fd, uint64_t unique)
 {
        fstring buf;
        char *dir;
        char *lockfile_name;
        int lockfile_fd;
-       struct flock lck = {};
+       struct flock lck;
        int unique_len, ret;
        ssize_t written;
        bool ok;
@@ -72,7 +68,7 @@ static int messaging_dgm_lockfile_create(TALLOC_CTX *tmp_ctx,
                return ENOMEM;
        }
 
-       ok = directory_create_or_exist_strict(dir, sec_initial_uid(), 0755);
+       ok = directory_create_or_exist_strict(dir, dir_owner, 0755);
        if (!ok) {
                ret = errno;
                DEBUG(1, ("%s: Could not create lock directory: %s\n",
@@ -98,10 +94,10 @@ static int messaging_dgm_lockfile_create(TALLOC_CTX 
*tmp_ctx,
                goto fail_free;
        }
 
-       lck.l_type = F_WRLCK;
-       lck.l_whence = SEEK_SET;
-       lck.l_start = 0;
-       lck.l_len = 0;
+       lck = (struct flock) {
+               .l_type = F_WRLCK,
+               .l_whence = SEEK_SET
+       };
 
        ret = fcntl(lockfile_fd, F_SETLK, &lck);
        if (ret == -1) {
@@ -165,38 +161,37 @@ static int messaging_dgm_lockfile_remove(TALLOC_CTX 
*tmp_ctx,
        return ret;
 }
 
-int messaging_dgm_init(struct messaging_context *msg_ctx,
-                      TALLOC_CTX *mem_ctx,
-                      struct messaging_backend **presult)
+int messaging_dgm_init(TALLOC_CTX *mem_ctx,
+                      struct tevent_context *ev,
+                      struct server_id pid,
+                      const char *cache_dir,
+                      uid_t dir_owner,
+                      void (*recv_cb)(const uint8_t *msg,
+                                      size_t msg_len,
+                                      void *private_data),
+                      void *recv_cb_private_data,
+                      struct messaging_dgm_context **pctx)
 {
-       struct messaging_backend *result;
        struct messaging_dgm_context *ctx;
-       struct server_id pid = messaging_server_id(msg_ctx);
        int ret;
        bool ok;
-       const char *cache_dir;
        char *socket_dir;
        struct sockaddr_un socket_address;
        size_t sockname_len;
        uint64_t cookie;
+       static bool have_dgm_context = false;
 
-       cache_dir = lp_cache_directory();
-       if (cache_dir == NULL) {
-               return errno;
+       if (have_dgm_context) {
+               return EEXIST;
        }
 
-       result = talloc(mem_ctx, struct messaging_backend);
-       if (result == NULL) {
-               goto fail_nomem;
-       }
-       ctx = talloc_zero(result, struct messaging_dgm_context);
+       ctx = talloc_zero(mem_ctx, struct messaging_dgm_context);
        if (ctx == NULL) {
                goto fail_nomem;
        }
-
-       result->private_data = ctx;
-       result->send_fn = messaging_dgm_send;
-       ctx->msg_ctx = msg_ctx;
+       ctx->pid = pid.pid;
+       ctx->recv_cb = recv_cb;
+       ctx->recv_cb_private_data = recv_cb_private_data;
 
        ctx->cache_dir = talloc_strdup(ctx, cache_dir);
        if (ctx->cache_dir == NULL) {
@@ -212,18 +207,16 @@ int messaging_dgm_init(struct messaging_context *msg_ctx,
                                sizeof(socket_address.sun_path),
                                "%s/%u", socket_dir, (unsigned)pid.pid);
        if (sockname_len >= sizeof(socket_address.sun_path)) {
-               TALLOC_FREE(result);
+               TALLOC_FREE(ctx);
                return ENAMETOOLONG;
        }
 
-       sec_init();
-
-       ret = messaging_dgm_lockfile_create(ctx, cache_dir, pid.pid,
+       ret = messaging_dgm_lockfile_create(ctx, cache_dir, dir_owner, pid.pid,
                                            &ctx->lockfile_fd, pid.unique_id);
        if (ret != 0) {
                DEBUG(1, ("%s: messaging_dgm_create_lockfile failed: %s\n",
                          __func__, strerror(ret)));
-               TALLOC_FREE(result);
+               TALLOC_FREE(ctx);
                return ret;
        }
 
@@ -233,17 +226,15 @@ int messaging_dgm_init(struct messaging_context *msg_ctx,
        }
 
        ctx->tevent_handle = poll_funcs_tevent_register(
-               ctx, ctx->msg_callbacks,
-               messaging_tevent_context(msg_ctx));
+               ctx, ctx->msg_callbacks, ev);
        if (ctx->tevent_handle == NULL) {
                goto fail_nomem;
        }
 
-       ok = directory_create_or_exist_strict(socket_dir, sec_initial_uid(),
-                                             0700);
+       ok = directory_create_or_exist_strict(socket_dir, dir_owner, 0700);
        if (!ok) {
                DEBUG(1, ("Could not create socket directory\n"));
-               TALLOC_FREE(result);
+               TALLOC_FREE(ctx);
                return EACCES;
        }
        TALLOC_FREE(socket_dir);
@@ -256,46 +247,44 @@ int messaging_dgm_init(struct messaging_context *msg_ctx,
                            messaging_dgm_recv, ctx, &ctx->dgm_ctx);
        if (ret != 0) {
                DEBUG(1, ("unix_msg_init failed: %s\n", strerror(ret)));
-               TALLOC_FREE(result);
+               TALLOC_FREE(ctx);
                return ret;
        }
        talloc_set_destructor(ctx, messaging_dgm_context_destructor);
 
-       *presult = result;
+       ctx->have_dgm_context = &have_dgm_context;
+
+       *pctx = ctx;
        return 0;
 
 fail_nomem:
-       TALLOC_FREE(result);
+       TALLOC_FREE(ctx);
        return ENOMEM;
 }
 
 static int messaging_dgm_context_destructor(struct messaging_dgm_context *c)
 {
-       struct server_id pid = messaging_server_id(c->msg_ctx);
-
        /*
         * First delete the socket to avoid races. The lockfile is the
         * indicator that we're still around.
         */
        unix_msg_free(c->dgm_ctx);
 
-       if (getpid() == pid.pid) {
-               (void)messaging_dgm_lockfile_remove(c, c->cache_dir, pid.pid);
+       if (getpid() == c->pid) {
+               (void)messaging_dgm_lockfile_remove(c, c->cache_dir, c->pid);
        }
        close(c->lockfile_fd);
+
+       if (c->have_dgm_context != NULL) {
+               *c->have_dgm_context = false;
+       }
+
        return 0;
 }
 
-static int messaging_dgm_send(struct server_id src,
-                             struct server_id pid, int msg_type,
-                             const struct iovec *iov, int iovlen,
-                             struct messaging_backend *backend)
+int messaging_dgm_send(struct messaging_dgm_context *ctx, pid_t pid,
+                      const struct iovec *iov, int iovlen)
 {
-       struct messaging_dgm_context *ctx = talloc_get_type_abort(
-               backend->private_data, struct messaging_dgm_context);
-       struct messaging_dgm_hdr hdr;
-       struct iovec iov2[iovlen + 1];
-       struct server_id_buf idbuf;
        struct sockaddr_un dst;
        ssize_t dst_pathlen;
        int ret;
@@ -303,27 +292,14 @@ static int messaging_dgm_send(struct server_id src,
        dst = (struct sockaddr_un) { .sun_family = AF_UNIX };
 
        dst_pathlen = snprintf(dst.sun_path, sizeof(dst.sun_path),
-                              "%s/msg/%u", ctx->cache_dir, (unsigned)pid.pid);
+                              "%s/msg/%u", ctx->cache_dir, (unsigned)pid);
        if (dst_pathlen >= sizeof(dst.sun_path)) {
                return ENAMETOOLONG;
        }
 
-       hdr.msg_version = MESSAGE_VERSION;


-- 
Samba Shared Repository

Reply via email to