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/>.
21 #include "system/filesys.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"
37 struct serverid_data {
42 bool serverid_parent_init(TALLOC_CTX *mem_ctx)
45 struct loadparm_context *lp_ctx;
47 lp_ctx = loadparm_init_s3(mem_ctx, loadparm_s3_helpers());
49 DEBUG(0, ("loadparm_init_s3 failed\n"));
54 * Open the tdb in the parent process (smbd) so that our
55 * CLEAR_IF_FIRST optimization in tdb_reopen_all can properly
59 db = tdb_wrap_open(mem_ctx, lock_path("serverid.tdb"),
60 0, TDB_DEFAULT|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH, O_RDWR|O_CREAT,
62 talloc_unlink(mem_ctx, lp_ctx);
64 DEBUG(1, ("could not open serverid.tdb: %s\n",
71 static struct db_context *serverid_db(void)
73 static struct db_context *db;
78 db = db_open(NULL, lock_path("serverid.tdb"), 0,
79 TDB_DEFAULT|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
80 O_RDWR|O_CREAT, 0644, DBWRAP_LOCK_ORDER_2);
84 static void serverid_fill_key(const struct server_id *id,
85 struct serverid_key *key)
89 key->task_id = id->task_id;
93 bool serverid_register(const struct server_id id, uint32_t msg_flags)
95 struct db_context *db;
96 struct serverid_key key;
97 struct serverid_data data;
98 struct db_record *rec;
99 TDB_DATA tdbkey, tdbdata;
108 serverid_fill_key(&id, &key);
109 tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));
111 rec = dbwrap_fetch_locked(db, talloc_tos(), tdbkey);
113 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
118 data.unique_id = id.unique_id;
119 data.msg_flags = msg_flags;
121 tdbdata = make_tdb_data((uint8_t *)&data, sizeof(data));
122 status = dbwrap_record_store(rec, tdbdata, 0);
123 if (!NT_STATUS_IS_OK(status)) {
124 DEBUG(1, ("Storing serverid.tdb record failed: %s\n",
128 #ifdef HAVE_CTDB_CONTROL_CHECK_SRVIDS_DECL
129 if (lp_clustering()) {
130 register_with_ctdbd(messaging_ctdbd_connection(), id.unique_id);
139 bool serverid_register_msg_flags(const struct server_id id, bool do_reg,
142 struct db_context *db;
143 struct serverid_key key;
144 struct serverid_data *data;
145 struct db_record *rec;
156 serverid_fill_key(&id, &key);
157 tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));
159 rec = dbwrap_fetch_locked(db, talloc_tos(), tdbkey);
161 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
165 value = dbwrap_record_get_value(rec);
167 if (value.dsize != sizeof(struct serverid_data)) {
168 DEBUG(1, ("serverid record has unexpected size %d "
169 "(wanted %d)\n", (int)value.dsize,
170 (int)sizeof(struct serverid_data)));
174 data = (struct serverid_data *)value.dptr;
177 data->msg_flags |= msg_flags;
179 data->msg_flags &= ~msg_flags;
182 status = dbwrap_record_store(rec, value, 0);
183 if (!NT_STATUS_IS_OK(status)) {
184 DEBUG(1, ("Storing serverid.tdb record failed: %s\n",
194 bool serverid_deregister(struct server_id id)
196 struct db_context *db;
197 struct serverid_key key;
198 struct db_record *rec;
208 serverid_fill_key(&id, &key);
209 tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));
211 rec = dbwrap_fetch_locked(db, talloc_tos(), tdbkey);
213 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
217 status = dbwrap_record_delete(rec);
218 if (!NT_STATUS_IS_OK(status)) {
219 DEBUG(1, ("Deleting serverid.tdb record failed: %s\n",
229 struct serverid_exists_state {
230 const struct server_id *id;
234 static void server_exists_parse(TDB_DATA key, TDB_DATA data, void *priv)
236 struct serverid_exists_state *state =
237 (struct serverid_exists_state *)priv;
239 if (data.dsize != sizeof(struct serverid_data)) {
240 state->exists = false;
245 * Use memcmp, not direct compare. data.dptr might not be
248 state->exists = (memcmp(&state->id->unique_id, data.dptr,
249 sizeof(state->id->unique_id)) == 0);
252 bool serverid_exists(const struct server_id *id)
257 ok = serverids_exist(id, 1, &result);
265 bool serverids_exist(const struct server_id *ids, int num_ids, bool *results)
267 int *todo_idx = NULL;
268 struct server_id *todo_ids = NULL;
269 bool *todo_results = NULL;
271 int *remote_idx = NULL;
273 int *verify_idx = NULL;
277 struct db_context *db;
284 todo_idx = talloc_array(talloc_tos(), int, num_ids);
285 if (todo_idx == NULL) {
288 todo_ids = talloc_array(talloc_tos(), struct server_id, num_ids);
289 if (todo_ids == NULL) {
292 todo_results = talloc_array(talloc_tos(), bool, num_ids);
293 if (todo_results == NULL) {
297 remote_idx = talloc_array(talloc_tos(), int, num_ids);
298 if (remote_idx == NULL) {
301 verify_idx = talloc_array(talloc_tos(), int, num_ids);
302 if (verify_idx == NULL) {
306 for (idx=0; idx<num_ids; idx++) {
307 results[idx] = false;
309 if (server_id_is_disconnected(&ids[idx])) {
313 if (procid_is_me(&ids[idx])) {
318 if (procid_is_local(&ids[idx])) {
319 bool exists = process_exists_by_pid(ids[idx].pid);
325 if (ids[idx].unique_id == SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
330 verify_idx[verify_num] = idx;
335 if (!lp_clustering()) {
339 remote_idx[remote_num] = idx;
343 if (remote_num != 0) {
346 for (t=0; t<remote_num; t++) {
348 todo_idx[todo_num] = idx;
349 todo_ids[todo_num] = ids[idx];
350 todo_results[todo_num] = false;
354 #ifdef CLUSTER_SUPPORT
355 if (!ctdb_processes_exist(messaging_ctdbd_connection(),
362 for (t=0; t<todo_num; t++) {
365 if (!todo_results[t]) {
369 if (ids[idx].unique_id == SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
374 verify_idx[verify_num] = idx;
379 for (t=0; t<verify_num; t++) {
380 struct serverid_exists_state state;
381 struct serverid_key key;
387 serverid_fill_key(&ids[idx], &key);
388 tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));
390 state.id = &ids[idx];
391 state.exists = false;
392 status = dbwrap_parse_record(db, tdbkey, server_exists_parse, &state);
393 if (!NT_STATUS_IS_OK(status)) {
394 results[idx] = false;
397 results[idx] = state.exists;
402 TALLOC_FREE(verify_idx);
403 TALLOC_FREE(remote_idx);
404 TALLOC_FREE(todo_results);
405 TALLOC_FREE(todo_ids);
406 TALLOC_FREE(todo_idx);
410 static bool serverid_rec_parse(const struct db_record *rec,
411 struct server_id *id, uint32_t *msg_flags)
413 struct serverid_key key;
414 struct serverid_data data;
418 tdbkey = dbwrap_record_get_key(rec);
419 tdbdata = dbwrap_record_get_value(rec);
421 if (tdbkey.dsize != sizeof(key)) {
422 DEBUG(1, ("Found invalid key length %d in serverid.tdb\n",
426 if (tdbdata.dsize != sizeof(data)) {
427 DEBUG(1, ("Found invalid value length %d in serverid.tdb\n",
428 (int)tdbdata.dsize));
432 memcpy(&key, tdbkey.dptr, sizeof(key));
433 memcpy(&data, tdbdata.dptr, sizeof(data));
436 id->task_id = key.task_id;
438 id->unique_id = data.unique_id;
439 *msg_flags = data.msg_flags;
443 struct serverid_traverse_read_state {
444 int (*fn)(const struct server_id *id, uint32_t msg_flags,
449 static int serverid_traverse_read_fn(struct db_record *rec, void *private_data)
451 struct serverid_traverse_read_state *state =
452 (struct serverid_traverse_read_state *)private_data;
456 if (!serverid_rec_parse(rec, &id, &msg_flags)) {
459 return state->fn(&id, msg_flags,state->private_data);
462 bool serverid_traverse_read(int (*fn)(const struct server_id *id,
463 uint32_t msg_flags, void *private_data),
466 struct db_context *db;
467 struct serverid_traverse_read_state state;
475 state.private_data = private_data;
477 status = dbwrap_traverse_read(db, serverid_traverse_read_fn, &state,
479 return NT_STATUS_IS_OK(status);
482 struct serverid_traverse_state {
483 int (*fn)(struct db_record *rec, const struct server_id *id,
484 uint32_t msg_flags, void *private_data);
488 static int serverid_traverse_fn(struct db_record *rec, void *private_data)
490 struct serverid_traverse_state *state =
491 (struct serverid_traverse_state *)private_data;
495 if (!serverid_rec_parse(rec, &id, &msg_flags)) {
498 return state->fn(rec, &id, msg_flags, state->private_data);
501 bool serverid_traverse(int (*fn)(struct db_record *rec,
502 const struct server_id *id,
503 uint32_t msg_flags, void *private_data),
506 struct db_context *db;
507 struct serverid_traverse_state state;
515 state.private_data = private_data;
517 status = dbwrap_traverse(db, serverid_traverse_fn, &state, NULL);
518 return NT_STATUS_IS_OK(status);
521 uint64_t serverid_get_random_unique_id(void)
523 uint64_t unique_id = SERVERID_UNIQUE_ID_NOT_TO_VERIFY;
525 while (unique_id == SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
526 generate_random_buffer((uint8_t *)&unique_id,