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 #include "ctdb_conn.h"
24 #include "ctdbd_conn.h"
25 #include "lib/util/tevent_unix.h"
28 #ifdef HAVE_CTDB_PROTOCOL_H
29 #include "ctdb_protocol.h"
31 #include "ctdb_private.h"
36 struct ctdb_conn_test_state
{
37 struct tevent_context
*ev
;
38 struct ctdb_conn
*conn
;
39 struct ctdb_msg_channel
*channel
;
43 static void ctdb_conn_test_got_conn(struct tevent_req
*subreq
);
44 static void ctdb_conn_test_got_pnn(struct tevent_req
*subreq
);
45 static void ctdb_conn_test_got_channel(struct tevent_req
*subreq
);
46 static void ctdb_conn_test_got_msg(struct tevent_req
*subreq
);
47 static void ctdb_conn_test_msg_sent(struct tevent_req
*subreq
);
49 static struct tevent_req
*ctdb_conn_test_send(TALLOC_CTX
*mem_ctx
,
50 struct tevent_context
*ev
)
52 struct tevent_req
*req
, *subreq
;
53 struct ctdb_conn_test_state
*state
;
55 req
= tevent_req_create(mem_ctx
, &state
, struct ctdb_conn_test_state
);
61 subreq
= ctdb_conn_init_send(mem_ctx
, ev
, lp_ctdbd_socket());
62 if (tevent_req_nomem(subreq
, req
)) {
63 return tevent_req_post(req
, ev
);
65 tevent_req_set_callback(subreq
, ctdb_conn_test_got_conn
, req
);
69 static void ctdb_conn_test_got_conn(struct tevent_req
*subreq
)
71 struct tevent_req
*req
= tevent_req_callback_data(
72 subreq
, struct tevent_req
);
73 struct ctdb_conn_test_state
*state
= tevent_req_data(
74 req
, struct ctdb_conn_test_state
);
77 ret
= ctdb_conn_init_recv(subreq
, state
, &state
->conn
);
79 if (tevent_req_error(req
, ret
)) {
82 subreq
= ctdb_conn_control_send(state
, state
->ev
, state
->conn
,
84 CTDB_CONTROL_GET_PNN
, 0, 0, NULL
, 0);
85 if (tevent_req_nomem(subreq
, req
)) {
88 tevent_req_set_callback(subreq
, ctdb_conn_test_got_pnn
, req
);
91 static void ctdb_conn_test_got_pnn(struct tevent_req
*subreq
)
93 struct tevent_req
*req
= tevent_req_callback_data(
94 subreq
, struct tevent_req
);
95 struct ctdb_conn_test_state
*state
= tevent_req_data(
96 req
, struct ctdb_conn_test_state
);
98 struct ctdb_reply_control
*reply
;
100 ret
= ctdb_conn_control_recv(subreq
, talloc_tos(), &reply
);
102 if (tevent_req_error(req
, ret
)) {
105 printf("vnn=%d\n", (int)reply
->status
);
107 subreq
= ctdb_msg_channel_init_send(
108 state
, state
->ev
, lp_ctdbd_socket(), 999999);
109 if (tevent_req_nomem(subreq
, req
)) {
112 tevent_req_set_callback(subreq
, ctdb_conn_test_got_channel
, req
);
115 static void ctdb_conn_test_got_channel(struct tevent_req
*subreq
)
117 struct tevent_req
*req
= tevent_req_callback_data(
118 subreq
, struct tevent_req
);
119 struct ctdb_conn_test_state
*state
= tevent_req_data(
120 req
, struct ctdb_conn_test_state
);
123 ret
= ctdb_msg_channel_init_recv(subreq
, state
, &state
->channel
);
125 if (tevent_req_error(req
, ret
)) {
129 subreq
= ctdb_msg_read_send(state
, state
->ev
, state
->channel
);
130 if (tevent_req_nomem(subreq
, req
)) {
133 tevent_req_set_callback(subreq
, ctdb_conn_test_got_msg
, req
);
137 subreq
= ctdb_conn_msg_write_send(
138 state
, state
->ev
, state
->conn
, CTDB_CURRENT_NODE
, 999999,
139 (uint8_t *)&state
->msgno
, sizeof(state
->msgno
));
140 if (tevent_req_nomem(subreq
, req
)) {
143 tevent_req_set_callback(subreq
, ctdb_conn_test_msg_sent
, req
);
146 static void ctdb_conn_test_got_msg(struct tevent_req
*subreq
)
148 struct tevent_req
*req
= tevent_req_callback_data(
149 subreq
, struct tevent_req
);
150 struct ctdb_conn_test_state
*state
= tevent_req_data(
151 req
, struct ctdb_conn_test_state
);
156 ret
= ctdb_msg_read_recv(subreq
, talloc_tos(), &buf
, &buf_len
);
158 if (tevent_req_error(req
, ret
)) {
161 if (buf_len
!= sizeof(int)) {
162 printf("got invalid msg\n");
163 tevent_req_error(req
, EINVAL
);
166 memcpy(&ret
, buf
, buf_len
);
167 printf("got msg %d\n", ret
);
169 tevent_req_done(req
);
173 subreq
= ctdb_msg_read_send(state
, state
->ev
, state
->channel
);
174 if (tevent_req_nomem(subreq
, req
)) {
177 tevent_req_set_callback(subreq
, ctdb_conn_test_got_msg
, req
);
180 static void ctdb_conn_test_msg_sent(struct tevent_req
*subreq
)
182 struct tevent_req
*req
= tevent_req_callback_data(
183 subreq
, struct tevent_req
);
184 struct ctdb_conn_test_state
*state
= tevent_req_data(
185 req
, struct ctdb_conn_test_state
);
188 ret
= ctdb_conn_msg_write_recv(subreq
);
190 if (tevent_req_error(req
, ret
)) {
195 if (state
->msgno
>= 10) {
199 subreq
= ctdb_conn_msg_write_send(
200 state
, state
->ev
, state
->conn
, CTDB_CURRENT_NODE
, 999999,
201 (uint8_t *)&state
->msgno
, sizeof(state
->msgno
));
202 if (tevent_req_nomem(subreq
, req
)) {
205 tevent_req_set_callback(subreq
, ctdb_conn_test_msg_sent
, req
);
208 static int ctdb_conn_test_recv(struct tevent_req
*req
)
211 if (tevent_req_is_unix_error(req
, &err
)) {
217 bool run_ctdb_conn(int dummy
)
219 struct tevent_context
*ev
;
220 struct tevent_req
*req
;
223 ev
= samba_tevent_context_init(talloc_tos());
225 fprintf(stderr
, "tevent_context_init failed\n");
228 req
= ctdb_conn_test_send(ev
, ev
);
230 fprintf(stderr
, "ctdb_conn_test_send failed\n");
233 if (!tevent_req_poll(req
, ev
)) {
234 fprintf(stderr
, "tevent_req_poll failed\n");
237 ret
= ctdb_conn_test_recv(req
);
239 printf("ctdb_conn_test returned %s\n",
240 ret
? strerror(ret
) : "success");