5 Unix SMB/CIFS implementation.
7 trivial database library
9 Copyright (C) Andrew Tridgell 1999-2004
11 ** NOTE! The following LGPL license applies to the tdb
12 ** library. This does NOT imply that all of Samba is released
15 This library is free software; you can redistribute it and/or
16 modify it under the terms of the GNU Lesser General Public
17 License as published by the Free Software Foundation; either
18 version 3 of the License, or (at your option) any later version.
20 This library is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 Lesser General Public License for more details.
25 You should have received a copy of the GNU Lesser General Public
26 License along with this library; if not, see <http://www.gnu.org/licenses/>.
36 /* for old gcc releases that don't have the feature test macro __has_attribute */
37 #ifndef __has_attribute
38 #define __has_attribute(x) 0
42 #if __has_attribute(visibility)
43 #define _PUBLIC_ __attribute__((visibility("default")))
50 * @defgroup tdb The tdb API
52 * tdb is a Trivial database. In concept, it is very much like GDBM, and BSD's
53 * DB except that it allows multiple simultaneous writers and uses locking
54 * internally to keep writers from trampling on each other. tdb is also
57 * @section tdb_interface Interface
59 * The interface is very similar to gdbm except for the following:
62 * <li>different open interface. The tdb_open call is more similar to a
63 * traditional open()</li>
64 * <li>no tdbm_reorganise() function</li>
65 * <li>no tdbm_sync() function. No operations are cached in the library
67 * <li>added a tdb_traverse() function for traversing the whole database</li>
68 * <li>added transactions support</li>
71 * A general rule for using tdb is that the caller frees any returned TDB_DATA
72 * structures. Just call free(p.dptr) to free a TDB_DATA return value called p.
73 * This is the same as gdbm.
78 /** Flags to tdb_store() */
79 #define TDB_REPLACE 1 /** Unused */
80 #define TDB_INSERT 2 /** Don't overwrite an existing entry */
81 #define TDB_MODIFY 3 /** Don't create an existing entry */
83 /** Flags for tdb_open() */
84 #define TDB_DEFAULT 0 /** just a readability place holder */
85 #define TDB_CLEAR_IF_FIRST 1 /** If this is the first open, wipe the db */
86 #define TDB_INTERNAL 2 /** Don't store on disk */
87 #define TDB_NOLOCK 4 /** Don't do any locking */
88 #define TDB_NOMMAP 8 /** Don't use mmap */
89 #define TDB_CONVERT 16 /** Convert endian (internal use) */
90 #define TDB_BIGENDIAN 32 /** Header is big-endian (internal use) */
91 #define TDB_NOSYNC 64 /** Don't use synchronous transactions */
92 #define TDB_SEQNUM 128 /** Maintain a sequence number */
93 #define TDB_VOLATILE 256 /** Activate the per-hashchain freelist, default 5 */
94 #define TDB_ALLOW_NESTING 512 /** Allow transactions to nest */
95 #define TDB_DISALLOW_NESTING 1024 /** Disallow transactions to nest */
96 #define TDB_INCOMPATIBLE_HASH 2048 /** Better hashing: can't be opened by tdb < 1.2.6. */
97 #define TDB_MUTEX_LOCKING 4096 /** optimized locking using robust mutexes if supported,
98 only with tdb >= 1.3.0 and TDB_CLEAR_IF_FIRST
99 after checking tdb_runtime_check_for_robust_mutexes() */
101 /** The tdb error codes */
102 enum TDB_ERROR {TDB_SUCCESS=0, TDB_ERR_CORRUPT, TDB_ERR_IO, TDB_ERR_LOCK,
103 TDB_ERR_OOM, TDB_ERR_EXISTS, TDB_ERR_NOLOCK, TDB_ERR_LOCK_TIMEOUT,
104 TDB_ERR_NOEXIST, TDB_ERR_EINVAL, TDB_ERR_RDONLY,
107 /** Debugging uses one of the following levels */
108 enum tdb_debug_level {TDB_DEBUG_FATAL = 0, TDB_DEBUG_ERROR,
109 TDB_DEBUG_WARNING, TDB_DEBUG_TRACE};
111 /** The tdb data structure */
112 typedef struct TDB_DATA {
117 #ifndef PRINTF_ATTRIBUTE
118 #if __has_attribute(format) || (__GNUC__ >= 3)
119 /** Use gcc attribute to check printf fns. a1 is the 1-based index of
120 * the parameter containing the format, and a2 the index of the first
121 * argument. Note that some gcc 2.x versions don't handle this
123 #define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
125 #define PRINTF_ATTRIBUTE(a1, a2)
129 /** This is the context structure that is returned from a db open. */
130 typedef struct tdb_context TDB_CONTEXT;
132 typedef int (*tdb_traverse_func)(struct tdb_context *, TDB_DATA, TDB_DATA, void *);
133 typedef void (*tdb_log_func)(struct tdb_context *, enum tdb_debug_level, const char *, ...) PRINTF_ATTRIBUTE(3, 4);
134 typedef unsigned int (*tdb_hash_func)(TDB_DATA *key);
136 struct tdb_logging_context {
142 * @brief Open the database and creating it if necessary.
144 * @param[in] name The name of the db to open.
146 * @param[in] hash_size The hash size is advisory, use zero for a default
149 * @param[in] tdb_flags The flags to use to open the db:\n\n
150 * TDB_CLEAR_IF_FIRST - Clear database if we are the
151 * only one with it open\n
152 * TDB_INTERNAL - Don't use a file, instead store the
153 * data in memory. The filename is
154 * ignored in this case.\n
155 * TDB_NOLOCK - Don't do any locking\n
156 * TDB_NOMMAP - Don't use mmap\n
157 * TDB_NOSYNC - Don't synchronise transactions to disk\n
158 * TDB_SEQNUM - Maintain a sequence number\n
159 * TDB_VOLATILE - activate the per-hashchain freelist,
161 * TDB_ALLOW_NESTING - Allow transactions to nest.\n
162 * TDB_DISALLOW_NESTING - Disallow transactions to nest.\n
163 * TDB_INCOMPATIBLE_HASH - Better hashing: can't be opened by tdb < 1.2.6.\n
164 * TDB_MUTEX_LOCKING - Optimized locking using robust mutexes if supported,
165 * can't be opened by tdb < 1.3.0.
166 * Only valid in combination with TDB_CLEAR_IF_FIRST
167 * after checking tdb_runtime_check_for_robust_mutexes()\n
169 * @param[in] open_flags Flags for the open(2) function.
171 * @param[in] mode The mode for the open(2) function.
173 * @return A tdb context structure, NULL on error.
175 _PUBLIC_ struct tdb_context *tdb_open(const char *name, int hash_size, int tdb_flags,
176 int open_flags, mode_t mode);
179 * @brief Open the database and creating it if necessary.
181 * This is like tdb_open(), but allows you to pass an initial logging and
182 * hash function. Be careful when passing a hash function - all users of the
183 * database must use the same hash function or you will get data corruption.
185 * @param[in] name The name of the db to open.
187 * @param[in] hash_size The hash size is advisory, use zero for a default
190 * @param[in] tdb_flags The flags to use to open the db:\n\n
191 * TDB_CLEAR_IF_FIRST - Clear database if we are the
192 * only one with it open\n
193 * TDB_INTERNAL - Don't use a file, instead store the
194 * data in memory. The filename is
195 * ignored in this case.\n
196 * TDB_NOLOCK - Don't do any locking\n
197 * TDB_NOMMAP - Don't use mmap\n
198 * TDB_NOSYNC - Don't synchronise transactions to disk\n
199 * TDB_SEQNUM - Maintain a sequence number\n
200 * TDB_VOLATILE - activate the per-hashchain freelist,
202 * TDB_ALLOW_NESTING - Allow transactions to nest.\n
203 * TDB_DISALLOW_NESTING - Disallow transactions to nest.\n
204 * TDB_INCOMPATIBLE_HASH - Better hashing: can't be opened by tdb < 1.2.6.\n
205 * TDB_MUTEX_LOCKING - Optimized locking using robust mutexes if supported,
206 * can't be opened by tdb < 1.3.0.
207 * Only valid in combination with TDB_CLEAR_IF_FIRST
208 * after checking tdb_runtime_check_for_robust_mutexes()\n
210 * @param[in] open_flags Flags for the open(2) function.
212 * @param[in] mode The mode for the open(2) function.
214 * @param[in] log_ctx The logging function to use.
216 * @param[in] hash_fn The hash function you want to use.
218 * @return A tdb context structure, NULL on error.
222 _PUBLIC_ struct tdb_context *tdb_open_ex(const char *name, int hash_size, int tdb_flags,
223 int open_flags, mode_t mode,
224 const struct tdb_logging_context *log_ctx,
225 tdb_hash_func hash_fn);
228 * @brief Set the maximum number of dead records per hash chain.
230 * @param[in] tdb The database handle to set the maximum.
232 * @param[in] max_dead The maximum number of dead records per hash chain.
234 _PUBLIC_ void tdb_set_max_dead(struct tdb_context *tdb, int max_dead);
237 * @brief Reopen a tdb.
239 * This can be used after a fork to ensure that we have an independent seek
240 * pointer from our parent and to re-establish locks.
242 * @param[in] tdb The database to reopen. It will be free'd on error!
244 * @return 0 on success, -1 on error.
246 * @note Don't call tdb_error() after this function cause the tdb context will
249 _PUBLIC_ int tdb_reopen(struct tdb_context *tdb);
252 * @brief Reopen all tdb's
254 * If the parent is longlived (ie. a parent daemon architecture), we know it
255 * will keep it's active lock on a tdb opened with CLEAR_IF_FIRST. Thus for
256 * child processes we don't have to add an active lock. This is essential to
257 * improve performance on systems that keep POSIX locks as a non-scalable data
258 * structure in the kernel.
260 * @param[in] parent_longlived Whether the parent is longlived or not.
262 * @return 0 on success, -1 on error.
264 _PUBLIC_ int tdb_reopen_all(int parent_longlived);
267 * @brief Set a different tdb logging function.
269 * @param[in] tdb The tdb to set the logging function.
271 * @param[in] log_ctx The logging function to set.
273 _PUBLIC_ void tdb_set_logging_function(struct tdb_context *tdb, const struct tdb_logging_context *log_ctx);
276 * @brief Get the tdb last error code.
278 * @param[in] tdb The tdb to get the error code from.
280 * @return A TDB_ERROR code.
284 _PUBLIC_ enum TDB_ERROR tdb_error(struct tdb_context *tdb);
287 * @brief Get a error string for the last tdb error
289 * @param[in] tdb The tdb to get the error code from.
291 * @return An error string.
293 _PUBLIC_ const char *tdb_errorstr(struct tdb_context *tdb);
296 * @brief Fetch an entry in the database given a key.
298 * The caller must free the resulting data.
300 * @param[in] tdb The tdb to fetch the key.
302 * @param[in] key The key to fetch.
304 * @return The key entry found in the database, NULL on error with
308 * @see tdb_errorstr()
310 _PUBLIC_ TDB_DATA tdb_fetch(struct tdb_context *tdb, TDB_DATA key);
313 * @brief Hand a record to a parser function without allocating it.
315 * This function is meant as a fast tdb_fetch alternative for large records
316 * that are frequently read. The "key" and "data" arguments point directly
317 * into the tdb shared memory, they are not aligned at any boundary.
319 * @warning The parser is called while tdb holds a lock on the record. DO NOT
320 * call other tdb routines from within the parser. Also, for good performance
321 * you should make the parser fast to allow parallel operations.
323 * @param[in] tdb The tdb to parse the record.
325 * @param[in] key The key to parse.
327 * @param[in] parser The parser to use to parse the data.
329 * @param[in] private_data A private data pointer which is passed to the parser
332 * @return -1 if the record was not found. If the record was found,
333 * the return value of "parser" is passed up to the caller.
335 _PUBLIC_ int tdb_parse_record(struct tdb_context *tdb, TDB_DATA key,
336 int (*parser)(TDB_DATA key, TDB_DATA data,
341 * @brief Delete an entry in the database given a key.
343 * @param[in] tdb The tdb to delete the key.
345 * @param[in] key The key to delete.
347 * @return 0 on success, -1 if the key doesn't exist.
349 _PUBLIC_ int tdb_delete(struct tdb_context *tdb, TDB_DATA key);
352 * @brief Store an element in the database.
354 * This replaces any existing element with the same key.
356 * @param[in] tdb The tdb to store the entry.
358 * @param[in] key The key to use to store the entry.
360 * @param[in] dbuf The data to store under the key.
362 * @param[in] flag The flags to store the key:\n\n
363 * TDB_INSERT: Don't overwrite an existing entry.\n
364 * TDB_MODIFY: Don't create a new entry\n
366 * @return 0 on success, -1 on error with error code set.
369 * @see tdb_errorstr()
371 _PUBLIC_ int tdb_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag);
375 * @brief Store an element in the database.
377 * This replaces any existing element with the same key.
379 * @param[in] tdb The tdb to store the entry.
381 * @param[in] key The key to use to store the entry.
383 * @param[in] dbufs A vector of memory chunks to write
385 * @param[in] num_dbufs Length of the dbufs vector
387 * @param[in] flag The flags to store the key:\n\n
388 * TDB_INSERT: Don't overwrite an existing entry.\n
389 * TDB_MODIFY: Don't create a new entry\n
391 * @return 0 on success, -1 on error with error code set.
394 * @see tdb_errorstr()
396 _PUBLIC_ int tdb_storev(struct tdb_context *tdb, TDB_DATA key,
397 const TDB_DATA *dbufs, int num_dbufs, int flag);
400 * @brief Append data to an entry.
402 * If the entry doesn't exist, it will create a new one.
404 * @param[in] tdb The database to use.
406 * @param[in] key The key to append the data.
408 * @param[in] new_dbuf The data to append to the key.
410 * @return 0 on success, -1 on error with error code set.
413 * @see tdb_errorstr()
415 _PUBLIC_ int tdb_append(struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf);
418 * @brief Close a database.
420 * @param[in] tdb The database to close. The context will be free'd.
422 * @return 0 for success, -1 on error.
424 * @note Don't call tdb_error() after this function cause the tdb context will
427 _PUBLIC_ int tdb_close(struct tdb_context *tdb);
430 * @brief Find the first entry in the database and return its key.
432 * The caller must free the returned data.
434 * @param[in] tdb The database to use.
436 * @return The first entry of the database, an empty TDB_DATA entry
437 * if the database is empty.
439 _PUBLIC_ TDB_DATA tdb_firstkey(struct tdb_context *tdb);
442 * @brief Find the next entry in the database, returning its key.
444 * The caller must free the returned data.
446 * @param[in] tdb The database to use.
448 * @param[in] key The key from which you want the next key.
450 * @return The next entry of the current key, an empty TDB_DATA
451 * entry if there is no entry.
453 _PUBLIC_ TDB_DATA tdb_nextkey(struct tdb_context *tdb, TDB_DATA key);
456 * @brief Traverse the entire database.
458 * While traversing the function fn(tdb, key, data, state) is called on each
459 * element. If fn is NULL then it is not called. A non-zero return value from
460 * fn() indicates that the traversal should stop. Traversal callbacks may not
461 * start transactions.
463 * @warning The data buffer given to the callback fn does NOT meet the alignment
464 * restrictions malloc gives you.
466 * @param[in] tdb The database to traverse.
468 * @param[in] fn The function to call on each entry.
470 * @param[in] private_data The private data which should be passed to the
471 * traversing function.
473 * @return The record count traversed, -1 on error.
475 _PUBLIC_ int tdb_traverse(struct tdb_context *tdb, tdb_traverse_func fn, void *private_data);
478 * @brief Traverse the entire database.
480 * While traversing the database the function fn(tdb, key, data, state) is
481 * called on each element, but marking the database read only during the
482 * traversal, so any write operations will fail. This allows tdb to use read
483 * locks, which increases the parallelism possible during the traversal.
485 * @param[in] tdb The database to traverse.
487 * @param[in] fn The function to call on each entry.
489 * @param[in] private_data The private data which should be passed to the
490 * traversing function.
492 * @return The record count traversed, -1 on error.
494 _PUBLIC_ int tdb_traverse_read(struct tdb_context *tdb, tdb_traverse_func fn, void *private_data);
497 * @brief Traverse a single hash chain
499 * Traverse a single hash chain under a single lock operation. No
500 * database modification is possible in the callback.
502 * This exists for background cleanup of databases. In normal
503 * operations, traversing a complete database can be much too
504 * expensive. Databases can have many chains, which will all have to
505 * be looked at before tdb_traverse finishes. Also tdb_traverse does a
506 * lot of fcntl activity to protect against concurrent record deletes.
508 * With this you can walk a fraction of the whole tdb, collect the
509 * entries you want to prune, leave the traverse, and then modify or
510 * delete the records in a subsequent step.
512 * To walk the entire database, call this function tdb_hash_size()
513 * times, with 0<=chain<tdb_hash_size(tdb).
515 * @param[in] tdb The database to traverse.
517 * @param[in] chain The hash chain number to traverse.
519 * @param[in] fn The function to call on each entry.
521 * @param[in] private_data The private data which should be passed to the
522 * traversing function.
524 * @return The record count traversed, -1 on error.
527 _PUBLIC_ int tdb_traverse_chain(struct tdb_context *tdb,
529 tdb_traverse_func fn,
533 * @brief Traverse a single hash chain
535 * This is like tdb_traverse_chain(), but for all records that are in
536 * the same chain as the record corresponding to the key parameter.
538 * Use it for ongoing database maintenance under a lock. Whenever you
539 * are about to modify a database, you know which record you are going
540 * to write to. For that tdb_store(), an exclusive chainlock is taken
541 * behind the scenes. To utilize this exclusive lock for incremental
542 * database cleanup as well, tdb_chainlock() the key you are about to
543 * modify, then tdb_traverse_key_chain() to do the incremental
544 * maintenance, modify your record and tdb_chainunlock() the key
547 * @param[in] tdb The database to traverse.
549 * @param[in] key The record key to walk the chain for.
551 * @param[in] fn The function to call on each entry.
553 * @param[in] private_data The private data which should be passed to the
554 * traversing function.
556 * @return The record count traversed, -1 on error.
559 _PUBLIC_ int tdb_traverse_key_chain(struct tdb_context *tdb,
561 tdb_traverse_func fn,
564 * @brief Check if an entry in the database exists.
566 * @note 1 is returned if the key is found and 0 is returned if not found this
567 * doesn't match the conventions in the rest of this module, but is compatible
570 * @param[in] tdb The database to check if the entry exists.
572 * @param[in] key The key to check if the entry exists.
574 * @return 1 if the key is found, 0 if not.
576 _PUBLIC_ int tdb_exists(struct tdb_context *tdb, TDB_DATA key);
579 * @brief Lock entire database with a write lock.
581 * @param[in] tdb The database to lock.
583 * @return 0 on success, -1 on error with error code set.
586 * @see tdb_errorstr()
588 _PUBLIC_ int tdb_lockall(struct tdb_context *tdb);
591 * @brief Lock entire database with a write lock.
593 * This is the non-blocking call.
595 * @param[in] tdb The database to lock.
597 * @return 0 on success, -1 on error with error code set.
601 * @see tdb_errorstr()
603 _PUBLIC_ int tdb_lockall_nonblock(struct tdb_context *tdb);
606 * @brief Unlock entire database with write lock.
608 * @param[in] tdb The database to unlock.
610 * @return 0 on success, -1 on error with error code set.
614 * @see tdb_errorstr()
616 _PUBLIC_ int tdb_unlockall(struct tdb_context *tdb);
619 * @brief Lock entire database with a read lock.
621 * @param[in] tdb The database to lock.
623 * @return 0 on success, -1 on error with error code set.
626 * @see tdb_errorstr()
628 _PUBLIC_ int tdb_lockall_read(struct tdb_context *tdb);
631 * @brief Lock entire database with a read lock.
633 * This is the non-blocking call.
635 * @param[in] tdb The database to lock.
637 * @return 0 on success, -1 on error with error code set.
639 * @see tdb_lockall_read()
641 * @see tdb_errorstr()
643 _PUBLIC_ int tdb_lockall_read_nonblock(struct tdb_context *tdb);
646 * @brief Unlock entire database with read lock.
648 * @param[in] tdb The database to unlock.
650 * @return 0 on success, -1 on error with error code set.
652 * @see tdb_lockall_read()
654 * @see tdb_errorstr()
656 _PUBLIC_ int tdb_unlockall_read(struct tdb_context *tdb);
659 * @brief Lock entire database with write lock - mark only.
661 * @todo Add more details.
663 * @param[in] tdb The database to mark.
665 * @return 0 on success, -1 on error with error code set.
668 * @see tdb_errorstr()
670 _PUBLIC_ int tdb_lockall_mark(struct tdb_context *tdb);
673 * @brief Lock entire database with write lock - unmark only.
675 * @todo Add more details.
677 * @param[in] tdb The database to mark.
679 * @return 0 on success, -1 on error with error code set.
682 * @see tdb_errorstr()
684 _PUBLIC_ int tdb_lockall_unmark(struct tdb_context *tdb);
687 * @brief Get the name of the current tdb file.
689 * This is useful for external logging functions.
691 * @param[in] tdb The database to get the name from.
693 * @return The name of the database.
695 _PUBLIC_ const char *tdb_name(struct tdb_context *tdb);
698 * @brief Get the underlying file descriptor being used by tdb.
700 * This is useful for external routines that want to check the device/inode
703 * @param[in] tdb The database to get the fd from.
705 * @return The file descriptor or -1.
707 _PUBLIC_ int tdb_fd(struct tdb_context *tdb);
710 * @brief Get the current logging function.
712 * This is useful for external tdb routines that wish to log tdb errors.
714 * @param[in] tdb The database to get the logging function from.
716 * @return The logging function of the database.
718 * @see tdb_get_logging_private()
720 _PUBLIC_ tdb_log_func tdb_log_fn(struct tdb_context *tdb);
723 * @brief Get the private data of the logging function.
725 * @param[in] tdb The database to get the data from.
727 * @return The private data pointer of the logging function.
731 _PUBLIC_ void *tdb_get_logging_private(struct tdb_context *tdb);
734 * @brief Is a transaction active?
736 * It is helpful for the application to know if a transaction is
737 * active, rather than needing to maintain an application-level reference
740 * @param[in] tdb The database to start the transaction.
742 * @return true if there is a transaction active, false otherwise
744 * @see tdb_transaction_start()
745 * @see tdb_transaction_prepare_commit()
746 * @see tdb_transaction_commit()
747 * @see tdb_transaction_cancel()
749 _PUBLIC_ bool tdb_transaction_active(struct tdb_context *tdb);
752 * @brief Start a transaction.
754 * All operations after the transaction start can either be committed with
755 * tdb_transaction_commit() or cancelled with tdb_transaction_cancel().
757 * If (the default) TDB_ALLOW_NESTING was specified or
758 * TDB_DISALLOW_NESTING was not specified as a flag via tdb_open() or
759 * tdb_open_ex(), you call tdb_transaction_start() again on the same
760 * tdb context while a transaction is in progress, then the same
761 * transaction buffer is re-used. The number of
762 * tdb_transaction_{commit,cancel} operations must match the number of
763 * successful tdb_transaction_start() calls.
765 * Note that transactions are by default disk synchronous, and use a recover
766 * area in the database to automatically recover the database on the next open
767 * if the system crashes during a transaction. You can disable the synchronous
768 * transaction recovery setup using the TDB_NOSYNC flag, which will greatly
769 * speed up operations at the risk of corrupting your database if the system
772 * Operations made within a transaction are not visible to other users of the
773 * database until a successful commit.
775 * @param[in] tdb The database to start the transaction.
777 * @return 0 on success, -1 on error with error code set.
780 * @see tdb_errorstr()
782 _PUBLIC_ int tdb_transaction_start(struct tdb_context *tdb);
785 * @brief Start a transaction, non-blocking.
787 * @param[in] tdb The database to start the transaction.
789 * @return 0 on success, -1 on error with error code set.
792 * @see tdb_errorstr()
793 * @see tdb_transaction_start()
795 _PUBLIC_ int tdb_transaction_start_nonblock(struct tdb_context *tdb);
798 * @brief Prepare to commit a current transaction, for two-phase commits.
800 * Once prepared for commit, the only allowed calls are tdb_transaction_commit()
801 * or tdb_transaction_cancel(). Preparing allocates disk space for the pending
802 * updates, so a subsequent commit should succeed (barring any hardware
805 * @param[in] tdb The database to prepare the commit.
807 * @return 0 on success, -1 on error with error code set.
810 * @see tdb_errorstr()
812 _PUBLIC_ int tdb_transaction_prepare_commit(struct tdb_context *tdb);
815 * @brief Commit a current transaction.
817 * This updates the database and releases the current transaction locks.
819 * @param[in] tdb The database to commit the transaction.
821 * @return 0 on success, -1 on error with error code set.
824 * @see tdb_errorstr()
826 _PUBLIC_ int tdb_transaction_commit(struct tdb_context *tdb);
829 * @brief Cancel a current transaction.
831 * This discards all write and lock operations that have been made since the
832 * transaction started.
834 * @param[in] tdb The tdb to cancel the transaction on.
836 * @return 0 on success, -1 on error with error code set.
839 * @see tdb_errorstr()
841 _PUBLIC_ int tdb_transaction_cancel(struct tdb_context *tdb);
844 * @brief Get the tdb sequence number.
846 * Only makes sense if the writers opened with TDB_SEQNUM set. Note that this
847 * sequence number will wrap quite quickly, so it should only be used for a
848 * 'has something changed' test, not for code that relies on the count of the
849 * number of changes made. If you want a counter then use a tdb record.
851 * The aim of this sequence number is to allow for a very lightweight test of a
852 * possible tdb change.
854 * @param[in] tdb The database to get the sequence number from.
856 * @return The sequence number or 0.
859 * @see tdb_enable_seqnum()
861 _PUBLIC_ int tdb_get_seqnum(struct tdb_context *tdb);
864 * @brief Get the hash size.
866 * @param[in] tdb The database to get the hash size from.
868 * @return The hash size.
870 _PUBLIC_ int tdb_hash_size(struct tdb_context *tdb);
873 * @brief Get the map size.
875 * @param[in] tdb The database to get the map size from.
877 * @return The map size.
879 _PUBLIC_ size_t tdb_map_size(struct tdb_context *tdb);
882 * @brief Get the tdb flags set during open.
884 * @param[in] tdb The database to get the flags form.
886 * @return The flags set to on the database.
888 _PUBLIC_ int tdb_get_flags(struct tdb_context *tdb);
891 * @brief Add flags to the database.
893 * @param[in] tdb The database to add the flags.
895 * @param[in] flag The tdb flags to add.
897 _PUBLIC_ void tdb_add_flags(struct tdb_context *tdb, unsigned flag);
900 * @brief Remove flags from the database.
902 * @param[in] tdb The database to remove the flags.
904 * @param[in] flag The tdb flags to remove.
906 _PUBLIC_ void tdb_remove_flags(struct tdb_context *tdb, unsigned flag);
909 * @brief Enable sequence number handling on an open tdb.
911 * @param[in] tdb The database to enable sequence number handling.
913 * @see tdb_get_seqnum()
915 _PUBLIC_ void tdb_enable_seqnum(struct tdb_context *tdb);
918 * @brief Increment the tdb sequence number.
920 * This only works if the tdb has been opened using the TDB_SEQNUM flag or
921 * enabled using tdb_enable_seqnum().
923 * @param[in] tdb The database to increment the sequence number.
925 * @see tdb_enable_seqnum()
926 * @see tdb_get_seqnum()
928 _PUBLIC_ void tdb_increment_seqnum_nonblock(struct tdb_context *tdb);
931 * @brief Create a hash of the key.
933 * @param[in] key The key to hash
937 _PUBLIC_ unsigned int tdb_jenkins_hash(TDB_DATA *key);
940 * @brief Check the consistency of the database.
942 * This check the consistency of the database calling back the check function
943 * (if non-NULL) on each record. If some consistency check fails, or the
944 * supplied check function returns -1, tdb_check returns -1, otherwise 0.
946 * @note The logging function (if set) will be called with additional
947 * information on the corruption found.
949 * @param[in] tdb The database to check.
951 * @param[in] check The check function to use.
953 * @param[in] private_data the private data to pass to the check function.
955 * @return 0 on success, -1 on error with error code set.
958 * @see tdb_errorstr()
960 _PUBLIC_ int tdb_check(struct tdb_context *tdb,
961 int (*check) (TDB_DATA key, TDB_DATA data, void *private_data),
965 * @brief Dump all possible records in a corrupt database.
967 * This is the only way to get data out of a database where tdb_check() fails.
968 * It will call walk() with anything which looks like a database record; this
969 * may well include invalid, incomplete or duplicate records.
971 * @param[in] tdb The database to check.
973 * @param[in] walk The walk function to use.
975 * @param[in] private_data the private data to pass to the walk function.
977 * @return 0 on success, -1 on error with error code set.
980 * @see tdb_errorstr()
982 _PUBLIC_ int tdb_rescue(struct tdb_context *tdb,
983 void (*walk) (TDB_DATA key, TDB_DATA data, void *private_data),
987 * @brief Check if support for TDB_MUTEX_LOCKING is available at runtime.
989 * On some systems the API for pthread_mutexattr_setrobust() is not available.
990 * On other systems there are some bugs in the interaction between glibc and
993 * This function provides a runtime check if robust mutexes are really
996 * This needs to be called and return true before TDB_MUTEX_LOCKING
997 * can be used at runtime.
999 * @note This calls fork(), but the SIGCHILD handling should be transparent.
1001 * @return true if supported, false otherwise.
1003 * @see TDB_MUTEX_LOCKING
1005 _PUBLIC_ bool tdb_runtime_check_for_robust_mutexes(void);
1007 /* @} ******************************************************************/
1009 /* Low level locking functions: use with care */
1010 _PUBLIC_ int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key);
1011 _PUBLIC_ int tdb_chainlock_nonblock(struct tdb_context *tdb, TDB_DATA key);
1012 _PUBLIC_ int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key);
1013 _PUBLIC_ int tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key);
1014 _PUBLIC_ int tdb_chainlock_read_nonblock(struct tdb_context *tdb, TDB_DATA key);
1015 _PUBLIC_ int tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key);
1016 _PUBLIC_ int tdb_chainlock_mark(struct tdb_context *tdb, TDB_DATA key);
1017 _PUBLIC_ int tdb_chainlock_unmark(struct tdb_context *tdb, TDB_DATA key);
1019 _PUBLIC_ void tdb_setalarm_sigptr(struct tdb_context *tdb, volatile sig_atomic_t *sigptr);
1021 /* wipe and repack */
1022 _PUBLIC_ int tdb_wipe_all(struct tdb_context *tdb);
1023 _PUBLIC_ int tdb_repack(struct tdb_context *tdb);
1025 /* Debug functions. Not used in production. */
1026 _PUBLIC_ void tdb_dump_all(struct tdb_context *tdb);
1027 _PUBLIC_ int tdb_printfreelist(struct tdb_context *tdb);
1028 _PUBLIC_ int tdb_validate_freelist(struct tdb_context *tdb, int *pnum_entries);
1029 _PUBLIC_ int tdb_freelist_size(struct tdb_context *tdb);
1030 _PUBLIC_ char *tdb_summary(struct tdb_context *tdb);
1032 _PUBLIC_ extern TDB_DATA tdb_null;