Fix headers, ldb_includes.h is a private header,
[Samba/bjacke.git] / source3 / lib / conn_tdb.c
blob22d85c873d3399c1791c5af15df9a216ea97eb16
1 /*
2 Unix SMB/CIFS implementation.
3 Low-level connections.tdb access functions
4 Copyright (C) Volker Lendecke 2007
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"
22 static struct db_context *connections_db_ctx(bool rw)
24 static struct db_context *db_ctx;
26 if (db_ctx != NULL) {
27 return db_ctx;
30 if (rw) {
31 db_ctx = db_open(NULL, lock_path("connections.tdb"), 0,
32 TDB_CLEAR_IF_FIRST|TDB_DEFAULT,
33 O_RDWR | O_CREAT, 0644);
35 else {
36 db_ctx = db_open(NULL, lock_path("connections.tdb"), 0,
37 TDB_CLEAR_IF_FIRST|TDB_DEFAULT, O_RDONLY, 0);
40 return db_ctx;
43 struct db_record *connections_fetch_record(TALLOC_CTX *mem_ctx,
44 TDB_DATA key)
46 struct db_context *ctx = connections_db_ctx(True);
48 if (ctx == NULL) {
49 return NULL;
52 return ctx->fetch_locked(ctx, mem_ctx, key);
55 struct db_record *connections_fetch_entry(TALLOC_CTX *mem_ctx,
56 connection_struct *conn,
57 const char *name)
59 struct connections_key ckey;
60 TDB_DATA key;
62 ZERO_STRUCT(ckey);
63 ckey.pid = procid_self();
64 ckey.cnum = conn ? conn->cnum : -1;
65 strlcpy(ckey.name, name, sizeof(ckey.name));
67 key.dsize = sizeof(ckey);
68 key.dptr = (uint8 *)&ckey;
70 return connections_fetch_record(mem_ctx, key);
73 struct conn_traverse_state {
74 int (*fn)(struct db_record *rec,
75 const struct connections_key *key,
76 const struct connections_data *data,
77 void *private_data);
78 void *private_data;
81 static int conn_traverse_fn(struct db_record *rec, void *private_data)
83 struct conn_traverse_state *state =
84 (struct conn_traverse_state *)private_data;
86 if ((rec->key.dsize != sizeof(struct connections_key))
87 || (rec->value.dsize != sizeof(struct connections_data))) {
88 return 0;
91 return state->fn(rec, (const struct connections_key *)rec->key.dptr,
92 (const struct connections_data *)rec->value.dptr,
93 state->private_data);
96 int connections_traverse(int (*fn)(struct db_record *rec,
97 void *private_data),
98 void *private_data)
100 struct db_context *ctx = connections_db_ctx(False);
102 if (ctx == NULL) {
103 return -1;
106 return ctx->traverse(ctx, fn, private_data);
109 int connections_forall(int (*fn)(struct db_record *rec,
110 const struct connections_key *key,
111 const struct connections_data *data,
112 void *private_data),
113 void *private_data)
115 struct conn_traverse_state state;
117 state.fn = fn;
118 state.private_data = private_data;
120 return connections_traverse(conn_traverse_fn, (void *)&state);
123 bool connections_init(bool rw)
125 return (connections_db_ctx(rw) != NULL);