Fix compiler error due to uninitialized variable
[clumanager.git] / librhcm / quorum.c
blob5544562116a96f8af31e84ff3686ea3f0d5f11f4
1 /*
2 Copyright Red Hat, Inc. 2003
4 The Red Hat Cluster Manager API Library is free software; you can
5 redistribute it and/or modify it under the terms of the GNU Lesser
6 General Public License as published by the Free Software Foundation;
7 either version 2.1 of the License, or (at your option) any later
8 version.
10 The Red Hat Cluster Manager API Library is distributed in the hope
11 that it will be useful, but WITHOUT ANY WARRANTY; without even the
12 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 PURPOSE. See the GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 USA.
20 /** @file
21 * Quorum API functions.
23 #include <cm_api.h>
24 #include <unistd.h>
25 #include <stdlib.h>
26 #include <errno.h>
27 #include <pthread.h>
28 #include <apitcp.h>
30 static int __local_member_id = -1;
31 static pthread_mutex_t __local_member_id_lock = PTHREAD_MUTEX_INITIALIZER;
33 /**
34 * Register for quorum and/or quorum-proxied membership events.
36 * @return File descriptor, or -1 if clumembd couldn't be
37 * reached.
39 int
40 quorum_register(void)
42 int fd;
43 cm_event_hdr_t msg;
46 * Talks to the local node. If the user hasn't read the configuration
47 * file, it talks to the loopback interface, since the quorum
48 * daemon will also be listening on it.
50 fd = tcp_localconnect(CM_QUORUM_PORT);
51 if (fd == -1)
52 return -1;
54 msg.eh_magic = CLUSTER_MAGIC;
55 msg.eh_type = EV_REGISTER;
56 msg.eh_length = sizeof(msg);
58 swab_cm_event_hdr_t(&msg);
61 * Send query
63 if (tcp_send(fd, &msg, sizeof(msg)) != sizeof(msg)) {
64 close(fd);
65 return -1;
69 * Receive response
71 if (tcp_receive(fd, (void *)&msg, sizeof(msg)) !=
72 sizeof(msg)) {
73 close(fd);
74 errno = ETIMEDOUT;
75 return -1;
78 swab_cm_event_hdr_t(&msg);
80 if (msg.eh_type != EV_ACK) {
81 close(fd);
82 errno = EAGAIN;
83 return -1;
86 return fd;
90 /**
91 * Query the quorum daemon synchronously for the current status of
92 * quorum. This ALWAYS talks to the local node, and so running it from
93 * remote is not possible. This does not require the caller to know anything
94 * about the cluster configuration.
96 * @param result Unallocated space which will be allocated within
97 * and returned to the user.
98 * @return -1 on failure, 0 on success.
101 quorum_query(cm_event_t **result)
103 int fd, counter;
104 cm_event_hdr_t msg;
105 cm_event_t *view;
107 if (!result)
108 return -1;
111 * Talks to the local node. If the user hasn't read the configuration
112 * file, it talks to the loopback interface, since the quorum
113 * daemon will also be listening on it.
115 fd = tcp_localconnect(34003);
117 if (fd == -1)
118 return -1;
120 msg.eh_magic = CLUSTER_MAGIC;
121 msg.eh_type = QUORUM_QUERY;
122 msg.eh_length = sizeof(msg);
124 swab_cm_event_hdr_t(&msg);
127 * Send query
129 if (tcp_send(fd, &msg, sizeof(msg)) != sizeof(msg)) {
130 close(fd);
131 return -1;
134 *result = malloc(sizeof(*view));
135 view = (cm_event_t *)*result;
138 * Receive response
140 if (tcp_receive(fd, (void *)view, sizeof(*view)) !=
141 sizeof(*view)) {
142 close(fd);
143 free(view);
144 errno = ETIMEDOUT;
145 return -1;
148 swab_cm_event_hdr_t(&view->em_header);
149 swab_cm_quorum_event_t(&view->u.ev_quorum, counter);
151 pthread_mutex_lock(&__local_member_id_lock);
152 if (__local_member_id == -1)
153 __local_member_id = cm_ev_memberid(view);
154 pthread_mutex_unlock(&__local_member_id_lock);
156 close(fd);
157 return 0;
162 * Query the quorum daemon synchronously for the current status of
163 * quorum tiebreaker. This ALWAYS talks to the local node, and so running it
164 * from remote is not possible. This does not require the caller to know
165 * anything about the cluster configuration.
167 * @param result Unallocated space which will be allocated within
168 * and returned to the user.
169 * @return -1 on failure, 0 on success.
172 quorum_query_tb(cm_event_t **result)
174 int fd;
175 cm_event_hdr_t msg;
176 cm_event_t *view;
178 if (!result)
179 return -1;
182 * Talks to the local node. If the user hasn't read the configuration
183 * file, it talks to the loopback interface, since the quorum
184 * daemon will also be listening on it.
186 fd = tcp_localconnect(34003);
188 if (fd == -1)
189 return -1;
191 msg.eh_magic = CLUSTER_MAGIC;
192 msg.eh_type = QUORUM_QUERY_TB;
193 msg.eh_length = sizeof(msg);
195 swab_cm_event_hdr_t(&msg);
198 * Send query
200 if (tcp_send(fd, &msg, sizeof(msg)) != sizeof(msg)) {
201 close(fd);
202 return -1;
205 *result = malloc(sizeof(*view));
206 view = (cm_event_t *)*result;
209 * Receive response
211 if (tcp_receive(fd, (void *)view, sizeof(view->em_header)) !=
212 sizeof(view->em_header)) {
213 close(fd);
214 free(view);
215 errno = ETIMEDOUT;
216 return -1;
219 swab_cm_event_hdr_t(&view->em_header);
221 pthread_mutex_lock(&__local_member_id_lock);
222 if (__local_member_id == -1)
223 __local_member_id = cm_ev_memberid(view);
224 pthread_mutex_unlock(&__local_member_id_lock);
226 close(fd);
227 return 0;