- return false;
-}
-
-static uint64_t random_number(struct tdb_context *tdb)
-{
- int fd;
- uint64_t ret = 0;
- struct timeval now;
-
- fd = open("/dev/urandom", O_RDONLY);
- if (fd >= 0) {
- if (read(fd, &ret, sizeof(ret)) == sizeof(ret)) {
- tdb->log(tdb, TDB_DEBUG_TRACE, tdb->log_priv,
- "tdb_open: random from /dev/urandom\n");
- close(fd);
- return ret;
- }
- close(fd);
- }
- /* FIXME: Untested! Based on Wikipedia protocol description! */
- fd = open("/dev/egd-pool", O_RDWR);
- if (fd >= 0) {
- /* Command is 1, next byte is size we want to read. */
- char cmd[2] = { 1, sizeof(uint64_t) };
- if (write(fd, cmd, sizeof(cmd)) == sizeof(cmd)) {
- char reply[1 + sizeof(uint64_t)];
- int r = read(fd, reply, sizeof(reply));
- if (r > 1) {
- tdb->log(tdb, TDB_DEBUG_TRACE, tdb->log_priv,
- "tdb_open: %u random bytes from"
- " /dev/egd-pool\n", r-1);
- /* Copy at least some bytes. */
- memcpy(&ret, reply+1, r - 1);
- if (reply[0] == sizeof(uint64_t)
- && r == sizeof(reply)) {
- close(fd);
- return ret;
- }
- }
- }
- close(fd);
- }
-
- /* Fallback: pid and time. */
- gettimeofday(&now, NULL);
- ret = getpid() * 100132289ULL + now.tv_sec * 1000000ULL + now.tv_usec;
- tdb->log(tdb, TDB_DEBUG_TRACE, tdb->log_priv,
- "tdb_open: random from getpid and time\n");
- return ret;
-}
-
-struct new_database {
- struct tdb_header hdr;
- struct tdb_used_record hrec;
- tdb_off_t hash[1ULL << INITIAL_HASH_BITS];
- struct tdb_used_record frec;
- tdb_off_t free[INITIAL_FREE_BUCKETS + 1]; /* One overflow bucket */
-};
-
-/* initialise a new database */
-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;
-
- /* Fill in the header */
- newdb.hdr.version = TDB_VERSION;
- newdb.hdr.hash_seed = random_number(tdb);
- newdb.hdr.hash_test = TDB_HASH_MAGIC;
- newdb.hdr.hash_test = tdb->khash(&newdb.hdr.hash_test,
- sizeof(newdb.hdr.hash_test),
- newdb.hdr.hash_seed,
- tdb->hash_priv);
-
- newdb.hdr.v.generation = 0;
-
- /* Free array has 1 zone, 10 buckets. All buckets empty. */
- newdb.hdr.v.num_zones = 1;
- newdb.hdr.v.zone_bits = INITIAL_ZONE_BITS;
- newdb.hdr.v.free_buckets = INITIAL_FREE_BUCKETS;
- newdb.hdr.v.free_off = offsetof(struct new_database, free);
- set_header(tdb, &newdb.frec, 0,
- sizeof(newdb.free), sizeof(newdb.free), 0);
- memset(newdb.free, 0, sizeof(newdb.free));
-
- /* Initial hashes are empty. */
- newdb.hdr.v.hash_bits = INITIAL_HASH_BITS;
- newdb.hdr.v.hash_off = offsetof(struct new_database, hash);
- set_header(tdb, &newdb.hrec, 0,
- sizeof(newdb.hash), sizeof(newdb.hash), 0);
- memset(newdb.hash, 0, sizeof(newdb.hash));
-
- if (tdb->flags & TDB_INTERNAL) {
- tdb->map_size = sizeof(newdb);
- tdb->map_ptr = malloc(tdb->map_size);
- if (!tdb->map_ptr) {
- tdb->ecode = TDB_ERR_OOM;
- return -1;
- }
- memcpy(tdb->map_ptr, &newdb, tdb->map_size);
- tdb->header = newdb.hdr;
- /* Convert the `ondisk' version if asked. */
- tdb_convert(tdb, tdb->map_ptr, sizeof(newdb));
- return 0;
- }
- if (lseek(tdb->fd, 0, SEEK_SET) == -1)
- return -1;
-
- if (ftruncate(tdb->fd, 0) == -1)
- return -1;
-
- /* This creates an endian-converted header, as if read from disk */
- tdb->header = newdb.hdr;
- tdb_convert(tdb, &tdb->header, sizeof(tdb->header));
-
- /* Don't endian-convert the magic food! */
- memset(newdb.hdr.magic_food, 0, sizeof(newdb.hdr.magic_food));
- strcpy(newdb.hdr.magic_food, TDB_MAGIC_FOOD);
-
- if (!tdb_pwrite_all(tdb->fd, &newdb, sizeof(newdb), 0)) {
- tdb->ecode = TDB_ERR_IO;
- return -1;
- }
- return 0;
-}
-
-struct tdb_context *tdb_open(const char *name, int tdb_flags,
- int open_flags, mode_t mode,
- union tdb_attribute *attr)
-{
- struct tdb_context *tdb;
- struct stat st;
- int save_errno;
- uint64_t hash_test;
- unsigned v;
-
- if (!(tdb = (struct tdb_context *)calloc(1, sizeof *tdb))) {
- /* Can't log this */
- errno = ENOMEM;
- goto fail;
- }
- tdb->fd = -1;
- tdb->name = NULL;
- tdb->map_ptr = NULL;
- tdb->flags = tdb_flags;
- tdb->log = null_log_fn;
- tdb->log_priv = NULL;
- tdb->khash = jenkins_hash;
- tdb->hash_priv = NULL;
-
- /* FIXME */
- if (attr) {
- tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
- "tdb_open: attributes not yet supported\n");
- errno = EINVAL;
- goto fail;
- }
-
- if ((open_flags & O_ACCMODE) == O_WRONLY) {
- tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
- "tdb_open: can't open tdb %s write-only\n", name);
- errno = EINVAL;
- goto fail;
- }
-
- if ((open_flags & O_ACCMODE) == O_RDONLY) {
- tdb->read_only = 1;
- /* read only databases don't do locking */
- tdb->flags |= TDB_NOLOCK;
- }
-
- /* internal databases don't mmap or lock */
- if (tdb->flags & TDB_INTERNAL) {
- tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
- if (tdb_new_database(tdb) != 0) {
- tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
- "tdb_open: tdb_new_database failed!");
- goto fail;
- }
- TEST_IT(tdb->flags & TDB_CONVERT);
- goto internal;
- }
-
- if ((tdb->fd = open(name, open_flags, mode)) == -1) {
- tdb->log(tdb, TDB_DEBUG_WARNING, tdb->log_priv,
- "tdb_open: could not open file %s: %s\n",
- name, strerror(errno));
- goto fail; /* errno set by open(2) */
- }
-
- /* on exec, don't inherit the fd */
- v = fcntl(tdb->fd, F_GETFD, 0);
- fcntl(tdb->fd, F_SETFD, v | FD_CLOEXEC);
-
- /* ensure there is only one process initialising at once */
- if (tdb_lock_open(tdb) == -1) {
- tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
- "tdb_open: failed to get open lock on %s: %s\n",
- name, strerror(errno));
- goto fail; /* errno set by tdb_brlock */
- }
-
- errno = 0;
- if (read(tdb->fd, &tdb->header, sizeof(tdb->header)) != sizeof(tdb->header)
- || strcmp(tdb->header.magic_food, TDB_MAGIC_FOOD) != 0) {
- if (!(open_flags & O_CREAT) || tdb_new_database(tdb) == -1) {
- if (errno == 0) {
- errno = EIO; /* ie bad format or something */
- }
- goto fail;
- }
- } else if (tdb->header.version != TDB_VERSION) {
- if (tdb->header.version == bswap_64(TDB_VERSION))
- tdb->flags |= TDB_CONVERT;
- else {
- /* wrong version */
- tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
- "tdb_open: %s is unknown version 0x%llx\n",
- name, (long long)tdb->header.version);
- errno = EIO;
- goto fail;
- }