2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Rafal Szczesniak 2002
6 Copyright (C) Michael Adam 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 "system/filesys.h"
29 /* these are little tdb utility functions that are meant to make
30 dealing with a tdb database a little less cumbersome in Samba */
32 static SIG_ATOMIC_T gotalarm
;
34 /***************************************************************
35 Signal function to tell us we timed out.
36 ****************************************************************/
38 static void gotalarm_sig(int signum
)
43 /****************************************************************************
44 Lock a chain with timeout (in seconds).
45 ****************************************************************************/
47 static int tdb_chainlock_with_timeout_internal( TDB_CONTEXT
*tdb
, TDB_DATA key
, unsigned int timeout
, int rw_type
)
49 /* Allow tdb_chainlock to be interrupted by an alarm. */
54 CatchSignal(SIGALRM
, gotalarm_sig
);
55 tdb_setalarm_sigptr(tdb
, &gotalarm
);
59 if (rw_type
== F_RDLCK
)
60 ret
= tdb_chainlock_read(tdb
, key
);
62 ret
= tdb_chainlock(tdb
, key
);
66 tdb_setalarm_sigptr(tdb
, NULL
);
67 CatchSignal(SIGALRM
, SIG_IGN
);
68 if (gotalarm
&& (ret
== -1)) {
69 DEBUG(0,("tdb_chainlock_with_timeout_internal: alarm (%u) timed out for key %s in tdb %s\n",
70 timeout
, key
.dptr
, tdb_name(tdb
)));
71 /* TODO: If we time out waiting for a lock, it might
72 * be nice to use F_GETLK to get the pid of the
73 * process currently holding the lock and print that
74 * as part of the debugging message. -- mbp */
82 /****************************************************************************
83 Write lock a chain. Return -1 if timeout or lock failed.
84 ****************************************************************************/
86 int tdb_chainlock_with_timeout( TDB_CONTEXT
*tdb
, TDB_DATA key
, unsigned int timeout
)
88 return tdb_chainlock_with_timeout_internal(tdb
, key
, timeout
, F_WRLCK
);
91 int tdb_lock_bystring_with_timeout(TDB_CONTEXT
*tdb
, const char *keyval
,
94 TDB_DATA key
= string_term_tdb_data(keyval
);
96 return tdb_chainlock_with_timeout(tdb
, key
, timeout
);
99 /****************************************************************************
100 Read lock a chain by string. Return -1 if timeout or lock failed.
101 ****************************************************************************/
103 int tdb_read_lock_bystring_with_timeout(TDB_CONTEXT
*tdb
, const char *keyval
, unsigned int timeout
)
105 TDB_DATA key
= string_term_tdb_data(keyval
);
107 return tdb_chainlock_with_timeout_internal(tdb
, key
, timeout
, F_RDLCK
);
113 int tdb_trans_store_bystring(TDB_CONTEXT
*tdb
, const char *keystr
,
114 TDB_DATA data
, int flags
)
116 TDB_DATA key
= string_term_tdb_data(keystr
);
118 return tdb_trans_store(tdb
, key
, data
, flags
);
121 /****************************************************************************
122 Useful pair of routines for packing/unpacking data consisting of
123 integers and strings.
124 ****************************************************************************/
126 static size_t tdb_pack_va(uint8
*buf
, int bufsize
, const char *fmt
, va_list ap
)
137 const char *fmt0
= fmt
;
138 int bufsize0
= bufsize
;
141 switch ((c
= *fmt
++)) {
142 case 'b': /* unsigned 8-bit integer */
144 bt
= (uint8
)va_arg(ap
, int);
145 if (bufsize
&& bufsize
>= len
)
148 case 'w': /* unsigned 16-bit integer */
150 w
= (uint16
)va_arg(ap
, int);
151 if (bufsize
&& bufsize
>= len
)
154 case 'd': /* signed 32-bit integer (standard int in most systems) */
156 d
= va_arg(ap
, uint32
);
157 if (bufsize
&& bufsize
>= len
)
160 case 'p': /* pointer */
162 p
= va_arg(ap
, void *);
164 if (bufsize
&& bufsize
>= len
)
167 case 'P': /* null-terminated string */
168 s
= va_arg(ap
,char *);
171 if (bufsize
&& bufsize
>= len
)
174 case 'f': /* null-terminated string */
175 s
= va_arg(ap
,char *);
178 if (bufsize
&& bufsize
>= len
)
181 case 'B': /* fixed-length string */
183 s
= va_arg(ap
, char *);
185 if (bufsize
&& bufsize
>= len
) {
191 DEBUG(0,("Unknown tdb_pack format %c in %s\n",
204 DEBUG(18,("tdb_pack_va(%s, %d) -> %d\n",
205 fmt0
, bufsize0
, (int)PTR_DIFF(buf
, buf0
)));
207 return PTR_DIFF(buf
, buf0
);
210 size_t tdb_pack(uint8
*buf
, int bufsize
, const char *fmt
, ...)
216 result
= tdb_pack_va(buf
, bufsize
, fmt
, ap
);
221 bool tdb_pack_append(TALLOC_CTX
*mem_ctx
, uint8
**buf
, size_t *len
,
222 const char *fmt
, ...)
228 len1
= tdb_pack_va(NULL
, 0, fmt
, ap
);
231 if (mem_ctx
!= NULL
) {
232 *buf
= TALLOC_REALLOC_ARRAY(mem_ctx
, *buf
, uint8
,
235 *buf
= SMB_REALLOC_ARRAY(*buf
, uint8
, (*len
) + len1
);
243 len2
= tdb_pack_va((*buf
)+(*len
), len1
, fmt
, ap
);
255 /****************************************************************************
256 Useful pair of routines for packing/unpacking data consisting of
257 integers and strings.
258 ****************************************************************************/
260 int tdb_unpack(const uint8
*buf
, int bufsize
, const char *fmt
, ...)
271 const uint8
*buf0
= buf
;
272 const char *fmt0
= fmt
;
273 int bufsize0
= bufsize
;
278 switch ((c
=*fmt
++)) {
279 case 'b': /* unsigned 8-bit integer */
281 bt
= va_arg(ap
, uint8
*);
286 case 'w': /* unsigned 16-bit integer */
288 w
= va_arg(ap
, uint16
*);
293 case 'd': /* signed 32-bit integer (standard int in most systems) */
295 d
= va_arg(ap
, uint32
*);
300 case 'p': /* pointer */
302 p
= va_arg(ap
, void **);
306 * This isn't a real pointer - only a token (1 or 0)
307 * to mark the fact a pointer is present.
310 *p
= (void *)(IVAL(buf
, 0) ? (void *)1 : NULL
);
312 case 'P': /* null-terminated string */
313 /* Return malloc'ed string. */
314 ps
= va_arg(ap
,char **);
315 len
= strlen((const char *)buf
) + 1;
316 *ps
= SMB_STRDUP((const char *)buf
);
318 case 'f': /* null-terminated string */
319 s
= va_arg(ap
,char *);
320 len
= strlen((const char *)buf
) + 1;
321 if (bufsize
< len
|| len
> sizeof(fstring
))
325 case 'B': /* fixed-length string */
326 i
= va_arg(ap
, int *);
327 b
= va_arg(ap
, char **);
339 *b
= (char *)SMB_MALLOC(*i
);
342 memcpy(*b
, buf
+4, *i
);
345 DEBUG(0,("Unknown tdb_unpack format %c in %s\n",
358 DEBUG(18,("tdb_unpack(%s, %d) -> %d\n",
359 fmt0
, bufsize0
, (int)PTR_DIFF(buf
, buf0
)));
361 return PTR_DIFF(buf
, buf0
);
369 /****************************************************************************
370 Log tdb messages via DEBUG().
371 ****************************************************************************/
373 static void tdb_log(TDB_CONTEXT
*tdb
, enum tdb_debug_level level
, const char *format
, ...)
379 va_start(ap
, format
);
380 ret
= vasprintf(&ptr
, format
, ap
);
383 if ((ret
== -1) || !*ptr
)
386 DEBUG((int)level
, ("tdb(%s): %s", tdb_name(tdb
) ? tdb_name(tdb
) : "unnamed", ptr
));
390 /****************************************************************************
391 Like tdb_open() but also setup a logging function that redirects to
392 the samba DEBUG() system.
393 ****************************************************************************/
395 TDB_CONTEXT
*tdb_open_log(const char *name
, int hash_size
, int tdb_flags
,
396 int open_flags
, mode_t mode
)
399 struct tdb_logging_context log_ctx
;
402 tdb_flags
|= TDB_NOMMAP
;
404 log_ctx
.log_fn
= tdb_log
;
405 log_ctx
.log_private
= NULL
;
407 if ((hash_size
== 0) && (name
!= NULL
)) {
408 const char *base
= strrchr_m(name
, '/');
415 hash_size
= lp_parm_int(-1, "tdb_hashsize", base
, 0);
418 tdb
= tdb_open_ex(name
, hash_size
, tdb_flags
,
419 open_flags
, mode
, &log_ctx
, NULL
);
426 /****************************************************************************
427 tdb_store, wrapped in a transaction. This way we make sure that a process
428 that dies within writing does not leave a corrupt tdb behind.
429 ****************************************************************************/
431 int tdb_trans_store(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA dbuf
,
436 if ((res
= tdb_transaction_start(tdb
)) != 0) {
437 DEBUG(5, ("tdb_transaction_start failed\n"));
441 if ((res
= tdb_store(tdb
, key
, dbuf
, flag
)) != 0) {
442 DEBUG(10, ("tdb_store failed\n"));
443 if (tdb_transaction_cancel(tdb
) != 0) {
444 smb_panic("Cancelling transaction failed");
449 if ((res
= tdb_transaction_commit(tdb
)) != 0) {
450 DEBUG(5, ("tdb_transaction_commit failed\n"));
456 /****************************************************************************
457 tdb_delete, wrapped in a transaction. This way we make sure that a process
458 that dies within deleting does not leave a corrupt tdb behind.
459 ****************************************************************************/
461 int tdb_trans_delete(struct tdb_context
*tdb
, TDB_DATA key
)
465 if ((res
= tdb_transaction_start(tdb
)) != 0) {
466 DEBUG(5, ("tdb_transaction_start failed\n"));
470 if ((res
= tdb_delete(tdb
, key
)) != 0) {
471 DEBUG(10, ("tdb_delete failed\n"));
472 if (tdb_transaction_cancel(tdb
) != 0) {
473 smb_panic("Cancelling transaction failed");
478 if ((res
= tdb_transaction_commit(tdb
)) != 0) {
479 DEBUG(5, ("tdb_transaction_commit failed\n"));
486 Log tdb messages via DEBUG().
488 static void tdb_wrap_log(TDB_CONTEXT
*tdb
, enum tdb_debug_level level
,
489 const char *format
, ...) PRINTF_ATTRIBUTE(3,4);
491 static void tdb_wrap_log(TDB_CONTEXT
*tdb
, enum tdb_debug_level level
,
492 const char *format
, ...)
500 case TDB_DEBUG_FATAL
:
503 case TDB_DEBUG_ERROR
:
506 case TDB_DEBUG_WARNING
:
509 case TDB_DEBUG_TRACE
:
516 va_start(ap
, format
);
517 ret
= vasprintf(&ptr
, format
, ap
);
521 const char *name
= tdb_name(tdb
);
522 DEBUG(debuglevel
, ("tdb(%s): %s", name
? name
: "unnamed", ptr
));
527 struct tdb_wrap_private
{
528 struct tdb_context
*tdb
;
530 struct tdb_wrap_private
*next
, *prev
;
533 static struct tdb_wrap_private
*tdb_list
;
535 /* destroy the last connection to a tdb */
536 static int tdb_wrap_private_destructor(struct tdb_wrap_private
*w
)
539 DLIST_REMOVE(tdb_list
, w
);
543 static struct tdb_wrap_private
*tdb_wrap_private_open(TALLOC_CTX
*mem_ctx
,
550 struct tdb_wrap_private
*result
;
551 struct tdb_logging_context log_ctx
;
553 result
= talloc(mem_ctx
, struct tdb_wrap_private
);
554 if (result
== NULL
) {
557 result
->name
= talloc_strdup(result
, name
);
558 if (result
->name
== NULL
) {
562 log_ctx
.log_fn
= tdb_wrap_log
;
564 if (!lp_use_mmap()) {
565 tdb_flags
|= TDB_NOMMAP
;
568 if ((hash_size
== 0) && (name
!= NULL
)) {
570 base
= strrchr_m(name
, '/');
577 hash_size
= lp_parm_int(-1, "tdb_hashsize", base
, 0);
580 result
->tdb
= tdb_open_ex(name
, hash_size
, tdb_flags
,
581 open_flags
, mode
, &log_ctx
, NULL
);
582 if (result
->tdb
== NULL
) {
585 talloc_set_destructor(result
, tdb_wrap_private_destructor
);
586 DLIST_ADD(tdb_list
, result
);
595 wrapped connection to a tdb database
596 to close just talloc_free() the tdb_wrap pointer
598 struct tdb_wrap
*tdb_wrap_open(TALLOC_CTX
*mem_ctx
,
599 const char *name
, int hash_size
, int tdb_flags
,
600 int open_flags
, mode_t mode
)
602 struct tdb_wrap
*result
;
603 struct tdb_wrap_private
*w
;
605 result
= talloc(mem_ctx
, struct tdb_wrap
);
606 if (result
== NULL
) {
610 for (w
=tdb_list
;w
;w
=w
->next
) {
611 if (strcmp(name
, w
->name
) == 0) {
617 w
= tdb_wrap_private_open(result
, name
, hash_size
, tdb_flags
,
621 * Correctly use talloc_reference: The tdb will be
622 * closed when "w" is being freed. The caller never
623 * sees "w", so an incorrect use of talloc_free(w)
624 * instead of calling talloc_unlink is not possible.
625 * To avoid having to refcount ourselves, "w" will
626 * have multiple parents that hang off all the
627 * tdb_wrap's being returned from here. Those parents
628 * can be freed without problem.
630 if (talloc_reference(result
, w
) == NULL
) {
637 result
->tdb
= w
->tdb
;
644 NTSTATUS
map_nt_error_from_tdb(enum TDB_ERROR err
)
646 NTSTATUS result
= NT_STATUS_INTERNAL_ERROR
;
650 result
= NT_STATUS_OK
;
652 case TDB_ERR_CORRUPT
:
653 result
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
656 result
= NT_STATUS_UNEXPECTED_IO_ERROR
;
659 result
= NT_STATUS_NO_MEMORY
;
662 result
= NT_STATUS_OBJECT_NAME_COLLISION
;
667 * TDB_ERR_LOCK is very broad, we could for example
668 * distinguish between fcntl locks and invalid lock
669 * sequences. So NT_STATUS_FILE_LOCK_CONFLICT is a
672 result
= NT_STATUS_FILE_LOCK_CONFLICT
;
676 case TDB_ERR_LOCK_TIMEOUT
:
678 * These two ones in the enum are not actually used
680 result
= NT_STATUS_FILE_LOCK_CONFLICT
;
682 case TDB_ERR_NOEXIST
:
683 result
= NT_STATUS_NOT_FOUND
;
686 result
= NT_STATUS_INVALID_PARAMETER
;
689 result
= NT_STATUS_ACCESS_DENIED
;
691 case TDB_ERR_NESTING
:
692 result
= NT_STATUS_INTERNAL_ERROR
;
698 int tdb_data_cmp(TDB_DATA t1
, TDB_DATA t2
)
701 if (t1
.dptr
== NULL
&& t2
.dptr
!= NULL
) {
704 if (t1
.dptr
!= NULL
&& t2
.dptr
== NULL
) {
707 if (t1
.dptr
== t2
.dptr
) {
708 return t1
.dsize
- t2
.dsize
;
710 ret
= memcmp(t1
.dptr
, t2
.dptr
, MIN(t1
.dsize
, t2
.dsize
));
712 return t1
.dsize
- t2
.dsize
;