buildtools: Expose the Python 3 ABI tag
[Samba.git] / source3 / lib / serverid.c
blobf5e0937224e668d636ddfd969061a8c6f01798ef
1 /*
2 Unix SMB/CIFS implementation.
3 Implementation of a reliable server_exists()
4 Copyright (C) Volker Lendecke 2010
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 "includes.h"
21 #include "system/filesys.h"
22 #include "serverid.h"
23 #include "util_tdb.h"
24 #include "dbwrap/dbwrap.h"
25 #include "dbwrap/dbwrap_open.h"
26 #include "lib/tdb_wrap/tdb_wrap.h"
27 #include "lib/param/param.h"
28 #include "ctdbd_conn.h"
29 #include "messages.h"
31 struct serverid_key {
32 pid_t pid;
33 uint32_t task_id;
34 uint32_t vnn;
37 struct serverid_data {
38 uint64_t unique_id;
39 uint32_t msg_flags;
42 static struct db_context *serverid_db(void)
44 static struct db_context *db;
45 char *db_path;
47 if (db != NULL) {
48 return db;
51 db_path = lock_path("serverid.tdb");
52 if (db_path == NULL) {
53 return NULL;
56 db = db_open(NULL, db_path, 0,
57 TDB_DEFAULT|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
58 O_RDWR|O_CREAT, 0644, DBWRAP_LOCK_ORDER_2,
59 DBWRAP_FLAG_NONE);
60 TALLOC_FREE(db_path);
61 return db;
64 bool serverid_parent_init(TALLOC_CTX *mem_ctx)
66 struct db_context *db;
68 db = serverid_db();
69 if (db == NULL) {
70 DEBUG(1, ("could not open serverid.tdb: %s\n",
71 strerror(errno)));
72 return false;
75 return true;
78 static void serverid_fill_key(const struct server_id *id,
79 struct serverid_key *key)
81 ZERO_STRUCTP(key);
82 key->pid = id->pid;
83 key->task_id = id->task_id;
84 key->vnn = id->vnn;
87 bool serverid_register(const struct server_id id, uint32_t msg_flags)
89 struct db_context *db;
90 struct serverid_key key;
91 struct serverid_data data;
92 struct db_record *rec;
93 TDB_DATA tdbkey, tdbdata;
94 NTSTATUS status;
95 bool ret = false;
97 db = serverid_db();
98 if (db == NULL) {
99 return false;
102 serverid_fill_key(&id, &key);
103 tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));
105 rec = dbwrap_fetch_locked(db, talloc_tos(), tdbkey);
106 if (rec == NULL) {
107 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
108 return false;
111 ZERO_STRUCT(data);
112 data.unique_id = id.unique_id;
113 data.msg_flags = msg_flags;
115 tdbdata = make_tdb_data((uint8_t *)&data, sizeof(data));
116 status = dbwrap_record_store(rec, tdbdata, 0);
117 if (!NT_STATUS_IS_OK(status)) {
118 DEBUG(1, ("Storing serverid.tdb record failed: %s\n",
119 nt_errstr(status)));
120 goto done;
123 if (lp_clustering() &&
124 ctdb_serverids_exist_supported(messaging_ctdbd_connection()))
126 register_with_ctdbd(messaging_ctdbd_connection(), id.unique_id);
129 ret = true;
130 done:
131 TALLOC_FREE(rec);
132 return ret;
135 bool serverid_deregister(struct server_id id)
137 struct db_context *db;
138 struct serverid_key key;
139 struct db_record *rec;
140 TDB_DATA tdbkey;
141 NTSTATUS status;
142 bool ret = false;
144 db = serverid_db();
145 if (db == NULL) {
146 return false;
149 serverid_fill_key(&id, &key);
150 tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));
152 rec = dbwrap_fetch_locked(db, talloc_tos(), tdbkey);
153 if (rec == NULL) {
154 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
155 return false;
158 status = dbwrap_record_delete(rec);
159 if (!NT_STATUS_IS_OK(status)) {
160 DEBUG(1, ("Deleting serverid.tdb record failed: %s\n",
161 nt_errstr(status)));
162 goto done;
164 ret = true;
165 done:
166 TALLOC_FREE(rec);
167 return ret;
170 struct serverid_exists_state {
171 const struct server_id *id;
172 bool exists;
175 static void server_exists_parse(TDB_DATA key, TDB_DATA data, void *priv)
177 struct serverid_exists_state *state =
178 (struct serverid_exists_state *)priv;
180 if (data.dsize != sizeof(struct serverid_data)) {
181 state->exists = false;
182 return;
186 * Use memcmp, not direct compare. data.dptr might not be
187 * aligned.
189 state->exists = (memcmp(&state->id->unique_id, data.dptr,
190 sizeof(state->id->unique_id)) == 0);
193 bool serverid_exists(const struct server_id *id)
195 bool result = false;
196 bool ok = false;
198 ok = serverids_exist(id, 1, &result);
199 if (!ok) {
200 return false;
203 return result;
206 bool serverids_exist(const struct server_id *ids, int num_ids, bool *results)
208 int *todo_idx = NULL;
209 struct server_id *todo_ids = NULL;
210 bool *todo_results = NULL;
211 int todo_num = 0;
212 int *remote_idx = NULL;
213 int remote_num = 0;
214 int *verify_idx = NULL;
215 int verify_num = 0;
216 int t, idx;
217 bool result = false;
218 struct db_context *db;
220 db = serverid_db();
221 if (db == NULL) {
222 return false;
225 todo_idx = talloc_array(talloc_tos(), int, num_ids);
226 if (todo_idx == NULL) {
227 goto fail;
229 todo_ids = talloc_array(talloc_tos(), struct server_id, num_ids);
230 if (todo_ids == NULL) {
231 goto fail;
233 todo_results = talloc_array(talloc_tos(), bool, num_ids);
234 if (todo_results == NULL) {
235 goto fail;
238 remote_idx = talloc_array(talloc_tos(), int, num_ids);
239 if (remote_idx == NULL) {
240 goto fail;
242 verify_idx = talloc_array(talloc_tos(), int, num_ids);
243 if (verify_idx == NULL) {
244 goto fail;
247 for (idx=0; idx<num_ids; idx++) {
248 results[idx] = false;
250 if (server_id_is_disconnected(&ids[idx])) {
251 continue;
254 if (procid_is_me(&ids[idx])) {
255 results[idx] = true;
256 continue;
259 if (procid_is_local(&ids[idx])) {
260 bool exists = process_exists_by_pid(ids[idx].pid);
262 if (!exists) {
263 continue;
266 if (ids[idx].unique_id == SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
267 results[idx] = true;
268 continue;
271 verify_idx[verify_num] = idx;
272 verify_num += 1;
273 continue;
276 if (!lp_clustering()) {
277 continue;
280 remote_idx[remote_num] = idx;
281 remote_num += 1;
284 if (remote_num != 0 &&
285 ctdb_serverids_exist_supported(messaging_ctdbd_connection()))
287 int old_remote_num = remote_num;
289 remote_num = 0;
290 todo_num = 0;
292 for (t=0; t<old_remote_num; t++) {
293 idx = remote_idx[t];
295 if (ids[idx].unique_id == SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
296 remote_idx[remote_num] = idx;
297 remote_num += 1;
298 continue;
301 todo_idx[todo_num] = idx;
302 todo_ids[todo_num] = ids[idx];
303 todo_results[todo_num] = false;
304 todo_num += 1;
308 * Note: this only uses CTDB_CONTROL_CHECK_SRVIDS
309 * to verify that the server_id still exists,
310 * which means only the server_id.unique_id and
311 * server_id.vnn are verified, while server_id.pid
312 * is not verified at all.
314 * TODO: do we want to verify server_id.pid somehow?
316 if (!ctdb_serverids_exist(messaging_ctdbd_connection(),
317 todo_ids, todo_num, todo_results))
319 goto fail;
322 for (t=0; t<todo_num; t++) {
323 idx = todo_idx[t];
325 results[idx] = todo_results[t];
329 if (remote_num != 0) {
330 todo_num = 0;
332 for (t=0; t<remote_num; t++) {
333 idx = remote_idx[t];
334 todo_idx[todo_num] = idx;
335 todo_ids[todo_num] = ids[idx];
336 todo_results[todo_num] = false;
337 todo_num += 1;
340 if (!ctdb_processes_exist(messaging_ctdbd_connection(),
341 todo_ids, todo_num,
342 todo_results)) {
343 goto fail;
346 for (t=0; t<todo_num; t++) {
347 idx = todo_idx[t];
349 if (!todo_results[t]) {
350 continue;
353 if (ids[idx].unique_id == SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
354 results[idx] = true;
355 continue;
358 verify_idx[verify_num] = idx;
359 verify_num += 1;
363 for (t=0; t<verify_num; t++) {
364 struct serverid_exists_state state;
365 struct serverid_key key;
366 TDB_DATA tdbkey;
367 NTSTATUS status;
369 idx = verify_idx[t];
371 serverid_fill_key(&ids[idx], &key);
372 tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));
374 state.id = &ids[idx];
375 state.exists = false;
376 status = dbwrap_parse_record(db, tdbkey, server_exists_parse, &state);
377 if (!NT_STATUS_IS_OK(status)) {
378 results[idx] = false;
379 continue;
381 results[idx] = state.exists;
384 result = true;
385 fail:
386 TALLOC_FREE(verify_idx);
387 TALLOC_FREE(remote_idx);
388 TALLOC_FREE(todo_results);
389 TALLOC_FREE(todo_ids);
390 TALLOC_FREE(todo_idx);
391 return result;
394 static bool serverid_rec_parse(const struct db_record *rec,
395 struct server_id *id, uint32_t *msg_flags)
397 struct serverid_key key;
398 struct serverid_data data;
399 TDB_DATA tdbkey;
400 TDB_DATA tdbdata;
402 tdbkey = dbwrap_record_get_key(rec);
403 tdbdata = dbwrap_record_get_value(rec);
405 if (tdbkey.dsize != sizeof(key)) {
406 DEBUG(1, ("Found invalid key length %d in serverid.tdb\n",
407 (int)tdbkey.dsize));
408 return false;
410 if (tdbdata.dsize != sizeof(data)) {
411 DEBUG(1, ("Found invalid value length %d in serverid.tdb\n",
412 (int)tdbdata.dsize));
413 return false;
416 memcpy(&key, tdbkey.dptr, sizeof(key));
417 memcpy(&data, tdbdata.dptr, sizeof(data));
419 id->pid = key.pid;
420 id->task_id = key.task_id;
421 id->vnn = key.vnn;
422 id->unique_id = data.unique_id;
423 *msg_flags = data.msg_flags;
424 return true;
427 struct serverid_traverse_read_state {
428 int (*fn)(const struct server_id *id, uint32_t msg_flags,
429 void *private_data);
430 void *private_data;
433 static int serverid_traverse_read_fn(struct db_record *rec, void *private_data)
435 struct serverid_traverse_read_state *state =
436 (struct serverid_traverse_read_state *)private_data;
437 struct server_id id;
438 uint32_t msg_flags;
440 if (!serverid_rec_parse(rec, &id, &msg_flags)) {
441 return 0;
443 return state->fn(&id, msg_flags,state->private_data);
446 bool serverid_traverse_read(int (*fn)(const struct server_id *id,
447 uint32_t msg_flags, void *private_data),
448 void *private_data)
450 struct db_context *db;
451 struct serverid_traverse_read_state state;
452 NTSTATUS status;
454 db = serverid_db();
455 if (db == NULL) {
456 return false;
458 state.fn = fn;
459 state.private_data = private_data;
461 status = dbwrap_traverse_read(db, serverid_traverse_read_fn, &state,
462 NULL);
463 return NT_STATUS_IS_OK(status);
466 struct serverid_traverse_state {
467 int (*fn)(struct db_record *rec, const struct server_id *id,
468 uint32_t msg_flags, void *private_data);
469 void *private_data;
472 static int serverid_traverse_fn(struct db_record *rec, void *private_data)
474 struct serverid_traverse_state *state =
475 (struct serverid_traverse_state *)private_data;
476 struct server_id id;
477 uint32_t msg_flags;
479 if (!serverid_rec_parse(rec, &id, &msg_flags)) {
480 return 0;
482 return state->fn(rec, &id, msg_flags, state->private_data);
485 bool serverid_traverse(int (*fn)(struct db_record *rec,
486 const struct server_id *id,
487 uint32_t msg_flags, void *private_data),
488 void *private_data)
490 struct db_context *db;
491 struct serverid_traverse_state state;
492 NTSTATUS status;
494 db = serverid_db();
495 if (db == NULL) {
496 return false;
498 state.fn = fn;
499 state.private_data = private_data;
501 status = dbwrap_traverse(db, serverid_traverse_fn, &state, NULL);
502 return NT_STATUS_IS_OK(status);
505 uint64_t serverid_get_random_unique_id(void)
507 uint64_t unique_id = SERVERID_UNIQUE_ID_NOT_TO_VERIFY;
509 while (unique_id == SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
510 generate_random_buffer((uint8_t *)&unique_id,
511 sizeof(unique_id));
514 return unique_id;