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 "system/filesys.h"
22 #include "smbd/globals.h"
25 static struct db_context
*connections_db_ctx(bool rw
)
27 static struct db_context
*db_ctx
;
34 open_flags
= rw
? (O_RDWR
|O_CREAT
) : O_RDONLY
;
36 db_ctx
= db_open(NULL
, lock_path("connections.tdb"), 0,
37 TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
|TDB_DEFAULT
, open_flags
, 0644);
41 static struct db_record
*connections_fetch_record(TALLOC_CTX
*mem_ctx
,
44 struct db_context
*ctx
= connections_db_ctx(True
);
50 return ctx
->fetch_locked(ctx
, mem_ctx
, key
);
53 struct db_record
*connections_fetch_entry(TALLOC_CTX
*mem_ctx
,
54 connection_struct
*conn
,
57 struct connections_key ckey
;
61 ckey
.pid
= sconn_server_id(conn
->sconn
);
62 ckey
.cnum
= conn
->cnum
;
63 strlcpy(ckey
.name
, name
, sizeof(ckey
.name
));
65 key
.dsize
= sizeof(ckey
);
66 key
.dptr
= (uint8
*)&ckey
;
68 return connections_fetch_record(mem_ctx
, key
);
71 struct conn_traverse_state
{
72 int (*fn
)(struct db_record
*rec
,
73 const struct connections_key
*key
,
74 const struct connections_data
*data
,
79 static int conn_traverse_fn(struct db_record
*rec
, void *private_data
)
81 struct conn_traverse_state
*state
=
82 (struct conn_traverse_state
*)private_data
;
84 if ((rec
->key
.dsize
!= sizeof(struct connections_key
))
85 || (rec
->value
.dsize
!= sizeof(struct connections_data
))) {
89 return state
->fn(rec
, (const struct connections_key
*)rec
->key
.dptr
,
90 (const struct connections_data
*)rec
->value
.dptr
,
94 int connections_traverse(int (*fn
)(struct db_record
*rec
,
98 struct db_context
*ctx
= connections_db_ctx(False
);
104 return ctx
->traverse(ctx
, fn
, private_data
);
107 int connections_forall(int (*fn
)(struct db_record
*rec
,
108 const struct connections_key
*key
,
109 const struct connections_data
*data
,
113 struct db_context
*ctx
;
114 struct conn_traverse_state state
;
116 ctx
= connections_db_ctx(true);
122 state
.private_data
= private_data
;
124 return ctx
->traverse(ctx
, conn_traverse_fn
, (void *)&state
);
127 struct conn_traverse_read_state
{
128 int (*fn
)(const struct connections_key
*key
,
129 const struct connections_data
*data
,
134 static int connections_forall_read_fn(struct db_record
*rec
,
137 struct conn_traverse_read_state
*state
=
138 (struct conn_traverse_read_state
*)private_data
;
140 if ((rec
->key
.dsize
!= sizeof(struct connections_key
))
141 || (rec
->value
.dsize
!= sizeof(struct connections_data
))) {
144 return state
->fn((const struct connections_key
*)rec
->key
.dptr
,
145 (const struct connections_data
*)rec
->value
.dptr
,
146 state
->private_data
);
149 int connections_forall_read(int (*fn
)(const struct connections_key
*key
,
150 const struct connections_data
*data
,
154 struct db_context
*ctx
;
155 struct conn_traverse_read_state state
;
157 ctx
= connections_db_ctx(false);
163 state
.private_data
= private_data
;
165 return ctx
->traverse_read(ctx
, connections_forall_read_fn
,
169 bool connections_init(bool rw
)
171 return (connections_db_ctx(rw
) != NULL
);