]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/tdb.c
tdb2: direct access during transactions.
[ccan] / ccan / tdb2 / tdb.c
1 #include "private.h"
2 #include <ccan/tdb2/tdb2.h>
3 #include <assert.h>
4 #include <stdarg.h>
5
6 /* The null return. */
7 struct tdb_data tdb_null = { .dptr = NULL, .dsize = 0 };
8
9 /* all contexts, to ensure no double-opens (fcntl locks don't nest!) */
10 static struct tdb_context *tdbs = NULL;
11
12 static bool tdb_already_open(dev_t device, ino_t ino)
13 {
14         struct tdb_context *i;
15         
16         for (i = tdbs; i; i = i->next) {
17                 if (i->device == device && i->inode == ino) {
18                         return true;
19                 }
20         }
21
22         return false;
23 }
24
25 static uint64_t random_number(struct tdb_context *tdb)
26 {
27         int fd;
28         uint64_t ret = 0;
29         struct timeval now;
30
31         fd = open("/dev/urandom", O_RDONLY);
32         if (fd >= 0) {
33                 if (tdb_read_all(fd, &ret, sizeof(ret))) {
34                         tdb_logerr(tdb, TDB_SUCCESS, TDB_DEBUG_TRACE,
35                                  "tdb_open: random from /dev/urandom");
36                         close(fd);
37                         return ret;
38                 }
39                 close(fd);
40         }
41         /* FIXME: Untested!  Based on Wikipedia protocol description! */
42         fd = open("/dev/egd-pool", O_RDWR);
43         if (fd >= 0) {
44                 /* Command is 1, next byte is size we want to read. */
45                 char cmd[2] = { 1, sizeof(uint64_t) };
46                 if (write(fd, cmd, sizeof(cmd)) == sizeof(cmd)) {
47                         char reply[1 + sizeof(uint64_t)];
48                         int r = read(fd, reply, sizeof(reply));
49                         if (r > 1) {
50                                 tdb_logerr(tdb, TDB_SUCCESS, TDB_DEBUG_TRACE,
51                                            "tdb_open: %u random bytes from"
52                                            " /dev/egd-pool", r-1);
53                                 /* Copy at least some bytes. */
54                                 memcpy(&ret, reply+1, r - 1);
55                                 if (reply[0] == sizeof(uint64_t)
56                                     && r == sizeof(reply)) {
57                                         close(fd);
58                                         return ret;
59                                 }
60                         }
61                 }
62                 close(fd);
63         }
64
65         /* Fallback: pid and time. */
66         gettimeofday(&now, NULL);
67         ret = getpid() * 100132289ULL + now.tv_sec * 1000000ULL + now.tv_usec;
68         tdb_logerr(tdb, TDB_SUCCESS, TDB_DEBUG_TRACE,
69                    "tdb_open: random from getpid and time");
70         return ret;
71 }
72
73 struct new_database {
74         struct tdb_header hdr;
75         struct tdb_freelist flist;
76 };
77
78 /* initialise a new database */
79 static int tdb_new_database(struct tdb_context *tdb,
80                             struct tdb_attribute_seed *seed,
81                             struct tdb_header *hdr)
82 {
83         /* We make it up in memory, then write it out if not internal */
84         struct new_database newdb;
85         unsigned int magic_len;
86
87         /* Fill in the header */
88         newdb.hdr.version = TDB_VERSION;
89         if (seed)
90                 newdb.hdr.hash_seed = seed->seed;
91         else
92                 newdb.hdr.hash_seed = random_number(tdb);
93         newdb.hdr.hash_test = TDB_HASH_MAGIC;
94         newdb.hdr.hash_test = tdb->khash(&newdb.hdr.hash_test,
95                                          sizeof(newdb.hdr.hash_test),
96                                          newdb.hdr.hash_seed,
97                                          tdb->hash_priv);
98         newdb.hdr.recovery = 0;
99         memset(newdb.hdr.reserved, 0, sizeof(newdb.hdr.reserved));
100         /* Initial hashes are empty. */
101         memset(newdb.hdr.hashtable, 0, sizeof(newdb.hdr.hashtable));
102
103         /* Free is empty. */
104         newdb.hdr.free_list = offsetof(struct new_database, flist);
105         memset(&newdb.flist, 0, sizeof(newdb.flist));
106         set_used_header(NULL, &newdb.flist.hdr, 0,
107                         sizeof(newdb.flist) - sizeof(newdb.flist.hdr),
108                         sizeof(newdb.flist) - sizeof(newdb.flist.hdr), 1);
109
110         /* Magic food */
111         memset(newdb.hdr.magic_food, 0, sizeof(newdb.hdr.magic_food));
112         strcpy(newdb.hdr.magic_food, TDB_MAGIC_FOOD);
113
114         /* This creates an endian-converted database, as if read from disk */
115         magic_len = sizeof(newdb.hdr.magic_food);
116         tdb_convert(tdb,
117                     (char *)&newdb.hdr + magic_len, sizeof(newdb) - magic_len);
118
119         *hdr = newdb.hdr;
120
121         if (tdb->flags & TDB_INTERNAL) {
122                 tdb->map_size = sizeof(newdb);
123                 tdb->map_ptr = malloc(tdb->map_size);
124                 if (!tdb->map_ptr) {
125                         tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_FATAL,
126                                    "tdb_new_database: failed to allocate");
127                         return -1;
128                 }
129                 memcpy(tdb->map_ptr, &newdb, tdb->map_size);
130                 return 0;
131         }
132         if (lseek(tdb->fd, 0, SEEK_SET) == -1)
133                 return -1;
134
135         if (ftruncate(tdb->fd, 0) == -1)
136                 return -1;
137
138         if (!tdb_pwrite_all(tdb->fd, &newdb, sizeof(newdb), 0)) {
139                 tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL,
140                            "tdb_new_database: failed to write: %s",
141                            strerror(errno));
142                 return -1;
143         }
144         return 0;
145 }
146
147 struct tdb_context *tdb_open(const char *name, int tdb_flags,
148                              int open_flags, mode_t mode,
149                              union tdb_attribute *attr)
150 {
151         struct tdb_context *tdb;
152         struct stat st;
153         int saved_errno = 0;
154         uint64_t hash_test;
155         unsigned v;
156         struct tdb_header hdr;
157         struct tdb_attribute_seed *seed = NULL;
158
159         tdb = malloc(sizeof(*tdb));
160         if (!tdb) {
161                 /* Can't log this */
162                 errno = ENOMEM;
163                 return NULL;
164         }
165         tdb->name = NULL;
166         tdb->map_ptr = NULL;
167         tdb->direct_access = 0;
168         tdb->fd = -1;
169         tdb->map_size = sizeof(struct tdb_header);
170         tdb->ecode = TDB_SUCCESS;
171         tdb->flags = tdb_flags;
172         tdb->logfn = NULL;
173         tdb->transaction = NULL;
174         tdb->stats = NULL;
175         tdb->access = NULL;
176         tdb_hash_init(tdb);
177         tdb_io_init(tdb);
178         tdb_lock_init(tdb);
179
180         while (attr) {
181                 switch (attr->base.attr) {
182                 case TDB_ATTRIBUTE_LOG:
183                         tdb->logfn = attr->log.log_fn;
184                         tdb->log_private = attr->log.log_private;
185                         break;
186                 case TDB_ATTRIBUTE_HASH:
187                         tdb->khash = attr->hash.hash_fn;
188                         tdb->hash_priv = attr->hash.hash_private;
189                         break;
190                 case TDB_ATTRIBUTE_SEED:
191                         seed = &attr->seed;
192                         break;
193                 case TDB_ATTRIBUTE_STATS:
194                         tdb->stats = &attr->stats;
195                         /* They have stats we don't know about?  Tell them. */
196                         if (tdb->stats->size > sizeof(attr->stats))
197                                 tdb->stats->size = sizeof(attr->stats);
198                         break;
199                 default:
200                         tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_DEBUG_ERROR,
201                                    "tdb_open: unknown attribute type %u",
202                                    attr->base.attr);
203                         goto fail;
204                 }
205                 attr = attr->base.next;
206         }
207
208         if ((open_flags & O_ACCMODE) == O_WRONLY) {
209                 tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_DEBUG_ERROR,
210                            "tdb_open: can't open tdb %s write-only", name);
211                 goto fail;
212         }
213
214         if ((open_flags & O_ACCMODE) == O_RDONLY) {
215                 tdb->read_only = true;
216                 /* read only databases don't do locking */
217                 tdb->flags |= TDB_NOLOCK;
218                 tdb->mmap_flags = PROT_READ;
219         } else {
220                 tdb->read_only = false;
221                 tdb->mmap_flags = PROT_READ | PROT_WRITE;
222         }
223
224         /* internal databases don't need any of the rest. */
225         if (tdb->flags & TDB_INTERNAL) {
226                 tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
227                 if (tdb_new_database(tdb, seed, &hdr) != 0) {
228                         goto fail;
229                 }
230                 tdb_convert(tdb, &hdr.hash_seed, sizeof(hdr.hash_seed));
231                 tdb->hash_seed = hdr.hash_seed;
232                 tdb_flist_init(tdb);
233                 return tdb;
234         }
235
236         if ((tdb->fd = open(name, open_flags, mode)) == -1) {
237                 /* errno set by open(2) */
238                 saved_errno = errno;
239                 tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR,
240                            "tdb_open: could not open file %s: %s",
241                            name, strerror(errno));
242                 goto fail;
243         }
244
245         /* on exec, don't inherit the fd */
246         v = fcntl(tdb->fd, F_GETFD, 0);
247         fcntl(tdb->fd, F_SETFD, v | FD_CLOEXEC);
248
249         /* ensure there is only one process initialising at once */
250         if (tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK) == -1) {
251                 /* errno set by tdb_brlock */
252                 saved_errno = errno;
253                 goto fail;
254         }
255
256         if (!tdb_pread_all(tdb->fd, &hdr, sizeof(hdr), 0)
257             || strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) {
258                 if (!(open_flags & O_CREAT)) {
259                         tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR,
260                                    "tdb_open: %s is not a tdb file", name);
261                         goto fail;
262                 }
263                 if (tdb_new_database(tdb, seed, &hdr) == -1) {
264                         goto fail;
265                 }
266         } else if (hdr.version != TDB_VERSION) {
267                 if (hdr.version == bswap_64(TDB_VERSION))
268                         tdb->flags |= TDB_CONVERT;
269                 else {
270                         /* wrong version */
271                         tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR,
272                                    "tdb_open: %s is unknown version 0x%llx",
273                                    name, (long long)hdr.version);
274                         goto fail;
275                 }
276         }
277
278         tdb_convert(tdb, &hdr, sizeof(hdr));
279         tdb->hash_seed = hdr.hash_seed;
280         hash_test = TDB_HASH_MAGIC;
281         hash_test = tdb_hash(tdb, &hash_test, sizeof(hash_test));
282         if (hdr.hash_test != hash_test) {
283                 /* wrong hash variant */
284                 tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR,
285                            "tdb_open: %s uses a different hash function",
286                            name);
287                 goto fail;
288         }
289
290         if (fstat(tdb->fd, &st) == -1) {
291                 saved_errno = errno;
292                 tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR,
293                            "tdb_open: could not stat open %s: %s",
294                            name, strerror(errno));
295                 goto fail;
296         }
297
298         /* Is it already in the open list?  If so, fail. */
299         if (tdb_already_open(st.st_dev, st.st_ino)) {
300                 /* FIXME */
301                 tdb_logerr(tdb, TDB_ERR_NESTING, TDB_DEBUG_ERROR,
302                            "tdb_open: %s (%d,%d) is already open in this"
303                            " process",
304                            name, (int)st.st_dev, (int)st.st_ino);
305                 goto fail;
306         }
307
308         tdb->name = strdup(name);
309         if (!tdb->name) {
310                 tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_ERROR,
311                            "tdb_open: failed to allocate name");
312                 goto fail;
313         }
314
315         tdb->device = st.st_dev;
316         tdb->inode = st.st_ino;
317         tdb_unlock_open(tdb);
318
319         /* This make sure we have current map_size and mmap. */
320         tdb->methods->oob(tdb, tdb->map_size + 1, true);
321
322         /* Now it's fully formed, recover if necessary. */
323         if (tdb_needs_recovery(tdb) && tdb_lock_and_recover(tdb) == -1) {
324                 goto fail;
325         }
326
327         if (tdb_flist_init(tdb) == -1)
328                 goto fail;
329
330         tdb->next = tdbs;
331         tdbs = tdb;
332         return tdb;
333
334  fail:
335         /* Map ecode to some logical errno. */
336         if (!saved_errno) {
337                 switch (tdb->ecode) {
338                 case TDB_ERR_CORRUPT:
339                 case TDB_ERR_IO:
340                         saved_errno = EIO;
341                         break;
342                 case TDB_ERR_LOCK:
343                         saved_errno = EWOULDBLOCK;
344                         break;
345                 case TDB_ERR_OOM:
346                         saved_errno = ENOMEM;
347                         break;
348                 case TDB_ERR_EINVAL:
349                         saved_errno = EINVAL;
350                         break;
351                 case TDB_ERR_NESTING:
352                         saved_errno = EBUSY;
353                         break;
354                 default:
355                         saved_errno = EINVAL;
356                         break;
357                 }
358         }
359
360 #ifdef TDB_TRACE
361         close(tdb->tracefd);
362 #endif
363         if (tdb->map_ptr) {
364                 if (tdb->flags & TDB_INTERNAL) {
365                         free(tdb->map_ptr);
366                 } else
367                         tdb_munmap(tdb);
368         }
369         free((char *)tdb->name);
370         if (tdb->fd != -1)
371                 if (close(tdb->fd) != 0)
372                         tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR,
373                                    "tdb_open: failed to close tdb->fd"
374                                    " on error!");
375         free(tdb);
376         errno = saved_errno;
377         return NULL;
378 }
379
380 static int update_rec_hdr(struct tdb_context *tdb,
381                           tdb_off_t off,
382                           tdb_len_t keylen,
383                           tdb_len_t datalen,
384                           struct tdb_used_record *rec,
385                           uint64_t h)
386 {
387         uint64_t dataroom = rec_data_length(rec) + rec_extra_padding(rec);
388
389         if (set_used_header(tdb, rec, keylen, datalen, keylen + dataroom, h))
390                 return -1;
391
392         return tdb_write_convert(tdb, off, rec, sizeof(*rec));
393 }
394
395 /* Returns -1 on error, 0 on OK */
396 static int replace_data(struct tdb_context *tdb,
397                         struct hash_info *h,
398                         struct tdb_data key, struct tdb_data dbuf,
399                         tdb_off_t old_off, tdb_len_t old_room,
400                         bool growing)
401 {
402         tdb_off_t new_off;
403
404         /* Allocate a new record. */
405         new_off = alloc(tdb, key.dsize, dbuf.dsize, h->h, growing);
406         if (unlikely(new_off == TDB_OFF_ERR))
407                 return -1;
408
409         /* We didn't like the existing one: remove it. */
410         if (old_off) {
411                 add_stat(tdb, frees, 1);
412                 add_free_record(tdb, old_off,
413                                 sizeof(struct tdb_used_record)
414                                 + key.dsize + old_room);
415                 if (replace_in_hash(tdb, h, new_off) == -1)
416                         return -1;
417         } else {
418                 if (add_to_hash(tdb, h, new_off) == -1)
419                         return -1;
420         }
421
422         new_off += sizeof(struct tdb_used_record);
423         if (tdb->methods->write(tdb, new_off, key.dptr, key.dsize) == -1)
424                 return -1;
425
426         new_off += key.dsize;
427         if (tdb->methods->write(tdb, new_off, dbuf.dptr, dbuf.dsize) == -1)
428                 return -1;
429
430         /* FIXME: tdb_increment_seqnum(tdb); */
431         return 0;
432 }
433
434 int tdb_store(struct tdb_context *tdb,
435               struct tdb_data key, struct tdb_data dbuf, int flag)
436 {
437         struct hash_info h;
438         tdb_off_t off;
439         tdb_len_t old_room = 0;
440         struct tdb_used_record rec;
441         int ret;
442
443         off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
444         if (unlikely(off == TDB_OFF_ERR))
445                 return -1;
446
447         /* Now we have lock on this hash bucket. */
448         if (flag == TDB_INSERT) {
449                 if (off) {
450                         tdb->ecode = TDB_ERR_EXISTS;
451                         goto fail;
452                 }
453         } else {
454                 if (off) {
455                         old_room = rec_data_length(&rec)
456                                 + rec_extra_padding(&rec);
457                         if (old_room >= dbuf.dsize) {
458                                 /* Can modify in-place.  Easy! */
459                                 if (update_rec_hdr(tdb, off,
460                                                    key.dsize, dbuf.dsize,
461                                                    &rec, h.h))
462                                         goto fail;
463                                 if (tdb->methods->write(tdb, off + sizeof(rec)
464                                                         + key.dsize,
465                                                         dbuf.dptr, dbuf.dsize))
466                                         goto fail;
467                                 tdb_unlock_hashes(tdb, h.hlock_start,
468                                                   h.hlock_range, F_WRLCK);
469                                 return 0;
470                         }
471                 } else {
472                         if (flag == TDB_MODIFY) {
473                                 /* if the record doesn't exist and we
474                                    are in TDB_MODIFY mode then we should fail
475                                    the store */
476                                 tdb->ecode = TDB_ERR_NOEXIST;
477                                 goto fail;
478                         }
479                 }
480         }
481
482         /* If we didn't use the old record, this implies we're growing. */
483         ret = replace_data(tdb, &h, key, dbuf, off, old_room, off != 0);
484         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
485         return ret;
486
487 fail:
488         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
489         return -1;
490 }
491
492 int tdb_append(struct tdb_context *tdb,
493                struct tdb_data key, struct tdb_data dbuf)
494 {
495         struct hash_info h;
496         tdb_off_t off;
497         struct tdb_used_record rec;
498         tdb_len_t old_room = 0, old_dlen;
499         unsigned char *newdata;
500         struct tdb_data new_dbuf;
501         int ret;
502
503         off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
504         if (unlikely(off == TDB_OFF_ERR))
505                 return -1;
506
507         if (off) {
508                 old_dlen = rec_data_length(&rec);
509                 old_room = old_dlen + rec_extra_padding(&rec);
510
511                 /* Fast path: can append in place. */
512                 if (rec_extra_padding(&rec) >= dbuf.dsize) {
513                         if (update_rec_hdr(tdb, off, key.dsize,
514                                            old_dlen + dbuf.dsize, &rec, h.h))
515                                 goto fail;
516
517                         off += sizeof(rec) + key.dsize + old_dlen;
518                         if (tdb->methods->write(tdb, off, dbuf.dptr,
519                                                 dbuf.dsize) == -1)
520                                 goto fail;
521
522                         /* FIXME: tdb_increment_seqnum(tdb); */
523                         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range,
524                                           F_WRLCK);
525                         return 0;
526                 }
527
528                 /* Slow path. */
529                 newdata = malloc(key.dsize + old_dlen + dbuf.dsize);
530                 if (!newdata) {
531                         tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_FATAL,
532                                    "tdb_append: failed to allocate %zu bytes",
533                                    (size_t)(key.dsize+old_dlen+dbuf.dsize));
534                         goto fail;
535                 }
536                 if (tdb->methods->read(tdb, off + sizeof(rec) + key.dsize,
537                                        newdata, old_dlen) != 0) {
538                         free(newdata);
539                         goto fail;
540                 }
541                 memcpy(newdata + old_dlen, dbuf.dptr, dbuf.dsize);
542                 new_dbuf.dptr = newdata;
543                 new_dbuf.dsize = old_dlen + dbuf.dsize;
544         } else {
545                 newdata = NULL;
546                 new_dbuf = dbuf;
547         }
548
549         /* If they're using tdb_append(), it implies they're growing record. */
550         ret = replace_data(tdb, &h, key, new_dbuf, off, old_room, true);
551         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
552         free(newdata);
553
554         return ret;
555
556 fail:
557         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
558         return -1;
559 }
560
561 struct tdb_data tdb_fetch(struct tdb_context *tdb, struct tdb_data key)
562 {
563         tdb_off_t off;
564         struct tdb_used_record rec;
565         struct hash_info h;
566         struct tdb_data ret;
567
568         off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
569         if (unlikely(off == TDB_OFF_ERR))
570                 return tdb_null;
571
572         if (!off) {
573                 tdb->ecode = TDB_ERR_NOEXIST;
574                 ret = tdb_null;
575         } else {
576                 ret.dsize = rec_data_length(&rec);
577                 ret.dptr = tdb_alloc_read(tdb, off + sizeof(rec) + key.dsize,
578                                           ret.dsize);
579         }
580
581         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
582         return ret;
583 }
584
585 int tdb_delete(struct tdb_context *tdb, struct tdb_data key)
586 {
587         tdb_off_t off;
588         struct tdb_used_record rec;
589         struct hash_info h;
590
591         off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
592         if (unlikely(off == TDB_OFF_ERR))
593                 return -1;
594
595         if (!off) {
596                 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
597                 tdb->ecode = TDB_ERR_NOEXIST;
598                 return -1;
599         }
600
601         if (delete_from_hash(tdb, &h) == -1)
602                 goto unlock_err;
603
604         /* Free the deleted entry. */
605         add_stat(tdb, frees, 1);
606         if (add_free_record(tdb, off,
607                             sizeof(struct tdb_used_record)
608                             + rec_key_length(&rec)
609                             + rec_data_length(&rec)
610                             + rec_extra_padding(&rec)) != 0)
611                 goto unlock_err;
612
613         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
614         return 0;
615
616 unlock_err:
617         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
618         return -1;
619 }
620
621 int tdb_close(struct tdb_context *tdb)
622 {
623         struct tdb_context **i;
624         int ret = 0;
625
626         tdb_trace(tdb, "tdb_close");
627
628         if (tdb->transaction) {
629                 tdb_transaction_cancel(tdb);
630         }
631
632         if (tdb->map_ptr) {
633                 if (tdb->flags & TDB_INTERNAL)
634                         free(tdb->map_ptr);
635                 else
636                         tdb_munmap(tdb);
637         }
638         free((char *)tdb->name);
639         if (tdb->fd != -1) {
640                 ret = close(tdb->fd);
641                 tdb->fd = -1;
642         }
643         free(tdb->lockrecs);
644
645         /* Remove from contexts list */
646         for (i = &tdbs; *i; i = &(*i)->next) {
647                 if (*i == tdb) {
648                         *i = tdb->next;
649                         break;
650                 }
651         }
652
653 #ifdef TDB_TRACE
654         close(tdb->tracefd);
655 #endif
656         free(tdb);
657
658         return ret;
659 }
660
661 enum TDB_ERROR tdb_error(const struct tdb_context *tdb)
662 {
663         return tdb->ecode;
664 }
665
666 const char *tdb_errorstr(const struct tdb_context *tdb)
667 {
668         /* Gcc warns if you miss a case in the switch, so use that. */
669         switch (tdb->ecode) {
670         case TDB_SUCCESS: return "Success";
671         case TDB_ERR_CORRUPT: return "Corrupt database";
672         case TDB_ERR_IO: return "IO Error";
673         case TDB_ERR_LOCK: return "Locking error";
674         case TDB_ERR_OOM: return "Out of memory";
675         case TDB_ERR_EXISTS: return "Record exists";
676         case TDB_ERR_NESTING: return "Transaction already started";
677         case TDB_ERR_EINVAL: return "Invalid parameter";
678         case TDB_ERR_NOEXIST: return "Record does not exist";
679         case TDB_ERR_RDONLY: return "write not permitted";
680         }
681         return "Invalid error code";
682 }
683
684 void COLD tdb_logerr(struct tdb_context *tdb,
685                      enum TDB_ERROR ecode,
686                      enum tdb_debug_level level,
687                      const char *fmt, ...)
688 {
689         char *message;
690         va_list ap;
691         size_t len;
692         /* tdb_open paths care about errno, so save it. */
693         int saved_errno = errno;
694
695         tdb->ecode = ecode;
696
697         if (!tdb->logfn)
698                 return;
699
700         /* FIXME: Doesn't assume asprintf. */
701         va_start(ap, fmt);
702         len = vsnprintf(NULL, 0, fmt, ap);
703         va_end(ap);
704
705         message = malloc(len + 1);
706         if (!message) {
707                 tdb->logfn(tdb, level, tdb->log_private,
708                            "out of memory formatting message");
709                 return;
710         }
711         va_start(ap, fmt);
712         len = vsprintf(message, fmt, ap);
713         va_end(ap);
714         tdb->logfn(tdb, level, tdb->log_private, message);
715         free(message);
716         errno = saved_errno;
717 }