/* read a lump of data at a specified offset, maybe convert */ static int tdb_read(struct tdb_context *tdb, tdb_off_t off, void *buf, tdb_len_t len, int cv) { if (tdb->methods->tdb_oob(tdb, off + len, 0) != 0) { return -1; } if (tdb->map_ptr) { memcpy(buf, off + (char *)tdb->map_ptr, len); } else { ssize_t ret = pread(tdb->fd, buf, len, off); if (ret != (ssize_t)len) { /* Ensure ecode is set for log fn. */ tdb->ecode = TDB_ERR_IO; TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_read failed at %d " "len=%d ret=%d (%s) map_size=%d\n", (int)off, (int)len, (int)ret, strerror(errno), (int)tdb->map_size)); return -1; } } if (cv) { tdb_convert(buf, len); } return 0; }
struct tdb_context *tdb_open_ex(const char *name, int hash_size, int tdb_flags, int open_flags, mode_t mode, const struct tdb_logging_context *log_ctx, tdb_hash_func hash_fn) { struct tdb_context *tdb; struct stat st; int rev = 0, locked = 0; unsigned char *vp; u32 vertest; if (!(tdb = (struct tdb_context *)calloc(1, sizeof *tdb))) { /* Can't log this */ errno = ENOMEM; goto fail; } tdb_io_init(tdb); tdb->fd = -1; tdb->name = NULL; tdb->map_ptr = NULL; tdb->flags = tdb_flags; tdb->open_flags = open_flags; if (log_ctx) { tdb->log = *log_ctx; } else { tdb->log.log_fn = null_log_fn; tdb->log.log_private = NULL; } tdb->hash_fn = hash_fn ? hash_fn : default_tdb_hash; /* cache the page size */ tdb->page_size = getpagesize(); if (tdb->page_size <= 0) { tdb->page_size = 0x2000; } if ((open_flags & O_ACCMODE) == O_WRONLY) { TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: can't open tdb %s write-only\n", name)); errno = EINVAL; goto fail; } if (hash_size == 0) hash_size = DEFAULT_HASH_SIZE; if ((open_flags & O_ACCMODE) == O_RDONLY) { tdb->read_only = 1; /* read only databases don't do locking or clear if first */ tdb->flags |= TDB_NOLOCK; tdb->flags &= ~TDB_CLEAR_IF_FIRST; } /* internal databases don't mmap or lock, and start off cleared */ if (tdb->flags & TDB_INTERNAL) { tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP); tdb->flags &= ~TDB_CLEAR_IF_FIRST; if (tdb_new_database(tdb, hash_size) != 0) { TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: tdb_new_database failed!")); goto fail; } goto internal; } if ((tdb->fd = open(name, open_flags, mode)) == -1) { TDB_LOG((tdb, TDB_DEBUG_WARNING, "tdb_open_ex: could not open file %s: %s\n", name, strerror(errno))); goto fail; /* errno set by open(2) */ } /* ensure there is only one process initialising at once */ if (tdb->methods->tdb_brlock(tdb, GLOBAL_LOCK, F_WRLCK, F_SETLKW, 0, 1) == -1) { TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: failed to get global lock on %s: %s\n", name, strerror(errno))); goto fail; /* errno set by tdb_brlock */ } /* we need to zero database if we are the only one with it open */ if ((tdb_flags & TDB_CLEAR_IF_FIRST) && (locked = (tdb->methods->tdb_brlock(tdb, ACTIVE_LOCK, F_WRLCK, F_SETLK, 0, 1) == 0))) { open_flags |= O_CREAT; if (ftruncate(tdb->fd, 0) == -1) { TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_open_ex: " "failed to truncate %s: %s\n", name, strerror(errno))); goto fail; /* errno set by ftruncate */ } } if (read(tdb->fd, &tdb->header, sizeof(tdb->header)) != sizeof(tdb->header) || strcmp(tdb->header.magic_food, TDB_MAGIC_FOOD) != 0 || (tdb->header.version != TDB_VERSION && !(rev = (tdb->header.version==TDB_BYTEREV(TDB_VERSION))))) { /* its not a valid database - possibly initialise it */ if (!(open_flags & O_CREAT) || tdb_new_database(tdb, hash_size) == -1) { errno = EIO; /* ie bad format or something */ goto fail; } rev = (tdb->flags & TDB_CONVERT); } vp = (unsigned char *)&tdb->header.version; vertest = (((u32)vp[0]) << 24) | (((u32)vp[1]) << 16) | (((u32)vp[2]) << 8) | (u32)vp[3]; tdb->flags |= (vertest==TDB_VERSION) ? TDB_BIGENDIAN : 0; if (!rev) tdb->flags &= ~TDB_CONVERT; else { tdb->flags |= TDB_CONVERT; tdb_convert(&tdb->header, sizeof(tdb->header)); } if (fstat(tdb->fd, &st) == -1) goto fail; if (tdb->header.rwlocks != 0) { TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: spinlocks no longer supported\n")); goto fail; } /* Is it already in the open list? If so, fail. */ if (tdb_already_open(st.st_dev, st.st_ino)) { TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: " "%s (%d,%d) is already open in this process\n", name, (int)st.st_dev, (int)st.st_ino)); errno = EBUSY; goto fail; } if (!(tdb->name = (char *)strdup(name))) { errno = ENOMEM; goto fail; } tdb->map_size = st.st_size; tdb->device = st.st_dev; tdb->inode = st.st_ino; tdb->locked = (struct tdb_lock_type *)calloc(tdb->header.hash_size+1, sizeof(tdb->locked[0])); if (!tdb->locked) { TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: " "failed to allocate lock structure for %s\n", name)); errno = ENOMEM; goto fail; } tdb_mmap(tdb); if (locked) { if (tdb->methods->tdb_brlock(tdb, ACTIVE_LOCK, F_UNLCK, F_SETLK, 0, 1) == -1) { TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: " "failed to take ACTIVE_LOCK on %s: %s\n", name, strerror(errno))); goto fail; } } /* We always need to do this if the CLEAR_IF_FIRST flag is set, even if we didn't get the initial exclusive lock as we need to let all other users know we're using it. */ if (tdb_flags & TDB_CLEAR_IF_FIRST) { /* leave this lock in place to indicate it's in use */ if (tdb->methods->tdb_brlock(tdb, ACTIVE_LOCK, F_RDLCK, F_SETLKW, 0, 1) == -1) goto fail; } /* if needed, run recovery */ if (tdb_transaction_recover(tdb) == -1) { goto fail; } internal: /* Internal (memory-only) databases skip all the code above to * do with disk files, and resume here by releasing their * global lock and hooking into the active list. */ if (tdb->methods->tdb_brlock(tdb, GLOBAL_LOCK, F_UNLCK, F_SETLKW, 0, 1) == -1) goto fail; tdb->next = tdbs; tdbs = tdb; return tdb; fail: { int save_errno = errno; if (!tdb) return NULL; if (tdb->map_ptr) { if (tdb->flags & TDB_INTERNAL) SAFE_FREE(tdb->map_ptr); else tdb_munmap(tdb); } SAFE_FREE(tdb->name); if (tdb->fd != -1) if (close(tdb->fd) != 0) TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: failed to close tdb->fd on error!\n")); SAFE_FREE(tdb->locked); SAFE_FREE(tdb); errno = save_errno; return NULL; } }
int main(int argc, char *argv[]) { struct tdb_context *tdb; unsigned int log_count, flags; TDB_DATA d, r; struct tdb_logging_context log_ctx = { log_fn, &log_count }; plan_tests(38 * 2); for (flags = 0; flags <= TDB_CONVERT; flags += TDB_CONVERT) { unsigned int rwmagic = TDB_HASH_RWLOCK_MAGIC; if (flags & TDB_CONVERT) tdb_convert(&rwmagic, sizeof(rwmagic)); /* Create an old-style hash. */ log_count = 0; tdb = tdb_open_ex("run-incompatible.tdb", 0, flags, O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx, NULL); ok1(tdb); ok1(log_count == 0); d.dptr = (void *)"Hello"; d.dsize = 5; ok1(tdb_store(tdb, d, d, TDB_INSERT) == 0); tdb_close(tdb); /* Should not have marked rwlocks field. */ ok1(hdr_rwlocks("run-incompatible.tdb") == 0); /* We can still open any old-style with incompat flag. */ log_count = 0; tdb = tdb_open_ex("run-incompatible.tdb", 0, TDB_INCOMPATIBLE_HASH, O_RDWR, 0600, &log_ctx, NULL); ok1(tdb); ok1(log_count == 0); r = tdb_fetch(tdb, d); ok1(r.dsize == 5); free(r.dptr); ok1(tdb_check(tdb, NULL, NULL) == 0); tdb_close(tdb); log_count = 0; tdb = tdb_open_ex("test/jenkins-le-hash.tdb", 0, 0, O_RDONLY, 0, &log_ctx, tdb_jenkins_hash); ok1(tdb); ok1(log_count == 0); ok1(tdb_check(tdb, NULL, NULL) == 0); tdb_close(tdb); log_count = 0; tdb = tdb_open_ex("test/jenkins-be-hash.tdb", 0, 0, O_RDONLY, 0, &log_ctx, tdb_jenkins_hash); ok1(tdb); ok1(log_count == 0); ok1(tdb_check(tdb, NULL, NULL) == 0); tdb_close(tdb); /* OK, now create with incompatible flag, default hash. */ log_count = 0; tdb = tdb_open_ex("run-incompatible.tdb", 0, flags|TDB_INCOMPATIBLE_HASH, O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx, NULL); ok1(tdb); ok1(log_count == 0); d.dptr = (void *)"Hello"; d.dsize = 5; ok1(tdb_store(tdb, d, d, TDB_INSERT) == 0); tdb_close(tdb); /* Should have marked rwlocks field. */ ok1(hdr_rwlocks("run-incompatible.tdb") == rwmagic); /* Cannot open with old hash. */ log_count = 0; tdb = tdb_open_ex("run-incompatible.tdb", 0, 0, O_RDWR, 0600, &log_ctx, tdb_old_hash); ok1(!tdb); ok1(log_count == 1); /* Can open with jenkins hash. */ log_count = 0; tdb = tdb_open_ex("run-incompatible.tdb", 0, 0, O_RDWR, 0600, &log_ctx, tdb_jenkins_hash); ok1(tdb); ok1(log_count == 0); r = tdb_fetch(tdb, d); ok1(r.dsize == 5); free(r.dptr); ok1(tdb_check(tdb, NULL, NULL) == 0); tdb_close(tdb); /* Can open by letting it figure it out itself. */ log_count = 0; tdb = tdb_open_ex("run-incompatible.tdb", 0, 0, O_RDWR, 0600, &log_ctx, NULL); ok1(tdb); ok1(log_count == 0); r = tdb_fetch(tdb, d); ok1(r.dsize == 5); free(r.dptr); ok1(tdb_check(tdb, NULL, NULL) == 0); tdb_close(tdb); /* We can also use incompatible hash with other hashes. */ log_count = 0; tdb = tdb_open_ex("run-incompatible.tdb", 0, flags|TDB_INCOMPATIBLE_HASH, O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx, tdb_dumb_hash); ok1(tdb); ok1(log_count == 0); d.dptr = (void *)"Hello"; d.dsize = 5; ok1(tdb_store(tdb, d, d, TDB_INSERT) == 0); tdb_close(tdb); /* Should have marked rwlocks field. */ ok1(hdr_rwlocks("run-incompatible.tdb") == rwmagic); /* It should not open if we don't specify. */ log_count = 0; tdb = tdb_open_ex("run-incompatible.tdb", 0, 0, O_RDWR, 0, &log_ctx, NULL); ok1(!tdb); ok1(log_count == 1); /* Should reopen with correct hash. */ log_count = 0; tdb = tdb_open_ex("run-incompatible.tdb", 0, 0, O_RDWR, 0, &log_ctx, tdb_dumb_hash); ok1(tdb); ok1(log_count == 0); r = tdb_fetch(tdb, d); ok1(r.dsize == 5); free(r.dptr); ok1(tdb_check(tdb, NULL, NULL) == 0); tdb_close(tdb); } return exit_status(); }