add a machinereadable version of ctdb stats/statistics
[metze/ctdb/wip.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_getpnn_send
27 #undef ctdb_getnodemap_send
28 #undef ctdb_getpublicips_send
29
30 bool ctdb_getrecmaster_recv(struct ctdb_connection *ctdb,
31                            struct ctdb_request *req, uint32_t *recmaster)
32 {
33         struct ctdb_reply_control *reply;
34
35         reply = unpack_reply_control(ctdb, req, CTDB_CONTROL_GET_RECMASTER);
36         if (!reply) {
37                 return false;
38         }
39         if (reply->status == -1) {
40                 DEBUG(ctdb, LOG_ERR, "ctdb_getrecmaster_recv: status -1");
41                 return false;
42         }
43         *recmaster = reply->status;
44         return true;
45 }
46
47 struct ctdb_request *ctdb_getrecmaster_send(struct ctdb_connection *ctdb,
48                                             uint32_t destnode,
49                                             ctdb_callback_t callback,
50                                             void *private_data)
51 {
52         return new_ctdb_control_request(ctdb, CTDB_CONTROL_GET_RECMASTER,
53                                         destnode, NULL, 0,
54                                         callback, private_data);
55 }
56
57 bool ctdb_getpnn_recv(struct ctdb_connection *ctdb,
58                      struct ctdb_request *req, uint32_t *pnn)
59 {
60         struct ctdb_reply_control *reply;
61
62         reply = unpack_reply_control(ctdb, req, CTDB_CONTROL_GET_PNN);
63         if (!reply) {
64                 return false;
65         }
66         if (reply->status == -1) {
67                 DEBUG(ctdb, LOG_ERR, "ctdb_getpnn_recv: status -1");
68                 return false;
69         }
70         *pnn = reply->status;
71         return true;
72 }
73
74 struct ctdb_request *ctdb_getpnn_send(struct ctdb_connection *ctdb,
75                                       uint32_t destnode,
76                                       ctdb_callback_t callback,
77                                       void *private_data)
78 {
79         return new_ctdb_control_request(ctdb, CTDB_CONTROL_GET_PNN, destnode,
80                                         NULL, 0, callback, private_data);
81 }
82
83 bool ctdb_getnodemap_recv(struct ctdb_connection *ctdb,
84                       struct ctdb_request *req, struct ctdb_node_map **nodemap)
85 {
86         struct ctdb_reply_control *reply;
87
88         *nodemap = NULL;
89         reply = unpack_reply_control(ctdb, req, CTDB_CONTROL_GET_NODEMAP);
90         if (!reply) {
91                 return false;
92         }
93         if (reply->status == -1) {
94                 DEBUG(ctdb, LOG_ERR, "ctdb_getnodemap_recv: status -1");
95                 return false;
96         }
97         if (reply->datalen == 0) {
98                 DEBUG(ctdb, LOG_ERR, "ctdb_getnodemap_recv: returned data is 0 bytes");
99                 return false;
100         }
101
102         *nodemap = malloc(reply->datalen);
103         if (*nodemap == NULL) {
104                 DEBUG(ctdb, LOG_ERR, "ctdb_getnodemap_recv: failed to malloc buffer");
105                 return false;
106         }
107         memcpy(*nodemap, reply->data, reply->datalen);
108
109         return true;
110 }
111 struct ctdb_request *ctdb_getnodemap_send(struct ctdb_connection *ctdb,
112                                           uint32_t destnode,
113                                           ctdb_callback_t callback,
114                                           void *private_data)
115 {
116         return new_ctdb_control_request(ctdb, CTDB_CONTROL_GET_NODEMAP,
117                                         destnode,
118                                         NULL, 0, callback, private_data);
119 }
120
121 void ctdb_free_nodemap(struct ctdb_node_map *nodemap)
122 {
123         if (nodemap == NULL) {
124                 return;
125         }
126         free(nodemap);
127 }
128
129 bool ctdb_getpublicips_recv(struct ctdb_connection *ctdb,
130                             struct ctdb_request *req,
131                             struct ctdb_all_public_ips **ips)
132 {
133         struct ctdb_reply_control *reply;
134
135         *ips = NULL;
136         reply = unpack_reply_control(ctdb, req, CTDB_CONTROL_GET_PUBLIC_IPS);
137         if (!reply) {
138                 return false;
139         }
140         if (reply->status == -1) {
141                 DEBUG(ctdb, LOG_ERR, "ctdb_getpublicips_recv: status -1");
142                 return false;
143         }
144         if (reply->datalen == 0) {
145                 DEBUG(ctdb, LOG_ERR, "ctdb_getpublicips_recv: returned data is 0 bytes");
146                 return false;
147         }
148
149         *ips = malloc(reply->datalen);
150         if (*ips == NULL) {
151                 DEBUG(ctdb, LOG_ERR, "ctdb_getpublicips_recv: failed to malloc buffer");
152                 return false;
153         }
154         memcpy(*ips, reply->data, reply->datalen);
155
156         return true;
157 }
158 struct ctdb_request *ctdb_getpublicips_send(struct ctdb_connection *ctdb,
159                                             uint32_t destnode,
160                                             ctdb_callback_t callback,
161                                             void *private_data)
162 {
163         return new_ctdb_control_request(ctdb, CTDB_CONTROL_GET_PUBLIC_IPS,
164                                         destnode,
165                                         NULL, 0, callback, private_data);
166 }
167
168 void ctdb_free_publicips(struct ctdb_all_public_ips *ips)
169 {
170         if (ips == NULL) {
171                 return;
172         }
173         free(ips);
174 }