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 "system/time.h"
28 #include "dlinklist.h"
29 #include "lib/socket/socket.h"
31 /* change the message version with any incompatible changes in the protocol */
32 #define MESSAGING_VERSION 1
34 struct messaging_context {
36 struct socket_context *sock;
38 struct dispatch_fn *dispatch;
39 struct messaging_rec *pending;
42 struct event_context *ev;
47 /* we have a linked list of dispatch handlers that this messaging
48 server can deal with */
50 struct dispatch_fn *next, *prev;
53 void (*fn)(struct messaging_context *msg, void *private,
54 uint32_t msg_type, uint32_t server_id, DATA_BLOB *data);
57 /* an individual message */
58 struct messaging_rec {
59 struct messaging_rec *next, *prev;
60 struct messaging_context *msg;
63 struct messaging_header {
76 A useful function for testing the message system.
78 static void ping_message(struct messaging_context *msg, void *private,
79 uint32_t msg_type, uint32_t src, DATA_BLOB *data)
81 DEBUG(1,("INFO: Received PING message from server %u [%.*s]\n",
82 (uint_t)src, data->length, data->data?(const char *)data->data:""));
83 messaging_send(msg, src, MSG_PONG, data);
87 return the path to a messaging socket
89 static char *messaging_path(TALLOC_CTX *mem_ctx, uint32_t server_id)
91 char *name = talloc_asprintf(mem_ctx, "messaging/msg.%u", (unsigned)server_id);
93 ret = smbd_tmp_path(mem_ctx, name);
99 dispatch a fully received message
101 static void messaging_dispatch(struct messaging_context *msg, struct messaging_rec *rec)
103 struct dispatch_fn *d, *next;
104 for (d=msg->dispatch;d;d=next) {
106 if (d->msg_type == rec->header->msg_type) {
108 data.data = rec->packet.data + sizeof(*rec->header);
109 data.length = rec->header->length;
110 d->fn(msg, d->private, d->msg_type, rec->header->from, &data);
113 rec->header->length = 0;
118 try to send the message
120 static NTSTATUS try_send(struct messaging_rec *rec)
122 struct messaging_context *msg = rec->msg;
127 /* we send with privileges so messages work from any context */
128 priv = root_privileges();
129 status = socket_sendto(msg->sock, &rec->packet, &nsent, 0, rec->path, 0);
136 handle a socket write event
138 static void messaging_send_handler(struct messaging_context *msg)
140 while (msg->pending) {
141 struct messaging_rec *rec = msg->pending;
143 status = try_send(rec);
144 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
147 if (!NT_STATUS_IS_OK(status)) {
148 DEBUG(1,("messaging: Lost message from %u to %u of type %u - %s\n",
149 rec->header->from, rec->header->to, rec->header->msg_type,
152 DLIST_REMOVE(msg->pending, rec);
155 if (msg->pending == NULL) {
156 EVENT_FD_NOT_WRITEABLE(msg->event.fde);
161 handle a new incoming packet
163 static void messaging_recv_handler(struct messaging_context *msg)
165 struct messaging_rec *rec;
170 /* see how many bytes are in the next packet */
171 status = socket_pending(msg->sock, &msize);
172 if (!NT_STATUS_IS_OK(status)) {
173 DEBUG(0,("socket_pending failed in messaging - %s\n",
178 packet = data_blob_talloc(msg, NULL, msize);
179 if (packet.data == NULL) {
180 /* assume this is temporary and retry */
184 status = socket_recv(msg->sock, packet.data, msize, &msize, 0);
185 if (!NT_STATUS_IS_OK(status)) {
186 data_blob_free(&packet);
190 if (msize < sizeof(*rec->header)) {
191 DEBUG(0,("messaging: bad message of size %d\n", msize));
192 data_blob_free(&packet);
196 rec = talloc(msg, struct messaging_rec);
198 smb_panic("Unable to allocate messaging_rec");
201 talloc_steal(rec, packet.data);
203 rec->path = msg->path;
204 rec->header = (struct messaging_header *)packet.data;
205 rec->packet = packet;
207 if (msize != sizeof(*rec->header) + rec->header->length) {
208 DEBUG(0,("messaging: bad message header size %d should be %d\n",
209 rec->header->length, msize - sizeof(*rec->header)));
214 messaging_dispatch(msg, rec);
220 handle a socket event
222 static void messaging_handler(struct event_context *ev, struct fd_event *fde,
223 uint16_t flags, void *private)
225 struct messaging_context *msg = talloc_get_type(private,
226 struct messaging_context);
227 if (flags & EVENT_FD_WRITE) {
228 messaging_send_handler(msg);
230 if (flags & EVENT_FD_READ) {
231 messaging_recv_handler(msg);
237 Register a dispatch function for a particular message type.
239 void messaging_register(struct messaging_context *msg, void *private,
241 void (*fn)(struct messaging_context *, void *, uint32_t, uint32_t, DATA_BLOB *))
243 struct dispatch_fn *d;
245 d = talloc(msg, struct dispatch_fn);
246 d->msg_type = msg_type;
247 d->private = private;
249 DLIST_ADD(msg->dispatch, d);
253 De-register the function for a particular message type.
255 void messaging_deregister(struct messaging_context *msg, uint32_t msg_type, void *private)
257 struct dispatch_fn *d, *next;
259 for (d = msg->dispatch; d; d = next) {
261 if (d->msg_type == msg_type &&
262 d->private == private) {
263 DLIST_REMOVE(msg->dispatch, d);
271 Send a message to a particular server
273 NTSTATUS messaging_send(struct messaging_context *msg, uint32_t server,
274 uint32_t msg_type, DATA_BLOB *data)
276 struct messaging_rec *rec;
278 size_t dlength = data?data->length:0;
280 rec = talloc(msg, struct messaging_rec);
282 return NT_STATUS_NO_MEMORY;
285 rec->packet = data_blob_talloc(rec, NULL, sizeof(*rec->header) + dlength);
286 if (rec->packet.data == NULL) {
288 return NT_STATUS_NO_MEMORY;
292 rec->header = (struct messaging_header *)rec->packet.data;
293 rec->header->version = MESSAGING_VERSION;
294 rec->header->msg_type = msg_type;
295 rec->header->from = msg->server_id;
296 rec->header->to = server;
297 rec->header->length = dlength;
299 memcpy(rec->packet.data + sizeof(*rec->header),
300 data->data, dlength);
303 rec->path = messaging_path(rec, server);
305 status = try_send(rec);
306 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
307 if (msg->pending == NULL) {
308 EVENT_FD_WRITEABLE(msg->event.fde);
310 DLIST_ADD(msg->pending, rec);
320 Send a message to a particular server, with the message containing a single pointer
322 NTSTATUS messaging_send_ptr(struct messaging_context *msg, uint32_t server,
323 uint32_t msg_type, void *ptr)
327 blob.data = (void *)&ptr;
328 blob.length = sizeof(void *);
330 return messaging_send(msg, server, msg_type, &blob);
335 destroy the messaging context
337 static int messaging_destructor(void *ptr)
339 struct messaging_context *msg = ptr;
345 create the listening socket and setup the dispatcher
347 struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx, uint32_t server_id,
348 struct event_context *ev)
350 struct messaging_context *msg;
354 msg = talloc(mem_ctx, struct messaging_context);
359 /* create the messaging directory if needed */
360 path = smbd_tmp_path(msg, "messaging");
364 msg->path = messaging_path(msg, server_id);
365 msg->server_id = server_id;
366 msg->dispatch = NULL;
369 status = socket_create("unix", SOCKET_TYPE_DGRAM, &msg->sock, 0);
370 if (!NT_STATUS_IS_OK(status)) {
375 /* by stealing here we ensure that the socket is cleaned up (and even
377 talloc_steal(msg, msg->sock);
379 status = socket_listen(msg->sock, msg->path, 0, 50, 0);
380 if (!NT_STATUS_IS_OK(status)) {
381 DEBUG(0,("Unable to setup messaging listener for '%s'\n", msg->path));
386 /* it needs to be non blocking for sends */
387 set_blocking(socket_get_fd(msg->sock), False);
389 msg->event.ev = talloc_reference(msg, ev);
390 msg->event.fde = event_add_fd(ev, msg, socket_get_fd(msg->sock),
391 EVENT_FD_READ, messaging_handler, msg);
393 talloc_set_destructor(msg, messaging_destructor);
395 messaging_register(msg, NULL, MSG_PING, ping_message);