s3: Fix some nonempty blank lines
[ambi/samba-autobuild/.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 3 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, see <http://www.gnu.org/licenses/>.
21 */
22
23 /**
24   @defgroup messages Internal messaging framework
25   @{
26   @file messages.c
27
28   @brief  Module for internal messaging between Samba daemons. 
29
30    The idea is that if a part of Samba wants to do communication with
31    another Samba process then it will do a message_register() of a
32    dispatch function, and use message_send_pid() to send messages to
33    that process.
34
35    The dispatch function is given the pid of the sender, and it can
36    use that to reply by message_send_pid().  See ping_message() for a
37    simple example.
38
39    @caution Dispatch functions must be able to cope with incoming
40    messages on an *odd* byte boundary.
41
42    This system doesn't have any inherent size limitations but is not
43    very efficient for large messages or when messages are sent in very
44    quick succession.
45
46 */
47
48 #include "includes.h"
49 #include "dbwrap/dbwrap.h"
50 #include "serverid.h"
51 #include "messages.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, const struct server_id *id,
100                        uint32_t msg_flags, void *state)
101 {
102         struct msg_all *msg_all = (struct msg_all *)state;
103         NTSTATUS status;
104
105         /* Don't send if the receiver hasn't registered an interest. */
106
107         if((msg_flags & msg_all->msg_flag) == 0) {
108                 return 0;
109         }
110
111         /* If the msg send fails because the pid was not found (i.e. smbd died), 
112          * the msg has already been deleted from the messages.tdb.*/
113
114         status = messaging_send_buf(msg_all->msg_ctx, *id, msg_all->msg_type,
115                                     (const uint8 *)msg_all->buf, msg_all->len);
116
117         if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
118
119                 /*
120                  * If the pid was not found delete the entry from
121                  * serverid.tdb
122                  */
123
124                 DEBUG(2, ("pid %s doesn't exist\n", procid_str_static(id)));
125
126                 dbwrap_record_delete(rec);
127         }
128         msg_all->n_sent++;
129         return 0;
130 }
131
132 /**
133  * Send a message to all smbd processes.
134  *
135  * It isn't very efficient, but should be OK for the sorts of
136  * applications that use it. When we need efficient broadcast we can add
137  * it.
138  *
139  * @param n_sent Set to the number of messages sent.  This should be
140  * equal to the number of processes, but be careful for races.
141  *
142  * @retval True for success.
143  **/
144 bool message_send_all(struct messaging_context *msg_ctx,
145                       int msg_type,
146                       const void *buf, size_t len,
147                       int *n_sent)
148 {
149         struct msg_all msg_all;
150
151         msg_all.msg_type = msg_type;
152         if (msg_type < 0x100) {
153                 msg_all.msg_flag = FLAG_MSG_GENERAL;
154         } else if (msg_type > 0x100 && msg_type < 0x200) {
155                 msg_all.msg_flag = FLAG_MSG_NMBD;
156         } else if (msg_type > 0x200 && msg_type < 0x300) {
157                 msg_all.msg_flag = FLAG_MSG_PRINT_GENERAL;
158         } else if (msg_type > 0x300 && msg_type < 0x400) {
159                 msg_all.msg_flag = FLAG_MSG_SMBD;
160         } else if (msg_type > 0x400 && msg_type < 0x600) {
161                 msg_all.msg_flag = FLAG_MSG_WINBIND;
162         } else if (msg_type > 4000 && msg_type < 5000) {
163                 msg_all.msg_flag = FLAG_MSG_DBWRAP;
164         } else {
165                 return false;
166         }
167
168         msg_all.buf = buf;
169         msg_all.len = len;
170         msg_all.n_sent = 0;
171         msg_all.msg_ctx = msg_ctx;
172
173         serverid_traverse(traverse_fn, &msg_all);
174         if (n_sent)
175                 *n_sent = msg_all.n_sent;
176         return true;
177 }
178
179 struct event_context *messaging_event_context(struct messaging_context *msg_ctx)
180 {
181         return msg_ctx->event_ctx;
182 }
183
184 struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx, 
185                                          struct server_id server_id, 
186                                          struct event_context *ev)
187 {
188         struct messaging_context *ctx;
189         NTSTATUS status;
190
191         if (!(ctx = talloc_zero(mem_ctx, struct messaging_context))) {
192                 return NULL;
193         }
194
195         ctx->id = server_id;
196         ctx->event_ctx = ev;
197
198         status = messaging_tdb_init(ctx, ctx, &ctx->local);
199
200         if (!NT_STATUS_IS_OK(status)) {
201                 DEBUG(2, ("messaging_tdb_init failed: %s\n",
202                           nt_errstr(status)));
203                 TALLOC_FREE(ctx);
204                 return NULL;
205         }
206
207 #ifdef CLUSTER_SUPPORT
208         if (lp_clustering()) {
209                 status = messaging_ctdbd_init(ctx, ctx, &ctx->remote);
210
211                 if (!NT_STATUS_IS_OK(status)) {
212                         DEBUG(2, ("messaging_ctdbd_init failed: %s\n",
213                                   nt_errstr(status)));
214                         TALLOC_FREE(ctx);
215                         return NULL;
216                 }
217         }
218         ctx->id.vnn = get_my_vnn();
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 struct server_id messaging_server_id(const struct messaging_context *msg_ctx)
233 {
234         return msg_ctx->id;
235 }
236
237 /*
238  * re-init after a fork
239  */
240 NTSTATUS messaging_reinit(struct messaging_context *msg_ctx,
241                           struct server_id id)
242 {
243         NTSTATUS status;
244
245         TALLOC_FREE(msg_ctx->local);
246
247         msg_ctx->id = id;
248
249         status = messaging_tdb_init(msg_ctx, msg_ctx, &msg_ctx->local);
250         if (!NT_STATUS_IS_OK(status)) {
251                 DEBUG(0, ("messaging_tdb_init failed: %s\n",
252                           nt_errstr(status)));
253                 return status;
254         }
255
256 #ifdef CLUSTER_SUPPORT
257         TALLOC_FREE(msg_ctx->remote);
258
259         if (lp_clustering()) {
260                 status = messaging_ctdbd_init(msg_ctx, msg_ctx,
261                                               &msg_ctx->remote);
262
263                 if (!NT_STATUS_IS_OK(status)) {
264                         DEBUG(1, ("messaging_ctdbd_init failed: %s\n",
265                                   nt_errstr(status)));
266                         return status;
267                 }
268         }
269
270 #endif
271
272         return NT_STATUS_OK;
273 }
274
275
276 /*
277  * Register a dispatch function for a particular message type. Allow multiple
278  * registrants
279 */
280 NTSTATUS messaging_register(struct messaging_context *msg_ctx,
281                             void *private_data,
282                             uint32_t msg_type,
283                             void (*fn)(struct messaging_context *msg,
284                                        void *private_data, 
285                                        uint32_t msg_type, 
286                                        struct server_id server_id,
287                                        DATA_BLOB *data))
288 {
289         struct messaging_callback *cb;
290
291         /*
292          * Only one callback per type
293          */
294
295         for (cb = msg_ctx->callbacks; cb != NULL; cb = cb->next) {
296                 /* we allow a second registration of the same message
297                    type if it has a different private pointer. This is
298                    needed in, for example, the internal notify code,
299                    which creates a new notify context for each tree
300                    connect, and expects to receive messages to each of
301                    them. */
302                 if (cb->msg_type == msg_type && private_data == cb->private_data) {
303                         DEBUG(5,("Overriding messaging pointer for type %u - private_data=%p\n",
304                                   (unsigned)msg_type, private_data));
305                         cb->fn = fn;
306                         cb->private_data = private_data;
307                         return NT_STATUS_OK;
308                 }
309         }
310
311         if (!(cb = talloc(msg_ctx, struct messaging_callback))) {
312                 return NT_STATUS_NO_MEMORY;
313         }
314
315         cb->msg_type = msg_type;
316         cb->fn = fn;
317         cb->private_data = private_data;
318
319         DLIST_ADD(msg_ctx->callbacks, cb);
320         return NT_STATUS_OK;
321 }
322
323 /*
324   De-register the function for a particular message type.
325 */
326 void messaging_deregister(struct messaging_context *ctx, uint32_t msg_type,
327                           void *private_data)
328 {
329         struct messaging_callback *cb, *next;
330
331         for (cb = ctx->callbacks; cb; cb = next) {
332                 next = cb->next;
333                 if ((cb->msg_type == msg_type)
334                     && (cb->private_data == private_data)) {
335                         DEBUG(5,("Deregistering messaging pointer for type %u - private_data=%p\n",
336                                   (unsigned)msg_type, private_data));
337                         DLIST_REMOVE(ctx->callbacks, cb);
338                         TALLOC_FREE(cb);
339                 }
340         }
341 }
342
343 /*
344   Send a message to a particular server
345 */
346 NTSTATUS messaging_send(struct messaging_context *msg_ctx,
347                         struct server_id server, uint32_t msg_type,
348                         const DATA_BLOB *data)
349 {
350 #ifdef CLUSTER_SUPPORT
351         if (!procid_is_local(&server)) {
352                 return msg_ctx->remote->send_fn(msg_ctx, server,
353                                                 msg_type, data,
354                                                 msg_ctx->remote);
355         }
356 #endif
357         return msg_ctx->local->send_fn(msg_ctx, server, msg_type, data,
358                                        msg_ctx->local);
359 }
360
361 NTSTATUS messaging_send_buf(struct messaging_context *msg_ctx,
362                             struct server_id server, uint32_t msg_type,
363                             const uint8 *buf, size_t len)
364 {
365         DATA_BLOB blob = data_blob_const(buf, len);
366         return messaging_send(msg_ctx, server, msg_type, &blob);
367 }
368
369 /*
370   Dispatch one messaging_rec
371 */
372 void messaging_dispatch_rec(struct messaging_context *msg_ctx,
373                             struct messaging_rec *rec)
374 {
375         struct messaging_callback *cb, *next;
376
377         for (cb = msg_ctx->callbacks; cb != NULL; cb = next) {
378                 next = cb->next;
379                 if (cb->msg_type == rec->msg_type) {
380                         cb->fn(msg_ctx, cb->private_data, rec->msg_type,
381                                rec->src, &rec->buf);
382                         /* we continue looking for matching messages
383                            after finding one. This matters for
384                            subsystems like the internal notify code
385                            which register more than one handler for
386                            the same message type */
387                 }
388         }
389         return;
390 }
391
392 /** @} **/