lib: Simplify iov_buf[len]
[obnox/samba/samba-obnox.git] / source3 / lib / messages_ctdbd.c
index f1a02e6af9fdb3d6cc8ff86c2c888b17b531b7b5..53aeb1fd057ab62563d09be46a30658670e53178 100644 (file)
@@ -2,26 +2,44 @@
    Unix SMB/CIFS implementation.
    Samba internal messaging functions
    Copyright (C) 2007 by Volker Lendecke
-   
+
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
-   
+
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
-   
+
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
+#include "messages.h"
+#include "util_tdb.h"
+#include "lib/iov_buf.h"
+
+/*
+ * It is not possible to include ctdb.h and tdb_compat.h (included via
+ * some other include above) without warnings. This fixes those
+ * warnings.
+ */
 
-#ifdef CLUSTER_SUPPORT
+#ifdef typesafe_cb
+#undef typesafe_cb
+#endif
+
+#ifdef typesafe_cb_preargs
+#undef typesafe_cb_preargs
+#endif
+
+#ifdef typesafe_cb_postargs
+#undef typesafe_cb_postargs
+#endif
 
-#include "librpc/gen_ndr/messaging.h"
 #include "ctdb.h"
 #include "ctdb_private.h"
 #include "ctdbd_conn.h"
@@ -35,30 +53,87 @@ struct messaging_ctdbd_context {
  * This is a Samba3 hack/optimization. Routines like process_exists need to
  * talk to ctdbd, and they don't get handed a messaging context.
  */
-struct ctdbd_connection *global_ctdbd_connection;
+static struct ctdbd_connection *global_ctdbd_connection;
+static int global_ctdb_connection_pid;
 
 struct ctdbd_connection *messaging_ctdbd_connection(void)
 {
+       if (!lp_clustering()) {
+               return NULL;
+       }
+
+       if (global_ctdb_connection_pid == 0 &&
+           global_ctdbd_connection == NULL) {
+               struct tevent_context *ev;
+               struct messaging_context *msg;
+
+               ev = samba_tevent_context_init(NULL);
+               if (!ev) {
+                       DEBUG(0,("samba_tevent_context_init failed\n"));
+               }
+
+               msg = messaging_init(NULL, ev);
+               if (!msg) {
+                       DEBUG(0,("messaging_init failed\n"));
+                       return NULL;
+               }
+       }
+
+       if (global_ctdb_connection_pid != getpid()) {
+               DEBUG(0,("messaging_ctdbd_connection():"
+                        "valid for pid[%jd] but it's [%jd]\n",
+                        (intmax_t)global_ctdb_connection_pid,
+                        (intmax_t)getpid()));
+               smb_panic("messaging_ctdbd_connection() invalid process\n");
+       }
+
        return global_ctdbd_connection;
 }
 
-static NTSTATUS messaging_ctdb_send(struct messaging_context *msg_ctx,
-                                   struct server_id pid, int msg_type,
-                                   const DATA_BLOB *data,
-                                   struct messaging_backend *backend)
+static int messaging_ctdb_send(struct server_id src,
+                              struct server_id pid, int msg_type,
+                              const struct iovec *iov, int iovlen,
+                              const int *fds, size_t num_fds,
+                              struct messaging_backend *backend)
 {
        struct messaging_ctdbd_context *ctx = talloc_get_type_abort(
                backend->private_data, struct messaging_ctdbd_context);
-
        struct messaging_rec msg;
+       uint8_t *buf;
+       ssize_t buflen;
+       NTSTATUS status;
 
-       msg.msg_version = MESSAGE_VERSION;
-       msg.msg_type    = msg_type;
-       msg.dest        = pid;
-       msg.src         = procid_self();
-       msg.buf         = *data;
+       if (num_fds > 0) {
+               return ENOSYS;
+       }
+
+       buflen = iov_buflen(iov, iovlen);
+       if (buflen == -1) {
+               return EMSGSIZE;
+       }
+
+       buf = talloc_array(talloc_tos(), uint8_t, buflen);
+       if (buflen == NULL) {
+               return ENOMEM;
+       }
+       iov_buf(iov, iovlen, buf, buflen);
 
-       return ctdbd_messaging_send(ctx->conn, pid.vnn, pid.pid, &msg);
+       msg = (struct messaging_rec) {
+               .msg_version    = MESSAGE_VERSION,
+               .msg_type       = msg_type,
+               .dest           = pid,
+               .src            = src,
+               .buf            = data_blob_const(buf, talloc_get_size(buf)),
+       };
+
+       status = ctdbd_messaging_send(ctx->conn, pid.vnn, pid.pid, &msg);
+
+       TALLOC_FREE(buf);
+
+       if (NT_STATUS_IS_OK(status)) {
+               return 0;
+       }
+       return map_errno_from_nt_status(status);
 }
 
 static int messaging_ctdbd_destructor(struct messaging_ctdbd_context *ctx)
@@ -66,6 +141,7 @@ static int messaging_ctdbd_destructor(struct messaging_ctdbd_context *ctx)
        /*
         * The global connection just went away
         */
+       global_ctdb_connection_pid = 0;
        global_ctdbd_connection = NULL;
        return 0;
 }
@@ -78,12 +154,12 @@ NTSTATUS messaging_ctdbd_init(struct messaging_context *msg_ctx,
        struct messaging_ctdbd_context *ctx;
        NTSTATUS status;
 
-       if (!(result = TALLOC_P(mem_ctx, struct messaging_backend))) {
+       if (!(result = talloc(mem_ctx, struct messaging_backend))) {
                DEBUG(0, ("talloc failed\n"));
                return NT_STATUS_NO_MEMORY;
        }
 
-       if (!(ctx = TALLOC_P(result, struct messaging_ctdbd_context))) {
+       if (!(ctx = talloc(result, struct messaging_ctdbd_context))) {
                DEBUG(0, ("talloc failed\n"));
                TALLOC_FREE(result);
                return NT_STATUS_NO_MEMORY;
@@ -107,6 +183,7 @@ NTSTATUS messaging_ctdbd_init(struct messaging_context *msg_ctx,
                return status;
        }
 
+       global_ctdb_connection_pid = getpid();
        global_ctdbd_connection = ctx->conn;
        talloc_set_destructor(ctx, messaging_ctdbd_destructor);
 
@@ -118,14 +195,3 @@ NTSTATUS messaging_ctdbd_init(struct messaging_context *msg_ctx,
        *presult = result;
        return NT_STATUS_OK;
 }
-
-#else
-
-NTSTATUS messaging_ctdbd_init(struct messaging_context *msg_ctx,
-                             TALLOC_CTX *mem_ctx,
-                             struct messaging_backend **presult)
-{
-       return NT_STATUS_NOT_IMPLEMENTED;
-}
-
-#endif