2 * Copyright (c) 2009-2014 Red Hat, Inc.
6 * Author: Christine Caulfield (ccaulfie@redhat.com)
8 * This software licensed under BSD license, the text of which follows:
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
13 * - Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 * - Neither the name of the MontaVista Software, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived from this
20 * software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
37 #include <sys/types.h>
44 #include <corosync/corotypes.h>
45 #include <corosync/votequorum.h>
47 static votequorum_handle_t handle
;
49 static votequorum_ring_id_t last_received_ring_id
;
50 static votequorum_ring_id_t ring_id_to_send
;
51 static int no_sent_old_ringid
= 0;
53 static int print_info(int ok_to_fail
)
55 struct votequorum_info info
;
58 if ( (err
=votequorum_getinfo(handle
, VOTEQUORUM_QDEVICE_NODEID
, &info
)) != CS_OK
) {
59 fprintf(stderr
, "votequorum_getinfo error %d: %s\n", err
, ok_to_fail
?"OK":"FAILED");
63 printf("name %s\n", info
.qdevice_name
);
64 printf("qdevice votes %d\n", info
.qdevice_votes
);
65 if (info
.flags
& VOTEQUORUM_INFO_QDEVICE_ALIVE
) {
68 if (info
.flags
& VOTEQUORUM_INFO_QDEVICE_CAST_VOTE
) {
71 if (info
.flags
& VOTEQUORUM_INFO_QDEVICE_MASTER_WINS
) {
72 printf("master-wins");
79 static void votequorum_nodelist_notification_fn(
80 votequorum_handle_t vq_handle
,
82 votequorum_ring_id_t ring_id
,
83 uint32_t node_list_entries
,
87 printf("votequorum nodelist notification called \n");
88 printf(" current ringid = (" CS_PRI_RING_ID
")\n", ring_id
.nodeid
, ring_id
.seq
);
91 memcpy(&last_received_ring_id
, &ring_id
, sizeof(ring_id
));
92 no_sent_old_ringid
= 0;
95 static void usage(const char *command
)
97 printf("%s [-F <num>] [-p <num>] [-t <time>] [-n <name>] [-c] [-m]\n", command
);
98 printf(" -F <num> Number of times to send old ringid before actual ringid is sent (for testing, default = 0)\n");
99 printf(" -p <num> Number of times to poll qdevice (default 0=infinte)\n");
100 printf(" -t <secs> Time (in seconds) to wait between polls (default=1)\n");
101 printf(" -n <name> Name of quorum device (default QDEVICE)\n");
102 printf(" -c Don't cast vote (default is to cast vote)\n");
103 printf(" -q Don't print device status every poll time (default=will print)\n");
104 printf(" -m Master wins (default no)\n");
105 printf(" -1 Print status once and exit\n");
108 int main(int argc
, char *argv
[])
111 int cast_vote
= 1, master_wins
= 0;
112 int pollcount
=0, polltime
=1, quiet
=0, once
=0;
113 int send_old_ringid
= 0;
116 votequorum_callbacks_t callbacks
;
117 const char *devicename
= "QDEVICE";
118 const char *options
= "F:n:p:t:cmq1h";
120 memset(&callbacks
, 0, sizeof(callbacks
));
121 callbacks
.votequorum_nodelist_notify_fn
= votequorum_nodelist_notification_fn
;
123 while ((opt
= getopt(argc
, argv
, options
)) != -1) {
138 send_old_ringid
= atoi(optarg
)+1;
141 pollcount
= atoi(optarg
)+1;
144 devicename
= strdup(optarg
);
147 polltime
= atoi(optarg
);
155 if ( (err
=votequorum_initialize(&handle
, &callbacks
)) != CS_OK
) {
156 fprintf(stderr
, "votequorum_initialize FAILED: %d\n", err
);
161 fprintf(stderr
, "setting both -q (quet) and -1 (once) makes no sense\n");
174 if ( (err
= votequorum_trackstart(handle
, handle
, CS_TRACK_CHANGES
)) != CS_OK
) {
175 fprintf(stderr
, "votequorum_trackstart FAILED: %d\n", err
);
180 if ( (err
=votequorum_qdevice_register(handle
, devicename
)) != CS_OK
) {
181 fprintf(stderr
, "qdevice_register FAILED: %d\n", err
);
186 if ( (err
=votequorum_qdevice_master_wins(handle
, devicename
, master_wins
)) != CS_OK
) {
187 fprintf(stderr
, "qdevice_master_wins FAILED: %d\n", err
);
193 while (--pollcount
) {
194 if (votequorum_dispatch(handle
, CS_DISPATCH_ALL
) != CS_OK
) {
195 fprintf(stderr
, "votequorum_dispatch error\n");
200 if (!quiet
) print_info(0);
202 if (no_sent_old_ringid
+ 1 >= send_old_ringid
) {
204 * Finally send correct ringid
206 memcpy(&ring_id_to_send
, &last_received_ring_id
, sizeof(ring_id_to_send
));
208 no_sent_old_ringid
++;
211 if ((err
=votequorum_qdevice_poll(handle
, devicename
, cast_vote
, ring_id_to_send
)) != CS_OK
&&
212 err
!= CS_ERR_MESSAGE_ERROR
) {
213 fprintf(stderr
, "qdevice poll FAILED: %d\n", err
);
217 if (err
== CS_ERR_MESSAGE_ERROR
) {
218 fprintf(stderr
, "qdevice poll passed OLD ring_id\n");
221 if (!quiet
) print_info(0);
224 if ((err
= votequorum_qdevice_unregister(handle
, devicename
)) != CS_OK
) {
225 fprintf(stderr
, "qdevice unregister FAILED: %d\n", err
);
231 if (!quiet
) print_info(1);
234 votequorum_finalize(handle
);