s3:smbd: make typedef write_cache private to fileio.c
[Samba/gebeck_regimport.git] / source3 / lib / util_tdb.c
blobe9965ed838d484b377076c147337a089c825c5c4
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"
26 #undef malloc
27 #undef realloc
28 #undef calloc
29 #undef strdup
31 #ifdef BUILD_TDB2
32 static struct flock flock_struct;
34 /* Return a value which is none of v1, v2 or v3. */
35 static inline short int invalid_value(short int v1, short int v2, short int v3)
37 short int try = (v1+v2+v3)^((v1+v2+v3) << 16);
38 while (try == v1 || try == v2 || try == v3)
39 try++;
40 return try;
43 /* We invalidate in as many ways as we can, so the OS rejects it */
44 static void invalidate_flock_struct(int signum)
46 flock_struct.l_type = invalid_value(F_RDLCK, F_WRLCK, F_UNLCK);
47 flock_struct.l_whence = invalid_value(SEEK_SET, SEEK_CUR, SEEK_END);
48 flock_struct.l_start = -1;
49 /* A large negative. */
50 flock_struct.l_len = (((off_t)1 << (sizeof(off_t)*CHAR_BIT - 1)) + 1);
53 static int timeout_lock(int fd, int rw, off_t off, off_t len, bool waitflag,
54 void *_timeout)
56 int ret, saved_errno;
57 unsigned int timeout = *(unsigned int *)_timeout;
59 flock_struct.l_type = rw;
60 flock_struct.l_whence = SEEK_SET;
61 flock_struct.l_start = off;
62 flock_struct.l_len = len;
64 CatchSignal(SIGALRM, invalidate_flock_struct);
65 alarm(timeout);
67 for (;;) {
68 if (waitflag)
69 ret = fcntl(fd, F_SETLKW, &flock_struct);
70 else
71 ret = fcntl(fd, F_SETLK, &flock_struct);
73 if (ret == 0)
74 break;
76 /* Not signalled? Something else went wrong. */
77 if (flock_struct.l_len == len) {
78 if (errno == EAGAIN || errno == EINTR)
79 continue;
80 saved_errno = errno;
81 break;
82 } else {
83 saved_errno = EINTR;
84 break;
88 alarm(0);
89 errno = saved_errno;
90 return ret;
93 static int tdb_chainlock_with_timeout_internal(struct tdb_context *tdb,
94 TDB_DATA key,
95 unsigned int timeout,
96 int rw_type)
98 union tdb_attribute locking;
99 enum TDB_ERROR ecode;
101 if (timeout) {
102 locking.base.attr = TDB_ATTRIBUTE_FLOCK;
103 ecode = tdb_get_attribute(tdb, &locking);
104 if (ecode != TDB_SUCCESS)
105 return -1;
107 /* Replace locking function with our own. */
108 locking.flock.data = &timeout;
109 locking.flock.lock = timeout_lock;
111 ecode = tdb_set_attribute(tdb, &locking);
112 if (ecode != TDB_SUCCESS)
113 return -1;
115 if (rw_type == F_RDLCK)
116 ecode = tdb_chainlock_read(tdb, key);
117 else
118 ecode = tdb_chainlock(tdb, key);
120 if (timeout) {
121 tdb_unset_attribute(tdb, TDB_ATTRIBUTE_FLOCK);
123 return ecode == TDB_SUCCESS ? 0 : -1;
125 #else
126 /* these are little tdb utility functions that are meant to make
127 dealing with a tdb database a little less cumbersome in Samba */
129 static SIG_ATOMIC_T gotalarm;
131 /***************************************************************
132 Signal function to tell us we timed out.
133 ****************************************************************/
135 static void gotalarm_sig(int signum)
137 gotalarm = 1;
140 /****************************************************************************
141 Lock a chain with timeout (in seconds).
142 ****************************************************************************/
144 static int tdb_chainlock_with_timeout_internal( TDB_CONTEXT *tdb, TDB_DATA key, unsigned int timeout, int rw_type)
146 /* Allow tdb_chainlock to be interrupted by an alarm. */
147 int ret;
148 gotalarm = 0;
150 if (timeout) {
151 CatchSignal(SIGALRM, gotalarm_sig);
152 tdb_setalarm_sigptr(tdb, &gotalarm);
153 alarm(timeout);
156 if (rw_type == F_RDLCK)
157 ret = tdb_chainlock_read(tdb, key);
158 else
159 ret = tdb_chainlock(tdb, key);
161 if (timeout) {
162 alarm(0);
163 tdb_setalarm_sigptr(tdb, NULL);
164 CatchSignal(SIGALRM, SIG_IGN);
165 if (gotalarm && (ret != 0)) {
166 DEBUG(0,("tdb_chainlock_with_timeout_internal: alarm (%u) timed out for key %s in tdb %s\n",
167 timeout, key.dptr, tdb_name(tdb)));
168 /* TODO: If we time out waiting for a lock, it might
169 * be nice to use F_GETLK to get the pid of the
170 * process currently holding the lock and print that
171 * as part of the debugging message. -- mbp */
172 return -1;
176 return ret == 0 ? 0 : -1;
178 #endif /* TDB1 */
180 /****************************************************************************
181 Write lock a chain. Return non-zero if timeout or lock failed.
182 ****************************************************************************/
184 int tdb_chainlock_with_timeout( TDB_CONTEXT *tdb, TDB_DATA key, unsigned int timeout)
186 return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_WRLCK);
189 int tdb_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval,
190 int timeout)
192 TDB_DATA key = string_term_tdb_data(keyval);
194 return tdb_chainlock_with_timeout(tdb, key, timeout);
197 /****************************************************************************
198 Read lock a chain by string. Return non-zero if timeout or lock failed.
199 ****************************************************************************/
201 int tdb_read_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval, unsigned int timeout)
203 TDB_DATA key = string_term_tdb_data(keyval);
205 return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_RDLCK);
211 int tdb_trans_store_bystring(TDB_CONTEXT *tdb, const char *keystr,
212 TDB_DATA data, int flags)
214 TDB_DATA key = string_term_tdb_data(keystr);
216 return tdb_trans_store(tdb, key, data, flags);
219 /****************************************************************************
220 Useful pair of routines for packing/unpacking data consisting of
221 integers and strings.
222 ****************************************************************************/
224 static size_t tdb_pack_va(uint8 *buf, int bufsize, const char *fmt, va_list ap)
226 uint8 bt;
227 uint16 w;
228 uint32 d;
229 int i;
230 void *p;
231 int len;
232 char *s;
233 char c;
234 uint8 *buf0 = buf;
235 const char *fmt0 = fmt;
236 int bufsize0 = bufsize;
238 while (*fmt) {
239 switch ((c = *fmt++)) {
240 case 'b': /* unsigned 8-bit integer */
241 len = 1;
242 bt = (uint8)va_arg(ap, int);
243 if (bufsize && bufsize >= len)
244 SSVAL(buf, 0, bt);
245 break;
246 case 'w': /* unsigned 16-bit integer */
247 len = 2;
248 w = (uint16)va_arg(ap, int);
249 if (bufsize && bufsize >= len)
250 SSVAL(buf, 0, w);
251 break;
252 case 'd': /* signed 32-bit integer (standard int in most systems) */
253 len = 4;
254 d = va_arg(ap, uint32);
255 if (bufsize && bufsize >= len)
256 SIVAL(buf, 0, d);
257 break;
258 case 'p': /* pointer */
259 len = 4;
260 p = va_arg(ap, void *);
261 d = p?1:0;
262 if (bufsize && bufsize >= len)
263 SIVAL(buf, 0, d);
264 break;
265 case 'P': /* null-terminated string */
266 s = va_arg(ap,char *);
267 w = strlen(s);
268 len = w + 1;
269 if (bufsize && bufsize >= len)
270 memcpy(buf, s, len);
271 break;
272 case 'f': /* null-terminated string */
273 s = va_arg(ap,char *);
274 w = strlen(s);
275 len = w + 1;
276 if (bufsize && bufsize >= len)
277 memcpy(buf, s, len);
278 break;
279 case 'B': /* fixed-length string */
280 i = va_arg(ap, int);
281 s = va_arg(ap, char *);
282 len = 4+i;
283 if (bufsize && bufsize >= len) {
284 SIVAL(buf, 0, i);
285 memcpy(buf+4, s, i);
287 break;
288 default:
289 DEBUG(0,("Unknown tdb_pack format %c in %s\n",
290 c, fmt));
291 len = 0;
292 break;
295 buf += len;
296 if (bufsize)
297 bufsize -= len;
298 if (bufsize < 0)
299 bufsize = 0;
302 DEBUG(18,("tdb_pack_va(%s, %d) -> %d\n",
303 fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
305 return PTR_DIFF(buf, buf0);
308 size_t tdb_pack(uint8 *buf, int bufsize, const char *fmt, ...)
310 va_list ap;
311 size_t result;
313 va_start(ap, fmt);
314 result = tdb_pack_va(buf, bufsize, fmt, ap);
315 va_end(ap);
316 return result;
319 bool tdb_pack_append(TALLOC_CTX *mem_ctx, uint8 **buf, size_t *len,
320 const char *fmt, ...)
322 va_list ap;
323 size_t len1, len2;
325 va_start(ap, fmt);
326 len1 = tdb_pack_va(NULL, 0, fmt, ap);
327 va_end(ap);
329 if (mem_ctx != NULL) {
330 *buf = talloc_realloc(mem_ctx, *buf, uint8,
331 (*len) + len1);
332 } else {
333 *buf = SMB_REALLOC_ARRAY(*buf, uint8, (*len) + len1);
336 if (*buf == NULL) {
337 return False;
340 va_start(ap, fmt);
341 len2 = tdb_pack_va((*buf)+(*len), len1, fmt, ap);
342 va_end(ap);
344 if (len1 != len2) {
345 return False;
348 *len += len2;
350 return True;
353 /****************************************************************************
354 Useful pair of routines for packing/unpacking data consisting of
355 integers and strings.
356 ****************************************************************************/
358 int tdb_unpack(const uint8 *buf, int bufsize, const char *fmt, ...)
360 va_list ap;
361 uint8 *bt;
362 uint16 *w;
363 uint32 *d;
364 int len;
365 int *i;
366 void **p;
367 char *s, **b, **ps;
368 char c;
369 const uint8 *buf0 = buf;
370 const char *fmt0 = fmt;
371 int bufsize0 = bufsize;
373 va_start(ap, fmt);
375 while (*fmt) {
376 switch ((c=*fmt++)) {
377 case 'b': /* unsigned 8-bit integer */
378 len = 1;
379 bt = va_arg(ap, uint8 *);
380 if (bufsize < len)
381 goto no_space;
382 *bt = SVAL(buf, 0);
383 break;
384 case 'w': /* unsigned 16-bit integer */
385 len = 2;
386 w = va_arg(ap, uint16 *);
387 if (bufsize < len)
388 goto no_space;
389 *w = SVAL(buf, 0);
390 break;
391 case 'd': /* unsigned 32-bit integer (standard int in most systems) */
392 len = 4;
393 d = va_arg(ap, uint32 *);
394 if (bufsize < len)
395 goto no_space;
396 *d = IVAL(buf, 0);
397 break;
398 case 'p': /* pointer */
399 len = 4;
400 p = va_arg(ap, void **);
401 if (bufsize < len)
402 goto no_space;
404 * This isn't a real pointer - only a token (1 or 0)
405 * to mark the fact a pointer is present.
408 *p = (void *)(IVAL(buf, 0) ? (void *)1 : NULL);
409 break;
410 case 'P': /* null-terminated string */
411 /* Return malloc'ed string. */
412 ps = va_arg(ap,char **);
413 len = strnlen((const char *)buf, bufsize) + 1;
414 if (bufsize < len)
415 goto no_space;
416 *ps = SMB_STRDUP((const char *)buf);
417 break;
418 case 'f': /* null-terminated string */
419 s = va_arg(ap,char *);
420 len = strnlen((const char *)buf, bufsize) + 1;
421 if (bufsize < len || len > sizeof(fstring))
422 goto no_space;
423 memcpy(s, buf, len);
424 break;
425 case 'B': /* fixed-length string */
426 i = va_arg(ap, int *);
427 b = va_arg(ap, char **);
428 len = 4;
429 if (bufsize < len)
430 goto no_space;
431 *i = IVAL(buf, 0);
432 if (! *i) {
433 *b = NULL;
434 break;
436 len += *i;
437 if (bufsize < len)
438 goto no_space;
439 *b = (char *)SMB_MALLOC(*i);
440 if (! *b)
441 goto no_space;
442 memcpy(*b, buf+4, *i);
443 break;
444 default:
445 DEBUG(0,("Unknown tdb_unpack format %c in %s\n",
446 c, fmt));
448 len = 0;
449 break;
452 buf += len;
453 bufsize -= len;
456 va_end(ap);
458 DEBUG(18,("tdb_unpack(%s, %d) -> %d\n",
459 fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
461 return PTR_DIFF(buf, buf0);
463 no_space:
464 va_end(ap);
465 return -1;
469 /****************************************************************************
470 Log tdb messages via DEBUG().
471 ****************************************************************************/
473 #ifdef BUILD_TDB2
474 static void tdb_log(TDB_CONTEXT *tdb, enum tdb_log_level level,
475 enum TDB_ERROR ecode, const char *message, void *unused)
477 DEBUG((int)level, ("tdb(%s):%s: %s",
478 tdb_name(tdb) ? tdb_name(tdb) : "unnamed",
479 tdb_errorstr(ecode), message));
481 #else
482 static void tdb_log(TDB_CONTEXT *tdb, enum tdb_debug_level level, const char *format, ...)
484 va_list ap;
485 char *ptr = NULL;
486 int ret;
488 va_start(ap, format);
489 ret = vasprintf(&ptr, format, ap);
490 va_end(ap);
492 if ((ret == -1) || !*ptr)
493 return;
495 DEBUG((int)level, ("tdb(%s): %s", tdb_name(tdb) ? tdb_name(tdb) : "unnamed", ptr));
496 SAFE_FREE(ptr);
498 #endif /* TDB1 */
500 /****************************************************************************
501 Like tdb_open() but also setup a logging function that redirects to
502 the samba DEBUG() system.
503 ****************************************************************************/
505 TDB_CONTEXT *tdb_open_log(const char *name, int hash_size, int tdb_flags,
506 int open_flags, mode_t mode)
508 TDB_CONTEXT *tdb;
510 if (!lp_use_mmap())
511 tdb_flags |= TDB_NOMMAP;
513 if ((hash_size == 0) && (name != NULL)) {
514 const char *base = strrchr_m(name, '/');
515 if (base != NULL) {
516 base += 1;
518 else {
519 base = name;
521 hash_size = lp_parm_int(-1, "tdb_hashsize", base, 0);
524 tdb = tdb_open_compat(name, hash_size, tdb_flags,
525 open_flags, mode, tdb_log, NULL);
526 if (!tdb)
527 return NULL;
529 return tdb;
532 /****************************************************************************
533 tdb_store, wrapped in a transaction. This way we make sure that a process
534 that dies within writing does not leave a corrupt tdb behind.
535 ****************************************************************************/
537 int tdb_trans_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
538 int flag)
540 int res;
542 if ((res = tdb_transaction_start(tdb)) != 0) {
543 DEBUG(5, ("tdb_transaction_start failed\n"));
544 return res;
547 if ((res = tdb_store(tdb, key, dbuf, flag)) != 0) {
548 DEBUG(10, ("tdb_store failed\n"));
549 tdb_transaction_cancel(tdb);
550 return res;
553 if ((res = tdb_transaction_commit(tdb)) != 0) {
554 DEBUG(5, ("tdb_transaction_commit failed\n"));
557 return res;
560 /****************************************************************************
561 tdb_delete, wrapped in a transaction. This way we make sure that a process
562 that dies within deleting does not leave a corrupt tdb behind.
563 ****************************************************************************/
565 int tdb_trans_delete(struct tdb_context *tdb, TDB_DATA key)
567 int res;
569 if ((res = tdb_transaction_start(tdb)) != 0) {
570 DEBUG(5, ("tdb_transaction_start failed\n"));
571 return res;
574 if ((res = tdb_delete(tdb, key)) != 0) {
575 DEBUG(10, ("tdb_delete failed\n"));
576 tdb_transaction_cancel(tdb);
577 return res;
580 if ((res = tdb_transaction_commit(tdb)) != 0) {
581 DEBUG(5, ("tdb_transaction_commit failed\n"));
584 return res;
587 int tdb_data_cmp(TDB_DATA t1, TDB_DATA t2)
589 int ret;
590 if (t1.dptr == NULL && t2.dptr != NULL) {
591 return -1;
593 if (t1.dptr != NULL && t2.dptr == NULL) {
594 return 1;
596 if (t1.dptr == t2.dptr) {
597 return t1.dsize - t2.dsize;
599 ret = memcmp(t1.dptr, t2.dptr, MIN(t1.dsize, t2.dsize));
600 if (ret == 0) {
601 return t1.dsize - t2.dsize;
603 return ret;