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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 static struct db_context
*connections_db_ctx(BOOL rw
)
25 static struct db_context
*db_ctx
;
32 db_ctx
= db_open(NULL
, lock_path("connections.tdb"), 0,
33 TDB_CLEAR_IF_FIRST
|TDB_DEFAULT
,
34 O_RDWR
| O_CREAT
, 0644);
37 db_ctx
= db_open(NULL
, lock_path("connections.tdb"), 0,
38 TDB_DEFAULT
, O_RDONLY
, 0);
44 struct db_record
*connections_fetch_record(TALLOC_CTX
*mem_ctx
,
47 struct db_context
*ctx
= connections_db_ctx(True
);
53 return ctx
->fetch_locked(ctx
, mem_ctx
, key
);
56 struct db_record
*connections_fetch_entry(TALLOC_CTX
*mem_ctx
,
57 connection_struct
*conn
,
60 struct connections_key ckey
;
64 ckey
.pid
= procid_self();
65 ckey
.cnum
= conn
? conn
->cnum
: -1;
66 strlcpy(ckey
.name
, name
, sizeof(ckey
.name
));
68 key
.dsize
= sizeof(ckey
);
69 key
.dptr
= (uint8
*)&ckey
;
71 return connections_fetch_record(mem_ctx
, key
);
74 struct conn_traverse_state
{
75 int (*fn
)(struct db_record
*rec
,
76 const struct connections_key
*key
,
77 const struct connections_data
*data
,
82 static int conn_traverse_fn(struct db_record
*rec
, void *private_data
)
84 struct conn_traverse_state
*state
=
85 (struct conn_traverse_state
*)private_data
;
87 if ((rec
->key
.dsize
!= sizeof(struct connections_key
))
88 || (rec
->value
.dsize
!= sizeof(struct connections_data
))) {
92 return state
->fn(rec
, (const struct connections_key
*)rec
->key
.dptr
,
93 (const struct connections_data
*)rec
->value
.dptr
,
97 int connections_traverse(int (*fn
)(struct db_record
*rec
,
101 struct db_context
*ctx
= connections_db_ctx(False
);
107 return ctx
->traverse(ctx
, fn
, private_data
);
110 int connections_forall(int (*fn
)(struct db_record
*rec
,
111 const struct connections_key
*key
,
112 const struct connections_data
*data
,
116 struct conn_traverse_state state
;
119 state
.private_data
= private_data
;
121 return connections_traverse(conn_traverse_fn
, (void *)&state
);
124 BOOL
connections_init(BOOL rw
)
126 return (connections_db_ctx(rw
) != NULL
);