s3:messaging: fix conversion specifier
[obnox/samba/samba-obnox.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 #include "messages.h"
22 #include "util_tdb.h"
23
24 /*
25  * It is not possible to include ctdb.h and tdb_compat.h (included via
26  * some other include above) without warnings. This fixes those
27  * warnings.
28  */
29
30 #ifdef typesafe_cb
31 #undef typesafe_cb
32 #endif
33
34 #ifdef typesafe_cb_preargs
35 #undef typesafe_cb_preargs
36 #endif
37
38 #ifdef typesafe_cb_postargs
39 #undef typesafe_cb_postargs
40 #endif
41
42 #include "ctdb.h"
43 #include "ctdb_private.h"
44 #include "ctdbd_conn.h"
45
46
47 struct messaging_ctdbd_context {
48         struct ctdbd_connection *conn;
49 };
50
51 /*
52  * This is a Samba3 hack/optimization. Routines like process_exists need to
53  * talk to ctdbd, and they don't get handed a messaging context.
54  */
55 static struct ctdbd_connection *global_ctdbd_connection;
56 static int global_ctdb_connection_pid;
57
58 struct ctdbd_connection *messaging_ctdbd_connection(void)
59 {
60         if (!lp_clustering()) {
61                 return NULL;
62         }
63
64         if (global_ctdb_connection_pid == 0 &&
65             global_ctdbd_connection == NULL) {
66                 struct tevent_context *ev;
67                 struct messaging_context *msg;
68
69                 ev = samba_tevent_context_init(NULL);
70                 if (!ev) {
71                         DEBUG(0,("samba_tevent_context_init failed\n"));
72                 }
73
74                 msg = messaging_init(NULL, ev);
75                 if (!msg) {
76                         DEBUG(0,("messaging_init failed\n"));
77                         return NULL;
78                 }
79         }
80
81         if (global_ctdb_connection_pid != getpid()) {
82                 DEBUG(0,("messaging_ctdbd_connection():"
83                          "valid for pid[%jd] but it's [%jd]\n",
84                          (intmax_t)global_ctdb_connection_pid,
85                          (intmax_t)getpid()));
86                 smb_panic("messaging_ctdbd_connection() invalid process\n");
87         }
88
89         return global_ctdbd_connection;
90 }
91
92 static int messaging_ctdb_send(struct server_id src,
93                                struct server_id pid, int msg_type,
94                                const struct iovec *iov, int iovlen,
95                                const int *fds, size_t num_fds,
96                                struct messaging_backend *backend)
97 {
98         struct messaging_ctdbd_context *ctx = talloc_get_type_abort(
99                 backend->private_data, struct messaging_ctdbd_context);
100         struct messaging_rec msg;
101         uint8_t *buf;
102         NTSTATUS status;
103
104         if (num_fds > 0) {
105                 return ENOSYS;
106         }
107
108         buf = iov_buf(talloc_tos(), iov, iovlen);
109         if (buf == NULL) {
110                 return ENOMEM;
111         }
112
113         msg = (struct messaging_rec) {
114                 .msg_version    = MESSAGE_VERSION,
115                 .msg_type       = msg_type,
116                 .dest           = pid,
117                 .src            = src,
118                 .buf            = data_blob_const(buf, talloc_get_size(buf)),
119         };
120
121         status = ctdbd_messaging_send(ctx->conn, pid.vnn, pid.pid, &msg);
122
123         TALLOC_FREE(buf);
124
125         if (NT_STATUS_IS_OK(status)) {
126                 return 0;
127         }
128         return map_errno_from_nt_status(status);
129 }
130
131 static int messaging_ctdbd_destructor(struct messaging_ctdbd_context *ctx)
132 {
133         /*
134          * The global connection just went away
135          */
136         global_ctdb_connection_pid = 0;
137         global_ctdbd_connection = NULL;
138         return 0;
139 }
140
141 NTSTATUS messaging_ctdbd_init(struct messaging_context *msg_ctx,
142                               TALLOC_CTX *mem_ctx,
143                               struct messaging_backend **presult)
144 {
145         struct messaging_backend *result;
146         struct messaging_ctdbd_context *ctx;
147         NTSTATUS status;
148
149         if (!(result = talloc(mem_ctx, struct messaging_backend))) {
150                 DEBUG(0, ("talloc failed\n"));
151                 return NT_STATUS_NO_MEMORY;
152         }
153
154         if (!(ctx = talloc(result, struct messaging_ctdbd_context))) {
155                 DEBUG(0, ("talloc failed\n"));
156                 TALLOC_FREE(result);
157                 return NT_STATUS_NO_MEMORY;
158         }
159
160         status = ctdbd_messaging_connection(ctx, &ctx->conn);
161
162         if (!NT_STATUS_IS_OK(status)) {
163                 DEBUG(10, ("ctdbd_messaging_connection failed: %s\n",
164                            nt_errstr(status)));
165                 TALLOC_FREE(result);
166                 return status;
167         }
168
169         status = ctdbd_register_msg_ctx(ctx->conn, msg_ctx);
170
171         if (!NT_STATUS_IS_OK(status)) {
172                 DEBUG(10, ("ctdbd_register_msg_ctx failed: %s\n",
173                            nt_errstr(status)));
174                 TALLOC_FREE(result);
175                 return status;
176         }
177
178         global_ctdb_connection_pid = getpid();
179         global_ctdbd_connection = ctx->conn;
180         talloc_set_destructor(ctx, messaging_ctdbd_destructor);
181
182         set_my_vnn(ctdbd_vnn(ctx->conn));
183
184         result->send_fn = messaging_ctdb_send;
185         result->private_data = (void *)ctx;
186
187         *presult = result;
188         return NT_STATUS_OK;
189 }