ctdbd: Pass event name to hung script debugger
[Samba.git] / ctdb / tests / src / ctdb_fetch_lock_once.c
blobf92b0b45ba60079a03fb18aa0b0a22aa6187643c
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>
25 #include "ctdb.h"
27 #define TESTKEY "testkey"
29 static void rrl_cb(struct ctdb_db *ctdb_db,
30 struct ctdb_lock *lock, TDB_DATA outdata, void *private)
32 bool *rrl_cb_called = private;
34 printf("Record fetchlocked.\n");
35 printf("Press enter to release the record ...\n");
36 (void)getchar();
37 printf("Record released.\n");
39 *rrl_cb_called = true;
40 return;
44 Just try locking/unlocking a single record once
46 static void fetch_lock_once(struct ctdb_connection *ctdb, struct ctdb_db *ctdb_db)
48 TDB_DATA key;
49 bool rrl_cb_finished = false;
51 key.dptr = discard_const(TESTKEY);
52 key.dsize = strlen(TESTKEY);
54 printf("Trying to fetch lock the record ...\n");
56 /* In the non-contended case the callback might be invoked
57 * immediately, before ctdb_readrecordlock_async() returns.
58 * In the contended case the callback will be invoked later.
60 * Normally an application would not care whether the callback
61 * has already been invoked here or not, but if the application
62 * needs to know, it can use the *private_data pointer
63 * to pass data through to the callback and back.
65 if (!ctdb_readrecordlock_async(ctdb_db, key,
66 rrl_cb, &rrl_cb_finished)) {
67 printf("Failed to send READRECORDLOCK\n");
68 exit(10);
70 while (!rrl_cb_finished) {
71 struct pollfd pfd;
73 pfd.fd = ctdb_get_fd(ctdb);
74 pfd.events = ctdb_which_events(ctdb);
75 if (poll(&pfd, 1, -1) < 0) {
76 printf("Poll failed");
77 exit(10);
79 if (ctdb_service(ctdb, pfd.revents) < 0) {
80 printf("Failed to service\n");
81 exit(10);
87 main program
89 int main(int argc, const char *argv[])
91 struct ctdb_connection *ctdb;
92 struct ctdb_db *ctdb_db;
94 struct poptOption popt_options[] = {
95 POPT_AUTOHELP
96 POPT_TABLEEND
98 int opt;
99 const char **extra_argv;
100 int extra_argc = 0;
101 poptContext pc;
103 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
105 while ((opt = poptGetNextOpt(pc)) != -1) {
106 switch (opt) {
107 default:
108 fprintf(stderr, "Invalid option %s: %s\n",
109 poptBadOption(pc, 0), poptStrerror(opt));
110 exit(1);
114 /* setup the remaining options for the main program to use */
115 extra_argv = poptGetArgs(pc);
116 if (extra_argv) {
117 extra_argv++;
118 while (extra_argv[extra_argc]) extra_argc++;
121 ctdb = ctdb_connect("/tmp/ctdb.socket",
122 ctdb_log_file, stderr);
123 if (!ctdb) {
124 printf("Connecting to /tmp/ctdb.socket\n");
125 exit(1);
128 /* attach to a specific database */
129 ctdb_db = ctdb_attachdb(ctdb, "test.tdb", false, 0);
130 if (!ctdb_db) {
131 printf("ctdb_attachdb failed\n");
132 exit(1);
135 printf("Waiting for cluster\n");
136 while (1) {
137 uint32_t recmode=1;
138 ctdb_getrecmode(ctdb, CTDB_CURRENT_NODE, &recmode);
139 if (recmode == 0) break;
140 sleep(1);
143 fetch_lock_once(ctdb, ctdb_db);
145 return 0;