s3-utils/net_rpc_printer.c: print more info on write error
[Samba/gebeck_regimport.git] / source3 / lib / util_tdb.c
blobade46bf18ec86b89ebbdb9b1d445c06af76e70e0
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 ecode;
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 ecode;
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': /* signed 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 = strlen((const char *)buf) + 1;
414 *ps = SMB_STRDUP((const char *)buf);
415 break;
416 case 'f': /* null-terminated string */
417 s = va_arg(ap,char *);
418 len = strlen((const char *)buf) + 1;
419 if (bufsize < len || len > sizeof(fstring))
420 goto no_space;
421 memcpy(s, buf, len);
422 break;
423 case 'B': /* fixed-length string */
424 i = va_arg(ap, int *);
425 b = va_arg(ap, char **);
426 len = 4;
427 if (bufsize < len)
428 goto no_space;
429 *i = IVAL(buf, 0);
430 if (! *i) {
431 *b = NULL;
432 break;
434 len += *i;
435 if (bufsize < len)
436 goto no_space;
437 *b = (char *)SMB_MALLOC(*i);
438 if (! *b)
439 goto no_space;
440 memcpy(*b, buf+4, *i);
441 break;
442 default:
443 DEBUG(0,("Unknown tdb_unpack format %c in %s\n",
444 c, fmt));
446 len = 0;
447 break;
450 buf += len;
451 bufsize -= len;
454 va_end(ap);
456 DEBUG(18,("tdb_unpack(%s, %d) -> %d\n",
457 fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
459 return PTR_DIFF(buf, buf0);
461 no_space:
462 va_end(ap);
463 return -1;
467 /****************************************************************************
468 Log tdb messages via DEBUG().
469 ****************************************************************************/
471 #ifdef BUILD_TDB2
472 static void tdb_log(TDB_CONTEXT *tdb, enum tdb_log_level level,
473 const char *message, void *unused)
475 DEBUG((int)level, ("tdb(%s): %s",
476 tdb_name(tdb) ? tdb_name(tdb) : "unnamed", message));
478 #else
479 static void tdb_log(TDB_CONTEXT *tdb, enum tdb_debug_level level, const char *format, ...)
481 va_list ap;
482 char *ptr = NULL;
483 int ret;
485 va_start(ap, format);
486 ret = vasprintf(&ptr, format, ap);
487 va_end(ap);
489 if ((ret == -1) || !*ptr)
490 return;
492 DEBUG((int)level, ("tdb(%s): %s", tdb_name(tdb) ? tdb_name(tdb) : "unnamed", ptr));
493 SAFE_FREE(ptr);
495 #endif /* TDB1 */
497 /****************************************************************************
498 Like tdb_open() but also setup a logging function that redirects to
499 the samba DEBUG() system.
500 ****************************************************************************/
502 TDB_CONTEXT *tdb_open_log(const char *name, int hash_size, int tdb_flags,
503 int open_flags, mode_t mode)
505 TDB_CONTEXT *tdb;
507 if (!lp_use_mmap())
508 tdb_flags |= TDB_NOMMAP;
510 if ((hash_size == 0) && (name != NULL)) {
511 const char *base = strrchr_m(name, '/');
512 if (base != NULL) {
513 base += 1;
515 else {
516 base = name;
518 hash_size = lp_parm_int(-1, "tdb_hashsize", base, 0);
521 tdb = tdb_open_compat(name, hash_size, tdb_flags,
522 open_flags, mode, tdb_log, NULL);
523 if (!tdb)
524 return NULL;
526 return tdb;
529 /****************************************************************************
530 tdb_store, wrapped in a transaction. This way we make sure that a process
531 that dies within writing does not leave a corrupt tdb behind.
532 ****************************************************************************/
534 int tdb_trans_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
535 int flag)
537 int res;
539 if ((res = tdb_transaction_start(tdb)) != 0) {
540 DEBUG(5, ("tdb_transaction_start failed\n"));
541 return res;
544 if ((res = tdb_store(tdb, key, dbuf, flag)) != 0) {
545 DEBUG(10, ("tdb_store failed\n"));
546 tdb_transaction_cancel(tdb);
547 return res;
550 if ((res = tdb_transaction_commit(tdb)) != 0) {
551 DEBUG(5, ("tdb_transaction_commit failed\n"));
554 return res;
557 /****************************************************************************
558 tdb_delete, wrapped in a transaction. This way we make sure that a process
559 that dies within deleting does not leave a corrupt tdb behind.
560 ****************************************************************************/
562 int tdb_trans_delete(struct tdb_context *tdb, TDB_DATA key)
564 int res;
566 if ((res = tdb_transaction_start(tdb)) != 0) {
567 DEBUG(5, ("tdb_transaction_start failed\n"));
568 return res;
571 if ((res = tdb_delete(tdb, key)) != 0) {
572 DEBUG(10, ("tdb_delete failed\n"));
573 tdb_transaction_cancel(tdb);
574 return res;
577 if ((res = tdb_transaction_commit(tdb)) != 0) {
578 DEBUG(5, ("tdb_transaction_commit failed\n"));
581 return res;
584 NTSTATUS map_nt_error_from_tdb(enum TDB_ERROR err)
586 NTSTATUS result = NT_STATUS_INTERNAL_ERROR;
588 switch (err) {
589 case TDB_SUCCESS:
590 result = NT_STATUS_OK;
591 break;
592 case TDB_ERR_CORRUPT:
593 result = NT_STATUS_INTERNAL_DB_CORRUPTION;
594 break;
595 case TDB_ERR_IO:
596 result = NT_STATUS_UNEXPECTED_IO_ERROR;
597 break;
598 case TDB_ERR_OOM:
599 result = NT_STATUS_NO_MEMORY;
600 break;
601 case TDB_ERR_EXISTS:
602 result = NT_STATUS_OBJECT_NAME_COLLISION;
603 break;
605 case TDB_ERR_LOCK:
607 * TDB_ERR_LOCK is very broad, we could for example
608 * distinguish between fcntl locks and invalid lock
609 * sequences. So NT_STATUS_FILE_LOCK_CONFLICT is a
610 * compromise.
612 result = NT_STATUS_FILE_LOCK_CONFLICT;
613 break;
615 #ifndef BUILD_TDB2
616 case TDB_ERR_NOLOCK:
617 case TDB_ERR_LOCK_TIMEOUT:
619 * These two ones in the enum are not actually used
621 result = NT_STATUS_FILE_LOCK_CONFLICT;
622 break;
623 #endif
624 case TDB_ERR_NOEXIST:
625 result = NT_STATUS_NOT_FOUND;
626 break;
627 case TDB_ERR_EINVAL:
628 result = NT_STATUS_INVALID_PARAMETER;
629 break;
630 case TDB_ERR_RDONLY:
631 result = NT_STATUS_ACCESS_DENIED;
632 break;
633 #ifndef BUILD_TDB2
634 case TDB_ERR_NESTING:
635 result = NT_STATUS_INTERNAL_ERROR;
636 break;
637 #endif
639 return result;
642 int tdb_data_cmp(TDB_DATA t1, TDB_DATA t2)
644 int ret;
645 if (t1.dptr == NULL && t2.dptr != NULL) {
646 return -1;
648 if (t1.dptr != NULL && t2.dptr == NULL) {
649 return 1;
651 if (t1.dptr == t2.dptr) {
652 return t1.dsize - t2.dsize;
654 ret = memcmp(t1.dptr, t2.dptr, MIN(t1.dsize, t2.dsize));
655 if (ret == 0) {
656 return t1.dsize - t2.dsize;
658 return ret;