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"
34 struct serverid_data
{
39 bool serverid_parent_init(TALLOC_CTX
*mem_ctx
)
44 * Open the tdb in the parent process (smbd) so that our
45 * CLEAR_IF_FIRST optimization in tdb_reopen_all can properly
49 db
= tdb_wrap_open(mem_ctx
, lock_path("serverid.tdb"),
50 0, TDB_DEFAULT
|TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
, O_RDWR
|O_CREAT
,
53 DEBUG(1, ("could not open serverid.tdb: %s\n",
60 static struct db_context
*serverid_db(void)
62 static struct db_context
*db
;
67 db
= db_open(NULL
, lock_path("serverid.tdb"), 0,
68 TDB_DEFAULT
|TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
, O_RDWR
|O_CREAT
, 0644);
72 static void serverid_fill_key(const struct server_id
*id
,
73 struct serverid_key
*key
)
77 key
->task_id
= id
->task_id
;
81 bool serverid_register(const struct server_id id
, uint32_t msg_flags
)
83 struct db_context
*db
;
84 struct serverid_key key
;
85 struct serverid_data data
;
86 struct db_record
*rec
;
87 TDB_DATA tdbkey
, tdbdata
;
96 serverid_fill_key(&id
, &key
);
97 tdbkey
= make_tdb_data((uint8_t *)&key
, sizeof(key
));
99 rec
= db
->fetch_locked(db
, talloc_tos(), tdbkey
);
101 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
106 data
.unique_id
= id
.unique_id
;
107 data
.msg_flags
= msg_flags
;
109 tdbdata
= make_tdb_data((uint8_t *)&data
, sizeof(data
));
110 status
= rec
->store(rec
, tdbdata
, 0);
111 if (!NT_STATUS_IS_OK(status
)) {
112 DEBUG(1, ("Storing serverid.tdb record failed: %s\n",
122 bool serverid_register_msg_flags(const struct server_id id
, bool do_reg
,
125 struct db_context
*db
;
126 struct serverid_key key
;
127 struct serverid_data
*data
;
128 struct db_record
*rec
;
138 serverid_fill_key(&id
, &key
);
139 tdbkey
= make_tdb_data((uint8_t *)&key
, sizeof(key
));
141 rec
= db
->fetch_locked(db
, talloc_tos(), tdbkey
);
143 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
147 if (rec
->value
.dsize
!= sizeof(struct serverid_data
)) {
148 DEBUG(1, ("serverid record has unexpected size %d "
149 "(wanted %d)\n", (int)rec
->value
.dsize
,
150 (int)sizeof(struct serverid_data
)));
154 data
= (struct serverid_data
*)rec
->value
.dptr
;
157 data
->msg_flags
|= msg_flags
;
159 data
->msg_flags
&= ~msg_flags
;
162 status
= rec
->store(rec
, rec
->value
, 0);
163 if (!NT_STATUS_IS_OK(status
)) {
164 DEBUG(1, ("Storing serverid.tdb record failed: %s\n",
174 bool serverid_deregister(struct server_id id
)
176 struct db_context
*db
;
177 struct serverid_key key
;
178 struct db_record
*rec
;
188 serverid_fill_key(&id
, &key
);
189 tdbkey
= make_tdb_data((uint8_t *)&key
, sizeof(key
));
191 rec
= db
->fetch_locked(db
, talloc_tos(), tdbkey
);
193 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
197 status
= rec
->delete_rec(rec
);
198 if (!NT_STATUS_IS_OK(status
)) {
199 DEBUG(1, ("Deleting serverid.tdb record failed: %s\n",
209 struct serverid_exists_state
{
210 const struct server_id
*id
;
214 static int server_exists_parse(TDB_DATA key
, TDB_DATA data
, void *priv
)
216 struct serverid_exists_state
*state
=
217 (struct serverid_exists_state
*)priv
;
219 if (data
.dsize
!= sizeof(struct serverid_data
)) {
224 * Use memcmp, not direct compare. data.dptr might not be
227 state
->exists
= (memcmp(&state
->id
->unique_id
, data
.dptr
,
228 sizeof(state
->id
->unique_id
)) == 0);
232 bool serverid_exists(const struct server_id
*id
)
234 struct db_context
*db
;
235 struct serverid_exists_state state
;
236 struct serverid_key key
;
239 if (procid_is_me(id
)) {
243 if (!process_exists(*id
)) {
252 serverid_fill_key(id
, &key
);
253 tdbkey
= make_tdb_data((uint8_t *)&key
, sizeof(key
));
256 state
.exists
= false;
258 if (db
->parse_record(db
, tdbkey
, server_exists_parse
, &state
) != 0) {
264 static bool serverid_rec_parse(const struct db_record
*rec
,
265 struct server_id
*id
, uint32_t *msg_flags
)
267 struct serverid_key key
;
268 struct serverid_data data
;
270 if (rec
->key
.dsize
!= sizeof(key
)) {
271 DEBUG(1, ("Found invalid key length %d in serverid.tdb\n",
272 (int)rec
->key
.dsize
));
275 if (rec
->value
.dsize
!= sizeof(data
)) {
276 DEBUG(1, ("Found invalid value length %d in serverid.tdb\n",
277 (int)rec
->value
.dsize
));
281 memcpy(&key
, rec
->key
.dptr
, sizeof(key
));
282 memcpy(&data
, rec
->value
.dptr
, sizeof(data
));
285 id
->task_id
= key
.task_id
;
287 id
->unique_id
= data
.unique_id
;
288 *msg_flags
= data
.msg_flags
;
292 struct serverid_traverse_read_state
{
293 int (*fn
)(const struct server_id
*id
, uint32_t msg_flags
,
298 static int serverid_traverse_read_fn(struct db_record
*rec
, void *private_data
)
300 struct serverid_traverse_read_state
*state
=
301 (struct serverid_traverse_read_state
*)private_data
;
305 if (!serverid_rec_parse(rec
, &id
, &msg_flags
)) {
308 return state
->fn(&id
, msg_flags
,state
->private_data
);
311 bool serverid_traverse_read(int (*fn
)(const struct server_id
*id
,
312 uint32_t msg_flags
, void *private_data
),
315 struct db_context
*db
;
316 struct serverid_traverse_read_state state
;
323 state
.private_data
= private_data
;
324 return db
->traverse_read(db
, serverid_traverse_read_fn
, &state
);
327 struct serverid_traverse_state
{
328 int (*fn
)(struct db_record
*rec
, const struct server_id
*id
,
329 uint32_t msg_flags
, void *private_data
);
333 static int serverid_traverse_fn(struct db_record
*rec
, void *private_data
)
335 struct serverid_traverse_state
*state
=
336 (struct serverid_traverse_state
*)private_data
;
340 if (!serverid_rec_parse(rec
, &id
, &msg_flags
)) {
343 return state
->fn(rec
, &id
, msg_flags
, state
->private_data
);
346 bool serverid_traverse(int (*fn
)(struct db_record
*rec
,
347 const struct server_id
*id
,
348 uint32_t msg_flags
, void *private_data
),
351 struct db_context
*db
;
352 struct serverid_traverse_state state
;
359 state
.private_data
= private_data
;
360 return db
->traverse(db
, serverid_traverse_fn
, &state
);