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 static struct db_context
*serverid_db(void)
44 static struct db_context
*db
;
51 db_path
= lock_path("serverid.tdb");
52 if (db_path
== 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
,
64 bool serverid_parent_init(TALLOC_CTX
*mem_ctx
)
66 struct db_context
*db
;
70 DEBUG(1, ("could not open serverid.tdb: %s\n",
78 static void serverid_fill_key(const struct server_id
*id
,
79 struct serverid_key
*key
)
83 key
->task_id
= id
->task_id
;
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
;
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
);
107 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
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",
123 if (lp_clustering() &&
124 ctdb_serverids_exist_supported(messaging_ctdbd_connection()))
126 register_with_ctdbd(messaging_ctdbd_connection(), id
.unique_id
);
135 bool serverid_deregister(struct server_id id
)
137 struct db_context
*db
;
138 struct serverid_key key
;
139 struct db_record
*rec
;
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
);
154 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
158 status
= dbwrap_record_delete(rec
);
159 if (!NT_STATUS_IS_OK(status
)) {
160 DEBUG(1, ("Deleting serverid.tdb record failed: %s\n",
170 struct serverid_exists_state
{
171 const struct server_id
*id
;
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;
186 * Use memcmp, not direct compare. data.dptr might not be
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
)
198 ok
= serverids_exist(id
, 1, &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
;
212 int *remote_idx
= NULL
;
214 int *verify_idx
= NULL
;
218 struct db_context
*db
;
225 todo_idx
= talloc_array(talloc_tos(), int, num_ids
);
226 if (todo_idx
== NULL
) {
229 todo_ids
= talloc_array(talloc_tos(), struct server_id
, num_ids
);
230 if (todo_ids
== NULL
) {
233 todo_results
= talloc_array(talloc_tos(), bool, num_ids
);
234 if (todo_results
== NULL
) {
238 remote_idx
= talloc_array(talloc_tos(), int, num_ids
);
239 if (remote_idx
== NULL
) {
242 verify_idx
= talloc_array(talloc_tos(), int, num_ids
);
243 if (verify_idx
== NULL
) {
247 for (idx
=0; idx
<num_ids
; idx
++) {
248 results
[idx
] = false;
250 if (server_id_is_disconnected(&ids
[idx
])) {
254 if (procid_is_me(&ids
[idx
])) {
259 if (procid_is_local(&ids
[idx
])) {
260 bool exists
= process_exists_by_pid(ids
[idx
].pid
);
266 if (ids
[idx
].unique_id
== SERVERID_UNIQUE_ID_NOT_TO_VERIFY
) {
271 verify_idx
[verify_num
] = idx
;
276 if (!lp_clustering()) {
280 remote_idx
[remote_num
] = idx
;
284 if (remote_num
!= 0 &&
285 ctdb_serverids_exist_supported(messaging_ctdbd_connection()))
287 int old_remote_num
= remote_num
;
292 for (t
=0; t
<old_remote_num
; t
++) {
295 if (ids
[idx
].unique_id
== SERVERID_UNIQUE_ID_NOT_TO_VERIFY
) {
296 remote_idx
[remote_num
] = idx
;
301 todo_idx
[todo_num
] = idx
;
302 todo_ids
[todo_num
] = ids
[idx
];
303 todo_results
[todo_num
] = false;
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
))
322 for (t
=0; t
<todo_num
; t
++) {
325 results
[idx
] = todo_results
[t
];
329 if (remote_num
!= 0) {
332 for (t
=0; t
<remote_num
; t
++) {
334 todo_idx
[todo_num
] = idx
;
335 todo_ids
[todo_num
] = ids
[idx
];
336 todo_results
[todo_num
] = false;
340 if (!ctdb_processes_exist(messaging_ctdbd_connection(),
346 for (t
=0; t
<todo_num
; t
++) {
349 if (!todo_results
[t
]) {
353 if (ids
[idx
].unique_id
== SERVERID_UNIQUE_ID_NOT_TO_VERIFY
) {
358 verify_idx
[verify_num
] = idx
;
363 for (t
=0; t
<verify_num
; t
++) {
364 struct serverid_exists_state state
;
365 struct serverid_key key
;
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;
381 results
[idx
] = state
.exists
;
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
);
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
;
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",
410 if (tdbdata
.dsize
!= sizeof(data
)) {
411 DEBUG(1, ("Found invalid value length %d in serverid.tdb\n",
412 (int)tdbdata
.dsize
));
416 memcpy(&key
, tdbkey
.dptr
, sizeof(key
));
417 memcpy(&data
, tdbdata
.dptr
, sizeof(data
));
420 id
->task_id
= key
.task_id
;
422 id
->unique_id
= data
.unique_id
;
423 *msg_flags
= data
.msg_flags
;
427 struct serverid_traverse_read_state
{
428 int (*fn
)(const struct server_id
*id
, uint32_t msg_flags
,
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
;
440 if (!serverid_rec_parse(rec
, &id
, &msg_flags
)) {
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
),
450 struct db_context
*db
;
451 struct serverid_traverse_read_state state
;
459 state
.private_data
= private_data
;
461 status
= dbwrap_traverse_read(db
, serverid_traverse_read_fn
, &state
,
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
);
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
;
479 if (!serverid_rec_parse(rec
, &id
, &msg_flags
)) {
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
),
490 struct db_context
*db
;
491 struct serverid_traverse_state state
;
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
,