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/>.
*/
@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
#include "librpc/gen_ndr/messaging.h"
#include "librpc/gen_ndr/ndr_messaging.h"
-static sig_atomic_t received_signal;
+struct messaging_tdb_context {
+ struct messaging_context *msg_ctx;
+ struct tdb_wrap *tdb;
+ struct tevent_signal *se;
+ int received_messages;
+};
static NTSTATUS messaging_tdb_send(struct messaging_context *msg_ctx,
struct server_id pid, int msg_type,
const DATA_BLOB *data,
struct messaging_backend *backend);
+static void message_dispatch(struct messaging_context *msg_ctx);
-/****************************************************************************
- Notifications come in as signals.
-****************************************************************************/
-
-static void sig_usr1(void)
+static void messaging_tdb_signal_handler(struct tevent_context *ev_ctx,
+ struct tevent_signal *se,
+ int signum, int count,
+ void *_info, void *private_data)
{
- received_signal = 1;
- sys_select_signal(SIGUSR1);
-}
+ struct messaging_tdb_context *ctx = talloc_get_type(private_data,
+ struct messaging_tdb_context);
-static int messaging_tdb_destructor(struct messaging_backend *tdb_ctx)
-{
- TDB_CONTEXT *tdb = (TDB_CONTEXT *)tdb_ctx->private_data;
- tdb_close(tdb);
- return 0;
+ ctx->received_messages++;
+
+ DEBUG(10, ("messaging_tdb_signal_handler: sig[%d] count[%d] msgs[%d]\n",
+ signum, count, ctx->received_messages));
+
+ message_dispatch(ctx->msg_ctx);
}
/****************************************************************************
struct messaging_backend **presult)
{
struct messaging_backend *result;
- TDB_CONTEXT *tdb;
+ struct messaging_tdb_context *ctx;
if (!(result = TALLOC_P(mem_ctx, struct messaging_backend))) {
DEBUG(0, ("talloc failed\n"));
return NT_STATUS_NO_MEMORY;
}
- tdb = tdb_open_log(lock_path("messages.tdb"),
- 0, TDB_CLEAR_IF_FIRST|TDB_DEFAULT,
- O_RDWR|O_CREAT,0600);
+ ctx = TALLOC_ZERO_P(result, struct messaging_tdb_context);
+ if (!ctx) {
+ DEBUG(0, ("talloc failed\n"));
+ TALLOC_FREE(result);
+ return NT_STATUS_NO_MEMORY;
+ }
+ result->private_data = ctx;
+ result->send_fn = messaging_tdb_send;
- if (!tdb) {
+ 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_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: "
"%s\n", strerror(errno)));
return status;
}
- sec_init();
+ ctx->se = tevent_add_signal(msg_ctx->event_ctx,
+ ctx,
+ SIGUSR1, 0,
+ messaging_tdb_signal_handler,
+ ctx);
+ if (!ctx->se) {
+ NTSTATUS status = map_nt_error_from_unix(errno);
+ DEBUG(0, ("ERROR: Failed to initialise messages signal handler: "
+ "%s\n", strerror(errno)));
+ TALLOC_FREE(result);
+ return status;
+ }
- /* Activate the per-hashchain freelist */
- tdb_set_max_dead(tdb, 5);
+ sec_init();
- CatchSignal(SIGUSR1, SIGNAL_CAST sig_usr1);
+ *presult = result;
+ return NT_STATUS_OK;
+}
- result->private_data = (void *)tdb;
- result->send_fn = messaging_tdb_send;
+bool messaging_tdb_parent_init(TALLOC_CTX *mem_ctx)
+{
+ struct tdb_wrap *db;
- talloc_set_destructor(result, messaging_tdb_destructor);
+ /*
+ * Open the tdb in the parent process (smbd) so that our
+ * CLEAR_IF_FIRST optimization in tdb_reopen_all can properly
+ * work.
+ */
- *presult = result;
- return NT_STATUS_OK;
+ 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;
}
/*******************************************************************
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;
return NT_STATUS_NO_MEMORY;
}
- ndr_err = ndr_push_struct_blob(
- &blob, mem_ctx, 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)) {
const DATA_BLOB *data,
struct messaging_backend *backend)
{
+ struct messaging_tdb_context *ctx = talloc_get_type(backend->private_data,
+ struct messaging_tdb_context);
struct messaging_array *msg_array;
struct messaging_rec *rec;
NTSTATUS status;
TDB_DATA key;
- TDB_CONTEXT *tdb = (TDB_CONTEXT *)backend->private_data;
+ struct tdb_wrap *tdb = ctx->tdb;
TALLOC_CTX *frame = talloc_stackframe();
/* NULL pointer means implicit length zero. */
key = message_key_pid(frame, pid);
- if (tdb_chainlock(tdb, key) == -1) {
+ if (tdb_chainlock(tdb->tdb, key) == -1) {
TALLOC_FREE(frame);
return NT_STATUS_LOCK_NOT_GRANTED;
}
- status = messaging_tdb_fetch(tdb, key, talloc_tos(), &msg_array);
+ status = messaging_tdb_fetch(tdb->tdb, key, talloc_tos(), &msg_array);
if (!NT_STATUS_IS_OK(status)) {
goto done;
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;
msg_array->num_messages += 1;
- status = messaging_tdb_store(tdb, key, msg_array);
+ status = messaging_tdb_store(tdb->tdb, key, msg_array);
if (!NT_STATUS_IS_OK(status)) {
goto done;
}
-
+
status = message_notify(pid);
if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
DEBUG(2, ("pid %s doesn't exist - deleting messages record\n",
procid_str_static(&pid)));
- tdb_delete(tdb, message_key_pid(talloc_tos(), pid));
+ tdb_delete(tdb->tdb, message_key_pid(talloc_tos(), pid));
}
done:
- tdb_chainunlock(tdb, key);
+ tdb_chainunlock(tdb->tdb, key);
TALLOC_FREE(frame);
return status;
}
/****************************************************************************
- 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) {
messages on an *odd* byte boundary.
****************************************************************************/
-void message_dispatch(struct messaging_context *msg_ctx)
+static void message_dispatch(struct messaging_context *msg_ctx)
{
+ struct messaging_tdb_context *ctx = talloc_get_type(msg_ctx->local->private_data,
+ struct messaging_tdb_context);
struct messaging_array *msg_array = NULL;
- TDB_CONTEXT *tdb = (TDB_CONTEXT *)(msg_ctx->local->private_data);
+ struct tdb_wrap *tdb = ctx->tdb;
+ NTSTATUS status;
uint32 i;
- if (!received_signal)
+ if (ctx->received_messages == 0) {
return;
+ }
- DEBUG(10, ("message_dispatch: received_signal = %d\n",
- received_signal));
-
- received_signal = 0;
+ DEBUG(10, ("message_dispatch: received_messages = %d\n",
+ ctx->received_messages));
- if (!NT_STATUS_IS_OK(retrieve_all_messages(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)));
return;
}
+ ctx->received_messages = 0;
+
for (i=0; i<msg_array->num_messages; i++) {
messaging_dispatch_rec(msg_ctx, &msg_array->messages[i]);
}