buildtools: Expose the Python 3 ABI tag
[Samba.git] / source3 / lib / messages_ctdbd.c
blob799780e6a141096937af3f58adfbe604e95787db
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"
25 #include "ctdb.h"
26 #include "ctdb_private.h"
27 #include "ctdbd_conn.h"
30 struct messaging_ctdbd_context {
31 struct ctdbd_connection *conn;
35 * This is a Samba3 hack/optimization. Routines like process_exists need to
36 * talk to ctdbd, and they don't get handed a messaging context.
38 static struct ctdbd_connection *global_ctdbd_connection;
39 static int global_ctdb_connection_pid;
41 struct ctdbd_connection *messaging_ctdbd_connection(void)
43 if (!lp_clustering()) {
44 return NULL;
47 if (global_ctdb_connection_pid == 0 &&
48 global_ctdbd_connection == NULL) {
49 struct tevent_context *ev;
50 struct messaging_context *msg;
52 ev = samba_tevent_context_init(NULL);
53 if (!ev) {
54 DEBUG(0,("samba_tevent_context_init failed\n"));
57 msg = messaging_init(NULL, ev);
58 if (!msg) {
59 DEBUG(0,("messaging_init failed\n"));
60 return NULL;
64 if (global_ctdb_connection_pid != getpid()) {
65 DEBUG(0,("messaging_ctdbd_connection():"
66 "valid for pid[%jd] but it's [%jd]\n",
67 (intmax_t)global_ctdb_connection_pid,
68 (intmax_t)getpid()));
69 smb_panic("messaging_ctdbd_connection() invalid process\n");
72 return global_ctdbd_connection;
75 static int messaging_ctdb_send(struct server_id src,
76 struct server_id pid, int msg_type,
77 const struct iovec *iov, int iovlen,
78 const int *fds, size_t num_fds,
79 struct messaging_backend *backend)
81 struct messaging_ctdbd_context *ctx = talloc_get_type_abort(
82 backend->private_data, struct messaging_ctdbd_context);
83 struct messaging_rec msg;
84 uint8_t *buf;
85 ssize_t buflen;
86 NTSTATUS status;
88 if (num_fds > 0) {
89 return ENOSYS;
92 buflen = iov_buflen(iov, iovlen);
93 if (buflen == -1) {
94 return EMSGSIZE;
97 buf = talloc_array(talloc_tos(), uint8_t, buflen);
98 if (buf == NULL) {
99 return ENOMEM;
101 iov_buf(iov, iovlen, buf, buflen);
103 msg = (struct messaging_rec) {
104 .msg_version = MESSAGE_VERSION,
105 .msg_type = msg_type,
106 .dest = pid,
107 .src = src,
108 .buf = data_blob_const(buf, talloc_get_size(buf)),
111 status = ctdbd_messaging_send(ctx->conn, pid.vnn, pid.pid, &msg);
113 TALLOC_FREE(buf);
115 if (NT_STATUS_IS_OK(status)) {
116 return 0;
118 return map_errno_from_nt_status(status);
121 static int messaging_ctdbd_destructor(struct messaging_ctdbd_context *ctx)
124 * The global connection just went away
126 global_ctdb_connection_pid = 0;
127 global_ctdbd_connection = NULL;
128 return 0;
131 NTSTATUS messaging_ctdbd_init(struct messaging_context *msg_ctx,
132 TALLOC_CTX *mem_ctx,
133 struct messaging_backend **presult)
135 struct messaging_backend *result;
136 struct messaging_ctdbd_context *ctx;
137 NTSTATUS status;
139 if (!(result = talloc(mem_ctx, struct messaging_backend))) {
140 DEBUG(0, ("talloc failed\n"));
141 return NT_STATUS_NO_MEMORY;
144 if (!(ctx = talloc(result, struct messaging_ctdbd_context))) {
145 DEBUG(0, ("talloc failed\n"));
146 TALLOC_FREE(result);
147 return NT_STATUS_NO_MEMORY;
150 status = ctdbd_messaging_connection(ctx, &ctx->conn);
152 if (!NT_STATUS_IS_OK(status)) {
153 DEBUG(10, ("ctdbd_messaging_connection failed: %s\n",
154 nt_errstr(status)));
155 TALLOC_FREE(result);
156 return status;
159 status = ctdbd_register_msg_ctx(ctx->conn, msg_ctx);
161 if (!NT_STATUS_IS_OK(status)) {
162 DEBUG(10, ("ctdbd_register_msg_ctx failed: %s\n",
163 nt_errstr(status)));
164 TALLOC_FREE(result);
165 return status;
168 global_ctdb_connection_pid = getpid();
169 global_ctdbd_connection = ctx->conn;
170 talloc_set_destructor(ctx, messaging_ctdbd_destructor);
172 set_my_vnn(ctdbd_vnn(ctx->conn));
174 result->send_fn = messaging_ctdb_send;
175 result->private_data = (void *)ctx;
177 *presult = result;
178 return NT_STATUS_OK;