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"
31 /* these are little tdb utility functions that are meant to make
32 dealing with a tdb database a little less cumbersome in Samba */
34 static SIG_ATOMIC_T gotalarm
;
36 /***************************************************************
37 Signal function to tell us we timed out.
38 ****************************************************************/
40 static void gotalarm_sig(int signum
)
45 /****************************************************************************
46 Lock a chain with timeout (in seconds).
47 ****************************************************************************/
49 static int tdb_chainlock_with_timeout_internal( TDB_CONTEXT
*tdb
, TDB_DATA key
, unsigned int timeout
, int rw_type
)
51 /* Allow tdb_chainlock to be interrupted by an alarm. */
56 CatchSignal(SIGALRM
, gotalarm_sig
);
57 tdb_setalarm_sigptr(tdb
, &gotalarm
);
61 if (rw_type
== F_RDLCK
)
62 ret
= tdb_chainlock_read(tdb
, key
);
64 ret
= tdb_chainlock(tdb
, key
);
68 tdb_setalarm_sigptr(tdb
, NULL
);
69 CatchSignal(SIGALRM
, SIG_IGN
);
70 if (gotalarm
&& (ret
== -1)) {
71 DEBUG(0,("tdb_chainlock_with_timeout_internal: alarm (%u) timed out for key %s in tdb %s\n",
72 timeout
, key
.dptr
, tdb_name(tdb
)));
73 /* TODO: If we time out waiting for a lock, it might
74 * be nice to use F_GETLK to get the pid of the
75 * process currently holding the lock and print that
76 * as part of the debugging message. -- mbp */
84 /****************************************************************************
85 Write lock a chain. Return -1 if timeout or lock failed.
86 ****************************************************************************/
88 int tdb_chainlock_with_timeout( TDB_CONTEXT
*tdb
, TDB_DATA key
, unsigned int timeout
)
90 return tdb_chainlock_with_timeout_internal(tdb
, key
, timeout
, F_WRLCK
);
93 int tdb_lock_bystring_with_timeout(TDB_CONTEXT
*tdb
, const char *keyval
,
96 TDB_DATA key
= string_term_tdb_data(keyval
);
98 return tdb_chainlock_with_timeout(tdb
, key
, timeout
);
101 /****************************************************************************
102 Read lock a chain by string. Return -1 if timeout or lock failed.
103 ****************************************************************************/
105 int tdb_read_lock_bystring_with_timeout(TDB_CONTEXT
*tdb
, const char *keyval
, unsigned int timeout
)
107 TDB_DATA key
= string_term_tdb_data(keyval
);
109 return tdb_chainlock_with_timeout_internal(tdb
, key
, timeout
, F_RDLCK
);
115 int tdb_trans_store_bystring(TDB_CONTEXT
*tdb
, const char *keystr
,
116 TDB_DATA data
, int flags
)
118 TDB_DATA key
= string_term_tdb_data(keystr
);
120 return tdb_trans_store(tdb
, key
, data
, flags
);
123 /****************************************************************************
124 Useful pair of routines for packing/unpacking data consisting of
125 integers and strings.
126 ****************************************************************************/
128 static size_t tdb_pack_va(uint8
*buf
, int bufsize
, const char *fmt
, va_list ap
)
139 const char *fmt0
= fmt
;
140 int bufsize0
= bufsize
;
143 switch ((c
= *fmt
++)) {
144 case 'b': /* unsigned 8-bit integer */
146 bt
= (uint8
)va_arg(ap
, int);
147 if (bufsize
&& bufsize
>= len
)
150 case 'w': /* unsigned 16-bit integer */
152 w
= (uint16
)va_arg(ap
, int);
153 if (bufsize
&& bufsize
>= len
)
156 case 'd': /* signed 32-bit integer (standard int in most systems) */
158 d
= va_arg(ap
, uint32
);
159 if (bufsize
&& bufsize
>= len
)
162 case 'p': /* pointer */
164 p
= va_arg(ap
, void *);
166 if (bufsize
&& bufsize
>= len
)
169 case 'P': /* null-terminated string */
170 s
= va_arg(ap
,char *);
173 if (bufsize
&& bufsize
>= len
)
176 case 'f': /* null-terminated string */
177 s
= va_arg(ap
,char *);
180 if (bufsize
&& bufsize
>= len
)
183 case 'B': /* fixed-length string */
185 s
= va_arg(ap
, char *);
187 if (bufsize
&& bufsize
>= len
) {
193 DEBUG(0,("Unknown tdb_pack format %c in %s\n",
206 DEBUG(18,("tdb_pack_va(%s, %d) -> %d\n",
207 fmt0
, bufsize0
, (int)PTR_DIFF(buf
, buf0
)));
209 return PTR_DIFF(buf
, buf0
);
212 size_t tdb_pack(uint8
*buf
, int bufsize
, const char *fmt
, ...)
218 result
= tdb_pack_va(buf
, bufsize
, fmt
, ap
);
223 bool tdb_pack_append(TALLOC_CTX
*mem_ctx
, uint8
**buf
, size_t *len
,
224 const char *fmt
, ...)
230 len1
= tdb_pack_va(NULL
, 0, fmt
, ap
);
233 if (mem_ctx
!= NULL
) {
234 *buf
= TALLOC_REALLOC_ARRAY(mem_ctx
, *buf
, uint8
,
237 *buf
= SMB_REALLOC_ARRAY(*buf
, uint8
, (*len
) + len1
);
245 len2
= tdb_pack_va((*buf
)+(*len
), len1
, fmt
, ap
);
257 /****************************************************************************
258 Useful pair of routines for packing/unpacking data consisting of
259 integers and strings.
260 ****************************************************************************/
262 int tdb_unpack(const uint8
*buf
, int bufsize
, const char *fmt
, ...)
273 const uint8
*buf0
= buf
;
274 const char *fmt0
= fmt
;
275 int bufsize0
= bufsize
;
280 switch ((c
=*fmt
++)) {
281 case 'b': /* unsigned 8-bit integer */
283 bt
= va_arg(ap
, uint8
*);
288 case 'w': /* unsigned 16-bit integer */
290 w
= va_arg(ap
, uint16
*);
295 case 'd': /* signed 32-bit integer (standard int in most systems) */
297 d
= va_arg(ap
, uint32
*);
302 case 'p': /* pointer */
304 p
= va_arg(ap
, void **);
308 * This isn't a real pointer - only a token (1 or 0)
309 * to mark the fact a pointer is present.
312 *p
= (void *)(IVAL(buf
, 0) ? (void *)1 : NULL
);
314 case 'P': /* null-terminated string */
315 /* Return malloc'ed string. */
316 ps
= va_arg(ap
,char **);
317 len
= strlen((const char *)buf
) + 1;
318 *ps
= SMB_STRDUP((const char *)buf
);
320 case 'f': /* null-terminated string */
321 s
= va_arg(ap
,char *);
322 len
= strlen((const char *)buf
) + 1;
323 if (bufsize
< len
|| len
> sizeof(fstring
))
327 case 'B': /* fixed-length string */
328 i
= va_arg(ap
, int *);
329 b
= va_arg(ap
, char **);
341 *b
= (char *)SMB_MALLOC(*i
);
344 memcpy(*b
, buf
+4, *i
);
347 DEBUG(0,("Unknown tdb_unpack format %c in %s\n",
360 DEBUG(18,("tdb_unpack(%s, %d) -> %d\n",
361 fmt0
, bufsize0
, (int)PTR_DIFF(buf
, buf0
)));
363 return PTR_DIFF(buf
, buf0
);
371 /****************************************************************************
372 Log tdb messages via DEBUG().
373 ****************************************************************************/
375 static void tdb_log(TDB_CONTEXT
*tdb
, enum tdb_debug_level level
, const char *format
, ...)
381 va_start(ap
, format
);
382 ret
= vasprintf(&ptr
, format
, ap
);
385 if ((ret
== -1) || !*ptr
)
388 DEBUG((int)level
, ("tdb(%s): %s", tdb_name(tdb
) ? tdb_name(tdb
) : "unnamed", ptr
));
392 /****************************************************************************
393 Like tdb_open() but also setup a logging function that redirects to
394 the samba DEBUG() system.
395 ****************************************************************************/
397 TDB_CONTEXT
*tdb_open_log(const char *name
, int hash_size
, int tdb_flags
,
398 int open_flags
, mode_t mode
)
401 struct tdb_logging_context log_ctx
;
404 tdb_flags
|= TDB_NOMMAP
;
406 log_ctx
.log_fn
= tdb_log
;
407 log_ctx
.log_private
= NULL
;
409 if ((hash_size
== 0) && (name
!= NULL
)) {
410 const char *base
= strrchr_m(name
, '/');
417 hash_size
= lp_parm_int(-1, "tdb_hashsize", base
, 0);
420 tdb
= tdb_open_ex(name
, hash_size
, tdb_flags
,
421 open_flags
, mode
, &log_ctx
, NULL
);
428 /****************************************************************************
429 tdb_store, wrapped in a transaction. This way we make sure that a process
430 that dies within writing does not leave a corrupt tdb behind.
431 ****************************************************************************/
433 int tdb_trans_store(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA dbuf
,
438 if ((res
= tdb_transaction_start(tdb
)) != 0) {
439 DEBUG(5, ("tdb_transaction_start failed\n"));
443 if ((res
= tdb_store(tdb
, key
, dbuf
, flag
)) != 0) {
444 DEBUG(10, ("tdb_store failed\n"));
445 if (tdb_transaction_cancel(tdb
) != 0) {
446 smb_panic("Cancelling transaction failed");
451 if ((res
= tdb_transaction_commit(tdb
)) != 0) {
452 DEBUG(5, ("tdb_transaction_commit failed\n"));
458 /****************************************************************************
459 tdb_delete, wrapped in a transaction. This way we make sure that a process
460 that dies within deleting does not leave a corrupt tdb behind.
461 ****************************************************************************/
463 int tdb_trans_delete(struct tdb_context
*tdb
, TDB_DATA key
)
467 if ((res
= tdb_transaction_start(tdb
)) != 0) {
468 DEBUG(5, ("tdb_transaction_start failed\n"));
472 if ((res
= tdb_delete(tdb
, key
)) != 0) {
473 DEBUG(10, ("tdb_delete failed\n"));
474 if (tdb_transaction_cancel(tdb
) != 0) {
475 smb_panic("Cancelling transaction failed");
480 if ((res
= tdb_transaction_commit(tdb
)) != 0) {
481 DEBUG(5, ("tdb_transaction_commit failed\n"));
487 NTSTATUS
map_nt_error_from_tdb(enum TDB_ERROR err
)
489 NTSTATUS result
= NT_STATUS_INTERNAL_ERROR
;
493 result
= NT_STATUS_OK
;
495 case TDB_ERR_CORRUPT
:
496 result
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
499 result
= NT_STATUS_UNEXPECTED_IO_ERROR
;
502 result
= NT_STATUS_NO_MEMORY
;
505 result
= NT_STATUS_OBJECT_NAME_COLLISION
;
510 * TDB_ERR_LOCK is very broad, we could for example
511 * distinguish between fcntl locks and invalid lock
512 * sequences. So NT_STATUS_FILE_LOCK_CONFLICT is a
515 result
= NT_STATUS_FILE_LOCK_CONFLICT
;
519 case TDB_ERR_LOCK_TIMEOUT
:
521 * These two ones in the enum are not actually used
523 result
= NT_STATUS_FILE_LOCK_CONFLICT
;
525 case TDB_ERR_NOEXIST
:
526 result
= NT_STATUS_NOT_FOUND
;
529 result
= NT_STATUS_INVALID_PARAMETER
;
532 result
= NT_STATUS_ACCESS_DENIED
;
534 case TDB_ERR_NESTING
:
535 result
= NT_STATUS_INTERNAL_ERROR
;
541 int tdb_data_cmp(TDB_DATA t1
, TDB_DATA t2
)
544 if (t1
.dptr
== NULL
&& t2
.dptr
!= NULL
) {
547 if (t1
.dptr
!= NULL
&& t2
.dptr
== NULL
) {
550 if (t1
.dptr
== t2
.dptr
) {
551 return t1
.dsize
- t2
.dsize
;
553 ret
= memcmp(t1
.dptr
, t2
.dptr
, MIN(t1
.dsize
, t2
.dsize
));
555 return t1
.dsize
- t2
.dsize
;