2
* Copyright (c) 2006-2009 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 CONTRIBUTORS "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.
42
#include <sys/types.h>
43
#include <sys/socket.h>
44
#include <sys/select.h>
46
#include <netinet/in.h>
47
#include <arpa/inet.h>
49
#include <corosync/corotypes.h>
50
#include <corosync/cpg.h>
53
static int show_ip = 0;
55
static void print_cpgname (const struct cpg_name *name)
59
for (i = 0; i < name->length; i++) {
60
printf ("%c", name->value[i]);
64
static void DeliverCallback (
66
const struct cpg_name *groupName,
74
saddr.s_addr = nodeid;
75
printf("DeliverCallback: message (len=%lu)from node/pid %s/%d: '%s'\n",
76
(unsigned long int) msg_len,
77
inet_ntoa(saddr), pid, (const char *)msg);
80
printf("DeliverCallback: message (len=%lu)from node/pid %d/%d: '%s'\n",
81
(unsigned long int) msg_len, nodeid, pid,
86
static void ConfchgCallback (
88
const struct cpg_name *groupName,
89
const struct cpg_address *member_list, size_t member_list_entries,
90
const struct cpg_address *left_list, size_t left_list_entries,
91
const struct cpg_address *joined_list, size_t joined_list_entries)
96
printf("\nConfchgCallback: group '");
97
print_cpgname(groupName);
99
for (i=0; i<joined_list_entries; i++) {
101
saddr.s_addr = joined_list[i].nodeid;
102
printf("joined node/pid: %s/%d reason: %d\n",
103
inet_ntoa (saddr), joined_list[i].pid,
104
joined_list[i].reason);
107
printf("joined node/pid: %d/%d reason: %d\n",
108
joined_list[i].nodeid, joined_list[i].pid,
109
joined_list[i].reason);
113
for (i=0; i<left_list_entries; i++) {
115
saddr.s_addr = left_list[i].nodeid;
116
printf("left node/pid: %s/%d reason: %d\n",
117
inet_ntoa (saddr), left_list[i].pid,
118
left_list[i].reason);
121
printf("left node/pid: %d/%d reason: %d\n",
122
left_list[i].nodeid, left_list[i].pid,
123
left_list[i].reason);
127
printf("nodes in group now %lu\n",
128
(unsigned long int) member_list_entries);
129
for (i=0; i<member_list_entries; i++) {
131
saddr.s_addr = member_list[i].nodeid;
132
printf("node/pid: %s/%d\n",
133
inet_ntoa (saddr), member_list[i].pid);
136
printf("node/pid: %d/%d\n",
137
member_list[i].nodeid, member_list[i].pid);
142
NOTE: in reality we should also check the nodeid */
143
if (left_list_entries && left_list[0].pid == getpid()) {
144
printf("We have left the building\n");
149
static cpg_callbacks_t callbacks = {
150
.cpg_deliver_fn = DeliverCallback,
151
.cpg_confchg_fn = ConfchgCallback,
154
static void sigintr_handler (int signum) __attribute__((__noreturn__));
155
static void sigintr_handler (int signum) {
158
static struct cpg_name group_name;
160
int main (int argc, char *argv[]) {
165
const char *options = "i";
171
while ( (opt = getopt(argc, argv, options)) != -1 ) {
180
strcpy(group_name.value, argv[optind]);
181
group_name.length = strlen(argv[optind])+1;
184
strcpy(group_name.value, "GROUP");
185
group_name.length = 6;
188
result = cpg_initialize (&handle, &callbacks);
189
if (result != CS_OK) {
190
printf ("Could not initialize Cluster Process Group API instance error %d\n", result);
193
cpg_zcb_alloc (handle, 8192, &buffer);
194
cpg_zcb_free (handle, buffer);
195
cpg_zcb_alloc (handle, 8192, &buffer);
197
result = cpg_local_get (handle, &nodeid);
198
if (result != CS_OK) {
199
printf ("Could not get local node id\n");
203
printf ("Local node id is %x\n", nodeid);
204
result = cpg_join(handle, &group_name);
205
if (result != CS_OK) {
206
printf ("Could not join process group, error %d\n", result);
211
cpg_fd_get(handle, &select_fd);
212
printf ("Type EXIT to finish\n");
214
FD_SET (select_fd, &read_fds);
215
FD_SET (STDIN_FILENO, &read_fds);
216
result = select (select_fd + 1, &read_fds, 0, 0, 0);
220
if (FD_ISSET (STDIN_FILENO, &read_fds)) {
221
fgets_res = fgets(buffer, sizeof(buffer), stdin);
222
if (fgets_res == NULL) {
223
cpg_leave(handle, &group_name);
225
if (strncmp(buffer, "EXIT", 4) == 0) {
226
cpg_leave(handle, &group_name);
229
cpg_zcb_mcast_joined (handle, CPG_TYPE_AGREED,
230
buffer, strlen (buffer) + 1);
233
if (FD_ISSET (select_fd, &read_fds)) {
234
if (cpg_dispatch (handle, CS_DISPATCH_ALL) != CS_OK)
237
} while (result && !quit);
240
result = cpg_finalize (handle);
241
printf ("Finalize result is %d (should be 1)\n", result);