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
;
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
,
56 bool serverid_parent_init(TALLOC_CTX
*mem_ctx
)
58 struct db_context
*db
;
62 DEBUG(1, ("could not open serverid.tdb: %s\n",
70 static void serverid_fill_key(const struct server_id
*id
,
71 struct serverid_key
*key
)
75 key
->task_id
= id
->task_id
;
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
;
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
);
99 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
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",
115 if (lp_clustering() &&
116 ctdb_serverids_exist_supported(messaging_ctdbd_connection()))
118 register_with_ctdbd(messaging_ctdbd_connection(), id
.unique_id
);
127 bool serverid_deregister(struct server_id id
)
129 struct db_context
*db
;
130 struct serverid_key key
;
131 struct db_record
*rec
;
141 serverid_fill_key(&id
, &key
);
142 tdbkey
= make_tdb_data((uint8_t *)&key
, sizeof(key
));
144 rec
= dbwrap_fetch_locked(db
, talloc_tos(), tdbkey
);
146 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
150 status
= dbwrap_record_delete(rec
);
151 if (!NT_STATUS_IS_OK(status
)) {
152 DEBUG(1, ("Deleting serverid.tdb record failed: %s\n",
162 struct serverid_exists_state
{
163 const struct server_id
*id
;
167 static void server_exists_parse(TDB_DATA key
, TDB_DATA data
, void *priv
)
169 struct serverid_exists_state
*state
=
170 (struct serverid_exists_state
*)priv
;
172 if (data
.dsize
!= sizeof(struct serverid_data
)) {
173 state
->exists
= false;
178 * Use memcmp, not direct compare. data.dptr might not be
181 state
->exists
= (memcmp(&state
->id
->unique_id
, data
.dptr
,
182 sizeof(state
->id
->unique_id
)) == 0);
185 bool serverid_exists(const struct server_id
*id
)
190 ok
= serverids_exist(id
, 1, &result
);
198 bool serverids_exist(const struct server_id
*ids
, int num_ids
, bool *results
)
200 int *todo_idx
= NULL
;
201 struct server_id
*todo_ids
= NULL
;
202 bool *todo_results
= NULL
;
204 int *remote_idx
= NULL
;
206 int *verify_idx
= NULL
;
210 struct db_context
*db
;
217 todo_idx
= talloc_array(talloc_tos(), int, num_ids
);
218 if (todo_idx
== NULL
) {
221 todo_ids
= talloc_array(talloc_tos(), struct server_id
, num_ids
);
222 if (todo_ids
== NULL
) {
225 todo_results
= talloc_array(talloc_tos(), bool, num_ids
);
226 if (todo_results
== NULL
) {
230 remote_idx
= talloc_array(talloc_tos(), int, num_ids
);
231 if (remote_idx
== NULL
) {
234 verify_idx
= talloc_array(talloc_tos(), int, num_ids
);
235 if (verify_idx
== NULL
) {
239 for (idx
=0; idx
<num_ids
; idx
++) {
240 results
[idx
] = false;
242 if (server_id_is_disconnected(&ids
[idx
])) {
246 if (procid_is_me(&ids
[idx
])) {
251 if (procid_is_local(&ids
[idx
])) {
252 bool exists
= process_exists_by_pid(ids
[idx
].pid
);
258 if (ids
[idx
].unique_id
== SERVERID_UNIQUE_ID_NOT_TO_VERIFY
) {
263 verify_idx
[verify_num
] = idx
;
268 if (!lp_clustering()) {
272 remote_idx
[remote_num
] = idx
;
276 if (remote_num
!= 0 &&
277 ctdb_serverids_exist_supported(messaging_ctdbd_connection()))
279 int old_remote_num
= remote_num
;
284 for (t
=0; t
<old_remote_num
; t
++) {
287 if (ids
[idx
].unique_id
== SERVERID_UNIQUE_ID_NOT_TO_VERIFY
) {
288 remote_idx
[remote_num
] = idx
;
293 todo_idx
[todo_num
] = idx
;
294 todo_ids
[todo_num
] = ids
[idx
];
295 todo_results
[todo_num
] = false;
300 * Note: this only uses CTDB_CONTROL_CHECK_SRVIDS
301 * to verify that the server_id still exists,
302 * which means only the server_id.unique_id and
303 * server_id.vnn are verified, while server_id.pid
304 * is not verified at all.
306 * TODO: do we want to verify server_id.pid somehow?
308 if (!ctdb_serverids_exist(messaging_ctdbd_connection(),
309 todo_ids
, todo_num
, todo_results
))
314 for (t
=0; t
<todo_num
; t
++) {
317 results
[idx
] = todo_results
[t
];
321 if (remote_num
!= 0) {
324 for (t
=0; t
<remote_num
; t
++) {
326 todo_idx
[todo_num
] = idx
;
327 todo_ids
[todo_num
] = ids
[idx
];
328 todo_results
[todo_num
] = false;
332 if (!ctdb_processes_exist(messaging_ctdbd_connection(),
338 for (t
=0; t
<todo_num
; t
++) {
341 if (!todo_results
[t
]) {
345 if (ids
[idx
].unique_id
== SERVERID_UNIQUE_ID_NOT_TO_VERIFY
) {
350 verify_idx
[verify_num
] = idx
;
355 for (t
=0; t
<verify_num
; t
++) {
356 struct serverid_exists_state state
;
357 struct serverid_key key
;
363 serverid_fill_key(&ids
[idx
], &key
);
364 tdbkey
= make_tdb_data((uint8_t *)&key
, sizeof(key
));
366 state
.id
= &ids
[idx
];
367 state
.exists
= false;
368 status
= dbwrap_parse_record(db
, tdbkey
, server_exists_parse
, &state
);
369 if (!NT_STATUS_IS_OK(status
)) {
370 results
[idx
] = false;
373 results
[idx
] = state
.exists
;
378 TALLOC_FREE(verify_idx
);
379 TALLOC_FREE(remote_idx
);
380 TALLOC_FREE(todo_results
);
381 TALLOC_FREE(todo_ids
);
382 TALLOC_FREE(todo_idx
);
386 static bool serverid_rec_parse(const struct db_record
*rec
,
387 struct server_id
*id
, uint32_t *msg_flags
)
389 struct serverid_key key
;
390 struct serverid_data data
;
394 tdbkey
= dbwrap_record_get_key(rec
);
395 tdbdata
= dbwrap_record_get_value(rec
);
397 if (tdbkey
.dsize
!= sizeof(key
)) {
398 DEBUG(1, ("Found invalid key length %d in serverid.tdb\n",
402 if (tdbdata
.dsize
!= sizeof(data
)) {
403 DEBUG(1, ("Found invalid value length %d in serverid.tdb\n",
404 (int)tdbdata
.dsize
));
408 memcpy(&key
, tdbkey
.dptr
, sizeof(key
));
409 memcpy(&data
, tdbdata
.dptr
, sizeof(data
));
412 id
->task_id
= key
.task_id
;
414 id
->unique_id
= data
.unique_id
;
415 *msg_flags
= data
.msg_flags
;
419 struct serverid_traverse_read_state
{
420 int (*fn
)(const struct server_id
*id
, uint32_t msg_flags
,
425 static int serverid_traverse_read_fn(struct db_record
*rec
, void *private_data
)
427 struct serverid_traverse_read_state
*state
=
428 (struct serverid_traverse_read_state
*)private_data
;
432 if (!serverid_rec_parse(rec
, &id
, &msg_flags
)) {
435 return state
->fn(&id
, msg_flags
,state
->private_data
);
438 bool serverid_traverse_read(int (*fn
)(const struct server_id
*id
,
439 uint32_t msg_flags
, void *private_data
),
442 struct db_context
*db
;
443 struct serverid_traverse_read_state state
;
451 state
.private_data
= private_data
;
453 status
= dbwrap_traverse_read(db
, serverid_traverse_read_fn
, &state
,
455 return NT_STATUS_IS_OK(status
);
458 struct serverid_traverse_state
{
459 int (*fn
)(struct db_record
*rec
, const struct server_id
*id
,
460 uint32_t msg_flags
, void *private_data
);
464 static int serverid_traverse_fn(struct db_record
*rec
, void *private_data
)
466 struct serverid_traverse_state
*state
=
467 (struct serverid_traverse_state
*)private_data
;
471 if (!serverid_rec_parse(rec
, &id
, &msg_flags
)) {
474 return state
->fn(rec
, &id
, msg_flags
, state
->private_data
);
477 bool serverid_traverse(int (*fn
)(struct db_record
*rec
,
478 const struct server_id
*id
,
479 uint32_t msg_flags
, void *private_data
),
482 struct db_context
*db
;
483 struct serverid_traverse_state state
;
491 state
.private_data
= private_data
;
493 status
= dbwrap_traverse(db
, serverid_traverse_fn
, &state
, NULL
);
494 return NT_STATUS_IS_OK(status
);
497 uint64_t serverid_get_random_unique_id(void)
499 uint64_t unique_id
= SERVERID_UNIQUE_ID_NOT_TO_VERIFY
;
501 while (unique_id
== SERVERID_UNIQUE_ID_NOT_TO_VERIFY
) {
502 generate_random_buffer((uint8_t *)&unique_id
,