ctdb-tool: Add replacement ctdb tool using new client API
[Samba.git] / ctdb / client / client_util.c
blob1ba1e7d10e858ec1199271e8553949763d3ab719
1 /*
2 CTDB client code
4 Copyright (C) Amitay Isaacs 2015
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.
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.
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/>.
20 #include "replace.h"
21 #include "system/network.h"
22 #include "system/filesys.h"
24 #include <talloc.h>
25 #include <tevent.h>
26 #include <tdb.h>
28 #include "common/logging.h"
30 #include "lib/util/debug.h"
32 #include "protocol/protocol.h"
33 #include "protocol/protocol_api.h"
34 #include "client/client_private.h"
35 #include "client/client.h"
37 int list_of_nodes(struct ctdb_node_map *nodemap,
38 uint32_t flags_mask, uint32_t exclude_pnn,
39 TALLOC_CTX *mem_ctx, uint32_t **pnn_list)
41 int num_nodes = 0;
42 uint32_t *list;
43 int i;
45 /* Allocate the list of same number of nodes */
46 list = talloc_array(mem_ctx, uint32_t, nodemap->num);
47 if (list == NULL) {
48 return -1;
51 for (i=0; i<nodemap->num; i++) {
52 if (nodemap->node[i].flags & flags_mask) {
53 continue;
55 if (nodemap->node[i].pnn == exclude_pnn) {
56 continue;
58 list[num_nodes] = nodemap->node[i].pnn;
59 num_nodes++;
62 *pnn_list = list;
63 return num_nodes;
66 int list_of_active_nodes(struct ctdb_node_map *nodemap, uint32_t exclude_pnn,
67 TALLOC_CTX *mem_ctx, uint32_t **pnn_list)
69 return list_of_nodes(nodemap, NODE_FLAGS_INACTIVE, exclude_pnn,
70 mem_ctx, pnn_list);
73 int list_of_connected_nodes(struct ctdb_node_map *nodemap,
74 uint32_t exclude_pnn,
75 TALLOC_CTX *mem_ctx, uint32_t **pnn_list)
77 return list_of_nodes(nodemap, NODE_FLAGS_DISCONNECTED, exclude_pnn,
78 mem_ctx, pnn_list);
81 int ctdb_ctrl_modflags(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
82 struct ctdb_client_context *client,
83 uint32_t destnode, struct timeval timeout,
84 uint32_t set, uint32_t clear)
86 struct ctdb_node_map *nodemap;
87 struct ctdb_node_flag_change flag_change;
88 struct ctdb_req_control request;
89 uint32_t *pnn_list;
90 int ret, count;
92 ret = ctdb_ctrl_get_nodemap(mem_ctx, ev, client, destnode,
93 tevent_timeval_zero(), &nodemap);
94 if (ret != 0) {
95 return ret;
98 flag_change.pnn = destnode;
99 flag_change.old_flags = nodemap->node[destnode].flags;
100 flag_change.new_flags = flag_change.old_flags | set;
101 flag_change.new_flags &= ~clear;
103 count = list_of_connected_nodes(nodemap, -1, mem_ctx, &pnn_list);
104 if (count == -1) {
105 return ENOMEM;
108 ctdb_req_control_modify_flags(&request, &flag_change);
109 ret = ctdb_client_control_multi(mem_ctx, ev, client, pnn_list, count,
110 tevent_timeval_zero(), &request,
111 NULL, NULL);
112 return ret;
115 struct ctdb_server_id ctdb_client_get_server_id(
116 struct ctdb_client_context *client,
117 uint32_t task_id)
119 struct ctdb_server_id sid;
121 sid.pid = getpid();
122 sid.task_id = task_id;
123 sid.vnn = ctdb_client_pnn(client);
124 sid.unique_id = task_id;
125 sid.unique_id = (sid.unique_id << 32) | sid.pid;
127 return sid;
130 bool ctdb_server_id_equal(struct ctdb_server_id *sid1,
131 struct ctdb_server_id *sid2)
133 if (sid1->pid != sid2->pid) {
134 return false;
136 if (sid1->task_id != sid2->task_id) {
137 return false;
139 if (sid1->vnn != sid2->vnn) {
140 return false;
142 if (sid1->unique_id != sid2->unique_id) {
143 return false;
146 return true;
149 int ctdb_server_id_exists(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
150 struct ctdb_client_context *client,
151 struct ctdb_server_id *sid, bool *exists)
153 uint8_t *result;
154 int ret;
156 ret = ctdb_ctrl_check_srvids(mem_ctx, ev, client, sid->vnn,
157 tevent_timeval_zero(),
158 &sid->unique_id, 1, &result);
159 if (ret != 0) {
160 return ret;
163 if (result[0] == 1) {
164 *exists = true;
165 } else {
166 *exists = false;
169 return 0;