2 Unix SMB/CIFS implementation.
4 Samba internal messaging functions
6 Copyright (C) Andrew Tridgell 2004
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "lib/events/events.h"
24 #include "system/filesys.h"
25 #include "messaging/messaging.h"
26 #include "lib/util/dlinklist.h"
27 #include "lib/socket/socket.h"
28 #include "librpc/gen_ndr/ndr_irpc.h"
29 #include "lib/messaging/irpc.h"
31 #include "lib/util/unix_privs.h"
32 #include "librpc/rpc/dcerpc.h"
33 #include "../tdb/include/tdb.h"
34 #include "lib/util/util_tdb.h"
35 #include "cluster/cluster.h"
36 #include "param/param.h"
38 /* change the message version with any incompatible changes in the protocol */
39 #define MESSAGING_VERSION 1
41 struct messaging_context {
42 struct server_id server_id;
43 struct socket_context *sock;
44 const char *base_path;
46 struct dispatch_fn **dispatch;
48 struct idr_context *dispatch_tree;
49 struct messaging_rec *pending;
50 struct messaging_rec *retry_queue;
51 struct smb_iconv_convenience *iconv_convenience;
52 struct irpc_list *irpc;
53 struct idr_context *idr;
55 struct timeval start_time;
56 struct timed_event *retry_te;
58 struct event_context *ev;
63 /* we have a linked list of dispatch handlers for each msg_type that
64 this messaging server can deal with */
66 struct dispatch_fn *next, *prev;
72 /* an individual message */
73 struct messaging_rec {
74 struct messaging_rec *next, *prev;
75 struct messaging_context *msg;
78 struct messaging_header {
81 struct server_id from;
91 static void irpc_handler(struct messaging_context *, void *,
92 uint32_t, struct server_id, DATA_BLOB *);
96 A useful function for testing the message system.
98 static void ping_message(struct messaging_context *msg, void *private,
99 uint32_t msg_type, struct server_id src, DATA_BLOB *data)
101 DEBUG(1,("INFO: Received PING message from server %u.%u [%.*s]\n",
102 (uint_t)src.node, (uint_t)src.id, (int)data->length,
103 data->data?(const char *)data->data:""));
104 messaging_send(msg, src, MSG_PONG, data);
108 return uptime of messaging server via irpc
110 static NTSTATUS irpc_uptime(struct irpc_message *msg,
111 struct irpc_uptime *r)
113 struct messaging_context *ctx = talloc_get_type(msg->private, struct messaging_context);
114 *r->out.start_time = timeval_to_nttime(&ctx->start_time);
119 return the path to a messaging socket
121 static char *messaging_path(struct messaging_context *msg, struct server_id server_id)
123 return talloc_asprintf(msg, "%s/msg.%s", msg->base_path,
124 cluster_id_string(msg, server_id));
128 dispatch a fully received message
130 note that this deliberately can match more than one message handler
131 per message. That allows a single messasging context to register
132 (for example) a debug handler for more than one piece of code
134 static void messaging_dispatch(struct messaging_context *msg, struct messaging_rec *rec)
136 struct dispatch_fn *d, *next;
138 /* temporary IDs use an idtree, the rest use a array of pointers */
139 if (rec->header->msg_type >= MSG_TMP_BASE) {
140 d = (struct dispatch_fn *)idr_find(msg->dispatch_tree,
141 rec->header->msg_type);
142 } else if (rec->header->msg_type < msg->num_types) {
143 d = msg->dispatch[rec->header->msg_type];
148 for (; d; d = next) {
151 data.data = rec->packet.data + sizeof(*rec->header);
152 data.length = rec->header->length;
153 d->fn(msg, d->private, d->msg_type, rec->header->from, &data);
155 rec->header->length = 0;
159 handler for messages that arrive from other nodes in the cluster
161 static void cluster_message_handler(struct messaging_context *msg, DATA_BLOB packet)
163 struct messaging_rec *rec;
165 rec = talloc(msg, struct messaging_rec);
167 smb_panic("Unable to allocate messaging_rec");
171 rec->path = msg->path;
172 rec->header = (struct messaging_header *)packet.data;
173 rec->packet = packet;
176 if (packet.length != sizeof(*rec->header) + rec->header->length) {
177 DEBUG(0,("messaging: bad message header size %d should be %d\n",
178 rec->header->length, (int)(packet.length - sizeof(*rec->header))));
183 messaging_dispatch(msg, rec);
190 try to send the message
192 static NTSTATUS try_send(struct messaging_rec *rec)
194 struct messaging_context *msg = rec->msg;
198 struct socket_address *path;
200 /* rec->path is the path of the *other* socket, where we want
202 path = socket_address_from_strings(msg, msg->sock->backend_name,
205 return NT_STATUS_NO_MEMORY;
208 /* we send with privileges so messages work from any context */
209 priv = root_privileges();
210 status = socket_sendto(msg->sock, &rec->packet, &nsent, path);
218 retry backed off messages
220 static void msg_retry_timer(struct event_context *ev, struct timed_event *te,
221 struct timeval t, void *private)
223 struct messaging_context *msg = talloc_get_type(private,
224 struct messaging_context);
225 msg->retry_te = NULL;
227 /* put the messages back on the main queue */
228 while (msg->retry_queue) {
229 struct messaging_rec *rec = msg->retry_queue;
230 DLIST_REMOVE(msg->retry_queue, rec);
231 DLIST_ADD_END(msg->pending, rec, struct messaging_rec *);
234 EVENT_FD_WRITEABLE(msg->event.fde);
238 handle a socket write event
240 static void messaging_send_handler(struct messaging_context *msg)
242 while (msg->pending) {
243 struct messaging_rec *rec = msg->pending;
245 status = try_send(rec);
246 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
248 if (rec->retries > 3) {
249 /* we're getting continuous write errors -
250 backoff this record */
251 DLIST_REMOVE(msg->pending, rec);
252 DLIST_ADD_END(msg->retry_queue, rec,
253 struct messaging_rec *);
254 if (msg->retry_te == NULL) {
256 event_add_timed(msg->event.ev, msg,
257 timeval_current_ofs(1, 0),
258 msg_retry_timer, msg);
264 if (!NT_STATUS_IS_OK(status)) {
265 DEBUG(1,("messaging: Lost message from %s to %s of type %u - %s\n",
266 cluster_id_string(debug_ctx(), rec->header->from),
267 cluster_id_string(debug_ctx(), rec->header->to),
268 rec->header->msg_type,
271 DLIST_REMOVE(msg->pending, rec);
274 if (msg->pending == NULL) {
275 EVENT_FD_NOT_WRITEABLE(msg->event.fde);
280 handle a new incoming packet
282 static void messaging_recv_handler(struct messaging_context *msg)
284 struct messaging_rec *rec;
289 /* see how many bytes are in the next packet */
290 status = socket_pending(msg->sock, &msize);
291 if (!NT_STATUS_IS_OK(status)) {
292 DEBUG(0,("socket_pending failed in messaging - %s\n",
297 packet = data_blob_talloc(msg, NULL, msize);
298 if (packet.data == NULL) {
299 /* assume this is temporary and retry */
303 status = socket_recv(msg->sock, packet.data, msize, &msize);
304 if (!NT_STATUS_IS_OK(status)) {
305 data_blob_free(&packet);
309 if (msize < sizeof(*rec->header)) {
310 DEBUG(0,("messaging: bad message of size %d\n", (int)msize));
311 data_blob_free(&packet);
315 rec = talloc(msg, struct messaging_rec);
317 smb_panic("Unable to allocate messaging_rec");
320 talloc_steal(rec, packet.data);
322 rec->path = msg->path;
323 rec->header = (struct messaging_header *)packet.data;
324 rec->packet = packet;
327 if (msize != sizeof(*rec->header) + rec->header->length) {
328 DEBUG(0,("messaging: bad message header size %d should be %d\n",
329 rec->header->length, (int)(msize - sizeof(*rec->header))));
334 messaging_dispatch(msg, rec);
340 handle a socket event
342 static void messaging_handler(struct event_context *ev, struct fd_event *fde,
343 uint16_t flags, void *private)
345 struct messaging_context *msg = talloc_get_type(private,
346 struct messaging_context);
347 if (flags & EVENT_FD_WRITE) {
348 messaging_send_handler(msg);
350 if (flags & EVENT_FD_READ) {
351 messaging_recv_handler(msg);
357 Register a dispatch function for a particular message type.
359 NTSTATUS messaging_register(struct messaging_context *msg, void *private,
360 uint32_t msg_type, msg_callback_t fn)
362 struct dispatch_fn *d;
364 /* possibly expand dispatch array */
365 if (msg_type >= msg->num_types) {
366 struct dispatch_fn **dp;
368 dp = talloc_realloc(msg, msg->dispatch, struct dispatch_fn *, msg_type+1);
369 NT_STATUS_HAVE_NO_MEMORY(dp);
371 for (i=msg->num_types;i<=msg_type;i++) {
372 msg->dispatch[i] = NULL;
374 msg->num_types = msg_type+1;
377 d = talloc_zero(msg->dispatch, struct dispatch_fn);
378 NT_STATUS_HAVE_NO_MEMORY(d);
379 d->msg_type = msg_type;
380 d->private = private;
383 DLIST_ADD(msg->dispatch[msg_type], d);
389 register a temporary message handler. The msg_type is allocated
392 NTSTATUS messaging_register_tmp(struct messaging_context *msg, void *private,
393 msg_callback_t fn, uint32_t *msg_type)
395 struct dispatch_fn *d;
398 d = talloc_zero(msg->dispatch, struct dispatch_fn);
399 NT_STATUS_HAVE_NO_MEMORY(d);
400 d->private = private;
403 id = idr_get_new_above(msg->dispatch_tree, d, MSG_TMP_BASE, UINT16_MAX);
406 return NT_STATUS_TOO_MANY_CONTEXT_IDS;
409 d->msg_type = (uint32_t)id;
410 (*msg_type) = d->msg_type;
416 De-register the function for a particular message type.
418 void messaging_deregister(struct messaging_context *msg, uint32_t msg_type, void *private)
420 struct dispatch_fn *d, *next;
422 if (msg_type >= msg->num_types) {
423 d = (struct dispatch_fn *)idr_find(msg->dispatch_tree,
426 idr_remove(msg->dispatch_tree, msg_type);
431 for (d = msg->dispatch[msg_type]; d; d = next) {
433 if (d->private == private) {
434 DLIST_REMOVE(msg->dispatch[msg_type], d);
441 Send a message to a particular server
443 NTSTATUS messaging_send(struct messaging_context *msg, struct server_id server,
444 uint32_t msg_type, DATA_BLOB *data)
446 struct messaging_rec *rec;
448 size_t dlength = data?data->length:0;
450 rec = talloc(msg, struct messaging_rec);
452 return NT_STATUS_NO_MEMORY;
455 rec->packet = data_blob_talloc(rec, NULL, sizeof(*rec->header) + dlength);
456 if (rec->packet.data == NULL) {
458 return NT_STATUS_NO_MEMORY;
463 rec->header = (struct messaging_header *)rec->packet.data;
465 ZERO_STRUCTP(rec->header);
466 rec->header->version = MESSAGING_VERSION;
467 rec->header->msg_type = msg_type;
468 rec->header->from = msg->server_id;
469 rec->header->to = server;
470 rec->header->length = dlength;
472 memcpy(rec->packet.data + sizeof(*rec->header),
473 data->data, dlength);
476 if (!cluster_node_equal(&msg->server_id, &server)) {
477 /* the destination is on another node - dispatch via
479 status = cluster_message_send(server, &rec->packet);
484 rec->path = messaging_path(msg, server);
485 talloc_steal(rec, rec->path);
487 if (msg->pending != NULL) {
488 status = STATUS_MORE_ENTRIES;
490 status = try_send(rec);
493 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
494 if (msg->pending == NULL) {
495 EVENT_FD_WRITEABLE(msg->event.fde);
497 DLIST_ADD_END(msg->pending, rec, struct messaging_rec *);
507 Send a message to a particular server, with the message containing a single pointer
509 NTSTATUS messaging_send_ptr(struct messaging_context *msg, struct server_id server,
510 uint32_t msg_type, void *ptr)
514 blob.data = (uint8_t *)&ptr;
515 blob.length = sizeof(void *);
517 return messaging_send(msg, server, msg_type, &blob);
522 destroy the messaging context
524 static int messaging_destructor(struct messaging_context *msg)
527 while (msg->names && msg->names[0]) {
528 irpc_remove_name(msg, msg->names[0]);
534 create the listening socket and setup the dispatcher
536 struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
538 struct server_id server_id,
539 struct smb_iconv_convenience *iconv_convenience,
540 struct event_context *ev)
542 struct messaging_context *msg;
544 struct socket_address *path;
550 msg = talloc_zero(mem_ctx, struct messaging_context);
555 /* setup a handler for messages from other cluster nodes, if appropriate */
556 status = cluster_message_init(msg, server_id, cluster_message_handler);
557 if (!NT_STATUS_IS_OK(status)) {
562 /* create the messaging directory if needed */
565 msg->base_path = talloc_reference(msg, dir);
566 msg->path = messaging_path(msg, server_id);
567 msg->server_id = server_id;
568 msg->iconv_convenience = iconv_convenience;
569 msg->idr = idr_init(msg);
570 msg->dispatch_tree = idr_init(msg);
571 msg->start_time = timeval_current();
573 status = socket_create("unix", SOCKET_TYPE_DGRAM, &msg->sock, 0);
574 if (!NT_STATUS_IS_OK(status)) {
579 /* by stealing here we ensure that the socket is cleaned up (and even
581 talloc_steal(msg, msg->sock);
583 path = socket_address_from_strings(msg, msg->sock->backend_name,
590 status = socket_listen(msg->sock, path, 50, 0);
591 if (!NT_STATUS_IS_OK(status)) {
592 DEBUG(0,("Unable to setup messaging listener for '%s':%s\n", msg->path, nt_errstr(status)));
597 /* it needs to be non blocking for sends */
598 set_blocking(socket_get_fd(msg->sock), false);
600 msg->event.ev = talloc_reference(msg, ev);
601 msg->event.fde = event_add_fd(ev, msg, socket_get_fd(msg->sock),
602 EVENT_FD_READ, messaging_handler, msg);
604 talloc_set_destructor(msg, messaging_destructor);
606 messaging_register(msg, NULL, MSG_PING, ping_message);
607 messaging_register(msg, NULL, MSG_IRPC, irpc_handler);
608 IRPC_REGISTER(msg, irpc, IRPC_UPTIME, irpc_uptime, msg);
614 A hack, for the short term until we get 'client only' messaging in place
616 struct messaging_context *messaging_client_init(TALLOC_CTX *mem_ctx,
618 struct smb_iconv_convenience *iconv_convenience,
619 struct event_context *ev)
623 id.id = random() % 0x10000000;
624 return messaging_init(mem_ctx, dir, id, iconv_convenience, ev);
627 a list of registered irpc server functions
630 struct irpc_list *next, *prev;
632 const struct ndr_interface_table *table;
640 register a irpc server function
642 NTSTATUS irpc_register(struct messaging_context *msg_ctx,
643 const struct ndr_interface_table *table,
644 int callnum, irpc_function_t fn, void *private)
646 struct irpc_list *irpc;
648 /* override an existing handler, if any */
649 for (irpc=msg_ctx->irpc; irpc; irpc=irpc->next) {
650 if (irpc->table == table && irpc->callnum == callnum) {
655 irpc = talloc(msg_ctx, struct irpc_list);
656 NT_STATUS_HAVE_NO_MEMORY(irpc);
657 DLIST_ADD(msg_ctx->irpc, irpc);
661 irpc->callnum = callnum;
663 irpc->private = private;
664 irpc->uuid = irpc->table->syntax_id.uuid;
671 handle an incoming irpc reply message
673 static void irpc_handler_reply(struct messaging_context *msg_ctx, struct irpc_message *m)
675 struct irpc_request *irpc;
676 enum ndr_err_code ndr_err;
678 irpc = (struct irpc_request *)idr_find(msg_ctx->idr, m->header.callid);
679 if (irpc == NULL) return;
681 /* parse the reply data */
682 ndr_err = irpc->table->calls[irpc->callnum].ndr_pull(m->ndr, NDR_OUT, irpc->r);
683 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
684 irpc->status = m->header.status;
685 talloc_steal(irpc->mem_ctx, m);
687 irpc->status = ndr_map_error2ntstatus(ndr_err);
688 talloc_steal(irpc, m);
691 if (irpc->async.fn) {
692 irpc->async.fn(irpc);
699 NTSTATUS irpc_send_reply(struct irpc_message *m, NTSTATUS status)
701 struct ndr_push *push;
703 enum ndr_err_code ndr_err;
705 m->header.status = status;
707 /* setup the reply */
708 push = ndr_push_init_ctx(m->ndr, m->msg_ctx->iconv_convenience);
710 status = NT_STATUS_NO_MEMORY;
714 m->header.flags |= IRPC_FLAG_REPLY;
716 /* construct the packet */
717 ndr_err = ndr_push_irpc_header(push, NDR_SCALARS|NDR_BUFFERS, &m->header);
718 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
719 status = ndr_map_error2ntstatus(ndr_err);
723 ndr_err = m->irpc->table->calls[m->irpc->callnum].ndr_push(push, NDR_OUT, m->data);
724 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
725 status = ndr_map_error2ntstatus(ndr_err);
729 /* send the reply message */
730 packet = ndr_push_blob(push);
731 status = messaging_send(m->msg_ctx, m->from, MSG_IRPC, &packet);
732 if (!NT_STATUS_IS_OK(status)) goto failed;
740 handle an incoming irpc request message
742 static void irpc_handler_request(struct messaging_context *msg_ctx,
743 struct irpc_message *m)
747 enum ndr_err_code ndr_err;
749 for (i=msg_ctx->irpc; i; i=i->next) {
750 if (GUID_equal(&i->uuid, &m->header.uuid) &&
751 i->table->syntax_id.if_version == m->header.if_version &&
752 i->callnum == m->header.callnum) {
758 /* no registered handler for this message */
763 /* allocate space for the structure */
764 r = talloc_zero_size(m->ndr, i->table->calls[m->header.callnum].struct_size);
765 if (r == NULL) goto failed;
767 /* parse the request data */
768 ndr_err = i->table->calls[i->callnum].ndr_pull(m->ndr, NDR_IN, r);
769 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) goto failed;
772 m->private = i->private;
773 m->defer_reply = false;
774 m->msg_ctx = msg_ctx;
777 m->ev = msg_ctx->event.ev;
779 m->header.status = i->fn(m, r);
781 if (m->defer_reply) {
782 /* the server function has asked to defer the reply to later */
783 talloc_steal(msg_ctx, m);
787 irpc_send_reply(m, m->header.status);
795 handle an incoming irpc message
797 static void irpc_handler(struct messaging_context *msg_ctx, void *private,
798 uint32_t msg_type, struct server_id src, DATA_BLOB *packet)
800 struct irpc_message *m;
801 enum ndr_err_code ndr_err;
803 m = talloc(msg_ctx, struct irpc_message);
804 if (m == NULL) goto failed;
808 m->ndr = ndr_pull_init_blob(packet, m, msg_ctx->iconv_convenience);
809 if (m->ndr == NULL) goto failed;
811 m->ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
813 ndr_err = ndr_pull_irpc_header(m->ndr, NDR_BUFFERS|NDR_SCALARS, &m->header);
814 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) goto failed;
816 if (m->header.flags & IRPC_FLAG_REPLY) {
817 irpc_handler_reply(msg_ctx, m);
819 irpc_handler_request(msg_ctx, m);
829 destroy a irpc request
831 static int irpc_destructor(struct irpc_request *irpc)
833 if (irpc->callid != -1) {
834 idr_remove(irpc->msg_ctx->idr, irpc->callid);
838 if (irpc->reject_free) {
845 timeout a irpc request
847 static void irpc_timeout(struct event_context *ev, struct timed_event *te,
848 struct timeval t, void *private)
850 struct irpc_request *irpc = talloc_get_type(private, struct irpc_request);
851 irpc->status = NT_STATUS_IO_TIMEOUT;
853 if (irpc->async.fn) {
854 irpc->async.fn(irpc);
860 make a irpc call - async send
862 struct irpc_request *irpc_call_send(struct messaging_context *msg_ctx,
863 struct server_id server_id,
864 const struct ndr_interface_table *table,
865 int callnum, void *r, TALLOC_CTX *ctx)
867 struct irpc_header header;
868 struct ndr_push *ndr;
871 struct irpc_request *irpc;
872 enum ndr_err_code ndr_err;
874 irpc = talloc(msg_ctx, struct irpc_request);
875 if (irpc == NULL) goto failed;
877 irpc->msg_ctx = msg_ctx;
879 irpc->callnum = callnum;
880 irpc->callid = idr_get_new(msg_ctx->idr, irpc, UINT16_MAX);
881 if (irpc->callid == -1) goto failed;
884 irpc->async.fn = NULL;
886 irpc->reject_free = false;
888 talloc_set_destructor(irpc, irpc_destructor);
890 /* setup the header */
891 header.uuid = table->syntax_id.uuid;
893 header.if_version = table->syntax_id.if_version;
894 header.callid = irpc->callid;
895 header.callnum = callnum;
897 header.status = NT_STATUS_OK;
899 /* construct the irpc packet */
900 ndr = ndr_push_init_ctx(irpc, msg_ctx->iconv_convenience);
901 if (ndr == NULL) goto failed;
903 ndr_err = ndr_push_irpc_header(ndr, NDR_SCALARS|NDR_BUFFERS, &header);
904 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) goto failed;
906 ndr_err = table->calls[callnum].ndr_push(ndr, NDR_IN, r);
907 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) goto failed;
910 packet = ndr_push_blob(ndr);
911 status = messaging_send(msg_ctx, server_id, MSG_IRPC, &packet);
912 if (!NT_STATUS_IS_OK(status)) goto failed;
914 event_add_timed(msg_ctx->event.ev, irpc,
915 timeval_current_ofs(IRPC_CALL_TIMEOUT, 0),
927 wait for a irpc reply
929 NTSTATUS irpc_call_recv(struct irpc_request *irpc)
933 NT_STATUS_HAVE_NO_MEMORY(irpc);
935 irpc->reject_free = true;
937 while (!irpc->done) {
938 if (event_loop_once(irpc->msg_ctx->event.ev) != 0) {
939 return NT_STATUS_CONNECTION_DISCONNECTED;
943 irpc->reject_free = false;
945 status = irpc->status;
951 perform a synchronous irpc request
953 NTSTATUS irpc_call(struct messaging_context *msg_ctx,
954 struct server_id server_id,
955 const struct ndr_interface_table *table,
956 int callnum, void *r,
959 struct irpc_request *irpc = irpc_call_send(msg_ctx, server_id,
960 table, callnum, r, mem_ctx);
961 return irpc_call_recv(irpc);
965 open the naming database
967 static struct tdb_wrap *irpc_namedb_open(struct messaging_context *msg_ctx)
970 char *path = talloc_asprintf(msg_ctx, "%s/names.tdb", msg_ctx->base_path);
974 t = tdb_wrap_open(msg_ctx, path, 0, 0, O_RDWR|O_CREAT, 0660);
981 add a string name that this irpc server can be called on
983 NTSTATUS irpc_add_name(struct messaging_context *msg_ctx, const char *name)
988 NTSTATUS status = NT_STATUS_OK;
990 t = irpc_namedb_open(msg_ctx);
991 NT_STATUS_HAVE_NO_MEMORY(t);
993 if (tdb_lock_bystring(t->tdb, name) != 0) {
995 return NT_STATUS_LOCK_NOT_GRANTED;
997 rec = tdb_fetch_bystring(t->tdb, name);
998 count = rec.dsize / sizeof(struct server_id);
999 rec.dptr = (unsigned char *)realloc_p(rec.dptr, struct server_id, count+1);
1000 rec.dsize += sizeof(struct server_id);
1001 if (rec.dptr == NULL) {
1002 tdb_unlock_bystring(t->tdb, name);
1004 return NT_STATUS_NO_MEMORY;
1006 ((struct server_id *)rec.dptr)[count] = msg_ctx->server_id;
1007 if (tdb_store_bystring(t->tdb, name, rec, 0) != 0) {
1008 status = NT_STATUS_INTERNAL_ERROR;
1011 tdb_unlock_bystring(t->tdb, name);
1014 msg_ctx->names = str_list_add(msg_ctx->names, name);
1015 talloc_steal(msg_ctx, msg_ctx->names);
1021 return a list of server ids for a server name
1023 struct server_id *irpc_servers_byname(struct messaging_context *msg_ctx,
1024 TALLOC_CTX *mem_ctx,
1030 struct server_id *ret;
1032 t = irpc_namedb_open(msg_ctx);
1037 if (tdb_lock_bystring(t->tdb, name) != 0) {
1041 rec = tdb_fetch_bystring(t->tdb, name);
1042 if (rec.dptr == NULL) {
1043 tdb_unlock_bystring(t->tdb, name);
1047 count = rec.dsize / sizeof(struct server_id);
1048 ret = talloc_array(mem_ctx, struct server_id, count+1);
1050 tdb_unlock_bystring(t->tdb, name);
1054 for (i=0;i<count;i++) {
1055 ret[i] = ((struct server_id *)rec.dptr)[i];
1057 ret[i] = cluster_id(0, 0);
1059 tdb_unlock_bystring(t->tdb, name);
1066 remove a name from a messaging context
1068 void irpc_remove_name(struct messaging_context *msg_ctx, const char *name)
1073 struct server_id *ids;
1075 str_list_remove(msg_ctx->names, name);
1077 t = irpc_namedb_open(msg_ctx);
1082 if (tdb_lock_bystring(t->tdb, name) != 0) {
1086 rec = tdb_fetch_bystring(t->tdb, name);
1087 if (rec.dptr == NULL) {
1088 tdb_unlock_bystring(t->tdb, name);
1092 count = rec.dsize / sizeof(struct server_id);
1095 tdb_unlock_bystring(t->tdb, name);
1099 ids = (struct server_id *)rec.dptr;
1100 for (i=0;i<count;i++) {
1101 if (cluster_id_equal(&ids[i], &msg_ctx->server_id)) {
1103 memmove(ids+i, ids+i+1,
1104 sizeof(struct server_id) * (count-(i+1)));
1106 rec.dsize -= sizeof(struct server_id);
1110 tdb_store_bystring(t->tdb, name, rec, 0);
1112 tdb_unlock_bystring(t->tdb, name);
1116 struct server_id messaging_get_server_id(struct messaging_context *msg_ctx)
1118 return msg_ctx->server_id;