a3dc76d6b73216dc07ca5f82da95199af1bab202
[gd/samba-autobuild/.git] / lib / util / tdb_wrap.c
1 /* 
2    Unix SMB/CIFS implementation.
3    TDB wrap functions
4
5    Copyright (C) Andrew Tridgell 2004
6    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
7    
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.
12    
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.
17    
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/>.
20 */
21
22 #include "includes.h"
23 #include "lib/util/dlinklist.h"
24 #include "lib/util/tdb_wrap.h"
25
26 /* FIXME: TDB2 does this internally, so no need to wrap multiple opens! */
27 #if BUILD_TDB2
28 static void tdb_wrap_log(struct tdb_context *tdb,
29                          enum tdb_log_level level,
30                          enum TDB_ERROR ecode,
31                          const char *message,
32                          void *unused)
33 {
34         int dl;
35         const char *name = tdb_name(tdb);
36
37         switch (level) {
38         case TDB_LOG_USE_ERROR:
39         case TDB_LOG_ERROR:
40                 dl = 0;
41                 break;
42         case TDB_LOG_WARNING:
43                 dl = 2;
44                 break;
45         default:
46                 dl = 0;
47         }
48
49         DEBUG(dl, ("tdb(%s):%s: %s", name ? name : "unnamed",
50                    tdb_errorstr(ecode), message));
51 }
52 #else
53 /*
54  Log tdb messages via DEBUG().
55 */
56 static void tdb_wrap_log(TDB_CONTEXT *tdb, enum tdb_debug_level level, 
57                          const char *format, ...) PRINTF_ATTRIBUTE(3,4);
58
59 static void tdb_wrap_log(TDB_CONTEXT *tdb, enum tdb_debug_level level, 
60                          const char *format, ...)
61 {
62         va_list ap;
63         char *ptr = NULL;
64         int debuglevel = 0;
65         int ret;
66
67         switch (level) {
68         case TDB_DEBUG_FATAL:
69                 debuglevel = 0;
70                 break;
71         case TDB_DEBUG_ERROR:
72                 debuglevel = 1;
73                 break;
74         case TDB_DEBUG_WARNING:
75                 debuglevel = 2;
76                 break;
77         case TDB_DEBUG_TRACE:
78                 debuglevel = 5;
79                 break;
80         default:
81                 debuglevel = 0;
82         }               
83
84         va_start(ap, format);
85         ret = vasprintf(&ptr, format, ap);
86         va_end(ap);
87
88         if (ret != -1) {
89                 const char *name = tdb_name(tdb);
90                 DEBUG(debuglevel, ("tdb(%s): %s", name ? name : "unnamed", ptr));
91                 free(ptr);
92         }
93 }
94 #endif
95
96 struct tdb_wrap_private {
97         struct tdb_context *tdb;
98         const char *name;
99         struct tdb_wrap_private *next, *prev;
100 };
101
102 static struct tdb_wrap_private *tdb_list;
103
104 /* destroy the last connection to a tdb */
105 static int tdb_wrap_private_destructor(struct tdb_wrap_private *w)
106 {
107         tdb_close(w->tdb);
108         DLIST_REMOVE(tdb_list, w);
109         return 0;
110 }                                
111
112 static struct tdb_wrap_private *tdb_wrap_private_open(TALLOC_CTX *mem_ctx,
113                                                       const char *name,
114                                                       int hash_size,
115                                                       int tdb_flags,
116                                                       int open_flags,
117                                                       mode_t mode)
118 {
119         struct tdb_wrap_private *result;
120
121         result = talloc(mem_ctx, struct tdb_wrap_private);
122         if (result == NULL) {
123                 return NULL;
124         }
125         result->name = talloc_strdup(result, name);
126         if (result->name == NULL) {
127                 goto fail;
128         }
129
130 #if _SAMBA_BUILD_ == 3  
131         /* This #if _SAMBA_BUILD == 3 is very unfortunate, as it means
132          * that in the top level build, these options are not
133          * available for these databases.  However, having two
134          * different tdb_wrap lists is a worse fate, so this will do
135          * for now */
136
137         if (!lp_use_mmap()) {
138                 tdb_flags |= TDB_NOMMAP;
139         }
140
141         if ((hash_size == 0) && (name != NULL)) {
142                 const char *base;
143                 base = strrchr_m(name, '/');
144
145                 if (base != NULL) {
146                         base += 1;
147                 } else {
148                         base = name;
149                 }
150                 hash_size = lp_parm_int(-1, "tdb_hashsize", base, 0);
151         }
152 #endif
153
154         result->tdb = tdb_open_compat(name, hash_size, tdb_flags,
155                                       open_flags, mode, tdb_wrap_log, NULL);
156         if (result->tdb == NULL) {
157                 goto fail;
158         }
159         talloc_set_destructor(result, tdb_wrap_private_destructor);
160         DLIST_ADD(tdb_list, result);
161         return result;
162
163 fail:
164         TALLOC_FREE(result);
165         return NULL;
166 }
167
168 /*
169   wrapped connection to a tdb database
170   to close just talloc_free() the tdb_wrap pointer
171  */
172 struct tdb_wrap *tdb_wrap_open(TALLOC_CTX *mem_ctx,
173                                const char *name, int hash_size, int tdb_flags,
174                                int open_flags, mode_t mode)
175 {
176         struct tdb_wrap *result;
177         struct tdb_wrap_private *w;
178
179         result = talloc(mem_ctx, struct tdb_wrap);
180         if (result == NULL) {
181                 return NULL;
182         }
183
184         for (w=tdb_list;w;w=w->next) {
185                 if (strcmp(name, w->name) == 0) {
186                         break;
187                 }
188         }
189
190         if (w == NULL) {
191                 w = tdb_wrap_private_open(result, name, hash_size, tdb_flags,
192                                           open_flags, mode);
193         } else {
194                 /*
195                  * Correctly use talloc_reference: The tdb will be
196                  * closed when "w" is being freed. The caller never
197                  * sees "w", so an incorrect use of talloc_free(w)
198                  * instead of calling talloc_unlink is not possible.
199                  * To avoid having to refcount ourselves, "w" will
200                  * have multiple parents that hang off all the
201                  * tdb_wrap's being returned from here. Those parents
202                  * can be freed without problem.
203                  */
204                 if (talloc_reference(result, w) == NULL) {
205                         goto fail;
206                 }
207         }
208         if (w == NULL) {
209                 goto fail;
210         }
211         result->tdb = w->tdb;
212         return result;
213 fail:
214         TALLOC_FREE(result);
215         return NULL;
216 }
217