ldb: Allow GUID index mode to be tested on TDB
[samba.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 "common/logging.h"
29
30 #include "lib/util/debug.h"
31
32 #include "protocol/protocol.h"
33 #include "protocol/protocol_api.h"
34 #include "client/client_private.h"
35 #include "client/client.h"
36 #include "client/client_sync.h"
37
38 int list_of_nodes(struct ctdb_node_map *nodemap,
39                   uint32_t flags_mask, uint32_t exclude_pnn,
40                   TALLOC_CTX *mem_ctx, uint32_t **pnn_list)
41 {
42         int num_nodes = 0;
43         uint32_t *list;
44         int i;
45
46         /* Allocate the list of same number of nodes */
47         list = talloc_array(mem_ctx, uint32_t, nodemap->num);
48         if (list == NULL) {
49                 return -1;
50         }
51
52         for (i=0; i<nodemap->num; i++) {
53                 if (nodemap->node[i].flags & flags_mask) {
54                         continue;
55                 }
56                 if (nodemap->node[i].pnn == exclude_pnn) {
57                         continue;
58                 }
59                 list[num_nodes] = nodemap->node[i].pnn;
60                 num_nodes++;
61         }
62
63         *pnn_list = list;
64         return num_nodes;
65 }
66
67 int list_of_active_nodes(struct ctdb_node_map *nodemap, uint32_t exclude_pnn,
68                          TALLOC_CTX *mem_ctx, uint32_t **pnn_list)
69 {
70         return list_of_nodes(nodemap, NODE_FLAGS_INACTIVE, exclude_pnn,
71                              mem_ctx, pnn_list);
72 }
73
74 int list_of_connected_nodes(struct ctdb_node_map *nodemap,
75                             uint32_t exclude_pnn,
76                             TALLOC_CTX *mem_ctx, uint32_t **pnn_list)
77 {
78         return list_of_nodes(nodemap, NODE_FLAGS_DISCONNECTED, exclude_pnn,
79                              mem_ctx, pnn_list);
80 }
81
82 struct ctdb_server_id ctdb_client_get_server_id(
83                                 struct ctdb_client_context *client,
84                                 uint32_t task_id)
85 {
86         struct ctdb_server_id sid;
87
88         sid.pid = getpid();
89         sid.task_id = task_id;
90         sid.vnn = ctdb_client_pnn(client);
91         sid.unique_id = task_id;
92         sid.unique_id = (sid.unique_id << 32) | sid.pid;
93
94         return sid;
95 }
96
97 bool ctdb_server_id_equal(struct ctdb_server_id *sid1,
98                           struct ctdb_server_id *sid2)
99 {
100         if (sid1->pid != sid2->pid) {
101                 return false;
102         }
103         if (sid1->task_id != sid2->task_id) {
104                 return false;
105         }
106         if (sid1->vnn != sid2->vnn) {
107                 return false;
108         }
109         if (sid1->unique_id != sid2->unique_id) {
110                 return false;
111         }
112
113         return true;
114 }
115
116 int ctdb_server_id_exists(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
117                           struct ctdb_client_context *client,
118                           struct ctdb_server_id *sid, bool *exists)
119 {
120         int result;
121         int ret;
122
123         ret = ctdb_ctrl_process_exists(mem_ctx, ev, client, sid->vnn,
124                                        tevent_timeval_zero(),
125                                        sid->pid, &result);
126         if (ret != 0) {
127                 return ret;
128         }
129
130         if (result == 1) {
131                 *exists = true;
132         } else {
133                 *exists = false;
134         }
135
136         return 0;
137 }