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
)
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_register_msg_flags(const struct server_id id
, bool do_reg
,
130 struct db_context
*db
;
131 struct serverid_key key
;
132 struct serverid_data
*data
;
133 struct db_record
*rec
;
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
);
149 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
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
)));
162 data
= (struct serverid_data
*)value
.dptr
;
165 data
->msg_flags
|= msg_flags
;
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",
182 bool serverid_deregister(struct server_id id
)
184 struct db_context
*db
;
185 struct serverid_key key
;
186 struct db_record
*rec
;
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
);
201 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
205 status
= dbwrap_record_delete(rec
);
206 if (!NT_STATUS_IS_OK(status
)) {
207 DEBUG(1, ("Deleting serverid.tdb record failed: %s\n",
217 struct serverid_exists_state
{
218 const struct server_id
*id
;
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;
233 * Use memcmp, not direct compare. data.dptr might not be
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
)
245 ok
= serverids_exist(id
, 1, &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
;
259 int *remote_idx
= NULL
;
261 int *verify_idx
= NULL
;
265 struct db_context
*db
;
272 todo_idx
= talloc_array(talloc_tos(), int, num_ids
);
273 if (todo_idx
== NULL
) {
276 todo_ids
= talloc_array(talloc_tos(), struct server_id
, num_ids
);
277 if (todo_ids
== NULL
) {
280 todo_results
= talloc_array(talloc_tos(), bool, num_ids
);
281 if (todo_results
== NULL
) {
285 remote_idx
= talloc_array(talloc_tos(), int, num_ids
);
286 if (remote_idx
== NULL
) {
289 verify_idx
= talloc_array(talloc_tos(), int, num_ids
);
290 if (verify_idx
== NULL
) {
294 for (idx
=0; idx
<num_ids
; idx
++) {
295 results
[idx
] = false;
297 if (server_id_is_disconnected(&ids
[idx
])) {
301 if (procid_is_me(&ids
[idx
])) {
306 if (procid_is_local(&ids
[idx
])) {
307 bool exists
= process_exists_by_pid(ids
[idx
].pid
);
313 if (ids
[idx
].unique_id
== SERVERID_UNIQUE_ID_NOT_TO_VERIFY
) {
318 verify_idx
[verify_num
] = idx
;
323 if (!lp_clustering()) {
327 remote_idx
[remote_num
] = idx
;
331 if (remote_num
!= 0 &&
332 ctdb_serverids_exist_supported(messaging_ctdbd_connection()))
334 int old_remote_num
= remote_num
;
339 for (t
=0; t
<old_remote_num
; t
++) {
342 if (ids
[idx
].unique_id
== SERVERID_UNIQUE_ID_NOT_TO_VERIFY
) {
343 remote_idx
[remote_num
] = idx
;
348 todo_idx
[todo_num
] = idx
;
349 todo_ids
[todo_num
] = ids
[idx
];
350 todo_results
[todo_num
] = false;
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
))
369 for (t
=0; t
<todo_num
; t
++) {
372 results
[idx
] = todo_results
[t
];
376 if (remote_num
!= 0) {
379 for (t
=0; t
<remote_num
; t
++) {
381 todo_idx
[todo_num
] = idx
;
382 todo_ids
[todo_num
] = ids
[idx
];
383 todo_results
[todo_num
] = false;
387 if (!ctdb_processes_exist(messaging_ctdbd_connection(),
393 for (t
=0; t
<todo_num
; t
++) {
396 if (!todo_results
[t
]) {
400 if (ids
[idx
].unique_id
== SERVERID_UNIQUE_ID_NOT_TO_VERIFY
) {
405 verify_idx
[verify_num
] = idx
;
410 for (t
=0; t
<verify_num
; t
++) {
411 struct serverid_exists_state state
;
412 struct serverid_key key
;
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;
428 results
[idx
] = state
.exists
;
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
);
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
;
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",
457 if (tdbdata
.dsize
!= sizeof(data
)) {
458 DEBUG(1, ("Found invalid value length %d in serverid.tdb\n",
459 (int)tdbdata
.dsize
));
463 memcpy(&key
, tdbkey
.dptr
, sizeof(key
));
464 memcpy(&data
, tdbdata
.dptr
, sizeof(data
));
467 id
->task_id
= key
.task_id
;
469 id
->unique_id
= data
.unique_id
;
470 *msg_flags
= data
.msg_flags
;
474 struct serverid_traverse_read_state
{
475 int (*fn
)(const struct server_id
*id
, uint32_t msg_flags
,
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
;
487 if (!serverid_rec_parse(rec
, &id
, &msg_flags
)) {
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
),
497 struct db_context
*db
;
498 struct serverid_traverse_read_state state
;
506 state
.private_data
= private_data
;
508 status
= dbwrap_traverse_read(db
, serverid_traverse_read_fn
, &state
,
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
);
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
;
526 if (!serverid_rec_parse(rec
, &id
, &msg_flags
)) {
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
),
537 struct db_context
*db
;
538 struct serverid_traverse_state state
;
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
,