2 Unix SMB/CIFS implementation.
4 trivial database library
6 Copyright (C) Andrew Tridgell 1999-2005
7 Copyright (C) Paul `Rusty' Russell 2000
8 Copyright (C) Jeremy Allison 2000-2003
10 ** NOTE! The following LGPL license applies to the tdb
11 ** library. This does NOT imply that all of Samba is released
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 #include "tdb_private.h"
33 non-blocking increment of the tdb sequence number if the tdb has been opened using
36 void tdb_increment_seqnum_nonblock(struct tdb_context *tdb)
40 if (!(tdb->flags & TDB_SEQNUM)) {
44 tdb_trace(tdb, "tdb_increment_seqnum_nonblock");
46 /* we ignore errors from this, as we have no sane way of
49 tdb_ofs_read(tdb, TDB_SEQNUM_OFS, &seqnum);
51 tdb_ofs_write(tdb, TDB_SEQNUM_OFS, &seqnum);
55 increment the tdb sequence number if the tdb has been opened using
58 static void tdb_increment_seqnum(struct tdb_context *tdb)
60 if (!(tdb->flags & TDB_SEQNUM)) {
64 if (tdb_brlock(tdb, TDB_SEQNUM_OFS, F_WRLCK, F_SETLKW, 1, 1) != 0) {
68 tdb_increment_seqnum_nonblock(tdb);
70 tdb_brlock(tdb, TDB_SEQNUM_OFS, F_UNLCK, F_SETLKW, 1, 1);
73 static int tdb_key_compare(TDB_DATA key, TDB_DATA data, void *private_data)
75 return memcmp(data.dptr, key.dptr, data.dsize);
78 /* Returns 0 on fail. On success, return offset of record, and fills
80 static tdb_off_t tdb_find(struct tdb_context *tdb, TDB_DATA key, uint32_t hash,
81 struct list_struct *r)
85 /* read in the hash top */
86 if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
89 /* keep looking until we find the right record */
91 if (tdb_rec_read(tdb, rec_ptr, r) == -1)
94 if (!TDB_DEAD(r) && hash==r->full_hash
95 && key.dsize==r->key_len
96 && tdb_parse_data(tdb, key, rec_ptr + sizeof(*r),
97 r->key_len, tdb_key_compare,
103 return TDB_ERRCODE(TDB_ERR_NOEXIST, 0);
106 /* As tdb_find, but if you succeed, keep the lock */
107 tdb_off_t tdb_find_lock_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash, int locktype,
108 struct list_struct *rec)
112 if (tdb_lock(tdb, BUCKET(hash), locktype) == -1)
114 if (!(rec_ptr = tdb_find(tdb, key, hash, rec)))
115 tdb_unlock(tdb, BUCKET(hash), locktype);
120 /* update an entry in place - this only works if the new data size
121 is <= the old data size and the key exists.
122 on failure return -1.
124 static int tdb_update_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash, TDB_DATA dbuf)
126 struct list_struct rec;
130 if (!(rec_ptr = tdb_find(tdb, key, hash, &rec)))
133 /* must be long enough key, data and tailer */
134 if (rec.rec_len < key.dsize + dbuf.dsize + sizeof(tdb_off_t)) {
135 tdb->ecode = TDB_SUCCESS; /* Not really an error */
139 if (tdb->methods->tdb_write(tdb, rec_ptr + sizeof(rec) + rec.key_len,
140 dbuf.dptr, dbuf.dsize) == -1)
143 if (dbuf.dsize != rec.data_len) {
145 rec.data_len = dbuf.dsize;
146 return tdb_rec_write(tdb, rec_ptr, &rec);
152 /* find an entry in the database given a key */
153 /* If an entry doesn't exist tdb_err will be set to
154 * TDB_ERR_NOEXIST. If a key has no data attached
155 * then the TDB_DATA will have zero length but
158 TDB_DATA tdb_fetch(struct tdb_context *tdb, TDB_DATA key)
161 struct list_struct rec;
165 /* find which hash bucket it is in */
166 hash = tdb->hash_fn(&key);
167 if (!(rec_ptr = tdb_find_lock_hash(tdb,key,hash,F_RDLCK,&rec))) {
168 tdb_trace(tdb, "tdb_fetch ");
169 tdb_trace_record(tdb, key);
170 tdb_trace(tdb, "= ENOENT\n");
173 ret.dptr = tdb_alloc_read(tdb, rec_ptr + sizeof(rec) + rec.key_len,
175 ret.dsize = rec.data_len;
176 tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
177 tdb_trace(tdb, "tdb_fetch ");
178 tdb_trace_record(tdb, key);
179 tdb_trace(tdb, "= ");
180 tdb_trace_record(tdb, ret);
181 tdb_trace(tdb, "\n");
186 * Find an entry in the database and hand the record's data to a parsing
187 * function. The parsing function is executed under the chain read lock, so it
188 * should be fast and should not block on other syscalls.
190 * DONT CALL OTHER TDB CALLS FROM THE PARSER, THIS MIGHT LEAD TO SEGFAULTS.
192 * For mmapped tdb's that do not have a transaction open it points the parsing
193 * function directly at the mmap area, it avoids the malloc/memcpy in this
194 * case. If a transaction is open or no mmap is available, it has to do
195 * malloc/read/parse/free.
197 * This is interesting for all readers of potentially large data structures in
198 * the tdb records, ldb indexes being one example.
201 int tdb_parse_record(struct tdb_context *tdb, TDB_DATA key,
202 int (*parser)(TDB_DATA key, TDB_DATA data,
207 struct list_struct rec;
211 /* find which hash bucket it is in */
212 hash = tdb->hash_fn(&key);
214 if (!(rec_ptr = tdb_find_lock_hash(tdb,key,hash,F_RDLCK,&rec))) {
215 tdb_trace(tdb, "tdb_parse_record ");
216 tdb_trace_record(tdb, key);
217 tdb_trace(tdb, "= ENOENT\n");
218 return TDB_ERRCODE(TDB_ERR_NOEXIST, 0);
221 tdb_trace(tdb, "tdb_parse_record ");
222 tdb_trace_record(tdb, key);
223 tdb_trace(tdb, "= %u\n", rec.data_len);
225 ret = tdb_parse_data(tdb, key, rec_ptr + sizeof(rec) + rec.key_len,
226 rec.data_len, parser, private_data);
228 tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
233 /* check if an entry in the database exists
235 note that 1 is returned if the key is found and 0 is returned if not found
236 this doesn't match the conventions in the rest of this module, but is
239 static int tdb_exists_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash)
241 struct list_struct rec;
243 if (tdb_find_lock_hash(tdb, key, hash, F_RDLCK, &rec) == 0)
245 tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
249 int tdb_exists(struct tdb_context *tdb, TDB_DATA key)
251 uint32_t hash = tdb->hash_fn(&key);
254 ret = tdb_exists_hash(tdb, key, hash);
255 tdb_trace(tdb, "tdb_exists ");
256 tdb_trace_record(tdb, key);
257 tdb_trace(tdb, "= %i\n", ret);
261 /* actually delete an entry in the database given the offset */
262 int tdb_do_delete(struct tdb_context *tdb, tdb_off_t rec_ptr, struct list_struct *rec)
264 tdb_off_t last_ptr, i;
265 struct list_struct lastrec;
267 if (tdb->read_only || tdb->traverse_read) return -1;
269 if (tdb->traverse_write != 0 ||
270 tdb_write_lock_record(tdb, rec_ptr) == -1) {
271 /* Someone traversing here: mark it as dead */
272 rec->magic = TDB_DEAD_MAGIC;
273 return tdb_rec_write(tdb, rec_ptr, rec);
275 if (tdb_write_unlock_record(tdb, rec_ptr) != 0)
278 /* find previous record in hash chain */
279 if (tdb_ofs_read(tdb, TDB_HASH_TOP(rec->full_hash), &i) == -1)
281 for (last_ptr = 0; i != rec_ptr; last_ptr = i, i = lastrec.next)
282 if (tdb_rec_read(tdb, i, &lastrec) == -1)
285 /* unlink it: next ptr is at start of record. */
287 last_ptr = TDB_HASH_TOP(rec->full_hash);
288 if (tdb_ofs_write(tdb, last_ptr, &rec->next) == -1)
291 /* recover the space */
292 if (tdb_free(tdb, rec_ptr, rec) == -1)
297 static int tdb_count_dead(struct tdb_context *tdb, uint32_t hash)
301 struct list_struct rec;
303 /* read in the hash top */
304 if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
308 if (tdb_rec_read(tdb, rec_ptr, &rec) == -1)
311 if (rec.magic == TDB_DEAD_MAGIC) {
320 * Purge all DEAD records from a hash chain
322 static int tdb_purge_dead(struct tdb_context *tdb, uint32_t hash)
325 struct list_struct rec;
328 if (tdb_lock(tdb, -1, F_WRLCK) == -1) {
332 /* read in the hash top */
333 if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
339 if (tdb_rec_read(tdb, rec_ptr, &rec) == -1) {
345 if (rec.magic == TDB_DEAD_MAGIC
346 && tdb_do_delete(tdb, rec_ptr, &rec) == -1) {
353 tdb_unlock(tdb, -1, F_WRLCK);
357 /* delete an entry in the database given a key */
358 static int tdb_delete_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash)
361 struct list_struct rec;
364 if (tdb->max_dead_records != 0) {
367 * Allow for some dead records per hash chain, mainly for
368 * tdb's with a very high create/delete rate like locking.tdb.
371 if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
374 if (tdb_count_dead(tdb, hash) >= tdb->max_dead_records) {
376 * Don't let the per-chain freelist grow too large,
377 * delete all existing dead records
379 tdb_purge_dead(tdb, hash);
382 if (!(rec_ptr = tdb_find(tdb, key, hash, &rec))) {
383 tdb_unlock(tdb, BUCKET(hash), F_WRLCK);
388 * Just mark the record as dead.
390 rec.magic = TDB_DEAD_MAGIC;
391 ret = tdb_rec_write(tdb, rec_ptr, &rec);
394 if (!(rec_ptr = tdb_find_lock_hash(tdb, key, hash, F_WRLCK,
398 ret = tdb_do_delete(tdb, rec_ptr, &rec);
402 tdb_increment_seqnum(tdb);
405 if (tdb_unlock(tdb, BUCKET(rec.full_hash), F_WRLCK) != 0)
406 TDB_LOG((tdb, TDB_DEBUG_WARNING, "tdb_delete: WARNING tdb_unlock failed!\n"));
410 int tdb_delete(struct tdb_context *tdb, TDB_DATA key)
412 uint32_t hash = tdb->hash_fn(&key);
415 ret = tdb_delete_hash(tdb, key, hash);
416 tdb_trace(tdb, "tdb_delete ");
417 tdb_trace_record(tdb, key);
418 tdb_trace(tdb, "= %s\n", ret ? "ENOENT" : "0");
423 * See if we have a dead record around with enough space
425 static tdb_off_t tdb_find_dead(struct tdb_context *tdb, uint32_t hash,
426 struct list_struct *r, tdb_len_t length)
430 /* read in the hash top */
431 if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
434 /* keep looking until we find the right record */
436 if (tdb_rec_read(tdb, rec_ptr, r) == -1)
439 if (TDB_DEAD(r) && r->rec_len >= length) {
441 * First fit for simple coding, TODO: change to best
451 /* store an element in the database, replacing any existing element
454 return 0 on success, -1 on failure
456 int tdb_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag)
458 struct list_struct rec;
464 if (tdb->read_only || tdb->traverse_read) {
465 tdb->ecode = TDB_ERR_RDONLY;
469 /* find which hash bucket it is in */
470 hash = tdb->hash_fn(&key);
471 if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
474 tdb_trace(tdb, "tdb_store %s ", flag == TDB_INSERT ? "insert" :
475 flag == TDB_MODIFY ? "modify" : "normal");
476 tdb_trace_record(tdb, key);
477 tdb_trace_record(tdb, dbuf);
479 /* check for it existing, on insert. */
480 if (flag == TDB_INSERT) {
481 if (tdb_exists_hash(tdb, key, hash)) {
482 tdb->ecode = TDB_ERR_EXISTS;
483 tdb_trace(tdb, "= EEXIST\n");
487 /* first try in-place update, on modify or replace. */
488 if (tdb_update_hash(tdb, key, hash, dbuf) == 0) {
489 tdb_trace(tdb, "= inplace\n");
492 if (tdb->ecode == TDB_ERR_NOEXIST &&
493 flag == TDB_MODIFY) {
494 /* if the record doesn't exist and we are in TDB_MODIFY mode then
495 we should fail the store */
496 tdb_trace(tdb, "= ENOENT\n");
500 /* reset the error code potentially set by the tdb_update() */
501 tdb->ecode = TDB_SUCCESS;
503 /* delete any existing record - if it doesn't exist we don't
504 care. Doing this first reduces fragmentation, and avoids
505 coalescing with `allocated' block before it's updated. */
506 if (flag != TDB_INSERT)
507 tdb_delete_hash(tdb, key, hash);
509 /* Copy key+value *before* allocating free space in case malloc
510 fails and we are left with a dead spot in the tdb. */
512 if (!(p = (char *)malloc(key.dsize + dbuf.dsize))) {
513 tdb->ecode = TDB_ERR_OOM;
517 memcpy(p, key.dptr, key.dsize);
519 memcpy(p+key.dsize, dbuf.dptr, dbuf.dsize);
521 if (tdb->max_dead_records != 0) {
523 * Allow for some dead records per hash chain, look if we can
524 * find one that can hold the new record. We need enough space
525 * for key, data and tailer. If we find one, we don't have to
526 * consult the central freelist.
528 rec_ptr = tdb_find_dead(
530 key.dsize + dbuf.dsize + sizeof(tdb_off_t));
533 rec.key_len = key.dsize;
534 rec.data_len = dbuf.dsize;
535 rec.full_hash = hash;
536 rec.magic = TDB_MAGIC;
537 if (tdb_rec_write(tdb, rec_ptr, &rec) == -1
538 || tdb->methods->tdb_write(
539 tdb, rec_ptr + sizeof(rec),
540 p, key.dsize + dbuf.dsize) == -1) {
544 tdb_trace(tdb, "= fromdead\n");
549 * We have to allocate some space from the freelist, so this means we
550 * have to lock it. Use the chance to purge all the DEAD records from
551 * the hash chain under the freelist lock.
554 if (tdb_lock(tdb, -1, F_WRLCK) == -1) {
558 if ((tdb->max_dead_records != 0)
559 && (tdb_purge_dead(tdb, hash) == -1)) {
560 tdb_unlock(tdb, -1, F_WRLCK);
564 /* we have to allocate some space */
565 rec_ptr = tdb_allocate(tdb, key.dsize + dbuf.dsize, &rec);
567 tdb_unlock(tdb, -1, F_WRLCK);
573 /* Read hash top into next ptr */
574 if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec.next) == -1)
577 rec.key_len = key.dsize;
578 rec.data_len = dbuf.dsize;
579 rec.full_hash = hash;
580 rec.magic = TDB_MAGIC;
582 tdb_trace(tdb, "= allocate\n");
584 /* write out and point the top of the hash chain at it */
585 if (tdb_rec_write(tdb, rec_ptr, &rec) == -1
586 || tdb->methods->tdb_write(tdb, rec_ptr+sizeof(rec), p, key.dsize+dbuf.dsize)==-1
587 || tdb_ofs_write(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1) {
588 /* Need to tdb_unallocate() here */
596 tdb_increment_seqnum(tdb);
600 tdb_unlock(tdb, BUCKET(hash), F_WRLCK);
605 /* Append to an entry. Create if not exist. */
606 int tdb_append(struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf)
612 /* find which hash bucket it is in */
613 hash = tdb->hash_fn(&key);
614 if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
617 dbuf = tdb_fetch(tdb, key);
618 tdb_trace(tdb, "tdb_append ");
619 tdb_trace_record(tdb, key);
620 tdb_trace_record(tdb, dbuf);
621 tdb_trace(tdb, "= %s\n", dbuf.dptr ? "insert" : "append");
623 if (dbuf.dptr == NULL) {
624 dbuf.dptr = (unsigned char *)malloc(new_dbuf.dsize);
626 unsigned int new_len = dbuf.dsize + new_dbuf.dsize;
627 unsigned char *new_dptr;
629 /* realloc '0' is special: don't do that. */
632 new_dptr = (unsigned char *)realloc(dbuf.dptr, new_len);
633 if (new_dptr == NULL) {
636 dbuf.dptr = new_dptr;
639 if (dbuf.dptr == NULL) {
640 tdb->ecode = TDB_ERR_OOM;
644 memcpy(dbuf.dptr + dbuf.dsize, new_dbuf.dptr, new_dbuf.dsize);
645 dbuf.dsize += new_dbuf.dsize;
647 ret = tdb_store(tdb, key, dbuf, 0);
650 tdb_unlock(tdb, BUCKET(hash), F_WRLCK);
651 SAFE_FREE(dbuf.dptr);
657 return the name of the current tdb file
658 useful for external logging functions
660 const char *tdb_name(struct tdb_context *tdb)
666 return the underlying file descriptor being used by tdb, or -1
667 useful for external routines that want to check the device/inode
670 int tdb_fd(struct tdb_context *tdb)
676 return the current logging function
677 useful for external tdb routines that wish to log tdb errors
679 tdb_log_func tdb_log_fn(struct tdb_context *tdb)
681 return tdb->log.log_fn;
686 get the tdb sequence number. Only makes sense if the writers opened
687 with TDB_SEQNUM set. Note that this sequence number will wrap quite
688 quickly, so it should only be used for a 'has something changed'
689 test, not for code that relies on the count of the number of changes
690 made. If you want a counter then use a tdb record.
692 The aim of this sequence number is to allow for a very lightweight
693 test of a possible tdb change.
695 int tdb_get_seqnum(struct tdb_context *tdb)
699 tdb_ofs_read(tdb, TDB_SEQNUM_OFS, &seqnum);
700 tdb_trace(tdb, "tdb_get_seqnum = %u\n", seqnum);
704 int tdb_hash_size(struct tdb_context *tdb)
706 return tdb->header.hash_size;
709 size_t tdb_map_size(struct tdb_context *tdb)
711 return tdb->map_size;
714 int tdb_get_flags(struct tdb_context *tdb)
719 void tdb_add_flags(struct tdb_context *tdb, unsigned flags)
724 void tdb_remove_flags(struct tdb_context *tdb, unsigned flags)
726 tdb->flags &= ~flags;
731 enable sequence number handling on an open tdb
733 void tdb_enable_seqnum(struct tdb_context *tdb)
735 tdb->flags |= TDB_SEQNUM;
740 add a region of the file to the freelist. Length is the size of the region in bytes,
741 which includes the free list header that needs to be added
743 static int tdb_free_region(struct tdb_context *tdb, tdb_off_t offset, ssize_t length)
745 struct list_struct rec;
746 if (length <= sizeof(rec)) {
747 /* the region is not worth adding */
750 if (length + offset > tdb->map_size) {
751 TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_free_region: adding region beyond end of file\n"));
754 memset(&rec,'\0',sizeof(rec));
755 rec.rec_len = length - sizeof(rec);
756 if (tdb_free(tdb, offset, &rec) == -1) {
757 TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_free_region: failed to add free record\n"));
764 wipe the entire database, deleting all records. This can be done
765 very fast by using a global lock. The entire data portion of the
766 file becomes a single entry in the freelist.
768 This code carefully steps around the recovery area, leaving it alone
770 int tdb_wipe_all(struct tdb_context *tdb)
773 tdb_off_t offset = 0;
775 tdb_off_t recovery_head;
776 tdb_len_t recovery_size = 0;
778 if (tdb_lockall(tdb) != 0) {
782 tdb_trace(tdb, "tdb_wipe_all\n");
784 /* see if the tdb has a recovery area, and remember its size
785 if so. We don't want to lose this as otherwise each
786 tdb_wipe_all() in a transaction will increase the size of
787 the tdb by the size of the recovery area */
788 if (tdb_ofs_read(tdb, TDB_RECOVERY_HEAD, &recovery_head) == -1) {
789 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_wipe_all: failed to read recovery head\n"));
793 if (recovery_head != 0) {
794 struct list_struct rec;
795 if (tdb->methods->tdb_read(tdb, recovery_head, &rec, sizeof(rec), DOCONV()) == -1) {
796 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_wipe_all: failed to read recovery record\n"));
799 recovery_size = rec.rec_len + sizeof(rec);
802 /* wipe the hashes */
803 for (i=0;i<tdb->header.hash_size;i++) {
804 if (tdb_ofs_write(tdb, TDB_HASH_TOP(i), &offset) == -1) {
805 TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_wipe_all: failed to write hash %d\n", i));
810 /* wipe the freelist */
811 if (tdb_ofs_write(tdb, FREELIST_TOP, &offset) == -1) {
812 TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_wipe_all: failed to write freelist\n"));
816 /* add all the rest of the file to the freelist, possibly leaving a gap
817 for the recovery area */
818 if (recovery_size == 0) {
819 /* the simple case - the whole file can be used as a freelist */
820 data_len = (tdb->map_size - TDB_DATA_START(tdb->header.hash_size));
821 if (tdb_free_region(tdb, TDB_DATA_START(tdb->header.hash_size), data_len) != 0) {
825 /* we need to add two freelist entries - one on either
826 side of the recovery area
828 Note that we cannot shift the recovery area during
829 this operation. Only the transaction.c code may
830 move the recovery area or we risk subtle data
833 data_len = (recovery_head - TDB_DATA_START(tdb->header.hash_size));
834 if (tdb_free_region(tdb, TDB_DATA_START(tdb->header.hash_size), data_len) != 0) {
837 /* and the 2nd free list entry after the recovery area - if any */
838 data_len = tdb->map_size - (recovery_head+recovery_size);
839 if (tdb_free_region(tdb, recovery_head+recovery_size, data_len) != 0) {
844 if (tdb_unlockall(tdb) != 0) {
845 TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_wipe_all: failed to unlock\n"));
859 void tdb_trace(const struct tdb_context *tdb, const char *fmt, ...)
866 len = vsprintf(msg, fmt, args);
869 err = write(tdb->tracefd, msg, len);
872 void tdb_trace_record(const struct tdb_context *tdb, TDB_DATA rec)
878 err = write(tdb->tracefd, msg, sprintf(msg, "%zu:", rec.dsize));
879 for (i = 0; i < rec.dsize; i++)
880 err += write(tdb->tracefd, msg, sprintf(msg, "%02x",
882 err += write(tdb->tracefd, " ", 1);