ctdb-build: Replace os.system with samba_utils.RUN_COMMAND
[Samba.git] / source3 / lib / serverid.c
blobf98209873bd24cb935c63662c729054bf02e7c46
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;
46 if (db != NULL) {
47 return db;
49 db = db_open(NULL, lock_path("serverid.tdb"), 0,
50 TDB_DEFAULT|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
51 O_RDWR|O_CREAT, 0644, DBWRAP_LOCK_ORDER_2,
52 DBWRAP_FLAG_NONE);
53 return db;
56 bool serverid_parent_init(TALLOC_CTX *mem_ctx)
58 struct db_context *db;
60 db = serverid_db();
61 if (db == NULL) {
62 DEBUG(1, ("could not open serverid.tdb: %s\n",
63 strerror(errno)));
64 return false;
67 return true;
70 static void serverid_fill_key(const struct server_id *id,
71 struct serverid_key *key)
73 ZERO_STRUCTP(key);
74 key->pid = id->pid;
75 key->task_id = id->task_id;
76 key->vnn = id->vnn;
79 bool serverid_register(const struct server_id id, uint32_t msg_flags)
81 struct db_context *db;
82 struct serverid_key key;
83 struct serverid_data data;
84 struct db_record *rec;
85 TDB_DATA tdbkey, tdbdata;
86 NTSTATUS status;
87 bool ret = false;
89 db = serverid_db();
90 if (db == NULL) {
91 return false;
94 serverid_fill_key(&id, &key);
95 tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));
97 rec = dbwrap_fetch_locked(db, talloc_tos(), tdbkey);
98 if (rec == NULL) {
99 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
100 return false;
103 ZERO_STRUCT(data);
104 data.unique_id = id.unique_id;
105 data.msg_flags = msg_flags;
107 tdbdata = make_tdb_data((uint8_t *)&data, sizeof(data));
108 status = dbwrap_record_store(rec, tdbdata, 0);
109 if (!NT_STATUS_IS_OK(status)) {
110 DEBUG(1, ("Storing serverid.tdb record failed: %s\n",
111 nt_errstr(status)));
112 goto done;
115 if (lp_clustering() &&
116 ctdb_serverids_exist_supported(messaging_ctdbd_connection()))
118 register_with_ctdbd(messaging_ctdbd_connection(), id.unique_id);
121 ret = true;
122 done:
123 TALLOC_FREE(rec);
124 return ret;
127 bool serverid_register_msg_flags(const struct server_id id, bool do_reg,
128 uint32_t msg_flags)
130 struct db_context *db;
131 struct serverid_key key;
132 struct serverid_data *data;
133 struct db_record *rec;
134 TDB_DATA tdbkey;
135 TDB_DATA value;
136 NTSTATUS status;
137 bool ret = false;
139 db = serverid_db();
140 if (db == NULL) {
141 return false;
144 serverid_fill_key(&id, &key);
145 tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));
147 rec = dbwrap_fetch_locked(db, talloc_tos(), tdbkey);
148 if (rec == NULL) {
149 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
150 return false;
153 value = dbwrap_record_get_value(rec);
155 if (value.dsize != sizeof(struct serverid_data)) {
156 DEBUG(1, ("serverid record has unexpected size %d "
157 "(wanted %d)\n", (int)value.dsize,
158 (int)sizeof(struct serverid_data)));
159 goto done;
162 data = (struct serverid_data *)value.dptr;
164 if (do_reg) {
165 data->msg_flags |= msg_flags;
166 } else {
167 data->msg_flags &= ~msg_flags;
170 status = dbwrap_record_store(rec, value, 0);
171 if (!NT_STATUS_IS_OK(status)) {
172 DEBUG(1, ("Storing serverid.tdb record failed: %s\n",
173 nt_errstr(status)));
174 goto done;
176 ret = true;
177 done:
178 TALLOC_FREE(rec);
179 return ret;
182 bool serverid_deregister(struct server_id id)
184 struct db_context *db;
185 struct serverid_key key;
186 struct db_record *rec;
187 TDB_DATA tdbkey;
188 NTSTATUS status;
189 bool ret = false;
191 db = serverid_db();
192 if (db == NULL) {
193 return false;
196 serverid_fill_key(&id, &key);
197 tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));
199 rec = dbwrap_fetch_locked(db, talloc_tos(), tdbkey);
200 if (rec == NULL) {
201 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
202 return false;
205 status = dbwrap_record_delete(rec);
206 if (!NT_STATUS_IS_OK(status)) {
207 DEBUG(1, ("Deleting serverid.tdb record failed: %s\n",
208 nt_errstr(status)));
209 goto done;
211 ret = true;
212 done:
213 TALLOC_FREE(rec);
214 return ret;
217 struct serverid_exists_state {
218 const struct server_id *id;
219 bool exists;
222 static void server_exists_parse(TDB_DATA key, TDB_DATA data, void *priv)
224 struct serverid_exists_state *state =
225 (struct serverid_exists_state *)priv;
227 if (data.dsize != sizeof(struct serverid_data)) {
228 state->exists = false;
229 return;
233 * Use memcmp, not direct compare. data.dptr might not be
234 * aligned.
236 state->exists = (memcmp(&state->id->unique_id, data.dptr,
237 sizeof(state->id->unique_id)) == 0);
240 bool serverid_exists(const struct server_id *id)
242 bool result = false;
243 bool ok = false;
245 ok = serverids_exist(id, 1, &result);
246 if (!ok) {
247 return false;
250 return result;
253 bool serverids_exist(const struct server_id *ids, int num_ids, bool *results)
255 int *todo_idx = NULL;
256 struct server_id *todo_ids = NULL;
257 bool *todo_results = NULL;
258 int todo_num = 0;
259 int *remote_idx = NULL;
260 int remote_num = 0;
261 int *verify_idx = NULL;
262 int verify_num = 0;
263 int t, idx;
264 bool result = false;
265 struct db_context *db;
267 db = serverid_db();
268 if (db == NULL) {
269 return false;
272 todo_idx = talloc_array(talloc_tos(), int, num_ids);
273 if (todo_idx == NULL) {
274 goto fail;
276 todo_ids = talloc_array(talloc_tos(), struct server_id, num_ids);
277 if (todo_ids == NULL) {
278 goto fail;
280 todo_results = talloc_array(talloc_tos(), bool, num_ids);
281 if (todo_results == NULL) {
282 goto fail;
285 remote_idx = talloc_array(talloc_tos(), int, num_ids);
286 if (remote_idx == NULL) {
287 goto fail;
289 verify_idx = talloc_array(talloc_tos(), int, num_ids);
290 if (verify_idx == NULL) {
291 goto fail;
294 for (idx=0; idx<num_ids; idx++) {
295 results[idx] = false;
297 if (server_id_is_disconnected(&ids[idx])) {
298 continue;
301 if (procid_is_me(&ids[idx])) {
302 results[idx] = true;
303 continue;
306 if (procid_is_local(&ids[idx])) {
307 bool exists = process_exists_by_pid(ids[idx].pid);
309 if (!exists) {
310 continue;
313 if (ids[idx].unique_id == SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
314 results[idx] = true;
315 continue;
318 verify_idx[verify_num] = idx;
319 verify_num += 1;
320 continue;
323 if (!lp_clustering()) {
324 continue;
327 remote_idx[remote_num] = idx;
328 remote_num += 1;
331 if (remote_num != 0 &&
332 ctdb_serverids_exist_supported(messaging_ctdbd_connection()))
334 int old_remote_num = remote_num;
336 remote_num = 0;
337 todo_num = 0;
339 for (t=0; t<old_remote_num; t++) {
340 idx = remote_idx[t];
342 if (ids[idx].unique_id == SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
343 remote_idx[remote_num] = idx;
344 remote_num += 1;
345 continue;
348 todo_idx[todo_num] = idx;
349 todo_ids[todo_num] = ids[idx];
350 todo_results[todo_num] = false;
351 todo_num += 1;
355 * Note: this only uses CTDB_CONTROL_CHECK_SRVIDS
356 * to verify that the server_id still exists,
357 * which means only the server_id.unique_id and
358 * server_id.vnn are verified, while server_id.pid
359 * is not verified at all.
361 * TODO: do we want to verify server_id.pid somehow?
363 if (!ctdb_serverids_exist(messaging_ctdbd_connection(),
364 todo_ids, todo_num, todo_results))
366 goto fail;
369 for (t=0; t<todo_num; t++) {
370 idx = todo_idx[t];
372 results[idx] = todo_results[t];
376 if (remote_num != 0) {
377 todo_num = 0;
379 for (t=0; t<remote_num; t++) {
380 idx = remote_idx[t];
381 todo_idx[todo_num] = idx;
382 todo_ids[todo_num] = ids[idx];
383 todo_results[todo_num] = false;
384 todo_num += 1;
387 if (!ctdb_processes_exist(messaging_ctdbd_connection(),
388 todo_ids, todo_num,
389 todo_results)) {
390 goto fail;
393 for (t=0; t<todo_num; t++) {
394 idx = todo_idx[t];
396 if (!todo_results[t]) {
397 continue;
400 if (ids[idx].unique_id == SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
401 results[idx] = true;
402 continue;
405 verify_idx[verify_num] = idx;
406 verify_num += 1;
410 for (t=0; t<verify_num; t++) {
411 struct serverid_exists_state state;
412 struct serverid_key key;
413 TDB_DATA tdbkey;
414 NTSTATUS status;
416 idx = verify_idx[t];
418 serverid_fill_key(&ids[idx], &key);
419 tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));
421 state.id = &ids[idx];
422 state.exists = false;
423 status = dbwrap_parse_record(db, tdbkey, server_exists_parse, &state);
424 if (!NT_STATUS_IS_OK(status)) {
425 results[idx] = false;
426 continue;
428 results[idx] = state.exists;
431 result = true;
432 fail:
433 TALLOC_FREE(verify_idx);
434 TALLOC_FREE(remote_idx);
435 TALLOC_FREE(todo_results);
436 TALLOC_FREE(todo_ids);
437 TALLOC_FREE(todo_idx);
438 return result;
441 static bool serverid_rec_parse(const struct db_record *rec,
442 struct server_id *id, uint32_t *msg_flags)
444 struct serverid_key key;
445 struct serverid_data data;
446 TDB_DATA tdbkey;
447 TDB_DATA tdbdata;
449 tdbkey = dbwrap_record_get_key(rec);
450 tdbdata = dbwrap_record_get_value(rec);
452 if (tdbkey.dsize != sizeof(key)) {
453 DEBUG(1, ("Found invalid key length %d in serverid.tdb\n",
454 (int)tdbkey.dsize));
455 return false;
457 if (tdbdata.dsize != sizeof(data)) {
458 DEBUG(1, ("Found invalid value length %d in serverid.tdb\n",
459 (int)tdbdata.dsize));
460 return false;
463 memcpy(&key, tdbkey.dptr, sizeof(key));
464 memcpy(&data, tdbdata.dptr, sizeof(data));
466 id->pid = key.pid;
467 id->task_id = key.task_id;
468 id->vnn = key.vnn;
469 id->unique_id = data.unique_id;
470 *msg_flags = data.msg_flags;
471 return true;
474 struct serverid_traverse_read_state {
475 int (*fn)(const struct server_id *id, uint32_t msg_flags,
476 void *private_data);
477 void *private_data;
480 static int serverid_traverse_read_fn(struct db_record *rec, void *private_data)
482 struct serverid_traverse_read_state *state =
483 (struct serverid_traverse_read_state *)private_data;
484 struct server_id id;
485 uint32_t msg_flags;
487 if (!serverid_rec_parse(rec, &id, &msg_flags)) {
488 return 0;
490 return state->fn(&id, msg_flags,state->private_data);
493 bool serverid_traverse_read(int (*fn)(const struct server_id *id,
494 uint32_t msg_flags, void *private_data),
495 void *private_data)
497 struct db_context *db;
498 struct serverid_traverse_read_state state;
499 NTSTATUS status;
501 db = serverid_db();
502 if (db == NULL) {
503 return false;
505 state.fn = fn;
506 state.private_data = private_data;
508 status = dbwrap_traverse_read(db, serverid_traverse_read_fn, &state,
509 NULL);
510 return NT_STATUS_IS_OK(status);
513 struct serverid_traverse_state {
514 int (*fn)(struct db_record *rec, const struct server_id *id,
515 uint32_t msg_flags, void *private_data);
516 void *private_data;
519 static int serverid_traverse_fn(struct db_record *rec, void *private_data)
521 struct serverid_traverse_state *state =
522 (struct serverid_traverse_state *)private_data;
523 struct server_id id;
524 uint32_t msg_flags;
526 if (!serverid_rec_parse(rec, &id, &msg_flags)) {
527 return 0;
529 return state->fn(rec, &id, msg_flags, state->private_data);
532 bool serverid_traverse(int (*fn)(struct db_record *rec,
533 const struct server_id *id,
534 uint32_t msg_flags, void *private_data),
535 void *private_data)
537 struct db_context *db;
538 struct serverid_traverse_state state;
539 NTSTATUS status;
541 db = serverid_db();
542 if (db == NULL) {
543 return false;
545 state.fn = fn;
546 state.private_data = private_data;
548 status = dbwrap_traverse(db, serverid_traverse_fn, &state, NULL);
549 return NT_STATUS_IS_OK(status);
552 uint64_t serverid_get_random_unique_id(void)
554 uint64_t unique_id = SERVERID_UNIQUE_ID_NOT_TO_VERIFY;
556 while (unique_id == SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
557 generate_random_buffer((uint8_t *)&unique_id,
558 sizeof(unique_id));
561 return unique_id;