]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb2/tdb.c
tdb2: fix leak on lock failure during open.
[ccan] / ccan / tdb2 / tdb.c
index a4dbc53d6f237050fc8e9ae5310138a3e8e87913..def2642e5560b84d059cb14e8c6542adc5e76585 100644 (file)
@@ -22,6 +22,24 @@ static bool tdb_already_open(dev_t device, ino_t ino)
        return false;
 }
 
+static bool read_all(int fd, void *buf, size_t len)
+{
+       while (len) {
+               ssize_t ret;
+               ret = read(fd, buf, len);
+               if (ret < 0)
+                       return false;
+               if (ret == 0) {
+                       /* ETOOSHORT? */
+                       errno = EWOULDBLOCK;
+                       return false;
+               }
+               buf = (char *)buf + ret;
+               len -= ret;
+       }
+       return true;
+}
+
 static uint64_t random_number(struct tdb_context *tdb)
 {
        int fd;
@@ -30,7 +48,7 @@ static uint64_t random_number(struct tdb_context *tdb)
 
        fd = open("/dev/urandom", O_RDONLY);
        if (fd >= 0) {
-               if (tdb_read_all(fd, &ret, sizeof(ret))) {
+               if (read_all(fd, &ret, sizeof(ret))) {
                        tdb_logerr(tdb, TDB_SUCCESS, TDB_DEBUG_TRACE,
                                 "tdb_open: random from /dev/urandom");
                        close(fd);
@@ -72,7 +90,7 @@ static uint64_t random_number(struct tdb_context *tdb)
 
 struct new_database {
        struct tdb_header hdr;
-       struct tdb_freelist flist;
+       struct tdb_freetable ftable;
 };
 
 /* initialise a new database */
@@ -83,6 +101,7 @@ static int tdb_new_database(struct tdb_context *tdb,
        /* We make it up in memory, then write it out if not internal */
        struct new_database newdb;
        unsigned int magic_len;
+       ssize_t rlen;
 
        /* Fill in the header */
        newdb.hdr.version = TDB_VERSION;
@@ -101,11 +120,11 @@ static int tdb_new_database(struct tdb_context *tdb,
        memset(newdb.hdr.hashtable, 0, sizeof(newdb.hdr.hashtable));
 
        /* Free is empty. */
-       newdb.hdr.free_list = offsetof(struct new_database, flist);
-       memset(&newdb.flist, 0, sizeof(newdb.flist));
-       set_used_header(NULL, &newdb.flist.hdr, 0,
-                       sizeof(newdb.flist) - sizeof(newdb.flist.hdr),
-                       sizeof(newdb.flist) - sizeof(newdb.flist.hdr), 1);
+       newdb.hdr.free_table = offsetof(struct new_database, ftable);
+       memset(&newdb.ftable, 0, sizeof(newdb.ftable));
+       set_header(NULL, &newdb.ftable.hdr, TDB_FTABLE_MAGIC, 0,
+                  sizeof(newdb.ftable) - sizeof(newdb.ftable.hdr),
+                  sizeof(newdb.ftable) - sizeof(newdb.ftable.hdr), 0);
 
        /* Magic food */
        memset(newdb.hdr.magic_food, 0, sizeof(newdb.hdr.magic_food));
@@ -135,10 +154,13 @@ static int tdb_new_database(struct tdb_context *tdb,
        if (ftruncate(tdb->fd, 0) == -1)
                return -1;
 
-       if (!tdb_pwrite_all(tdb->fd, &newdb, sizeof(newdb), 0)) {
+       rlen = write(tdb->fd, &newdb, sizeof(newdb));
+       if (rlen != sizeof(newdb)) {
+               if (rlen >= 0)
+                       errno = ENOSPC;
                tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL,
-                          "tdb_new_database: failed to write: %s",
-                          strerror(errno));
+                          "tdb_new_database: %zi writing header: %s",
+                          rlen, strerror(errno));
                return -1;
        }
        return 0;
@@ -153,6 +175,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
        int saved_errno = 0;
        uint64_t hash_test;
        unsigned v;
+       ssize_t rlen;
        struct tdb_header hdr;
        struct tdb_attribute_seed *seed = NULL;
 
@@ -172,6 +195,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
        tdb->logfn = NULL;
        tdb->transaction = NULL;
        tdb->stats = NULL;
+       tdb->access = NULL;
        tdb_hash_init(tdb);
        tdb_io_init(tdb);
        tdb_lock_init(tdb);
@@ -228,7 +252,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
                }
                tdb_convert(tdb, &hdr.hash_seed, sizeof(hdr.hash_seed));
                tdb->hash_seed = hdr.hash_seed;
-               tdb_flist_init(tdb);
+               tdb_ftable_init(tdb);
                return tdb;
        }
 
@@ -252,17 +276,25 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
                goto fail;
        }
 
-       if (!tdb_pread_all(tdb->fd, &hdr, sizeof(hdr), 0)
-           || strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) {
-               if (!(open_flags & O_CREAT)) {
-                       tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR,
-                                  "tdb_open: %s is not a tdb file", name);
-                       goto fail;
-               }
+       /* If they used O_TRUNC, read will return 0. */
+       rlen = read(tdb->fd, &hdr, sizeof(hdr));
+       if (rlen == 0 && (open_flags & O_CREAT)) {
                if (tdb_new_database(tdb, seed, &hdr) == -1) {
                        goto fail;
                }
-       } else if (hdr.version != TDB_VERSION) {
+       } else if (rlen < 0) {
+               tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR,
+                          "tdb_open: error %s reading %s",
+                          strerror(errno), name);
+               goto fail;
+       } else if (rlen < sizeof(hdr)
+                  || strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) {
+               tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR,
+                          "tdb_open: %s is not a tdb file", name);
+               goto fail;
+       }
+
+       if (hdr.version != TDB_VERSION) {
                if (hdr.version == bswap_64(TDB_VERSION))
                        tdb->flags |= TDB_CONVERT;
                else {
@@ -323,7 +355,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
                goto fail;
        }
 
-       if (tdb_flist_init(tdb) == -1)
+       if (tdb_ftable_init(tdb) == -1)
                goto fail;
 
        tdb->next = tdbs;
@@ -365,6 +397,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
                } else
                        tdb_munmap(tdb);
        }
+       free(tdb->lockrecs);
        free((char *)tdb->name);
        if (tdb->fd != -1)
                if (close(tdb->fd) != 0)
@@ -376,7 +409,6 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
        return NULL;
 }
 
-/* FIXME: modify, don't rewrite! */
 static int update_rec_hdr(struct tdb_context *tdb,
                          tdb_off_t off,
                          tdb_len_t keylen,
@@ -386,7 +418,8 @@ static int update_rec_hdr(struct tdb_context *tdb,
 {
        uint64_t dataroom = rec_data_length(rec) + rec_extra_padding(rec);
 
-       if (set_used_header(tdb, rec, keylen, datalen, keylen + dataroom, h))
+       if (set_header(tdb, rec, TDB_USED_MAGIC, keylen, datalen,
+                      keylen + dataroom, h))
                return -1;
 
        return tdb_write_convert(tdb, off, rec, sizeof(*rec));
@@ -402,7 +435,8 @@ static int replace_data(struct tdb_context *tdb,
        tdb_off_t new_off;
 
        /* Allocate a new record. */
-       new_off = alloc(tdb, key.dsize, dbuf.dsize, h->h, growing);
+       new_off = alloc(tdb, key.dsize, dbuf.dsize, h->h, TDB_USED_MAGIC,
+                       growing);
        if (unlikely(new_off == TDB_OFF_ERR))
                return -1;
 
@@ -420,11 +454,11 @@ static int replace_data(struct tdb_context *tdb,
        }
 
        new_off += sizeof(struct tdb_used_record);
-       if (tdb->methods->write(tdb, new_off, key.dptr, key.dsize) == -1)
+       if (tdb->methods->twrite(tdb, new_off, key.dptr, key.dsize) == -1)
                return -1;
 
        new_off += key.dsize;
-       if (tdb->methods->write(tdb, new_off, dbuf.dptr, dbuf.dsize) == -1)
+       if (tdb->methods->twrite(tdb, new_off, dbuf.dptr, dbuf.dsize) == -1)
                return -1;
 
        /* FIXME: tdb_increment_seqnum(tdb); */
@@ -460,15 +494,14 @@ int tdb_store(struct tdb_context *tdb,
                                                   key.dsize, dbuf.dsize,
                                                   &rec, h.h))
                                        goto fail;
-                               if (tdb->methods->write(tdb, off + sizeof(rec)
-                                                       + key.dsize,
-                                                       dbuf.dptr, dbuf.dsize))
+                               if (tdb->methods->twrite(tdb, off + sizeof(rec)
+                                                        + key.dsize,
+                                                        dbuf.dptr, dbuf.dsize))
                                        goto fail;
                                tdb_unlock_hashes(tdb, h.hlock_start,
                                                  h.hlock_range, F_WRLCK);
                                return 0;
                        }
-                       /* FIXME: See if right record is free? */
                } else {
                        if (flag == TDB_MODIFY) {
                                /* if the record doesn't exist and we
@@ -516,8 +549,8 @@ int tdb_append(struct tdb_context *tdb,
                                goto fail;
 
                        off += sizeof(rec) + key.dsize + old_dlen;
-                       if (tdb->methods->write(tdb, off, dbuf.dptr,
-                                               dbuf.dsize) == -1)
+                       if (tdb->methods->twrite(tdb, off, dbuf.dptr,
+                                                dbuf.dsize) == -1)
                                goto fail;
 
                        /* FIXME: tdb_increment_seqnum(tdb); */
@@ -525,7 +558,6 @@ int tdb_append(struct tdb_context *tdb,
                                          F_WRLCK);
                        return 0;
                }
-               /* FIXME: Check right record free? */
 
                /* Slow path. */
                newdata = malloc(key.dsize + old_dlen + dbuf.dsize);
@@ -535,8 +567,8 @@ int tdb_append(struct tdb_context *tdb,
                                   (size_t)(key.dsize+old_dlen+dbuf.dsize));
                        goto fail;
                }
-               if (tdb->methods->read(tdb, off + sizeof(rec) + key.dsize,
-                                      newdata, old_dlen) != 0) {
+               if (tdb->methods->tread(tdb, off + sizeof(rec) + key.dsize,
+                                       newdata, old_dlen) != 0) {
                        free(newdata);
                        goto fail;
                }
@@ -706,8 +738,9 @@ void COLD tdb_logerr(struct tdb_context *tdb,
 
        message = malloc(len + 1);
        if (!message) {
-               tdb->logfn(tdb, level, tdb->log_private,
-                          "out of memory formatting message");
+               tdb->logfn(tdb, TDB_DEBUG_ERROR, tdb->log_private,
+                          "out of memory formatting message:");
+               tdb->logfn(tdb, level, tdb->log_private, fmt);
                return;
        }
        va_start(ap, fmt);