torture: fix whitespace/tab mixup in internal_torture_run_test()
[Samba.git] / source3 / lib / serverid.c
blobee479b4e68970f0b11cf973ae28ad373e1b4a58b
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 static struct db_context *serverid_db(void)
44 static struct db_context *db;
46 if (db != NULL) {
47 return db;
49 db = db_open(NULL, lock_path("serverid.tdb"), 0,
50 TDB_DEFAULT|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
51 O_RDWR|O_CREAT, 0644, DBWRAP_LOCK_ORDER_2,
52 DBWRAP_FLAG_NONE);
53 return db;
56 bool serverid_parent_init(TALLOC_CTX *mem_ctx)
58 struct db_context *db;
60 db = serverid_db();
61 if (db == NULL) {
62 DEBUG(1, ("could not open serverid.tdb: %s\n",
63 strerror(errno)));
64 return false;
67 return true;
70 static void serverid_fill_key(const struct server_id *id,
71 struct serverid_key *key)
73 ZERO_STRUCTP(key);
74 key->pid = id->pid;
75 key->task_id = id->task_id;
76 key->vnn = id->vnn;
79 bool serverid_register(const struct server_id id, uint32_t msg_flags)
81 struct db_context *db;
82 struct serverid_key key;
83 struct serverid_data data;
84 struct db_record *rec;
85 TDB_DATA tdbkey, tdbdata;
86 NTSTATUS status;
87 bool ret = false;
89 db = serverid_db();
90 if (db == NULL) {
91 return false;
94 serverid_fill_key(&id, &key);
95 tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));
97 rec = dbwrap_fetch_locked(db, talloc_tos(), tdbkey);
98 if (rec == NULL) {
99 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
100 return false;
103 ZERO_STRUCT(data);
104 data.unique_id = id.unique_id;
105 data.msg_flags = msg_flags;
107 tdbdata = make_tdb_data((uint8_t *)&data, sizeof(data));
108 status = dbwrap_record_store(rec, tdbdata, 0);
109 if (!NT_STATUS_IS_OK(status)) {
110 DEBUG(1, ("Storing serverid.tdb record failed: %s\n",
111 nt_errstr(status)));
112 goto done;
115 if (lp_clustering() &&
116 ctdb_serverids_exist_supported(messaging_ctdbd_connection()))
118 register_with_ctdbd(messaging_ctdbd_connection(), id.unique_id);
121 ret = true;
122 done:
123 TALLOC_FREE(rec);
124 return ret;
127 bool serverid_deregister(struct server_id id)
129 struct db_context *db;
130 struct serverid_key key;
131 struct db_record *rec;
132 TDB_DATA tdbkey;
133 NTSTATUS status;
134 bool ret = false;
136 db = serverid_db();
137 if (db == NULL) {
138 return false;
141 serverid_fill_key(&id, &key);
142 tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));
144 rec = dbwrap_fetch_locked(db, talloc_tos(), tdbkey);
145 if (rec == NULL) {
146 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
147 return false;
150 status = dbwrap_record_delete(rec);
151 if (!NT_STATUS_IS_OK(status)) {
152 DEBUG(1, ("Deleting serverid.tdb record failed: %s\n",
153 nt_errstr(status)));
154 goto done;
156 ret = true;
157 done:
158 TALLOC_FREE(rec);
159 return ret;
162 struct serverid_exists_state {
163 const struct server_id *id;
164 bool exists;
167 static void server_exists_parse(TDB_DATA key, TDB_DATA data, void *priv)
169 struct serverid_exists_state *state =
170 (struct serverid_exists_state *)priv;
172 if (data.dsize != sizeof(struct serverid_data)) {
173 state->exists = false;
174 return;
178 * Use memcmp, not direct compare. data.dptr might not be
179 * aligned.
181 state->exists = (memcmp(&state->id->unique_id, data.dptr,
182 sizeof(state->id->unique_id)) == 0);
185 bool serverid_exists(const struct server_id *id)
187 bool result = false;
188 bool ok = false;
190 ok = serverids_exist(id, 1, &result);
191 if (!ok) {
192 return false;
195 return result;
198 bool serverids_exist(const struct server_id *ids, int num_ids, bool *results)
200 int *todo_idx = NULL;
201 struct server_id *todo_ids = NULL;
202 bool *todo_results = NULL;
203 int todo_num = 0;
204 int *remote_idx = NULL;
205 int remote_num = 0;
206 int *verify_idx = NULL;
207 int verify_num = 0;
208 int t, idx;
209 bool result = false;
210 struct db_context *db;
212 db = serverid_db();
213 if (db == NULL) {
214 return false;
217 todo_idx = talloc_array(talloc_tos(), int, num_ids);
218 if (todo_idx == NULL) {
219 goto fail;
221 todo_ids = talloc_array(talloc_tos(), struct server_id, num_ids);
222 if (todo_ids == NULL) {
223 goto fail;
225 todo_results = talloc_array(talloc_tos(), bool, num_ids);
226 if (todo_results == NULL) {
227 goto fail;
230 remote_idx = talloc_array(talloc_tos(), int, num_ids);
231 if (remote_idx == NULL) {
232 goto fail;
234 verify_idx = talloc_array(talloc_tos(), int, num_ids);
235 if (verify_idx == NULL) {
236 goto fail;
239 for (idx=0; idx<num_ids; idx++) {
240 results[idx] = false;
242 if (server_id_is_disconnected(&ids[idx])) {
243 continue;
246 if (procid_is_me(&ids[idx])) {
247 results[idx] = true;
248 continue;
251 if (procid_is_local(&ids[idx])) {
252 bool exists = process_exists_by_pid(ids[idx].pid);
254 if (!exists) {
255 continue;
258 if (ids[idx].unique_id == SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
259 results[idx] = true;
260 continue;
263 verify_idx[verify_num] = idx;
264 verify_num += 1;
265 continue;
268 if (!lp_clustering()) {
269 continue;
272 remote_idx[remote_num] = idx;
273 remote_num += 1;
276 if (remote_num != 0 &&
277 ctdb_serverids_exist_supported(messaging_ctdbd_connection()))
279 int old_remote_num = remote_num;
281 remote_num = 0;
282 todo_num = 0;
284 for (t=0; t<old_remote_num; t++) {
285 idx = remote_idx[t];
287 if (ids[idx].unique_id == SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
288 remote_idx[remote_num] = idx;
289 remote_num += 1;
290 continue;
293 todo_idx[todo_num] = idx;
294 todo_ids[todo_num] = ids[idx];
295 todo_results[todo_num] = false;
296 todo_num += 1;
300 * Note: this only uses CTDB_CONTROL_CHECK_SRVIDS
301 * to verify that the server_id still exists,
302 * which means only the server_id.unique_id and
303 * server_id.vnn are verified, while server_id.pid
304 * is not verified at all.
306 * TODO: do we want to verify server_id.pid somehow?
308 if (!ctdb_serverids_exist(messaging_ctdbd_connection(),
309 todo_ids, todo_num, todo_results))
311 goto fail;
314 for (t=0; t<todo_num; t++) {
315 idx = todo_idx[t];
317 results[idx] = todo_results[t];
321 if (remote_num != 0) {
322 todo_num = 0;
324 for (t=0; t<remote_num; t++) {
325 idx = remote_idx[t];
326 todo_idx[todo_num] = idx;
327 todo_ids[todo_num] = ids[idx];
328 todo_results[todo_num] = false;
329 todo_num += 1;
332 if (!ctdb_processes_exist(messaging_ctdbd_connection(),
333 todo_ids, todo_num,
334 todo_results)) {
335 goto fail;
338 for (t=0; t<todo_num; t++) {
339 idx = todo_idx[t];
341 if (!todo_results[t]) {
342 continue;
345 if (ids[idx].unique_id == SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
346 results[idx] = true;
347 continue;
350 verify_idx[verify_num] = idx;
351 verify_num += 1;
355 for (t=0; t<verify_num; t++) {
356 struct serverid_exists_state state;
357 struct serverid_key key;
358 TDB_DATA tdbkey;
359 NTSTATUS status;
361 idx = verify_idx[t];
363 serverid_fill_key(&ids[idx], &key);
364 tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));
366 state.id = &ids[idx];
367 state.exists = false;
368 status = dbwrap_parse_record(db, tdbkey, server_exists_parse, &state);
369 if (!NT_STATUS_IS_OK(status)) {
370 results[idx] = false;
371 continue;
373 results[idx] = state.exists;
376 result = true;
377 fail:
378 TALLOC_FREE(verify_idx);
379 TALLOC_FREE(remote_idx);
380 TALLOC_FREE(todo_results);
381 TALLOC_FREE(todo_ids);
382 TALLOC_FREE(todo_idx);
383 return result;
386 static bool serverid_rec_parse(const struct db_record *rec,
387 struct server_id *id, uint32_t *msg_flags)
389 struct serverid_key key;
390 struct serverid_data data;
391 TDB_DATA tdbkey;
392 TDB_DATA tdbdata;
394 tdbkey = dbwrap_record_get_key(rec);
395 tdbdata = dbwrap_record_get_value(rec);
397 if (tdbkey.dsize != sizeof(key)) {
398 DEBUG(1, ("Found invalid key length %d in serverid.tdb\n",
399 (int)tdbkey.dsize));
400 return false;
402 if (tdbdata.dsize != sizeof(data)) {
403 DEBUG(1, ("Found invalid value length %d in serverid.tdb\n",
404 (int)tdbdata.dsize));
405 return false;
408 memcpy(&key, tdbkey.dptr, sizeof(key));
409 memcpy(&data, tdbdata.dptr, sizeof(data));
411 id->pid = key.pid;
412 id->task_id = key.task_id;
413 id->vnn = key.vnn;
414 id->unique_id = data.unique_id;
415 *msg_flags = data.msg_flags;
416 return true;
419 struct serverid_traverse_read_state {
420 int (*fn)(const struct server_id *id, uint32_t msg_flags,
421 void *private_data);
422 void *private_data;
425 static int serverid_traverse_read_fn(struct db_record *rec, void *private_data)
427 struct serverid_traverse_read_state *state =
428 (struct serverid_traverse_read_state *)private_data;
429 struct server_id id;
430 uint32_t msg_flags;
432 if (!serverid_rec_parse(rec, &id, &msg_flags)) {
433 return 0;
435 return state->fn(&id, msg_flags,state->private_data);
438 bool serverid_traverse_read(int (*fn)(const struct server_id *id,
439 uint32_t msg_flags, void *private_data),
440 void *private_data)
442 struct db_context *db;
443 struct serverid_traverse_read_state state;
444 NTSTATUS status;
446 db = serverid_db();
447 if (db == NULL) {
448 return false;
450 state.fn = fn;
451 state.private_data = private_data;
453 status = dbwrap_traverse_read(db, serverid_traverse_read_fn, &state,
454 NULL);
455 return NT_STATUS_IS_OK(status);
458 struct serverid_traverse_state {
459 int (*fn)(struct db_record *rec, const struct server_id *id,
460 uint32_t msg_flags, void *private_data);
461 void *private_data;
464 static int serverid_traverse_fn(struct db_record *rec, void *private_data)
466 struct serverid_traverse_state *state =
467 (struct serverid_traverse_state *)private_data;
468 struct server_id id;
469 uint32_t msg_flags;
471 if (!serverid_rec_parse(rec, &id, &msg_flags)) {
472 return 0;
474 return state->fn(rec, &id, msg_flags, state->private_data);
477 bool serverid_traverse(int (*fn)(struct db_record *rec,
478 const struct server_id *id,
479 uint32_t msg_flags, void *private_data),
480 void *private_data)
482 struct db_context *db;
483 struct serverid_traverse_state state;
484 NTSTATUS status;
486 db = serverid_db();
487 if (db == NULL) {
488 return false;
490 state.fn = fn;
491 state.private_data = private_data;
493 status = dbwrap_traverse(db, serverid_traverse_fn, &state, NULL);
494 return NT_STATUS_IS_OK(status);
497 uint64_t serverid_get_random_unique_id(void)
499 uint64_t unique_id = SERVERID_UNIQUE_ID_NOT_TO_VERIFY;
501 while (unique_id == SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
502 generate_random_buffer((uint8_t *)&unique_id,
503 sizeof(unique_id));
506 return unique_id;