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 "lib/tdb/include/tdb.h"
34 #include "lib/util/util_tdb.h"
35 #include "lib/util/util_tdb.h"
36 #include "cluster/cluster.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 irpc_list *irpc;
52 struct idr_context *idr;
54 struct timeval start_time;
55 struct timed_event *retry_te;
57 struct event_context *ev;
62 /* we have a linked list of dispatch handlers for each msg_type that
63 this messaging server can deal with */
65 struct dispatch_fn *next, *prev;
71 /* an individual message */
72 struct messaging_rec {
73 struct messaging_rec *next, *prev;
74 struct messaging_context *msg;
77 struct messaging_header {
80 struct server_id from;
90 static void irpc_handler(struct messaging_context *, void *,
91 uint32_t, struct server_id, DATA_BLOB *);
95 A useful function for testing the message system.
97 static void ping_message(struct messaging_context *msg, void *private,
98 uint32_t msg_type, struct server_id src, DATA_BLOB *data)
100 DEBUG(1,("INFO: Received PING message from server %u.%u [%.*s]\n",
101 (uint_t)src.node, (uint_t)src.id, (int)data->length,
102 data->data?(const char *)data->data:""));
103 messaging_send(msg, src, MSG_PONG, data);
107 return uptime of messaging server via irpc
109 static NTSTATUS irpc_uptime(struct irpc_message *msg,
110 struct irpc_uptime *r)
112 struct messaging_context *ctx = talloc_get_type(msg->private, struct messaging_context);
113 *r->out.start_time = timeval_to_nttime(&ctx->start_time);
118 return the path to a messaging socket
120 static char *messaging_path(struct messaging_context *msg, struct server_id server_id)
122 return talloc_asprintf(msg, "%s/msg.%u.%u", msg->base_path,
123 (unsigned)server_id.node, (unsigned)server_id.id);
127 dispatch a fully received message
129 note that this deliberately can match more than one message handler
130 per message. That allows a single messasging context to register
131 (for example) a debug handler for more than one piece of code
133 static void messaging_dispatch(struct messaging_context *msg, struct messaging_rec *rec)
135 struct dispatch_fn *d, *next;
137 /* temporary IDs use an idtree, the rest use a array of pointers */
138 if (rec->header->msg_type >= MSG_TMP_BASE) {
139 d = idr_find(msg->dispatch_tree, rec->header->msg_type);
140 } else if (rec->header->msg_type < msg->num_types) {
141 d = msg->dispatch[rec->header->msg_type];
146 for (; d; d = next) {
149 data.data = rec->packet.data + sizeof(*rec->header);
150 data.length = rec->header->length;
151 d->fn(msg, d->private, d->msg_type, rec->header->from, &data);
153 rec->header->length = 0;
157 handler for messages that arrive from other nodes in the cluster
159 static void cluster_message_handler(struct messaging_context *msg, DATA_BLOB packet)
161 struct messaging_rec *rec;
163 rec = talloc(msg, struct messaging_rec);
165 smb_panic("Unable to allocate messaging_rec");
169 rec->path = msg->path;
170 rec->header = (struct messaging_header *)packet.data;
171 rec->packet = packet;
174 if (packet.length != sizeof(*rec->header) + rec->header->length) {
175 DEBUG(0,("messaging: bad message header size %d should be %d\n",
176 rec->header->length, (int)(packet.length - sizeof(*rec->header))));
181 messaging_dispatch(msg, rec);
188 try to send the message
190 static NTSTATUS try_send(struct messaging_rec *rec)
192 struct messaging_context *msg = rec->msg;
196 struct socket_address *path;
198 /* rec->path is the path of the *other* socket, where we want
200 path = socket_address_from_strings(msg, msg->sock->backend_name,
203 return NT_STATUS_NO_MEMORY;
206 /* we send with privileges so messages work from any context */
207 priv = root_privileges();
208 status = socket_sendto(msg->sock, &rec->packet, &nsent, path);
216 retry backed off messages
218 static void msg_retry_timer(struct event_context *ev, struct timed_event *te,
219 struct timeval t, void *private)
221 struct messaging_context *msg = talloc_get_type(private,
222 struct messaging_context);
223 msg->retry_te = NULL;
225 /* put the messages back on the main queue */
226 while (msg->retry_queue) {
227 struct messaging_rec *rec = msg->retry_queue;
228 DLIST_REMOVE(msg->retry_queue, rec);
229 DLIST_ADD_END(msg->pending, rec, struct messaging_rec *);
232 EVENT_FD_WRITEABLE(msg->event.fde);
236 handle a socket write event
238 static void messaging_send_handler(struct messaging_context *msg)
240 while (msg->pending) {
241 struct messaging_rec *rec = msg->pending;
243 status = try_send(rec);
244 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
246 if (rec->retries > 3) {
247 /* we're getting continuous write errors -
248 backoff this record */
249 DLIST_REMOVE(msg->pending, rec);
250 DLIST_ADD_END(msg->retry_queue, rec,
251 struct messaging_rec *);
252 if (msg->retry_te == NULL) {
254 event_add_timed(msg->event.ev, msg,
255 timeval_current_ofs(1, 0),
256 msg_retry_timer, msg);
262 if (!NT_STATUS_IS_OK(status)) {
263 DEBUG(1,("messaging: Lost message from %u to %u of type %u - %s\n",
264 rec->header->from.id, rec->header->to.id, rec->header->msg_type,
267 DLIST_REMOVE(msg->pending, rec);
270 if (msg->pending == NULL) {
271 EVENT_FD_NOT_WRITEABLE(msg->event.fde);
276 handle a new incoming packet
278 static void messaging_recv_handler(struct messaging_context *msg)
280 struct messaging_rec *rec;
285 /* see how many bytes are in the next packet */
286 status = socket_pending(msg->sock, &msize);
287 if (!NT_STATUS_IS_OK(status)) {
288 DEBUG(0,("socket_pending failed in messaging - %s\n",
293 packet = data_blob_talloc(msg, NULL, msize);
294 if (packet.data == NULL) {
295 /* assume this is temporary and retry */
299 status = socket_recv(msg->sock, packet.data, msize, &msize);
300 if (!NT_STATUS_IS_OK(status)) {
301 data_blob_free(&packet);
305 if (msize < sizeof(*rec->header)) {
306 DEBUG(0,("messaging: bad message of size %d\n", (int)msize));
307 data_blob_free(&packet);
311 rec = talloc(msg, struct messaging_rec);
313 smb_panic("Unable to allocate messaging_rec");
316 talloc_steal(rec, packet.data);
318 rec->path = msg->path;
319 rec->header = (struct messaging_header *)packet.data;
320 rec->packet = packet;
323 if (msize != sizeof(*rec->header) + rec->header->length) {
324 DEBUG(0,("messaging: bad message header size %d should be %d\n",
325 rec->header->length, (int)(msize - sizeof(*rec->header))));
330 messaging_dispatch(msg, rec);
336 handle a socket event
338 static void messaging_handler(struct event_context *ev, struct fd_event *fde,
339 uint16_t flags, void *private)
341 struct messaging_context *msg = talloc_get_type(private,
342 struct messaging_context);
343 if (flags & EVENT_FD_WRITE) {
344 messaging_send_handler(msg);
346 if (flags & EVENT_FD_READ) {
347 messaging_recv_handler(msg);
353 Register a dispatch function for a particular message type.
355 NTSTATUS messaging_register(struct messaging_context *msg, void *private,
356 uint32_t msg_type, msg_callback_t fn)
358 struct dispatch_fn *d;
360 /* possibly expand dispatch array */
361 if (msg_type >= msg->num_types) {
362 struct dispatch_fn **dp;
364 dp = talloc_realloc(msg, msg->dispatch, struct dispatch_fn *, msg_type+1);
365 NT_STATUS_HAVE_NO_MEMORY(dp);
367 for (i=msg->num_types;i<=msg_type;i++) {
368 msg->dispatch[i] = NULL;
370 msg->num_types = msg_type+1;
373 d = talloc_zero(msg->dispatch, struct dispatch_fn);
374 NT_STATUS_HAVE_NO_MEMORY(d);
375 d->msg_type = msg_type;
376 d->private = private;
379 DLIST_ADD(msg->dispatch[msg_type], d);
385 register a temporary message handler. The msg_type is allocated
388 NTSTATUS messaging_register_tmp(struct messaging_context *msg, void *private,
389 msg_callback_t fn, uint32_t *msg_type)
391 struct dispatch_fn *d;
394 d = talloc_zero(msg->dispatch, struct dispatch_fn);
395 NT_STATUS_HAVE_NO_MEMORY(d);
396 d->private = private;
399 id = idr_get_new_above(msg->dispatch_tree, d, MSG_TMP_BASE, UINT16_MAX);
402 return NT_STATUS_TOO_MANY_CONTEXT_IDS;
405 d->msg_type = (uint32_t)id;
406 (*msg_type) = d->msg_type;
412 De-register the function for a particular message type.
414 void messaging_deregister(struct messaging_context *msg, uint32_t msg_type, void *private)
416 struct dispatch_fn *d, *next;
418 if (msg_type >= msg->num_types) {
419 d = idr_find(msg->dispatch_tree, msg_type);
421 idr_remove(msg->dispatch_tree, msg_type);
426 for (d = msg->dispatch[msg_type]; d; d = next) {
428 if (d->private == private) {
429 DLIST_REMOVE(msg->dispatch[msg_type], d);
436 Send a message to a particular server
438 NTSTATUS messaging_send(struct messaging_context *msg, struct server_id server,
439 uint32_t msg_type, DATA_BLOB *data)
441 struct messaging_rec *rec;
443 size_t dlength = data?data->length:0;
445 rec = talloc(msg, struct messaging_rec);
447 return NT_STATUS_NO_MEMORY;
450 rec->packet = data_blob_talloc(rec, NULL, sizeof(*rec->header) + dlength);
451 if (rec->packet.data == NULL) {
453 return NT_STATUS_NO_MEMORY;
458 rec->header = (struct messaging_header *)rec->packet.data;
459 rec->header->version = MESSAGING_VERSION;
460 rec->header->msg_type = msg_type;
461 rec->header->from = msg->server_id;
462 rec->header->to = server;
463 rec->header->length = dlength;
465 memcpy(rec->packet.data + sizeof(*rec->header),
466 data->data, dlength);
469 if (!cluster_node_equal(&msg->server_id, &server)) {
470 /* the destination is on another node - dispatch via
472 status = cluster_message_send(server, &rec->packet);
477 rec->path = messaging_path(msg, server);
478 talloc_steal(rec, rec->path);
480 if (msg->pending != NULL) {
481 status = STATUS_MORE_ENTRIES;
483 status = try_send(rec);
486 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
487 if (msg->pending == NULL) {
488 EVENT_FD_WRITEABLE(msg->event.fde);
490 DLIST_ADD_END(msg->pending, rec, struct messaging_rec *);
500 Send a message to a particular server, with the message containing a single pointer
502 NTSTATUS messaging_send_ptr(struct messaging_context *msg, struct server_id server,
503 uint32_t msg_type, void *ptr)
507 blob.data = (void *)&ptr;
508 blob.length = sizeof(void *);
510 return messaging_send(msg, server, msg_type, &blob);
515 destroy the messaging context
517 static int messaging_destructor(struct messaging_context *msg)
520 while (msg->names && msg->names[0]) {
521 irpc_remove_name(msg, msg->names[0]);
527 create the listening socket and setup the dispatcher
529 struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
530 struct server_id server_id,
531 struct event_context *ev)
533 struct messaging_context *msg;
535 struct socket_address *path;
538 msg = talloc_zero(mem_ctx, struct messaging_context);
543 /* setup a handler for messages from other cluster nodes, if appropriate */
544 status = cluster_message_init(msg, server_id, cluster_message_handler);
545 if (!NT_STATUS_IS_OK(status)) {
551 ev = event_context_init(msg);
554 /* create the messaging directory if needed */
555 dir = smbd_tmp_path(msg, "messaging");
559 msg->base_path = smbd_tmp_path(msg, "messaging");
560 msg->path = messaging_path(msg, server_id);
561 msg->server_id = server_id;
562 msg->idr = idr_init(msg);
563 msg->dispatch_tree = idr_init(msg);
564 msg->start_time = timeval_current();
566 status = socket_create("unix", SOCKET_TYPE_DGRAM, &msg->sock, 0);
567 if (!NT_STATUS_IS_OK(status)) {
572 /* by stealing here we ensure that the socket is cleaned up (and even
574 talloc_steal(msg, msg->sock);
576 path = socket_address_from_strings(msg, msg->sock->backend_name,
583 status = socket_listen(msg->sock, path, 50, 0);
584 if (!NT_STATUS_IS_OK(status)) {
585 DEBUG(0,("Unable to setup messaging listener for '%s':%s\n", msg->path, nt_errstr(status)));
590 /* it needs to be non blocking for sends */
591 set_blocking(socket_get_fd(msg->sock), false);
593 msg->event.ev = talloc_reference(msg, ev);
594 msg->event.fde = event_add_fd(ev, msg, socket_get_fd(msg->sock),
595 EVENT_FD_READ, messaging_handler, msg);
597 talloc_set_destructor(msg, messaging_destructor);
599 messaging_register(msg, NULL, MSG_PING, ping_message);
600 messaging_register(msg, NULL, MSG_IRPC, irpc_handler);
601 IRPC_REGISTER(msg, irpc, IRPC_UPTIME, irpc_uptime, msg);
607 A hack, for the short term until we get 'client only' messaging in place
609 struct messaging_context *messaging_client_init(TALLOC_CTX *mem_ctx,
610 struct event_context *ev)
614 id.id = random() % 0x10000000;
615 return messaging_init(mem_ctx, id, ev);
618 a list of registered irpc server functions
621 struct irpc_list *next, *prev;
623 const struct ndr_interface_table *table;
631 register a irpc server function
633 NTSTATUS irpc_register(struct messaging_context *msg_ctx,
634 const struct ndr_interface_table *table,
635 int callnum, irpc_function_t fn, void *private)
637 struct irpc_list *irpc;
639 /* override an existing handler, if any */
640 for (irpc=msg_ctx->irpc; irpc; irpc=irpc->next) {
641 if (irpc->table == table && irpc->callnum == callnum) {
646 irpc = talloc(msg_ctx, struct irpc_list);
647 NT_STATUS_HAVE_NO_MEMORY(irpc);
648 DLIST_ADD(msg_ctx->irpc, irpc);
652 irpc->callnum = callnum;
654 irpc->private = private;
655 irpc->uuid = irpc->table->syntax_id.uuid;
662 handle an incoming irpc reply message
664 static void irpc_handler_reply(struct messaging_context *msg_ctx, struct irpc_message *m)
666 struct irpc_request *irpc;
668 irpc = idr_find(msg_ctx->idr, m->header.callid);
669 if (irpc == NULL) return;
671 /* parse the reply data */
672 irpc->status = irpc->table->calls[irpc->callnum].ndr_pull(m->ndr, NDR_OUT, irpc->r);
673 if (NT_STATUS_IS_OK(irpc->status)) {
674 irpc->status = m->header.status;
675 talloc_steal(irpc->mem_ctx, m);
677 talloc_steal(irpc, m);
680 if (irpc->async.fn) {
681 irpc->async.fn(irpc);
688 NTSTATUS irpc_send_reply(struct irpc_message *m, NTSTATUS status)
690 struct ndr_push *push;
693 m->header.status = status;
695 /* setup the reply */
696 push = ndr_push_init_ctx(m->ndr);
698 status = NT_STATUS_NO_MEMORY;
702 m->header.flags |= IRPC_FLAG_REPLY;
704 /* construct the packet */
705 status = ndr_push_irpc_header(push, NDR_SCALARS|NDR_BUFFERS, &m->header);
706 if (!NT_STATUS_IS_OK(status)) goto failed;
708 status = m->irpc->table->calls[m->irpc->callnum].ndr_push(push, NDR_OUT, m->data);
709 if (!NT_STATUS_IS_OK(status)) goto failed;
711 /* send the reply message */
712 packet = ndr_push_blob(push);
713 status = messaging_send(m->msg_ctx, m->from, MSG_IRPC, &packet);
714 if (!NT_STATUS_IS_OK(status)) goto failed;
722 handle an incoming irpc request message
724 static void irpc_handler_request(struct messaging_context *msg_ctx,
725 struct irpc_message *m)
731 for (i=msg_ctx->irpc; i; i=i->next) {
732 if (GUID_equal(&i->uuid, &m->header.uuid) &&
733 i->table->syntax_id.if_version == m->header.if_version &&
734 i->callnum == m->header.callnum) {
740 /* no registered handler for this message */
745 /* allocate space for the structure */
746 r = talloc_zero_size(m->ndr, i->table->calls[m->header.callnum].struct_size);
747 if (r == NULL) goto failed;
749 /* parse the request data */
750 status = i->table->calls[i->callnum].ndr_pull(m->ndr, NDR_IN, r);
751 if (!NT_STATUS_IS_OK(status)) goto failed;
754 m->private = i->private;
755 m->defer_reply = false;
756 m->msg_ctx = msg_ctx;
759 m->ev = msg_ctx->event.ev;
761 m->header.status = i->fn(m, r);
763 if (m->defer_reply) {
764 /* the server function has asked to defer the reply to later */
765 talloc_steal(msg_ctx, m);
769 irpc_send_reply(m, m->header.status);
777 handle an incoming irpc message
779 static void irpc_handler(struct messaging_context *msg_ctx, void *private,
780 uint32_t msg_type, struct server_id src, DATA_BLOB *packet)
782 struct irpc_message *m;
785 m = talloc(msg_ctx, struct irpc_message);
786 if (m == NULL) goto failed;
790 m->ndr = ndr_pull_init_blob(packet, m);
791 if (m->ndr == NULL) goto failed;
793 m->ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
795 status = ndr_pull_irpc_header(m->ndr, NDR_BUFFERS|NDR_SCALARS, &m->header);
796 if (!NT_STATUS_IS_OK(status)) goto failed;
798 if (m->header.flags & IRPC_FLAG_REPLY) {
799 irpc_handler_reply(msg_ctx, m);
801 irpc_handler_request(msg_ctx, m);
811 destroy a irpc request
813 static int irpc_destructor(struct irpc_request *irpc)
815 if (irpc->callid != -1) {
816 idr_remove(irpc->msg_ctx->idr, irpc->callid);
820 if (irpc->reject_free) {
827 timeout a irpc request
829 static void irpc_timeout(struct event_context *ev, struct timed_event *te,
830 struct timeval t, void *private)
832 struct irpc_request *irpc = talloc_get_type(private, struct irpc_request);
833 irpc->status = NT_STATUS_IO_TIMEOUT;
835 if (irpc->async.fn) {
836 irpc->async.fn(irpc);
842 make a irpc call - async send
844 struct irpc_request *irpc_call_send(struct messaging_context *msg_ctx,
845 struct server_id server_id,
846 const struct ndr_interface_table *table,
847 int callnum, void *r, TALLOC_CTX *ctx)
849 struct irpc_header header;
850 struct ndr_push *ndr;
853 struct irpc_request *irpc;
855 irpc = talloc(msg_ctx, struct irpc_request);
856 if (irpc == NULL) goto failed;
858 irpc->msg_ctx = msg_ctx;
860 irpc->callnum = callnum;
861 irpc->callid = idr_get_new(msg_ctx->idr, irpc, UINT16_MAX);
862 if (irpc->callid == -1) goto failed;
865 irpc->async.fn = NULL;
867 irpc->reject_free = false;
869 talloc_set_destructor(irpc, irpc_destructor);
871 /* setup the header */
872 header.uuid = table->syntax_id.uuid;
874 header.if_version = table->syntax_id.if_version;
875 header.callid = irpc->callid;
876 header.callnum = callnum;
878 header.status = NT_STATUS_OK;
880 /* construct the irpc packet */
881 ndr = ndr_push_init_ctx(irpc);
882 if (ndr == NULL) goto failed;
884 status = ndr_push_irpc_header(ndr, NDR_SCALARS|NDR_BUFFERS, &header);
885 if (!NT_STATUS_IS_OK(status)) goto failed;
887 status = table->calls[callnum].ndr_push(ndr, NDR_IN, r);
888 if (!NT_STATUS_IS_OK(status)) goto failed;
891 packet = ndr_push_blob(ndr);
892 status = messaging_send(msg_ctx, server_id, MSG_IRPC, &packet);
893 if (!NT_STATUS_IS_OK(status)) goto failed;
895 event_add_timed(msg_ctx->event.ev, irpc,
896 timeval_current_ofs(IRPC_CALL_TIMEOUT, 0),
908 wait for a irpc reply
910 NTSTATUS irpc_call_recv(struct irpc_request *irpc)
914 NT_STATUS_HAVE_NO_MEMORY(irpc);
916 irpc->reject_free = true;
918 while (!irpc->done) {
919 if (event_loop_once(irpc->msg_ctx->event.ev) != 0) {
920 return NT_STATUS_CONNECTION_DISCONNECTED;
924 irpc->reject_free = false;
926 status = irpc->status;
932 perform a synchronous irpc request
934 NTSTATUS irpc_call(struct messaging_context *msg_ctx,
935 struct server_id server_id,
936 const struct ndr_interface_table *table,
937 int callnum, void *r,
940 struct irpc_request *irpc = irpc_call_send(msg_ctx, server_id,
941 table, callnum, r, mem_ctx);
942 return irpc_call_recv(irpc);
946 open the naming database
948 static struct tdb_wrap *irpc_namedb_open(struct messaging_context *msg_ctx)
951 char *path = talloc_asprintf(msg_ctx, "%s/names.tdb", msg_ctx->base_path);
955 t = tdb_wrap_open(msg_ctx, path, 0, 0, O_RDWR|O_CREAT, 0660);
962 add a string name that this irpc server can be called on
964 NTSTATUS irpc_add_name(struct messaging_context *msg_ctx, const char *name)
969 NTSTATUS status = NT_STATUS_OK;
971 t = irpc_namedb_open(msg_ctx);
972 NT_STATUS_HAVE_NO_MEMORY(t);
974 if (tdb_lock_bystring(t->tdb, name) != 0) {
976 return NT_STATUS_LOCK_NOT_GRANTED;
978 rec = tdb_fetch_bystring(t->tdb, name);
979 count = rec.dsize / sizeof(struct server_id);
980 rec.dptr = (unsigned char *)realloc_p(rec.dptr, struct server_id, count+1);
981 rec.dsize += sizeof(struct server_id);
982 if (rec.dptr == NULL) {
983 tdb_unlock_bystring(t->tdb, name);
985 return NT_STATUS_NO_MEMORY;
987 ((struct server_id *)rec.dptr)[count] = msg_ctx->server_id;
988 if (tdb_store_bystring(t->tdb, name, rec, 0) != 0) {
989 status = NT_STATUS_INTERNAL_ERROR;
992 tdb_unlock_bystring(t->tdb, name);
995 msg_ctx->names = str_list_add(msg_ctx->names, name);
996 talloc_steal(msg_ctx, msg_ctx->names);
1002 return a list of server ids for a server name
1004 struct server_id *irpc_servers_byname(struct messaging_context *msg_ctx,
1005 TALLOC_CTX *mem_ctx,
1011 struct server_id *ret;
1013 t = irpc_namedb_open(msg_ctx);
1018 if (tdb_lock_bystring(t->tdb, name) != 0) {
1022 rec = tdb_fetch_bystring(t->tdb, name);
1023 if (rec.dptr == NULL) {
1024 tdb_unlock_bystring(t->tdb, name);
1028 count = rec.dsize / sizeof(struct server_id);
1029 ret = talloc_array(mem_ctx, struct server_id, count+1);
1031 tdb_unlock_bystring(t->tdb, name);
1035 for (i=0;i<count;i++) {
1036 ret[i] = ((struct server_id *)rec.dptr)[i];
1038 ret[i] = cluster_id(0);
1040 tdb_unlock_bystring(t->tdb, name);
1047 remove a name from a messaging context
1049 void irpc_remove_name(struct messaging_context *msg_ctx, const char *name)
1054 struct server_id *ids;
1056 str_list_remove(msg_ctx->names, name);
1058 t = irpc_namedb_open(msg_ctx);
1063 if (tdb_lock_bystring(t->tdb, name) != 0) {
1067 rec = tdb_fetch_bystring(t->tdb, name);
1068 count = rec.dsize / sizeof(struct server_id);
1070 tdb_unlock_bystring(t->tdb, name);
1074 ids = (struct server_id *)rec.dptr;
1075 for (i=0;i<count;i++) {
1076 if (cluster_id_equal(&ids[i], &msg_ctx->server_id)) {
1078 memmove(ids+i, ids+i+1,
1079 sizeof(struct server_id) * (count-(i+1)));
1081 rec.dsize -= sizeof(struct server_id);
1085 tdb_store_bystring(t->tdb, name, rec, 0);
1087 tdb_unlock_bystring(t->tdb, name);