3 /* all lock info, to detect double-opens (fcntl file don't nest!) */
4 static struct tdb_file *files = NULL;
6 static struct tdb_file *find_file(dev_t device, ino_t ino)
10 for (i = files; i; i = i->next) {
11 if (i->device == device && i->inode == ino) {
18 static bool read_all(int fd, void *buf, size_t len)
22 ret = read(fd, buf, len);
30 buf = (char *)buf + ret;
36 static uint64_t random_number(struct tdb_context *tdb)
42 fd = open("/dev/urandom", O_RDONLY);
44 if (read_all(fd, &ret, sizeof(ret))) {
50 /* FIXME: Untested! Based on Wikipedia protocol description! */
51 fd = open("/dev/egd-pool", O_RDWR);
53 /* Command is 1, next byte is size we want to read. */
54 char cmd[2] = { 1, sizeof(uint64_t) };
55 if (write(fd, cmd, sizeof(cmd)) == sizeof(cmd)) {
56 char reply[1 + sizeof(uint64_t)];
57 int r = read(fd, reply, sizeof(reply));
59 /* Copy at least some bytes. */
60 memcpy(&ret, reply+1, r - 1);
61 if (reply[0] == sizeof(uint64_t)
62 && r == sizeof(reply)) {
71 /* Fallback: pid and time. */
72 gettimeofday(&now, NULL);
73 ret = getpid() * 100132289ULL + now.tv_sec * 1000000ULL + now.tv_usec;
74 tdb_logerr(tdb, TDB_SUCCESS, TDB_LOG_WARNING,
75 "tdb_open: random from getpid and time");
80 struct tdb_header hdr;
81 struct tdb_freetable ftable;
84 /* initialise a new database */
85 static enum TDB_ERROR tdb_new_database(struct tdb_context *tdb,
86 struct tdb_attribute_seed *seed,
87 struct tdb_header *hdr)
89 /* We make it up in memory, then write it out if not internal */
90 struct new_database newdb;
91 unsigned int magic_len;
95 /* Fill in the header */
96 newdb.hdr.version = TDB_VERSION;
98 newdb.hdr.hash_seed = seed->seed;
100 newdb.hdr.hash_seed = random_number(tdb);
101 newdb.hdr.hash_test = TDB_HASH_MAGIC;
102 newdb.hdr.hash_test = tdb->khash(&newdb.hdr.hash_test,
103 sizeof(newdb.hdr.hash_test),
106 newdb.hdr.recovery = 0;
107 newdb.hdr.features_used = newdb.hdr.features_offered = TDB_FEATURE_MASK;
108 memset(newdb.hdr.reserved, 0, sizeof(newdb.hdr.reserved));
109 /* Initial hashes are empty. */
110 memset(newdb.hdr.hashtable, 0, sizeof(newdb.hdr.hashtable));
113 newdb.hdr.free_table = offsetof(struct new_database, ftable);
114 memset(&newdb.ftable, 0, sizeof(newdb.ftable));
115 ecode = set_header(NULL, &newdb.ftable.hdr, TDB_FTABLE_MAGIC, 0,
116 sizeof(newdb.ftable) - sizeof(newdb.ftable.hdr),
117 sizeof(newdb.ftable) - sizeof(newdb.ftable.hdr),
119 if (ecode != TDB_SUCCESS) {
124 memset(newdb.hdr.magic_food, 0, sizeof(newdb.hdr.magic_food));
125 strcpy(newdb.hdr.magic_food, TDB_MAGIC_FOOD);
127 /* This creates an endian-converted database, as if read from disk */
128 magic_len = sizeof(newdb.hdr.magic_food);
130 (char *)&newdb.hdr + magic_len, sizeof(newdb) - magic_len);
134 if (tdb->flags & TDB_INTERNAL) {
135 tdb->map_size = sizeof(newdb);
136 tdb->map_ptr = malloc(tdb->map_size);
138 return tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
140 " failed to allocate");
142 memcpy(tdb->map_ptr, &newdb, tdb->map_size);
145 if (lseek(tdb->file->fd, 0, SEEK_SET) == -1) {
146 return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
148 " failed to seek: %s", strerror(errno));
151 if (ftruncate(tdb->file->fd, 0) == -1) {
152 return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
154 " failed to truncate: %s", strerror(errno));
157 rlen = write(tdb->file->fd, &newdb, sizeof(newdb));
158 if (rlen != sizeof(newdb)) {
161 return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
162 "tdb_new_database: %zi writing header: %s",
163 rlen, strerror(errno));
168 struct tdb_context *tdb_open(const char *name, int tdb_flags,
169 int open_flags, mode_t mode,
170 union tdb_attribute *attr)
172 struct tdb_context *tdb;
178 struct tdb_header hdr;
179 struct tdb_attribute_seed *seed = NULL;
181 enum TDB_ERROR ecode;
183 tdb = malloc(sizeof(*tdb));
191 tdb->direct_access = 0;
192 tdb->map_size = sizeof(struct tdb_header);
193 tdb->flags = tdb_flags;
195 tdb->transaction = NULL;
203 switch (attr->base.attr) {
204 case TDB_ATTRIBUTE_LOG:
205 tdb->logfn = attr->log.log_fn;
206 tdb->log_private = attr->log.log_private;
208 case TDB_ATTRIBUTE_HASH:
209 tdb->khash = attr->hash.hash_fn;
210 tdb->hash_priv = attr->hash.hash_private;
212 case TDB_ATTRIBUTE_SEED:
215 case TDB_ATTRIBUTE_STATS:
216 tdb->stats = &attr->stats;
217 /* They have stats we don't know about? Tell them. */
218 if (tdb->stats->size > sizeof(attr->stats))
219 tdb->stats->size = sizeof(attr->stats);
222 ecode = tdb_logerr(tdb, TDB_ERR_EINVAL,
225 " unknown attribute type %u",
229 attr = attr->base.next;
232 if (tdb_flags & ~(TDB_INTERNAL | TDB_NOLOCK | TDB_NOMMAP | TDB_CONVERT
234 ecode = tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
235 "tdb_open: unknown flags %u", tdb_flags);
239 if ((open_flags & O_ACCMODE) == O_WRONLY) {
240 ecode = tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
241 "tdb_open: can't open tdb %s write-only",
246 if ((open_flags & O_ACCMODE) == O_RDONLY) {
247 tdb->read_only = true;
248 tdb->mmap_flags = PROT_READ;
250 tdb->read_only = false;
251 tdb->mmap_flags = PROT_READ | PROT_WRITE;
254 /* internal databases don't need any of the rest. */
255 if (tdb->flags & TDB_INTERNAL) {
256 tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
257 ecode = tdb_new_database(tdb, seed, &hdr);
258 if (ecode != TDB_SUCCESS) {
261 tdb_convert(tdb, &hdr.hash_seed, sizeof(hdr.hash_seed));
262 tdb->hash_seed = hdr.hash_seed;
263 tdb_ftable_init(tdb);
267 if (stat(name, &st) != -1)
268 tdb->file = find_file(st.st_dev, st.st_ino);
273 if ((fd = open(name, open_flags, mode)) == -1) {
274 /* errno set by open(2) */
276 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
277 "tdb_open: could not open file %s: %s",
278 name, strerror(errno));
282 /* on exec, don't inherit the fd */
283 v = fcntl(fd, F_GETFD, 0);
284 fcntl(fd, F_SETFD, v | FD_CLOEXEC);
286 if (fstat(fd, &st) == -1) {
288 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
289 "tdb_open: could not stat open %s: %s",
290 name, strerror(errno));
294 tdb->file = malloc(sizeof(*tdb->file));
296 saved_errno = ENOMEM;
297 tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
298 "tdb_open: could alloc file");
302 tdb->file->next = files;
303 tdb->file->num_lockrecs = 0;
304 tdb->file->lockrecs = NULL;
305 tdb->file->allrecord_lock.count = 0;
307 tdb->file->device = st.st_dev;
308 tdb->file->inode = st.st_ino;
311 ecode = tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
312 "tdb_open: %s (%d,%d) is already open in"
314 name, (int)st.st_dev, (int)st.st_ino);
318 /* ensure there is only one process initialising at once */
319 ecode = tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK);
320 if (ecode != TDB_SUCCESS) {
324 /* If they used O_TRUNC, read will return 0. */
325 rlen = read(tdb->file->fd, &hdr, sizeof(hdr));
326 if (rlen == 0 && (open_flags & O_CREAT)) {
327 ecode = tdb_new_database(tdb, seed, &hdr);
328 if (ecode != TDB_SUCCESS) {
331 } else if (rlen < 0) {
332 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
333 "tdb_open: error %s reading %s",
334 strerror(errno), name);
336 } else if (rlen < sizeof(hdr)
337 || strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) {
338 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
339 "tdb_open: %s is not a tdb file", name);
343 if (hdr.version != TDB_VERSION) {
344 if (hdr.version == bswap_64(TDB_VERSION))
345 tdb->flags |= TDB_CONVERT;
348 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
350 " %s is unknown version 0x%llx",
351 name, (long long)hdr.version);
356 tdb_convert(tdb, &hdr, sizeof(hdr));
357 tdb->hash_seed = hdr.hash_seed;
358 hash_test = TDB_HASH_MAGIC;
359 hash_test = tdb_hash(tdb, &hash_test, sizeof(hash_test));
360 if (hdr.hash_test != hash_test) {
361 /* wrong hash variant */
362 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
364 " %s uses a different hash function",
369 tdb->name = strdup(name);
371 ecode = tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
372 "tdb_open: failed to allocate name");
376 /* Clear any features we don't understand. */
377 if ((open_flags & O_ACCMODE) != O_RDONLY) {
378 hdr.features_used &= TDB_FEATURE_MASK;
379 if (tdb_write_convert(tdb, offsetof(struct tdb_header,
382 sizeof(hdr.features_used)) == -1)
386 tdb_unlock_open(tdb);
388 /* This make sure we have current map_size and mmap. */
389 tdb->methods->oob(tdb, tdb->map_size + 1, true);
391 /* Now it's fully formed, recover if necessary. */
392 berr = tdb_needs_recovery(tdb);
393 if (unlikely(berr != false)) {
398 ecode = tdb_lock_and_recover(tdb);
399 if (ecode != TDB_SUCCESS) {
404 ecode = tdb_ftable_init(tdb);
405 if (ecode != TDB_SUCCESS) {
409 /* Add to linked list. */
414 /* Map ecode to some logical errno. */
417 case TDB_ERR_CORRUPT:
422 saved_errno = EWOULDBLOCK;
425 saved_errno = ENOMEM;
428 saved_errno = EINVAL;
431 saved_errno = EINVAL;
440 if (tdb->flags & TDB_INTERNAL) {
445 free((char *)tdb->name);
447 if (close(tdb->file->fd) != 0)
448 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
449 "tdb_open: failed to close tdb fd"
450 " on error: %s", strerror(errno));
451 free(tdb->file->lockrecs);
460 int tdb_close(struct tdb_context *tdb)
464 tdb_trace(tdb, "tdb_close");
466 if (tdb->transaction) {
467 tdb_transaction_cancel(tdb);
471 if (tdb->flags & TDB_INTERNAL)
476 free((char *)tdb->name);
479 ret = close(tdb->file->fd);
481 /* Remove from files list */
482 for (i = &files; *i; i = &(*i)->next) {
483 if (*i == tdb->file) {
484 *i = tdb->file->next;
488 free(tdb->file->lockrecs);