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 db_ctx
= db_open(NULL
, lock_path("connections.tdb"), 0,
32 TDB_CLEAR_IF_FIRST
|TDB_DEFAULT
,
33 O_RDWR
| O_CREAT
, 0644);
36 db_ctx
= db_open(NULL
, lock_path("connections.tdb"), 0,
37 TDB_DEFAULT
, O_RDONLY
, 0);
43 struct db_record
*connections_fetch_record(TALLOC_CTX
*mem_ctx
,
46 struct db_context
*ctx
= connections_db_ctx(True
);
52 return ctx
->fetch_locked(ctx
, mem_ctx
, key
);
55 struct db_record
*connections_fetch_entry(TALLOC_CTX
*mem_ctx
,
56 connection_struct
*conn
,
59 struct connections_key ckey
;
63 ckey
.pid
= procid_self();
64 ckey
.cnum
= conn
? conn
->cnum
: -1;
65 strlcpy(ckey
.name
, name
, sizeof(ckey
.name
));
67 key
.dsize
= sizeof(ckey
);
68 key
.dptr
= (uint8
*)&ckey
;
70 return connections_fetch_record(mem_ctx
, key
);
73 struct conn_traverse_state
{
74 int (*fn
)(struct db_record
*rec
,
75 const struct connections_key
*key
,
76 const struct connections_data
*data
,
81 static int conn_traverse_fn(struct db_record
*rec
, void *private_data
)
83 struct conn_traverse_state
*state
=
84 (struct conn_traverse_state
*)private_data
;
86 if ((rec
->key
.dsize
!= sizeof(struct connections_key
))
87 || (rec
->value
.dsize
!= sizeof(struct connections_data
))) {
91 return state
->fn(rec
, (const struct connections_key
*)rec
->key
.dptr
,
92 (const struct connections_data
*)rec
->value
.dptr
,
96 int connections_traverse(int (*fn
)(struct db_record
*rec
,
100 struct db_context
*ctx
= connections_db_ctx(False
);
106 return ctx
->traverse(ctx
, fn
, private_data
);
109 int connections_forall(int (*fn
)(struct db_record
*rec
,
110 const struct connections_key
*key
,
111 const struct connections_data
*data
,
115 struct conn_traverse_state state
;
118 state
.private_data
= private_data
;
120 return connections_traverse(conn_traverse_fn
, (void *)&state
);
123 BOOL
connections_init(BOOL rw
)
125 return (connections_db_ctx(rw
) != NULL
);