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"
37 /* change the message version with any incompatible changes in the protocol */
38 #define MESSAGING_VERSION 1
40 struct messaging_context {
41 struct server_id server_id;
42 struct socket_context *sock;
43 const char *base_path;
45 struct dispatch_fn **dispatch;
47 struct idr_context *dispatch_tree;
48 struct messaging_rec *pending;
49 struct messaging_rec *retry_queue;
50 struct irpc_list *irpc;
51 struct idr_context *idr;
53 struct timeval start_time;
54 struct tevent_timer *retry_te;
56 struct tevent_context *ev;
57 struct tevent_fd *fde;
61 /* we have a linked list of dispatch handlers for each msg_type that
62 this messaging server can deal with */
64 struct dispatch_fn *next, *prev;
70 /* an individual message */
71 struct messaging_rec {
72 struct messaging_rec *next, *prev;
73 struct messaging_context *msg;
76 struct messaging_header {
79 struct server_id from;
89 static void irpc_handler(struct messaging_context *, void *,
90 uint32_t, struct server_id, DATA_BLOB *);
94 A useful function for testing the message system.
96 static void ping_message(struct messaging_context *msg, void *private_data,
97 uint32_t msg_type, struct server_id src, DATA_BLOB *data)
99 DEBUG(1,("INFO: Received PING message from server %u.%u [%.*s]\n",
100 (unsigned int)src.node, (unsigned int)src.id, (int)data->length,
101 data->data?(const char *)data->data:""));
102 messaging_send(msg, src, MSG_PONG, data);
106 return uptime of messaging server via irpc
108 static NTSTATUS irpc_uptime(struct irpc_message *msg,
109 struct irpc_uptime *r)
111 struct messaging_context *ctx = talloc_get_type(msg->private_data, struct messaging_context);
112 *r->out.start_time = timeval_to_nttime(&ctx->start_time);
117 return the path to a messaging socket
119 static char *messaging_path(struct messaging_context *msg, struct server_id server_id)
121 TALLOC_CTX *tmp_ctx = talloc_new(msg);
122 const char *id = cluster_id_string(tmp_ctx, server_id);
127 s = talloc_asprintf(msg, "%s/msg.%s", msg->base_path, id);
128 talloc_steal(s, tmp_ctx);
133 dispatch a fully received message
135 note that this deliberately can match more than one message handler
136 per message. That allows a single messasging context to register
137 (for example) a debug handler for more than one piece of code
139 static void messaging_dispatch(struct messaging_context *msg, struct messaging_rec *rec)
141 struct dispatch_fn *d, *next;
143 /* temporary IDs use an idtree, the rest use a array of pointers */
144 if (rec->header->msg_type >= MSG_TMP_BASE) {
145 d = (struct dispatch_fn *)idr_find(msg->dispatch_tree,
146 rec->header->msg_type);
147 } else if (rec->header->msg_type < msg->num_types) {
148 d = msg->dispatch[rec->header->msg_type];
153 for (; d; d = next) {
156 data.data = rec->packet.data + sizeof(*rec->header);
157 data.length = rec->header->length;
158 d->fn(msg, d->private_data, d->msg_type, rec->header->from, &data);
160 rec->header->length = 0;
164 handler for messages that arrive from other nodes in the cluster
166 static void cluster_message_handler(struct messaging_context *msg, DATA_BLOB packet)
168 struct messaging_rec *rec;
170 rec = talloc(msg, struct messaging_rec);
172 smb_panic("Unable to allocate messaging_rec");
176 rec->path = msg->path;
177 rec->header = (struct messaging_header *)packet.data;
178 rec->packet = packet;
181 if (packet.length != sizeof(*rec->header) + rec->header->length) {
182 DEBUG(0,("messaging: bad message header size %d should be %d\n",
183 rec->header->length, (int)(packet.length - sizeof(*rec->header))));
188 messaging_dispatch(msg, rec);
195 try to send the message
197 static NTSTATUS try_send(struct messaging_rec *rec)
199 struct messaging_context *msg = rec->msg;
203 struct socket_address *path;
205 /* rec->path is the path of the *other* socket, where we want
207 path = socket_address_from_strings(msg, msg->sock->backend_name,
210 return NT_STATUS_NO_MEMORY;
213 /* we send with privileges so messages work from any context */
214 priv = root_privileges();
215 status = socket_sendto(msg->sock, &rec->packet, &nsent, path);
223 retry backed off messages
225 static void msg_retry_timer(struct tevent_context *ev, struct tevent_timer *te,
226 struct timeval t, void *private_data)
228 struct messaging_context *msg = talloc_get_type(private_data,
229 struct messaging_context);
230 msg->retry_te = NULL;
232 /* put the messages back on the main queue */
233 while (msg->retry_queue) {
234 struct messaging_rec *rec = msg->retry_queue;
235 DLIST_REMOVE(msg->retry_queue, rec);
236 DLIST_ADD_END(msg->pending, rec, struct messaging_rec *);
239 EVENT_FD_WRITEABLE(msg->event.fde);
243 handle a socket write event
245 static void messaging_send_handler(struct messaging_context *msg)
247 while (msg->pending) {
248 struct messaging_rec *rec = msg->pending;
250 status = try_send(rec);
251 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
253 if (rec->retries > 3) {
254 /* we're getting continuous write errors -
255 backoff this record */
256 DLIST_REMOVE(msg->pending, rec);
257 DLIST_ADD_END(msg->retry_queue, rec,
258 struct messaging_rec *);
259 if (msg->retry_te == NULL) {
261 event_add_timed(msg->event.ev, msg,
262 timeval_current_ofs(1, 0),
263 msg_retry_timer, msg);
269 if (!NT_STATUS_IS_OK(status)) {
270 TALLOC_CTX *tmp_ctx = talloc_new(msg);
271 DEBUG(1,("messaging: Lost message from %s to %s of type %u - %s\n",
272 cluster_id_string(tmp_ctx, rec->header->from),
273 cluster_id_string(tmp_ctx, rec->header->to),
274 rec->header->msg_type,
276 talloc_free(tmp_ctx);
278 DLIST_REMOVE(msg->pending, rec);
281 if (msg->pending == NULL) {
282 EVENT_FD_NOT_WRITEABLE(msg->event.fde);
287 handle a new incoming packet
289 static void messaging_recv_handler(struct messaging_context *msg)
291 struct messaging_rec *rec;
296 /* see how many bytes are in the next packet */
297 status = socket_pending(msg->sock, &msize);
298 if (!NT_STATUS_IS_OK(status)) {
299 DEBUG(0,("socket_pending failed in messaging - %s\n",
304 packet = data_blob_talloc(msg, NULL, msize);
305 if (packet.data == NULL) {
306 /* assume this is temporary and retry */
310 status = socket_recv(msg->sock, packet.data, msize, &msize);
311 if (!NT_STATUS_IS_OK(status)) {
312 data_blob_free(&packet);
316 if (msize < sizeof(*rec->header)) {
317 DEBUG(0,("messaging: bad message of size %d\n", (int)msize));
318 data_blob_free(&packet);
322 rec = talloc(msg, struct messaging_rec);
324 smb_panic("Unable to allocate messaging_rec");
327 talloc_steal(rec, packet.data);
329 rec->path = msg->path;
330 rec->header = (struct messaging_header *)packet.data;
331 rec->packet = packet;
334 if (msize != sizeof(*rec->header) + rec->header->length) {
335 DEBUG(0,("messaging: bad message header size %d should be %d\n",
336 rec->header->length, (int)(msize - sizeof(*rec->header))));
341 messaging_dispatch(msg, rec);
347 handle a socket event
349 static void messaging_handler(struct tevent_context *ev, struct tevent_fd *fde,
350 uint16_t flags, void *private_data)
352 struct messaging_context *msg = talloc_get_type(private_data,
353 struct messaging_context);
354 if (flags & EVENT_FD_WRITE) {
355 messaging_send_handler(msg);
357 if (flags & EVENT_FD_READ) {
358 messaging_recv_handler(msg);
364 Register a dispatch function for a particular message type.
366 NTSTATUS messaging_register(struct messaging_context *msg, void *private_data,
367 uint32_t msg_type, msg_callback_t fn)
369 struct dispatch_fn *d;
371 /* possibly expand dispatch array */
372 if (msg_type >= msg->num_types) {
373 struct dispatch_fn **dp;
375 dp = talloc_realloc(msg, msg->dispatch, struct dispatch_fn *, msg_type+1);
376 NT_STATUS_HAVE_NO_MEMORY(dp);
378 for (i=msg->num_types;i<=msg_type;i++) {
379 msg->dispatch[i] = NULL;
381 msg->num_types = msg_type+1;
384 d = talloc_zero(msg->dispatch, struct dispatch_fn);
385 NT_STATUS_HAVE_NO_MEMORY(d);
386 d->msg_type = msg_type;
387 d->private_data = private_data;
390 DLIST_ADD(msg->dispatch[msg_type], d);
396 register a temporary message handler. The msg_type is allocated
399 NTSTATUS messaging_register_tmp(struct messaging_context *msg, void *private_data,
400 msg_callback_t fn, uint32_t *msg_type)
402 struct dispatch_fn *d;
405 d = talloc_zero(msg->dispatch, struct dispatch_fn);
406 NT_STATUS_HAVE_NO_MEMORY(d);
407 d->private_data = private_data;
410 id = idr_get_new_above(msg->dispatch_tree, d, MSG_TMP_BASE, UINT16_MAX);
413 return NT_STATUS_TOO_MANY_CONTEXT_IDS;
416 d->msg_type = (uint32_t)id;
417 (*msg_type) = d->msg_type;
423 De-register the function for a particular message type.
425 void messaging_deregister(struct messaging_context *msg, uint32_t msg_type, void *private_data)
427 struct dispatch_fn *d, *next;
429 if (msg_type >= msg->num_types) {
430 d = (struct dispatch_fn *)idr_find(msg->dispatch_tree,
433 idr_remove(msg->dispatch_tree, msg_type);
438 for (d = msg->dispatch[msg_type]; d; d = next) {
440 if (d->private_data == private_data) {
441 DLIST_REMOVE(msg->dispatch[msg_type], d);
448 Send a message to a particular server
450 NTSTATUS messaging_send(struct messaging_context *msg, struct server_id server,
451 uint32_t msg_type, DATA_BLOB *data)
453 struct messaging_rec *rec;
455 size_t dlength = data?data->length:0;
457 rec = talloc(msg, struct messaging_rec);
459 return NT_STATUS_NO_MEMORY;
462 rec->packet = data_blob_talloc(rec, NULL, sizeof(*rec->header) + dlength);
463 if (rec->packet.data == NULL) {
465 return NT_STATUS_NO_MEMORY;
470 rec->header = (struct messaging_header *)rec->packet.data;
472 ZERO_STRUCTP(rec->header);
473 rec->header->version = MESSAGING_VERSION;
474 rec->header->msg_type = msg_type;
475 rec->header->from = msg->server_id;
476 rec->header->to = server;
477 rec->header->length = dlength;
479 memcpy(rec->packet.data + sizeof(*rec->header),
480 data->data, dlength);
483 if (!cluster_node_equal(&msg->server_id, &server)) {
484 /* the destination is on another node - dispatch via
486 status = cluster_message_send(server, &rec->packet);
491 rec->path = messaging_path(msg, server);
492 talloc_steal(rec, rec->path);
494 if (msg->pending != NULL) {
495 status = STATUS_MORE_ENTRIES;
497 status = try_send(rec);
500 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
501 if (msg->pending == NULL) {
502 EVENT_FD_WRITEABLE(msg->event.fde);
504 DLIST_ADD_END(msg->pending, rec, struct messaging_rec *);
514 Send a message to a particular server, with the message containing a single pointer
516 NTSTATUS messaging_send_ptr(struct messaging_context *msg, struct server_id server,
517 uint32_t msg_type, void *ptr)
521 blob.data = (uint8_t *)&ptr;
522 blob.length = sizeof(void *);
524 return messaging_send(msg, server, msg_type, &blob);
529 destroy the messaging context
531 static int messaging_destructor(struct messaging_context *msg)
534 while (msg->names && msg->names[0]) {
535 irpc_remove_name(msg, msg->names[0]);
541 create the listening socket and setup the dispatcher
543 struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx,
545 struct server_id server_id,
546 struct tevent_context *ev)
548 struct messaging_context *msg;
550 struct socket_address *path;
556 msg = talloc_zero(mem_ctx, struct messaging_context);
561 /* setup a handler for messages from other cluster nodes, if appropriate */
562 status = cluster_message_init(msg, server_id, cluster_message_handler);
563 if (!NT_STATUS_IS_OK(status)) {
568 /* create the messaging directory if needed */
571 msg->base_path = talloc_reference(msg, dir);
572 msg->path = messaging_path(msg, server_id);
573 msg->server_id = server_id;
574 msg->idr = idr_init(msg);
575 msg->dispatch_tree = idr_init(msg);
576 msg->start_time = timeval_current();
578 status = socket_create("unix", SOCKET_TYPE_DGRAM, &msg->sock, 0);
579 if (!NT_STATUS_IS_OK(status)) {
584 /* by stealing here we ensure that the socket is cleaned up (and even
586 talloc_steal(msg, msg->sock);
588 path = socket_address_from_strings(msg, msg->sock->backend_name,
595 status = socket_listen(msg->sock, path, 50, 0);
596 if (!NT_STATUS_IS_OK(status)) {
597 DEBUG(0,("Unable to setup messaging listener for '%s':%s\n", msg->path, nt_errstr(status)));
602 /* it needs to be non blocking for sends */
603 set_blocking(socket_get_fd(msg->sock), false);
606 msg->event.fde = event_add_fd(ev, msg, socket_get_fd(msg->sock),
607 EVENT_FD_READ, messaging_handler, msg);
608 tevent_fd_set_auto_close(msg->event.fde);
610 talloc_set_destructor(msg, messaging_destructor);
612 messaging_register(msg, NULL, MSG_PING, ping_message);
613 messaging_register(msg, NULL, MSG_IRPC, irpc_handler);
614 IRPC_REGISTER(msg, irpc, IRPC_UPTIME, irpc_uptime, msg);
620 A hack, for the short term until we get 'client only' messaging in place
622 struct messaging_context *messaging_client_init(TALLOC_CTX *mem_ctx,
624 struct tevent_context *ev)
628 id.id = random() % 0x10000000;
629 return messaging_init(mem_ctx, dir, id, ev);
632 a list of registered irpc server functions
635 struct irpc_list *next, *prev;
637 const struct ndr_interface_table *table;
645 register a irpc server function
647 NTSTATUS irpc_register(struct messaging_context *msg_ctx,
648 const struct ndr_interface_table *table,
649 int callnum, irpc_function_t fn, void *private_data)
651 struct irpc_list *irpc;
653 /* override an existing handler, if any */
654 for (irpc=msg_ctx->irpc; irpc; irpc=irpc->next) {
655 if (irpc->table == table && irpc->callnum == callnum) {
660 irpc = talloc(msg_ctx, struct irpc_list);
661 NT_STATUS_HAVE_NO_MEMORY(irpc);
662 DLIST_ADD(msg_ctx->irpc, irpc);
666 irpc->callnum = callnum;
668 irpc->private_data = private_data;
669 irpc->uuid = irpc->table->syntax_id.uuid;
676 handle an incoming irpc reply message
678 static void irpc_handler_reply(struct messaging_context *msg_ctx, struct irpc_message *m)
680 struct irpc_request *irpc;
681 enum ndr_err_code ndr_err;
683 irpc = (struct irpc_request *)idr_find(msg_ctx->idr, m->header.callid);
684 if (irpc == NULL) return;
686 /* parse the reply data */
687 ndr_err = irpc->table->calls[irpc->callnum].ndr_pull(m->ndr, NDR_OUT, irpc->r);
688 if (NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
689 irpc->status = m->header.status;
690 talloc_steal(irpc->mem_ctx, m);
692 irpc->status = ndr_map_error2ntstatus(ndr_err);
693 talloc_steal(irpc, m);
696 if (irpc->async.fn) {
697 irpc->async.fn(irpc);
704 NTSTATUS irpc_send_reply(struct irpc_message *m, NTSTATUS status)
706 struct ndr_push *push;
708 enum ndr_err_code ndr_err;
710 m->header.status = status;
712 /* setup the reply */
713 push = ndr_push_init_ctx(m->ndr);
715 status = NT_STATUS_NO_MEMORY;
719 m->header.flags |= IRPC_FLAG_REPLY;
721 /* construct the packet */
722 ndr_err = ndr_push_irpc_header(push, NDR_SCALARS|NDR_BUFFERS, &m->header);
723 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
724 status = ndr_map_error2ntstatus(ndr_err);
728 ndr_err = m->irpc->table->calls[m->irpc->callnum].ndr_push(push, NDR_OUT, m->data);
729 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
730 status = ndr_map_error2ntstatus(ndr_err);
734 /* send the reply message */
735 packet = ndr_push_blob(push);
736 status = messaging_send(m->msg_ctx, m->from, MSG_IRPC, &packet);
737 if (!NT_STATUS_IS_OK(status)) goto failed;
745 handle an incoming irpc request message
747 static void irpc_handler_request(struct messaging_context *msg_ctx,
748 struct irpc_message *m)
752 enum ndr_err_code ndr_err;
754 for (i=msg_ctx->irpc; i; i=i->next) {
755 if (GUID_equal(&i->uuid, &m->header.uuid) &&
756 i->table->syntax_id.if_version == m->header.if_version &&
757 i->callnum == m->header.callnum) {
763 /* no registered handler for this message */
768 /* allocate space for the structure */
769 r = talloc_zero_size(m->ndr, i->table->calls[m->header.callnum].struct_size);
770 if (r == NULL) goto failed;
772 /* parse the request data */
773 ndr_err = i->table->calls[i->callnum].ndr_pull(m->ndr, NDR_IN, r);
774 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) goto failed;
777 m->private_data= i->private_data;
778 m->defer_reply = false;
779 m->msg_ctx = msg_ctx;
782 m->ev = msg_ctx->event.ev;
784 m->header.status = i->fn(m, r);
786 if (m->defer_reply) {
787 /* the server function has asked to defer the reply to later */
788 talloc_steal(msg_ctx, m);
792 irpc_send_reply(m, m->header.status);
800 handle an incoming irpc message
802 static void irpc_handler(struct messaging_context *msg_ctx, void *private_data,
803 uint32_t msg_type, struct server_id src, DATA_BLOB *packet)
805 struct irpc_message *m;
806 enum ndr_err_code ndr_err;
808 m = talloc(msg_ctx, struct irpc_message);
809 if (m == NULL) goto failed;
813 m->ndr = ndr_pull_init_blob(packet, m);
814 if (m->ndr == NULL) goto failed;
816 m->ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
818 ndr_err = ndr_pull_irpc_header(m->ndr, NDR_BUFFERS|NDR_SCALARS, &m->header);
819 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) goto failed;
821 if (m->header.flags & IRPC_FLAG_REPLY) {
822 irpc_handler_reply(msg_ctx, m);
824 irpc_handler_request(msg_ctx, m);
834 destroy a irpc request
836 static int irpc_destructor(struct irpc_request *irpc)
838 if (irpc->callid != -1) {
839 idr_remove(irpc->msg_ctx->idr, irpc->callid);
843 if (irpc->reject_free) {
850 timeout a irpc request
852 static void irpc_timeout(struct tevent_context *ev, struct tevent_timer *te,
853 struct timeval t, void *private_data)
855 struct irpc_request *irpc = talloc_get_type(private_data, struct irpc_request);
856 irpc->status = NT_STATUS_IO_TIMEOUT;
858 if (irpc->async.fn) {
859 irpc->async.fn(irpc);
865 make a irpc call - async send
867 struct irpc_request *irpc_call_send(struct messaging_context *msg_ctx,
868 struct server_id server_id,
869 const struct ndr_interface_table *table,
870 int callnum, void *r, TALLOC_CTX *ctx)
872 struct irpc_header header;
873 struct ndr_push *ndr;
876 struct irpc_request *irpc;
877 enum ndr_err_code ndr_err;
879 irpc = talloc(msg_ctx, struct irpc_request);
880 if (irpc == NULL) goto failed;
882 irpc->msg_ctx = msg_ctx;
884 irpc->callnum = callnum;
885 irpc->callid = idr_get_new(msg_ctx->idr, irpc, UINT16_MAX);
886 if (irpc->callid == -1) goto failed;
889 irpc->async.fn = NULL;
891 irpc->reject_free = false;
893 talloc_set_destructor(irpc, irpc_destructor);
895 /* setup the header */
896 header.uuid = table->syntax_id.uuid;
898 header.if_version = table->syntax_id.if_version;
899 header.callid = irpc->callid;
900 header.callnum = callnum;
902 header.status = NT_STATUS_OK;
904 /* construct the irpc packet */
905 ndr = ndr_push_init_ctx(irpc);
906 if (ndr == NULL) goto failed;
908 ndr_err = ndr_push_irpc_header(ndr, NDR_SCALARS|NDR_BUFFERS, &header);
909 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) goto failed;
911 ndr_err = table->calls[callnum].ndr_push(ndr, NDR_IN, r);
912 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) goto failed;
915 packet = ndr_push_blob(ndr);
916 status = messaging_send(msg_ctx, server_id, MSG_IRPC, &packet);
917 if (!NT_STATUS_IS_OK(status)) goto failed;
919 event_add_timed(msg_ctx->event.ev, irpc,
920 timeval_current_ofs(IRPC_CALL_TIMEOUT, 0),
932 wait for a irpc reply
934 NTSTATUS irpc_call_recv(struct irpc_request *irpc)
938 NT_STATUS_HAVE_NO_MEMORY(irpc);
940 irpc->reject_free = true;
942 while (!irpc->done) {
943 if (event_loop_once(irpc->msg_ctx->event.ev) != 0) {
944 return NT_STATUS_CONNECTION_DISCONNECTED;
948 irpc->reject_free = false;
950 status = irpc->status;
956 perform a synchronous irpc request
958 NTSTATUS irpc_call(struct messaging_context *msg_ctx,
959 struct server_id server_id,
960 const struct ndr_interface_table *table,
961 int callnum, void *r,
964 struct irpc_request *irpc = irpc_call_send(msg_ctx, server_id,
965 table, callnum, r, mem_ctx);
966 return irpc_call_recv(irpc);
970 open the naming database
972 static struct tdb_wrap *irpc_namedb_open(struct messaging_context *msg_ctx)
975 char *path = talloc_asprintf(msg_ctx, "%s/names.tdb", msg_ctx->base_path);
979 t = tdb_wrap_open(msg_ctx, path, 0, 0, O_RDWR|O_CREAT, 0660);
986 add a string name that this irpc server can be called on
988 NTSTATUS irpc_add_name(struct messaging_context *msg_ctx, const char *name)
993 NTSTATUS status = NT_STATUS_OK;
995 t = irpc_namedb_open(msg_ctx);
996 NT_STATUS_HAVE_NO_MEMORY(t);
998 if (tdb_lock_bystring(t->tdb, name) != 0) {
1000 return NT_STATUS_LOCK_NOT_GRANTED;
1002 rec = tdb_fetch_bystring(t->tdb, name);
1003 count = rec.dsize / sizeof(struct server_id);
1004 rec.dptr = (unsigned char *)realloc_p(rec.dptr, struct server_id, count+1);
1005 rec.dsize += sizeof(struct server_id);
1006 if (rec.dptr == NULL) {
1007 tdb_unlock_bystring(t->tdb, name);
1009 return NT_STATUS_NO_MEMORY;
1011 ((struct server_id *)rec.dptr)[count] = msg_ctx->server_id;
1012 if (tdb_store_bystring(t->tdb, name, rec, 0) != 0) {
1013 status = NT_STATUS_INTERNAL_ERROR;
1016 tdb_unlock_bystring(t->tdb, name);
1019 msg_ctx->names = str_list_add(msg_ctx->names, name);
1020 talloc_steal(msg_ctx, msg_ctx->names);
1026 return a list of server ids for a server name
1028 struct server_id *irpc_servers_byname(struct messaging_context *msg_ctx,
1029 TALLOC_CTX *mem_ctx,
1035 struct server_id *ret;
1037 t = irpc_namedb_open(msg_ctx);
1042 if (tdb_lock_bystring(t->tdb, name) != 0) {
1046 rec = tdb_fetch_bystring(t->tdb, name);
1047 if (rec.dptr == NULL) {
1048 tdb_unlock_bystring(t->tdb, name);
1052 count = rec.dsize / sizeof(struct server_id);
1053 ret = talloc_array(mem_ctx, struct server_id, count+1);
1055 tdb_unlock_bystring(t->tdb, name);
1059 for (i=0;i<count;i++) {
1060 ret[i] = ((struct server_id *)rec.dptr)[i];
1062 ret[i] = cluster_id(0, 0);
1064 tdb_unlock_bystring(t->tdb, name);
1071 remove a name from a messaging context
1073 void irpc_remove_name(struct messaging_context *msg_ctx, const char *name)
1078 struct server_id *ids;
1080 str_list_remove(msg_ctx->names, name);
1082 t = irpc_namedb_open(msg_ctx);
1087 if (tdb_lock_bystring(t->tdb, name) != 0) {
1091 rec = tdb_fetch_bystring(t->tdb, name);
1092 if (rec.dptr == NULL) {
1093 tdb_unlock_bystring(t->tdb, name);
1097 count = rec.dsize / sizeof(struct server_id);
1100 tdb_unlock_bystring(t->tdb, name);
1104 ids = (struct server_id *)rec.dptr;
1105 for (i=0;i<count;i++) {
1106 if (cluster_id_equal(&ids[i], &msg_ctx->server_id)) {
1108 memmove(ids+i, ids+i+1,
1109 sizeof(struct server_id) * (count-(i+1)));
1111 rec.dsize -= sizeof(struct server_id);
1115 tdb_store_bystring(t->tdb, name, rec, 0);
1117 tdb_unlock_bystring(t->tdb, name);
1121 struct server_id messaging_get_server_id(struct messaging_context *msg_ctx)
1123 return msg_ctx->server_id;