s4-dsdb Allow repl server to start even when no master NCs are present
[Samba/gebeck_regimport.git] / source3 / lib / serverid.c
blob7e56301bf2ba34d87b92d037c08905d8601b8f89
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/util/tdb_wrap.h"
28 struct serverid_key {
29 pid_t pid;
30 uint32_t task_id;
31 uint32_t vnn;
34 struct serverid_data {
35 uint64_t unique_id;
36 uint32_t msg_flags;
39 bool serverid_parent_init(TALLOC_CTX *mem_ctx)
41 struct tdb_wrap *db;
44 * Open the tdb in the parent process (smbd) so that our
45 * CLEAR_IF_FIRST optimization in tdb_reopen_all can properly
46 * work.
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,
51 0644);
52 if (db == NULL) {
53 DEBUG(1, ("could not open serverid.tdb: %s\n",
54 strerror(errno)));
55 return false;
57 return true;
60 static struct db_context *serverid_db(void)
62 static struct db_context *db;
64 if (db != NULL) {
65 return 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);
69 return db;
72 static void serverid_fill_key(const struct server_id *id,
73 struct serverid_key *key)
75 ZERO_STRUCTP(key);
76 key->pid = id->pid;
77 key->task_id = id->task_id;
78 key->vnn = id->vnn;
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;
88 NTSTATUS status;
89 bool ret = false;
91 db = serverid_db();
92 if (db == NULL) {
93 return false;
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);
100 if (rec == NULL) {
101 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
102 return false;
105 ZERO_STRUCT(data);
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",
113 nt_errstr(status)));
114 goto done;
116 ret = true;
117 done:
118 TALLOC_FREE(rec);
119 return ret;
122 bool serverid_register_msg_flags(const struct server_id id, bool do_reg,
123 uint32_t msg_flags)
125 struct db_context *db;
126 struct serverid_key key;
127 struct serverid_data *data;
128 struct db_record *rec;
129 TDB_DATA tdbkey;
130 NTSTATUS status;
131 bool ret = false;
133 db = serverid_db();
134 if (db == NULL) {
135 return false;
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);
142 if (rec == NULL) {
143 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
144 return false;
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)));
151 goto done;
154 data = (struct serverid_data *)rec->value.dptr;
156 if (do_reg) {
157 data->msg_flags |= msg_flags;
158 } else {
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",
165 nt_errstr(status)));
166 goto done;
168 ret = true;
169 done:
170 TALLOC_FREE(rec);
171 return ret;
174 bool serverid_deregister(struct server_id id)
176 struct db_context *db;
177 struct serverid_key key;
178 struct db_record *rec;
179 TDB_DATA tdbkey;
180 NTSTATUS status;
181 bool ret = false;
183 db = serverid_db();
184 if (db == NULL) {
185 return false;
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);
192 if (rec == NULL) {
193 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
194 return false;
197 status = rec->delete_rec(rec);
198 if (!NT_STATUS_IS_OK(status)) {
199 DEBUG(1, ("Deleting serverid.tdb record failed: %s\n",
200 nt_errstr(status)));
201 goto done;
203 ret = true;
204 done:
205 TALLOC_FREE(rec);
206 return ret;
209 struct serverid_exists_state {
210 const struct server_id *id;
211 bool exists;
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)) {
220 return -1;
224 * Use memcmp, not direct compare. data.dptr might not be
225 * aligned.
227 state->exists = (memcmp(&state->id->unique_id, data.dptr,
228 sizeof(state->id->unique_id)) == 0);
229 return 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;
237 TDB_DATA tdbkey;
239 if (procid_is_me(id)) {
240 return true;
243 if (!process_exists(*id)) {
244 return false;
247 db = serverid_db();
248 if (db == NULL) {
249 return false;
252 serverid_fill_key(id, &key);
253 tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));
255 state.id = id;
256 state.exists = false;
258 if (db->parse_record(db, tdbkey, server_exists_parse, &state) != 0) {
259 return false;
261 return state.exists;
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));
273 return false;
275 if (rec->value.dsize != sizeof(data)) {
276 DEBUG(1, ("Found invalid value length %d in serverid.tdb\n",
277 (int)rec->value.dsize));
278 return false;
281 memcpy(&key, rec->key.dptr, sizeof(key));
282 memcpy(&data, rec->value.dptr, sizeof(data));
284 id->pid = key.pid;
285 id->task_id = key.task_id;
286 id->vnn = key.vnn;
287 id->unique_id = data.unique_id;
288 *msg_flags = data.msg_flags;
289 return true;
292 struct serverid_traverse_read_state {
293 int (*fn)(const struct server_id *id, uint32_t msg_flags,
294 void *private_data);
295 void *private_data;
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;
302 struct server_id id;
303 uint32_t msg_flags;
305 if (!serverid_rec_parse(rec, &id, &msg_flags)) {
306 return 0;
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),
313 void *private_data)
315 struct db_context *db;
316 struct serverid_traverse_read_state state;
318 db = serverid_db();
319 if (db == NULL) {
320 return false;
322 state.fn = fn;
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);
330 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;
337 struct server_id id;
338 uint32_t msg_flags;
340 if (!serverid_rec_parse(rec, &id, &msg_flags)) {
341 return 0;
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),
349 void *private_data)
351 struct db_context *db;
352 struct serverid_traverse_state state;
354 db = serverid_db();
355 if (db == NULL) {
356 return false;
358 state.fn = fn;
359 state.private_data = private_data;
360 return db->traverse(db, serverid_traverse_fn, &state);