Merge branch 'v3-2-test' of ssh://git.samba.org/data/git/samba into v3-2-simo
[ira/wip.git] / source3 / 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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21
22 #ifdef CLUSTER_SUPPORT
23
24 #include "librpc/gen_ndr/messaging.h"
25 #include "ctdb.h"
26 #include "ctdb_private.h"
27 #include "ctdbd_conn.h"
28
29
30 struct messaging_ctdbd_context {
31         struct ctdbd_connection *conn;
32 };
33
34 /*
35  * This is a Samba3 hack/optimization. Routines like process_exists need to
36  * talk to ctdbd, and they don't get handed a messaging context.
37  */
38 struct ctdbd_connection *global_ctdbd_connection;
39
40 struct ctdbd_connection *messaging_ctdbd_connection(void)
41 {
42         return global_ctdbd_connection;
43 }
44
45 static NTSTATUS messaging_ctdb_send(struct messaging_context *msg_ctx,
46                                     struct server_id pid, int msg_type,
47                                     const DATA_BLOB *data,
48                                     struct messaging_backend *backend)
49 {
50         struct messaging_ctdbd_context *ctx = talloc_get_type_abort(
51                 backend->private_data, struct messaging_ctdbd_context);
52
53         struct messaging_rec msg;
54
55         msg.msg_version = MESSAGE_VERSION;
56         msg.msg_type    = msg_type;
57         msg.dest        = pid;
58         msg.src         = procid_self();
59         msg.buf         = *data;
60
61         return ctdbd_messaging_send(ctx->conn, pid.vnn, pid.pid, &msg);
62 }
63
64 static int messaging_ctdbd_destructor(struct messaging_ctdbd_context *ctx)
65 {
66         /*
67          * The global connection just went away
68          */
69         global_ctdbd_connection = NULL;
70         return 0;
71 }
72
73 NTSTATUS messaging_ctdbd_init(struct messaging_context *msg_ctx,
74                               TALLOC_CTX *mem_ctx,
75                               struct messaging_backend **presult)
76 {
77         struct messaging_backend *result;
78         struct messaging_ctdbd_context *ctx;
79         NTSTATUS status;
80
81         if (!(result = TALLOC_P(mem_ctx, struct messaging_backend))) {
82                 DEBUG(0, ("talloc failed\n"));
83                 return NT_STATUS_NO_MEMORY;
84         }
85
86         if (!(ctx = TALLOC_P(result, struct messaging_ctdbd_context))) {
87                 DEBUG(0, ("talloc failed\n"));
88                 TALLOC_FREE(result);
89                 return NT_STATUS_NO_MEMORY;
90         }
91
92         status = ctdbd_messaging_connection(ctx, &ctx->conn);
93
94         if (!NT_STATUS_IS_OK(status)) {
95                 DEBUG(10, ("ctdbd_messaging_connection failed: %s\n",
96                            nt_errstr(status)));
97                 TALLOC_FREE(result);
98                 return status;
99         }
100
101         status = ctdbd_register_msg_ctx(ctx->conn, msg_ctx);
102
103         if (!NT_STATUS_IS_OK(status)) {
104                 DEBUG(10, ("ctdbd_register_msg_ctx failed: %s\n",
105                            nt_errstr(status)));
106                 TALLOC_FREE(result);
107                 return status;
108         }
109
110         global_ctdbd_connection = ctx->conn;
111         talloc_set_destructor(ctx, messaging_ctdbd_destructor);
112
113         set_my_vnn(ctdbd_vnn(ctx->conn));
114
115         result->send_fn = messaging_ctdb_send;
116         result->private_data = (void *)ctx;
117
118         *presult = result;
119         return NT_STATUS_OK;
120 }
121
122 #else
123
124 NTSTATUS messaging_ctdbd_init(struct messaging_context *msg_ctx,
125                               TALLOC_CTX *mem_ctx,
126                               struct messaging_backend **presult)
127 {
128         return NT_STATUS_NOT_IMPLEMENTED;
129 }
130
131 #endif