s3-talloc Change TALLOC_ZERO_P() to talloc_zero()
[nivanova/samba-autobuild/.git] / source3 / lib / messages_local.c
index 0da05466a2f2ce52287d10eec51722c171d7cbc6..09b8706262127235edc1d5f1d4c548f06cf57827 100644 (file)
@@ -2,17 +2,17 @@
    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/>.
 */
@@ -21,7 +21,7 @@
   @defgroup messages Internal messaging framework
   @{
   @file messages.c
-  
+
   @brief  Module for internal messaging between Samba daemons. 
 
    The idea is that if a part of Samba wants to do communication with
@@ -43,8 +43,9 @@
 */
 
 #include "includes.h"
-#include "librpc/gen_ndr/messaging.h"
-#include "librpc/gen_ndr/ndr_messaging.h"
+#include "system/filesys.h"
+#include "messages.h"
+#include "lib/util/tdb_wrap.h"
 
 struct messaging_tdb_context {
        struct messaging_context *msg_ctx;
@@ -86,12 +87,12 @@ NTSTATUS messaging_tdb_init(struct messaging_context *msg_ctx,
        struct messaging_backend *result;
        struct messaging_tdb_context *ctx;
 
-       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;
        }
 
-       ctx = TALLOC_ZERO_P(result, struct messaging_tdb_context);
+       ctx = talloc_zero(result, struct messaging_tdb_context);
        if (!ctx) {
                DEBUG(0, ("talloc failed\n"));
                TALLOC_FREE(result);
@@ -103,12 +104,12 @@ NTSTATUS messaging_tdb_init(struct messaging_context *msg_ctx,
        ctx->msg_ctx = msg_ctx;
 
        ctx->tdb = tdb_wrap_open(ctx, lock_path("messages.tdb"), 0,
-                                TDB_CLEAR_IF_FIRST|TDB_DEFAULT|TDB_VOLATILE,
+                                TDB_CLEAR_IF_FIRST|TDB_DEFAULT|TDB_VOLATILE|TDB_INCOMPATIBLE_HASH,
                                 O_RDWR|O_CREAT,0600);
 
        if (!ctx->tdb) {
                NTSTATUS status = map_nt_error_from_unix(errno);
-               DEBUG(0, ("ERROR: Failed to initialise messages database: "
+               DEBUG(2, ("ERROR: Failed to initialise messages database: "
                          "%s\n", strerror(errno)));
                TALLOC_FREE(result);
                return status;
@@ -133,6 +134,27 @@ NTSTATUS messaging_tdb_init(struct messaging_context *msg_ctx,
        return NT_STATUS_OK;
 }
 
+bool messaging_tdb_parent_init(TALLOC_CTX *mem_ctx)
+{
+       struct tdb_wrap *db;
+
+       /*
+        * Open the tdb in the parent process (smbd) so that our
+        * CLEAR_IF_FIRST optimization in tdb_reopen_all can properly
+        * work.
+        */
+
+       db = tdb_wrap_open(mem_ctx, lock_path("messages.tdb"), 0,
+                          TDB_CLEAR_IF_FIRST|TDB_DEFAULT|TDB_VOLATILE|TDB_INCOMPATIBLE_HASH,
+                          O_RDWR|O_CREAT,0600);
+       if (db == NULL) {
+               DEBUG(1, ("could not open messaging.tdb: %s\n",
+                         strerror(errno)));
+               return false;
+       }
+       return true;
+}
+
 /*******************************************************************
  Form a static tdb key from a pid.
 ******************************************************************/
@@ -145,7 +167,7 @@ static TDB_DATA message_key_pid(TALLOC_CTX *mem_ctx, struct server_id pid)
        key = talloc_asprintf(talloc_tos(), "PID/%s", procid_str_static(&pid));
 
        SMB_ASSERT(key != NULL);
-       
+
        kbuf.dptr = (uint8 *)key;
        kbuf.dsize = strlen(key)+1;
        return kbuf;
@@ -165,7 +187,7 @@ static NTSTATUS messaging_tdb_fetch(TDB_CONTEXT *msg_tdb,
        DATA_BLOB blob;
        enum ndr_err_code ndr_err;
 
-       if (!(result = TALLOC_ZERO_P(mem_ctx, struct messaging_array))) {
+       if (!(result = talloc_zero(mem_ctx, struct messaging_array))) {
                return NT_STATUS_NO_MEMORY;
        }
 
@@ -179,7 +201,7 @@ static NTSTATUS messaging_tdb_fetch(TDB_CONTEXT *msg_tdb,
        blob = data_blob_const(data.dptr, data.dsize);
 
        ndr_err = ndr_pull_struct_blob(
-               &blob, result, NULL, result,
+               &blob, result, result,
                (ndr_pull_flags_fn_t)ndr_pull_messaging_array);
 
        SAFE_FREE(data.dptr);
@@ -221,8 +243,7 @@ static NTSTATUS messaging_tdb_store(TDB_CONTEXT *msg_tdb,
                return NT_STATUS_NO_MEMORY;
        }
 
-       ndr_err = ndr_push_struct_blob(
-               &blob, mem_ctx, NULL, array,
+       ndr_err = ndr_push_struct_blob(&blob, mem_ctx, array,
                (ndr_push_flags_fn_t)ndr_push_messaging_array);
 
        if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
@@ -350,7 +371,7 @@ static NTSTATUS messaging_tdb_send(struct messaging_context *msg_ctx,
                goto done;
        }
 
-       if (!(rec = TALLOC_REALLOC_ARRAY(talloc_tos(), msg_array->messages,
+       if (!(rec = talloc_realloc(talloc_tos(), msg_array->messages,
                                         struct messaging_rec,
                                         msg_array->num_messages+1))) {
                status = NT_STATUS_NO_MEMORY;
@@ -360,7 +381,7 @@ static NTSTATUS messaging_tdb_send(struct messaging_context *msg_ctx,
        rec[msg_array->num_messages].msg_version = MESSAGE_VERSION;
        rec[msg_array->num_messages].msg_type = msg_type & MSG_TYPE_MASK;
        rec[msg_array->num_messages].dest = pid;
-       rec[msg_array->num_messages].src = procid_self();
+       rec[msg_array->num_messages].src = msg_ctx->id;
        rec[msg_array->num_messages].buf = *data;
 
        msg_array->messages = rec;
@@ -371,7 +392,7 @@ static NTSTATUS messaging_tdb_send(struct messaging_context *msg_ctx,
        if (!NT_STATUS_IS_OK(status)) {
                goto done;
        }
-       
+
        status = message_notify(pid);
 
        if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
@@ -387,15 +408,16 @@ static NTSTATUS messaging_tdb_send(struct messaging_context *msg_ctx,
 }
 
 /****************************************************************************
- Retrieve all messages for the current process.
+ Retrieve all messages for a process.
 ****************************************************************************/
 
 static NTSTATUS retrieve_all_messages(TDB_CONTEXT *msg_tdb,
+                                     struct server_id id,
                                      TALLOC_CTX *mem_ctx,
                                      struct messaging_array **presult)
 {
        struct messaging_array *result;
-       TDB_DATA key = message_key_pid(mem_ctx, procid_self());
+       TDB_DATA key = message_key_pid(mem_ctx, id);
        NTSTATUS status;
 
        if (tdb_chainlock(msg_tdb, key) == -1) {
@@ -443,7 +465,7 @@ static void message_dispatch(struct messaging_context *msg_ctx)
        DEBUG(10, ("message_dispatch: received_messages = %d\n",
                   ctx->received_messages));
 
-       status = retrieve_all_messages(tdb->tdb, NULL, &msg_array);
+       status = retrieve_all_messages(tdb->tdb, msg_ctx->id, NULL, &msg_array);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0, ("message_dispatch: failed to retrieve messages: %s\n",
                           nt_errstr(status)));