s3:param: move lp_ctdbd_socket() to ctdbd_conn.c
[Samba.git] / source3 / torture / test_ctdbconn.c
blobd8f744c24b8a71eb3dc67ebde6d09821d94b4834
1 /*
2 Unix SMB/CIFS implementation.
3 Test new ctdb API
4 Copyright (C) Volker Lendecke 2012
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 "torture/proto.h"
23 #ifdef CLUSTER_SUPPORT
25 #include "ctdb_conn.h"
26 #include "ctdbd_conn.h"
27 #include "lib/util/tevent_unix.h"
28 #include "tdb.h"
30 #ifdef HAVE_CTDB_PROTOCOL_H
31 #include "ctdb_protocol.h"
32 #else
33 #include "ctdb_private.h"
34 #endif
36 #include "messages.h"
38 struct ctdb_conn_test_state {
39 struct tevent_context *ev;
40 struct ctdb_conn *conn;
41 struct ctdb_msg_channel *channel;
42 int msgno;
45 static void ctdb_conn_test_got_conn(struct tevent_req *subreq);
46 static void ctdb_conn_test_got_pnn(struct tevent_req *subreq);
47 static void ctdb_conn_test_got_channel(struct tevent_req *subreq);
48 static void ctdb_conn_test_got_msg(struct tevent_req *subreq);
49 static void ctdb_conn_test_msg_sent(struct tevent_req *subreq);
51 static struct tevent_req *ctdb_conn_test_send(TALLOC_CTX *mem_ctx,
52 struct tevent_context *ev)
54 struct tevent_req *req, *subreq;
55 struct ctdb_conn_test_state *state;
57 req = tevent_req_create(mem_ctx, &state, struct ctdb_conn_test_state);
58 if (req == NULL) {
59 return NULL;
61 state->ev = ev;
63 subreq = ctdb_conn_init_send(mem_ctx, ev, lp_ctdbd_socket());
64 if (tevent_req_nomem(subreq, req)) {
65 return tevent_req_post(req, ev);
67 tevent_req_set_callback(subreq, ctdb_conn_test_got_conn, req);
68 return req;
71 static void ctdb_conn_test_got_conn(struct tevent_req *subreq)
73 struct tevent_req *req = tevent_req_callback_data(
74 subreq, struct tevent_req);
75 struct ctdb_conn_test_state *state = tevent_req_data(
76 req, struct ctdb_conn_test_state);
77 uint64_t ret;
79 ret = ctdb_conn_init_recv(subreq, state, &state->conn);
80 TALLOC_FREE(subreq);
81 if (tevent_req_error(req, ret)) {
82 return;
84 subreq = ctdb_conn_control_send(state, state->ev, state->conn,
85 CTDB_CURRENT_NODE,
86 CTDB_CONTROL_GET_PNN, 0, 0, NULL, 0);
87 if (tevent_req_nomem(subreq, req)) {
88 return;
90 tevent_req_set_callback(subreq, ctdb_conn_test_got_pnn, req);
93 static void ctdb_conn_test_got_pnn(struct tevent_req *subreq)
95 struct tevent_req *req = tevent_req_callback_data(
96 subreq, struct tevent_req);
97 struct ctdb_conn_test_state *state = tevent_req_data(
98 req, struct ctdb_conn_test_state);
99 int ret;
100 struct ctdb_reply_control *reply;
102 ret = ctdb_conn_control_recv(subreq, talloc_tos(), &reply);
103 TALLOC_FREE(subreq);
104 if (tevent_req_error(req, ret)) {
105 return;
107 printf("vnn=%d\n", (int)reply->status);
109 subreq = ctdb_msg_channel_init_send(
110 state, state->ev, lp_ctdbd_socket(), 999999);
111 if (tevent_req_nomem(subreq, req)) {
112 return;
114 tevent_req_set_callback(subreq, ctdb_conn_test_got_channel, req);
117 static void ctdb_conn_test_got_channel(struct tevent_req *subreq)
119 struct tevent_req *req = tevent_req_callback_data(
120 subreq, struct tevent_req);
121 struct ctdb_conn_test_state *state = tevent_req_data(
122 req, struct ctdb_conn_test_state);
123 int ret;
125 ret = ctdb_msg_channel_init_recv(subreq, state, &state->channel);
126 TALLOC_FREE(subreq);
127 if (tevent_req_error(req, ret)) {
128 return;
131 subreq = ctdb_msg_read_send(state, state->ev, state->channel);
132 if (tevent_req_nomem(subreq, req)) {
133 return;
135 tevent_req_set_callback(subreq, ctdb_conn_test_got_msg, req);
137 state->msgno += 1;
139 subreq = ctdb_conn_msg_write_send(
140 state, state->ev, state->conn, CTDB_CURRENT_NODE, 999999,
141 (uint8_t *)&state->msgno, sizeof(state->msgno));
142 if (tevent_req_nomem(subreq, req)) {
143 return;
145 tevent_req_set_callback(subreq, ctdb_conn_test_msg_sent, req);
148 static void ctdb_conn_test_got_msg(struct tevent_req *subreq)
150 struct tevent_req *req = tevent_req_callback_data(
151 subreq, struct tevent_req);
152 struct ctdb_conn_test_state *state = tevent_req_data(
153 req, struct ctdb_conn_test_state);
154 uint8_t *buf;
155 size_t buf_len;
156 int ret;
158 ret = ctdb_msg_read_recv(subreq, talloc_tos(), &buf, &buf_len);
159 TALLOC_FREE(subreq);
160 if (tevent_req_error(req, ret)) {
161 return;
163 if (buf_len != sizeof(int)) {
164 printf("got invalid msg\n");
165 tevent_req_error(req, EINVAL);
166 return;
168 memcpy(&ret, buf, buf_len);
169 printf("got msg %d\n", ret);
170 if (ret == 5) {
171 tevent_req_done(req);
172 return;
175 subreq = ctdb_msg_read_send(state, state->ev, state->channel);
176 if (tevent_req_nomem(subreq, req)) {
177 return;
179 tevent_req_set_callback(subreq, ctdb_conn_test_got_msg, req);
182 static void ctdb_conn_test_msg_sent(struct tevent_req *subreq)
184 struct tevent_req *req = tevent_req_callback_data(
185 subreq, struct tevent_req);
186 struct ctdb_conn_test_state *state = tevent_req_data(
187 req, struct ctdb_conn_test_state);
188 int ret;
190 ret = ctdb_conn_msg_write_recv(subreq);
191 TALLOC_FREE(subreq);
192 if (tevent_req_error(req, ret)) {
193 return;
195 state->msgno += 1;
197 if (state->msgno >= 10) {
198 return;
201 subreq = ctdb_conn_msg_write_send(
202 state, state->ev, state->conn, CTDB_CURRENT_NODE, 999999,
203 (uint8_t *)&state->msgno, sizeof(state->msgno));
204 if (tevent_req_nomem(subreq, req)) {
205 return;
207 tevent_req_set_callback(subreq, ctdb_conn_test_msg_sent, req);
210 static int ctdb_conn_test_recv(struct tevent_req *req)
212 int err;
213 if (tevent_req_is_unix_error(req, &err)) {
214 return err;
216 return 0;
219 bool run_ctdb_conn(int dummy)
221 struct tevent_context *ev;
222 struct tevent_req *req;
223 int ret;
225 ev = samba_tevent_context_init(talloc_tos());
226 if (ev == NULL) {
227 fprintf(stderr, "tevent_context_init failed\n");
228 return false;
230 req = ctdb_conn_test_send(ev, ev);
231 if (req == NULL) {
232 fprintf(stderr, "ctdb_conn_test_send failed\n");
233 return false;
235 if (!tevent_req_poll(req, ev)) {
236 fprintf(stderr, "tevent_req_poll failed\n");
237 return false;
239 ret = ctdb_conn_test_recv(req);
240 TALLOC_FREE(req);
241 printf("ctdb_conn_test returned %s\n",
242 ret ? strerror(ret) : "success");
243 TALLOC_FREE(ev);
244 return (ret == 0);
247 #endif