s3-libsmb: Don't leak memory on error.
[Samba.git] / lib / util / util_tdb.c
blob93df958f1ae360ba6dd55b53b8adb930fa036faf
1 /*
2 Unix SMB/CIFS implementation.
4 tdb utility functions
6 Copyright (C) Andrew Tridgell 1992-2006
7 Copyright (C) Volker Lendecke 2007-2011
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "system/filesys.h"
25 #include "../lib/tdb/include/tdb.h"
26 #include "../lib/util/util_tdb.h"
28 /* these are little tdb utility functions that are meant to make
29 dealing with a tdb database a little less cumbersome in Samba */
31 /***************************************************************
32 Make a TDB_DATA and keep the const warning in one place
33 ****************************************************************/
35 TDB_DATA make_tdb_data(const uint8_t *dptr, size_t dsize)
37 TDB_DATA ret;
38 ret.dptr = discard_const_p(uint8_t, dptr);
39 ret.dsize = dsize;
40 return ret;
43 bool tdb_data_equal(TDB_DATA t1, TDB_DATA t2)
45 if (t1.dsize != t2.dsize) {
46 return false;
48 return (memcmp(t1.dptr, t2.dptr, t1.dsize) == 0);
51 bool tdb_data_is_empty(TDB_DATA d)
53 return (d.dsize == 0) || (d.dptr == NULL);
56 TDB_DATA string_tdb_data(const char *string)
58 return make_tdb_data((const uint8_t *)string, string ? strlen(string) : 0 );
61 TDB_DATA string_term_tdb_data(const char *string)
63 return make_tdb_data((const uint8_t *)string, string ? strlen(string) + 1 : 0);
66 TDB_DATA tdb_data_talloc_copy(TALLOC_CTX* mem_ctx, TDB_DATA data) {
67 TDB_DATA ret = {
68 .dptr = (uint8_t *)talloc_size(mem_ctx, data.dsize+1),
69 .dsize = data.dsize
71 if (ret.dptr == NULL) {
72 ret.dsize = 0;
73 } else {
74 memcpy(ret.dptr, data.dptr, data.dsize);
75 ret.dptr[ret.dsize] = '\0';
77 return ret;
81 /****************************************************************************
82 Lock a chain by string. Return non-zero if lock failed.
83 ****************************************************************************/
85 int tdb_lock_bystring(struct tdb_context *tdb, const char *keyval)
87 TDB_DATA key = string_term_tdb_data(keyval);
89 return tdb_chainlock(tdb, key);
92 /****************************************************************************
93 Unlock a chain by string.
94 ****************************************************************************/
96 void tdb_unlock_bystring(struct tdb_context *tdb, const char *keyval)
98 TDB_DATA key = string_term_tdb_data(keyval);
100 tdb_chainunlock(tdb, key);
103 /****************************************************************************
104 Read lock a chain by string. Return non-zero if lock failed.
105 ****************************************************************************/
107 int tdb_read_lock_bystring(struct tdb_context *tdb, const char *keyval)
109 TDB_DATA key = string_term_tdb_data(keyval);
111 return tdb_chainlock_read(tdb, key);
114 /****************************************************************************
115 Read unlock a chain by string.
116 ****************************************************************************/
118 void tdb_read_unlock_bystring(struct tdb_context *tdb, const char *keyval)
120 TDB_DATA key = string_term_tdb_data(keyval);
122 tdb_chainunlock_read(tdb, key);
126 /****************************************************************************
127 Fetch a int32_t value by a arbitrary blob key, return -1 if not found.
128 Output is int32_t in native byte order.
129 ****************************************************************************/
131 int32_t tdb_fetch_int32_byblob(struct tdb_context *tdb, TDB_DATA key)
133 TDB_DATA data;
134 int32_t ret;
136 data = tdb_fetch(tdb, key);
137 if (!data.dptr || data.dsize != sizeof(int32_t)) {
138 SAFE_FREE(data.dptr);
139 return -1;
142 ret = IVAL(data.dptr,0);
143 SAFE_FREE(data.dptr);
144 return ret;
147 /****************************************************************************
148 Fetch a int32_t value by string key, return -1 if not found.
149 Output is int32_t in native byte order.
150 ****************************************************************************/
152 int32_t tdb_fetch_int32(struct tdb_context *tdb, const char *keystr)
154 return tdb_fetch_int32_byblob(tdb, string_term_tdb_data(keystr));
157 /****************************************************************************
158 Store a int32_t value by an arbitrary blob key, return 0 on success, -ve on failure.
159 Input is int32_t in native byte order. Output in tdb is in little-endian.
160 ****************************************************************************/
162 int tdb_store_int32_byblob(struct tdb_context *tdb, TDB_DATA key, int32_t v)
164 TDB_DATA data;
165 int32_t v_store;
167 SIVAL(&v_store,0,v);
168 data.dptr = (unsigned char *)&v_store;
169 data.dsize = sizeof(int32_t);
171 return tdb_store(tdb, key, data, TDB_REPLACE);
174 /****************************************************************************
175 Store a int32_t value by string key, return 0 on success, -ve on failure.
176 Input is int32_t in native byte order. Output in tdb is in little-endian.
177 ****************************************************************************/
179 int tdb_store_int32(struct tdb_context *tdb, const char *keystr, int32_t v)
181 return tdb_store_int32_byblob(tdb, string_term_tdb_data(keystr), v);
184 /****************************************************************************
185 Fetch a uint32_t value by a arbitrary blob key, return false if not found.
186 Output is uint32_t in native byte order.
187 ****************************************************************************/
189 bool tdb_fetch_uint32_byblob(struct tdb_context *tdb, TDB_DATA key, uint32_t *value)
191 TDB_DATA data;
193 data = tdb_fetch(tdb, key);
194 if (!data.dptr || data.dsize != sizeof(uint32_t)) {
195 SAFE_FREE(data.dptr);
196 return false;
199 *value = IVAL(data.dptr,0);
200 SAFE_FREE(data.dptr);
201 return true;
204 /****************************************************************************
205 Fetch a uint32_t value by string key, return false if not found.
206 Output is uint32_t in native byte order.
207 ****************************************************************************/
209 bool tdb_fetch_uint32(struct tdb_context *tdb, const char *keystr, uint32_t *value)
211 return tdb_fetch_uint32_byblob(tdb, string_term_tdb_data(keystr), value);
214 /****************************************************************************
215 Store a uint32_t value by an arbitrary blob key, return true on success, false on failure.
216 Input is uint32_t in native byte order. Output in tdb is in little-endian.
217 ****************************************************************************/
219 bool tdb_store_uint32_byblob(struct tdb_context *tdb, TDB_DATA key, uint32_t value)
221 TDB_DATA data;
222 uint32_t v_store;
223 bool ret = true;
225 SIVAL(&v_store, 0, value);
226 data.dptr = (unsigned char *)&v_store;
227 data.dsize = sizeof(uint32_t);
229 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
230 ret = false;
232 return ret;
235 /****************************************************************************
236 Store a uint32_t value by string key, return true on success, false on failure.
237 Input is uint32_t in native byte order. Output in tdb is in little-endian.
238 ****************************************************************************/
240 bool tdb_store_uint32(struct tdb_context *tdb, const char *keystr, uint32_t value)
242 return tdb_store_uint32_byblob(tdb, string_term_tdb_data(keystr), value);
244 /****************************************************************************
245 Store a buffer by a null terminated string key. Return 0 on success, -ve
246 on failure.
247 ****************************************************************************/
249 int tdb_store_bystring(struct tdb_context *tdb, const char *keystr, TDB_DATA data, int flags)
251 TDB_DATA key = string_term_tdb_data(keystr);
253 return tdb_store(tdb, key, data, flags);
256 /****************************************************************************
257 Fetch a buffer using a null terminated string key. Don't forget to call
258 free() on the result dptr.
259 ****************************************************************************/
261 TDB_DATA tdb_fetch_bystring(struct tdb_context *tdb, const char *keystr)
263 TDB_DATA key = string_term_tdb_data(keystr);
265 return tdb_fetch(tdb, key);
268 /****************************************************************************
269 Delete an entry using a null terminated string key.
270 ****************************************************************************/
272 int tdb_delete_bystring(struct tdb_context *tdb, const char *keystr)
274 TDB_DATA key = string_term_tdb_data(keystr);
276 return tdb_delete(tdb, key);
279 /****************************************************************************
280 Atomic integer change. Returns old value. To create, set initial value in *oldval.
281 ****************************************************************************/
283 int32_t tdb_change_int32_atomic(struct tdb_context *tdb, const char *keystr, int32_t *oldval, int32_t change_val)
285 int32_t val;
286 int32_t ret = -1;
288 if (tdb_lock_bystring(tdb, keystr) != 0)
289 return -1;
291 if ((val = tdb_fetch_int32(tdb, keystr)) == -1) {
292 /* The lookup failed */
293 if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
294 /* but not because it didn't exist */
295 goto err_out;
298 /* Start with 'old' value */
299 val = *oldval;
301 } else {
302 /* It worked, set return value (oldval) to tdb data */
303 *oldval = val;
306 /* Increment value for storage and return next time */
307 val += change_val;
309 if (tdb_store_int32(tdb, keystr, val) != 0)
310 goto err_out;
312 ret = 0;
314 err_out:
316 tdb_unlock_bystring(tdb, keystr);
317 return ret;
320 static sig_atomic_t gotalarm;
322 /***************************************************************
323 Signal function to tell us we timed out.
324 ****************************************************************/
326 static void gotalarm_sig(int signum)
328 gotalarm = 1;
331 /****************************************************************************
332 Lock a chain with timeout (in seconds).
333 ****************************************************************************/
335 static int tdb_chainlock_with_timeout_internal( TDB_CONTEXT *tdb, TDB_DATA key, unsigned int timeout, int rw_type)
337 /* Allow tdb_chainlock to be interrupted by an alarm. */
338 int ret;
339 gotalarm = 0;
341 if (timeout) {
342 CatchSignal(SIGALRM, gotalarm_sig);
343 tdb_setalarm_sigptr(tdb, &gotalarm);
344 alarm(timeout);
347 if (rw_type == F_RDLCK)
348 ret = tdb_chainlock_read(tdb, key);
349 else
350 ret = tdb_chainlock(tdb, key);
352 if (timeout) {
353 alarm(0);
354 tdb_setalarm_sigptr(tdb, NULL);
355 CatchSignal(SIGALRM, SIG_IGN);
356 if (gotalarm && (ret != 0)) {
357 DEBUG(0,("tdb_chainlock_with_timeout_internal: alarm (%u) timed out for key %s in tdb %s\n",
358 timeout, key.dptr, tdb_name(tdb)));
359 /* TODO: If we time out waiting for a lock, it might
360 * be nice to use F_GETLK to get the pid of the
361 * process currently holding the lock and print that
362 * as part of the debugging message. -- mbp */
363 return -1;
367 return ret == 0 ? 0 : -1;
370 /****************************************************************************
371 Write lock a chain. Return non-zero if timeout or lock failed.
372 ****************************************************************************/
374 int tdb_chainlock_with_timeout( TDB_CONTEXT *tdb, TDB_DATA key, unsigned int timeout)
376 return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_WRLCK);
379 int tdb_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval,
380 int timeout)
382 TDB_DATA key = string_term_tdb_data(keyval);
384 return tdb_chainlock_with_timeout(tdb, key, timeout);
387 /****************************************************************************
388 Read lock a chain by string. Return non-zero if timeout or lock failed.
389 ****************************************************************************/
391 int tdb_read_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval, unsigned int timeout)
393 TDB_DATA key = string_term_tdb_data(keyval);
395 return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_RDLCK);
398 /****************************************************************************
399 Atomic unsigned integer change. Returns old value. To create, set initial value in *oldval.
400 ****************************************************************************/
402 bool tdb_change_uint32_atomic(struct tdb_context *tdb, const char *keystr, uint32_t *oldval, uint32_t change_val)
404 uint32_t val;
405 bool ret = false;
407 if (tdb_lock_bystring(tdb, keystr) != 0)
408 return false;
410 if (!tdb_fetch_uint32(tdb, keystr, &val)) {
411 /* It failed */
412 if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
413 /* and not because it didn't exist */
414 goto err_out;
417 /* Start with 'old' value */
418 val = *oldval;
420 } else {
421 /* it worked, set return value (oldval) to tdb data */
422 *oldval = val;
426 /* get a new value to store */
427 val += change_val;
429 if (!tdb_store_uint32(tdb, keystr, val))
430 goto err_out;
432 ret = true;
434 err_out:
436 tdb_unlock_bystring(tdb, keystr);
437 return ret;
440 /****************************************************************************
441 Allow tdb_delete to be used as a tdb_traversal_fn.
442 ****************************************************************************/
444 int tdb_traverse_delete_fn(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf,
445 void *state)
447 return tdb_delete(the_tdb, key);
450 /****************************************************************************
451 Return an NTSTATUS from a TDB_ERROR
452 ****************************************************************************/
454 NTSTATUS map_nt_error_from_tdb(enum TDB_ERROR err)
456 NTSTATUS result = NT_STATUS_INTERNAL_ERROR;
458 switch (err) {
459 case TDB_SUCCESS:
460 result = NT_STATUS_OK;
461 break;
462 case TDB_ERR_CORRUPT:
463 result = NT_STATUS_INTERNAL_DB_CORRUPTION;
464 break;
465 case TDB_ERR_IO:
466 result = NT_STATUS_UNEXPECTED_IO_ERROR;
467 break;
468 case TDB_ERR_OOM:
469 result = NT_STATUS_NO_MEMORY;
470 break;
471 case TDB_ERR_EXISTS:
472 result = NT_STATUS_OBJECT_NAME_COLLISION;
473 break;
475 case TDB_ERR_LOCK:
477 * TDB_ERR_LOCK is very broad, we could for example
478 * distinguish between fcntl locks and invalid lock
479 * sequences. So NT_STATUS_FILE_LOCK_CONFLICT is a
480 * compromise.
482 result = NT_STATUS_FILE_LOCK_CONFLICT;
483 break;
485 case TDB_ERR_NOLOCK:
486 case TDB_ERR_LOCK_TIMEOUT:
488 * These two ones in the enum are not actually used
490 result = NT_STATUS_FILE_LOCK_CONFLICT;
491 break;
492 case TDB_ERR_NOEXIST:
493 result = NT_STATUS_NOT_FOUND;
494 break;
495 case TDB_ERR_EINVAL:
496 result = NT_STATUS_INVALID_PARAMETER;
497 break;
498 case TDB_ERR_RDONLY:
499 result = NT_STATUS_ACCESS_DENIED;
500 break;
501 case TDB_ERR_NESTING:
502 result = NT_STATUS_INTERNAL_ERROR;
503 break;
505 return result;