backupkey: Handle more clearly the case where we find the secret, but it has no value
[Samba.git] / source3 / lib / messages_ctdbd.c
blob1268bd4a5060b169515185ca181f6c9e9cccc1d3
1 /*
2 Unix SMB/CIFS implementation.
3 Samba internal messaging functions
4 Copyright (C) 2007 by Volker Lendecke
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.
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.
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/>.
20 #include "includes.h"
21 #include "messages.h"
22 #include "util_tdb.h"
23 #include "lib/util/iov_buf.h"
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.
31 #ifdef typesafe_cb
32 #undef typesafe_cb
33 #endif
35 #ifdef typesafe_cb_preargs
36 #undef typesafe_cb_preargs
37 #endif
39 #ifdef typesafe_cb_postargs
40 #undef typesafe_cb_postargs
41 #endif
43 #include "ctdb.h"
44 #include "ctdb_private.h"
45 #include "ctdbd_conn.h"
48 struct messaging_ctdbd_context {
49 struct ctdbd_connection *conn;
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.
56 static struct ctdbd_connection *global_ctdbd_connection;
57 static int global_ctdb_connection_pid;
59 struct ctdbd_connection *messaging_ctdbd_connection(void)
61 if (!lp_clustering()) {
62 return NULL;
65 if (global_ctdb_connection_pid == 0 &&
66 global_ctdbd_connection == NULL) {
67 struct tevent_context *ev;
68 struct messaging_context *msg;
70 ev = samba_tevent_context_init(NULL);
71 if (!ev) {
72 DEBUG(0,("samba_tevent_context_init failed\n"));
75 msg = messaging_init(NULL, ev);
76 if (!msg) {
77 DEBUG(0,("messaging_init failed\n"));
78 return NULL;
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");
90 return global_ctdbd_connection;
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)
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;
106 if (num_fds > 0) {
107 return ENOSYS;
110 buflen = iov_buflen(iov, iovlen);
111 if (buflen == -1) {
112 return EMSGSIZE;
115 buf = talloc_array(talloc_tos(), uint8_t, buflen);
116 if (buf == NULL) {
117 return ENOMEM;
119 iov_buf(iov, iovlen, buf, buflen);
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)),
129 status = ctdbd_messaging_send(ctx->conn, pid.vnn, pid.pid, &msg);
131 TALLOC_FREE(buf);
133 if (NT_STATUS_IS_OK(status)) {
134 return 0;
136 return map_errno_from_nt_status(status);
139 static int messaging_ctdbd_destructor(struct messaging_ctdbd_context *ctx)
142 * The global connection just went away
144 global_ctdb_connection_pid = 0;
145 global_ctdbd_connection = NULL;
146 return 0;
149 NTSTATUS messaging_ctdbd_init(struct messaging_context *msg_ctx,
150 TALLOC_CTX *mem_ctx,
151 struct messaging_backend **presult)
153 struct messaging_backend *result;
154 struct messaging_ctdbd_context *ctx;
155 NTSTATUS status;
157 if (!(result = talloc(mem_ctx, struct messaging_backend))) {
158 DEBUG(0, ("talloc failed\n"));
159 return NT_STATUS_NO_MEMORY;
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;
168 status = ctdbd_messaging_connection(ctx, &ctx->conn);
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;
177 status = ctdbd_register_msg_ctx(ctx->conn, msg_ctx);
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;
186 global_ctdb_connection_pid = getpid();
187 global_ctdbd_connection = ctx->conn;
188 talloc_set_destructor(ctx, messaging_ctdbd_destructor);
190 set_my_vnn(ctdbd_vnn(ctx->conn));
192 result->send_fn = messaging_ctdb_send;
193 result->private_data = (void *)ctx;
195 *presult = result;
196 return NT_STATUS_OK;