s4-smbtorture: add ndr test for nbt_netlogon_packet to avoid future regressions.
[Samba/gebeck_regimport.git] / lib / util / tdb_wrap.c
blob7c3318bcadf9c10f58fb2608609c310bf6b03a87
1 /*
2 Unix SMB/CIFS implementation.
3 TDB wrap functions
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/>.
22 #include "includes.h"
23 #include "lib/util/dlinklist.h"
24 #include "lib/util/tdb_wrap.h"
25 #include "lib/param/param.h"
27 /* FIXME: TDB2 does this internally, so no need to wrap multiple opens! */
28 #if BUILD_TDB2
29 static void tdb_wrap_log(struct tdb_context *tdb,
30 enum tdb_log_level level,
31 enum TDB_ERROR ecode,
32 const char *message,
33 void *unused)
35 int dl;
36 const char *name = tdb_name(tdb);
38 switch (level) {
39 case TDB_LOG_USE_ERROR:
40 case TDB_LOG_ERROR:
41 dl = 0;
42 break;
43 case TDB_LOG_WARNING:
44 dl = 2;
45 break;
46 default:
47 dl = 0;
50 DEBUG(dl, ("tdb(%s):%s: %s", name ? name : "unnamed",
51 tdb_errorstr(ecode), message));
53 #else
55 Log tdb messages via DEBUG().
57 static void tdb_wrap_log(TDB_CONTEXT *tdb, enum tdb_debug_level level,
58 const char *format, ...) PRINTF_ATTRIBUTE(3,4);
60 static void tdb_wrap_log(TDB_CONTEXT *tdb, enum tdb_debug_level level,
61 const char *format, ...)
63 va_list ap;
64 char *ptr = NULL;
65 int debuglevel = 0;
66 int ret;
68 switch (level) {
69 case TDB_DEBUG_FATAL:
70 debuglevel = 0;
71 break;
72 case TDB_DEBUG_ERROR:
73 debuglevel = 1;
74 break;
75 case TDB_DEBUG_WARNING:
76 debuglevel = 2;
77 break;
78 case TDB_DEBUG_TRACE:
79 debuglevel = 5;
80 break;
81 default:
82 debuglevel = 0;
85 va_start(ap, format);
86 ret = vasprintf(&ptr, format, ap);
87 va_end(ap);
89 if (ret != -1) {
90 const char *name = tdb_name(tdb);
91 DEBUG(debuglevel, ("tdb(%s): %s", name ? name : "unnamed", ptr));
92 free(ptr);
95 #endif
97 struct tdb_wrap_private {
98 struct tdb_context *tdb;
99 const char *name;
100 struct tdb_wrap_private *next, *prev;
103 static struct tdb_wrap_private *tdb_list;
105 /* destroy the last connection to a tdb */
106 static int tdb_wrap_private_destructor(struct tdb_wrap_private *w)
108 tdb_close(w->tdb);
109 DLIST_REMOVE(tdb_list, w);
110 return 0;
113 static struct tdb_wrap_private *tdb_wrap_private_open(TALLOC_CTX *mem_ctx,
114 const char *name,
115 int hash_size,
116 int tdb_flags,
117 int open_flags,
118 mode_t mode,
119 struct loadparm_context *lp_ctx)
121 struct tdb_wrap_private *result;
123 result = talloc(mem_ctx, struct tdb_wrap_private);
124 if (result == NULL) {
125 return NULL;
127 result->name = talloc_strdup(result, name);
128 if (result->name == NULL) {
129 goto fail;
132 if (!lpcfg_use_mmap(lp_ctx)) {
133 tdb_flags |= TDB_NOMMAP;
136 if ((hash_size == 0) && (name != NULL)) {
137 const char *base;
138 base = strrchr_m(name, '/');
140 if (base != NULL) {
141 base += 1;
142 } else {
143 base = name;
145 hash_size = lpcfg_parm_int(lp_ctx, NULL, "tdb_hashsize", base, 0);
148 result->tdb = tdb_open_compat(name, hash_size, tdb_flags,
149 open_flags, mode, tdb_wrap_log, NULL);
150 if (result->tdb == NULL) {
151 goto fail;
153 talloc_set_destructor(result, tdb_wrap_private_destructor);
154 DLIST_ADD(tdb_list, result);
155 return result;
157 fail:
158 TALLOC_FREE(result);
159 return NULL;
163 wrapped connection to a tdb database
164 to close just talloc_free() the tdb_wrap pointer
166 struct tdb_wrap *tdb_wrap_open(TALLOC_CTX *mem_ctx,
167 const char *name, int hash_size, int tdb_flags,
168 int open_flags, mode_t mode,
169 struct loadparm_context *lp_ctx)
171 struct tdb_wrap *result;
172 struct tdb_wrap_private *w;
174 result = talloc(mem_ctx, struct tdb_wrap);
175 if (result == NULL) {
176 return NULL;
179 for (w=tdb_list;w;w=w->next) {
180 if (strcmp(name, w->name) == 0) {
181 break;
185 if (w == NULL) {
186 w = tdb_wrap_private_open(result, name, hash_size, tdb_flags,
187 open_flags, mode, lp_ctx);
188 } else {
190 * Correctly use talloc_reference: The tdb will be
191 * closed when "w" is being freed. The caller never
192 * sees "w", so an incorrect use of talloc_free(w)
193 * instead of calling talloc_unlink is not possible.
194 * To avoid having to refcount ourselves, "w" will
195 * have multiple parents that hang off all the
196 * tdb_wrap's being returned from here. Those parents
197 * can be freed without problem.
199 if (talloc_reference(result, w) == NULL) {
200 goto fail;
203 if (w == NULL) {
204 goto fail;
206 result->tdb = w->tdb;
207 return result;
208 fail:
209 TALLOC_FREE(result);
210 return NULL;