lib: Use ctdbd_control_unix in ctdbd_register_ips
[Samba.git] / ctdb / server / ctdb_serverids.c
blobd264a1f9a359fd7de3a761a81aac2956df4c3283
1 /*
2 ctdb_control protocol code to manage server ids
4 Copyright (C) Ronnie Sahlberg 2007
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/>.
19 #include "includes.h"
20 #include "../include/ctdb_private.h"
21 #include "../common/rb_tree.h"
22 #include "common/reqid.h"
25 #define SERVER_ID_KEY_SIZE 3
26 static uint32_t *get_server_id_key(struct ctdb_server_id *server_id)
28 static uint32_t key[SERVER_ID_KEY_SIZE];
30 key[0] = server_id->type;
31 key[1] = server_id->pnn;
32 key[2] = server_id->server_id;
34 return &key[0];
37 /* add a server_id to the tree.
38 if we had already 'data' in the tree then this is a duplicate and we can
39 just talloc_free the structure in parm and leave data in the tree.
40 othervise if this is a new node we return parm and that is inserted
41 into the tree.
43 static void *add_server_id_callback(void *parm, void *data)
45 if (data) {
46 talloc_free(parm);
47 return data;
49 return parm;
53 register a server id
54 a serverid that is registered with ctdb will be automatically unregistered
55 once the client domain socket dissappears.
57 int32_t ctdb_control_register_server_id(struct ctdb_context *ctdb,
58 uint32_t client_id,
59 TDB_DATA indata)
61 struct ctdb_server_id *server_id;
62 struct ctdb_client *client = reqid_find(ctdb->idr, client_id, struct ctdb_client);
65 if (client == NULL) {
66 DEBUG(DEBUG_ERR,(__location__ " Could not find client parent structure. You can not send this control to a remote node\n"));
67 return 1;
70 /* hang the server_id structure off client before storing it in the
71 tree so that is will be automatically destroyed when client
72 is destroyed.
73 when the structure is free'd it will be automatically
74 removed from the tree
76 server_id = talloc_zero(client, struct ctdb_server_id);
77 CTDB_NO_MEMORY(ctdb, server_id);
78 memcpy(server_id, indata.dptr, sizeof(struct ctdb_server_id));
80 trbt_insertarray32_callback(ctdb->server_ids, SERVER_ID_KEY_SIZE,
81 get_server_id_key(server_id),
82 add_server_id_callback, server_id);
84 return 0;
89 check whether a server id exists
91 int32_t ctdb_control_check_server_id(struct ctdb_context *ctdb,
92 TDB_DATA indata)
94 struct ctdb_server_id *server_id = (struct ctdb_server_id *)indata.dptr;
96 return trbt_lookuparray32(ctdb->server_ids,
97 SERVER_ID_KEY_SIZE,
98 get_server_id_key(server_id)) == NULL? 0 : 1;
102 unregisters a server id
104 int32_t ctdb_control_unregister_server_id(struct ctdb_context *ctdb,
105 TDB_DATA indata)
107 struct ctdb_server_id *server_id = (struct ctdb_server_id *)indata.dptr;
109 talloc_free(trbt_lookuparray32(ctdb->server_ids,
110 SERVER_ID_KEY_SIZE,
111 get_server_id_key(server_id)));
112 return 0;
118 struct count_server_ids {
119 int count;
120 struct ctdb_server_id_list *list;
123 static int server_id_count(void *param, void *data)
125 struct count_server_ids *svid = talloc_get_type(param,
126 struct count_server_ids);
128 if (svid == NULL) {
129 DEBUG(DEBUG_ERR, (__location__ " Got null pointer for svid\n"));
130 return -1;
133 svid->count++;
134 return 0;
137 static int server_id_store(void *param, void *data)
139 struct count_server_ids *svid = talloc_get_type(param,
140 struct count_server_ids);
141 struct ctdb_server_id *server_id = talloc_get_type(data,
142 struct ctdb_server_id);
144 if (svid == NULL) {
145 DEBUG(DEBUG_ERR, (__location__ " Got null pointer for svid\n"));
146 return -1;
149 if (svid->count >= svid->list->num) {
150 DEBUG(DEBUG_ERR, (__location__ " size of server id tree changed during traverse\n"));
151 return -1;
154 memcpy(&svid->list->server_ids[svid->count], server_id, sizeof(struct ctdb_server_id));
155 svid->count++;
156 return 0;
160 returns a list of all registered server ids for a node
162 int32_t ctdb_control_get_server_id_list(struct ctdb_context *ctdb, TDB_DATA *outdata)
164 struct count_server_ids *svid;
167 svid = talloc_zero(outdata, struct count_server_ids);
168 CTDB_NO_MEMORY(ctdb, svid);
171 /* first we must count how many entries we have */
172 trbt_traversearray32(ctdb->server_ids, SERVER_ID_KEY_SIZE,
173 server_id_count, svid);
176 outdata->dsize = offsetof(struct ctdb_server_id_list,
177 server_ids)
178 + sizeof(struct ctdb_server_id) * svid->count;
179 outdata->dptr = talloc_size(outdata, outdata->dsize);
180 CTDB_NO_MEMORY(ctdb, outdata->dptr);
183 /* now fill the structure in */
184 svid->list = (struct ctdb_server_id_list *)(outdata->dptr);
185 svid->list->num = svid->count;
186 svid->count=0;
187 trbt_traversearray32(ctdb->server_ids, SERVER_ID_KEY_SIZE,
188 server_id_store, svid);
191 return 0;