4ac9ea9904364ce5b6ba3f0192de73774a184ebd
[kai/samba-autobuild/.git] / source / lib / messages_ctdbd.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba internal messaging functions
4    Copyright (C) 2007 by Volker Lendecke
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 #ifdef CLUSTER_SUPPORT
24
25 #include "librpc/gen_ndr/messaging.h"
26
27 struct messaging_ctdbd_context {
28         struct ctdbd_connection *conn;
29 };
30
31 /*
32  * This is a Samba3 hack/optimization. Routines like process_exists need to
33  * talk to ctdbd, and they don't get handed a messaging context.
34  */
35 struct ctdbd_connection *global_ctdbd_connection;
36
37 struct ctdbd_connection *messaging_ctdbd_connection(void)
38 {
39         return global_ctdbd_connection;
40 }
41
42 static NTSTATUS messaging_ctdb_send(struct messaging_context *msg_ctx,
43                                     struct server_id pid, int msg_type,
44                                     const DATA_BLOB *data,
45                                     struct messaging_backend *backend)
46 {
47         struct messaging_ctdbd_context *ctx = talloc_get_type_abort(
48                 backend->private_data, struct messaging_ctdbd_context);
49
50         struct messaging_rec msg;
51
52         msg.msg_version = MESSAGE_VERSION;
53         msg.msg_type    = msg_type;
54         msg.dest        = pid;
55         msg.src         = procid_self();
56         msg.buf         = *data;
57
58         return ctdbd_messaging_send(ctx->conn, pid.vnn, pid.pid, &msg);
59 }
60
61 static int messaging_ctdbd_destructor(struct messaging_ctdbd_context *ctx)
62 {
63         /*
64          * The global connection just went away
65          */
66         global_ctdbd_connection = NULL;
67         return 0;
68 }
69
70 NTSTATUS messaging_ctdbd_init(struct messaging_context *msg_ctx,
71                               TALLOC_CTX *mem_ctx,
72                               struct messaging_backend **presult)
73 {
74         struct messaging_backend *result;
75         struct messaging_ctdbd_context *ctx;
76         NTSTATUS status;
77
78         if (!(result = TALLOC_P(mem_ctx, struct messaging_backend))) {
79                 DEBUG(0, ("talloc failed\n"));
80                 return NT_STATUS_NO_MEMORY;
81         }
82
83         if (!(ctx = TALLOC_P(result, struct messaging_ctdbd_context))) {
84                 DEBUG(0, ("talloc failed\n"));
85                 TALLOC_FREE(result);
86                 return NT_STATUS_NO_MEMORY;
87         }
88
89         status = ctdbd_messaging_connection(ctx, &ctx->conn);
90
91         if (!NT_STATUS_IS_OK(status)) {
92                 DEBUG(10, ("ctdbd_init_connection failed: %s\n",
93                            nt_errstr(status)));
94                 TALLOC_FREE(result);
95                 return status;
96         }
97
98         global_ctdbd_connection = ctx->conn;
99         talloc_set_destructor(ctx, messaging_ctdbd_destructor);
100
101         set_my_vnn(ctdbd_vnn(ctx->conn));
102
103         result->send_fn = messaging_ctdb_send;
104         result->private_data = (void *)ctx;
105
106         *presult = result;
107         return NT_STATUS_OK;
108 }
109
110 #else
111
112 NTSTATUS messaging_ctdbd_init(struct messaging_context *msg_ctx,
113                               TALLOC_CTX *mem_ctx,
114                               struct messaging_backend **presult)
115 {
116         return NT_STATUS_NOT_IMPLEMENTED;
117 }
118
119 #endif