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/util/tdb_wrap.h"
27 #include "lib/param/param.h"
35 struct serverid_data
{
40 bool serverid_parent_init(TALLOC_CTX
*mem_ctx
)
43 struct loadparm_context
*lp_ctx
;
45 lp_ctx
= loadparm_init_s3(mem_ctx
, loadparm_s3_context());
47 DEBUG(0, ("loadparm_init_s3 failed\n"));
52 * Open the tdb in the parent process (smbd) so that our
53 * CLEAR_IF_FIRST optimization in tdb_reopen_all can properly
57 db
= tdb_wrap_open(mem_ctx
, lock_path("serverid.tdb"),
58 0, TDB_DEFAULT
|TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
, O_RDWR
|O_CREAT
,
60 talloc_unlink(mem_ctx
, lp_ctx
);
62 DEBUG(1, ("could not open serverid.tdb: %s\n",
69 static struct db_context
*serverid_db(void)
71 static struct db_context
*db
;
76 db
= db_open(NULL
, lock_path("serverid.tdb"), 0,
77 TDB_DEFAULT
|TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
, O_RDWR
|O_CREAT
, 0644);
81 static void serverid_fill_key(const struct server_id
*id
,
82 struct serverid_key
*key
)
86 key
->task_id
= id
->task_id
;
90 bool serverid_register(const struct server_id id
, uint32_t msg_flags
)
92 struct db_context
*db
;
93 struct serverid_key key
;
94 struct serverid_data data
;
95 struct db_record
*rec
;
96 TDB_DATA tdbkey
, tdbdata
;
105 serverid_fill_key(&id
, &key
);
106 tdbkey
= make_tdb_data((uint8_t *)&key
, sizeof(key
));
108 rec
= dbwrap_fetch_locked(db
, talloc_tos(), tdbkey
);
110 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
115 data
.unique_id
= id
.unique_id
;
116 data
.msg_flags
= msg_flags
;
118 tdbdata
= make_tdb_data((uint8_t *)&data
, sizeof(data
));
119 status
= dbwrap_record_store(rec
, tdbdata
, 0);
120 if (!NT_STATUS_IS_OK(status
)) {
121 DEBUG(1, ("Storing serverid.tdb record failed: %s\n",
131 bool serverid_register_msg_flags(const struct server_id id
, bool do_reg
,
134 struct db_context
*db
;
135 struct serverid_key key
;
136 struct serverid_data
*data
;
137 struct db_record
*rec
;
148 serverid_fill_key(&id
, &key
);
149 tdbkey
= make_tdb_data((uint8_t *)&key
, sizeof(key
));
151 rec
= dbwrap_fetch_locked(db
, talloc_tos(), tdbkey
);
153 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
157 value
= dbwrap_record_get_value(rec
);
159 if (value
.dsize
!= sizeof(struct serverid_data
)) {
160 DEBUG(1, ("serverid record has unexpected size %d "
161 "(wanted %d)\n", (int)value
.dsize
,
162 (int)sizeof(struct serverid_data
)));
166 data
= (struct serverid_data
*)value
.dptr
;
169 data
->msg_flags
|= msg_flags
;
171 data
->msg_flags
&= ~msg_flags
;
174 status
= dbwrap_record_store(rec
, value
, 0);
175 if (!NT_STATUS_IS_OK(status
)) {
176 DEBUG(1, ("Storing serverid.tdb record failed: %s\n",
186 bool serverid_deregister(struct server_id id
)
188 struct db_context
*db
;
189 struct serverid_key key
;
190 struct db_record
*rec
;
200 serverid_fill_key(&id
, &key
);
201 tdbkey
= make_tdb_data((uint8_t *)&key
, sizeof(key
));
203 rec
= dbwrap_fetch_locked(db
, talloc_tos(), tdbkey
);
205 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
209 status
= dbwrap_record_delete(rec
);
210 if (!NT_STATUS_IS_OK(status
)) {
211 DEBUG(1, ("Deleting serverid.tdb record failed: %s\n",
221 struct serverid_exists_state
{
222 const struct server_id
*id
;
226 static int server_exists_parse(TDB_DATA key
, TDB_DATA data
, void *priv
)
228 struct serverid_exists_state
*state
=
229 (struct serverid_exists_state
*)priv
;
231 if (data
.dsize
!= sizeof(struct serverid_data
)) {
236 * Use memcmp, not direct compare. data.dptr might not be
239 state
->exists
= (memcmp(&state
->id
->unique_id
, data
.dptr
,
240 sizeof(state
->id
->unique_id
)) == 0);
244 bool serverid_exists(const struct server_id
*id
)
246 struct db_context
*db
;
247 struct serverid_exists_state state
;
248 struct serverid_key key
;
251 if (procid_is_me(id
)) {
255 if (!process_exists(*id
)) {
264 serverid_fill_key(id
, &key
);
265 tdbkey
= make_tdb_data((uint8_t *)&key
, sizeof(key
));
268 state
.exists
= false;
270 if (dbwrap_parse_record(db
, tdbkey
, server_exists_parse
, &state
) != 0) {
276 bool serverids_exist(const struct server_id
*ids
, int num_ids
, bool *results
)
278 struct db_context
*db
;
281 if (!processes_exist(ids
, num_ids
, results
)) {
290 for (i
=0; i
<num_ids
; i
++) {
291 struct serverid_exists_state state
;
292 struct serverid_key key
;
299 serverid_fill_key(&ids
[i
], &key
);
300 tdbkey
= make_tdb_data((uint8_t *)&key
, sizeof(key
));
303 state
.exists
= false;
304 dbwrap_parse_record(db
, tdbkey
, server_exists_parse
, &state
);
305 results
[i
] = state
.exists
;
310 static bool serverid_rec_parse(const struct db_record
*rec
,
311 struct server_id
*id
, uint32_t *msg_flags
)
313 struct serverid_key key
;
314 struct serverid_data data
;
318 tdbkey
= dbwrap_record_get_key(rec
);
319 tdbdata
= dbwrap_record_get_value(rec
);
321 if (tdbkey
.dsize
!= sizeof(key
)) {
322 DEBUG(1, ("Found invalid key length %d in serverid.tdb\n",
326 if (tdbdata
.dsize
!= sizeof(data
)) {
327 DEBUG(1, ("Found invalid value length %d in serverid.tdb\n",
328 (int)tdbdata
.dsize
));
332 memcpy(&key
, tdbkey
.dptr
, sizeof(key
));
333 memcpy(&data
, tdbdata
.dptr
, sizeof(data
));
336 id
->task_id
= key
.task_id
;
338 id
->unique_id
= data
.unique_id
;
339 *msg_flags
= data
.msg_flags
;
343 struct serverid_traverse_read_state
{
344 int (*fn
)(const struct server_id
*id
, uint32_t msg_flags
,
349 static int serverid_traverse_read_fn(struct db_record
*rec
, void *private_data
)
351 struct serverid_traverse_read_state
*state
=
352 (struct serverid_traverse_read_state
*)private_data
;
356 if (!serverid_rec_parse(rec
, &id
, &msg_flags
)) {
359 return state
->fn(&id
, msg_flags
,state
->private_data
);
362 bool serverid_traverse_read(int (*fn
)(const struct server_id
*id
,
363 uint32_t msg_flags
, void *private_data
),
366 struct db_context
*db
;
367 struct serverid_traverse_read_state state
;
375 state
.private_data
= private_data
;
377 status
= dbwrap_traverse_read(db
, serverid_traverse_read_fn
, &state
,
379 return NT_STATUS_IS_OK(status
);
382 struct serverid_traverse_state
{
383 int (*fn
)(struct db_record
*rec
, const struct server_id
*id
,
384 uint32_t msg_flags
, void *private_data
);
388 static int serverid_traverse_fn(struct db_record
*rec
, void *private_data
)
390 struct serverid_traverse_state
*state
=
391 (struct serverid_traverse_state
*)private_data
;
395 if (!serverid_rec_parse(rec
, &id
, &msg_flags
)) {
398 return state
->fn(rec
, &id
, msg_flags
, state
->private_data
);
401 bool serverid_traverse(int (*fn
)(struct db_record
*rec
,
402 const struct server_id
*id
,
403 uint32_t msg_flags
, void *private_data
),
406 struct db_context
*db
;
407 struct serverid_traverse_state state
;
415 state
.private_data
= private_data
;
417 status
= dbwrap_traverse(db
, serverid_traverse_fn
, &state
, NULL
);
418 return NT_STATUS_IS_OK(status
);