ctdb-daemon: Fix some strict-aliasing warnings
[Samba.git] / source3 / lib / messages_ctdbd.c
blobadd089da38b48306bae1bcb5f8e1522330bc9d27
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[%d] but it's [%d]\n",
84 global_ctdb_connection_pid, getpid()));
85 smb_panic("messaging_ctdbd_connection() invalid process\n");
88 return global_ctdbd_connection;
91 static int messaging_ctdb_send(struct server_id src,
92 struct server_id pid, int msg_type,
93 const struct iovec *iov, int iovlen,
94 struct messaging_backend *backend)
96 struct messaging_ctdbd_context *ctx = talloc_get_type_abort(
97 backend->private_data, struct messaging_ctdbd_context);
99 struct messaging_rec msg;
100 uint8_t *buf;
101 NTSTATUS status;
103 buf = iov_buf(talloc_tos(), iov, iovlen);
104 if (buf == NULL) {
105 return ENOMEM;
109 msg.msg_version = MESSAGE_VERSION;
110 msg.msg_type = msg_type;
111 msg.dest = pid;
112 msg.src = src;
113 msg.buf = data_blob_const(buf, talloc_get_size(buf));
115 status = ctdbd_messaging_send(ctx->conn, pid.vnn, pid.pid, &msg);
117 TALLOC_FREE(buf);
119 if (NT_STATUS_IS_OK(status)) {
120 return 0;
122 return map_errno_from_nt_status(status);
125 static int messaging_ctdbd_destructor(struct messaging_ctdbd_context *ctx)
128 * The global connection just went away
130 global_ctdb_connection_pid = 0;
131 global_ctdbd_connection = NULL;
132 return 0;
135 NTSTATUS messaging_ctdbd_init(struct messaging_context *msg_ctx,
136 TALLOC_CTX *mem_ctx,
137 struct messaging_backend **presult)
139 struct messaging_backend *result;
140 struct messaging_ctdbd_context *ctx;
141 NTSTATUS status;
143 if (!(result = talloc(mem_ctx, struct messaging_backend))) {
144 DEBUG(0, ("talloc failed\n"));
145 return NT_STATUS_NO_MEMORY;
148 if (!(ctx = talloc(result, struct messaging_ctdbd_context))) {
149 DEBUG(0, ("talloc failed\n"));
150 TALLOC_FREE(result);
151 return NT_STATUS_NO_MEMORY;
154 status = ctdbd_messaging_connection(ctx, &ctx->conn);
156 if (!NT_STATUS_IS_OK(status)) {
157 DEBUG(10, ("ctdbd_messaging_connection failed: %s\n",
158 nt_errstr(status)));
159 TALLOC_FREE(result);
160 return status;
163 status = ctdbd_register_msg_ctx(ctx->conn, msg_ctx);
165 if (!NT_STATUS_IS_OK(status)) {
166 DEBUG(10, ("ctdbd_register_msg_ctx failed: %s\n",
167 nt_errstr(status)));
168 TALLOC_FREE(result);
169 return status;
172 global_ctdb_connection_pid = getpid();
173 global_ctdbd_connection = ctx->conn;
174 talloc_set_destructor(ctx, messaging_ctdbd_destructor);
176 set_my_vnn(ctdbd_vnn(ctx->conn));
178 result->send_fn = messaging_ctdb_send;
179 result->private_data = (void *)ctx;
181 *presult = result;
182 return NT_STATUS_OK;