54657d8d56380a88e6ff20266a5a7905499b0bfa
[samba.git] / source3 / lib / messages.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba internal messaging functions
4    Copyright (C) Andrew Tridgell 2000
5    Copyright (C) 2001 by Martin Pool
6    Copyright (C) 2002 by Jeremy Allison
7    Copyright (C) 2007 by Volker Lendecke
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 /**
25   @defgroup messages Internal messaging framework
26   @{
27   @file messages.c
28   
29   @brief  Module for internal messaging between Samba daemons. 
30
31    The idea is that if a part of Samba wants to do communication with
32    another Samba process then it will do a message_register() of a
33    dispatch function, and use message_send_pid() to send messages to
34    that process.
35
36    The dispatch function is given the pid of the sender, and it can
37    use that to reply by message_send_pid().  See ping_message() for a
38    simple example.
39
40    @caution Dispatch functions must be able to cope with incoming
41    messages on an *odd* byte boundary.
42
43    This system doesn't have any inherent size limitations but is not
44    very efficient for large messages or when messages are sent in very
45    quick succession.
46
47 */
48
49 #include "includes.h"
50 #include "librpc/gen_ndr/messaging.h"
51 #include "librpc/gen_ndr/ndr_messaging.h"
52
53 struct messaging_callback {
54         struct messaging_callback *prev, *next;
55         uint32 msg_type;
56         void (*fn)(struct messaging_context *msg, void *private_data, 
57                    uint32_t msg_type, 
58                    struct server_id server_id, DATA_BLOB *data);
59         void *private_data;
60 };
61
62 /****************************************************************************
63  A useful function for testing the message system.
64 ****************************************************************************/
65
66 static void ping_message(struct messaging_context *msg_ctx,
67                          void *private_data,
68                          uint32_t msg_type,
69                          struct server_id src,
70                          DATA_BLOB *data)
71 {
72         const char *msg = data->data ? (const char *)data->data : "none";
73
74         DEBUG(1,("INFO: Received PING message from PID %s [%s]\n",
75                  procid_str_static(&src), msg));
76         messaging_send(msg_ctx, src, MSG_PONG, data);
77 }
78
79 /****************************************************************************
80  Register/replace a dispatch function for a particular message type.
81  JRA changed Dec 13 2006. Only one message handler now permitted per type.
82  *NOTE*: Dispatch functions must be able to cope with incoming
83  messages on an *odd* byte boundary.
84 ****************************************************************************/
85
86 struct msg_all {
87         struct messaging_context *msg_ctx;
88         int msg_type;
89         uint32 msg_flag;
90         const void *buf;
91         size_t len;
92         int n_sent;
93 };
94
95 /****************************************************************************
96  Send one of the messages for the broadcast.
97 ****************************************************************************/
98
99 static int traverse_fn(struct db_record *rec,
100                        const struct connections_key *ckey,
101                        const struct connections_data *crec,
102                        void *state)
103 {
104         struct msg_all *msg_all = (struct msg_all *)state;
105         NTSTATUS status;
106
107         if (crec->cnum != -1)
108                 return 0;
109
110         /* Don't send if the receiver hasn't registered an interest. */
111
112         if(!(crec->bcast_msg_flags & msg_all->msg_flag))
113                 return 0;
114
115         /* If the msg send fails because the pid was not found (i.e. smbd died), 
116          * the msg has already been deleted from the messages.tdb.*/
117
118         status = messaging_send_buf(msg_all->msg_ctx,
119                                     crec->pid, msg_all->msg_type,
120                                     (uint8 *)msg_all->buf, msg_all->len);
121
122         if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
123                 
124                 /* If the pid was not found delete the entry from connections.tdb */
125
126                 DEBUG(2,("pid %s doesn't exist - deleting connections %d [%s]\n",
127                          procid_str_static(&crec->pid), crec->cnum,
128                          crec->servicename));
129
130                 rec->delete_rec(rec);
131         }
132         msg_all->n_sent++;
133         return 0;
134 }
135
136 /**
137  * Send a message to all smbd processes.
138  *
139  * It isn't very efficient, but should be OK for the sorts of
140  * applications that use it. When we need efficient broadcast we can add
141  * it.
142  *
143  * @param n_sent Set to the number of messages sent.  This should be
144  * equal to the number of processes, but be careful for races.
145  *
146  * @retval True for success.
147  **/
148 BOOL message_send_all(struct messaging_context *msg_ctx,
149                       int msg_type,
150                       const void *buf, size_t len,
151                       int *n_sent)
152 {
153         struct msg_all msg_all;
154
155         msg_all.msg_type = msg_type;
156         if (msg_type < 1000)
157                 msg_all.msg_flag = FLAG_MSG_GENERAL;
158         else if (msg_type > 1000 && msg_type < 2000)
159                 msg_all.msg_flag = FLAG_MSG_NMBD;
160         else if (msg_type > 2000 && msg_type < 2100)
161                 msg_all.msg_flag = FLAG_MSG_PRINT_NOTIFY;
162         else if (msg_type > 2100 && msg_type < 3000)
163                 msg_all.msg_flag = FLAG_MSG_PRINT_GENERAL;
164         else if (msg_type > 3000 && msg_type < 4000)
165                 msg_all.msg_flag = FLAG_MSG_SMBD;
166         else
167                 return False;
168
169         msg_all.buf = buf;
170         msg_all.len = len;
171         msg_all.n_sent = 0;
172         msg_all.msg_ctx = msg_ctx;
173
174         connections_forall(traverse_fn, &msg_all);
175         if (n_sent)
176                 *n_sent = msg_all.n_sent;
177         return True;
178 }
179
180 struct event_context *messaging_event_context(struct messaging_context *msg_ctx)
181 {
182         return msg_ctx->event_ctx;
183 }
184
185 struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx, 
186                                          struct server_id server_id, 
187                                          struct event_context *ev)
188 {
189         struct messaging_context *ctx;
190         NTSTATUS status;
191
192         if (!(ctx = TALLOC_ZERO_P(mem_ctx, struct messaging_context))) {
193                 return NULL;
194         }
195
196         ctx->id = server_id;
197         ctx->event_ctx = ev;
198
199         status = messaging_tdb_init(ctx, ctx, &ctx->local);
200
201         if (!NT_STATUS_IS_OK(status)) {
202                 DEBUG(0, ("messaging_tdb_init failed: %s\n",
203                           nt_errstr(status)));
204                 TALLOC_FREE(ctx);
205                 return NULL;
206         }
207
208 #ifdef CLUSTER_SUPPORT
209         if (lp_clustering()) {
210                 status = messaging_ctdbd_init(ctx, ctx, &ctx->remote);
211
212                 if (!NT_STATUS_IS_OK(status)) {
213                         DEBUG(0, ("messaging_ctdb_init failed: %s\n",
214                                   nt_errstr(status)));
215                         TALLOC_FREE(ctx);
216                         return NULL;
217                 }
218         }
219 #endif
220
221         messaging_register(ctx, NULL, MSG_PING, ping_message);
222
223         /* Register some debugging related messages */
224
225         register_msg_pool_usage(ctx);
226         register_dmalloc_msgs(ctx);
227         debug_register_msgs(ctx);
228
229         return ctx;
230 }
231
232 /*
233  * re-init after a fork
234  */
235 NTSTATUS messaging_reinit(struct messaging_context *msg_ctx)
236 {
237 #ifdef CLUSTER_SUPPORT
238
239         TALLOC_FREE(msg_ctx->remote);
240
241         if (lp_clustering()) {
242                 NTSTATUS status;
243
244                 status = messaging_ctdbd_init(msg_ctx, msg_ctx,
245                                               &msg_ctx->remote);
246
247                 if (!NT_STATUS_IS_OK(status)) {
248                         DEBUG(0, ("messaging_ctdb_init failed: %s\n",
249                                   nt_errstr(status)));
250                         return status;
251                 }
252         }
253
254 #endif
255
256         return NT_STATUS_OK;
257 }
258
259
260 /*
261  * Register a dispatch function for a particular message type. Allow multiple
262  * registrants
263 */
264 NTSTATUS messaging_register(struct messaging_context *msg_ctx,
265                             void *private_data,
266                             uint32_t msg_type,
267                             void (*fn)(struct messaging_context *msg,
268                                        void *private_data, 
269                                        uint32_t msg_type, 
270                                        struct server_id server_id,
271                                        DATA_BLOB *data))
272 {
273         struct messaging_callback *cb;
274
275         /*
276          * Only one callback per type
277          */
278
279         for (cb = msg_ctx->callbacks; cb != NULL; cb = cb->next) {
280                 if (cb->msg_type == msg_type) {
281                         cb->fn = fn;
282                         cb->private_data = private_data;
283                         return NT_STATUS_OK;
284                 }
285         }
286
287         if (!(cb = talloc(msg_ctx, struct messaging_callback))) {
288                 return NT_STATUS_NO_MEMORY;
289         }
290
291         cb->msg_type = msg_type;
292         cb->fn = fn;
293         cb->private_data = private_data;
294
295         DLIST_ADD(msg_ctx->callbacks, cb);
296         return NT_STATUS_OK;
297 }
298
299 /*
300   De-register the function for a particular message type.
301 */
302 void messaging_deregister(struct messaging_context *ctx, uint32_t msg_type,
303                           void *private_data)
304 {
305         struct messaging_callback *cb, *next;
306
307         for (cb = ctx->callbacks; cb; cb = next) {
308                 next = cb->next;
309                 if ((cb->msg_type == msg_type)
310                     && (cb->private_data == private_data)) {
311                         DLIST_REMOVE(ctx->callbacks, cb);
312                         TALLOC_FREE(cb);
313                 }
314         }
315 }
316
317 /*
318   Send a message to a particular server
319 */
320 NTSTATUS messaging_send(struct messaging_context *msg_ctx,
321                         struct server_id server, uint32_t msg_type,
322                         const DATA_BLOB *data)
323 {
324         return msg_ctx->local->send_fn(msg_ctx, server, msg_type, data,
325                                        msg_ctx->local);
326 }
327
328 NTSTATUS messaging_send_buf(struct messaging_context *msg_ctx,
329                             struct server_id server, uint32_t msg_type,
330                             const uint8 *buf, size_t len)
331 {
332         DATA_BLOB blob = data_blob_const(buf, len);
333         return messaging_send(msg_ctx, server, msg_type, &blob);
334 }
335
336 /*
337   Dispatch one messsaging_rec
338 */
339 void messaging_dispatch_rec(struct messaging_context *msg_ctx,
340                             struct messaging_rec *rec)
341 {
342         struct messaging_callback *cb, *next;
343
344         for (cb = msg_ctx->callbacks; cb != NULL; cb = next) {
345                 next = cb->next;
346                 if (cb->msg_type == rec->msg_type) {
347                         cb->fn(msg_ctx, cb->private_data, rec->msg_type,
348                                rec->src, &rec->buf);
349                         return;
350                 }
351         }
352         return;
353 }
354
355 /** @} **/