s4:torture/rpc/samba3rpc.c: make use of dcerpc_binding_handle stubs
[Samba/nascimento.git] / source3 / lib / conn_tdb.c
blob0770f66828d3f75f782d057d1ad2bf53cca1a7b7
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;
25 int open_flags;
27 if (db_ctx != NULL) {
28 return 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);
35 return db_ctx;
38 static struct db_record *connections_fetch_record(TALLOC_CTX *mem_ctx,
39 TDB_DATA key)
41 struct db_context *ctx = connections_db_ctx(True);
43 if (ctx == NULL) {
44 return NULL;
47 return ctx->fetch_locked(ctx, mem_ctx, key);
50 struct db_record *connections_fetch_entry(TALLOC_CTX *mem_ctx,
51 connection_struct *conn,
52 const char *name)
54 struct connections_key ckey;
55 TDB_DATA key;
57 ZERO_STRUCT(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,
72 void *private_data);
73 void *private_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))) {
83 return 0;
86 return state->fn(rec, (const struct connections_key *)rec->key.dptr,
87 (const struct connections_data *)rec->value.dptr,
88 state->private_data);
91 int connections_traverse(int (*fn)(struct db_record *rec,
92 void *private_data),
93 void *private_data)
95 struct db_context *ctx = connections_db_ctx(False);
97 if (ctx == NULL) {
98 return -1;
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,
107 void *private_data),
108 void *private_data)
110 struct db_context *ctx;
111 struct conn_traverse_state state;
113 ctx = connections_db_ctx(true);
114 if (ctx == NULL) {
115 return -1;
118 state.fn = fn;
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,
127 void *private_data);
128 void *private_data;
131 static int connections_forall_read_fn(struct db_record *rec,
132 void *private_data)
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))) {
139 return 0;
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,
148 void *private_data),
149 void *private_data)
151 struct db_context *ctx;
152 struct conn_traverse_read_state state;
154 ctx = connections_db_ctx(false);
155 if (ctx == NULL) {
156 return -1;
159 state.fn = fn;
160 state.private_data = private_data;
162 return ctx->traverse_read(ctx, connections_forall_read_fn,
163 (void *)&state);
166 bool connections_init(bool rw)
168 return (connections_db_ctx(rw) != NULL);