2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Rafal Szczesniak 2002
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 /***************************************************************
29 Allow a caller to set a "alarm" flag that tdb can check to abort
30 a blocking lock on SIGALRM.
31 ***************************************************************/
33 static sig_atomic_t *palarm_fired;
35 static void tdb_set_lock_alarm(sig_atomic_t *palarm)
37 palarm_fired = palarm;
40 /* these are little tdb utility functions that are meant to make
41 dealing with a tdb database a little less cumbersome in Samba */
43 static SIG_ATOMIC_T gotalarm;
45 /***************************************************************
46 Signal function to tell us we timed out.
47 ****************************************************************/
49 static void gotalarm_sig(void)
54 /***************************************************************
55 Make a TDB_DATA and keep the const warning in one place
56 ****************************************************************/
58 TDB_DATA make_tdb_data(const char *dptr, size_t dsize)
61 ret.dptr = CONST_DISCARD(char *, dptr);
66 TDB_DATA string_tdb_data(const char *string)
68 return make_tdb_data(string, strlen(string));
71 /****************************************************************************
72 Lock a chain with timeout (in seconds).
73 ****************************************************************************/
75 static int tdb_chainlock_with_timeout_internal( TDB_CONTEXT *tdb, TDB_DATA key, unsigned int timeout, int rw_type)
77 /* Allow tdb_chainlock to be interrupted by an alarm. */
80 tdb_set_lock_alarm(CONST_DISCARD(sig_atomic_t *, &gotalarm));
83 CatchSignal(SIGALRM, SIGNAL_CAST gotalarm_sig);
87 if (rw_type == F_RDLCK)
88 ret = tdb_chainlock_read(tdb, key);
90 ret = tdb_chainlock(tdb, key);
94 CatchSignal(SIGALRM, SIGNAL_CAST SIG_IGN);
96 DEBUG(0,("tdb_chainlock_with_timeout_internal: alarm (%u) timed out for key %s in tdb %s\n",
97 timeout, key.dptr, tdb_name(tdb)));
98 /* TODO: If we time out waiting for a lock, it might
99 * be nice to use F_GETLK to get the pid of the
100 * process currently holding the lock and print that
101 * as part of the debugging message. -- mbp */
109 /****************************************************************************
110 Write lock a chain. Return -1 if timeout or lock failed.
111 ****************************************************************************/
113 int tdb_chainlock_with_timeout( TDB_CONTEXT *tdb, TDB_DATA key, unsigned int timeout)
115 return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_WRLCK);
118 /****************************************************************************
119 Lock a chain by string. Return -1 if timeout or lock failed.
120 ****************************************************************************/
122 int tdb_lock_bystring(TDB_CONTEXT *tdb, const char *keyval)
124 TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
126 return tdb_chainlock(tdb, key);
129 int tdb_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval,
132 TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
134 return tdb_chainlock_with_timeout(tdb, key, timeout);
137 /****************************************************************************
138 Unlock a chain by string.
139 ****************************************************************************/
141 void tdb_unlock_bystring(TDB_CONTEXT *tdb, const char *keyval)
143 TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
145 tdb_chainunlock(tdb, key);
148 /****************************************************************************
149 Read lock a chain by string. Return -1 if timeout or lock failed.
150 ****************************************************************************/
152 int tdb_read_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval, unsigned int timeout)
154 TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
156 return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_RDLCK);
159 /****************************************************************************
160 Read unlock a chain by string.
161 ****************************************************************************/
163 void tdb_read_unlock_bystring(TDB_CONTEXT *tdb, const char *keyval)
165 TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
167 tdb_chainunlock_read(tdb, key);
171 /****************************************************************************
172 Fetch a int32 value by a arbitrary blob key, return -1 if not found.
173 Output is int32 in native byte order.
174 ****************************************************************************/
176 int32 tdb_fetch_int32_byblob(TDB_CONTEXT *tdb, const char *keyval, size_t len)
178 TDB_DATA key = make_tdb_data(keyval, len);
182 data = tdb_fetch(tdb, key);
183 if (!data.dptr || data.dsize != sizeof(int32)) {
184 SAFE_FREE(data.dptr);
188 ret = IVAL(data.dptr,0);
189 SAFE_FREE(data.dptr);
193 /****************************************************************************
194 Fetch a int32 value by string key, return -1 if not found.
195 Output is int32 in native byte order.
196 ****************************************************************************/
198 int32 tdb_fetch_int32(TDB_CONTEXT *tdb, const char *keystr)
200 return tdb_fetch_int32_byblob(tdb, keystr, strlen(keystr) + 1);
203 /****************************************************************************
204 Store a int32 value by an arbitary blob key, return 0 on success, -1 on failure.
205 Input is int32 in native byte order. Output in tdb is in little-endian.
206 ****************************************************************************/
208 int tdb_store_int32_byblob(TDB_CONTEXT *tdb, const char *keystr, size_t len, int32 v)
210 TDB_DATA key = make_tdb_data(keystr, len);
215 data.dptr = (char *)&v_store;
216 data.dsize = sizeof(int32);
218 return tdb_store(tdb, key, data, TDB_REPLACE);
221 /****************************************************************************
222 Store a int32 value by string key, return 0 on success, -1 on failure.
223 Input is int32 in native byte order. Output in tdb is in little-endian.
224 ****************************************************************************/
226 int tdb_store_int32(TDB_CONTEXT *tdb, const char *keystr, int32 v)
228 return tdb_store_int32_byblob(tdb, keystr, strlen(keystr) + 1, v);
231 /****************************************************************************
232 Fetch a uint32 value by a arbitrary blob key, return -1 if not found.
233 Output is uint32 in native byte order.
234 ****************************************************************************/
236 BOOL tdb_fetch_uint32_byblob(TDB_CONTEXT *tdb, const char *keyval, size_t len, uint32 *value)
238 TDB_DATA key = make_tdb_data(keyval, len);
241 data = tdb_fetch(tdb, key);
242 if (!data.dptr || data.dsize != sizeof(uint32)) {
243 SAFE_FREE(data.dptr);
247 *value = IVAL(data.dptr,0);
248 SAFE_FREE(data.dptr);
252 /****************************************************************************
253 Fetch a uint32 value by string key, return -1 if not found.
254 Output is uint32 in native byte order.
255 ****************************************************************************/
257 BOOL tdb_fetch_uint32(TDB_CONTEXT *tdb, const char *keystr, uint32 *value)
259 return tdb_fetch_uint32_byblob(tdb, keystr, strlen(keystr) + 1, value);
262 /****************************************************************************
263 Store a uint32 value by an arbitary blob key, return 0 on success, -1 on failure.
264 Input is uint32 in native byte order. Output in tdb is in little-endian.
265 ****************************************************************************/
267 BOOL tdb_store_uint32_byblob(TDB_CONTEXT *tdb, const char *keystr, size_t len, uint32 value)
269 TDB_DATA key = make_tdb_data(keystr, len);
274 SIVAL(&v_store, 0, value);
275 data.dptr = (char *)&v_store;
276 data.dsize = sizeof(uint32);
278 if (tdb_store(tdb, key, data, TDB_REPLACE) == -1)
284 /****************************************************************************
285 Store a uint32 value by string key, return 0 on success, -1 on failure.
286 Input is uint32 in native byte order. Output in tdb is in little-endian.
287 ****************************************************************************/
289 BOOL tdb_store_uint32(TDB_CONTEXT *tdb, const char *keystr, uint32 value)
291 return tdb_store_uint32_byblob(tdb, keystr, strlen(keystr) + 1, value);
293 /****************************************************************************
294 Store a buffer by a null terminated string key. Return 0 on success, -1
296 ****************************************************************************/
298 int tdb_store_bystring(TDB_CONTEXT *tdb, const char *keystr, TDB_DATA data, int flags)
300 TDB_DATA key = make_tdb_data(keystr, strlen(keystr)+1);
302 return tdb_store(tdb, key, data, flags);
305 int tdb_trans_store_bystring(TDB_CONTEXT *tdb, const char *keystr,
306 TDB_DATA data, int flags)
308 TDB_DATA key = make_tdb_data(keystr, strlen(keystr)+1);
310 return tdb_trans_store(tdb, key, data, flags);
313 /****************************************************************************
314 Fetch a buffer using a null terminated string key. Don't forget to call
315 free() on the result dptr.
316 ****************************************************************************/
318 TDB_DATA tdb_fetch_bystring(TDB_CONTEXT *tdb, const char *keystr)
320 TDB_DATA key = make_tdb_data(keystr, strlen(keystr)+1);
322 return tdb_fetch(tdb, key);
325 /****************************************************************************
326 Delete an entry using a null terminated string key.
327 ****************************************************************************/
329 int tdb_delete_bystring(TDB_CONTEXT *tdb, const char *keystr)
331 TDB_DATA key = make_tdb_data(keystr, strlen(keystr)+1);
333 return tdb_delete(tdb, key);
336 /****************************************************************************
337 Atomic integer change. Returns old value. To create, set initial value in *oldval.
338 ****************************************************************************/
340 int32 tdb_change_int32_atomic(TDB_CONTEXT *tdb, const char *keystr, int32 *oldval, int32 change_val)
345 if (tdb_lock_bystring(tdb, keystr) == -1)
348 if ((val = tdb_fetch_int32(tdb, keystr)) == -1) {
349 /* The lookup failed */
350 if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
351 /* but not because it didn't exist */
355 /* Start with 'old' value */
359 /* It worked, set return value (oldval) to tdb data */
363 /* Increment value for storage and return next time */
366 if (tdb_store_int32(tdb, keystr, val) == -1)
373 tdb_unlock_bystring(tdb, keystr);
377 /****************************************************************************
378 Atomic unsigned integer change. Returns old value. To create, set initial value in *oldval.
379 ****************************************************************************/
381 BOOL tdb_change_uint32_atomic(TDB_CONTEXT *tdb, const char *keystr, uint32 *oldval, uint32 change_val)
386 if (tdb_lock_bystring(tdb, keystr) == -1)
389 if (!tdb_fetch_uint32(tdb, keystr, &val)) {
391 if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
392 /* and not because it didn't exist */
396 /* Start with 'old' value */
400 /* it worked, set return value (oldval) to tdb data */
405 /* get a new value to store */
408 if (!tdb_store_uint32(tdb, keystr, val))
415 tdb_unlock_bystring(tdb, keystr);
419 /****************************************************************************
420 Useful pair of routines for packing/unpacking data consisting of
421 integers and strings.
422 ****************************************************************************/
424 size_t tdb_pack_va(char *buf, int bufsize, const char *fmt, va_list ap)
435 const char *fmt0 = fmt;
436 int bufsize0 = bufsize;
439 switch ((c = *fmt++)) {
440 case 'b': /* unsigned 8-bit integer */
442 bt = (uint8)va_arg(ap, int);
443 if (bufsize && bufsize >= len)
446 case 'w': /* unsigned 16-bit integer */
448 w = (uint16)va_arg(ap, int);
449 if (bufsize && bufsize >= len)
452 case 'd': /* signed 32-bit integer (standard int in most systems) */
454 d = va_arg(ap, uint32);
455 if (bufsize && bufsize >= len)
458 case 'p': /* pointer */
460 p = va_arg(ap, void *);
462 if (bufsize && bufsize >= len)
465 case 'P': /* null-terminated string */
466 s = va_arg(ap,char *);
469 if (bufsize && bufsize >= len)
472 case 'f': /* null-terminated string */
473 s = va_arg(ap,char *);
476 if (bufsize && bufsize >= len)
479 case 'B': /* fixed-length string */
481 s = va_arg(ap, char *);
483 if (bufsize && bufsize >= len) {
489 DEBUG(0,("Unknown tdb_pack format %c in %s\n",
502 DEBUG(18,("tdb_pack_va(%s, %d) -> %d\n",
503 fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
505 return PTR_DIFF(buf, buf0);
508 size_t tdb_pack(char *buf, int bufsize, const char *fmt, ...)
514 result = tdb_pack_va(buf, bufsize, fmt, ap);
519 BOOL tdb_pack_append(TALLOC_CTX *mem_ctx, uint8 **buf, size_t *len,
520 const char *fmt, ...)
526 len1 = tdb_pack_va(NULL, 0, fmt, ap);
529 if (mem_ctx != NULL) {
530 *buf = TALLOC_REALLOC_ARRAY(mem_ctx, *buf, uint8,
533 *buf = SMB_REALLOC_ARRAY(*buf, uint8, (*len) + len1);
541 len2 = tdb_pack_va((char *)(*buf)+(*len), len1, fmt, ap);
553 /****************************************************************************
554 Useful pair of routines for packing/unpacking data consisting of
555 integers and strings.
556 ****************************************************************************/
558 int tdb_unpack(char *buf, int bufsize, const char *fmt, ...)
570 const char *fmt0 = fmt;
571 int bufsize0 = bufsize;
576 switch ((c=*fmt++)) {
579 bt = va_arg(ap, uint8 *);
586 w = va_arg(ap, uint16 *);
593 d = va_arg(ap, uint32 *);
600 p = va_arg(ap, void **);
604 * This isn't a real pointer - only a token (1 or 0)
605 * to mark the fact a pointer is present.
608 *p = (void *)(IVAL(buf, 0) ? (void *)1 : NULL);
611 s = va_arg(ap,char *);
612 len = strlen(buf) + 1;
613 if (bufsize < len || len > sizeof(pstring))
618 s = va_arg(ap,char *);
619 len = strlen(buf) + 1;
620 if (bufsize < len || len > sizeof(fstring))
625 i = va_arg(ap, int *);
626 b = va_arg(ap, char **);
638 *b = (char *)SMB_MALLOC(*i);
641 memcpy(*b, buf+4, *i);
644 DEBUG(0,("Unknown tdb_unpack format %c in %s\n",
657 DEBUG(18,("tdb_unpack(%s, %d) -> %d\n",
658 fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
660 return PTR_DIFF(buf, buf0);
667 /****************************************************************************
668 Log tdb messages via DEBUG().
669 ****************************************************************************/
671 static void tdb_log(TDB_CONTEXT *tdb, enum tdb_debug_level level, const char *format, ...)
676 va_start(ap, format);
677 vasprintf(&ptr, format, ap);
683 DEBUG((int)level, ("tdb(%s): %s", tdb_name(tdb) ? tdb_name(tdb) : "unnamed", ptr));
687 /****************************************************************************
688 Like tdb_open() but also setup a logging function that redirects to
689 the samba DEBUG() system.
690 ****************************************************************************/
692 TDB_CONTEXT *tdb_open_log(const char *name, int hash_size, int tdb_flags,
693 int open_flags, mode_t mode)
696 struct tdb_logging_context log_ctx;
699 tdb_flags |= TDB_NOMMAP;
701 log_ctx.log_fn = tdb_log;
702 log_ctx.log_private = NULL;
704 tdb = tdb_open_ex(name, hash_size, tdb_flags,
705 open_flags, mode, &log_ctx, NULL);
712 /****************************************************************************
713 Allow tdb_delete to be used as a tdb_traversal_fn.
714 ****************************************************************************/
716 int tdb_traverse_delete_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf,
719 return tdb_delete(the_tdb, key);
725 * Search across the whole tdb for keys that match the given pattern
726 * return the result as a list of keys
728 * @param tdb pointer to opened tdb file context
729 * @param pattern searching pattern used by fnmatch(3) functions
731 * @return list of keys found by looking up with given pattern
733 TDB_LIST_NODE *tdb_search_keys(TDB_CONTEXT *tdb, const char* pattern)
736 TDB_LIST_NODE *list = NULL;
737 TDB_LIST_NODE *rec = NULL;
739 for (key = tdb_firstkey(tdb); key.dptr; key = next) {
740 /* duplicate key string to ensure null-termination */
741 char *key_str = (char*) SMB_STRNDUP(key.dptr, key.dsize);
743 DEBUG(0, ("tdb_search_keys: strndup() failed!\n"));
744 smb_panic("strndup failed!\n");
747 DEBUG(18, ("checking %s for match to pattern %s\n", key_str, pattern));
749 next = tdb_nextkey(tdb, key);
751 /* do the pattern checking */
752 if (fnmatch(pattern, key_str, 0) == 0) {
753 rec = SMB_MALLOC_P(TDB_LIST_NODE);
758 DLIST_ADD_END(list, rec, TDB_LIST_NODE *);
760 DEBUG(18, ("checking %s matched pattern %s\n", key_str, pattern));
765 /* free duplicated key string */
775 * Free the list returned by tdb_search_keys
777 * @param node list of results found by tdb_search_keys
779 void tdb_search_list_free(TDB_LIST_NODE* node)
781 TDB_LIST_NODE *next_node;
784 next_node = node->next;
785 SAFE_FREE(node->node_key.dptr);
791 /****************************************************************************
792 tdb_store, wrapped in a transaction. This way we make sure that a process
793 that dies within writing does not leave a corrupt tdb behind.
794 ****************************************************************************/
796 int tdb_trans_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
801 if ((res = tdb_transaction_start(tdb)) != 0) {
802 DEBUG(5, ("tdb_transaction_start failed\n"));
806 if ((res = tdb_store(tdb, key, dbuf, flag)) != 0) {
807 DEBUG(10, ("tdb_store failed\n"));
808 if (tdb_transaction_cancel(tdb) != 0) {
809 smb_panic("Cancelling transaction failed\n");
814 if ((res = tdb_transaction_commit(tdb)) != 0) {
815 DEBUG(5, ("tdb_transaction_commit failed\n"));
821 /****************************************************************************
822 tdb_delete, wrapped in a transaction. This way we make sure that a process
823 that dies within deleting does not leave a corrupt tdb behind.
824 ****************************************************************************/
826 int tdb_trans_delete(struct tdb_context *tdb, TDB_DATA key)
830 if ((res = tdb_transaction_start(tdb)) != 0) {
831 DEBUG(5, ("tdb_transaction_start failed\n"));
835 if ((res = tdb_delete(tdb, key)) != 0) {
836 DEBUG(10, ("tdb_delete failed\n"));
837 if (tdb_transaction_cancel(tdb) != 0) {
838 smb_panic("Cancelling transaction failed\n");
843 if ((res = tdb_transaction_commit(tdb)) != 0) {
844 DEBUG(5, ("tdb_transaction_commit failed\n"));