~ubuntu-branches/ubuntu/vivid/ctdb/vivid-proposed

« back to all changes in this revision

Viewing changes to tests/src/ctdb_fetch_readonly_once.c

  • Committer: Package Import Robot
  • Author(s): Mathieu Parent
  • Date: 2011-11-06 15:18:59 UTC
  • mfrom: (1.2.14)
  • Revision ID: package-import@ubuntu.com-20111106151859-41lblk8ml4es7ra3
Tags: 1.11+git20111102-1
* New upstream release
  - removed 92-apache-service-enable.diff: integrated 
  - removed 99-fix-broken-readdir-test.diff: integrated
* d/rules, d/control, d/compat:
  - converted to dh (% target and dh_auto_*)
  - moved to compat level 9 (buildeps upgraded)
  - dh9 enabled hardening build flags
  - added hardening=+bindnow
  - dh9 enabled multiarch
    + Don't use /use/lib64 on ppc64 (Closes: #644907)
    + libctdb-dev is Multi-Arch: same
    + removed 10_no-lib64.diff: not needed with multiarch
* ctdb.init:
  - removed gettext support
  - synced with upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
   simple ctdb test tool
 
3
   This test just fetch_locks a record and releases it once.
 
4
 
 
5
   Copyright (C) Ronnie Sahlberg 2009
 
6
 
 
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.
 
11
   
 
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.
 
16
   
 
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/>.
 
19
*/
 
20
 
 
21
#include "includes.h"
 
22
#include "system/filesys.h"
 
23
#include "popt.h"
 
24
#include <poll.h>
 
25
#include "ctdb.h"
 
26
 
 
27
const char *TESTKEY = "testkey";
 
28
 
 
29
static void rorl_cb(struct ctdb_db *ctdb_db,
 
30
                   struct ctdb_lock *lock, TDB_DATA outdata, void *private)
 
31
{
 
32
        int *finished = private;
 
33
 
 
34
        printf("Record fetchlocked.\n");
 
35
        printf("Press enter to release the record ...\n");
 
36
        (void)getchar();
 
37
 
 
38
        *finished = 1;
 
39
}
 
40
 
 
41
/*
 
42
        Just try locking/unlocking a single record once
 
43
*/
 
44
static void fetch_readonly_once(struct ctdb_connection *ctdb, struct ctdb_db *ctdb_db, TDB_DATA key)
 
45
{
 
46
        int finished;
 
47
 
 
48
        printf("Trying to fetch lock the record ...\n");
 
49
 
 
50
        finished = 0;
 
51
        if (!ctdb_readonlyrecordlock_async(ctdb_db, key,
 
52
                                       rorl_cb, &finished)) {
 
53
                printf("Failed to send READONLYRECORDLOCK\n");
 
54
                exit(10);
 
55
        }
 
56
 
 
57
        while (!finished) {
 
58
                struct pollfd pfd;
 
59
 
 
60
                pfd.fd = ctdb_get_fd(ctdb);
 
61
                pfd.events = ctdb_which_events(ctdb);
 
62
                if (poll(&pfd, 1, -1) < 0) {
 
63
                        fprintf(stderr, "Poll failed");
 
64
                        exit(10);
 
65
                }
 
66
                if (ctdb_service(ctdb, pfd.revents) < 0) {
 
67
                        fprintf(stderr, "Failed to service");
 
68
                        exit(10);
 
69
                }
 
70
        }
 
71
 
 
72
        printf("Record released.\n");
 
73
}
 
74
 
 
75
/*
 
76
  main program
 
77
*/
 
78
int main(int argc, const char *argv[])
 
79
{
 
80
        struct ctdb_connection *ctdb;
 
81
        struct ctdb_db *ctdb_db;
 
82
 
 
83
        TDB_DATA key;
 
84
 
 
85
        struct poptOption popt_options[] = {
 
86
                POPT_AUTOHELP
 
87
                { "record",      'r', POPT_ARG_STRING, &TESTKEY, 0, "record", "string" },
 
88
                POPT_TABLEEND
 
89
        };
 
90
        int opt;
 
91
        const char **extra_argv;
 
92
        int extra_argc = 0;
 
93
        poptContext pc;
 
94
 
 
95
        pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
 
96
 
 
97
        while ((opt = poptGetNextOpt(pc)) != -1) {
 
98
                switch (opt) {
 
99
                default:
 
100
                        fprintf(stderr, "Invalid option %s: %s\n", 
 
101
                                poptBadOption(pc, 0), poptStrerror(opt));
 
102
                        exit(1);
 
103
                }
 
104
        }
 
105
 
 
106
        /* setup the remaining options for the main program to use */
 
107
        extra_argv = poptGetArgs(pc);
 
108
        if (extra_argv) {
 
109
                extra_argv++;
 
110
                while (extra_argv[extra_argc]) extra_argc++;
 
111
        }
 
112
 
 
113
        ctdb = ctdb_connect("/tmp/ctdb.socket",
 
114
                            ctdb_log_file, stderr);
 
115
 
 
116
        if (!ctdb) {
 
117
                fprintf(stderr, "Connecting to /tmp/ctdb.socket");
 
118
                exit(10);
 
119
        }
 
120
 
 
121
        key.dptr  = discard_const(TESTKEY);
 
122
        key.dsize = strlen(TESTKEY);
 
123
 
 
124
        /* attach to a specific database */
 
125
        ctdb_db = ctdb_attachdb(ctdb, "test.tdb", false, 0);
 
126
        if (!ctdb_db) {
 
127
                fprintf(stderr, "ctdb_attachdb failed\n");
 
128
                exit(10);
 
129
        }
 
130
 
 
131
        printf("Waiting for cluster\n");
 
132
        while (1) {
 
133
                uint32_t recmode=1;
 
134
                ctdb_getrecmode(ctdb, CTDB_CURRENT_NODE, &recmode);
 
135
                if (recmode == 0) break;
 
136
                sleep(1);
 
137
        }
 
138
 
 
139
        fetch_readonly_once(ctdb, ctdb_db, key);
 
140
 
 
141
        return 0;
 
142
}