4 /* all lock info, to detect double-opens (fcntl file don't nest!) */
5 static struct tdb_file *files = NULL;
7 static struct tdb_file *find_file(dev_t device, ino_t ino)
11 for (i = files; i; i = i->next) {
12 if (i->device == device && i->inode == ino) {
20 static bool read_all(int fd, void *buf, size_t len)
24 ret = read(fd, buf, len);
32 buf = (char *)buf + ret;
38 static uint64_t random_number(struct tdb_context *tdb)
44 fd = open("/dev/urandom", O_RDONLY);
46 if (read_all(fd, &ret, sizeof(ret))) {
52 /* FIXME: Untested! Based on Wikipedia protocol description! */
53 fd = open("/dev/egd-pool", O_RDWR);
55 /* Command is 1, next byte is size we want to read. */
56 char cmd[2] = { 1, sizeof(uint64_t) };
57 if (write(fd, cmd, sizeof(cmd)) == sizeof(cmd)) {
58 char reply[1 + sizeof(uint64_t)];
59 int r = read(fd, reply, sizeof(reply));
61 /* Copy at least some bytes. */
62 memcpy(&ret, reply+1, r - 1);
63 if (reply[0] == sizeof(uint64_t)
64 && r == sizeof(reply)) {
73 /* Fallback: pid and time. */
74 gettimeofday(&now, NULL);
75 ret = getpid() * 100132289ULL + now.tv_sec * 1000000ULL + now.tv_usec;
76 tdb_logerr(tdb, TDB_SUCCESS, TDB_LOG_WARNING,
77 "tdb_open: random from getpid and time");
82 struct tdb_header hdr;
83 struct tdb_freetable ftable;
86 /* initialise a new database */
87 static enum TDB_ERROR tdb_new_database(struct tdb_context *tdb,
88 struct tdb_attribute_seed *seed,
89 struct tdb_header *hdr)
91 /* We make it up in memory, then write it out if not internal */
92 struct new_database newdb;
93 unsigned int magic_len;
97 /* Fill in the header */
98 newdb.hdr.version = TDB_VERSION;
100 newdb.hdr.hash_seed = seed->seed;
102 newdb.hdr.hash_seed = random_number(tdb);
103 newdb.hdr.hash_test = TDB_HASH_MAGIC;
104 newdb.hdr.hash_test = tdb->hash_fn(&newdb.hdr.hash_test,
105 sizeof(newdb.hdr.hash_test),
108 newdb.hdr.recovery = 0;
109 newdb.hdr.features_used = newdb.hdr.features_offered = TDB_FEATURE_MASK;
110 newdb.hdr.seqnum = 0;
111 memset(newdb.hdr.reserved, 0, sizeof(newdb.hdr.reserved));
112 /* Initial hashes are empty. */
113 memset(newdb.hdr.hashtable, 0, sizeof(newdb.hdr.hashtable));
116 newdb.hdr.free_table = offsetof(struct new_database, ftable);
117 memset(&newdb.ftable, 0, sizeof(newdb.ftable));
118 ecode = set_header(NULL, &newdb.ftable.hdr, TDB_FTABLE_MAGIC, 0,
119 sizeof(newdb.ftable) - sizeof(newdb.ftable.hdr),
120 sizeof(newdb.ftable) - sizeof(newdb.ftable.hdr),
122 if (ecode != TDB_SUCCESS) {
127 memset(newdb.hdr.magic_food, 0, sizeof(newdb.hdr.magic_food));
128 strcpy(newdb.hdr.magic_food, TDB_MAGIC_FOOD);
130 /* This creates an endian-converted database, as if read from disk */
131 magic_len = sizeof(newdb.hdr.magic_food);
133 (char *)&newdb.hdr + magic_len, sizeof(newdb) - magic_len);
137 if (tdb->flags & TDB_INTERNAL) {
138 tdb->file->map_size = sizeof(newdb);
139 tdb->file->map_ptr = malloc(tdb->file->map_size);
140 if (!tdb->file->map_ptr) {
141 return tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
143 " failed to allocate");
145 memcpy(tdb->file->map_ptr, &newdb, tdb->file->map_size);
148 if (lseek(tdb->file->fd, 0, SEEK_SET) == -1) {
149 return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
151 " failed to seek: %s", strerror(errno));
154 if (ftruncate(tdb->file->fd, 0) == -1) {
155 return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
157 " failed to truncate: %s", strerror(errno));
160 rlen = write(tdb->file->fd, &newdb, sizeof(newdb));
161 if (rlen != sizeof(newdb)) {
164 return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
165 "tdb_new_database: %zi writing header: %s",
166 rlen, strerror(errno));
171 static enum TDB_ERROR tdb_new_file(struct tdb_context *tdb)
173 tdb->file = malloc(sizeof(*tdb->file));
175 return tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
176 "tdb_open: could alloc tdb_file structure");
177 tdb->file->num_lockrecs = 0;
178 tdb->file->lockrecs = NULL;
179 tdb->file->allrecord_lock.count = 0;
180 tdb->file->refcnt = 1;
184 struct tdb_context *tdb_open(const char *name, int tdb_flags,
185 int open_flags, mode_t mode,
186 union tdb_attribute *attr)
188 struct tdb_context *tdb;
194 struct tdb_header hdr;
195 struct tdb_attribute_seed *seed = NULL;
196 struct tdb_attribute_openhook *openhook = NULL;
198 enum TDB_ERROR ecode;
200 tdb = malloc(sizeof(*tdb));
207 tdb->direct_access = 0;
208 tdb->flags = tdb_flags;
210 tdb->transaction = NULL;
213 tdb->last_error = TDB_SUCCESS;
219 switch (attr->base.attr) {
220 case TDB_ATTRIBUTE_LOG:
221 tdb->log_fn = attr->log.fn;
222 tdb->log_data = attr->log.data;
224 case TDB_ATTRIBUTE_HASH:
225 tdb->hash_fn = attr->hash.fn;
226 tdb->hash_data = attr->hash.data;
228 case TDB_ATTRIBUTE_SEED:
231 case TDB_ATTRIBUTE_STATS:
232 tdb->stats = &attr->stats;
233 /* They have stats we don't know about? Tell them. */
234 if (tdb->stats->size > sizeof(attr->stats))
235 tdb->stats->size = sizeof(attr->stats);
237 case TDB_ATTRIBUTE_OPENHOOK:
238 openhook = &attr->openhook;
241 ecode = tdb_logerr(tdb, TDB_ERR_EINVAL,
244 " unknown attribute type %u",
248 attr = attr->base.next;
251 if (tdb_flags & ~(TDB_INTERNAL | TDB_NOLOCK | TDB_NOMMAP | TDB_CONVERT
252 | TDB_NOSYNC | TDB_SEQNUM)) {
253 ecode = tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
254 "tdb_open: unknown flags %u", tdb_flags);
258 if ((open_flags & O_ACCMODE) == O_WRONLY) {
259 ecode = tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
260 "tdb_open: can't open tdb %s write-only",
265 if ((open_flags & O_ACCMODE) == O_RDONLY) {
266 tdb->read_only = true;
267 tdb->mmap_flags = PROT_READ;
269 tdb->read_only = false;
270 tdb->mmap_flags = PROT_READ | PROT_WRITE;
273 /* internal databases don't need any of the rest. */
274 if (tdb->flags & TDB_INTERNAL) {
275 tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
276 ecode = tdb_new_file(tdb);
277 if (ecode != TDB_SUCCESS) {
281 ecode = tdb_new_database(tdb, seed, &hdr);
282 if (ecode != TDB_SUCCESS) {
286 tdb->name = strdup(name);
288 ecode = tdb_logerr(tdb, TDB_ERR_OOM,
290 "tdb_open: failed to"
295 tdb_convert(tdb, &hdr.hash_seed, sizeof(hdr.hash_seed));
296 tdb->hash_seed = hdr.hash_seed;
297 tdb_ftable_init(tdb);
301 if (stat(name, &st) != -1)
302 tdb->file = find_file(st.st_dev, st.st_ino);
307 if ((fd = open(name, open_flags, mode)) == -1) {
308 /* errno set by open(2) */
310 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
311 "tdb_open: could not open file %s: %s",
312 name, strerror(errno));
316 /* on exec, don't inherit the fd */
317 v = fcntl(fd, F_GETFD, 0);
318 fcntl(fd, F_SETFD, v | FD_CLOEXEC);
320 if (fstat(fd, &st) == -1) {
322 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
323 "tdb_open: could not stat open %s: %s",
324 name, strerror(errno));
328 ecode = tdb_new_file(tdb);
329 if (ecode != TDB_SUCCESS)
332 tdb->file->next = files;
334 tdb->file->device = st.st_dev;
335 tdb->file->inode = st.st_ino;
336 tdb->file->map_ptr = NULL;
337 tdb->file->map_size = sizeof(struct tdb_header);
340 /* ensure there is only one process initialising at once */
341 ecode = tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK);
342 if (ecode != TDB_SUCCESS) {
346 /* call their open hook if they gave us one. */
348 ecode = openhook->fn(tdb->file->fd, openhook->data);
349 if (ecode != TDB_SUCCESS) {
350 tdb_logerr(tdb, ecode, TDB_LOG_ERROR,
351 "tdb_open: open hook failed");
354 open_flags |= O_CREAT;
357 /* If they used O_TRUNC, read will return 0. */
358 rlen = pread(tdb->file->fd, &hdr, sizeof(hdr), 0);
359 if (rlen == 0 && (open_flags & O_CREAT)) {
360 ecode = tdb_new_database(tdb, seed, &hdr);
361 if (ecode != TDB_SUCCESS) {
364 } else if (rlen < 0) {
365 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
366 "tdb_open: error %s reading %s",
367 strerror(errno), name);
369 } else if (rlen < sizeof(hdr)
370 || strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) {
371 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
372 "tdb_open: %s is not a tdb file", name);
376 if (hdr.version != TDB_VERSION) {
377 if (hdr.version == bswap_64(TDB_VERSION))
378 tdb->flags |= TDB_CONVERT;
381 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
383 " %s is unknown version 0x%llx",
384 name, (long long)hdr.version);
389 tdb_convert(tdb, &hdr, sizeof(hdr));
390 tdb->hash_seed = hdr.hash_seed;
391 hash_test = TDB_HASH_MAGIC;
392 hash_test = tdb_hash(tdb, &hash_test, sizeof(hash_test));
393 if (hdr.hash_test != hash_test) {
394 /* wrong hash variant */
395 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
397 " %s uses a different hash function",
402 tdb->name = strdup(name);
404 ecode = tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
405 "tdb_open: failed to allocate name");
409 /* Clear any features we don't understand. */
410 if ((open_flags & O_ACCMODE) != O_RDONLY) {
411 hdr.features_used &= TDB_FEATURE_MASK;
412 if (tdb_write_convert(tdb, offsetof(struct tdb_header,
415 sizeof(hdr.features_used)) == -1)
419 tdb_unlock_open(tdb);
421 /* This make sure we have current map_size and mmap. */
422 tdb->methods->oob(tdb, tdb->file->map_size + 1, true);
424 /* Now it's fully formed, recover if necessary. */
425 berr = tdb_needs_recovery(tdb);
426 if (unlikely(berr != false)) {
431 ecode = tdb_lock_and_recover(tdb);
432 if (ecode != TDB_SUCCESS) {
437 ecode = tdb_ftable_init(tdb);
438 if (ecode != TDB_SUCCESS) {
442 /* Add to linked list if we're new. */
443 if (tdb->file->refcnt == 1)
448 /* Map ecode to some logical errno. */
450 case TDB_ERR_CORRUPT:
455 saved_errno = EWOULDBLOCK;
458 saved_errno = ENOMEM;
461 saved_errno = EINVAL;
464 saved_errno = EINVAL;
472 free(cast_const(char *, tdb->name));
474 tdb_lock_cleanup(tdb);
475 if (--tdb->file->refcnt == 0) {
476 assert(tdb->file->num_lockrecs == 0);
477 if (tdb->file->map_ptr) {
478 if (tdb->flags & TDB_INTERNAL) {
479 free(tdb->file->map_ptr);
481 tdb_munmap(tdb->file);
483 if (close(tdb->file->fd) != 0)
484 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
485 "tdb_open: failed to close tdb fd"
486 " on error: %s", strerror(errno));
487 free(tdb->file->lockrecs);
497 int tdb_close(struct tdb_context *tdb)
501 tdb_trace(tdb, "tdb_close");
503 if (tdb->transaction) {
504 tdb_transaction_cancel(tdb);
507 if (tdb->file->map_ptr) {
508 if (tdb->flags & TDB_INTERNAL)
509 free(tdb->file->map_ptr);
511 tdb_munmap(tdb->file);
513 free(cast_const(char *, tdb->name));
517 tdb_lock_cleanup(tdb);
518 if (--tdb->file->refcnt == 0) {
519 ret = close(tdb->file->fd);
521 /* Remove from files list */
522 for (i = &files; *i; i = &(*i)->next) {
523 if (*i == tdb->file) {
524 *i = tdb->file->next;
528 free(tdb->file->lockrecs);