[222] | 1 | /*
|
---|
| 2 | Unix SMB/CIFS implementation.
|
---|
| 3 | Samba internal messaging functions
|
---|
| 4 | Copyright (C) Andrew Tridgell 2000
|
---|
| 5 | Copyright (C) 2001 by Martin Pool
|
---|
| 6 | Copyright (C) 2002 by Jeremy Allison
|
---|
| 7 | Copyright (C) 2007 by Volker Lendecke
|
---|
| 8 |
|
---|
| 9 | This program is free software; you can redistribute it and/or modify
|
---|
| 10 | it under the terms of the GNU General Public License as published by
|
---|
| 11 | the Free Software Foundation; either version 3 of the License, or
|
---|
| 12 | (at your option) any later version.
|
---|
| 13 |
|
---|
| 14 | This program is distributed in the hope that it will be useful,
|
---|
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 17 | GNU General Public License for more details.
|
---|
| 18 |
|
---|
| 19 | You should have received a copy of the GNU General Public License
|
---|
| 20 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 21 | */
|
---|
| 22 |
|
---|
| 23 | /**
|
---|
| 24 | @defgroup messages Internal messaging framework
|
---|
| 25 | @{
|
---|
| 26 | @file messages.c
|
---|
| 27 |
|
---|
| 28 | @brief Module for internal messaging between Samba daemons.
|
---|
| 29 |
|
---|
| 30 | The idea is that if a part of Samba wants to do communication with
|
---|
| 31 | another Samba process then it will do a message_register() of a
|
---|
| 32 | dispatch function, and use message_send_pid() to send messages to
|
---|
| 33 | that process.
|
---|
| 34 |
|
---|
| 35 | The dispatch function is given the pid of the sender, and it can
|
---|
| 36 | use that to reply by message_send_pid(). See ping_message() for a
|
---|
| 37 | simple example.
|
---|
| 38 |
|
---|
| 39 | @caution Dispatch functions must be able to cope with incoming
|
---|
| 40 | messages on an *odd* byte boundary.
|
---|
| 41 |
|
---|
| 42 | This system doesn't have any inherent size limitations but is not
|
---|
| 43 | very efficient for large messages or when messages are sent in very
|
---|
| 44 | quick succession.
|
---|
| 45 |
|
---|
| 46 | */
|
---|
| 47 |
|
---|
| 48 | #include "includes.h"
|
---|
| 49 | #include "librpc/gen_ndr/messaging.h"
|
---|
| 50 | #include "librpc/gen_ndr/ndr_messaging.h"
|
---|
| 51 |
|
---|
| 52 | struct messaging_callback {
|
---|
| 53 | struct messaging_callback *prev, *next;
|
---|
| 54 | uint32 msg_type;
|
---|
| 55 | void (*fn)(struct messaging_context *msg, void *private_data,
|
---|
| 56 | uint32_t msg_type,
|
---|
| 57 | struct server_id server_id, DATA_BLOB *data);
|
---|
| 58 | void *private_data;
|
---|
| 59 | };
|
---|
| 60 |
|
---|
| 61 | /****************************************************************************
|
---|
| 62 | A useful function for testing the message system.
|
---|
| 63 | ****************************************************************************/
|
---|
| 64 |
|
---|
| 65 | static void ping_message(struct messaging_context *msg_ctx,
|
---|
| 66 | void *private_data,
|
---|
| 67 | uint32_t msg_type,
|
---|
| 68 | struct server_id src,
|
---|
| 69 | DATA_BLOB *data)
|
---|
| 70 | {
|
---|
| 71 | const char *msg = data->data ? (const char *)data->data : "none";
|
---|
| 72 |
|
---|
| 73 | DEBUG(1,("INFO: Received PING message from PID %s [%s]\n",
|
---|
| 74 | procid_str_static(&src), msg));
|
---|
| 75 | messaging_send(msg_ctx, src, MSG_PONG, data);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | /****************************************************************************
|
---|
| 79 | Register/replace a dispatch function for a particular message type.
|
---|
| 80 | JRA changed Dec 13 2006. Only one message handler now permitted per type.
|
---|
| 81 | *NOTE*: Dispatch functions must be able to cope with incoming
|
---|
| 82 | messages on an *odd* byte boundary.
|
---|
| 83 | ****************************************************************************/
|
---|
| 84 |
|
---|
| 85 | struct msg_all {
|
---|
| 86 | struct messaging_context *msg_ctx;
|
---|
| 87 | int msg_type;
|
---|
| 88 | uint32 msg_flag;
|
---|
| 89 | const void *buf;
|
---|
| 90 | size_t len;
|
---|
| 91 | int n_sent;
|
---|
| 92 | };
|
---|
| 93 |
|
---|
| 94 | /****************************************************************************
|
---|
| 95 | Send one of the messages for the broadcast.
|
---|
| 96 | ****************************************************************************/
|
---|
| 97 |
|
---|
| 98 | static int traverse_fn(struct db_record *rec,
|
---|
| 99 | const struct connections_key *ckey,
|
---|
| 100 | const struct connections_data *crec,
|
---|
| 101 | void *state)
|
---|
| 102 | {
|
---|
| 103 | struct msg_all *msg_all = (struct msg_all *)state;
|
---|
| 104 | NTSTATUS status;
|
---|
| 105 |
|
---|
| 106 | if (crec->cnum != -1)
|
---|
| 107 | return 0;
|
---|
| 108 |
|
---|
| 109 | /* Don't send if the receiver hasn't registered an interest. */
|
---|
| 110 |
|
---|
| 111 | if(!(crec->bcast_msg_flags & msg_all->msg_flag))
|
---|
| 112 | return 0;
|
---|
| 113 |
|
---|
| 114 | /* If the msg send fails because the pid was not found (i.e. smbd died),
|
---|
| 115 | * the msg has already been deleted from the messages.tdb.*/
|
---|
| 116 |
|
---|
| 117 | status = messaging_send_buf(msg_all->msg_ctx,
|
---|
| 118 | crec->pid, msg_all->msg_type,
|
---|
| 119 | (uint8 *)msg_all->buf, msg_all->len);
|
---|
| 120 |
|
---|
| 121 | if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
|
---|
| 122 |
|
---|
| 123 | /* If the pid was not found delete the entry from connections.tdb */
|
---|
| 124 |
|
---|
| 125 | DEBUG(2,("pid %s doesn't exist - deleting connections %d [%s]\n",
|
---|
| 126 | procid_str_static(&crec->pid), crec->cnum,
|
---|
| 127 | crec->servicename));
|
---|
| 128 |
|
---|
| 129 | rec->delete_rec(rec);
|
---|
| 130 | }
|
---|
| 131 | msg_all->n_sent++;
|
---|
| 132 | return 0;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | /**
|
---|
| 136 | * Send a message to all smbd processes.
|
---|
| 137 | *
|
---|
| 138 | * It isn't very efficient, but should be OK for the sorts of
|
---|
| 139 | * applications that use it. When we need efficient broadcast we can add
|
---|
| 140 | * it.
|
---|
| 141 | *
|
---|
| 142 | * @param n_sent Set to the number of messages sent. This should be
|
---|
| 143 | * equal to the number of processes, but be careful for races.
|
---|
| 144 | *
|
---|
| 145 | * @retval True for success.
|
---|
| 146 | **/
|
---|
| 147 | bool message_send_all(struct messaging_context *msg_ctx,
|
---|
| 148 | int msg_type,
|
---|
| 149 | const void *buf, size_t len,
|
---|
| 150 | int *n_sent)
|
---|
| 151 | {
|
---|
| 152 | struct msg_all msg_all;
|
---|
| 153 |
|
---|
| 154 | msg_all.msg_type = msg_type;
|
---|
| 155 | if (msg_type < 1000)
|
---|
| 156 | msg_all.msg_flag = FLAG_MSG_GENERAL;
|
---|
| 157 | else if (msg_type > 1000 && msg_type < 2000)
|
---|
| 158 | msg_all.msg_flag = FLAG_MSG_NMBD;
|
---|
| 159 | else if (msg_type > 2000 && msg_type < 2100)
|
---|
| 160 | msg_all.msg_flag = FLAG_MSG_PRINT_NOTIFY;
|
---|
| 161 | else if (msg_type > 2100 && msg_type < 3000)
|
---|
| 162 | msg_all.msg_flag = FLAG_MSG_PRINT_GENERAL;
|
---|
| 163 | else if (msg_type > 3000 && msg_type < 4000)
|
---|
| 164 | msg_all.msg_flag = FLAG_MSG_SMBD;
|
---|
| 165 | else if (msg_type > 4000 && msg_type < 5000)
|
---|
| 166 | msg_all.msg_flag = FLAG_MSG_DBWRAP;
|
---|
| 167 | else
|
---|
| 168 | return False;
|
---|
| 169 |
|
---|
| 170 | msg_all.buf = buf;
|
---|
| 171 | msg_all.len = len;
|
---|
| 172 | msg_all.n_sent = 0;
|
---|
| 173 | msg_all.msg_ctx = msg_ctx;
|
---|
| 174 |
|
---|
| 175 | connections_forall(traverse_fn, &msg_all);
|
---|
| 176 | if (n_sent)
|
---|
| 177 | *n_sent = msg_all.n_sent;
|
---|
| 178 | return True;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | struct event_context *messaging_event_context(struct messaging_context *msg_ctx)
|
---|
| 182 | {
|
---|
| 183 | return msg_ctx->event_ctx;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
|
---|
| 187 | struct server_id server_id,
|
---|
| 188 | struct event_context *ev)
|
---|
| 189 | {
|
---|
| 190 | struct messaging_context *ctx;
|
---|
| 191 | NTSTATUS status;
|
---|
| 192 |
|
---|
| 193 | if (!(ctx = TALLOC_ZERO_P(mem_ctx, struct messaging_context))) {
|
---|
| 194 | return NULL;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | ctx->id = server_id;
|
---|
| 198 | ctx->event_ctx = ev;
|
---|
| 199 |
|
---|
| 200 | status = messaging_tdb_init(ctx, ctx, &ctx->local);
|
---|
| 201 |
|
---|
| 202 | if (!NT_STATUS_IS_OK(status)) {
|
---|
| 203 | DEBUG(0, ("messaging_tdb_init failed: %s\n",
|
---|
| 204 | nt_errstr(status)));
|
---|
| 205 | TALLOC_FREE(ctx);
|
---|
| 206 | return NULL;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | #ifdef CLUSTER_SUPPORT
|
---|
| 210 | if (lp_clustering()) {
|
---|
| 211 | status = messaging_ctdbd_init(ctx, ctx, &ctx->remote);
|
---|
| 212 |
|
---|
| 213 | if (!NT_STATUS_IS_OK(status)) {
|
---|
| 214 | DEBUG(1, ("messaging_ctdb_init failed: %s\n",
|
---|
| 215 | nt_errstr(status)));
|
---|
| 216 | TALLOC_FREE(ctx);
|
---|
| 217 | return NULL;
|
---|
| 218 | }
|
---|
| 219 | }
|
---|
| 220 | #endif
|
---|
| 221 |
|
---|
| 222 | messaging_register(ctx, NULL, MSG_PING, ping_message);
|
---|
| 223 |
|
---|
| 224 | /* Register some debugging related messages */
|
---|
| 225 |
|
---|
| 226 | register_msg_pool_usage(ctx);
|
---|
| 227 | register_dmalloc_msgs(ctx);
|
---|
| 228 | debug_register_msgs(ctx);
|
---|
| 229 |
|
---|
| 230 | return ctx;
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | /*
|
---|
| 234 | * re-init after a fork
|
---|
| 235 | */
|
---|
| 236 | NTSTATUS messaging_reinit(struct messaging_context *msg_ctx)
|
---|
| 237 | {
|
---|
| 238 | #ifdef CLUSTER_SUPPORT
|
---|
| 239 |
|
---|
| 240 | TALLOC_FREE(msg_ctx->remote);
|
---|
| 241 |
|
---|
| 242 | if (lp_clustering()) {
|
---|
| 243 | NTSTATUS status;
|
---|
| 244 |
|
---|
| 245 | status = messaging_ctdbd_init(msg_ctx, msg_ctx,
|
---|
| 246 | &msg_ctx->remote);
|
---|
| 247 |
|
---|
| 248 | if (!NT_STATUS_IS_OK(status)) {
|
---|
| 249 | DEBUG(1, ("messaging_ctdb_init failed: %s\n",
|
---|
| 250 | nt_errstr(status)));
|
---|
| 251 | return status;
|
---|
| 252 | }
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | #endif
|
---|
| 256 |
|
---|
| 257 | return NT_STATUS_OK;
|
---|
| 258 | }
|
---|
| 259 |
|
---|
| 260 |
|
---|
| 261 | /*
|
---|
| 262 | * Register a dispatch function for a particular message type. Allow multiple
|
---|
| 263 | * registrants
|
---|
| 264 | */
|
---|
| 265 | NTSTATUS messaging_register(struct messaging_context *msg_ctx,
|
---|
| 266 | void *private_data,
|
---|
| 267 | uint32_t msg_type,
|
---|
| 268 | void (*fn)(struct messaging_context *msg,
|
---|
| 269 | void *private_data,
|
---|
| 270 | uint32_t msg_type,
|
---|
| 271 | struct server_id server_id,
|
---|
| 272 | DATA_BLOB *data))
|
---|
| 273 | {
|
---|
| 274 | struct messaging_callback *cb;
|
---|
| 275 |
|
---|
| 276 | /*
|
---|
| 277 | * Only one callback per type
|
---|
| 278 | */
|
---|
| 279 |
|
---|
| 280 | for (cb = msg_ctx->callbacks; cb != NULL; cb = cb->next) {
|
---|
| 281 | /* we allow a second registration of the same message
|
---|
| 282 | type if it has a different private pointer. This is
|
---|
| 283 | needed in, for example, the internal notify code,
|
---|
| 284 | which creates a new notify context for each tree
|
---|
| 285 | connect, and expects to receive messages to each of
|
---|
| 286 | them. */
|
---|
| 287 | if (cb->msg_type == msg_type && private_data == cb->private_data) {
|
---|
| 288 | DEBUG(5,("Overriding messaging pointer for type %u - private_data=%p\n",
|
---|
| 289 | (unsigned)msg_type, private_data));
|
---|
| 290 | cb->fn = fn;
|
---|
| 291 | cb->private_data = private_data;
|
---|
| 292 | return NT_STATUS_OK;
|
---|
| 293 | }
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | if (!(cb = talloc(msg_ctx, struct messaging_callback))) {
|
---|
| 297 | return NT_STATUS_NO_MEMORY;
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 | cb->msg_type = msg_type;
|
---|
| 301 | cb->fn = fn;
|
---|
| 302 | cb->private_data = private_data;
|
---|
| 303 |
|
---|
| 304 | DLIST_ADD(msg_ctx->callbacks, cb);
|
---|
| 305 | return NT_STATUS_OK;
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | /*
|
---|
| 309 | De-register the function for a particular message type.
|
---|
| 310 | */
|
---|
| 311 | void messaging_deregister(struct messaging_context *ctx, uint32_t msg_type,
|
---|
| 312 | void *private_data)
|
---|
| 313 | {
|
---|
| 314 | struct messaging_callback *cb, *next;
|
---|
| 315 |
|
---|
| 316 | for (cb = ctx->callbacks; cb; cb = next) {
|
---|
| 317 | next = cb->next;
|
---|
| 318 | if ((cb->msg_type == msg_type)
|
---|
| 319 | && (cb->private_data == private_data)) {
|
---|
| 320 | DEBUG(5,("Deregistering messaging pointer for type %u - private_data=%p\n",
|
---|
| 321 | (unsigned)msg_type, private_data));
|
---|
| 322 | DLIST_REMOVE(ctx->callbacks, cb);
|
---|
| 323 | TALLOC_FREE(cb);
|
---|
| 324 | }
|
---|
| 325 | }
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | /*
|
---|
| 329 | Send a message to a particular server
|
---|
| 330 | */
|
---|
| 331 | NTSTATUS messaging_send(struct messaging_context *msg_ctx,
|
---|
| 332 | struct server_id server, uint32_t msg_type,
|
---|
| 333 | const DATA_BLOB *data)
|
---|
| 334 | {
|
---|
| 335 | #ifdef CLUSTER_SUPPORT
|
---|
| 336 | if (!procid_is_local(&server)) {
|
---|
| 337 | return msg_ctx->remote->send_fn(msg_ctx, server,
|
---|
| 338 | msg_type, data,
|
---|
| 339 | msg_ctx->remote);
|
---|
| 340 | }
|
---|
| 341 | #endif
|
---|
| 342 | return msg_ctx->local->send_fn(msg_ctx, server, msg_type, data,
|
---|
| 343 | msg_ctx->local);
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 | NTSTATUS messaging_send_buf(struct messaging_context *msg_ctx,
|
---|
| 347 | struct server_id server, uint32_t msg_type,
|
---|
| 348 | const uint8 *buf, size_t len)
|
---|
| 349 | {
|
---|
| 350 | DATA_BLOB blob = data_blob_const(buf, len);
|
---|
| 351 | return messaging_send(msg_ctx, server, msg_type, &blob);
|
---|
| 352 | }
|
---|
| 353 |
|
---|
| 354 | /*
|
---|
| 355 | Dispatch one messsaging_rec
|
---|
| 356 | */
|
---|
| 357 | void messaging_dispatch_rec(struct messaging_context *msg_ctx,
|
---|
| 358 | struct messaging_rec *rec)
|
---|
| 359 | {
|
---|
| 360 | struct messaging_callback *cb, *next;
|
---|
| 361 |
|
---|
| 362 | for (cb = msg_ctx->callbacks; cb != NULL; cb = next) {
|
---|
| 363 | next = cb->next;
|
---|
| 364 | if (cb->msg_type == rec->msg_type) {
|
---|
| 365 | cb->fn(msg_ctx, cb->private_data, rec->msg_type,
|
---|
| 366 | rec->src, &rec->buf);
|
---|
| 367 | /* we continue looking for matching messages
|
---|
| 368 | after finding one. This matters for
|
---|
| 369 | subsystems like the internal notify code
|
---|
| 370 | which register more than one handler for
|
---|
| 371 | the same message type */
|
---|
| 372 | }
|
---|
| 373 | }
|
---|
| 374 | return;
|
---|
| 375 | }
|
---|
| 376 |
|
---|
| 377 | /** @} **/
|
---|