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