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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "lib/events/events.h"
25 #include "system/filesys.h"
26 #include "messaging/messaging.h"
27 #include "lib/util/dlinklist.h"
28 #include "lib/socket/socket.h"
29 #include "librpc/gen_ndr/ndr_irpc.h"
30 #include "lib/messaging/irpc.h"
32 #include "lib/util/unix_privs.h"
33 #include "librpc/rpc/dcerpc.h"
34 #include "lib/tdb/include/tdb.h"
35 #include "lib/util/util_tdb.h"
36 #include "lib/util/util_tdb.h"
37 #include "cluster/cluster.h"
39 /* change the message version with any incompatible changes in the protocol */
40 #define MESSAGING_VERSION 1
42 struct messaging_context {
43 struct server_id server_id;
44 struct socket_context *sock;
45 const char *base_path;
47 struct dispatch_fn **dispatch;
49 struct idr_context *dispatch_tree;
50 struct messaging_rec *pending;
51 struct messaging_rec *retry_queue;
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.%u.%u", msg->base_path,
124 (unsigned)server_id.node, (unsigned)server_id.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 = idr_find(msg->dispatch_tree, rec->header->msg_type);
141 } else if (rec->header->msg_type < msg->num_types) {
142 d = msg->dispatch[rec->header->msg_type];
147 for (; d; d = next) {
150 data.data = rec->packet.data + sizeof(*rec->header);
151 data.length = rec->header->length;
152 d->fn(msg, d->private, d->msg_type, rec->header->from, &data);
154 rec->header->length = 0;
158 handler for messages that arrive from other nodes in the cluster
160 static void cluster_message_handler(struct messaging_context *msg, DATA_BLOB packet)
162 struct messaging_rec *rec;
164 rec = talloc(msg, struct messaging_rec);
166 smb_panic("Unable to allocate messaging_rec");
170 rec->path = msg->path;
171 rec->header = (struct messaging_header *)packet.data;
172 rec->packet = packet;
175 if (packet.length != sizeof(*rec->header) + rec->header->length) {
176 DEBUG(0,("messaging: bad message header size %d should be %d\n",
177 rec->header->length, (int)(packet.length - sizeof(*rec->header))));
182 messaging_dispatch(msg, rec);
189 try to send the message
191 static NTSTATUS try_send(struct messaging_rec *rec)
193 struct messaging_context *msg = rec->msg;
197 struct socket_address *path;
199 /* rec->path is the path of the *other* socket, where we want
201 path = socket_address_from_strings(msg, msg->sock->backend_name,
204 return NT_STATUS_NO_MEMORY;
207 /* we send with privileges so messages work from any context */
208 priv = root_privileges();
209 status = socket_sendto(msg->sock, &rec->packet, &nsent, path);
217 retry backed off messages
219 static void msg_retry_timer(struct event_context *ev, struct timed_event *te,
220 struct timeval t, void *private)
222 struct messaging_context *msg = talloc_get_type(private,
223 struct messaging_context);
224 msg->retry_te = NULL;
226 /* put the messages back on the main queue */
227 while (msg->retry_queue) {
228 struct messaging_rec *rec = msg->retry_queue;
229 DLIST_REMOVE(msg->retry_queue, rec);
230 DLIST_ADD_END(msg->pending, rec, struct messaging_rec *);
233 EVENT_FD_WRITEABLE(msg->event.fde);
237 handle a socket write event
239 static void messaging_send_handler(struct messaging_context *msg)
241 while (msg->pending) {
242 struct messaging_rec *rec = msg->pending;
244 status = try_send(rec);
245 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
247 if (rec->retries > 3) {
248 /* we're getting continuous write errors -
249 backoff this record */
250 DLIST_REMOVE(msg->pending, rec);
251 DLIST_ADD_END(msg->retry_queue, rec,
252 struct messaging_rec *);
253 if (msg->retry_te == NULL) {
255 event_add_timed(msg->event.ev, msg,
256 timeval_current_ofs(1, 0),
257 msg_retry_timer, msg);
263 if (!NT_STATUS_IS_OK(status)) {
264 DEBUG(1,("messaging: Lost message from %u to %u of type %u - %s\n",
265 rec->header->from.id, rec->header->to.id, rec->header->msg_type,
268 DLIST_REMOVE(msg->pending, rec);
271 if (msg->pending == NULL) {
272 EVENT_FD_NOT_WRITEABLE(msg->event.fde);
277 handle a new incoming packet
279 static void messaging_recv_handler(struct messaging_context *msg)
281 struct messaging_rec *rec;
286 /* see how many bytes are in the next packet */
287 status = socket_pending(msg->sock, &msize);
288 if (!NT_STATUS_IS_OK(status)) {
289 DEBUG(0,("socket_pending failed in messaging - %s\n",
294 packet = data_blob_talloc(msg, NULL, msize);
295 if (packet.data == NULL) {
296 /* assume this is temporary and retry */
300 status = socket_recv(msg->sock, packet.data, msize, &msize);
301 if (!NT_STATUS_IS_OK(status)) {
302 data_blob_free(&packet);
306 if (msize < sizeof(*rec->header)) {
307 DEBUG(0,("messaging: bad message of size %d\n", (int)msize));
308 data_blob_free(&packet);
312 rec = talloc(msg, struct messaging_rec);
314 smb_panic("Unable to allocate messaging_rec");
317 talloc_steal(rec, packet.data);
319 rec->path = msg->path;
320 rec->header = (struct messaging_header *)packet.data;
321 rec->packet = packet;
324 if (msize != sizeof(*rec->header) + rec->header->length) {
325 DEBUG(0,("messaging: bad message header size %d should be %d\n",
326 rec->header->length, (int)(msize - sizeof(*rec->header))));
331 messaging_dispatch(msg, rec);
337 handle a socket event
339 static void messaging_handler(struct event_context *ev, struct fd_event *fde,
340 uint16_t flags, void *private)
342 struct messaging_context *msg = talloc_get_type(private,
343 struct messaging_context);
344 if (flags & EVENT_FD_WRITE) {
345 messaging_send_handler(msg);
347 if (flags & EVENT_FD_READ) {
348 messaging_recv_handler(msg);
354 Register a dispatch function for a particular message type.
356 NTSTATUS messaging_register(struct messaging_context *msg, void *private,
357 uint32_t msg_type, msg_callback_t fn)
359 struct dispatch_fn *d;
361 /* possibly expand dispatch array */
362 if (msg_type >= msg->num_types) {
363 struct dispatch_fn **dp;
365 dp = talloc_realloc(msg, msg->dispatch, struct dispatch_fn *, msg_type+1);
366 NT_STATUS_HAVE_NO_MEMORY(dp);
368 for (i=msg->num_types;i<=msg_type;i++) {
369 msg->dispatch[i] = NULL;
371 msg->num_types = msg_type+1;
374 d = talloc_zero(msg->dispatch, struct dispatch_fn);
375 NT_STATUS_HAVE_NO_MEMORY(d);
376 d->msg_type = msg_type;
377 d->private = private;
380 DLIST_ADD(msg->dispatch[msg_type], d);
386 register a temporary message handler. The msg_type is allocated
389 NTSTATUS messaging_register_tmp(struct messaging_context *msg, void *private,
390 msg_callback_t fn, uint32_t *msg_type)
392 struct dispatch_fn *d;
395 d = talloc_zero(msg->dispatch, struct dispatch_fn);
396 NT_STATUS_HAVE_NO_MEMORY(d);
397 d->private = private;
400 id = idr_get_new_above(msg->dispatch_tree, d, MSG_TMP_BASE, UINT16_MAX);
403 return NT_STATUS_TOO_MANY_CONTEXT_IDS;
406 d->msg_type = (uint32_t)id;
407 (*msg_type) = d->msg_type;
413 De-register the function for a particular message type.
415 void messaging_deregister(struct messaging_context *msg, uint32_t msg_type, void *private)
417 struct dispatch_fn *d, *next;
419 if (msg_type >= msg->num_types) {
420 d = idr_find(msg->dispatch_tree, msg_type);
422 idr_remove(msg->dispatch_tree, msg_type);
427 for (d = msg->dispatch[msg_type]; d; d = next) {
429 if (d->private == private) {
430 DLIST_REMOVE(msg->dispatch[msg_type], d);
437 Send a message to a particular server
439 NTSTATUS messaging_send(struct messaging_context *msg, struct server_id server,
440 uint32_t msg_type, DATA_BLOB *data)
442 struct messaging_rec *rec;
444 size_t dlength = data?data->length:0;
446 rec = talloc(msg, struct messaging_rec);
448 return NT_STATUS_NO_MEMORY;
451 rec->packet = data_blob_talloc(rec, NULL, sizeof(*rec->header) + dlength);
452 if (rec->packet.data == NULL) {
454 return NT_STATUS_NO_MEMORY;
459 rec->header = (struct messaging_header *)rec->packet.data;
460 rec->header->version = MESSAGING_VERSION;
461 rec->header->msg_type = msg_type;
462 rec->header->from = msg->server_id;
463 rec->header->to = server;
464 rec->header->length = dlength;
466 memcpy(rec->packet.data + sizeof(*rec->header),
467 data->data, dlength);
470 if (!cluster_node_equal(&msg->server_id, &server)) {
471 /* the destination is on another node - dispatch via
473 status = cluster_message_send(server, &rec->packet);
478 rec->path = messaging_path(msg, server);
479 talloc_steal(rec, rec->path);
481 if (msg->pending != NULL) {
482 status = STATUS_MORE_ENTRIES;
484 status = try_send(rec);
487 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
488 if (msg->pending == NULL) {
489 EVENT_FD_WRITEABLE(msg->event.fde);
491 DLIST_ADD_END(msg->pending, rec, struct messaging_rec *);
501 Send a message to a particular server, with the message containing a single pointer
503 NTSTATUS messaging_send_ptr(struct messaging_context *msg, struct server_id server,
504 uint32_t msg_type, void *ptr)
508 blob.data = (void *)&ptr;
509 blob.length = sizeof(void *);
511 return messaging_send(msg, server, msg_type, &blob);
516 destroy the messaging context
518 static int messaging_destructor(struct messaging_context *msg)
521 while (msg->names && msg->names[0]) {
522 irpc_remove_name(msg, msg->names[0]);
528 create the listening socket and setup the dispatcher
530 struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
531 struct server_id server_id,
532 struct event_context *ev)
534 struct messaging_context *msg;
536 struct socket_address *path;
539 msg = talloc_zero(mem_ctx, struct messaging_context);
544 /* setup a handler for messages from other cluster nodes, if appropriate */
545 status = cluster_message_init(msg, server_id, cluster_message_handler);
546 if (!NT_STATUS_IS_OK(status)) {
552 ev = event_context_init(msg);
555 /* create the messaging directory if needed */
556 dir = smbd_tmp_path(msg, "messaging");
560 msg->base_path = smbd_tmp_path(msg, "messaging");
561 msg->path = messaging_path(msg, server_id);
562 msg->server_id = server_id;
563 msg->idr = idr_init(msg);
564 msg->dispatch_tree = idr_init(msg);
565 msg->start_time = timeval_current();
567 status = socket_create("unix", SOCKET_TYPE_DGRAM, &msg->sock, 0);
568 if (!NT_STATUS_IS_OK(status)) {
573 /* by stealing here we ensure that the socket is cleaned up (and even
575 talloc_steal(msg, msg->sock);
577 path = socket_address_from_strings(msg, msg->sock->backend_name,
584 status = socket_listen(msg->sock, path, 50, 0);
585 if (!NT_STATUS_IS_OK(status)) {
586 DEBUG(0,("Unable to setup messaging listener for '%s':%s\n", msg->path, nt_errstr(status)));
591 /* it needs to be non blocking for sends */
592 set_blocking(socket_get_fd(msg->sock), False);
594 msg->event.ev = talloc_reference(msg, ev);
595 msg->event.fde = event_add_fd(ev, msg, socket_get_fd(msg->sock),
596 EVENT_FD_READ, messaging_handler, msg);
598 talloc_set_destructor(msg, messaging_destructor);
600 messaging_register(msg, NULL, MSG_PING, ping_message);
601 messaging_register(msg, NULL, MSG_IRPC, irpc_handler);
602 IRPC_REGISTER(msg, irpc, IRPC_UPTIME, irpc_uptime, msg);
608 A hack, for the short term until we get 'client only' messaging in place
610 struct messaging_context *messaging_client_init(TALLOC_CTX *mem_ctx,
611 struct event_context *ev)
615 id.id = random() % 0x10000000;
616 return messaging_init(mem_ctx, id, ev);
619 a list of registered irpc server functions
622 struct irpc_list *next, *prev;
624 const struct dcerpc_interface_table *table;
632 register a irpc server function
634 NTSTATUS irpc_register(struct messaging_context *msg_ctx,
635 const struct dcerpc_interface_table *table,
636 int callnum, irpc_function_t fn, void *private)
638 struct irpc_list *irpc;
640 /* override an existing handler, if any */
641 for (irpc=msg_ctx->irpc; irpc; irpc=irpc->next) {
642 if (irpc->table == table && irpc->callnum == callnum) {
647 irpc = talloc(msg_ctx, struct irpc_list);
648 NT_STATUS_HAVE_NO_MEMORY(irpc);
649 DLIST_ADD(msg_ctx->irpc, irpc);
653 irpc->callnum = callnum;
655 irpc->private = private;
656 irpc->uuid = irpc->table->syntax_id.uuid;
663 handle an incoming irpc reply message
665 static void irpc_handler_reply(struct messaging_context *msg_ctx, struct irpc_message *m)
667 struct irpc_request *irpc;
669 irpc = idr_find(msg_ctx->idr, m->header.callid);
670 if (irpc == NULL) return;
672 /* parse the reply data */
673 irpc->status = irpc->table->calls[irpc->callnum].ndr_pull(m->ndr, NDR_OUT, irpc->r);
674 if (NT_STATUS_IS_OK(irpc->status)) {
675 irpc->status = m->header.status;
676 talloc_steal(irpc->mem_ctx, m);
678 talloc_steal(irpc, m);
681 if (irpc->async.fn) {
682 irpc->async.fn(irpc);
689 NTSTATUS irpc_send_reply(struct irpc_message *m, NTSTATUS status)
691 struct ndr_push *push;
694 m->header.status = status;
696 /* setup the reply */
697 push = ndr_push_init_ctx(m->ndr);
699 status = NT_STATUS_NO_MEMORY;
703 m->header.flags |= IRPC_FLAG_REPLY;
705 /* construct the packet */
706 status = ndr_push_irpc_header(push, NDR_SCALARS|NDR_BUFFERS, &m->header);
707 if (!NT_STATUS_IS_OK(status)) goto failed;
709 status = m->irpc->table->calls[m->irpc->callnum].ndr_push(push, NDR_OUT, m->data);
710 if (!NT_STATUS_IS_OK(status)) goto failed;
712 /* send the reply message */
713 packet = ndr_push_blob(push);
714 status = messaging_send(m->msg_ctx, m->from, MSG_IRPC, &packet);
715 if (!NT_STATUS_IS_OK(status)) goto failed;
723 handle an incoming irpc request message
725 static void irpc_handler_request(struct messaging_context *msg_ctx,
726 struct irpc_message *m)
732 for (i=msg_ctx->irpc; i; i=i->next) {
733 if (GUID_equal(&i->uuid, &m->header.uuid) &&
734 i->table->syntax_id.if_version == m->header.if_version &&
735 i->callnum == m->header.callnum) {
741 /* no registered handler for this message */
746 /* allocate space for the structure */
747 r = talloc_zero_size(m->ndr, i->table->calls[m->header.callnum].struct_size);
748 if (r == NULL) goto failed;
750 /* parse the request data */
751 status = i->table->calls[i->callnum].ndr_pull(m->ndr, NDR_IN, r);
752 if (!NT_STATUS_IS_OK(status)) goto failed;
755 m->private = i->private;
756 m->defer_reply = False;
757 m->msg_ctx = msg_ctx;
760 m->ev = msg_ctx->event.ev;
762 m->header.status = i->fn(m, r);
764 if (m->defer_reply) {
765 /* the server function has asked to defer the reply to later */
766 talloc_steal(msg_ctx, m);
770 irpc_send_reply(m, m->header.status);
778 handle an incoming irpc message
780 static void irpc_handler(struct messaging_context *msg_ctx, void *private,
781 uint32_t msg_type, struct server_id src, DATA_BLOB *packet)
783 struct irpc_message *m;
786 m = talloc(msg_ctx, struct irpc_message);
787 if (m == NULL) goto failed;
791 m->ndr = ndr_pull_init_blob(packet, m);
792 if (m->ndr == NULL) goto failed;
794 m->ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
796 status = ndr_pull_irpc_header(m->ndr, NDR_BUFFERS|NDR_SCALARS, &m->header);
797 if (!NT_STATUS_IS_OK(status)) goto failed;
799 if (m->header.flags & IRPC_FLAG_REPLY) {
800 irpc_handler_reply(msg_ctx, m);
802 irpc_handler_request(msg_ctx, m);
812 destroy a irpc request
814 static int irpc_destructor(struct irpc_request *irpc)
816 if (irpc->callid != -1) {
817 idr_remove(irpc->msg_ctx->idr, irpc->callid);
821 if (irpc->reject_free) {
828 timeout a irpc request
830 static void irpc_timeout(struct event_context *ev, struct timed_event *te,
831 struct timeval t, void *private)
833 struct irpc_request *irpc = talloc_get_type(private, struct irpc_request);
834 irpc->status = NT_STATUS_IO_TIMEOUT;
836 if (irpc->async.fn) {
837 irpc->async.fn(irpc);
843 make a irpc call - async send
845 struct irpc_request *irpc_call_send(struct messaging_context *msg_ctx,
846 struct server_id server_id,
847 const struct dcerpc_interface_table *table,
848 int callnum, void *r, TALLOC_CTX *ctx)
850 struct irpc_header header;
851 struct ndr_push *ndr;
854 struct irpc_request *irpc;
856 irpc = talloc(msg_ctx, struct irpc_request);
857 if (irpc == NULL) goto failed;
859 irpc->msg_ctx = msg_ctx;
861 irpc->callnum = callnum;
862 irpc->callid = idr_get_new(msg_ctx->idr, irpc, UINT16_MAX);
863 if (irpc->callid == -1) goto failed;
866 irpc->async.fn = NULL;
868 irpc->reject_free = False;
870 talloc_set_destructor(irpc, irpc_destructor);
872 /* setup the header */
873 header.uuid = table->syntax_id.uuid;
875 header.if_version = table->syntax_id.if_version;
876 header.callid = irpc->callid;
877 header.callnum = callnum;
879 header.status = NT_STATUS_OK;
881 /* construct the irpc packet */
882 ndr = ndr_push_init_ctx(irpc);
883 if (ndr == NULL) goto failed;
885 status = ndr_push_irpc_header(ndr, NDR_SCALARS|NDR_BUFFERS, &header);
886 if (!NT_STATUS_IS_OK(status)) goto failed;
888 status = table->calls[callnum].ndr_push(ndr, NDR_IN, r);
889 if (!NT_STATUS_IS_OK(status)) goto failed;
892 packet = ndr_push_blob(ndr);
893 status = messaging_send(msg_ctx, server_id, MSG_IRPC, &packet);
894 if (!NT_STATUS_IS_OK(status)) goto failed;
896 event_add_timed(msg_ctx->event.ev, irpc,
897 timeval_current_ofs(IRPC_CALL_TIMEOUT, 0),
909 wait for a irpc reply
911 NTSTATUS irpc_call_recv(struct irpc_request *irpc)
915 NT_STATUS_HAVE_NO_MEMORY(irpc);
917 irpc->reject_free = true;
919 while (!irpc->done) {
920 if (event_loop_once(irpc->msg_ctx->event.ev) != 0) {
921 return NT_STATUS_CONNECTION_DISCONNECTED;
925 irpc->reject_free = false;
927 status = irpc->status;
933 perform a synchronous irpc request
935 NTSTATUS irpc_call(struct messaging_context *msg_ctx,
936 struct server_id server_id,
937 const struct dcerpc_interface_table *table,
938 int callnum, void *r,
941 struct irpc_request *irpc = irpc_call_send(msg_ctx, server_id,
942 table, callnum, r, mem_ctx);
943 return irpc_call_recv(irpc);
947 open the naming database
949 static struct tdb_wrap *irpc_namedb_open(struct messaging_context *msg_ctx)
952 char *path = talloc_asprintf(msg_ctx, "%s/names.tdb", msg_ctx->base_path);
956 t = tdb_wrap_open(msg_ctx, path, 0, 0, O_RDWR|O_CREAT, 0660);
963 add a string name that this irpc server can be called on
965 NTSTATUS irpc_add_name(struct messaging_context *msg_ctx, const char *name)
970 NTSTATUS status = NT_STATUS_OK;
972 t = irpc_namedb_open(msg_ctx);
973 NT_STATUS_HAVE_NO_MEMORY(t);
975 if (tdb_lock_bystring(t->tdb, name) != 0) {
977 return NT_STATUS_LOCK_NOT_GRANTED;
979 rec = tdb_fetch_bystring(t->tdb, name);
980 count = rec.dsize / sizeof(struct server_id);
981 rec.dptr = (unsigned char *)realloc_p(rec.dptr, struct server_id, count+1);
982 rec.dsize += sizeof(struct server_id);
983 if (rec.dptr == NULL) {
984 tdb_unlock_bystring(t->tdb, name);
986 return NT_STATUS_NO_MEMORY;
988 ((struct server_id *)rec.dptr)[count] = msg_ctx->server_id;
989 if (tdb_store_bystring(t->tdb, name, rec, 0) != 0) {
990 status = NT_STATUS_INTERNAL_ERROR;
993 tdb_unlock_bystring(t->tdb, name);
996 msg_ctx->names = str_list_add(msg_ctx->names, name);
997 talloc_steal(msg_ctx, msg_ctx->names);
1003 return a list of server ids for a server name
1005 struct server_id *irpc_servers_byname(struct messaging_context *msg_ctx,
1006 TALLOC_CTX *mem_ctx,
1012 struct server_id *ret;
1014 t = irpc_namedb_open(msg_ctx);
1019 if (tdb_lock_bystring(t->tdb, name) != 0) {
1023 rec = tdb_fetch_bystring(t->tdb, name);
1024 if (rec.dptr == NULL) {
1025 tdb_unlock_bystring(t->tdb, name);
1029 count = rec.dsize / sizeof(struct server_id);
1030 ret = talloc_array(mem_ctx, struct server_id, count+1);
1032 tdb_unlock_bystring(t->tdb, name);
1036 for (i=0;i<count;i++) {
1037 ret[i] = ((struct server_id *)rec.dptr)[i];
1039 ret[i] = cluster_id(0);
1041 tdb_unlock_bystring(t->tdb, name);
1048 remove a name from a messaging context
1050 void irpc_remove_name(struct messaging_context *msg_ctx, const char *name)
1055 struct server_id *ids;
1057 str_list_remove(msg_ctx->names, name);
1059 t = irpc_namedb_open(msg_ctx);
1064 if (tdb_lock_bystring(t->tdb, name) != 0) {
1068 rec = tdb_fetch_bystring(t->tdb, name);
1069 count = rec.dsize / sizeof(struct server_id);
1071 tdb_unlock_bystring(t->tdb, name);
1075 ids = (struct server_id *)rec.dptr;
1076 for (i=0;i<count;i++) {
1077 if (cluster_id_equal(&ids[i], &msg_ctx->server_id)) {
1079 memmove(ids+i, ids+i+1,
1080 sizeof(struct server_id) * (count-(i+1)));
1082 rec.dsize -= sizeof(struct server_id);
1086 tdb_store_bystring(t->tdb, name, rec, 0);
1088 tdb_unlock_bystring(t->tdb, name);