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
;
48 lp_ctx
= loadparm_init_s3(mem_ctx
, loadparm_s3_helpers());
50 DEBUG(0, ("loadparm_init_s3 failed\n"));
55 * Open the tdb in the parent process (smbd) so that our
56 * CLEAR_IF_FIRST optimization in tdb_reopen_all can properly
60 fname
= lock_path("serverid.tdb");
62 db
= tdb_wrap_open(mem_ctx
, fname
,
63 lpcfg_tdb_hash_size(lp_ctx
, fname
),
64 lpcfg_tdb_flags(lp_ctx
,
65 TDB_DEFAULT
|TDB_CLEAR_IF_FIRST
|
66 TDB_INCOMPATIBLE_HASH
),
67 O_RDWR
|O_CREAT
, 0644);
68 talloc_unlink(mem_ctx
, lp_ctx
);
70 DEBUG(1, ("could not open serverid.tdb: %s\n",
77 static struct db_context
*serverid_db(void)
79 static struct db_context
*db
;
84 db
= db_open(NULL
, lock_path("serverid.tdb"), 0,
85 TDB_DEFAULT
|TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
,
86 O_RDWR
|O_CREAT
, 0644, DBWRAP_LOCK_ORDER_2
,
91 static void serverid_fill_key(const struct server_id
*id
,
92 struct serverid_key
*key
)
96 key
->task_id
= id
->task_id
;
100 bool serverid_register(const struct server_id id
, uint32_t msg_flags
)
102 struct db_context
*db
;
103 struct serverid_key key
;
104 struct serverid_data data
;
105 struct db_record
*rec
;
106 TDB_DATA tdbkey
, tdbdata
;
115 serverid_fill_key(&id
, &key
);
116 tdbkey
= make_tdb_data((uint8_t *)&key
, sizeof(key
));
118 rec
= dbwrap_fetch_locked(db
, talloc_tos(), tdbkey
);
120 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
125 data
.unique_id
= id
.unique_id
;
126 data
.msg_flags
= msg_flags
;
128 tdbdata
= make_tdb_data((uint8_t *)&data
, sizeof(data
));
129 status
= dbwrap_record_store(rec
, tdbdata
, 0);
130 if (!NT_STATUS_IS_OK(status
)) {
131 DEBUG(1, ("Storing serverid.tdb record failed: %s\n",
136 if (lp_clustering() &&
137 ctdb_serverids_exist_supported(messaging_ctdbd_connection()))
139 register_with_ctdbd(messaging_ctdbd_connection(), id
.unique_id
);
148 bool serverid_register_msg_flags(const struct server_id id
, bool do_reg
,
151 struct db_context
*db
;
152 struct serverid_key key
;
153 struct serverid_data
*data
;
154 struct db_record
*rec
;
165 serverid_fill_key(&id
, &key
);
166 tdbkey
= make_tdb_data((uint8_t *)&key
, sizeof(key
));
168 rec
= dbwrap_fetch_locked(db
, talloc_tos(), tdbkey
);
170 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
174 value
= dbwrap_record_get_value(rec
);
176 if (value
.dsize
!= sizeof(struct serverid_data
)) {
177 DEBUG(1, ("serverid record has unexpected size %d "
178 "(wanted %d)\n", (int)value
.dsize
,
179 (int)sizeof(struct serverid_data
)));
183 data
= (struct serverid_data
*)value
.dptr
;
186 data
->msg_flags
|= msg_flags
;
188 data
->msg_flags
&= ~msg_flags
;
191 status
= dbwrap_record_store(rec
, value
, 0);
192 if (!NT_STATUS_IS_OK(status
)) {
193 DEBUG(1, ("Storing serverid.tdb record failed: %s\n",
203 bool serverid_deregister(struct server_id id
)
205 struct db_context
*db
;
206 struct serverid_key key
;
207 struct db_record
*rec
;
217 serverid_fill_key(&id
, &key
);
218 tdbkey
= make_tdb_data((uint8_t *)&key
, sizeof(key
));
220 rec
= dbwrap_fetch_locked(db
, talloc_tos(), tdbkey
);
222 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
226 status
= dbwrap_record_delete(rec
);
227 if (!NT_STATUS_IS_OK(status
)) {
228 DEBUG(1, ("Deleting serverid.tdb record failed: %s\n",
238 struct serverid_exists_state
{
239 const struct server_id
*id
;
243 static void server_exists_parse(TDB_DATA key
, TDB_DATA data
, void *priv
)
245 struct serverid_exists_state
*state
=
246 (struct serverid_exists_state
*)priv
;
248 if (data
.dsize
!= sizeof(struct serverid_data
)) {
249 state
->exists
= false;
254 * Use memcmp, not direct compare. data.dptr might not be
257 state
->exists
= (memcmp(&state
->id
->unique_id
, data
.dptr
,
258 sizeof(state
->id
->unique_id
)) == 0);
261 bool serverid_exists(const struct server_id
*id
)
266 ok
= serverids_exist(id
, 1, &result
);
274 bool serverids_exist(const struct server_id
*ids
, int num_ids
, bool *results
)
276 int *todo_idx
= NULL
;
277 struct server_id
*todo_ids
= NULL
;
278 bool *todo_results
= NULL
;
280 int *remote_idx
= NULL
;
282 int *verify_idx
= NULL
;
286 struct db_context
*db
;
293 todo_idx
= talloc_array(talloc_tos(), int, num_ids
);
294 if (todo_idx
== NULL
) {
297 todo_ids
= talloc_array(talloc_tos(), struct server_id
, num_ids
);
298 if (todo_ids
== NULL
) {
301 todo_results
= talloc_array(talloc_tos(), bool, num_ids
);
302 if (todo_results
== NULL
) {
306 remote_idx
= talloc_array(talloc_tos(), int, num_ids
);
307 if (remote_idx
== NULL
) {
310 verify_idx
= talloc_array(talloc_tos(), int, num_ids
);
311 if (verify_idx
== NULL
) {
315 for (idx
=0; idx
<num_ids
; idx
++) {
316 results
[idx
] = false;
318 if (server_id_is_disconnected(&ids
[idx
])) {
322 if (procid_is_me(&ids
[idx
])) {
327 if (procid_is_local(&ids
[idx
])) {
328 bool exists
= process_exists_by_pid(ids
[idx
].pid
);
334 if (ids
[idx
].unique_id
== SERVERID_UNIQUE_ID_NOT_TO_VERIFY
) {
339 verify_idx
[verify_num
] = idx
;
344 if (!lp_clustering()) {
348 remote_idx
[remote_num
] = idx
;
352 if (remote_num
!= 0 &&
353 ctdb_serverids_exist_supported(messaging_ctdbd_connection()))
355 int old_remote_num
= remote_num
;
360 for (t
=0; t
<old_remote_num
; t
++) {
363 if (ids
[idx
].unique_id
== SERVERID_UNIQUE_ID_NOT_TO_VERIFY
) {
364 remote_idx
[remote_num
] = idx
;
369 todo_idx
[todo_num
] = idx
;
370 todo_ids
[todo_num
] = ids
[idx
];
371 todo_results
[todo_num
] = false;
376 * Note: this only uses CTDB_CONTROL_CHECK_SRVIDS
377 * to verify that the server_id still exists,
378 * which means only the server_id.unique_id and
379 * server_id.vnn are verified, while server_id.pid
380 * is not verified at all.
382 * TODO: do we want to verify server_id.pid somehow?
384 if (!ctdb_serverids_exist(messaging_ctdbd_connection(),
385 todo_ids
, todo_num
, todo_results
))
390 for (t
=0; t
<todo_num
; t
++) {
393 results
[idx
] = todo_results
[t
];
397 if (remote_num
!= 0) {
400 for (t
=0; t
<remote_num
; t
++) {
402 todo_idx
[todo_num
] = idx
;
403 todo_ids
[todo_num
] = ids
[idx
];
404 todo_results
[todo_num
] = false;
408 if (!ctdb_processes_exist(messaging_ctdbd_connection(),
414 for (t
=0; t
<todo_num
; t
++) {
417 if (!todo_results
[t
]) {
421 if (ids
[idx
].unique_id
== SERVERID_UNIQUE_ID_NOT_TO_VERIFY
) {
426 verify_idx
[verify_num
] = idx
;
431 for (t
=0; t
<verify_num
; t
++) {
432 struct serverid_exists_state state
;
433 struct serverid_key key
;
439 serverid_fill_key(&ids
[idx
], &key
);
440 tdbkey
= make_tdb_data((uint8_t *)&key
, sizeof(key
));
442 state
.id
= &ids
[idx
];
443 state
.exists
= false;
444 status
= dbwrap_parse_record(db
, tdbkey
, server_exists_parse
, &state
);
445 if (!NT_STATUS_IS_OK(status
)) {
446 results
[idx
] = false;
449 results
[idx
] = state
.exists
;
454 TALLOC_FREE(verify_idx
);
455 TALLOC_FREE(remote_idx
);
456 TALLOC_FREE(todo_results
);
457 TALLOC_FREE(todo_ids
);
458 TALLOC_FREE(todo_idx
);
462 static bool serverid_rec_parse(const struct db_record
*rec
,
463 struct server_id
*id
, uint32_t *msg_flags
)
465 struct serverid_key key
;
466 struct serverid_data data
;
470 tdbkey
= dbwrap_record_get_key(rec
);
471 tdbdata
= dbwrap_record_get_value(rec
);
473 if (tdbkey
.dsize
!= sizeof(key
)) {
474 DEBUG(1, ("Found invalid key length %d in serverid.tdb\n",
478 if (tdbdata
.dsize
!= sizeof(data
)) {
479 DEBUG(1, ("Found invalid value length %d in serverid.tdb\n",
480 (int)tdbdata
.dsize
));
484 memcpy(&key
, tdbkey
.dptr
, sizeof(key
));
485 memcpy(&data
, tdbdata
.dptr
, sizeof(data
));
488 id
->task_id
= key
.task_id
;
490 id
->unique_id
= data
.unique_id
;
491 *msg_flags
= data
.msg_flags
;
495 struct serverid_traverse_read_state
{
496 int (*fn
)(const struct server_id
*id
, uint32_t msg_flags
,
501 static int serverid_traverse_read_fn(struct db_record
*rec
, void *private_data
)
503 struct serverid_traverse_read_state
*state
=
504 (struct serverid_traverse_read_state
*)private_data
;
508 if (!serverid_rec_parse(rec
, &id
, &msg_flags
)) {
511 return state
->fn(&id
, msg_flags
,state
->private_data
);
514 bool serverid_traverse_read(int (*fn
)(const struct server_id
*id
,
515 uint32_t msg_flags
, void *private_data
),
518 struct db_context
*db
;
519 struct serverid_traverse_read_state state
;
527 state
.private_data
= private_data
;
529 status
= dbwrap_traverse_read(db
, serverid_traverse_read_fn
, &state
,
531 return NT_STATUS_IS_OK(status
);
534 struct serverid_traverse_state
{
535 int (*fn
)(struct db_record
*rec
, const struct server_id
*id
,
536 uint32_t msg_flags
, void *private_data
);
540 static int serverid_traverse_fn(struct db_record
*rec
, void *private_data
)
542 struct serverid_traverse_state
*state
=
543 (struct serverid_traverse_state
*)private_data
;
547 if (!serverid_rec_parse(rec
, &id
, &msg_flags
)) {
550 return state
->fn(rec
, &id
, msg_flags
, state
->private_data
);
553 bool serverid_traverse(int (*fn
)(struct db_record
*rec
,
554 const struct server_id
*id
,
555 uint32_t msg_flags
, void *private_data
),
558 struct db_context
*db
;
559 struct serverid_traverse_state state
;
567 state
.private_data
= private_data
;
569 status
= dbwrap_traverse(db
, serverid_traverse_fn
, &state
, NULL
);
570 return NT_STATUS_IS_OK(status
);
573 uint64_t serverid_get_random_unique_id(void)
575 uint64_t unique_id
= SERVERID_UNIQUE_ID_NOT_TO_VERIFY
;
577 while (unique_id
== SERVERID_UNIQUE_ID_NOT_TO_VERIFY
) {
578 generate_random_buffer((uint8_t *)&unique_id
,