2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Rafal Szczesniak 2002
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 /* these are little tdb utility functions that are meant to make
28 dealing with a tdb database a little less cumbersome in Samba */
30 static SIG_ATOMIC_T gotalarm
;
32 /***************************************************************
33 Signal function to tell us we timed out.
34 ****************************************************************/
36 static void gotalarm_sig(void)
41 /***************************************************************
42 Make a TDB_DATA and keep the const warning in one place
43 ****************************************************************/
45 TDB_DATA
make_tdb_data(const uint8
*dptr
, size_t dsize
)
48 ret
.dptr
= CONST_DISCARD(uint8
*, dptr
);
53 TDB_DATA
string_tdb_data(const char *string
)
55 return make_tdb_data((const uint8
*)string
, string
? strlen(string
) : 0 );
58 TDB_DATA
string_term_tdb_data(const char *string
)
60 return make_tdb_data((const uint8
*)string
, string
? strlen(string
) + 1 : 0);
63 /****************************************************************************
64 Lock a chain with timeout (in seconds).
65 ****************************************************************************/
67 static int tdb_chainlock_with_timeout_internal( TDB_CONTEXT
*tdb
, TDB_DATA key
, unsigned int timeout
, int rw_type
)
69 /* Allow tdb_chainlock to be interrupted by an alarm. */
74 CatchSignal(SIGALRM
, SIGNAL_CAST gotalarm_sig
);
78 if (rw_type
== F_RDLCK
)
79 ret
= tdb_chainlock_read(tdb
, key
);
81 ret
= tdb_chainlock(tdb
, key
);
85 CatchSignal(SIGALRM
, SIGNAL_CAST SIG_IGN
);
87 DEBUG(0,("tdb_chainlock_with_timeout_internal: alarm (%u) timed out for key %s in tdb %s\n",
88 timeout
, key
.dptr
, tdb_name(tdb
)));
89 /* TODO: If we time out waiting for a lock, it might
90 * be nice to use F_GETLK to get the pid of the
91 * process currently holding the lock and print that
92 * as part of the debugging message. -- mbp */
100 /****************************************************************************
101 Write lock a chain. Return -1 if timeout or lock failed.
102 ****************************************************************************/
104 int tdb_chainlock_with_timeout( TDB_CONTEXT
*tdb
, TDB_DATA key
, unsigned int timeout
)
106 return tdb_chainlock_with_timeout_internal(tdb
, key
, timeout
, F_WRLCK
);
109 /****************************************************************************
110 Lock a chain by string. Return -1 if timeout or lock failed.
111 ****************************************************************************/
113 int tdb_lock_bystring(TDB_CONTEXT
*tdb
, const char *keyval
)
115 TDB_DATA key
= string_term_tdb_data(keyval
);
117 return tdb_chainlock(tdb
, key
);
120 int tdb_lock_bystring_with_timeout(TDB_CONTEXT
*tdb
, const char *keyval
,
123 TDB_DATA key
= string_term_tdb_data(keyval
);
125 return tdb_chainlock_with_timeout(tdb
, key
, timeout
);
128 /****************************************************************************
129 Unlock a chain by string.
130 ****************************************************************************/
132 void tdb_unlock_bystring(TDB_CONTEXT
*tdb
, const char *keyval
)
134 TDB_DATA key
= string_term_tdb_data(keyval
);
136 tdb_chainunlock(tdb
, key
);
139 /****************************************************************************
140 Read lock a chain by string. Return -1 if timeout or lock failed.
141 ****************************************************************************/
143 int tdb_read_lock_bystring_with_timeout(TDB_CONTEXT
*tdb
, const char *keyval
, unsigned int timeout
)
145 TDB_DATA key
= string_term_tdb_data(keyval
);
147 return tdb_chainlock_with_timeout_internal(tdb
, key
, timeout
, F_RDLCK
);
150 /****************************************************************************
151 Read unlock a chain by string.
152 ****************************************************************************/
154 void tdb_read_unlock_bystring(TDB_CONTEXT
*tdb
, const char *keyval
)
156 TDB_DATA key
= string_term_tdb_data(keyval
);
158 tdb_chainunlock_read(tdb
, key
);
162 /****************************************************************************
163 Fetch a int32 value by a arbitrary blob key, return -1 if not found.
164 Output is int32 in native byte order.
165 ****************************************************************************/
167 int32
tdb_fetch_int32_byblob(TDB_CONTEXT
*tdb
, TDB_DATA key
)
172 data
= tdb_fetch(tdb
, key
);
173 if (!data
.dptr
|| data
.dsize
!= sizeof(int32
)) {
174 SAFE_FREE(data
.dptr
);
178 ret
= IVAL(data
.dptr
,0);
179 SAFE_FREE(data
.dptr
);
183 /****************************************************************************
184 Fetch a int32 value by string key, return -1 if not found.
185 Output is int32 in native byte order.
186 ****************************************************************************/
188 int32
tdb_fetch_int32(TDB_CONTEXT
*tdb
, const char *keystr
)
190 TDB_DATA key
= string_term_tdb_data(keystr
);
192 return tdb_fetch_int32_byblob(tdb
, key
);
195 /****************************************************************************
196 Store a int32 value by an arbitary blob key, return 0 on success, -1 on failure.
197 Input is int32 in native byte order. Output in tdb is in little-endian.
198 ****************************************************************************/
200 int tdb_store_int32_byblob(TDB_CONTEXT
*tdb
, TDB_DATA key
, int32 v
)
206 data
.dptr
= (uint8
*)&v_store
;
207 data
.dsize
= sizeof(int32
);
209 return tdb_store(tdb
, key
, data
, TDB_REPLACE
);
212 /****************************************************************************
213 Store a int32 value by string key, return 0 on success, -1 on failure.
214 Input is int32 in native byte order. Output in tdb is in little-endian.
215 ****************************************************************************/
217 int tdb_store_int32(TDB_CONTEXT
*tdb
, const char *keystr
, int32 v
)
219 TDB_DATA key
= string_term_tdb_data(keystr
);
221 return tdb_store_int32_byblob(tdb
, key
, v
);
224 /****************************************************************************
225 Fetch a uint32 value by a arbitrary blob key, return -1 if not found.
226 Output is uint32 in native byte order.
227 ****************************************************************************/
229 BOOL
tdb_fetch_uint32_byblob(TDB_CONTEXT
*tdb
, TDB_DATA key
, uint32
*value
)
233 data
= tdb_fetch(tdb
, key
);
234 if (!data
.dptr
|| data
.dsize
!= sizeof(uint32
)) {
235 SAFE_FREE(data
.dptr
);
239 *value
= IVAL(data
.dptr
,0);
240 SAFE_FREE(data
.dptr
);
244 /****************************************************************************
245 Fetch a uint32 value by string key, return -1 if not found.
246 Output is uint32 in native byte order.
247 ****************************************************************************/
249 BOOL
tdb_fetch_uint32(TDB_CONTEXT
*tdb
, const char *keystr
, uint32
*value
)
251 TDB_DATA key
= string_term_tdb_data(keystr
);
253 return tdb_fetch_uint32_byblob(tdb
, key
, value
);
256 /****************************************************************************
257 Store a uint32 value by an arbitary blob key, return 0 on success, -1 on failure.
258 Input is uint32 in native byte order. Output in tdb is in little-endian.
259 ****************************************************************************/
261 BOOL
tdb_store_uint32_byblob(TDB_CONTEXT
*tdb
, TDB_DATA key
, uint32 value
)
267 SIVAL(&v_store
, 0, value
);
268 data
.dptr
= (uint8
*)&v_store
;
269 data
.dsize
= sizeof(uint32
);
271 if (tdb_store(tdb
, key
, data
, TDB_REPLACE
) == -1)
277 /****************************************************************************
278 Store a uint32 value by string key, return 0 on success, -1 on failure.
279 Input is uint32 in native byte order. Output in tdb is in little-endian.
280 ****************************************************************************/
282 BOOL
tdb_store_uint32(TDB_CONTEXT
*tdb
, const char *keystr
, uint32 value
)
284 TDB_DATA key
= string_term_tdb_data(keystr
);
286 return tdb_store_uint32_byblob(tdb
, key
, value
);
288 /****************************************************************************
289 Store a buffer by a null terminated string key. Return 0 on success, -1
291 ****************************************************************************/
293 int tdb_store_bystring(TDB_CONTEXT
*tdb
, const char *keystr
, TDB_DATA data
, int flags
)
295 TDB_DATA key
= string_term_tdb_data(keystr
);
297 return tdb_store(tdb
, key
, data
, flags
);
300 int tdb_trans_store_bystring(TDB_CONTEXT
*tdb
, const char *keystr
,
301 TDB_DATA data
, int flags
)
303 TDB_DATA key
= string_term_tdb_data(keystr
);
305 return tdb_trans_store(tdb
, key
, data
, flags
);
308 /****************************************************************************
309 Fetch a buffer using a null terminated string key. Don't forget to call
310 free() on the result dptr.
311 ****************************************************************************/
313 TDB_DATA
tdb_fetch_bystring(TDB_CONTEXT
*tdb
, const char *keystr
)
315 TDB_DATA key
= string_term_tdb_data(keystr
);
317 return tdb_fetch(tdb
, key
);
320 /****************************************************************************
321 Delete an entry using a null terminated string key.
322 ****************************************************************************/
324 int tdb_delete_bystring(TDB_CONTEXT
*tdb
, const char *keystr
)
326 TDB_DATA key
= string_term_tdb_data(keystr
);
328 return tdb_delete(tdb
, key
);
331 /****************************************************************************
332 Atomic integer change. Returns old value. To create, set initial value in *oldval.
333 ****************************************************************************/
335 int32
tdb_change_int32_atomic(TDB_CONTEXT
*tdb
, const char *keystr
, int32
*oldval
, int32 change_val
)
340 if (tdb_lock_bystring(tdb
, keystr
) == -1)
343 if ((val
= tdb_fetch_int32(tdb
, keystr
)) == -1) {
344 /* The lookup failed */
345 if (tdb_error(tdb
) != TDB_ERR_NOEXIST
) {
346 /* but not because it didn't exist */
350 /* Start with 'old' value */
354 /* It worked, set return value (oldval) to tdb data */
358 /* Increment value for storage and return next time */
361 if (tdb_store_int32(tdb
, keystr
, val
) == -1)
368 tdb_unlock_bystring(tdb
, keystr
);
372 /****************************************************************************
373 Atomic unsigned integer change. Returns old value. To create, set initial value in *oldval.
374 ****************************************************************************/
376 BOOL
tdb_change_uint32_atomic(TDB_CONTEXT
*tdb
, const char *keystr
, uint32
*oldval
, uint32 change_val
)
381 if (tdb_lock_bystring(tdb
, keystr
) == -1)
384 if (!tdb_fetch_uint32(tdb
, keystr
, &val
)) {
386 if (tdb_error(tdb
) != TDB_ERR_NOEXIST
) {
387 /* and not because it didn't exist */
391 /* Start with 'old' value */
395 /* it worked, set return value (oldval) to tdb data */
400 /* get a new value to store */
403 if (!tdb_store_uint32(tdb
, keystr
, val
))
410 tdb_unlock_bystring(tdb
, keystr
);
414 /****************************************************************************
415 Useful pair of routines for packing/unpacking data consisting of
416 integers and strings.
417 ****************************************************************************/
419 size_t tdb_pack_va(uint8
*buf
, int bufsize
, const char *fmt
, va_list ap
)
430 const char *fmt0
= fmt
;
431 int bufsize0
= bufsize
;
434 switch ((c
= *fmt
++)) {
435 case 'b': /* unsigned 8-bit integer */
437 bt
= (uint8
)va_arg(ap
, int);
438 if (bufsize
&& bufsize
>= len
)
441 case 'w': /* unsigned 16-bit integer */
443 w
= (uint16
)va_arg(ap
, int);
444 if (bufsize
&& bufsize
>= len
)
447 case 'd': /* signed 32-bit integer (standard int in most systems) */
449 d
= va_arg(ap
, uint32
);
450 if (bufsize
&& bufsize
>= len
)
453 case 'p': /* pointer */
455 p
= va_arg(ap
, void *);
457 if (bufsize
&& bufsize
>= len
)
460 case 'P': /* null-terminated string */
461 s
= va_arg(ap
,char *);
464 if (bufsize
&& bufsize
>= len
)
467 case 'f': /* null-terminated string */
468 s
= va_arg(ap
,char *);
471 if (bufsize
&& bufsize
>= len
)
474 case 'B': /* fixed-length string */
476 s
= va_arg(ap
, char *);
478 if (bufsize
&& bufsize
>= len
) {
484 DEBUG(0,("Unknown tdb_pack format %c in %s\n",
497 DEBUG(18,("tdb_pack_va(%s, %d) -> %d\n",
498 fmt0
, bufsize0
, (int)PTR_DIFF(buf
, buf0
)));
500 return PTR_DIFF(buf
, buf0
);
503 size_t tdb_pack(uint8
*buf
, int bufsize
, const char *fmt
, ...)
509 result
= tdb_pack_va(buf
, bufsize
, fmt
, ap
);
514 BOOL
tdb_pack_append(TALLOC_CTX
*mem_ctx
, uint8
**buf
, size_t *len
,
515 const char *fmt
, ...)
521 len1
= tdb_pack_va(NULL
, 0, fmt
, ap
);
524 if (mem_ctx
!= NULL
) {
525 *buf
= TALLOC_REALLOC_ARRAY(mem_ctx
, *buf
, uint8
,
528 *buf
= SMB_REALLOC_ARRAY(*buf
, uint8
, (*len
) + len1
);
536 len2
= tdb_pack_va((*buf
)+(*len
), len1
, fmt
, ap
);
548 /****************************************************************************
549 Useful pair of routines for packing/unpacking data consisting of
550 integers and strings.
551 ****************************************************************************/
553 int tdb_unpack(const uint8
*buf
, int bufsize
, const char *fmt
, ...)
564 const uint8
*buf0
= buf
;
565 const char *fmt0
= fmt
;
566 int bufsize0
= bufsize
;
571 switch ((c
=*fmt
++)) {
574 bt
= va_arg(ap
, uint8
*);
581 w
= va_arg(ap
, uint16
*);
588 d
= va_arg(ap
, uint32
*);
595 p
= va_arg(ap
, void **);
599 * This isn't a real pointer - only a token (1 or 0)
600 * to mark the fact a pointer is present.
603 *p
= (void *)(IVAL(buf
, 0) ? (void *)1 : NULL
);
606 s
= va_arg(ap
,char *);
607 len
= strlen((const char *)buf
) + 1;
608 if (bufsize
< len
|| len
> sizeof(pstring
))
613 s
= va_arg(ap
,char *);
614 len
= strlen((const char *)buf
) + 1;
615 if (bufsize
< len
|| len
> sizeof(fstring
))
620 i
= va_arg(ap
, int *);
621 b
= va_arg(ap
, char **);
633 *b
= (char *)SMB_MALLOC(*i
);
636 memcpy(*b
, buf
+4, *i
);
639 DEBUG(0,("Unknown tdb_unpack format %c in %s\n",
652 DEBUG(18,("tdb_unpack(%s, %d) -> %d\n",
653 fmt0
, bufsize0
, (int)PTR_DIFF(buf
, buf0
)));
655 return PTR_DIFF(buf
, buf0
);
662 /****************************************************************************
663 Log tdb messages via DEBUG().
664 ****************************************************************************/
666 static void tdb_log(TDB_CONTEXT
*tdb
, enum tdb_debug_level level
, const char *format
, ...)
671 va_start(ap
, format
);
672 vasprintf(&ptr
, format
, ap
);
678 DEBUG((int)level
, ("tdb(%s): %s", tdb_name(tdb
) ? tdb_name(tdb
) : "unnamed", ptr
));
682 /****************************************************************************
683 Like tdb_open() but also setup a logging function that redirects to
684 the samba DEBUG() system.
685 ****************************************************************************/
687 TDB_CONTEXT
*tdb_open_log(const char *name
, int hash_size
, int tdb_flags
,
688 int open_flags
, mode_t mode
)
691 struct tdb_logging_context log_ctx
;
694 tdb_flags
|= TDB_NOMMAP
;
696 log_ctx
.log_fn
= tdb_log
;
697 log_ctx
.log_private
= NULL
;
699 tdb
= tdb_open_ex(name
, hash_size
, tdb_flags
,
700 open_flags
, mode
, &log_ctx
, NULL
);
707 /****************************************************************************
708 Allow tdb_delete to be used as a tdb_traversal_fn.
709 ****************************************************************************/
711 int tdb_traverse_delete_fn(TDB_CONTEXT
*the_tdb
, TDB_DATA key
, TDB_DATA dbuf
,
714 return tdb_delete(the_tdb
, key
);
720 * Search across the whole tdb for keys that match the given pattern
721 * return the result as a list of keys
723 * @param tdb pointer to opened tdb file context
724 * @param pattern searching pattern used by fnmatch(3) functions
726 * @return list of keys found by looking up with given pattern
728 TDB_LIST_NODE
*tdb_search_keys(TDB_CONTEXT
*tdb
, const char* pattern
)
731 TDB_LIST_NODE
*list
= NULL
;
732 TDB_LIST_NODE
*rec
= NULL
;
734 for (key
= tdb_firstkey(tdb
); key
.dptr
; key
= next
) {
735 /* duplicate key string to ensure null-termination */
736 char *key_str
= SMB_STRNDUP((const char *)key
.dptr
, key
.dsize
);
738 DEBUG(0, ("tdb_search_keys: strndup() failed!\n"));
739 smb_panic("strndup failed!\n");
742 DEBUG(18, ("checking %s for match to pattern %s\n", key_str
, pattern
));
744 next
= tdb_nextkey(tdb
, key
);
746 /* do the pattern checking */
747 if (fnmatch(pattern
, key_str
, 0) == 0) {
748 rec
= SMB_MALLOC_P(TDB_LIST_NODE
);
753 DLIST_ADD_END(list
, rec
, TDB_LIST_NODE
*);
755 DEBUG(18, ("checking %s matched pattern %s\n", key_str
, pattern
));
760 /* free duplicated key string */
770 * Free the list returned by tdb_search_keys
772 * @param node list of results found by tdb_search_keys
774 void tdb_search_list_free(TDB_LIST_NODE
* node
)
776 TDB_LIST_NODE
*next_node
;
779 next_node
= node
->next
;
780 SAFE_FREE(node
->node_key
.dptr
);
786 /****************************************************************************
787 tdb_store, wrapped in a transaction. This way we make sure that a process
788 that dies within writing does not leave a corrupt tdb behind.
789 ****************************************************************************/
791 int tdb_trans_store(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA dbuf
,
796 if ((res
= tdb_transaction_start(tdb
)) != 0) {
797 DEBUG(5, ("tdb_transaction_start failed\n"));
801 if ((res
= tdb_store(tdb
, key
, dbuf
, flag
)) != 0) {
802 DEBUG(10, ("tdb_store failed\n"));
803 if (tdb_transaction_cancel(tdb
) != 0) {
804 smb_panic("Cancelling transaction failed");
809 if ((res
= tdb_transaction_commit(tdb
)) != 0) {
810 DEBUG(5, ("tdb_transaction_commit failed\n"));
816 /****************************************************************************
817 tdb_delete, wrapped in a transaction. This way we make sure that a process
818 that dies within deleting does not leave a corrupt tdb behind.
819 ****************************************************************************/
821 int tdb_trans_delete(struct tdb_context
*tdb
, TDB_DATA key
)
825 if ((res
= tdb_transaction_start(tdb
)) != 0) {
826 DEBUG(5, ("tdb_transaction_start failed\n"));
830 if ((res
= tdb_delete(tdb
, key
)) != 0) {
831 DEBUG(10, ("tdb_delete failed\n"));
832 if (tdb_transaction_cancel(tdb
) != 0) {
833 smb_panic("Cancelling transaction failed");
838 if ((res
= tdb_transaction_commit(tdb
)) != 0) {
839 DEBUG(5, ("tdb_transaction_commit failed\n"));
846 Log tdb messages via DEBUG().
848 static void tdb_wrap_log(TDB_CONTEXT
*tdb
, enum tdb_debug_level level
,
849 const char *format
, ...) PRINTF_ATTRIBUTE(3,4);
851 static void tdb_wrap_log(TDB_CONTEXT
*tdb
, enum tdb_debug_level level
,
852 const char *format
, ...)
858 va_start(ap
, format
);
859 vasprintf(&ptr
, format
, ap
);
863 case TDB_DEBUG_FATAL
:
866 case TDB_DEBUG_ERROR
:
869 case TDB_DEBUG_WARNING
:
872 case TDB_DEBUG_TRACE
:
880 const char *name
= tdb_name(tdb
);
881 DEBUG(debuglevel
, ("tdb(%s): %s", name
? name
: "unnamed", ptr
));
886 static struct tdb_wrap
*tdb_list
;
888 /* destroy the last connection to a tdb */
889 static int tdb_wrap_destructor(struct tdb_wrap
*w
)
892 DLIST_REMOVE(tdb_list
, w
);
897 wrapped connection to a tdb database
898 to close just talloc_free() the tdb_wrap pointer
900 struct tdb_wrap
*tdb_wrap_open(TALLOC_CTX
*mem_ctx
,
901 const char *name
, int hash_size
, int tdb_flags
,
902 int open_flags
, mode_t mode
)
905 struct tdb_logging_context log_ctx
;
906 log_ctx
.log_fn
= tdb_wrap_log
;
909 tdb_flags
|= TDB_NOMMAP
;
911 for (w
=tdb_list
;w
;w
=w
->next
) {
912 if (strcmp(name
, w
->name
) == 0) {
914 * Yes, talloc_reference is exactly what we want
915 * here. Otherwise we would have to implement our own
916 * reference counting.
918 return talloc_reference(mem_ctx
, w
);
922 w
= talloc(mem_ctx
, struct tdb_wrap
);
927 if (!(w
->name
= talloc_strdup(w
, name
))) {
932 w
->tdb
= tdb_open_ex(name
, hash_size
, tdb_flags
,
933 open_flags
, mode
, &log_ctx
, NULL
);
934 if (w
->tdb
== NULL
) {
939 talloc_set_destructor(w
, tdb_wrap_destructor
);
941 DLIST_ADD(tdb_list
, w
);
946 NTSTATUS
map_nt_error_from_tdb(enum TDB_ERROR err
)
948 struct { enum TDB_ERROR err
; NTSTATUS status
; } map
[] =
949 { { TDB_SUCCESS
, NT_STATUS_OK
},
950 { TDB_ERR_CORRUPT
, NT_STATUS_INTERNAL_DB_CORRUPTION
},
951 { TDB_ERR_IO
, NT_STATUS_UNEXPECTED_IO_ERROR
},
952 { TDB_ERR_OOM
, NT_STATUS_NO_MEMORY
},
953 { TDB_ERR_EXISTS
, NT_STATUS_OBJECT_NAME_COLLISION
},
956 * TDB_ERR_LOCK is very broad, we could for example
957 * distinguish between fcntl locks and invalid lock
958 * sequences. So NT_STATUS_FILE_LOCK_CONFLICT is a
961 { TDB_ERR_LOCK
, NT_STATUS_FILE_LOCK_CONFLICT
},
963 * The next two ones in the enum are not actually used
965 { TDB_ERR_NOLOCK
, NT_STATUS_FILE_LOCK_CONFLICT
},
966 { TDB_ERR_LOCK_TIMEOUT
, NT_STATUS_FILE_LOCK_CONFLICT
},
967 { TDB_ERR_NOEXIST
, NT_STATUS_NOT_FOUND
},
968 { TDB_ERR_EINVAL
, NT_STATUS_INVALID_PARAMETER
},
969 { TDB_ERR_RDONLY
, NT_STATUS_ACCESS_DENIED
}
974 for (i
=0; i
< sizeof(map
) / sizeof(map
[0]); i
++) {
975 if (err
== map
[i
].err
) {
976 return map
[i
].status
;
980 return NT_STATUS_INTERNAL_ERROR
;
984 /*********************************************************************
985 * the following is a generic validation mechanism for tdbs.
986 *********************************************************************/
989 * internal validation function, executed by the child.
991 static int tdb_validate_child(const char *tdb_path
,
992 tdb_validate_data_func validate_fn
,
997 TDB_CONTEXT
*tdb
= NULL
;
998 struct tdb_validation_status v_status
;
1000 v_status
.tdb_error
= False
;
1001 v_status
.bad_freelist
= False
;
1002 v_status
.bad_entry
= False
;
1003 v_status
.unknown_key
= False
;
1004 v_status
.success
= True
;
1006 tdb
= tdb_open_log(tdb_path
, 0, TDB_DEFAULT
, O_RDONLY
, 0);
1008 v_status
.tdb_error
= True
;
1009 v_status
.success
= False
;
1013 /* Check the cache freelist is good. */
1014 if (tdb_validate_freelist(tdb
, &num_entries
) == -1) {
1015 DEBUG(0,("tdb_validate_child: bad freelist in cache %s\n",
1017 v_status
.bad_freelist
= True
;
1018 v_status
.success
= False
;
1022 DEBUG(10,("tdb_validate_child: cache %s freelist has %d entries\n",
1023 tdb_path
, num_entries
));
1025 /* Now traverse the cache to validate it. */
1026 num_entries
= tdb_traverse(tdb
, validate_fn
, (void *)&v_status
);
1027 if (num_entries
== -1 || !(v_status
.success
)) {
1028 DEBUG(0,("tdb_validate_child: cache %s traverse failed\n",
1030 if (!(v_status
.success
)) {
1031 if (v_status
.bad_entry
) {
1032 DEBUGADD(0, (" -> bad entry found\n"));
1034 if (v_status
.unknown_key
) {
1035 DEBUGADD(0, (" -> unknown key encountered\n"));
1041 DEBUG(10,("tdb_validate_child: cache %s is good "
1042 "with %d entries\n", tdb_path
, num_entries
));
1043 ret
= 0; /* Cache is good. */
1050 DEBUG(10, ("tdb_validate_child: writing status to pipe\n"));
1051 write (pfd
, (const char *)&v_status
, sizeof(v_status
));
1057 int tdb_validate(const char *tdb_path
, tdb_validate_data_func validate_fn
)
1059 pid_t child_pid
= -1;
1060 int child_status
= 0;
1064 struct tdb_validation_status v_status
;
1067 /* fork and let the child do the validation.
1068 * benefit: no need to twist signal handlers and panic functions.
1069 * just let the child panic. we catch the signal.
1070 * communicate the extended status struct over a pipe. */
1072 if (pipe(pipe_fds
) != 0) {
1073 DEBUG(0, ("tdb_validate: unable to create pipe, "
1074 "error %s", strerror(errno
)));
1075 smb_panic("winbind_validate_cache: unable to create pipe.");
1078 DEBUG(10, ("tdb_validate: forking to let child do validation.\n"));
1079 child_pid
= sys_fork();
1080 if (child_pid
== 0) {
1081 DEBUG(10, ("tdb_validate (validation child): created\n"));
1082 close(pipe_fds
[0]); /* close reading fd */
1083 DEBUG(10, ("tdb_validate (validation child): "
1084 "calling tdb_validate_child\n"));
1085 exit(tdb_validate_child(tdb_path
, validate_fn
, pipe_fds
[1]));
1087 else if (child_pid
< 0) {
1088 smb_panic("tdb_validate: fork for validation failed.");
1093 DEBUG(10, ("tdb_validate: fork succeeded, child PID = %d\n",
1095 close(pipe_fds
[1]); /* close writing fd */
1097 v_status
.success
= True
;
1098 v_status
.bad_entry
= False
;
1099 v_status
.unknown_key
= False
;
1101 DEBUG(10, ("tdb_validate: reading from pipe.\n"));
1102 bytes_read
= read(pipe_fds
[0], (void *)&v_status
, sizeof(v_status
));
1105 if (bytes_read
!= sizeof(v_status
)) {
1106 DEBUG(10, ("tdb_validate: read %d bytes from pipe "
1107 "but expected %d", bytes_read
, (int)sizeof(v_status
)));
1108 DEBUGADD(10, (" -> assuming child crashed\n"));
1109 v_status
.success
= False
;
1112 DEBUG(10, ("tdb_validate: read status from child\n"));
1113 DEBUGADD(10, (" * tdb error: %s\n", v_status
.tdb_error
? "yes" : "no"));
1114 DEBUGADD(10, (" * bad freelist: %s\n", v_status
.bad_freelist
? "yes" : "no"));
1115 DEBUGADD(10, (" * bad entry: %s\n", v_status
.bad_entry
? "yes" : "no"));
1116 DEBUGADD(10, (" * unknown key: %s\n", v_status
.unknown_key
? "yes" : "no"));
1117 DEBUGADD(10, (" => overall success: %s\n", v_status
.success
? "yes" : "no"));
1120 DEBUG(10, ("tdb_validate: waiting for child to finish...\n"));
1121 while ((wait_pid
= sys_waitpid(child_pid
, &child_status
, 0)) < 0) {
1122 if (errno
== EINTR
) {
1123 DEBUG(10, ("tdb_validate: got signal during "
1124 "waitpid, retrying\n"));
1128 DEBUG(0, ("tdb_validate: waitpid failed with "
1129 "errno %s\n", strerror(errno
)));
1130 smb_panic("tdb_validate: waitpid failed.");
1132 if (wait_pid
!= child_pid
) {
1133 DEBUG(0, ("tdb_validate: waitpid returned pid %d, "
1134 "but %d was expexted\n", wait_pid
, child_pid
));
1135 smb_panic("tdb_validate: waitpid returned "
1139 DEBUG(10, ("tdb_validate: validating child returned.\n"));
1140 if (WIFEXITED(child_status
)) {
1141 DEBUG(10, ("tdb_validate: child exited, code %d.\n",
1142 WEXITSTATUS(child_status
)));
1143 ret
= WEXITSTATUS(child_status
);
1145 if (WIFSIGNALED(child_status
)) {
1146 DEBUG(10, ("tdb_validate: child terminated "
1147 "by signal %d\n", WTERMSIG(child_status
)));
1149 if (WCOREDUMP(child_status
)) {
1150 DEBUGADD(10, ("core dumped\n"));
1153 ret
= WTERMSIG(child_status
);
1155 if (WIFSTOPPED(child_status
)) {
1156 DEBUG(10, ("tdb_validate: child was stopped "
1158 WSTOPSIG(child_status
)));
1159 ret
= WSTOPSIG(child_status
);