nsswitch: Fix bogus #include line.
[Samba.git] / source3 / torture / test_ctdbconn.c
blobff45a022d3334b1c0e033b40f79bea064203ca2a
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 #include "ctdb_conn.h"
24 #include "ctdbd_conn.h"
25 #include "lib/util/tevent_unix.h"
26 #include "tdb.h"
28 #ifdef HAVE_CTDB_PROTOCOL_H
29 #include "ctdb_protocol.h"
30 #else
31 #include "ctdb_private.h"
32 #endif
34 #include "messages.h"
36 struct ctdb_conn_test_state {
37 struct tevent_context *ev;
38 struct ctdb_conn *conn;
39 struct ctdb_msg_channel *channel;
40 int msgno;
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);
56 if (req == NULL) {
57 return NULL;
59 state->ev = ev;
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);
66 return 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);
75 uint64_t ret;
77 ret = ctdb_conn_init_recv(subreq, state, &state->conn);
78 TALLOC_FREE(subreq);
79 if (tevent_req_error(req, ret)) {
80 return;
82 subreq = ctdb_conn_control_send(state, state->ev, state->conn,
83 CTDB_CURRENT_NODE,
84 CTDB_CONTROL_GET_PNN, 0, 0, NULL, 0);
85 if (tevent_req_nomem(subreq, req)) {
86 return;
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);
97 int ret;
98 struct ctdb_reply_control *reply;
100 ret = ctdb_conn_control_recv(subreq, talloc_tos(), &reply);
101 TALLOC_FREE(subreq);
102 if (tevent_req_error(req, ret)) {
103 return;
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)) {
110 return;
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);
121 int ret;
123 ret = ctdb_msg_channel_init_recv(subreq, state, &state->channel);
124 TALLOC_FREE(subreq);
125 if (tevent_req_error(req, ret)) {
126 return;
129 subreq = ctdb_msg_read_send(state, state->ev, state->channel);
130 if (tevent_req_nomem(subreq, req)) {
131 return;
133 tevent_req_set_callback(subreq, ctdb_conn_test_got_msg, req);
135 state->msgno += 1;
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)) {
141 return;
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);
152 uint8_t *buf;
153 size_t buf_len;
154 int ret;
156 ret = ctdb_msg_read_recv(subreq, talloc_tos(), &buf, &buf_len);
157 TALLOC_FREE(subreq);
158 if (tevent_req_error(req, ret)) {
159 return;
161 if (buf_len != sizeof(int)) {
162 printf("got invalid msg\n");
163 tevent_req_error(req, EINVAL);
164 return;
166 memcpy(&ret, buf, buf_len);
167 printf("got msg %d\n", ret);
168 if (ret == 5) {
169 tevent_req_done(req);
170 return;
173 subreq = ctdb_msg_read_send(state, state->ev, state->channel);
174 if (tevent_req_nomem(subreq, req)) {
175 return;
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);
186 int ret;
188 ret = ctdb_conn_msg_write_recv(subreq);
189 TALLOC_FREE(subreq);
190 if (tevent_req_error(req, ret)) {
191 return;
193 state->msgno += 1;
195 if (state->msgno >= 10) {
196 return;
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)) {
203 return;
205 tevent_req_set_callback(subreq, ctdb_conn_test_msg_sent, req);
208 static int ctdb_conn_test_recv(struct tevent_req *req)
210 int err;
211 if (tevent_req_is_unix_error(req, &err)) {
212 return err;
214 return 0;
217 bool run_ctdb_conn(int dummy)
219 struct tevent_context *ev;
220 struct tevent_req *req;
221 int ret;
223 ev = samba_tevent_context_init(talloc_tos());
224 if (ev == NULL) {
225 fprintf(stderr, "tevent_context_init failed\n");
226 return false;
228 req = ctdb_conn_test_send(ev, ev);
229 if (req == NULL) {
230 fprintf(stderr, "ctdb_conn_test_send failed\n");
231 return false;
233 if (!tevent_req_poll(req, ev)) {
234 fprintf(stderr, "tevent_req_poll failed\n");
235 return false;
237 ret = ctdb_conn_test_recv(req);
238 TALLOC_FREE(req);
239 printf("ctdb_conn_test returned %s\n",
240 ret ? strerror(ret) : "success");
241 TALLOC_FREE(ev);
242 return (ret == 0);