ctdb-util: Rename db_wrap to tdb_wrap and make it a build subsystem
[Samba.git] / ctdb / tests / src / ctdb_fetch_readonly_once.c
blob5dc64e0e0639fce648406274d69429c8b98dc9ad
1 /*
2 simple ctdb test tool
3 This test just fetch_locks a record and releases it once.
5 Copyright (C) Ronnie Sahlberg 2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "popt.h"
24 #include <poll.h>
26 const char *TESTKEY = "testkey";
29 Just try locking/unlocking a single record once
31 static void fetch_readonly_once(struct ctdb_context *ctdb, struct ctdb_db_context *ctdb_db, TDB_DATA key)
33 TDB_DATA data;
34 struct ctdb_record_handle *h;
36 printf("Trying to readonly fetch lock the record ...\n");
38 h = ctdb_fetch_readonly_lock(ctdb_db, ctdb, key, &data, 1);
39 if (h == NULL) {
40 fprintf(stderr, "Failed to get readonly lock\n");
41 exit(1);
44 talloc_free(h);
45 printf("Record released.\n");
49 main program
51 int main(int argc, const char *argv[])
53 struct ctdb_context *ctdb;
54 struct ctdb_db_context *ctdb_db;
55 struct event_context *ev;
57 TDB_DATA key;
59 struct poptOption popt_options[] = {
60 POPT_AUTOHELP
61 { "record", 'r', POPT_ARG_STRING, &TESTKEY, 0, "record", "string" },
62 POPT_TABLEEND
64 int opt;
65 const char **extra_argv;
66 int extra_argc = 0;
67 poptContext pc;
69 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
71 while ((opt = poptGetNextOpt(pc)) != -1) {
72 switch (opt) {
73 default:
74 fprintf(stderr, "Invalid option %s: %s\n",
75 poptBadOption(pc, 0), poptStrerror(opt));
76 exit(1);
80 /* setup the remaining options for the main program to use */
81 extra_argv = poptGetArgs(pc);
82 if (extra_argv) {
83 extra_argv++;
84 while (extra_argv[extra_argc]) extra_argc++;
87 ev = event_context_init(NULL);
89 ctdb = ctdb_cmdline_client(ev, timeval_current_ofs(3, 0));
90 if (ctdb == NULL) {
91 printf("failed to connect to ctdb daemon.\n");
92 exit(1);
95 key.dptr = discard_const(TESTKEY);
96 key.dsize = strlen(TESTKEY);
98 /* attach to a specific database */
99 ctdb_db = ctdb_attach(ctdb, timeval_current_ofs(3, 0), "test.tdb",
100 false, 0);
101 if (!ctdb_db) {
102 fprintf(stderr, "ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
103 exit(10);
106 printf("Waiting for cluster\n");
107 while (1) {
108 uint32_t recmode=1;
109 ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
110 if (recmode == 0) break;
111 event_loop_once(ev);
114 fetch_readonly_once(ctdb, ctdb_db, key);
116 return 0;