CVE-2013-4408:s3:ctdb_conn: add some length verification to ctdb_packet_more()
[Samba.git] / lib / ntdb / ntdb.h
blobdf3a9ddc4eaee3a6e6f05d81247f6af8bdddbd70
1 #ifndef CCAN_NTDB_H
2 #define CCAN_NTDB_H
4 /*
5 NTDB: trivial database library version 2
7 Copyright (C) Andrew Tridgell 1999-2004
8 Copyright (C) Rusty Russell 2010-2012
10 ** NOTE! The following LGPL license applies to the ntdb
11 ** library. This does NOT imply that all of Samba is released
12 ** under the LGPL
14 This library is free software; you can redistribute it and/or
15 modify it under the terms of the GNU Lesser General Public
16 License as published by the Free Software Foundation; either
17 version 3 of the License, or (at your option) any later version.
19 This library is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 Lesser General Public License for more details.
24 You should have received a copy of the GNU Lesser General Public
25 License along with this library; if not, see <http://www.gnu.org/licenses/>.
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
32 #ifdef HAVE_LIBREPLACE
33 #include <replace.h>
34 #else
35 #if HAVE_FILE_OFFSET_BITS
36 #define _FILE_OFFSET_BITS 64
37 #endif
38 /* For mode_t */
39 #include <sys/types.h>
40 /* For O_* flags. */
41 #include <sys/stat.h>
42 /* For sig_atomic_t. */
43 #include <signal.h>
44 /* For uint64_t */
45 #include <stdint.h>
46 /* For bool */
47 #include <stdbool.h>
48 /* For memcmp */
49 #include <string.h>
50 #endif
52 #if HAVE_CCAN
53 #include <ccan/compiler/compiler.h>
54 #include <ccan/typesafe_cb/typesafe_cb.h>
55 #include <ccan/cast/cast.h>
56 #else
57 #ifndef typesafe_cb_preargs
58 /* Failing to have CCAN just mean less typesafe protection, etc. */
59 #define typesafe_cb_preargs(rtype, atype, fn, arg, ...) \
60 ((rtype (*)(__VA_ARGS__, atype))(fn))
61 #endif
62 #ifndef cast_const
63 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
64 #define cast_const(type, expr) ((type)((intptr_t)(expr)))
65 #else
66 #define cast_const(type, expr) ((type *)(expr))
67 #endif
68 #endif
69 #endif /* !HAVE_CCAN */
71 union ntdb_attribute;
72 struct ntdb_context;
74 /**
75 * struct TDB_DATA - (n)tdb data blob
77 * To ease compatibility, we use 'struct TDB_DATA' from tdb.h, so if
78 * you want to include both tdb.h and ntdb.h, you need to #include
79 * tdb.h first.
81 #ifndef __TDB_H__
82 struct TDB_DATA {
83 unsigned char *dptr;
84 size_t dsize;
86 #endif
88 typedef struct TDB_DATA NTDB_DATA;
90 /**
91 * ntdb_open - open a database file
92 * @name: the file name (or database name if flags contains NTDB_INTERNAL)
93 * @ntdb_flags: options for this database
94 * @open_flags: flags argument for ntdb's open() call.
95 * @mode: mode argument for ntdb's open() call.
96 * @attributes: linked list of extra attributes for this ntdb.
98 * This call opens (and potentially creates) a database file.
99 * Multiple processes can have the NTDB file open at once.
101 * On failure it will return NULL, and set errno: it may also call
102 * any log attribute found in @attributes.
104 * See also:
105 * union ntdb_attribute
107 struct ntdb_context *ntdb_open(const char *name, int ntdb_flags,
108 int open_flags, mode_t mode,
109 union ntdb_attribute *attributes);
112 /* flags for ntdb_open() */
113 #define NTDB_DEFAULT 0 /* just a readability place holder */
114 #define NTDB_INTERNAL 2 /* don't store on disk */
115 #define NTDB_NOLOCK 4 /* don't do any locking */
116 #define NTDB_NOMMAP 8 /* don't use mmap */
117 #define NTDB_CONVERT 16 /* convert endian */
118 #define NTDB_NOSYNC 64 /* don't use synchronous transactions */
119 #define NTDB_SEQNUM 128 /* maintain a sequence number */
120 #define NTDB_ALLOW_NESTING 256 /* fake nested transactions */
121 #define NTDB_RDONLY 512 /* implied by O_RDONLY */
122 #define NTDB_CANT_CHECK 2048 /* has a feature which we don't understand */
125 * ntdb_close - close and free a ntdb.
126 * @ntdb: the ntdb context returned from ntdb_open()
128 * This always succeeds, in that @ntdb is unusable after this call. But if
129 * some unexpected error occurred while closing, it will return non-zero
130 * (the only clue as to cause will be via the log attribute).
132 int ntdb_close(struct ntdb_context *ntdb);
135 * enum NTDB_ERROR - error returns for NTDB
137 * See Also:
138 * ntdb_errorstr()
140 enum NTDB_ERROR {
141 NTDB_SUCCESS = 0, /* No error. */
142 NTDB_ERR_CORRUPT = -1, /* We read the db, and it was bogus. */
143 NTDB_ERR_IO = -2, /* We couldn't read/write the db. */
144 NTDB_ERR_LOCK = -3, /* Locking failed. */
145 NTDB_ERR_OOM = -4, /* Out of Memory. */
146 NTDB_ERR_EXISTS = -5, /* The key already exists. */
147 NTDB_ERR_NOEXIST = -6, /* The key does not exist. */
148 NTDB_ERR_EINVAL = -7, /* You're using it wrong. */
149 NTDB_ERR_RDONLY = -8, /* The database is read-only. */
150 NTDB_ERR_LAST = NTDB_ERR_RDONLY
154 * ntdb_store - store a key/value pair in a ntdb.
155 * @ntdb: the ntdb context returned from ntdb_open()
156 * @key: the key
157 * @dbuf: the data to associate with the key.
158 * @flag: NTDB_REPLACE, NTDB_INSERT or NTDB_MODIFY.
160 * This inserts (or overwrites) a key/value pair in the NTDB. If flag
161 * is NTDB_REPLACE, it doesn't matter whether the key exists or not;
162 * NTDB_INSERT means it must not exist (returns NTDB_ERR_EXISTS otherwise),
163 * and NTDB_MODIFY means it must exist (returns NTDB_ERR_NOEXIST otherwise).
165 * On success, this returns NTDB_SUCCESS.
167 * See also:
168 * ntdb_fetch, ntdb_transaction_start, ntdb_append, ntdb_delete.
170 enum NTDB_ERROR ntdb_store(struct ntdb_context *ntdb,
171 NTDB_DATA key,
172 NTDB_DATA dbuf,
173 int flag);
175 /* flags to ntdb_store() */
176 #define NTDB_REPLACE 1 /* A readability place holder */
177 #define NTDB_INSERT 2 /* Don't overwrite an existing entry */
178 #define NTDB_MODIFY 3 /* Don't create an existing entry */
181 * ntdb_fetch - fetch a value from a ntdb.
182 * @ntdb: the ntdb context returned from ntdb_open()
183 * @key: the key
184 * @data: pointer to data.
186 * This looks up a key in the database and sets it in @data.
188 * If it returns NTDB_SUCCESS, the key was found: it is your
189 * responsibility to call free() on @data->dptr.
191 * Otherwise, it returns an error (usually, NTDB_ERR_NOEXIST) and @data is
192 * undefined.
194 enum NTDB_ERROR ntdb_fetch(struct ntdb_context *ntdb, NTDB_DATA key,
195 NTDB_DATA *data);
198 * ntdb_errorstr - map the ntdb error onto a constant readable string
199 * @ecode: the enum NTDB_ERROR to map.
201 * This is useful for displaying errors to users.
203 const char *ntdb_errorstr(enum NTDB_ERROR ecode);
206 * ntdb_append - append a value to a key/value pair in a ntdb.
207 * @ntdb: the ntdb context returned from ntdb_open()
208 * @key: the key
209 * @dbuf: the data to append.
211 * This is equivalent to fetching a record, reallocating .dptr to add the
212 * data, and writing it back, only it's much more efficient. If the key
213 * doesn't exist, it's equivalent to ntdb_store (with an additional hint that
214 * you expect to expand the record in future).
216 * See Also:
217 * ntdb_fetch(), ntdb_store()
219 enum NTDB_ERROR ntdb_append(struct ntdb_context *ntdb,
220 NTDB_DATA key, NTDB_DATA dbuf);
223 * ntdb_delete - delete a key from a ntdb.
224 * @ntdb: the ntdb context returned from ntdb_open()
225 * @key: the key to delete.
227 * Returns NTDB_SUCCESS on success, or an error (usually NTDB_ERR_NOEXIST).
229 * See Also:
230 * ntdb_fetch(), ntdb_store()
232 enum NTDB_ERROR ntdb_delete(struct ntdb_context *ntdb, NTDB_DATA key);
235 * ntdb_exists - does a key exist in the database?
236 * @ntdb: the ntdb context returned from ntdb_open()
237 * @key: the key to search for.
239 * Returns true if it exists, or false if it doesn't or any other error.
241 bool ntdb_exists(struct ntdb_context *ntdb, NTDB_DATA key);
244 * ntdb_deq - are NTDB_DATA equal?
245 * @a: one NTDB_DATA
246 * @b: another NTDB_DATA
248 static inline bool ntdb_deq(NTDB_DATA a, NTDB_DATA b)
250 return a.dsize == b.dsize && memcmp(a.dptr, b.dptr, a.dsize) == 0;
254 * ntdb_mkdata - make a NTDB_DATA from const data
255 * @p: the constant pointer
256 * @len: the length
258 * As the dptr member of NTDB_DATA is not constant, you need to
259 * cast it. This function keeps thost casts in one place, as well as
260 * suppressing the warning some compilers give when casting away a
261 * qualifier (eg. gcc with -Wcast-qual)
263 static inline NTDB_DATA ntdb_mkdata(const void *p, size_t len)
265 NTDB_DATA d;
266 d.dptr = cast_const(void *, p);
267 d.dsize = len;
268 return d;
272 * ntdb_transaction_start - start a transaction
273 * @ntdb: the ntdb context returned from ntdb_open()
275 * This begins a series of atomic operations. Other processes will be able
276 * to read the ntdb, but not alter it (they will block), nor will they see
277 * any changes until ntdb_transaction_commit() is called.
279 * Note that if the NTDB_ALLOW_NESTING flag is set, a ntdb_transaction_start()
280 * within a transaction will succeed, but it's not a real transaction:
281 * (1) An inner transaction which is committed is not actually committed until
282 * the outer transaction is; if the outer transaction is cancelled, the
283 * inner ones are discarded.
284 * (2) ntdb_transaction_cancel() marks the outer transaction as having an error,
285 * so the final ntdb_transaction_commit() will fail.
286 * (3) the outer transaction will see the results of the inner transaction.
288 * See Also:
289 * ntdb_transaction_cancel, ntdb_transaction_commit.
291 enum NTDB_ERROR ntdb_transaction_start(struct ntdb_context *ntdb);
294 * ntdb_transaction_cancel - abandon a transaction
295 * @ntdb: the ntdb context returned from ntdb_open()
297 * This aborts a transaction, discarding any changes which were made.
298 * ntdb_close() does this implicitly.
300 void ntdb_transaction_cancel(struct ntdb_context *ntdb);
303 * ntdb_transaction_commit - commit a transaction
304 * @ntdb: the ntdb context returned from ntdb_open()
306 * This completes a transaction, writing any changes which were made.
308 * fsync() is used to commit the transaction (unless NTDB_NOSYNC is set),
309 * making it robust against machine crashes, but very slow compared to
310 * other NTDB operations.
312 * A failure can only be caused by unexpected errors (eg. I/O or
313 * memory); this is no point looping on transaction failure.
315 * See Also:
316 * ntdb_transaction_prepare_commit()
318 enum NTDB_ERROR ntdb_transaction_commit(struct ntdb_context *ntdb);
321 * ntdb_transaction_prepare_commit - prepare to commit a transaction
322 * @ntdb: the ntdb context returned from ntdb_open()
324 * This ensures we have the resources to commit a transaction (using
325 * ntdb_transaction_commit): if this succeeds then a transaction will only
326 * fail if the write() or fsync() calls fail.
328 * If this fails you must still call ntdb_transaction_cancel() to cancel
329 * the transaction.
331 * See Also:
332 * ntdb_transaction_commit()
334 enum NTDB_ERROR ntdb_transaction_prepare_commit(struct ntdb_context *ntdb);
337 * ntdb_traverse - traverse a NTDB
338 * @ntdb: the ntdb context returned from ntdb_open()
339 * @fn: the function to call for every key/value pair (or NULL)
340 * @p: the pointer to hand to @f
342 * This walks the NTDB until all they keys have been traversed, or @fn
343 * returns non-zero. If the traverse function or other processes are
344 * changing data or adding or deleting keys, the traverse may be
345 * unreliable: keys may be skipped or (rarely) visited twice.
347 * There is one specific exception: the special case of deleting the
348 * current key does not undermine the reliability of the traversal.
350 * On success, returns the number of keys iterated. On error returns
351 * a negative enum NTDB_ERROR value.
353 #define ntdb_traverse(ntdb, fn, p) \
354 ntdb_traverse_(ntdb, typesafe_cb_preargs(int, void *, (fn), (p), \
355 struct ntdb_context *, \
356 NTDB_DATA, NTDB_DATA), (p))
358 int64_t ntdb_traverse_(struct ntdb_context *ntdb,
359 int (*fn)(struct ntdb_context *,
360 NTDB_DATA, NTDB_DATA, void *), void *p);
363 * ntdb_parse_record - operate directly on data in the database.
364 * @ntdb: the ntdb context returned from ntdb_open()
365 * @key: the key whose record we should hand to @parse
366 * @parse: the function to call for the data
367 * @data: the private pointer to hand to @parse (types must match).
369 * This avoids a copy for many cases, by handing you a pointer into
370 * the memory-mapped database. It also locks the record to prevent
371 * other accesses at the same time, so it won't change.
373 * Within the @parse callback you can perform read operations on the
374 * database, but no write operations: no ntdb_store() or
375 * ntdb_delete(), for example. The exception is if you call
376 * ntdb_lockall() before ntdb_parse_record().
378 * Never alter the data handed to parse()!
380 #define ntdb_parse_record(ntdb, key, parse, data) \
381 ntdb_parse_record_((ntdb), (key), \
382 typesafe_cb_preargs(enum NTDB_ERROR, void *, \
383 (parse), (data), \
384 NTDB_DATA, NTDB_DATA), (data))
386 enum NTDB_ERROR ntdb_parse_record_(struct ntdb_context *ntdb,
387 NTDB_DATA key,
388 enum NTDB_ERROR (*parse)(NTDB_DATA k,
389 NTDB_DATA d,
390 void *data),
391 void *data);
394 * ntdb_get_seqnum - get a database sequence number
395 * @ntdb: the ntdb context returned from ntdb_open()
397 * This returns a sequence number: any change to the database from a
398 * ntdb context opened with the NTDB_SEQNUM flag will cause that number
399 * to increment. Note that the incrementing is unreliable (it is done
400 * without locking), so this is only useful as an optimization.
402 * For example, you may have a regular database backup routine which
403 * does not operate if the sequence number is unchanged. In the
404 * unlikely event of a failed increment, it will be backed up next
405 * time any way.
407 * Returns an enum NTDB_ERROR (ie. negative) on error.
409 int64_t ntdb_get_seqnum(struct ntdb_context *ntdb);
412 * ntdb_firstkey - get the "first" key in a NTDB
413 * @ntdb: the ntdb context returned from ntdb_open()
414 * @key: pointer to key.
416 * This returns an arbitrary key in the database; with ntdb_nextkey() it allows
417 * open-coded traversal of the database, though it is slightly less efficient
418 * than ntdb_traverse.
420 * It is your responsibility to free @key->dptr on success.
422 * Returns NTDB_ERR_NOEXIST if the database is empty.
424 enum NTDB_ERROR ntdb_firstkey(struct ntdb_context *ntdb, NTDB_DATA *key);
427 * ntdb_nextkey - get the "next" key in a NTDB
428 * @ntdb: the ntdb context returned from ntdb_open()
429 * @key: a key returned by ntdb_firstkey() or ntdb_nextkey().
431 * This returns another key in the database; it will free @key.dptr for
432 * your convenience.
434 * Returns NTDB_ERR_NOEXIST if there are no more keys.
436 enum NTDB_ERROR ntdb_nextkey(struct ntdb_context *ntdb, NTDB_DATA *key);
439 * ntdb_chainlock - lock a record in the NTDB
440 * @ntdb: the ntdb context returned from ntdb_open()
441 * @key: the key to lock.
443 * This prevents any access occurring to a group of keys including @key,
444 * even if @key does not exist. This allows primitive atomic updates of
445 * records without using transactions.
447 * You cannot begin a transaction while holding a ntdb_chainlock(), nor can
448 * you do any operations on any other keys in the database. This also means
449 * that you cannot hold more than one ntdb_chainlock() at a time.
451 * See Also:
452 * ntdb_chainunlock()
454 enum NTDB_ERROR ntdb_chainlock(struct ntdb_context *ntdb, NTDB_DATA key);
457 * ntdb_chainunlock - unlock a record in the NTDB
458 * @ntdb: the ntdb context returned from ntdb_open()
459 * @key: the key to unlock.
461 * The key must have previously been locked by ntdb_chainlock().
463 void ntdb_chainunlock(struct ntdb_context *ntdb, NTDB_DATA key);
466 * ntdb_chainlock_read - lock a record in the NTDB, for reading
467 * @ntdb: the ntdb context returned from ntdb_open()
468 * @key: the key to lock.
470 * This prevents any changes from occurring to a group of keys including @key,
471 * even if @key does not exist. This allows primitive atomic updates of
472 * records without using transactions.
474 * You cannot begin a transaction while holding a ntdb_chainlock_read(), nor can
475 * you do any operations on any other keys in the database. This also means
476 * that you cannot hold more than one ntdb_chainlock()/read() at a time.
478 * See Also:
479 * ntdb_chainlock()
481 enum NTDB_ERROR ntdb_chainlock_read(struct ntdb_context *ntdb, NTDB_DATA key);
484 * ntdb_chainunlock_read - unlock a record in the NTDB for reading
485 * @ntdb: the ntdb context returned from ntdb_open()
486 * @key: the key to unlock.
488 * The key must have previously been locked by ntdb_chainlock_read().
490 void ntdb_chainunlock_read(struct ntdb_context *ntdb, NTDB_DATA key);
493 * ntdb_lockall - lock the entire NTDB
494 * @ntdb: the ntdb context returned from ntdb_open()
496 * You cannot hold a ntdb_chainlock while calling this. It nests, so you
497 * must call ntdb_unlockall as many times as you call ntdb_lockall.
499 enum NTDB_ERROR ntdb_lockall(struct ntdb_context *ntdb);
502 * ntdb_unlockall - unlock the entire NTDB
503 * @ntdb: the ntdb context returned from ntdb_open()
505 void ntdb_unlockall(struct ntdb_context *ntdb);
508 * ntdb_lockall_read - lock the entire NTDB for reading
509 * @ntdb: the ntdb context returned from ntdb_open()
511 * This prevents others writing to the database, eg. ntdb_delete, ntdb_store,
512 * ntdb_append, but not ntdb_fetch.
514 * You cannot hold a ntdb_chainlock while calling this. It nests, so you
515 * must call ntdb_unlockall_read as many times as you call ntdb_lockall_read.
517 enum NTDB_ERROR ntdb_lockall_read(struct ntdb_context *ntdb);
520 * ntdb_unlockall_read - unlock the entire NTDB for reading
521 * @ntdb: the ntdb context returned from ntdb_open()
523 void ntdb_unlockall_read(struct ntdb_context *ntdb);
526 * ntdb_wipe_all - wipe the database clean
527 * @ntdb: the ntdb context returned from ntdb_open()
529 * Completely erase the database. This is faster than iterating through
530 * each key and doing ntdb_delete.
532 enum NTDB_ERROR ntdb_wipe_all(struct ntdb_context *ntdb);
535 * ntdb_repack - repack the database
536 * @ntdb: the ntdb context returned from ntdb_open()
538 * This repacks the database; if it is suffering from a great deal of
539 * fragmentation this might help. However, it can take twice the
540 * memory of the existing NTDB.
542 enum NTDB_ERROR ntdb_repack(struct ntdb_context *ntdb);
545 * ntdb_check - check a NTDB for consistency
546 * @ntdb: the ntdb context returned from ntdb_open()
547 * @check: function to check each key/data pair (or NULL)
548 * @data: argument for @check, must match type.
550 * This performs a consistency check of the open database, optionally calling
551 * a check() function on each record so you can do your own data consistency
552 * checks as well. If check() returns an error, that is returned from
553 * ntdb_check().
555 * Note that the NTDB uses a feature which we don't understand which
556 * indicates we can't run ntdb_check(), this will log a warning to that
557 * effect and return NTDB_SUCCESS. You can detect this condition by
558 * looking for NTDB_CANT_CHECK in ntdb_get_flags().
560 * Returns NTDB_SUCCESS or an error.
562 #define ntdb_check(ntdb, check, data) \
563 ntdb_check_((ntdb), typesafe_cb_preargs(enum NTDB_ERROR, void *, \
564 (check), (data), \
565 NTDB_DATA, \
566 NTDB_DATA), \
567 (data))
569 enum NTDB_ERROR ntdb_check_(struct ntdb_context *ntdb,
570 enum NTDB_ERROR (*check)(NTDB_DATA k,
571 NTDB_DATA d,
572 void *data),
573 void *data);
576 * enum ntdb_summary_flags - flags for ntdb_summary.
578 enum ntdb_summary_flags {
579 NTDB_SUMMARY_HISTOGRAMS = 1 /* Draw graphs in the summary. */
583 * ntdb_summary - return a string describing the NTDB state
584 * @ntdb: the ntdb context returned from ntdb_open()
585 * @flags: flags to control the summary output.
586 * @summary: pointer to string to allocate.
588 * This returns a developer-readable string describing the overall
589 * state of the ntdb, such as the percentage used and sizes of records.
590 * It is designed to provide information about the ntdb at a glance
591 * without displaying any keys or data in the database.
593 * On success, sets @summary to point to a malloc()'ed nul-terminated
594 * multi-line string. It is your responsibility to free() it.
596 enum NTDB_ERROR ntdb_summary(struct ntdb_context *ntdb,
597 enum ntdb_summary_flags flags,
598 char **summary);
602 * ntdb_get_flags - return the flags for a ntdb
603 * @ntdb: the ntdb context returned from ntdb_open()
605 * This returns the flags on the current ntdb. Some of these are caused by
606 * the flags argument to ntdb_open(), others (such as NTDB_CONVERT) are
607 * intuited.
609 unsigned int ntdb_get_flags(struct ntdb_context *ntdb);
612 * ntdb_add_flag - set a flag for a ntdb
613 * @ntdb: the ntdb context returned from ntdb_open()
614 * @flag: one of NTDB_NOLOCK, NTDB_NOMMAP, NTDB_NOSYNC or NTDB_ALLOW_NESTING.
616 * You can use this to set a flag on the NTDB. You cannot set these flags
617 * on a NTDB_INTERNAL ntdb.
619 void ntdb_add_flag(struct ntdb_context *ntdb, unsigned flag);
622 * ntdb_remove_flag - unset a flag for a ntdb
623 * @ntdb: the ntdb context returned from ntdb_open()
624 * @flag: one of NTDB_NOLOCK, NTDB_NOMMAP, NTDB_NOSYNC or NTDB_ALLOW_NESTING.
626 * You can use this to clear a flag on the NTDB. You cannot clear flags
627 * on a NTDB_INTERNAL ntdb.
629 void ntdb_remove_flag(struct ntdb_context *ntdb, unsigned flag);
632 * enum ntdb_attribute_type - descriminator for union ntdb_attribute.
634 enum ntdb_attribute_type {
635 NTDB_ATTRIBUTE_LOG = 0,
636 NTDB_ATTRIBUTE_HASH = 1,
637 NTDB_ATTRIBUTE_SEED = 2,
638 NTDB_ATTRIBUTE_STATS = 3,
639 NTDB_ATTRIBUTE_OPENHOOK = 4,
640 NTDB_ATTRIBUTE_FLOCK = 5,
641 NTDB_ATTRIBUTE_ALLOCATOR = 6,
642 NTDB_ATTRIBUTE_HASHSIZE = 7
646 * ntdb_get_attribute - get an attribute for an existing ntdb
647 * @ntdb: the ntdb context returned from ntdb_open()
648 * @attr: the union ntdb_attribute to set.
650 * This gets an attribute from a NTDB which has previously been set (or
651 * may return the default values). Set @attr.base.attr to the
652 * attribute type you want get.
654 enum NTDB_ERROR ntdb_get_attribute(struct ntdb_context *ntdb,
655 union ntdb_attribute *attr);
658 * ntdb_set_attribute - set an attribute for an existing ntdb
659 * @ntdb: the ntdb context returned from ntdb_open()
660 * @attr: the union ntdb_attribute to set.
662 * This sets an attribute on a NTDB, overriding any previous attribute
663 * of the same type. It returns NTDB_ERR_EINVAL if the attribute is
664 * unknown or invalid.
666 * Note that NTDB_ATTRIBUTE_HASH, NTDB_ATTRIBUTE_SEED, and
667 * NTDB_ATTRIBUTE_OPENHOOK cannot currently be set after ntdb_open.
669 enum NTDB_ERROR ntdb_set_attribute(struct ntdb_context *ntdb,
670 const union ntdb_attribute *attr);
673 * ntdb_unset_attribute - reset an attribute for an existing ntdb
674 * @ntdb: the ntdb context returned from ntdb_open()
675 * @type: the attribute type to unset.
677 * This unsets an attribute on a NTDB, returning it to the defaults
678 * (where applicable).
680 * Note that it only makes sense for NTDB_ATTRIBUTE_LOG and NTDB_ATTRIBUTE_FLOCK
681 * to be unset.
683 void ntdb_unset_attribute(struct ntdb_context *ntdb,
684 enum ntdb_attribute_type type);
687 * ntdb_name - get the name of a ntdb
688 * @ntdb: the ntdb context returned from ntdb_open()
690 * This returns a copy of the name string, made at ntdb_open() time.
692 * This is mostly useful for logging.
694 const char *ntdb_name(const struct ntdb_context *ntdb);
697 * ntdb_fd - get the file descriptor of a ntdb
698 * @ntdb: the ntdb context returned from ntdb_open()
700 * This returns the file descriptor for the underlying database file, or -1
701 * for NTDB_INTERNAL.
703 int ntdb_fd(const struct ntdb_context *ntdb);
706 * ntdb_foreach - iterate through every open NTDB.
707 * @fn: the function to call for every NTDB
708 * @p: the pointer to hand to @fn
710 * NTDB internally keeps track of all open TDBs; this function allows you to
711 * iterate through them. If @fn returns non-zero, traversal stops.
713 #define ntdb_foreach(fn, p) \
714 ntdb_foreach_(typesafe_cb_preargs(int, void *, (fn), (p), \
715 struct ntdb_context *), (p))
717 void ntdb_foreach_(int (*fn)(struct ntdb_context *, void *), void *p);
720 * struct ntdb_attribute_base - common fields for all ntdb attributes.
722 struct ntdb_attribute_base {
723 enum ntdb_attribute_type attr;
724 union ntdb_attribute *next;
728 * enum ntdb_log_level - log levels for ntdb_attribute_log
729 * @NTDB_LOG_ERROR: used to log unrecoverable errors such as I/O errors
730 * or internal consistency failures.
731 * @NTDB_LOG_USE_ERROR: used to log usage errors such as invalid parameters
732 * or writing to a read-only database.
733 * @NTDB_LOG_WARNING: used for informational messages on issues which
734 * are unusual but handled by NTDB internally, such
735 * as a failure to mmap or failure to open /dev/urandom.
736 * It's also used when ntdb_open() fails without O_CREAT
737 * because a file does not exist.
739 enum ntdb_log_level {
740 NTDB_LOG_ERROR,
741 NTDB_LOG_USE_ERROR,
742 NTDB_LOG_WARNING
746 * struct ntdb_attribute_log - log function attribute
748 * This attribute provides a hook for you to log errors.
750 struct ntdb_attribute_log {
751 struct ntdb_attribute_base base; /* .attr = NTDB_ATTRIBUTE_LOG */
752 void (*fn)(struct ntdb_context *ntdb,
753 enum ntdb_log_level level,
754 enum NTDB_ERROR ecode,
755 const char *message,
756 void *data);
757 void *data;
761 * struct ntdb_attribute_hash - hash function attribute
763 * This attribute allows you to provide an alternative hash function.
764 * This hash function will be handed keys from the database; it will also
765 * be handed the 8-byte NTDB_HASH_MAGIC value for checking the header (the
766 * ntdb_open() will fail if the hash value doesn't match the header).
768 * Note that if your hash function gives different results on
769 * different machine endians, your ntdb will no longer work across
770 * different architectures!
772 struct ntdb_attribute_hash {
773 struct ntdb_attribute_base base; /* .attr = NTDB_ATTRIBUTE_HASH */
774 uint32_t (*fn)(const void *key, size_t len, uint32_t seed,
775 void *data);
776 void *data;
780 * struct ntdb_attribute_seed - hash function seed attribute
782 * The hash function seed is normally taken from /dev/urandom (or equivalent)
783 * but can be set manually here. This is mainly for testing purposes.
785 struct ntdb_attribute_seed {
786 struct ntdb_attribute_base base; /* .attr = NTDB_ATTRIBUTE_SEED */
787 uint64_t seed;
791 * struct ntdb_attribute_stats - ntdb operational statistics
793 * This attribute records statistics of various low-level NTDB operations.
794 * This can be used to assist performance evaluation. This is only
795 * useful for ntdb_get_attribute().
797 * New fields will be added at the end, hence the "size" argument which
798 * indicates how large your structure is: it must be filled in before
799 * calling ntdb_get_attribute(), which will overwrite it with the size
800 * ntdb knows about.
802 struct ntdb_attribute_stats {
803 struct ntdb_attribute_base base; /* .attr = NTDB_ATTRIBUTE_STATS */
804 size_t size; /* = sizeof(struct ntdb_attribute_stats) */
805 uint64_t allocs;
806 uint64_t alloc_subhash;
807 uint64_t alloc_chain;
808 uint64_t alloc_bucket_exact;
809 uint64_t alloc_bucket_max;
810 uint64_t alloc_leftover;
811 uint64_t alloc_coalesce_tried;
812 uint64_t alloc_coalesce_iterate_clash;
813 uint64_t alloc_coalesce_lockfail;
814 uint64_t alloc_coalesce_race;
815 uint64_t alloc_coalesce_succeeded;
816 uint64_t alloc_coalesce_num_merged;
817 uint64_t compares;
818 uint64_t compare_wrong_offsetbits;
819 uint64_t compare_wrong_keylen;
820 uint64_t compare_wrong_rechash;
821 uint64_t compare_wrong_keycmp;
822 uint64_t transactions;
823 uint64_t transaction_cancel;
824 uint64_t transaction_nest;
825 uint64_t transaction_expand_file;
826 uint64_t transaction_read_direct;
827 uint64_t transaction_read_direct_fail;
828 uint64_t transaction_write_direct;
829 uint64_t transaction_write_direct_fail;
830 uint64_t traverses;
831 uint64_t traverse_val_vanished;
832 uint64_t expands;
833 uint64_t frees;
834 uint64_t locks;
835 uint64_t lock_lowlevel;
836 uint64_t lock_nonblock;
837 uint64_t lock_nonblock_fail;
841 * struct ntdb_attribute_openhook - ntdb special effects hook for open
843 * This attribute contains a function to call once we have the OPEN_LOCK
844 * for the ntdb, but before we've examined its contents. If this succeeds,
845 * the ntdb will be populated if it's then zero-length.
847 * This is a hack to allow support for TDB-style TDB_CLEAR_IF_FIRST
848 * behaviour.
850 struct ntdb_attribute_openhook {
851 struct ntdb_attribute_base base; /* .attr = NTDB_ATTRIBUTE_OPENHOOK */
852 enum NTDB_ERROR (*fn)(int fd, void *data);
853 void *data;
857 * struct ntdb_attribute_flock - ntdb special effects hook for file locking
859 * This attribute contains function to call to place locks on a file; it can
860 * be used to support non-blocking operations or lock proxying.
862 * They should return 0 on success, -1 on failure and set errno.
864 * An error will be logged on error if errno is neither EAGAIN nor EINTR
865 * (normally it would only return EAGAIN if waitflag is false, and
866 * loop internally on EINTR).
868 struct ntdb_attribute_flock {
869 struct ntdb_attribute_base base; /* .attr = NTDB_ATTRIBUTE_FLOCK */
870 int (*lock)(int fd,int rw, off_t off, off_t len, bool waitflag, void *);
871 int (*unlock)(int fd, int rw, off_t off, off_t len, void *);
872 void *data;
876 * struct ntdb_attribute_hashsize - ntdb hashsize setting.
878 * This attribute is only settable on ntdb_open; it indicates that we create
879 * a hashtable of the given size, rather than the default.
881 struct ntdb_attribute_hashsize {
882 struct ntdb_attribute_base base; /* .attr = NTDB_ATTRIBUTE_HASHSIZE */
883 uint32_t size;
887 * struct ntdb_attribute_allocator - allocator for ntdb to use.
889 * You can replace malloc/free with your own allocation functions.
890 * The allocator takes an "owner" pointer, which is either NULL (for
891 * the initial struct ntdb_context and struct ntdb_file), or a
892 * previously allocated pointer. This is useful for relationship
893 * tracking, such as the talloc library.
895 * The expand function is realloc, but only ever used to expand an
896 * existing allocation.
898 * Be careful mixing allocators: two ntdb_contexts which have the same file
899 * open will share the same struct ntdb_file. This may be allocated by one
900 * ntdb's allocator, and freed by the other.
902 struct ntdb_attribute_allocator {
903 struct ntdb_attribute_base base; /* .attr = NTDB_ATTRIBUTE_ALLOCATOR */
904 void *(*alloc)(const void *owner, size_t len, void *priv_data);
905 void *(*expand)(void *old, size_t newlen, void *priv_data);
906 void (*free)(void *old, void *priv_data);
907 void *priv_data;
911 * union ntdb_attribute - ntdb attributes.
913 * This represents all the known attributes.
915 * See also:
916 * struct ntdb_attribute_log, struct ntdb_attribute_hash,
917 * struct ntdb_attribute_seed, struct ntdb_attribute_stats,
918 * struct ntdb_attribute_openhook, struct ntdb_attribute_flock,
919 * struct ntdb_attribute_allocator alloc.
921 union ntdb_attribute {
922 struct ntdb_attribute_base base;
923 struct ntdb_attribute_log log;
924 struct ntdb_attribute_hash hash;
925 struct ntdb_attribute_seed seed;
926 struct ntdb_attribute_stats stats;
927 struct ntdb_attribute_openhook openhook;
928 struct ntdb_attribute_flock flock;
929 struct ntdb_attribute_allocator alloc;
930 struct ntdb_attribute_hashsize hashsize;
933 #ifdef __cplusplus
935 #endif
937 #endif /* ntdb.h */