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"
25 #include "lib/util/tdb_wrap.h"
32 struct serverid_data
{
37 static struct db_context
*db_ptr
= NULL
;
39 static struct db_context
*serverid_init(TALLOC_CTX
*mem_ctx
)
41 db_ptr
= db_open(mem_ctx
, lock_path("serverid.tdb"),
42 0, TDB_DEFAULT
|TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
,
48 bool serverid_parent_init(TALLOC_CTX
*mem_ctx
)
51 * Open the tdb in the parent process (smbd) so that our
52 * CLEAR_IF_FIRST optimization in tdb_reopen_all can properly
56 if (serverid_init(mem_ctx
) == NULL
) {
57 DEBUG(1, ("could not open serverid.tdb: %s\n",
65 static struct db_context
*serverid_db(void)
71 return serverid_init(NULL
);
74 static void serverid_fill_key(const struct server_id
*id
,
75 struct serverid_key
*key
)
82 bool serverid_register(const struct server_id id
, uint32_t msg_flags
)
84 struct db_context
*db
;
85 struct serverid_key key
;
86 struct serverid_data data
;
87 struct db_record
*rec
;
88 TDB_DATA tdbkey
, tdbdata
;
97 serverid_fill_key(&id
, &key
);
98 tdbkey
= make_tdb_data((uint8_t *)&key
, sizeof(key
));
100 rec
= db
->fetch_locked(db
, talloc_tos(), tdbkey
);
102 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
107 data
.unique_id
= id
.unique_id
;
108 data
.msg_flags
= msg_flags
;
110 tdbdata
= make_tdb_data((uint8_t *)&data
, sizeof(data
));
111 status
= rec
->store(rec
, tdbdata
, 0);
112 if (!NT_STATUS_IS_OK(status
)) {
113 DEBUG(1, ("Storing serverid.tdb record failed: %s\n",
123 bool serverid_register_msg_flags(const struct server_id id
, bool do_reg
,
126 struct db_context
*db
;
127 struct serverid_key key
;
128 struct serverid_data
*data
;
129 struct db_record
*rec
;
139 serverid_fill_key(&id
, &key
);
140 tdbkey
= make_tdb_data((uint8_t *)&key
, sizeof(key
));
142 rec
= db
->fetch_locked(db
, talloc_tos(), tdbkey
);
144 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
148 if (rec
->value
.dsize
!= sizeof(struct serverid_data
)) {
149 DEBUG(1, ("serverid record has unexpected size %d "
150 "(wanted %d)\n", (int)rec
->value
.dsize
,
151 (int)sizeof(struct serverid_data
)));
155 data
= (struct serverid_data
*)rec
->value
.dptr
;
158 data
->msg_flags
|= msg_flags
;
160 data
->msg_flags
&= ~msg_flags
;
163 status
= rec
->store(rec
, rec
->value
, 0);
164 if (!NT_STATUS_IS_OK(status
)) {
165 DEBUG(1, ("Storing serverid.tdb record failed: %s\n",
175 bool serverid_deregister(struct server_id id
)
177 struct db_context
*db
;
178 struct serverid_key key
;
179 struct db_record
*rec
;
189 serverid_fill_key(&id
, &key
);
190 tdbkey
= make_tdb_data((uint8_t *)&key
, sizeof(key
));
192 rec
= db
->fetch_locked(db
, talloc_tos(), tdbkey
);
194 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
198 status
= rec
->delete_rec(rec
);
199 if (!NT_STATUS_IS_OK(status
)) {
200 DEBUG(1, ("Deleting serverid.tdb record failed: %s\n",
210 struct serverid_exists_state
{
211 const struct server_id
*id
;
215 static int server_exists_parse(TDB_DATA key
, TDB_DATA data
, void *priv
)
217 struct serverid_exists_state
*state
=
218 (struct serverid_exists_state
*)priv
;
220 if (data
.dsize
!= sizeof(struct serverid_data
)) {
225 * Use memcmp, not direct compare. data.dptr might not be
228 state
->exists
= (memcmp(&state
->id
->unique_id
, data
.dptr
,
229 sizeof(state
->id
->unique_id
)) == 0);
233 bool serverid_exists(const struct server_id
*id
)
235 struct db_context
*db
;
236 struct serverid_exists_state state
;
237 struct serverid_key key
;
240 if (procid_is_me(id
)) {
244 if (!process_exists(*id
)) {
248 if (id
->unique_id
== SERVERID_UNIQUE_ID_NOT_TO_VERIFY
) {
257 serverid_fill_key(id
, &key
);
258 tdbkey
= make_tdb_data((uint8_t *)&key
, sizeof(key
));
261 state
.exists
= false;
263 if (db
->parse_record(db
, tdbkey
, server_exists_parse
, &state
) == -1) {
269 static bool serverid_rec_parse(const struct db_record
*rec
,
270 struct server_id
*id
, uint32_t *msg_flags
)
272 struct serverid_key key
;
273 struct serverid_data data
;
275 if (rec
->key
.dsize
!= sizeof(key
)) {
276 DEBUG(1, ("Found invalid key length %d in serverid.tdb\n",
277 (int)rec
->key
.dsize
));
280 if (rec
->value
.dsize
!= sizeof(data
)) {
281 DEBUG(1, ("Found invalid value length %d in serverid.tdb\n",
282 (int)rec
->value
.dsize
));
286 memcpy(&key
, rec
->key
.dptr
, sizeof(key
));
287 memcpy(&data
, rec
->value
.dptr
, sizeof(data
));
291 id
->unique_id
= data
.unique_id
;
292 *msg_flags
= data
.msg_flags
;
296 struct serverid_traverse_read_state
{
297 int (*fn
)(const struct server_id
*id
, uint32_t msg_flags
,
302 static int serverid_traverse_read_fn(struct db_record
*rec
, void *private_data
)
304 struct serverid_traverse_read_state
*state
=
305 (struct serverid_traverse_read_state
*)private_data
;
309 if (!serverid_rec_parse(rec
, &id
, &msg_flags
)) {
312 return state
->fn(&id
, msg_flags
,state
->private_data
);
315 bool serverid_traverse_read(int (*fn
)(const struct server_id
*id
,
316 uint32_t msg_flags
, void *private_data
),
319 struct db_context
*db
;
320 struct serverid_traverse_read_state state
;
327 state
.private_data
= private_data
;
328 return db
->traverse_read(db
, serverid_traverse_read_fn
, &state
);
331 struct serverid_traverse_state
{
332 int (*fn
)(struct db_record
*rec
, const struct server_id
*id
,
333 uint32_t msg_flags
, void *private_data
);
337 static int serverid_traverse_fn(struct db_record
*rec
, void *private_data
)
339 struct serverid_traverse_state
*state
=
340 (struct serverid_traverse_state
*)private_data
;
344 if (!serverid_rec_parse(rec
, &id
, &msg_flags
)) {
347 return state
->fn(rec
, &id
, msg_flags
, state
->private_data
);
350 bool serverid_traverse(int (*fn
)(struct db_record
*rec
,
351 const struct server_id
*id
,
352 uint32_t msg_flags
, void *private_data
),
355 struct db_context
*db
;
356 struct serverid_traverse_state state
;
363 state
.private_data
= private_data
;
364 return db
->traverse(db
, serverid_traverse_fn
, &state
);
367 uint64_t serverid_get_random_unique_id(void)
369 uint64_t unique_id
= SERVERID_UNIQUE_ID_NOT_TO_VERIFY
;
371 while (unique_id
== SERVERID_UNIQUE_ID_NOT_TO_VERIFY
) {
372 generate_random_buffer((uint8_t *)&unique_id
,