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/>.
21 #include "smbd/globals.h"
24 static struct db_context
*connections_db_ctx(bool rw
)
26 static struct db_context
*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);
40 static struct db_record
*connections_fetch_record(TALLOC_CTX
*mem_ctx
,
43 struct db_context
*ctx
= connections_db_ctx(True
);
49 return ctx
->fetch_locked(ctx
, mem_ctx
, key
);
52 struct db_record
*connections_fetch_entry(TALLOC_CTX
*mem_ctx
,
53 connection_struct
*conn
,
56 struct connections_key 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
,
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
))) {
88 return state
->fn(rec
, (const struct connections_key
*)rec
->key
.dptr
,
89 (const struct connections_data
*)rec
->value
.dptr
,
93 int connections_traverse(int (*fn
)(struct db_record
*rec
,
97 struct db_context
*ctx
= connections_db_ctx(False
);
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
,
112 struct db_context
*ctx
;
113 struct conn_traverse_state state
;
115 ctx
= connections_db_ctx(true);
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
,
133 static int connections_forall_read_fn(struct db_record
*rec
,
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
))) {
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
,
153 struct db_context
*ctx
;
154 struct conn_traverse_read_state state
;
156 ctx
= connections_db_ctx(false);
162 state
.private_data
= private_data
;
164 return ctx
->traverse_read(ctx
, connections_forall_read_fn
,
168 bool connections_init(bool rw
)
170 return (connections_db_ctx(rw
) != NULL
);