Clean up RPM spec for newer distributions
[clumanager.git] / librhcm / api.c
blob9ec8bfae1e55695a29f53ad704ba7884a4671fb4
1 /*
2 Copyright Red Hat, Inc. 2002-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 * Main user/client API driver routines.
23 * This contains the API routines which user/client applications use to
24 * talk directly to Red Hat Cluster Manager.
26 #include <cm_api.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <pthread.h>
31 #include <apitcp.h>
33 int memb_register(void); /** Defined in membership.c */
34 int quorum_register(void); /** Defined in quorum.c */
36 /**
37 * Read a membership event from a file descriptor. The idea here is that the
38 * event itself is opaque to the user.
40 * @param fd The File descriptor we selected.
41 * @return The event, if any, present on fd.
43 cm_event_t *
44 cm_ev_read(int fd)
46 fd_set set;
47 cm_event_t *msg = NULL;
48 int rv, c;
49 struct timeval tv;
51 if (fd < 0) {
52 errno = EINVAL;
53 return NULL;
56 FD_ZERO(&set);
57 FD_SET(fd, &set);
59 tv.tv_sec = 3;
60 tv.tv_usec = 0;
62 rv = select(fd + 1, &set, NULL, NULL, &tv);
63 if (!rv)
64 return NULL;
66 msg = malloc(sizeof(cm_event_t));
67 if (!msg)
68 return NULL;
70 rv = tcp_receive(fd, msg, sizeof(cm_event_t));
71 if ((rv != sizeof(cm_event_t)) || (rv == -1)) {
72 free(msg);
73 return NULL;
76 swab_cm_event_hdr_t(&msg->em_header);
78 if ((cm_ev_type(msg) != EC_MEMBERSHIP) &&
79 (cm_ev_type(msg) != EC_QUORUM)) {
80 free(msg);
81 errno = EINVAL;
82 return NULL;
85 if (msg->em_header.eh_length < sizeof(cm_event_t)) {
86 free(msg);
87 errno = EINVAL;
88 return NULL;
91 switch(cm_ev_event(msg)) {
92 case EV_MEMB_UPDATE:
93 swab_cm_memb_event_t(&msg->u.ev_memb, c);
94 break;
95 case EV_QUORUM:
96 case EV_NO_QUORUM:
97 case EV_QUORUM_GAINED:
98 case EV_QUORUM_LOST:
99 swab_cm_quorum_event_t(&msg->u.ev_quorum, c);
100 break;
101 default:
102 break;
105 return msg;
110 * Free an event. Because the contents of a cluster event may change, we don't
111 * want users freeing them by hand.
113 * @param eventp The cluster event to free.
115 void
116 cm_ev_free(cm_event_t *eventp)
118 free(eventp);
122 * Register for event(s). This contacts the appropriate daemon(s) and sends
123 * an EV_REGISTER with the appropriate flags to the daemon(s).
125 * @param event_class The class of events to register for.
126 * @return -1 on failure, 0 on success.
129 cm_ev_register(int event_class)
131 errno = 0;
133 if (event_class == EC_MEMBERSHIP) {
134 return memb_register();
135 } else if (event_class == EC_QUORUM) {
136 return quorum_register();
139 errno = EINVAL;
140 return -1;
145 * Unregister for events on a given file descriptor. Right now, this simply
146 * closes the file descriptor.
148 * @param fd The file descriptor to unregister/close.
149 * @return 0
152 cm_ev_unregister(int fd)
154 close(fd);
155 return 0;