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
,
136 bool serverid_deregister(struct server_id id
)
138 struct db_context
*db
;
139 struct serverid_key key
;
140 struct db_record
*rec
;
150 serverid_fill_key(&id
, &key
);
151 tdbkey
= make_tdb_data((uint8_t *)&key
, sizeof(key
));
153 rec
= dbwrap_fetch_locked(db
, talloc_tos(), tdbkey
);
155 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
159 status
= dbwrap_record_delete(rec
);
160 if (!NT_STATUS_IS_OK(status
)) {
161 DEBUG(1, ("Deleting serverid.tdb record failed: %s\n",
171 struct serverid_exists_state
{
172 const struct server_id
*id
;
176 static void server_exists_parse(TDB_DATA key
, TDB_DATA data
, void *priv
)
178 struct serverid_exists_state
*state
=
179 (struct serverid_exists_state
*)priv
;
181 if (data
.dsize
!= sizeof(struct serverid_data
)) {
182 state
->exists
= false;
187 * Use memcmp, not direct compare. data.dptr might not be
190 state
->exists
= (memcmp(&state
->id
->unique_id
, data
.dptr
,
191 sizeof(state
->id
->unique_id
)) == 0);
194 bool serverid_exists(const struct server_id
*id
)
199 ok
= serverids_exist(id
, 1, &result
);
207 bool serverids_exist(const struct server_id
*ids
, int num_ids
, bool *results
)
209 int *todo_idx
= NULL
;
210 struct server_id
*todo_ids
= NULL
;
211 bool *todo_results
= NULL
;
213 int *remote_idx
= NULL
;
215 int *verify_idx
= NULL
;
219 struct db_context
*db
;
226 todo_idx
= talloc_array(talloc_tos(), int, num_ids
);
227 if (todo_idx
== NULL
) {
230 todo_ids
= talloc_array(talloc_tos(), struct server_id
, num_ids
);
231 if (todo_ids
== NULL
) {
234 todo_results
= talloc_array(talloc_tos(), bool, num_ids
);
235 if (todo_results
== NULL
) {
239 remote_idx
= talloc_array(talloc_tos(), int, num_ids
);
240 if (remote_idx
== NULL
) {
243 verify_idx
= talloc_array(talloc_tos(), int, num_ids
);
244 if (verify_idx
== NULL
) {
248 for (idx
=0; idx
<num_ids
; idx
++) {
249 results
[idx
] = false;
251 if (server_id_is_disconnected(&ids
[idx
])) {
255 if (procid_is_me(&ids
[idx
])) {
260 if (procid_is_local(&ids
[idx
])) {
261 bool exists
= process_exists_by_pid(ids
[idx
].pid
);
267 if (ids
[idx
].unique_id
== SERVERID_UNIQUE_ID_NOT_TO_VERIFY
) {
272 verify_idx
[verify_num
] = idx
;
277 if (!lp_clustering()) {
281 remote_idx
[remote_num
] = idx
;
285 if (remote_num
!= 0 &&
286 ctdb_serverids_exist_supported(messaging_ctdbd_connection()))
288 int old_remote_num
= remote_num
;
293 for (t
=0; t
<old_remote_num
; t
++) {
296 if (ids
[idx
].unique_id
== SERVERID_UNIQUE_ID_NOT_TO_VERIFY
) {
297 remote_idx
[remote_num
] = idx
;
302 todo_idx
[todo_num
] = idx
;
303 todo_ids
[todo_num
] = ids
[idx
];
304 todo_results
[todo_num
] = false;
309 * Note: this only uses CTDB_CONTROL_CHECK_SRVIDS
310 * to verify that the server_id still exists,
311 * which means only the server_id.unique_id and
312 * server_id.vnn are verified, while server_id.pid
313 * is not verified at all.
315 * TODO: do we want to verify server_id.pid somehow?
317 if (!ctdb_serverids_exist(messaging_ctdbd_connection(),
318 todo_ids
, todo_num
, todo_results
))
323 for (t
=0; t
<todo_num
; t
++) {
326 results
[idx
] = todo_results
[t
];
330 if (remote_num
!= 0) {
333 for (t
=0; t
<remote_num
; t
++) {
335 todo_idx
[todo_num
] = idx
;
336 todo_ids
[todo_num
] = ids
[idx
];
337 todo_results
[todo_num
] = false;
341 if (!ctdb_processes_exist(messaging_ctdbd_connection(),
347 for (t
=0; t
<todo_num
; t
++) {
350 if (!todo_results
[t
]) {
354 if (ids
[idx
].unique_id
== SERVERID_UNIQUE_ID_NOT_TO_VERIFY
) {
359 verify_idx
[verify_num
] = idx
;
364 for (t
=0; t
<verify_num
; t
++) {
365 struct serverid_exists_state state
;
366 struct serverid_key key
;
372 serverid_fill_key(&ids
[idx
], &key
);
373 tdbkey
= make_tdb_data((uint8_t *)&key
, sizeof(key
));
375 state
.id
= &ids
[idx
];
376 state
.exists
= false;
377 status
= dbwrap_parse_record(db
, tdbkey
, server_exists_parse
, &state
);
378 if (!NT_STATUS_IS_OK(status
)) {
379 results
[idx
] = false;
382 results
[idx
] = state
.exists
;
387 TALLOC_FREE(verify_idx
);
388 TALLOC_FREE(remote_idx
);
389 TALLOC_FREE(todo_results
);
390 TALLOC_FREE(todo_ids
);
391 TALLOC_FREE(todo_idx
);
395 static bool serverid_rec_parse(const struct db_record
*rec
,
396 struct server_id
*id
, uint32_t *msg_flags
)
398 struct serverid_key key
;
399 struct serverid_data data
;
403 tdbkey
= dbwrap_record_get_key(rec
);
404 tdbdata
= dbwrap_record_get_value(rec
);
406 if (tdbkey
.dsize
!= sizeof(key
)) {
407 DEBUG(1, ("Found invalid key length %d in serverid.tdb\n",
411 if (tdbdata
.dsize
!= sizeof(data
)) {
412 DEBUG(1, ("Found invalid value length %d in serverid.tdb\n",
413 (int)tdbdata
.dsize
));
417 memcpy(&key
, tdbkey
.dptr
, sizeof(key
));
418 memcpy(&data
, tdbdata
.dptr
, sizeof(data
));
421 id
->task_id
= key
.task_id
;
423 id
->unique_id
= data
.unique_id
;
424 *msg_flags
= data
.msg_flags
;
428 struct serverid_traverse_read_state
{
429 int (*fn
)(const struct server_id
*id
, uint32_t msg_flags
,
434 static int serverid_traverse_read_fn(struct db_record
*rec
, void *private_data
)
436 struct serverid_traverse_read_state
*state
=
437 (struct serverid_traverse_read_state
*)private_data
;
441 if (!serverid_rec_parse(rec
, &id
, &msg_flags
)) {
444 return state
->fn(&id
, msg_flags
,state
->private_data
);
447 bool serverid_traverse_read(int (*fn
)(const struct server_id
*id
,
448 uint32_t msg_flags
, void *private_data
),
451 struct db_context
*db
;
452 struct serverid_traverse_read_state state
;
460 state
.private_data
= private_data
;
462 status
= dbwrap_traverse_read(db
, serverid_traverse_read_fn
, &state
,
464 return NT_STATUS_IS_OK(status
);
467 struct serverid_traverse_state
{
468 int (*fn
)(struct db_record
*rec
, const struct server_id
*id
,
469 uint32_t msg_flags
, void *private_data
);
473 static int serverid_traverse_fn(struct db_record
*rec
, void *private_data
)
475 struct serverid_traverse_state
*state
=
476 (struct serverid_traverse_state
*)private_data
;
480 if (!serverid_rec_parse(rec
, &id
, &msg_flags
)) {
483 return state
->fn(rec
, &id
, msg_flags
, state
->private_data
);
486 bool serverid_traverse(int (*fn
)(struct db_record
*rec
,
487 const struct server_id
*id
,
488 uint32_t msg_flags
, void *private_data
),
491 struct db_context
*db
;
492 struct serverid_traverse_state state
;
500 state
.private_data
= private_data
;
502 status
= dbwrap_traverse(db
, serverid_traverse_fn
, &state
, NULL
);
503 return NT_STATUS_IS_OK(status
);
506 uint64_t serverid_get_random_unique_id(void)
508 uint64_t unique_id
= SERVERID_UNIQUE_ID_NOT_TO_VERIFY
;
510 while (unique_id
== SERVERID_UNIQUE_ID_NOT_TO_VERIFY
) {
511 generate_random_buffer((uint8_t *)&unique_id
,