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_internal( 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_internal: alarm (%u) timed out for key %s in tdb %s\n",
76 timeout, key.dptr, tdb->name ));
77 /* TODO: If we time out waiting for a lock, it might
78 * be nice to use F_GETLK to get the pid of the
79 * process currently holding the lock and print that
80 * as part of the debugging message. -- mbp */
88 /****************************************************************************
89 Write lock a chain. Return -1 if timeout or lock failed.
90 ****************************************************************************/
92 int tdb_chainlock_with_timeout( TDB_CONTEXT *tdb, TDB_DATA key, unsigned int timeout)
94 return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_WRLCK);
97 /****************************************************************************
98 Lock a chain by string. Return -1 if timeout or lock failed.
99 ****************************************************************************/
101 int tdb_lock_bystring(TDB_CONTEXT *tdb, const char *keyval, unsigned int timeout)
103 TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
105 return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_WRLCK);
108 /****************************************************************************
109 Unlock a chain by string.
110 ****************************************************************************/
112 void tdb_unlock_bystring(TDB_CONTEXT *tdb, const char *keyval)
114 TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
116 tdb_chainunlock(tdb, key);
119 /****************************************************************************
120 Read lock a chain by string. Return -1 if timeout or lock failed.
121 ****************************************************************************/
123 int tdb_read_lock_bystring(TDB_CONTEXT *tdb, const char *keyval, unsigned int timeout)
125 TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
127 return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_RDLCK);
130 /****************************************************************************
131 Read unlock a chain by string.
132 ****************************************************************************/
134 void tdb_read_unlock_bystring(TDB_CONTEXT *tdb, const char *keyval)
136 TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
138 tdb_chainunlock_read(tdb, key);
142 /****************************************************************************
143 Fetch a int32 value by a arbitrary blob key, return -1 if not found.
144 Output is int32 in native byte order.
145 ****************************************************************************/
147 int32 tdb_fetch_int32_byblob(TDB_CONTEXT *tdb, const char *keyval, size_t len)
149 TDB_DATA key = make_tdb_data(keyval, len);
153 data = tdb_fetch(tdb, key);
154 if (!data.dptr || data.dsize != sizeof(int32)) {
155 SAFE_FREE(data.dptr);
159 ret = IVAL(data.dptr,0);
160 SAFE_FREE(data.dptr);
164 /****************************************************************************
165 Fetch a int32 value by string key, return -1 if not found.
166 Output is int32 in native byte order.
167 ****************************************************************************/
169 int32 tdb_fetch_int32(TDB_CONTEXT *tdb, const char *keystr)
171 return tdb_fetch_int32_byblob(tdb, keystr, strlen(keystr) + 1);
174 /****************************************************************************
175 Store a int32 value by an arbitary blob key, return 0 on success, -1 on failure.
176 Input is int32 in native byte order. Output in tdb is in little-endian.
177 ****************************************************************************/
179 int tdb_store_int32_byblob(TDB_CONTEXT *tdb, const char *keystr, size_t len, int32 v)
181 TDB_DATA key = make_tdb_data(keystr, len);
186 data.dptr = (void *)&v_store;
187 data.dsize = sizeof(int32);
189 return tdb_store(tdb, key, data, TDB_REPLACE);
192 /****************************************************************************
193 Store a int32 value by string key, return 0 on success, -1 on failure.
194 Input is int32 in native byte order. Output in tdb is in little-endian.
195 ****************************************************************************/
197 int tdb_store_int32(TDB_CONTEXT *tdb, const char *keystr, int32 v)
199 return tdb_store_int32_byblob(tdb, keystr, strlen(keystr) + 1, v);
202 /****************************************************************************
203 Fetch a uint32 value by a arbitrary blob key, return -1 if not found.
204 Output is uint32 in native byte order.
205 ****************************************************************************/
207 BOOL tdb_fetch_uint32_byblob(TDB_CONTEXT *tdb, const char *keyval, size_t len, uint32 *value)
209 TDB_DATA key = make_tdb_data(keyval, len);
212 data = tdb_fetch(tdb, key);
213 if (!data.dptr || data.dsize != sizeof(uint32)) {
214 SAFE_FREE(data.dptr);
218 *value = IVAL(data.dptr,0);
219 SAFE_FREE(data.dptr);
223 /****************************************************************************
224 Fetch a uint32 value by string key, return -1 if not found.
225 Output is uint32 in native byte order.
226 ****************************************************************************/
228 BOOL tdb_fetch_uint32(TDB_CONTEXT *tdb, const char *keystr, uint32 *value)
230 return tdb_fetch_uint32_byblob(tdb, keystr, strlen(keystr) + 1, value);
233 /****************************************************************************
234 Store a uint32 value by an arbitary blob key, return 0 on success, -1 on failure.
235 Input is uint32 in native byte order. Output in tdb is in little-endian.
236 ****************************************************************************/
238 BOOL tdb_store_uint32_byblob(TDB_CONTEXT *tdb, const char *keystr, size_t len, uint32 value)
240 TDB_DATA key = make_tdb_data(keystr, len);
245 SIVAL(&v_store, 0, value);
246 data.dptr = (void *)&v_store;
247 data.dsize = sizeof(uint32);
249 if (tdb_store(tdb, key, data, TDB_REPLACE) == -1)
255 /****************************************************************************
256 Store a uint32 value by string key, return 0 on success, -1 on failure.
257 Input is uint32 in native byte order. Output in tdb is in little-endian.
258 ****************************************************************************/
260 BOOL tdb_store_uint32(TDB_CONTEXT *tdb, const char *keystr, uint32 value)
262 return tdb_store_uint32_byblob(tdb, keystr, strlen(keystr) + 1, value);
264 /****************************************************************************
265 Store a buffer by a null terminated string key. Return 0 on success, -1
267 ****************************************************************************/
269 int tdb_store_by_string(TDB_CONTEXT *tdb, const char *keystr, TDB_DATA data, int flags)
271 TDB_DATA key = make_tdb_data(keystr, strlen(keystr)+1);
273 return tdb_store(tdb, key, data, flags);
276 /****************************************************************************
277 Fetch a buffer using a null terminated string key. Don't forget to call
278 free() on the result dptr.
279 ****************************************************************************/
281 TDB_DATA tdb_fetch_by_string(TDB_CONTEXT *tdb, const char *keystr)
283 TDB_DATA key = make_tdb_data(keystr, strlen(keystr)+1);
285 return tdb_fetch(tdb, key);
288 /****************************************************************************
289 Delete an entry using a null terminated string key.
290 ****************************************************************************/
292 int tdb_delete_by_string(TDB_CONTEXT *tdb, const char *keystr)
294 TDB_DATA key = make_tdb_data(keystr, strlen(keystr)+1);
296 return tdb_delete(tdb, key);
299 /****************************************************************************
300 Atomic integer change. Returns old value. To create, set initial value in *oldval.
301 ****************************************************************************/
303 int32 tdb_change_int32_atomic(TDB_CONTEXT *tdb, const char *keystr, int32 *oldval, int32 change_val)
308 if (tdb_lock_bystring(tdb, keystr,0) == -1)
311 if ((val = tdb_fetch_int32(tdb, keystr)) == -1) {
312 /* The lookup failed */
313 if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
314 /* but not becouse it didn't exist */
318 /* Start with 'old' value */
322 /* It worked, set return value (oldval) to tdb data */
326 /* Increment value for storage and return next time */
329 if (tdb_store_int32(tdb, keystr, val) == -1)
336 tdb_unlock_bystring(tdb, keystr);
340 /****************************************************************************
341 Atomic unsigned integer change. Returns old value. To create, set initial value in *oldval.
342 ****************************************************************************/
344 BOOL tdb_change_uint32_atomic(TDB_CONTEXT *tdb, const char *keystr, uint32 *oldval, uint32 change_val)
349 if (tdb_lock_bystring(tdb, keystr,0) == -1)
352 if (!tdb_fetch_uint32(tdb, keystr, &val)) {
354 if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
355 /* and not becouse it didn't exist */
359 /* Start with 'old' value */
363 /* it worked, set return value (oldval) to tdb data */
368 /* get a new value to store */
371 if (!tdb_store_uint32(tdb, keystr, val))
378 tdb_unlock_bystring(tdb, keystr);
382 /****************************************************************************
383 Useful pair of routines for packing/unpacking data consisting of
384 integers and strings.
385 ****************************************************************************/
387 size_t tdb_pack(char *buf, int bufsize, const char *fmt, ...)
398 const char *fmt0 = fmt;
399 int bufsize0 = bufsize;
404 switch ((c = *fmt++)) {
407 w = (uint16)va_arg(ap, int);
408 if (bufsize && bufsize >= len)
413 d = va_arg(ap, uint32);
414 if (bufsize && bufsize >= len)
419 p = va_arg(ap, void *);
421 if (bufsize && bufsize >= len)
425 s = va_arg(ap,char *);
428 if (bufsize && bufsize >= len)
432 s = va_arg(ap,char *);
435 if (bufsize && bufsize >= len)
440 s = va_arg(ap, char *);
442 if (bufsize && bufsize >= len) {
448 DEBUG(0,("Unknown tdb_pack format %c in %s\n",
463 DEBUG(18,("tdb_pack(%s, %d) -> %d\n",
464 fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
466 return PTR_DIFF(buf, buf0);
469 /****************************************************************************
470 Useful pair of routines for packing/unpacking data consisting of
471 integers and strings.
472 ****************************************************************************/
474 int tdb_unpack(char *buf, int bufsize, const char *fmt, ...)
485 const char *fmt0 = fmt;
486 int bufsize0 = bufsize;
491 switch ((c=*fmt++)) {
494 w = va_arg(ap, uint16 *);
501 d = va_arg(ap, uint32 *);
508 p = va_arg(ap, void **);
511 *p = (void *)IVAL(buf, 0);
514 s = va_arg(ap,char *);
515 len = strlen(buf) + 1;
516 if (bufsize < len || len > sizeof(pstring))
521 s = va_arg(ap,char *);
522 len = strlen(buf) + 1;
523 if (bufsize < len || len > sizeof(fstring))
528 i = va_arg(ap, int *);
529 b = va_arg(ap, char **);
541 *b = (char *)malloc(*i);
544 memcpy(*b, buf+4, *i);
547 DEBUG(0,("Unknown tdb_unpack format %c in %s\n",
560 DEBUG(18,("tdb_unpack(%s, %d) -> %d\n",
561 fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
563 return PTR_DIFF(buf, buf0);
569 /****************************************************************************
570 Log tdb messages via DEBUG().
571 ****************************************************************************/
573 static void tdb_log(TDB_CONTEXT *tdb, int level, const char *format, ...)
578 va_start(ap, format);
579 vasprintf(&ptr, format, ap);
585 DEBUG(level, ("tdb(%s): %s", tdb->name ? tdb->name : "unnamed", ptr));
589 /****************************************************************************
590 Like tdb_open() but also setup a logging function that redirects to
591 the samba DEBUG() system.
592 ****************************************************************************/
594 TDB_CONTEXT *tdb_open_log(const char *name, int hash_size, int tdb_flags,
595 int open_flags, mode_t mode)
600 tdb_flags |= TDB_NOMMAP;
602 tdb = tdb_open_ex(name, hash_size, tdb_flags,
603 open_flags, mode, tdb_log);
611 /****************************************************************************
612 Allow tdb_delete to be used as a tdb_traversal_fn.
613 ****************************************************************************/
615 int tdb_traverse_delete_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf,
618 return tdb_delete(the_tdb, key);
624 * Search across the whole tdb for keys that match the given pattern
625 * return the result as a list of keys
627 * @param tdb pointer to opened tdb file context
628 * @param pattern searching pattern used by fnmatch(3) functions
630 * @return list of keys found by looking up with given pattern
632 TDB_LIST_NODE *tdb_search_keys(TDB_CONTEXT *tdb, const char* pattern)
635 TDB_LIST_NODE *list = NULL;
636 TDB_LIST_NODE *rec = NULL;
637 TDB_LIST_NODE *tmp = NULL;
639 for (key = tdb_firstkey(tdb); key.dptr; key = next) {
640 /* duplicate key string to ensure null-termination */
641 char *key_str = (char*) strndup(key.dptr, key.dsize);
643 DEBUG(0, ("tdb_search_keys: strndup() failed!\n"));
644 smb_panic("strndup failed!\n");
647 DEBUG(18, ("checking %s for match to pattern %s\n", key_str, pattern));
649 next = tdb_nextkey(tdb, key);
651 /* do the pattern checking */
652 if (fnmatch(pattern, key_str, 0) == 0) {
653 rec = (TDB_LIST_NODE*) malloc(sizeof(*rec));
658 DLIST_ADD_END(list, rec, tmp);
660 DEBUG(18, ("checking %s matched pattern %s\n", key_str, pattern));
665 /* free duplicated key string */
675 * Free the list returned by tdb_search_keys
677 * @param node list of results found by tdb_search_keys
679 void tdb_search_list_free(TDB_LIST_NODE* node)
681 TDB_LIST_NODE *next_node;
684 next_node = node->next;