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"
32 /* these are little tdb utility functions that are meant to make
33 dealing with a tdb database a little less cumbersome in Samba */
35 int tdb_trans_store_bystring(TDB_CONTEXT *tdb, const char *keystr,
36 TDB_DATA data, int flags)
38 TDB_DATA key = string_term_tdb_data(keystr);
40 return tdb_trans_store(tdb, key, data, flags);
43 /****************************************************************************
44 Useful pair of routines for packing/unpacking data consisting of
46 ****************************************************************************/
48 static size_t tdb_pack_va(uint8_t *buf, int bufsize, const char *fmt, va_list ap)
59 const char *fmt0 = fmt;
60 int bufsize0 = bufsize;
63 switch ((c = *fmt++)) {
64 case 'b': /* unsigned 8-bit integer */
66 bt = (uint8_t)va_arg(ap, int);
67 if (bufsize && bufsize >= len)
70 case 'w': /* unsigned 16-bit integer */
72 w = (uint16_t)va_arg(ap, int);
73 if (bufsize && bufsize >= len)
76 case 'd': /* signed 32-bit integer (standard int in most systems) */
78 d = va_arg(ap, uint32_t);
79 if (bufsize && bufsize >= len)
82 case 'p': /* pointer */
84 p = va_arg(ap, void *);
86 if (bufsize && bufsize >= len)
89 case 'P': /* null-terminated string */
90 s = va_arg(ap,char *);
93 if (bufsize && bufsize >= len)
96 case 'f': /* null-terminated string */
97 s = va_arg(ap,char *);
100 if (bufsize && bufsize >= len)
103 case 'B': /* fixed-length string */
105 s = va_arg(ap, char *);
107 if (bufsize && bufsize >= len) {
113 DEBUG(0,("Unknown tdb_pack format %c in %s\n",
126 DEBUG(18,("tdb_pack_va(%s, %d) -> %d\n",
127 fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
129 return PTR_DIFF(buf, buf0);
132 size_t tdb_pack(uint8_t *buf, int bufsize, const char *fmt, ...)
138 result = tdb_pack_va(buf, bufsize, fmt, ap);
143 /****************************************************************************
144 Useful pair of routines for packing/unpacking data consisting of
145 integers and strings.
146 ****************************************************************************/
148 int tdb_unpack(const uint8_t *buf, int in_bufsize, const char *fmt, ...)
154 size_t bufsize = in_bufsize;
160 const uint8_t *buf0 = buf;
161 const char *fmt0 = fmt;
166 switch ((c=*fmt++)) {
167 case 'b': /* unsigned 8-bit integer */
169 bt = va_arg(ap, uint8_t *);
174 case 'w': /* unsigned 16-bit integer */
176 w = va_arg(ap, uint16_t *);
181 case 'd': /* unsigned 32-bit integer (standard int in most systems) */
183 d = va_arg(ap, uint32_t *);
188 case 'p': /* pointer */
190 p = va_arg(ap, void **);
194 * This isn't a real pointer - only a token (1 or 0)
195 * to mark the fact a pointer is present.
198 *p = (void *)(IVAL(buf, 0) ? (void *)1 : NULL);
200 case 'P': /* null-terminated string */
201 /* Return malloc'ed string. */
202 ps = va_arg(ap,char **);
203 len = strnlen((const char *)buf, bufsize) + 1;
206 *ps = SMB_STRDUP((const char *)buf);
211 case 'f': /* null-terminated string */
212 s = va_arg(ap,char *);
213 len = strnlen((const char *)buf, bufsize) + 1;
214 if (bufsize < len || len > sizeof(fstring))
218 case 'B': /* fixed-length string */
219 i = va_arg(ap, int *);
220 b = va_arg(ap, char **);
232 *b = (char *)SMB_MALLOC(*i);
235 memcpy(*b, buf+4, *i);
238 DEBUG(0,("Unknown tdb_unpack format %c in %s\n",
251 DEBUG(18,("tdb_unpack(%s, %d) -> %d\n",
252 fmt0, in_bufsize, (int)PTR_DIFF(buf, buf0)));
254 return PTR_DIFF(buf, buf0);
262 /****************************************************************************
263 Log tdb messages via DEBUG().
264 ****************************************************************************/
266 static void tdb_log(TDB_CONTEXT *tdb, enum tdb_debug_level level,
267 const char *format, ...) PRINTF_ATTRIBUTE(3,4);
269 static void tdb_log(TDB_CONTEXT *tdb, enum tdb_debug_level level, const char *format, ...)
275 va_start(ap, format);
276 ret = vasprintf(&ptr, format, ap);
279 if ((ret == -1) || !*ptr)
282 DEBUG((int)level, ("tdb(%s): %s", tdb_name(tdb) ? tdb_name(tdb) : "unnamed", ptr));
286 /****************************************************************************
287 Like tdb_open() but also setup a logging function that redirects to
288 the samba DEBUG() system.
289 ****************************************************************************/
291 TDB_CONTEXT *tdb_open_log(const char *name, int hash_size, int tdb_flags,
292 int open_flags, mode_t mode)
295 struct tdb_logging_context log_ctx = { .log_fn = tdb_log };
298 tdb_flags |= TDB_NOMMAP;
300 if ((hash_size == 0) && (name != NULL)) {
301 const char *base = strrchr_m(name, '/');
308 hash_size = lp_parm_int(-1, "tdb_hashsize", base, 0);
311 tdb = tdb_open_ex(name, hash_size, tdb_flags,
312 open_flags, mode, &log_ctx, NULL);
319 /****************************************************************************
320 tdb_store, wrapped in a transaction. This way we make sure that a process
321 that dies within writing does not leave a corrupt tdb behind.
322 ****************************************************************************/
324 int tdb_trans_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
329 if ((res = tdb_transaction_start(tdb)) != 0) {
330 DEBUG(5, ("tdb_transaction_start failed\n"));
334 if ((res = tdb_store(tdb, key, dbuf, flag)) != 0) {
335 DEBUG(10, ("tdb_store failed\n"));
336 tdb_transaction_cancel(tdb);
340 if ((res = tdb_transaction_commit(tdb)) != 0) {
341 DEBUG(5, ("tdb_transaction_commit failed\n"));
347 /****************************************************************************
348 tdb_delete, wrapped in a transaction. This way we make sure that a process
349 that dies within deleting does not leave a corrupt tdb behind.
350 ****************************************************************************/
352 int tdb_trans_delete(struct tdb_context *tdb, TDB_DATA key)
356 if ((res = tdb_transaction_start(tdb)) != 0) {
357 DEBUG(5, ("tdb_transaction_start failed\n"));
361 if ((res = tdb_delete(tdb, key)) != 0) {
362 DEBUG(10, ("tdb_delete failed\n"));
363 tdb_transaction_cancel(tdb);
367 if ((res = tdb_transaction_commit(tdb)) != 0) {
368 DEBUG(5, ("tdb_transaction_commit failed\n"));
374 int tdb_data_cmp(TDB_DATA t1, TDB_DATA t2)
377 if (t1.dptr == NULL && t2.dptr != NULL) {
380 if (t1.dptr != NULL && t2.dptr == NULL) {
383 if (t1.dptr == t2.dptr) {
384 return t1.dsize - t2.dsize;
386 ret = memcmp(t1.dptr, t2.dptr, MIN(t1.dsize, t2.dsize));
388 return t1.dsize - t2.dsize;
393 char *tdb_data_string(TALLOC_CTX *mem_ctx, TDB_DATA d)
397 cbuf *ost = cbuf_new(mem_ctx);
403 len = cbuf_printf(ost, "%zu:", d.dsize);
408 if (d.dptr == NULL) {
409 len = cbuf_puts(ost, "<NULL>", -1);
411 len = cbuf_print_quoted(ost, (const char*)d.dptr, d.dsize);
417 cbuf_swapptr(ost, &ret, 0);
418 talloc_steal(mem_ctx, ret);
425 static sig_atomic_t gotalarm;
427 /***************************************************************
428 Signal function to tell us we timed out.
429 ****************************************************************/
431 static void gotalarm_sig(int signum)
436 /****************************************************************************
437 Lock a chain with timeout (in seconds).
438 ****************************************************************************/
440 static int tdb_chainlock_with_timeout_internal( TDB_CONTEXT *tdb, TDB_DATA key, unsigned int timeout, int rw_type)
442 /* Allow tdb_chainlock to be interrupted by an alarm. */
447 CatchSignal(SIGALRM, gotalarm_sig);
448 tdb_setalarm_sigptr(tdb, &gotalarm);
452 if (rw_type == F_RDLCK)
453 ret = tdb_chainlock_read(tdb, key);
455 ret = tdb_chainlock(tdb, key);
459 tdb_setalarm_sigptr(tdb, NULL);
460 CatchSignal(SIGALRM, SIG_IGN);
461 if (gotalarm && (ret != 0)) {
462 DEBUG(0,("tdb_chainlock_with_timeout_internal: alarm (%u) timed out for key %s in tdb %s\n",
463 timeout, key.dptr, tdb_name(tdb)));
464 /* TODO: If we time out waiting for a lock, it might
465 * be nice to use F_GETLK to get the pid of the
466 * process currently holding the lock and print that
467 * as part of the debugging message. -- mbp */
472 return ret == 0 ? 0 : -1;
475 /****************************************************************************
476 Write lock a chain. Return non-zero if timeout or lock failed.
477 ****************************************************************************/
479 int tdb_chainlock_with_timeout( TDB_CONTEXT *tdb, TDB_DATA key, unsigned int timeout)
481 return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_WRLCK);
484 int tdb_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval,
487 TDB_DATA key = string_term_tdb_data(keyval);
489 return tdb_chainlock_with_timeout(tdb, key, timeout);
492 /****************************************************************************
493 Read lock a chain by string. Return non-zero if timeout or lock failed.
494 ****************************************************************************/
496 int tdb_read_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval, unsigned int timeout)
498 TDB_DATA key = string_term_tdb_data(keyval);
500 return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_RDLCK);