lib/util: add debug_set_forced_log_priority()
[Samba.git] / source3 / lib / util_tdb.c
blobd85f676fbcf82bf44a5fbd177fa66a3f6be1068d
1 /*
2 Unix SMB/CIFS implementation.
3 tdb utility functions
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/>.
22 #include "includes.h"
23 #include "system/filesys.h"
24 #include "util_tdb.h"
25 #include "cbuf.h"
27 #undef malloc
28 #undef realloc
29 #undef calloc
30 #undef strdup
32 /****************************************************************************
33 Useful pair of routines for packing/unpacking data consisting of
34 integers and strings.
35 ****************************************************************************/
37 static size_t tdb_pack_va(uint8_t *buf, int bufsize, const char *fmt, va_list ap)
39 uint8_t bt;
40 uint16_t w;
41 uint32_t d;
42 int i;
43 void *p;
44 int len = 0;
45 char *s;
46 char c;
47 const char *fmt0 = fmt;
48 int bufsize0 = bufsize;
49 size_t to_write = 0;
50 while (*fmt) {
51 switch ((c = *fmt++)) {
52 case 'b': /* unsigned 8-bit integer */
53 len = 1;
54 bt = (uint8_t)va_arg(ap, int);
55 if (bufsize && bufsize >= len)
56 SSVAL(buf, 0, bt);
57 break;
58 case 'w': /* unsigned 16-bit integer */
59 len = 2;
60 w = (uint16_t)va_arg(ap, int);
61 if (bufsize && bufsize >= len)
62 SSVAL(buf, 0, w);
63 break;
64 case 'd': /* signed 32-bit integer (standard int in most systems) */
65 len = 4;
66 d = va_arg(ap, uint32_t);
67 if (bufsize && bufsize >= len)
68 SIVAL(buf, 0, d);
69 break;
70 case 'p': /* pointer */
71 len = 4;
72 p = va_arg(ap, void *);
73 d = p?1:0;
74 if (bufsize && bufsize >= len)
75 SIVAL(buf, 0, d);
76 break;
77 case 'P': /* null-terminated string */
78 case 'f': /* null-terminated string */
79 s = va_arg(ap,char *);
80 if (s == NULL) {
81 smb_panic("Invalid argument");
83 w = strlen(s);
84 len = w + 1;
85 if (bufsize && bufsize >= len)
86 memcpy(buf, s, len);
87 break;
88 case 'B': /* fixed-length string */
89 i = va_arg(ap, int);
90 s = va_arg(ap, char *);
91 len = 4+i;
92 if (bufsize && bufsize >= len) {
93 SIVAL(buf, 0, i);
94 if (s != NULL) {
95 memcpy(buf+4, s, i);
98 break;
99 default:
100 DEBUG(0,("Unknown tdb_pack format %c in %s\n",
101 c, fmt));
102 len = 0;
103 break;
106 to_write += len;
107 if (bufsize > 0) {
108 bufsize -= len;
109 buf += len;
111 if (bufsize < 0)
112 bufsize = 0;
115 DEBUG(18,("tdb_pack_va(%s, %d) -> %d\n",
116 fmt0, bufsize0, (int)to_write));
118 return to_write;
121 size_t tdb_pack(uint8_t *buf, int bufsize, const char *fmt, ...)
123 va_list ap;
124 size_t result;
126 va_start(ap, fmt);
127 result = tdb_pack_va(buf, bufsize, fmt, ap);
128 va_end(ap);
129 return result;
132 /****************************************************************************
133 Useful pair of routines for packing/unpacking data consisting of
134 integers and strings.
135 ****************************************************************************/
137 int tdb_unpack(const uint8_t *buf, int in_bufsize, const char *fmt, ...)
139 va_list ap;
140 uint8_t *bt;
141 uint16_t *w;
142 uint32_t *d;
143 size_t bufsize = in_bufsize;
144 size_t len;
145 uint32_t *i;
146 void **p;
147 char *s, **b, **ps;
148 char c;
149 const uint8_t *buf0 = buf;
150 const char *fmt0 = fmt;
152 va_start(ap, fmt);
154 while (*fmt) {
155 switch ((c=*fmt++)) {
156 case 'b': /* unsigned 8-bit integer */
157 len = 1;
158 bt = va_arg(ap, uint8_t *);
159 if (bufsize < len)
160 goto no_space;
161 *bt = SVAL(buf, 0);
162 break;
163 case 'w': /* unsigned 16-bit integer */
164 len = 2;
165 w = va_arg(ap, uint16_t *);
166 if (bufsize < len)
167 goto no_space;
168 *w = SVAL(buf, 0);
169 break;
170 case 'd': /* unsigned 32-bit integer (standard int in most systems) */
171 len = 4;
172 d = va_arg(ap, uint32_t *);
173 if (bufsize < len)
174 goto no_space;
175 *d = IVAL(buf, 0);
176 break;
177 case 'p': /* pointer */
178 len = 4;
179 p = va_arg(ap, void **);
180 if (bufsize < len)
181 goto no_space;
183 * This isn't a real pointer - only a token (1 or 0)
184 * to mark the fact a pointer is present.
187 *p = (void *)(IVAL(buf, 0) ? (void *)1 : NULL);
188 break;
189 case 'P': /* null-terminated string */
190 /* Return malloc'ed string. */
191 ps = va_arg(ap,char **);
192 len = strnlen((const char *)buf, bufsize) + 1;
193 if (bufsize < len)
194 goto no_space;
195 if (ps != NULL) {
196 *ps = SMB_STRDUP((const char *)buf);
197 if (*ps == NULL) {
198 goto no_space;
201 break;
202 case 'f': /* null-terminated string */
203 s = va_arg(ap,char *);
204 len = strnlen((const char *)buf, bufsize) + 1;
205 if (bufsize < len || len > sizeof(fstring))
206 goto no_space;
207 if (s != NULL) {
208 memcpy(s, buf, len);
210 break;
211 case 'B': /* fixed-length string */
212 i = va_arg(ap, uint32_t *);
213 b = va_arg(ap, char **);
214 len = 4;
215 if (bufsize < len)
216 goto no_space;
217 *i = IVAL(buf, 0);
218 if (! *i) {
219 *b = NULL;
220 break;
222 len += *i;
223 if (len < *i) {
224 goto no_space;
226 if (bufsize < len)
227 goto no_space;
228 if (b != NULL) {
229 *b = (char *)SMB_MALLOC(*i);
230 if (! *b)
231 goto no_space;
232 memcpy(*b, buf+4, *i);
234 break;
235 default:
236 DEBUG(0,("Unknown tdb_unpack format %c in %s\n",
237 c, fmt));
239 len = 0;
240 break;
243 buf += len;
244 bufsize -= len;
247 va_end(ap);
249 DEBUG(18,("tdb_unpack(%s, %d) -> %d\n",
250 fmt0, in_bufsize, (int)PTR_DIFF(buf, buf0)));
252 return PTR_DIFF(buf, buf0);
254 no_space:
255 va_end(ap);
256 return -1;
260 /****************************************************************************
261 Log tdb messages via DEBUG().
262 ****************************************************************************/
264 static void tdb_log(TDB_CONTEXT *tdb, enum tdb_debug_level level,
265 const char *format, ...) PRINTF_ATTRIBUTE(3,4);
267 static void tdb_log(TDB_CONTEXT *tdb, enum tdb_debug_level level, const char *format, ...)
269 va_list ap;
270 char *ptr = NULL;
271 int ret;
273 va_start(ap, format);
274 ret = vasprintf(&ptr, format, ap);
275 va_end(ap);
277 if ((ret == -1) || !*ptr)
278 return;
280 DEBUG((int)level, ("tdb(%s): %s", tdb_name(tdb) ? tdb_name(tdb) : "unnamed", ptr));
281 SAFE_FREE(ptr);
284 /****************************************************************************
285 Like tdb_open() but also setup a logging function that redirects to
286 the samba DEBUG() system.
287 ****************************************************************************/
289 TDB_CONTEXT *tdb_open_log(const char *name, int hash_size, int tdb_flags,
290 int open_flags, mode_t mode)
292 TDB_CONTEXT *tdb;
293 struct tdb_logging_context log_ctx = { .log_fn = tdb_log };
295 if (!lp_use_mmap())
296 tdb_flags |= TDB_NOMMAP;
298 if ((hash_size == 0) && (name != NULL)) {
299 const char *base = strrchr_m(name, '/');
300 if (base != NULL) {
301 base += 1;
303 else {
304 base = name;
306 hash_size = lp_parm_int(-1, "tdb_hashsize", base, 0);
309 tdb = tdb_open_ex(name, hash_size, tdb_flags,
310 open_flags, mode, &log_ctx, NULL);
311 if (!tdb)
312 return NULL;
314 return tdb;
317 int tdb_data_cmp(TDB_DATA t1, TDB_DATA t2)
319 int ret;
320 if (t1.dptr == NULL && t2.dptr != NULL) {
321 return -1;
323 if (t1.dptr != NULL && t2.dptr == NULL) {
324 return 1;
326 if (t1.dptr == t2.dptr) {
327 return t1.dsize - t2.dsize;
329 ret = memcmp(t1.dptr, t2.dptr, MIN(t1.dsize, t2.dsize));
330 if (ret == 0) {
331 return t1.dsize - t2.dsize;
333 return ret;
336 char *tdb_data_string(TALLOC_CTX *mem_ctx, TDB_DATA d)
338 int len;
339 char *ret = NULL;
340 cbuf *ost = cbuf_new(mem_ctx);
342 if (ost == NULL) {
343 return NULL;
346 len = cbuf_printf(ost, "%zu:", d.dsize);
347 if (len == -1) {
348 goto done;
351 if (d.dptr == NULL) {
352 len = cbuf_puts(ost, "<NULL>", -1);
353 } else {
354 len = cbuf_print_quoted(ost, (const char*)d.dptr, d.dsize);
356 if (len == -1) {
357 goto done;
360 cbuf_swapptr(ost, &ret, 0);
361 talloc_steal(mem_ctx, ret);
363 done:
364 talloc_free(ost);
365 return ret;
368 char *tdb_data_dbg(TDB_DATA d)
370 return hex_encode_talloc(talloc_tos(), d.dptr, d.dsize);
373 static sig_atomic_t gotalarm;
375 /***************************************************************
376 Signal function to tell us we timed out.
377 ****************************************************************/
379 static void gotalarm_sig(int signum)
381 gotalarm = 1;
384 /****************************************************************************
385 Lock a chain with timeout (in seconds).
386 ****************************************************************************/
388 static int tdb_chainlock_with_timeout_internal( TDB_CONTEXT *tdb, TDB_DATA key, unsigned int timeout, int rw_type)
390 /* Allow tdb_chainlock to be interrupted by an alarm. */
391 int ret;
392 gotalarm = 0;
394 if (timeout) {
395 CatchSignal(SIGALRM, gotalarm_sig);
396 tdb_setalarm_sigptr(tdb, &gotalarm);
397 alarm(timeout);
400 if (rw_type == F_RDLCK)
401 ret = tdb_chainlock_read(tdb, key);
402 else
403 ret = tdb_chainlock(tdb, key);
405 if (timeout) {
406 alarm(0);
407 tdb_setalarm_sigptr(tdb, NULL);
408 CatchSignal(SIGALRM, SIG_IGN);
409 if (gotalarm && (ret != 0)) {
410 DEBUG(0,("tdb_chainlock_with_timeout_internal: alarm (%u) timed out for key %s in tdb %s\n",
411 timeout, key.dptr, tdb_name(tdb)));
412 /* TODO: If we time out waiting for a lock, it might
413 * be nice to use F_GETLK to get the pid of the
414 * process currently holding the lock and print that
415 * as part of the debugging message. -- mbp */
416 return -1;
420 return ret == 0 ? 0 : -1;
423 /****************************************************************************
424 Write lock a chain. Return non-zero if timeout or lock failed.
425 ****************************************************************************/
427 int tdb_chainlock_with_timeout( TDB_CONTEXT *tdb, TDB_DATA key, unsigned int timeout)
429 return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_WRLCK);
432 int tdb_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval,
433 int timeout)
435 TDB_DATA key = string_term_tdb_data(keyval);
437 return tdb_chainlock_with_timeout(tdb, key, timeout);
440 /****************************************************************************
441 Read lock a chain by string. Return non-zero if timeout or lock failed.
442 ****************************************************************************/
444 int tdb_read_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval, unsigned int timeout)
446 TDB_DATA key = string_term_tdb_data(keyval);
448 return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_RDLCK);