s3:messaging: fix conversion specifier
[Samba.git] / source3 / lib / messages_ctdbd.c
blobeb7e929fc38d6921531ba6ba77e92a672beddaa7
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"
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.
30 #ifdef typesafe_cb
31 #undef typesafe_cb
32 #endif
34 #ifdef typesafe_cb_preargs
35 #undef typesafe_cb_preargs
36 #endif
38 #ifdef typesafe_cb_postargs
39 #undef typesafe_cb_postargs
40 #endif
42 #include "ctdb.h"
43 #include "ctdb_private.h"
44 #include "ctdbd_conn.h"
47 struct messaging_ctdbd_context {
48 struct ctdbd_connection *conn;
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.
55 static struct ctdbd_connection *global_ctdbd_connection;
56 static int global_ctdb_connection_pid;
58 struct ctdbd_connection *messaging_ctdbd_connection(void)
60 if (!lp_clustering()) {
61 return NULL;
64 if (global_ctdb_connection_pid == 0 &&
65 global_ctdbd_connection == NULL) {
66 struct tevent_context *ev;
67 struct messaging_context *msg;
69 ev = samba_tevent_context_init(NULL);
70 if (!ev) {
71 DEBUG(0,("samba_tevent_context_init failed\n"));
74 msg = messaging_init(NULL, ev);
75 if (!msg) {
76 DEBUG(0,("messaging_init failed\n"));
77 return NULL;
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");
89 return global_ctdbd_connection;
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)
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;
104 if (num_fds > 0) {
105 return ENOSYS;
108 buf = iov_buf(talloc_tos(), iov, iovlen);
109 if (buf == NULL) {
110 return ENOMEM;
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)),
121 status = ctdbd_messaging_send(ctx->conn, pid.vnn, pid.pid, &msg);
123 TALLOC_FREE(buf);
125 if (NT_STATUS_IS_OK(status)) {
126 return 0;
128 return map_errno_from_nt_status(status);
131 static int messaging_ctdbd_destructor(struct messaging_ctdbd_context *ctx)
134 * The global connection just went away
136 global_ctdb_connection_pid = 0;
137 global_ctdbd_connection = NULL;
138 return 0;
141 NTSTATUS messaging_ctdbd_init(struct messaging_context *msg_ctx,
142 TALLOC_CTX *mem_ctx,
143 struct messaging_backend **presult)
145 struct messaging_backend *result;
146 struct messaging_ctdbd_context *ctx;
147 NTSTATUS status;
149 if (!(result = talloc(mem_ctx, struct messaging_backend))) {
150 DEBUG(0, ("talloc failed\n"));
151 return NT_STATUS_NO_MEMORY;
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;
160 status = ctdbd_messaging_connection(ctx, &ctx->conn);
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;
169 status = ctdbd_register_msg_ctx(ctx->conn, msg_ctx);
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;
178 global_ctdb_connection_pid = getpid();
179 global_ctdbd_connection = ctx->conn;
180 talloc_set_destructor(ctx, messaging_ctdbd_destructor);
182 set_my_vnn(ctdbd_vnn(ctx->conn));
184 result->send_fn = messaging_ctdb_send;
185 result->private_data = (void *)ctx;
187 *presult = result;
188 return NT_STATUS_OK;