2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /* these are little tdb utility functions that are meant to make
25 dealing with a tdb database a little less cumbersome in Samba */
27 static SIG_ATOMIC_T gotalarm;
29 /***************************************************************
30 Signal function to tell us we timed out.
31 ****************************************************************/
33 static void gotalarm_sig(void)
38 /***************************************************************
39 Make a TDB_DATA and keep the const warning in one place
40 ****************************************************************/
42 static TDB_DATA make_tdb_data(const char *dptr, size_t dsize)
50 /****************************************************************************
51 Lock a chain with timeout (in seconds).
52 ****************************************************************************/
54 static int tdb_chainlock_with_timeout( TDB_CONTEXT *tdb, TDB_DATA key, unsigned int timeout, int rw_type)
56 /* Allow tdb_chainlock to be interrupted by an alarm. */
59 tdb_set_lock_alarm(&gotalarm);
62 CatchSignal(SIGALRM, SIGNAL_CAST gotalarm_sig);
66 if (rw_type == F_RDLCK)
67 ret = tdb_chainlock_read(tdb, key);
69 ret = tdb_chainlock(tdb, key);
73 CatchSignal(SIGALRM, SIGNAL_CAST SIG_IGN);
75 DEBUG(0,("tdb_chainlock_with_timeout: alarm (%u) timed out for key %s in tdb %s\n",
76 timeout, key.dptr, tdb->name ));
84 /****************************************************************************
85 Lock a chain by string. Return -1 if timeout or lock failed.
86 ****************************************************************************/
88 int tdb_lock_bystring(TDB_CONTEXT *tdb, const char *keyval, unsigned int timeout)
90 TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
92 return tdb_chainlock_with_timeout(tdb, key, timeout, F_WRLCK);
95 /****************************************************************************
96 Unlock a chain by string.
97 ****************************************************************************/
99 void tdb_unlock_bystring(TDB_CONTEXT *tdb, const char *keyval)
101 TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
103 tdb_chainunlock(tdb, key);
106 /****************************************************************************
107 Read lock a chain by string. Return -1 if timeout or lock failed.
108 ****************************************************************************/
110 int tdb_read_lock_bystring(TDB_CONTEXT *tdb, const char *keyval, unsigned int timeout)
112 TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
114 return tdb_chainlock_with_timeout(tdb, key, timeout, F_RDLCK);
117 /****************************************************************************
118 Read unlock a chain by string.
119 ****************************************************************************/
121 void tdb_read_unlock_bystring(TDB_CONTEXT *tdb, const char *keyval)
123 TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
125 tdb_chainunlock_read(tdb, key);
129 /****************************************************************************
130 Fetch a int32 value by a arbitrary blob key, return -1 if not found.
131 Output is int32 in native byte order.
132 ****************************************************************************/
134 int32 tdb_fetch_int32_byblob(TDB_CONTEXT *tdb, const char *keyval, size_t len)
136 TDB_DATA key = make_tdb_data(keyval, len);
140 data = tdb_fetch(tdb, key);
141 if (!data.dptr || data.dsize != sizeof(int32)) {
142 SAFE_FREE(data.dptr);
146 ret = IVAL(data.dptr,0);
147 SAFE_FREE(data.dptr);
151 /****************************************************************************
152 Fetch a int32 value by string key, return -1 if not found.
153 Output is int32 in native byte order.
154 ****************************************************************************/
156 int32 tdb_fetch_int32(TDB_CONTEXT *tdb, const char *keystr)
158 return tdb_fetch_int32_byblob(tdb, keystr, strlen(keystr) + 1);
161 /****************************************************************************
162 Store a int32 value by an arbitary blob key, return 0 on success, -1 on failure.
163 Input is int32 in native byte order. Output in tdb is in little-endian.
164 ****************************************************************************/
166 int tdb_store_int32_byblob(TDB_CONTEXT *tdb, const char *keystr, size_t len, int32 v)
168 TDB_DATA key = make_tdb_data(keystr, len);
173 data.dptr = (void *)&v_store;
174 data.dsize = sizeof(int32);
176 return tdb_store(tdb, key, data, TDB_REPLACE);
179 /****************************************************************************
180 Store a int32 value by string key, return 0 on success, -1 on failure.
181 Input is int32 in native byte order. Output in tdb is in little-endian.
182 ****************************************************************************/
184 int tdb_store_int32(TDB_CONTEXT *tdb, const char *keystr, int32 v)
186 return tdb_store_int32_byblob(tdb, keystr, strlen(keystr) + 1, v);
189 /****************************************************************************
190 Fetch a uint32 value by a arbitrary blob key, return -1 if not found.
191 Output is uint32 in native byte order.
192 ****************************************************************************/
194 BOOL tdb_fetch_uint32_byblob(TDB_CONTEXT *tdb, const char *keyval, size_t len, uint32 *value)
196 TDB_DATA key = make_tdb_data(keyval, len);
199 data = tdb_fetch(tdb, key);
200 if (!data.dptr || data.dsize != sizeof(uint32)) {
201 SAFE_FREE(data.dptr);
205 *value = IVAL(data.dptr,0);
206 SAFE_FREE(data.dptr);
210 /****************************************************************************
211 Fetch a uint32 value by string key, return -1 if not found.
212 Output is uint32 in native byte order.
213 ****************************************************************************/
215 BOOL tdb_fetch_uint32(TDB_CONTEXT *tdb, const char *keystr, uint32 *value)
217 return tdb_fetch_uint32_byblob(tdb, keystr, strlen(keystr) + 1, value);
220 /****************************************************************************
221 Store a uint32 value by an arbitary blob key, return 0 on success, -1 on failure.
222 Input is uint32 in native byte order. Output in tdb is in little-endian.
223 ****************************************************************************/
225 BOOL tdb_store_uint32_byblob(TDB_CONTEXT *tdb, const char *keystr, size_t len, uint32 value)
227 TDB_DATA key = make_tdb_data(keystr, len);
232 SIVAL(&v_store, 0, value);
233 data.dptr = (void *)&v_store;
234 data.dsize = sizeof(uint32);
236 if (tdb_store(tdb, key, data, TDB_REPLACE) == -1)
242 /****************************************************************************
243 Store a uint32 value by string key, return 0 on success, -1 on failure.
244 Input is uint32 in native byte order. Output in tdb is in little-endian.
245 ****************************************************************************/
247 BOOL tdb_store_uint32(TDB_CONTEXT *tdb, const char *keystr, uint32 value)
249 return tdb_store_uint32_byblob(tdb, keystr, strlen(keystr) + 1, value);
251 /****************************************************************************
252 Store a buffer by a null terminated string key. Return 0 on success, -1
254 ****************************************************************************/
256 int tdb_store_by_string(TDB_CONTEXT *tdb, const char *keystr, TDB_DATA data, int flags)
258 TDB_DATA key = make_tdb_data(keystr, strlen(keystr)+1);
260 return tdb_store(tdb, key, data, flags);
263 /****************************************************************************
264 Fetch a buffer using a null terminated string key. Don't forget to call
265 free() on the result dptr.
266 ****************************************************************************/
268 TDB_DATA tdb_fetch_by_string(TDB_CONTEXT *tdb, const char *keystr)
270 TDB_DATA key = make_tdb_data(keystr, strlen(keystr)+1);
272 return tdb_fetch(tdb, key);
275 /****************************************************************************
276 Delete an entry using a null terminated string key.
277 ****************************************************************************/
279 int tdb_delete_by_string(TDB_CONTEXT *tdb, const char *keystr)
281 TDB_DATA key = make_tdb_data(keystr, strlen(keystr)+1);
283 return tdb_delete(tdb, key);
286 /****************************************************************************
287 Atomic integer change. Returns old value. To create, set initial value in *oldval.
288 ****************************************************************************/
290 int32 tdb_change_int32_atomic(TDB_CONTEXT *tdb, const char *keystr, int32 *oldval, int32 change_val)
295 if (tdb_lock_bystring(tdb, keystr,0) == -1)
298 if ((val = tdb_fetch_int32(tdb, keystr)) == -1) {
299 /* The lookup failed */
300 if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
301 /* but not becouse it didn't exist */
305 /* Start with 'old' value */
309 /* It worked, set return value (oldval) to tdb data */
313 /* Increment value for storage and return next time */
316 if (tdb_store_int32(tdb, keystr, val) == -1)
323 tdb_unlock_bystring(tdb, keystr);
327 /****************************************************************************
328 Atomic unsigned integer change. Returns old value. To create, set initial value in *oldval.
329 ****************************************************************************/
331 BOOL tdb_change_uint32_atomic(TDB_CONTEXT *tdb, const char *keystr, uint32 *oldval, uint32 change_val)
336 if (tdb_lock_bystring(tdb, keystr,0) == -1)
339 if (!tdb_fetch_uint32(tdb, keystr, &val)) {
341 if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
342 /* and not becouse it didn't exist */
346 /* Start with 'old' value */
350 /* it worked, set return value (oldval) to tdb data */
355 /* get a new value to store */
358 if (!tdb_store_uint32(tdb, keystr, val))
365 tdb_unlock_bystring(tdb, keystr);
369 /****************************************************************************
370 Useful pair of routines for packing/unpacking data consisting of
371 integers and strings.
372 ****************************************************************************/
374 size_t tdb_pack(char *buf, int bufsize, const char *fmt, ...)
385 const char *fmt0 = fmt;
386 int bufsize0 = bufsize;
391 switch ((c = *fmt++)) {
394 w = (uint16)va_arg(ap, int);
400 d = va_arg(ap, uint32);
406 p = va_arg(ap, void *);
412 s = va_arg(ap,char *);
419 s = va_arg(ap,char *);
427 s = va_arg(ap, char *);
429 if (bufsize >= len) {
435 DEBUG(0,("Unknown tdb_pack format %c in %s\n",
447 DEBUG(18,("tdb_pack(%s, %d) -> %d\n",
448 fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
450 return PTR_DIFF(buf, buf0);
453 /****************************************************************************
454 Useful pair of routines for packing/unpacking data consisting of
455 integers and strings.
456 ****************************************************************************/
458 int tdb_unpack(char *buf, int bufsize, const char *fmt, ...)
469 const char *fmt0 = fmt;
470 int bufsize0 = bufsize;
475 switch ((c=*fmt++)) {
478 w = va_arg(ap, uint16 *);
485 d = va_arg(ap, uint32 *);
492 p = va_arg(ap, void **);
495 *p = (void *)IVAL(buf, 0);
498 s = va_arg(ap,char *);
499 len = strlen(buf) + 1;
500 if (bufsize < len || len > sizeof(pstring))
505 s = va_arg(ap,char *);
506 len = strlen(buf) + 1;
507 if (bufsize < len || len > sizeof(fstring))
512 i = va_arg(ap, int *);
513 b = va_arg(ap, char **);
525 *b = (char *)malloc(*i);
528 memcpy(*b, buf+4, *i);
531 DEBUG(0,("Unknown tdb_unpack format %c in %s\n",
544 DEBUG(18,("tdb_unpack(%s, %d) -> %d\n",
545 fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
547 return PTR_DIFF(buf, buf0);
553 /****************************************************************************
554 Log tdb messages via DEBUG().
555 ****************************************************************************/
557 static void tdb_log(TDB_CONTEXT *tdb, int level, const char *format, ...)
562 va_start(ap, format);
563 vasprintf(&ptr, format, ap);
569 DEBUG(level, ("tdb(%s): %s", tdb->name ? tdb->name : "unnamed", ptr));
573 /****************************************************************************
574 Like tdb_open() but also setup a logging function that redirects to
575 the samba DEBUG() system.
576 ****************************************************************************/
578 TDB_CONTEXT *tdb_open_log(const char *name, int hash_size, int tdb_flags,
579 int open_flags, mode_t mode)
584 tdb_flags |= TDB_NOMMAP;
586 tdb = tdb_open_ex(name, hash_size, tdb_flags,
587 open_flags, mode, tdb_log);
595 /****************************************************************************
596 Allow tdb_delete to be used as a tdb_traversal_fn.
597 ****************************************************************************/
599 int tdb_traverse_delete_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf,
602 return tdb_delete(the_tdb, key);
608 * Search across the whole tdb for keys that match the given pattern
609 * return the result as a list of keys
611 * @param tdb pointer to opened tdb file context
612 * @param pattern searching pattern used by fnmatch(3) functions
614 * @return list of keys found by looking up with given pattern
616 TDB_LIST_NODE *tdb_search_keys(TDB_CONTEXT *tdb, const char* pattern)
619 TDB_LIST_NODE *list = NULL;
620 TDB_LIST_NODE *rec = NULL;
621 TDB_LIST_NODE *tmp = NULL;
623 for (key = tdb_firstkey(tdb); key.dptr; key = next) {
624 /* duplicate key string to ensure null-termination */
625 char *key_str = (char*) strndup(key.dptr, key.dsize);
627 DEBUG(0, ("tdb_search_keys: strndup() failed!\n"));
628 smb_panic("strndup failed!\n");
631 DEBUG(18, ("checking %s for match to pattern %s\n", key_str, pattern));
633 next = tdb_nextkey(tdb, key);
635 /* do the pattern checking */
636 if (fnmatch(pattern, key_str, 0) == 0) {
637 rec = (TDB_LIST_NODE*) malloc(sizeof(*rec));
642 DLIST_ADD_END(list, rec, tmp);
644 DEBUG(18, ("checking %s matched pattern %s\n", key_str, pattern));
649 /* free duplicated key string */
659 * Free the list returned by tdb_search_keys
661 * @param node list of results found by tdb_search_keys
663 void tdb_search_list_free(TDB_LIST_NODE* node)
665 TDB_LIST_NODE *next_node;
668 next_node = node->next;