s3: Avoid some transaction_commit on gencache.tdb
[kai/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 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 = "none";
73         char *free_me = NULL;
74
75         if (data->data != NULL) {
76                 free_me = talloc_strndup(talloc_tos(), (char *)data->data,
77                                          data->length);
78                 msg = free_me;
79         }
80         DEBUG(1,("INFO: Received PING message from PID %s [%s]\n",
81                  procid_str_static(&src), msg));
82         TALLOC_FREE(free_me);
83         messaging_send(msg_ctx, src, MSG_PONG, data);
84 }
85
86 /****************************************************************************
87  Register/replace a dispatch function for a particular message type.
88  JRA changed Dec 13 2006. Only one message handler now permitted per type.
89  *NOTE*: Dispatch functions must be able to cope with incoming
90  messages on an *odd* byte boundary.
91 ****************************************************************************/
92
93 struct msg_all {
94         struct messaging_context *msg_ctx;
95         int msg_type;
96         uint32 msg_flag;
97         const void *buf;
98         size_t len;
99         int n_sent;
100 };
101
102 /****************************************************************************
103  Send one of the messages for the broadcast.
104 ****************************************************************************/
105
106 static int traverse_fn(struct db_record *rec, const struct server_id *id,
107                        uint32_t msg_flags, void *state)
108 {
109         struct msg_all *msg_all = (struct msg_all *)state;
110         NTSTATUS status;
111
112         /* Don't send if the receiver hasn't registered an interest. */
113
114         if((msg_flags & msg_all->msg_flag) == 0) {
115                 return 0;
116         }
117
118         /* If the msg send fails because the pid was not found (i.e. smbd died), 
119          * the msg has already been deleted from the messages.tdb.*/
120
121         status = messaging_send_buf(msg_all->msg_ctx, *id, msg_all->msg_type,
122                                     (const uint8 *)msg_all->buf, msg_all->len);
123
124         if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
125
126                 /*
127                  * If the pid was not found delete the entry from
128                  * serverid.tdb
129                  */
130
131                 DEBUG(2, ("pid %s doesn't exist\n", procid_str_static(id)));
132
133                 dbwrap_record_delete(rec);
134         }
135         msg_all->n_sent++;
136         return 0;
137 }
138
139 /**
140  * Send a message to all smbd processes.
141  *
142  * It isn't very efficient, but should be OK for the sorts of
143  * applications that use it. When we need efficient broadcast we can add
144  * it.
145  *
146  * @param n_sent Set to the number of messages sent.  This should be
147  * equal to the number of processes, but be careful for races.
148  *
149  * @retval True for success.
150  **/
151 bool message_send_all(struct messaging_context *msg_ctx,
152                       int msg_type,
153                       const void *buf, size_t len,
154                       int *n_sent)
155 {
156         struct msg_all msg_all;
157
158         msg_all.msg_type = msg_type;
159         if (msg_type < 0x100) {
160                 msg_all.msg_flag = FLAG_MSG_GENERAL;
161         } else if (msg_type > 0x100 && msg_type < 0x200) {
162                 msg_all.msg_flag = FLAG_MSG_NMBD;
163         } else if (msg_type > 0x200 && msg_type < 0x300) {
164                 msg_all.msg_flag = FLAG_MSG_PRINT_GENERAL;
165         } else if (msg_type > 0x300 && msg_type < 0x400) {
166                 msg_all.msg_flag = FLAG_MSG_SMBD;
167         } else if (msg_type > 0x400 && msg_type < 0x600) {
168                 msg_all.msg_flag = FLAG_MSG_WINBIND;
169         } else if (msg_type > 4000 && msg_type < 5000) {
170                 msg_all.msg_flag = FLAG_MSG_DBWRAP;
171         } else {
172                 return false;
173         }
174
175         msg_all.buf = buf;
176         msg_all.len = len;
177         msg_all.n_sent = 0;
178         msg_all.msg_ctx = msg_ctx;
179
180         serverid_traverse(traverse_fn, &msg_all);
181         if (n_sent)
182                 *n_sent = msg_all.n_sent;
183         return true;
184 }
185
186 struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx, 
187                                          struct event_context *ev)
188 {
189         struct messaging_context *ctx;
190         NTSTATUS status;
191
192         if (!(ctx = talloc_zero(mem_ctx, struct messaging_context))) {
193                 return NULL;
194         }
195
196         ctx->id = procid_self();
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(2, ("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(2, ("messaging_ctdbd_init failed: %s\n",
214                                   nt_errstr(status)));
215                         TALLOC_FREE(ctx);
216                         return NULL;
217                 }
218         }
219         ctx->id.vnn = get_my_vnn();
220 #endif
221
222         messaging_register(ctx, NULL, MSG_PING, ping_message);
223
224         /* Register some debugging related messages */
225
226         register_msg_pool_usage(ctx);
227         register_dmalloc_msgs(ctx);
228         debug_register_msgs(ctx);
229
230         return ctx;
231 }
232
233 struct server_id messaging_server_id(const struct messaging_context *msg_ctx)
234 {
235         return msg_ctx->id;
236 }
237
238 /*
239  * re-init after a fork
240  */
241 NTSTATUS messaging_reinit(struct messaging_context *msg_ctx)
242 {
243         NTSTATUS status;
244
245         TALLOC_FREE(msg_ctx->local);
246
247         msg_ctx->id = procid_self();
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         DEBUG(5, ("Registering messaging pointer for type %u - "
292                   "private_data=%p\n",
293                   (unsigned)msg_type, private_data));
294
295         /*
296          * Only one callback per type
297          */
298
299         for (cb = msg_ctx->callbacks; cb != NULL; cb = cb->next) {
300                 /* we allow a second registration of the same message
301                    type if it has a different private pointer. This is
302                    needed in, for example, the internal notify code,
303                    which creates a new notify context for each tree
304                    connect, and expects to receive messages to each of
305                    them. */
306                 if (cb->msg_type == msg_type && private_data == cb->private_data) {
307                         DEBUG(5,("Overriding messaging pointer for type %u - private_data=%p\n",
308                                   (unsigned)msg_type, private_data));
309                         cb->fn = fn;
310                         cb->private_data = private_data;
311                         return NT_STATUS_OK;
312                 }
313         }
314
315         if (!(cb = talloc(msg_ctx, struct messaging_callback))) {
316                 return NT_STATUS_NO_MEMORY;
317         }
318
319         cb->msg_type = msg_type;
320         cb->fn = fn;
321         cb->private_data = private_data;
322
323         DLIST_ADD(msg_ctx->callbacks, cb);
324         return NT_STATUS_OK;
325 }
326
327 /*
328   De-register the function for a particular message type.
329 */
330 void messaging_deregister(struct messaging_context *ctx, uint32_t msg_type,
331                           void *private_data)
332 {
333         struct messaging_callback *cb, *next;
334
335         for (cb = ctx->callbacks; cb; cb = next) {
336                 next = cb->next;
337                 if ((cb->msg_type == msg_type)
338                     && (cb->private_data == private_data)) {
339                         DEBUG(5,("Deregistering messaging pointer for type %u - private_data=%p\n",
340                                   (unsigned)msg_type, private_data));
341                         DLIST_REMOVE(ctx->callbacks, cb);
342                         TALLOC_FREE(cb);
343                 }
344         }
345 }
346
347 /*
348   Send a message to a particular server
349 */
350 NTSTATUS messaging_send(struct messaging_context *msg_ctx,
351                         struct server_id server, uint32_t msg_type,
352                         const DATA_BLOB *data)
353 {
354         if (server_id_is_disconnected(&server)) {
355                 return NT_STATUS_INVALID_PARAMETER_MIX;
356         }
357
358 #ifdef CLUSTER_SUPPORT
359         if (!procid_is_local(&server)) {
360                 return msg_ctx->remote->send_fn(msg_ctx, server,
361                                                 msg_type, data,
362                                                 msg_ctx->remote);
363         }
364 #endif
365         return msg_ctx->local->send_fn(msg_ctx, server, msg_type, data,
366                                        msg_ctx->local);
367 }
368
369 NTSTATUS messaging_send_buf(struct messaging_context *msg_ctx,
370                             struct server_id server, uint32_t msg_type,
371                             const uint8 *buf, size_t len)
372 {
373         DATA_BLOB blob = data_blob_const(buf, len);
374         return messaging_send(msg_ctx, server, msg_type, &blob);
375 }
376
377 /*
378   Dispatch one messaging_rec
379 */
380 void messaging_dispatch_rec(struct messaging_context *msg_ctx,
381                             struct messaging_rec *rec)
382 {
383         struct messaging_callback *cb, *next;
384
385         for (cb = msg_ctx->callbacks; cb != NULL; cb = next) {
386                 next = cb->next;
387                 if (cb->msg_type == rec->msg_type) {
388                         cb->fn(msg_ctx, cb->private_data, rec->msg_type,
389                                rec->src, &rec->buf);
390                         /* we continue looking for matching messages
391                            after finding one. This matters for
392                            subsystems like the internal notify code
393                            which register more than one handler for
394                            the same message type */
395                 }
396         }
397         return;
398 }
399
400 /** @} **/