ctdb: add ctdb_canonicalize_ip_inplace() helper
[Samba.git] / ctdb / tests / src / fetch_readonly.c
blobff126bdf5ae55c152bb4320d78fc09f1c5cf3d2c
1 /*
2 Fetch a single record using readonly
4 Copyright (C) Amitay Isaacs 2016
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 "replace.h"
21 #include "system/network.h"
23 #include "lib/util/debug.h"
24 #include "lib/util/tevent_unix.h"
26 #include "client/client.h"
27 #include "tests/src/test_options.h"
28 #include "tests/src/cluster_wait.h"
31 struct fetch_readonly_state {
32 struct tevent_context *ev;
35 static void fetch_readonly_done(struct tevent_req *subreq);
37 static struct tevent_req *fetch_readonly_send(TALLOC_CTX *mem_ctx,
38 struct tevent_context *ev,
39 struct ctdb_client_context *client,
40 struct ctdb_db_context *db,
41 const char *keystr,
42 int timelimit)
44 struct tevent_req *req, *subreq;
45 struct fetch_readonly_state *state;
46 TDB_DATA key;
48 req = tevent_req_create(mem_ctx, &state, struct fetch_readonly_state);
49 if (req == NULL) {
50 return NULL;
53 state->ev = ev;
55 key.dptr = (uint8_t *)discard_const(keystr);
56 key.dsize = strlen(keystr);
58 subreq = ctdb_fetch_lock_send(state, ev, client, db, key, true);
59 if (tevent_req_nomem(subreq, req)) {
60 return tevent_req_post(req, ev);
62 tevent_req_set_callback(subreq, fetch_readonly_done, req);
64 return req;
67 static void fetch_readonly_done(struct tevent_req *subreq)
69 struct tevent_req *req = tevent_req_callback_data(
70 subreq, struct tevent_req);
71 struct fetch_readonly_state *state = tevent_req_data(
72 req, struct fetch_readonly_state);
73 struct ctdb_record_handle *h;
74 int ret;
76 h = ctdb_fetch_lock_recv(subreq, NULL, state, NULL, &ret);
77 TALLOC_FREE(subreq);
78 if (h == NULL) {
79 tevent_req_error(req, ret);
80 return;
83 talloc_free(h);
84 tevent_req_done(req);
87 static bool fetch_readonly_recv(struct tevent_req *req, int *perr)
89 int err;
91 if (tevent_req_is_unix_error(req, &err)) {
92 if (perr != NULL) {
93 *perr = err;
95 return false;
97 return true;
100 int main(int argc, const char *argv[])
102 const struct test_options *opts;
103 TALLOC_CTX *mem_ctx;
104 struct tevent_context *ev;
105 struct ctdb_client_context *client;
106 struct ctdb_db_context *ctdb_db;
107 struct tevent_req *req;
108 int ret;
109 bool status;
111 setup_logging("fetch_readonly", DEBUG_STDERR);
113 status = process_options_database(argc, argv, &opts);
114 if (! status) {
115 exit(1);
118 mem_ctx = talloc_new(NULL);
119 if (mem_ctx == NULL) {
120 fprintf(stderr, "Memory allocation error\n");
121 exit(1);
124 ev = tevent_context_init(mem_ctx);
125 if (ev == NULL) {
126 fprintf(stderr, "Memory allocation error\n");
127 exit(1);
130 ret = ctdb_client_init(mem_ctx, ev, opts->socket, &client);
131 if (ret != 0) {
132 fprintf(stderr, "Failed to initialize client, %s\n",
133 strerror(ret));
134 exit(1);
137 if (! ctdb_recovery_wait(ev, client)) {
138 fprintf(stderr, "Memory allocation error\n");
139 exit(1);
142 ret = ctdb_attach(ev, client, tevent_timeval_zero(), opts->dbname, 0,
143 &ctdb_db);
144 if (ret != 0) {
145 fprintf(stderr, "Failed to attach to DB %s\n", opts->dbname);
146 exit(1);
149 req = fetch_readonly_send(mem_ctx, ev, client, ctdb_db,
150 opts->keystr, opts->timelimit);
151 if (req == NULL) {
152 fprintf(stderr, "Memory allocation error\n");
153 exit(1);
156 tevent_req_poll(req, ev);
158 status = fetch_readonly_recv(req, &ret);
159 if (! status) {
160 fprintf(stderr, "fetch readonly loop test failed\n");
161 exit(1);
164 talloc_free(mem_ctx);
165 return 0;