2 Unix SMB/CIFS implementation.
5 Copyright (C) Andrew Tridgell 2004
6 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "lib/util/dlinklist.h"
24 #include "lib/tdb_wrap/tdb_wrap.h"
25 #include "lib/param/param.h"
28 Log tdb messages via DEBUG().
30 static void tdb_wrap_log(TDB_CONTEXT
*tdb
, enum tdb_debug_level level
,
31 const char *format
, ...) PRINTF_ATTRIBUTE(3,4);
33 static void tdb_wrap_log(TDB_CONTEXT
*tdb
, enum tdb_debug_level level
,
34 const char *format
, ...)
48 case TDB_DEBUG_WARNING
:
59 ret
= vasprintf(&ptr
, format
, ap
);
63 const char *name
= tdb_name(tdb
);
64 DEBUG(debuglevel
, ("tdb(%s): %s", name
? name
: "unnamed", ptr
));
69 struct tdb_wrap_private
{
70 struct tdb_context
*tdb
;
72 struct tdb_wrap_private
*next
, *prev
;
75 static struct tdb_wrap_private
*tdb_list
;
77 /* destroy the last connection to a tdb */
78 static int tdb_wrap_private_destructor(struct tdb_wrap_private
*w
)
81 DLIST_REMOVE(tdb_list
, w
);
85 static struct tdb_wrap_private
*tdb_wrap_private_open(TALLOC_CTX
*mem_ctx
,
91 struct loadparm_context
*lp_ctx
)
93 struct tdb_wrap_private
*result
;
94 struct tdb_logging_context lctx
;
96 result
= talloc(mem_ctx
, struct tdb_wrap_private
);
100 result
->name
= talloc_strdup(result
, name
);
101 if (result
->name
== NULL
) {
105 if (!lpcfg_use_mmap(lp_ctx
)) {
106 tdb_flags
|= TDB_NOMMAP
;
109 if ((hash_size
== 0) && (name
!= NULL
)) {
111 base
= strrchr_m(name
, '/');
118 hash_size
= lpcfg_parm_int(lp_ctx
, NULL
, "tdb_hashsize", base
, 0);
121 lctx
.log_fn
= tdb_wrap_log
;
122 result
->tdb
= tdb_open_ex(name
, hash_size
, tdb_flags
,
123 open_flags
, mode
, &lctx
, NULL
);
124 if (result
->tdb
== NULL
) {
127 talloc_set_destructor(result
, tdb_wrap_private_destructor
);
128 DLIST_ADD(tdb_list
, result
);
137 wrapped connection to a tdb database
138 to close just talloc_free() the tdb_wrap pointer
140 struct tdb_wrap
*tdb_wrap_open(TALLOC_CTX
*mem_ctx
,
141 const char *name
, int hash_size
, int tdb_flags
,
142 int open_flags
, mode_t mode
,
143 struct loadparm_context
*lp_ctx
)
145 struct tdb_wrap
*result
;
146 struct tdb_wrap_private
*w
;
148 result
= talloc(mem_ctx
, struct tdb_wrap
);
149 if (result
== NULL
) {
153 for (w
=tdb_list
;w
;w
=w
->next
) {
154 if (strcmp(name
, w
->name
) == 0) {
160 w
= tdb_wrap_private_open(result
, name
, hash_size
, tdb_flags
,
161 open_flags
, mode
, lp_ctx
);
164 * Correctly use talloc_reference: The tdb will be
165 * closed when "w" is being freed. The caller never
166 * sees "w", so an incorrect use of talloc_free(w)
167 * instead of calling talloc_unlink is not possible.
168 * To avoid having to refcount ourselves, "w" will
169 * have multiple parents that hang off all the
170 * tdb_wrap's being returned from here. Those parents
171 * can be freed without problem.
173 if (talloc_reference(result
, w
) == NULL
) {
180 result
->tdb
= w
->tdb
;