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/>.
22 static struct db_context *connections_db_ctx(bool rw)
24 static struct db_context *db_ctx;
31 open_flags = rw ? (O_RDWR|O_CREAT) : O_RDONLY;
33 db_ctx = db_open(NULL, lock_path("connections.tdb"), 0,
34 TDB_CLEAR_IF_FIRST|TDB_DEFAULT, open_flags, 0644);
38 static struct db_record *connections_fetch_record(TALLOC_CTX *mem_ctx,
41 struct db_context *ctx = connections_db_ctx(True);
47 return ctx->fetch_locked(ctx, mem_ctx, key);
50 struct db_record *connections_fetch_entry(TALLOC_CTX *mem_ctx,
51 connection_struct *conn,
54 struct connections_key ckey;
58 ckey.pid = procid_self();
59 ckey.cnum = conn ? conn->cnum : -1;
60 strlcpy(ckey.name, name, sizeof(ckey.name));
62 key.dsize = sizeof(ckey);
63 key.dptr = (uint8 *)&ckey;
65 return connections_fetch_record(mem_ctx, key);
68 struct conn_traverse_state {
69 int (*fn)(struct db_record *rec,
70 const struct connections_key *key,
71 const struct connections_data *data,
76 static int conn_traverse_fn(struct db_record *rec, void *private_data)
78 struct conn_traverse_state *state =
79 (struct conn_traverse_state *)private_data;
81 if ((rec->key.dsize != sizeof(struct connections_key))
82 || (rec->value.dsize != sizeof(struct connections_data))) {
86 return state->fn(rec, (const struct connections_key *)rec->key.dptr,
87 (const struct connections_data *)rec->value.dptr,
91 int connections_traverse(int (*fn)(struct db_record *rec,
95 struct db_context *ctx = connections_db_ctx(False);
101 return ctx->traverse(ctx, fn, private_data);
104 int connections_forall(int (*fn)(struct db_record *rec,
105 const struct connections_key *key,
106 const struct connections_data *data,
110 struct db_context *ctx;
111 struct conn_traverse_state state;
113 ctx = connections_db_ctx(true);
119 state.private_data = private_data;
121 return ctx->traverse(ctx, conn_traverse_fn, (void *)&state);
124 struct conn_traverse_read_state {
125 int (*fn)(const struct connections_key *key,
126 const struct connections_data *data,
131 static int connections_forall_read_fn(struct db_record *rec,
134 struct conn_traverse_read_state *state =
135 (struct conn_traverse_read_state *)private_data;
137 if ((rec->key.dsize != sizeof(struct connections_key))
138 || (rec->value.dsize != sizeof(struct connections_data))) {
141 return state->fn((const struct connections_key *)rec->key.dptr,
142 (const struct connections_data *)rec->value.dptr,
143 state->private_data);
146 int connections_forall_read(int (*fn)(const struct connections_key *key,
147 const struct connections_data *data,
151 struct db_context *ctx;
152 struct conn_traverse_read_state state;
154 ctx = connections_db_ctx(false);
160 state.private_data = private_data;
162 return ctx->traverse_read(ctx, connections_forall_read_fn,
166 bool connections_init(bool rw)
168 return (connections_db_ctx(rw) != NULL);