1 /*
2 Fetch a single record using readonly
3
4 Copyright (C) Amitay Isaacs 2016
5
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.
10
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.
15
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/>.
18 */
19
20 #include "replace.h"
21 #include "system/network.h"
22
23 #include "lib/util/debug.h"
24 #include "lib/util/tevent_unix.h"
25
26 #include "client/client.h"
27 #include "tests/src/test_options.h"
28 #include "tests/src/cluster_wait.h"
29
30
31 struct fetch_readonly_state {
32 struct tevent_context *ev;
33 };
34
35 static void fetch_readonly_done(struct tevent_req *subreq);
36
fetch_readonly_send(TALLOC_CTX * mem_ctx,struct tevent_context * ev,struct ctdb_client_context * client,struct ctdb_db_context * db,const char * keystr,int timelimit)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)
43 {
44 struct tevent_req *req, *subreq;
45 struct fetch_readonly_state *state;
46 TDB_DATA key;
47
48 req = tevent_req_create(mem_ctx, &state, struct fetch_readonly_state);
49 if (req == NULL) {
50 return NULL;
51 }
52
53 state->ev = ev;
54
55 key.dptr = (uint8_t *)discard_const(keystr);
56 key.dsize = strlen(keystr);
57
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);
61 }
62 tevent_req_set_callback(subreq, fetch_readonly_done, req);
63
64 return req;
65 }
66
fetch_readonly_done(struct tevent_req * subreq)67 static void fetch_readonly_done(struct tevent_req *subreq)
68 {
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;
75
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;
81 }
82
83 talloc_free(h);
84 tevent_req_done(req);
85 }
86
fetch_readonly_recv(struct tevent_req * req,int * perr)87 static bool fetch_readonly_recv(struct tevent_req *req, int *perr)
88 {
89 int err;
90
91 if (tevent_req_is_unix_error(req, &err)) {
92 if (perr != NULL) {
93 *perr = err;
94 }
95 return false;
96 }
97 return true;
98 }
99
main(int argc,const char * argv[])100 int main(int argc, const char *argv[])
101 {
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;
110
111 setup_logging("fetch_readonly", DEBUG_STDERR);
112
113 status = process_options_database(argc, argv, &opts);
114 if (! status) {
115 exit(1);
116 }
117
118 mem_ctx = talloc_new(NULL);
119 if (mem_ctx == NULL) {
120 fprintf(stderr, "Memory allocation error\n");
121 exit(1);
122 }
123
124 ev = tevent_context_init(mem_ctx);
125 if (ev == NULL) {
126 fprintf(stderr, "Memory allocation error\n");
127 exit(1);
128 }
129
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);
135 }
136
137 if (! ctdb_recovery_wait(ev, client)) {
138 fprintf(stderr, "Memory allocation error\n");
139 exit(1);
140 }
141
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);
147 }
148
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);
154 }
155
156 tevent_req_poll(req, ev);
157
158 status = fetch_readonly_recv(req, &ret);
159 if (! status) {
160 fprintf(stderr, "fetch readonly loop test failed\n");
161 exit(1);
162 }
163
164 talloc_free(mem_ctx);
165 return 0;
166 }
167