2 Unix SMB/CIFS implementation.
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/>.
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"
30 #ifdef HAVE_CTDB_PROTOCOL_H
31 #include "ctdb_protocol.h"
33 #include "ctdb_private.h"
38 struct ctdb_conn_test_state
{
39 struct tevent_context
*ev
;
40 struct ctdb_conn
*conn
;
41 struct ctdb_msg_channel
*channel
;
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
);
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
);
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
);
79 ret
= ctdb_conn_init_recv(subreq
, state
, &state
->conn
);
81 if (tevent_req_error(req
, ret
)) {
84 subreq
= ctdb_conn_control_send(state
, state
->ev
, state
->conn
,
86 CTDB_CONTROL_GET_PNN
, 0, 0, NULL
, 0);
87 if (tevent_req_nomem(subreq
, req
)) {
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
);
100 struct ctdb_reply_control
*reply
;
102 ret
= ctdb_conn_control_recv(subreq
, talloc_tos(), &reply
);
104 if (tevent_req_error(req
, ret
)) {
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
)) {
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
);
125 ret
= ctdb_msg_channel_init_recv(subreq
, state
, &state
->channel
);
127 if (tevent_req_error(req
, ret
)) {
131 subreq
= ctdb_msg_read_send(state
, state
->ev
, state
->channel
);
132 if (tevent_req_nomem(subreq
, req
)) {
135 tevent_req_set_callback(subreq
, ctdb_conn_test_got_msg
, req
);
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
)) {
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
);
158 ret
= ctdb_msg_read_recv(subreq
, talloc_tos(), &buf
, &buf_len
);
160 if (tevent_req_error(req
, ret
)) {
163 if (buf_len
!= sizeof(int)) {
164 printf("got invalid msg\n");
165 tevent_req_error(req
, EINVAL
);
168 memcpy(&ret
, buf
, buf_len
);
169 printf("got msg %d\n", ret
);
171 tevent_req_done(req
);
175 subreq
= ctdb_msg_read_send(state
, state
->ev
, state
->channel
);
176 if (tevent_req_nomem(subreq
, req
)) {
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
);
190 ret
= ctdb_conn_msg_write_recv(subreq
);
192 if (tevent_req_error(req
, ret
)) {
197 if (state
->msgno
>= 10) {
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
)) {
207 tevent_req_set_callback(subreq
, ctdb_conn_test_msg_sent
, req
);
210 static int ctdb_conn_test_recv(struct tevent_req
*req
)
213 if (tevent_req_is_unix_error(req
, &err
)) {
219 bool run_ctdb_conn(int dummy
)
221 struct tevent_context
*ev
;
222 struct tevent_req
*req
;
225 ev
= samba_tevent_context_init(talloc_tos());
227 fprintf(stderr
, "tevent_context_init failed\n");
230 req
= ctdb_conn_test_send(ev
, ev
);
232 fprintf(stderr
, "ctdb_conn_test_send failed\n");
235 if (!tevent_req_poll(req
, ev
)) {
236 fprintf(stderr
, "tevent_req_poll failed\n");
239 ret
= ctdb_conn_test_recv(req
);
241 printf("ctdb_conn_test returned %s\n",
242 ret
? strerror(ret
) : "success");