s3-waf: use SAMBA3_*() build rules in source3/build
[Samba.git] / source3 / lib / conn_tdb.c
blobe4c5e720144e7a2c19e2ab7fcc8c5dbd3a1eba8e
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"
21 #include "smbd/globals.h"
22 #include "dbwrap.h"
24 static struct db_context *connections_db_ctx(bool rw)
26 static struct db_context *db_ctx;
27 int open_flags;
29 if (db_ctx != NULL) {
30 return db_ctx;
33 open_flags = rw ? (O_RDWR|O_CREAT) : O_RDONLY;
35 db_ctx = db_open(NULL, lock_path("connections.tdb"), 0,
36 TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH|TDB_DEFAULT, open_flags, 0644);
37 return db_ctx;
40 static struct db_record *connections_fetch_record(TALLOC_CTX *mem_ctx,
41 TDB_DATA key)
43 struct db_context *ctx = connections_db_ctx(True);
45 if (ctx == NULL) {
46 return NULL;
49 return ctx->fetch_locked(ctx, mem_ctx, key);
52 struct db_record *connections_fetch_entry(TALLOC_CTX *mem_ctx,
53 connection_struct *conn,
54 const char *name)
56 struct connections_key ckey;
57 TDB_DATA key;
59 ZERO_STRUCT(ckey);
60 ckey.pid = sconn_server_id(conn->sconn);
61 ckey.cnum = conn->cnum;
62 strlcpy(ckey.name, name, sizeof(ckey.name));
64 key.dsize = sizeof(ckey);
65 key.dptr = (uint8 *)&ckey;
67 return connections_fetch_record(mem_ctx, key);
70 struct conn_traverse_state {
71 int (*fn)(struct db_record *rec,
72 const struct connections_key *key,
73 const struct connections_data *data,
74 void *private_data);
75 void *private_data;
78 static int conn_traverse_fn(struct db_record *rec, void *private_data)
80 struct conn_traverse_state *state =
81 (struct conn_traverse_state *)private_data;
83 if ((rec->key.dsize != sizeof(struct connections_key))
84 || (rec->value.dsize != sizeof(struct connections_data))) {
85 return 0;
88 return state->fn(rec, (const struct connections_key *)rec->key.dptr,
89 (const struct connections_data *)rec->value.dptr,
90 state->private_data);
93 int connections_traverse(int (*fn)(struct db_record *rec,
94 void *private_data),
95 void *private_data)
97 struct db_context *ctx = connections_db_ctx(False);
99 if (ctx == NULL) {
100 return -1;
103 return ctx->traverse(ctx, fn, private_data);
106 int connections_forall(int (*fn)(struct db_record *rec,
107 const struct connections_key *key,
108 const struct connections_data *data,
109 void *private_data),
110 void *private_data)
112 struct db_context *ctx;
113 struct conn_traverse_state state;
115 ctx = connections_db_ctx(true);
116 if (ctx == NULL) {
117 return -1;
120 state.fn = fn;
121 state.private_data = private_data;
123 return ctx->traverse(ctx, conn_traverse_fn, (void *)&state);
126 struct conn_traverse_read_state {
127 int (*fn)(const struct connections_key *key,
128 const struct connections_data *data,
129 void *private_data);
130 void *private_data;
133 static int connections_forall_read_fn(struct db_record *rec,
134 void *private_data)
136 struct conn_traverse_read_state *state =
137 (struct conn_traverse_read_state *)private_data;
139 if ((rec->key.dsize != sizeof(struct connections_key))
140 || (rec->value.dsize != sizeof(struct connections_data))) {
141 return 0;
143 return state->fn((const struct connections_key *)rec->key.dptr,
144 (const struct connections_data *)rec->value.dptr,
145 state->private_data);
148 int connections_forall_read(int (*fn)(const struct connections_key *key,
149 const struct connections_data *data,
150 void *private_data),
151 void *private_data)
153 struct db_context *ctx;
154 struct conn_traverse_read_state state;
156 ctx = connections_db_ctx(false);
157 if (ctx == NULL) {
158 return -1;
161 state.fn = fn;
162 state.private_data = private_data;
164 return ctx->traverse_read(ctx, connections_forall_read_fn,
165 (void *)&state);
168 bool connections_init(bool rw)
170 return (connections_db_ctx(rw) != NULL);