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