Tests - eventscripts - new function simple_test_event()
[sahlberg/ctdb.git] / libctdb / control.c
1 /*
2    Misc control routines of libctdb
3
4    Copyright (C) Rusty Russell 2010
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19 #include <string.h>
20 #include <ctdb.h>
21 #include <ctdb_protocol.h>
22 #include "libctdb_private.h"
23
24 /* Remove type-safety macros. */
25 #undef ctdb_getrecmaster_send
26 #undef ctdb_getrecmode_send
27 #undef ctdb_getpnn_send
28 #undef ctdb_getnodemap_send
29 #undef ctdb_getpublicips_send
30
31 bool ctdb_getrecmaster_recv(struct ctdb_connection *ctdb,
32                            struct ctdb_request *req, uint32_t *recmaster)
33 {
34         struct ctdb_reply_control *reply;
35
36         reply = unpack_reply_control(req, CTDB_CONTROL_GET_RECMASTER);
37         if (!reply) {
38                 return false;
39         }
40         if (reply->status == -1) {
41                 DEBUG(ctdb, LOG_ERR, "ctdb_getrecmaster_recv: status -1");
42                 return false;
43         }
44         *recmaster = reply->status;
45         return true;
46 }
47
48 struct ctdb_request *ctdb_getrecmaster_send(struct ctdb_connection *ctdb,
49                                             uint32_t destnode,
50                                             ctdb_callback_t callback,
51                                             void *private_data)
52 {
53         return new_ctdb_control_request(ctdb, CTDB_CONTROL_GET_RECMASTER,
54                                         destnode, NULL, 0,
55                                         callback, private_data);
56 }
57
58 bool ctdb_getrecmode_recv(struct ctdb_connection *ctdb,
59                           struct ctdb_request *req, uint32_t *recmode)
60 {
61         struct ctdb_reply_control *reply;
62
63         reply = unpack_reply_control(req, CTDB_CONTROL_GET_RECMODE);
64         if (!reply) {
65                 return false;
66         }
67         if (reply->status == -1) {
68                 DEBUG(ctdb, LOG_ERR, "ctdb_getrecmode_recv: status -1");
69                 return false;
70         }
71         *recmode = reply->status;
72         return true;
73 }
74
75 struct ctdb_request *ctdb_getrecmode_send(struct ctdb_connection *ctdb,
76                                             uint32_t destnode,
77                                             ctdb_callback_t callback,
78                                             void *private_data)
79 {
80         return new_ctdb_control_request(ctdb, CTDB_CONTROL_GET_RECMODE,
81                                         destnode, NULL, 0,
82                                         callback, private_data);
83 }
84
85 bool ctdb_getpnn_recv(struct ctdb_connection *ctdb,
86                      struct ctdb_request *req, uint32_t *pnn)
87 {
88         struct ctdb_reply_control *reply;
89
90         reply = unpack_reply_control(req, CTDB_CONTROL_GET_PNN);
91         if (!reply) {
92                 return false;
93         }
94         if (reply->status == -1) {
95                 DEBUG(ctdb, LOG_ERR, "ctdb_getpnn_recv: status -1");
96                 return false;
97         }
98         *pnn = reply->status;
99         return true;
100 }
101
102 struct ctdb_request *ctdb_getpnn_send(struct ctdb_connection *ctdb,
103                                       uint32_t destnode,
104                                       ctdb_callback_t callback,
105                                       void *private_data)
106 {
107         return new_ctdb_control_request(ctdb, CTDB_CONTROL_GET_PNN, destnode,
108                                         NULL, 0, callback, private_data);
109 }
110
111 bool ctdb_getnodemap_recv(struct ctdb_connection *ctdb,
112                       struct ctdb_request *req, struct ctdb_node_map **nodemap)
113 {
114         struct ctdb_reply_control *reply;
115
116         *nodemap = NULL;
117         reply = unpack_reply_control(req, CTDB_CONTROL_GET_NODEMAP);
118         if (!reply) {
119                 return false;
120         }
121         if (reply->status == -1) {
122                 DEBUG(ctdb, LOG_ERR, "ctdb_getnodemap_recv: status -1");
123                 return false;
124         }
125         if (reply->datalen == 0) {
126                 DEBUG(ctdb, LOG_ERR, "ctdb_getnodemap_recv: returned data is 0 bytes");
127                 return false;
128         }
129
130         *nodemap = malloc(reply->datalen);
131         if (*nodemap == NULL) {
132                 DEBUG(ctdb, LOG_ERR, "ctdb_getnodemap_recv: failed to malloc buffer");
133                 return false;
134         }
135         memcpy(*nodemap, reply->data, reply->datalen);
136
137         return true;
138 }
139 struct ctdb_request *ctdb_getnodemap_send(struct ctdb_connection *ctdb,
140                                           uint32_t destnode,
141                                           ctdb_callback_t callback,
142                                           void *private_data)
143 {
144         return new_ctdb_control_request(ctdb, CTDB_CONTROL_GET_NODEMAP,
145                                         destnode,
146                                         NULL, 0, callback, private_data);
147 }
148
149 void ctdb_free_nodemap(struct ctdb_node_map *nodemap)
150 {
151         if (nodemap == NULL) {
152                 return;
153         }
154         free(nodemap);
155 }
156
157 bool ctdb_getpublicips_recv(struct ctdb_connection *ctdb,
158                             struct ctdb_request *req,
159                             struct ctdb_all_public_ips **ips)
160 {
161         struct ctdb_reply_control *reply;
162
163         *ips = NULL;
164         reply = unpack_reply_control(req, CTDB_CONTROL_GET_PUBLIC_IPS);
165         if (!reply) {
166                 return false;
167         }
168         if (reply->status == -1) {
169                 DEBUG(ctdb, LOG_ERR, "ctdb_getpublicips_recv: status -1");
170                 return false;
171         }
172         if (reply->datalen == 0) {
173                 DEBUG(ctdb, LOG_ERR, "ctdb_getpublicips_recv: returned data is 0 bytes");
174                 return false;
175         }
176
177         *ips = malloc(reply->datalen);
178         if (*ips == NULL) {
179                 DEBUG(ctdb, LOG_ERR, "ctdb_getpublicips_recv: failed to malloc buffer");
180                 return false;
181         }
182         memcpy(*ips, reply->data, reply->datalen);
183
184         return true;
185 }
186 struct ctdb_request *ctdb_getpublicips_send(struct ctdb_connection *ctdb,
187                                             uint32_t destnode,
188                                             ctdb_callback_t callback,
189                                             void *private_data)
190 {
191         return new_ctdb_control_request(ctdb, CTDB_CONTROL_GET_PUBLIC_IPS,
192                                         destnode,
193                                         NULL, 0, callback, private_data);
194 }
195
196 void ctdb_free_publicips(struct ctdb_all_public_ips *ips)
197 {
198         if (ips == NULL) {
199                 return;
200         }
201         free(ips);
202 }