ctdb-daemon: Rename struct ctdb_all_public_ips to ctdb_public_ip_list_old
[kai/samba-autobuild/.git] / ctdb / client / client_util.c
1 /*
2    CTDB client code
3
4    Copyright (C) Amitay Isaacs  2015
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
20 #include "replace.h"
21 #include "system/network.h"
22 #include "system/filesys.h"
23
24 #include <talloc.h>
25 #include <tevent.h>
26 #include <tdb.h>
27
28 #include "lib/util/debug.h"
29 #include "ctdb_logging.h"
30
31 #include "protocol/protocol.h"
32 #include "protocol/protocol_api.h"
33 #include "client/client_private.h"
34 #include "client/client.h"
35
36 int list_of_nodes(struct ctdb_node_map *nodemap,
37                   uint32_t flags_mask, uint32_t exclude_pnn,
38                   TALLOC_CTX *mem_ctx, uint32_t **pnn_list)
39 {
40         int num_nodes = 0;
41         uint32_t *list;
42         int i;
43
44         /* Allocate the list of same number of nodes */
45         list = talloc_array(mem_ctx, uint32_t, nodemap->num);
46         if (list == NULL) {
47                 return -1;
48         }
49
50         for (i=0; i<nodemap->num; i++) {
51                 if (nodemap->node[i].flags & flags_mask) {
52                         continue;
53                 }
54                 if (nodemap->node[i].pnn == exclude_pnn) {
55                         continue;
56                 }
57                 list[num_nodes] = nodemap->node[i].pnn;
58                 num_nodes++;
59         }
60
61         *pnn_list = list;
62         return num_nodes;
63 }
64
65 int list_of_active_nodes(struct ctdb_node_map *nodemap, uint32_t exclude_pnn,
66                          TALLOC_CTX *mem_ctx, uint32_t **pnn_list)
67 {
68         return list_of_nodes(nodemap, NODE_FLAGS_INACTIVE, exclude_pnn,
69                              mem_ctx, pnn_list);
70 }
71
72 int list_of_connected_nodes(struct ctdb_node_map *nodemap,
73                             uint32_t exclude_pnn,
74                             TALLOC_CTX *mem_ctx, uint32_t **pnn_list)
75 {
76         return list_of_nodes(nodemap, NODE_FLAGS_DISCONNECTED, exclude_pnn,
77                              mem_ctx, pnn_list);
78 }
79
80 int ctdb_ctrl_modflags(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
81                        struct ctdb_client_context *client,
82                        uint32_t destnode, struct timeval timeout,
83                        uint32_t set, uint32_t clear)
84 {
85         struct ctdb_node_map *nodemap;
86         struct ctdb_node_flag_change flag_change;
87         struct ctdb_req_control request;
88         uint32_t *pnn_list;
89         int ret, count;
90
91         ret = ctdb_ctrl_get_nodemap(mem_ctx, ev, client, destnode,
92                                     tevent_timeval_zero(), &nodemap);
93         if (ret != 0) {
94                 return ret;
95         }
96
97         flag_change.pnn = destnode;
98         flag_change.old_flags = nodemap->node[destnode].flags;
99         flag_change.new_flags = flag_change.old_flags | set;
100         flag_change.new_flags &= ~clear;
101
102         count = list_of_connected_nodes(nodemap, -1, mem_ctx, &pnn_list);
103         if (count == -1) {
104                 return ENOMEM;
105         }
106
107         ctdb_req_control_modify_flags(&request, &flag_change);
108         ret = ctdb_client_control_multi(mem_ctx, ev, client, pnn_list, count,
109                                         tevent_timeval_zero(), &request,
110                                         NULL, NULL);
111         return ret;
112 }
113
114 bool ctdb_server_id_equal(struct ctdb_server_id *sid1,
115                           struct ctdb_server_id *sid2)
116 {
117         if (sid1->pid != sid2->pid) {
118                 return false;
119         }
120         if (sid1->task_id != sid2->task_id) {
121                 return false;
122         }
123         if (sid1->vnn != sid2->vnn) {
124                 return false;
125         }
126         if (sid1->unique_id != sid2->unique_id) {
127                 return false;
128         }
129
130         return true;
131 }
132
133 int ctdb_server_id_exists(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
134                           struct ctdb_client_context *client,
135                           struct ctdb_server_id *sid, bool *exists)
136 {
137         uint8_t *result;
138         int ret;
139
140         ret = ctdb_ctrl_check_srvids(mem_ctx, ev, client, sid->vnn,
141                                      tevent_timeval_zero(),
142                                      &sid->unique_id, 1, &result);
143         if (ret != 0) {
144                 return ret;
145         }
146
147         if (result[0] == 1) {
148                 *exists = true;
149         } else {
150                 *exists = false;
151         }
152
153         return 0;
154 }