s3:lib: use server_id_is_disconnected() in serverids_exist()
[Samba/bjacke.git] / source3 / lib / serverid.c
blob61860977cf17da1d2974e56e4f3309d7f37a42f8
1 /*
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/>.
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "serverid.h"
23 #include "util_tdb.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"
29 #include "messages.h"
31 struct serverid_key {
32 pid_t pid;
33 uint32_t task_id;
34 uint32_t vnn;
37 struct serverid_data {
38 uint64_t unique_id;
39 uint32_t msg_flags;
42 bool serverid_parent_init(TALLOC_CTX *mem_ctx)
44 struct tdb_wrap *db;
45 struct loadparm_context *lp_ctx;
47 lp_ctx = loadparm_init_s3(mem_ctx, loadparm_s3_helpers());
48 if (lp_ctx == NULL) {
49 DEBUG(0, ("loadparm_init_s3 failed\n"));
50 return false;
54 * Open the tdb in the parent process (smbd) so that our
55 * CLEAR_IF_FIRST optimization in tdb_reopen_all can properly
56 * work.
59 db = tdb_wrap_open(mem_ctx, lock_path("serverid.tdb"),
60 0, TDB_DEFAULT|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH, O_RDWR|O_CREAT,
61 0644, lp_ctx);
62 talloc_unlink(mem_ctx, lp_ctx);
63 if (db == NULL) {
64 DEBUG(1, ("could not open serverid.tdb: %s\n",
65 strerror(errno)));
66 return false;
68 return true;
71 static struct db_context *serverid_db(void)
73 static struct db_context *db;
75 if (db != NULL) {
76 return db;
78 db = db_open(NULL, lock_path("serverid.tdb"), 0,
79 TDB_DEFAULT|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
80 O_RDWR|O_CREAT, 0644, DBWRAP_LOCK_ORDER_2);
81 return db;
84 static void serverid_fill_key(const struct server_id *id,
85 struct serverid_key *key)
87 ZERO_STRUCTP(key);
88 key->pid = id->pid;
89 key->task_id = id->task_id;
90 key->vnn = id->vnn;
93 bool serverid_register(const struct server_id id, uint32_t msg_flags)
95 struct db_context *db;
96 struct serverid_key key;
97 struct serverid_data data;
98 struct db_record *rec;
99 TDB_DATA tdbkey, tdbdata;
100 NTSTATUS status;
101 bool ret = false;
103 db = serverid_db();
104 if (db == NULL) {
105 return false;
108 serverid_fill_key(&id, &key);
109 tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));
111 rec = dbwrap_fetch_locked(db, talloc_tos(), tdbkey);
112 if (rec == NULL) {
113 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
114 return false;
117 ZERO_STRUCT(data);
118 data.unique_id = id.unique_id;
119 data.msg_flags = msg_flags;
121 tdbdata = make_tdb_data((uint8_t *)&data, sizeof(data));
122 status = dbwrap_record_store(rec, tdbdata, 0);
123 if (!NT_STATUS_IS_OK(status)) {
124 DEBUG(1, ("Storing serverid.tdb record failed: %s\n",
125 nt_errstr(status)));
126 goto done;
128 #ifdef HAVE_CTDB_CONTROL_CHECK_SRVIDS_DECL
129 if (lp_clustering()) {
130 register_with_ctdbd(messaging_ctdbd_connection(), id.unique_id);
132 #endif
133 ret = true;
134 done:
135 TALLOC_FREE(rec);
136 return ret;
139 bool serverid_register_msg_flags(const struct server_id id, bool do_reg,
140 uint32_t msg_flags)
142 struct db_context *db;
143 struct serverid_key key;
144 struct serverid_data *data;
145 struct db_record *rec;
146 TDB_DATA tdbkey;
147 TDB_DATA value;
148 NTSTATUS status;
149 bool ret = false;
151 db = serverid_db();
152 if (db == NULL) {
153 return false;
156 serverid_fill_key(&id, &key);
157 tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));
159 rec = dbwrap_fetch_locked(db, talloc_tos(), tdbkey);
160 if (rec == NULL) {
161 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
162 return false;
165 value = dbwrap_record_get_value(rec);
167 if (value.dsize != sizeof(struct serverid_data)) {
168 DEBUG(1, ("serverid record has unexpected size %d "
169 "(wanted %d)\n", (int)value.dsize,
170 (int)sizeof(struct serverid_data)));
171 goto done;
174 data = (struct serverid_data *)value.dptr;
176 if (do_reg) {
177 data->msg_flags |= msg_flags;
178 } else {
179 data->msg_flags &= ~msg_flags;
182 status = dbwrap_record_store(rec, value, 0);
183 if (!NT_STATUS_IS_OK(status)) {
184 DEBUG(1, ("Storing serverid.tdb record failed: %s\n",
185 nt_errstr(status)));
186 goto done;
188 ret = true;
189 done:
190 TALLOC_FREE(rec);
191 return ret;
194 bool serverid_deregister(struct server_id id)
196 struct db_context *db;
197 struct serverid_key key;
198 struct db_record *rec;
199 TDB_DATA tdbkey;
200 NTSTATUS status;
201 bool ret = false;
203 db = serverid_db();
204 if (db == NULL) {
205 return false;
208 serverid_fill_key(&id, &key);
209 tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));
211 rec = dbwrap_fetch_locked(db, talloc_tos(), tdbkey);
212 if (rec == NULL) {
213 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
214 return false;
217 status = dbwrap_record_delete(rec);
218 if (!NT_STATUS_IS_OK(status)) {
219 DEBUG(1, ("Deleting serverid.tdb record failed: %s\n",
220 nt_errstr(status)));
221 goto done;
223 ret = true;
224 done:
225 TALLOC_FREE(rec);
226 return ret;
229 struct serverid_exists_state {
230 const struct server_id *id;
231 bool exists;
234 static void server_exists_parse(TDB_DATA key, TDB_DATA data, void *priv)
236 struct serverid_exists_state *state =
237 (struct serverid_exists_state *)priv;
239 if (data.dsize != sizeof(struct serverid_data)) {
240 state->exists = false;
241 return;
245 * Use memcmp, not direct compare. data.dptr might not be
246 * aligned.
248 state->exists = (memcmp(&state->id->unique_id, data.dptr,
249 sizeof(state->id->unique_id)) == 0);
252 bool serverid_exists(const struct server_id *id)
254 bool result = false;
255 bool ok = false;
257 ok = serverids_exist(id, 1, &result);
258 if (!ok) {
259 return false;
262 return result;
265 bool serverids_exist(const struct server_id *ids, int num_ids, bool *results)
267 int *todo_idx = NULL;
268 struct server_id *todo_ids = NULL;
269 bool *todo_results = NULL;
270 int todo_num = 0;
271 int *remote_idx = NULL;
272 int remote_num = 0;
273 int t, idx;
274 bool result = false;
275 struct db_context *db;
277 db = serverid_db();
278 if (db == NULL) {
279 return false;
282 todo_idx = talloc_array(talloc_tos(), int, num_ids);
283 if (todo_idx == NULL) {
284 goto fail;
286 todo_ids = talloc_array(talloc_tos(), struct server_id, num_ids);
287 if (todo_ids == NULL) {
288 goto fail;
290 todo_results = talloc_array(talloc_tos(), bool, num_ids);
291 if (todo_results == NULL) {
292 goto fail;
295 remote_idx = talloc_array(talloc_tos(), int, num_ids);
296 if (remote_idx == NULL) {
297 goto fail;
300 for (idx=0; idx<num_ids; idx++) {
301 results[idx] = false;
303 if (server_id_is_disconnected(&ids[idx])) {
304 continue;
307 if (procid_is_me(&ids[idx])) {
308 results[idx] = true;
309 continue;
312 if (procid_is_local(&ids[idx])) {
313 bool exists = process_exists_by_pid(ids[idx].pid);
315 if (!exists) {
316 continue;
319 results[idx] = true;
320 continue;
323 if (!lp_clustering()) {
324 continue;
327 remote_idx[remote_num] = idx;
328 remote_num += 1;
331 if (remote_num != 0) {
332 todo_num = 0;
334 for (t=0; t<remote_num; t++) {
335 idx = remote_idx[t];
336 todo_idx[todo_num] = idx;
337 todo_ids[todo_num] = ids[idx];
338 todo_results[todo_num] = false;
339 todo_num += 1;
342 #ifdef CLUSTER_SUPPORT
343 if (!ctdb_processes_exist(messaging_ctdbd_connection(),
344 todo_ids, todo_num,
345 todo_results)) {
346 goto fail;
348 #endif
350 for (t=0; t<todo_num; t++) {
351 idx = todo_idx[t];
353 if (!todo_results[t]) {
354 continue;
357 results[idx] = true;
361 for (idx=0; idx<num_ids; idx++) {
362 struct serverid_exists_state state;
363 struct serverid_key key;
364 TDB_DATA tdbkey;
365 NTSTATUS status;
367 if (!results[idx]) {
368 continue;
371 if (ids[idx].unique_id == SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
372 results[idx] = true;
373 continue;
376 serverid_fill_key(&ids[idx], &key);
377 tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));
379 state.id = &ids[idx];
380 state.exists = false;
381 status = dbwrap_parse_record(db, tdbkey, server_exists_parse, &state);
382 if (!NT_STATUS_IS_OK(status)) {
383 results[idx] = false;
384 continue;
386 results[idx] = state.exists;
389 result = true;
390 fail:
391 TALLOC_FREE(remote_idx);
392 TALLOC_FREE(todo_results);
393 TALLOC_FREE(todo_ids);
394 TALLOC_FREE(todo_idx);
395 return result;
398 static bool serverid_rec_parse(const struct db_record *rec,
399 struct server_id *id, uint32_t *msg_flags)
401 struct serverid_key key;
402 struct serverid_data data;
403 TDB_DATA tdbkey;
404 TDB_DATA tdbdata;
406 tdbkey = dbwrap_record_get_key(rec);
407 tdbdata = dbwrap_record_get_value(rec);
409 if (tdbkey.dsize != sizeof(key)) {
410 DEBUG(1, ("Found invalid key length %d in serverid.tdb\n",
411 (int)tdbkey.dsize));
412 return false;
414 if (tdbdata.dsize != sizeof(data)) {
415 DEBUG(1, ("Found invalid value length %d in serverid.tdb\n",
416 (int)tdbdata.dsize));
417 return false;
420 memcpy(&key, tdbkey.dptr, sizeof(key));
421 memcpy(&data, tdbdata.dptr, sizeof(data));
423 id->pid = key.pid;
424 id->task_id = key.task_id;
425 id->vnn = key.vnn;
426 id->unique_id = data.unique_id;
427 *msg_flags = data.msg_flags;
428 return true;
431 struct serverid_traverse_read_state {
432 int (*fn)(const struct server_id *id, uint32_t msg_flags,
433 void *private_data);
434 void *private_data;
437 static int serverid_traverse_read_fn(struct db_record *rec, void *private_data)
439 struct serverid_traverse_read_state *state =
440 (struct serverid_traverse_read_state *)private_data;
441 struct server_id id;
442 uint32_t msg_flags;
444 if (!serverid_rec_parse(rec, &id, &msg_flags)) {
445 return 0;
447 return state->fn(&id, msg_flags,state->private_data);
450 bool serverid_traverse_read(int (*fn)(const struct server_id *id,
451 uint32_t msg_flags, void *private_data),
452 void *private_data)
454 struct db_context *db;
455 struct serverid_traverse_read_state state;
456 NTSTATUS status;
458 db = serverid_db();
459 if (db == NULL) {
460 return false;
462 state.fn = fn;
463 state.private_data = private_data;
465 status = dbwrap_traverse_read(db, serverid_traverse_read_fn, &state,
466 NULL);
467 return NT_STATUS_IS_OK(status);
470 struct serverid_traverse_state {
471 int (*fn)(struct db_record *rec, const struct server_id *id,
472 uint32_t msg_flags, void *private_data);
473 void *private_data;
476 static int serverid_traverse_fn(struct db_record *rec, void *private_data)
478 struct serverid_traverse_state *state =
479 (struct serverid_traverse_state *)private_data;
480 struct server_id id;
481 uint32_t msg_flags;
483 if (!serverid_rec_parse(rec, &id, &msg_flags)) {
484 return 0;
486 return state->fn(rec, &id, msg_flags, state->private_data);
489 bool serverid_traverse(int (*fn)(struct db_record *rec,
490 const struct server_id *id,
491 uint32_t msg_flags, void *private_data),
492 void *private_data)
494 struct db_context *db;
495 struct serverid_traverse_state state;
496 NTSTATUS status;
498 db = serverid_db();
499 if (db == NULL) {
500 return false;
502 state.fn = fn;
503 state.private_data = private_data;
505 status = dbwrap_traverse(db, serverid_traverse_fn, &state, NULL);
506 return NT_STATUS_IS_OK(status);
509 uint64_t serverid_get_random_unique_id(void)
511 uint64_t unique_id = SERVERID_UNIQUE_ID_NOT_TO_VERIFY;
513 while (unique_id == SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
514 generate_random_buffer((uint8_t *)&unique_id,
515 sizeof(unique_id));
518 return unique_id;