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->file->map_size = sizeof(newdb);
136 tdb->file->map_ptr = malloc(tdb->file->map_size);
137 if (!tdb->file->map_ptr) {
138 return tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
140 " failed to allocate");
142 memcpy(tdb->file->map_ptr, &newdb, tdb->file->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 static enum TDB_ERROR tdb_new_file(struct tdb_context *tdb)
170 tdb->file = malloc(sizeof(*tdb->file));
172 return tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
173 "tdb_open: could alloc tdb_file structure");
174 tdb->file->num_lockrecs = 0;
175 tdb->file->lockrecs = NULL;
176 tdb->file->allrecord_lock.count = 0;
180 struct tdb_context *tdb_open(const char *name, int tdb_flags,
181 int open_flags, mode_t mode,
182 union tdb_attribute *attr)
184 struct tdb_context *tdb;
190 struct tdb_header hdr;
191 struct tdb_attribute_seed *seed = NULL;
193 enum TDB_ERROR ecode;
195 tdb = malloc(sizeof(*tdb));
202 tdb->direct_access = 0;
203 tdb->flags = tdb_flags;
205 tdb->transaction = NULL;
213 switch (attr->base.attr) {
214 case TDB_ATTRIBUTE_LOG:
215 tdb->logfn = attr->log.log_fn;
216 tdb->log_private = attr->log.log_private;
218 case TDB_ATTRIBUTE_HASH:
219 tdb->khash = attr->hash.hash_fn;
220 tdb->hash_priv = attr->hash.hash_private;
222 case TDB_ATTRIBUTE_SEED:
225 case TDB_ATTRIBUTE_STATS:
226 tdb->stats = &attr->stats;
227 /* They have stats we don't know about? Tell them. */
228 if (tdb->stats->size > sizeof(attr->stats))
229 tdb->stats->size = sizeof(attr->stats);
232 ecode = tdb_logerr(tdb, TDB_ERR_EINVAL,
235 " unknown attribute type %u",
239 attr = attr->base.next;
242 if (tdb_flags & ~(TDB_INTERNAL | TDB_NOLOCK | TDB_NOMMAP | TDB_CONVERT
244 ecode = tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
245 "tdb_open: unknown flags %u", tdb_flags);
249 if ((open_flags & O_ACCMODE) == O_WRONLY) {
250 ecode = tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
251 "tdb_open: can't open tdb %s write-only",
256 if ((open_flags & O_ACCMODE) == O_RDONLY) {
257 tdb->read_only = true;
258 tdb->mmap_flags = PROT_READ;
260 tdb->read_only = false;
261 tdb->mmap_flags = PROT_READ | PROT_WRITE;
264 /* internal databases don't need any of the rest. */
265 if (tdb->flags & TDB_INTERNAL) {
266 tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
267 ecode = tdb_new_file(tdb);
268 if (ecode != TDB_SUCCESS) {
272 ecode = tdb_new_database(tdb, seed, &hdr);
273 if (ecode != TDB_SUCCESS) {
276 tdb_convert(tdb, &hdr.hash_seed, sizeof(hdr.hash_seed));
277 tdb->hash_seed = hdr.hash_seed;
278 tdb_ftable_init(tdb);
282 if (stat(name, &st) != -1)
283 tdb->file = find_file(st.st_dev, st.st_ino);
288 if ((fd = open(name, open_flags, mode)) == -1) {
289 /* errno set by open(2) */
291 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
292 "tdb_open: could not open file %s: %s",
293 name, strerror(errno));
297 /* on exec, don't inherit the fd */
298 v = fcntl(fd, F_GETFD, 0);
299 fcntl(fd, F_SETFD, v | FD_CLOEXEC);
301 if (fstat(fd, &st) == -1) {
303 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
304 "tdb_open: could not stat open %s: %s",
305 name, strerror(errno));
309 ecode = tdb_new_file(tdb);
310 if (ecode != TDB_SUCCESS)
313 tdb->file->next = files;
315 tdb->file->device = st.st_dev;
316 tdb->file->inode = st.st_ino;
317 tdb->file->map_ptr = NULL;
318 tdb->file->map_size = sizeof(struct tdb_header);
321 ecode = tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
322 "tdb_open: %s (%d,%d) is already open in"
324 name, (int)st.st_dev, (int)st.st_ino);
328 /* ensure there is only one process initialising at once */
329 ecode = tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK);
330 if (ecode != TDB_SUCCESS) {
334 /* If they used O_TRUNC, read will return 0. */
335 rlen = read(tdb->file->fd, &hdr, sizeof(hdr));
336 if (rlen == 0 && (open_flags & O_CREAT)) {
337 ecode = tdb_new_database(tdb, seed, &hdr);
338 if (ecode != TDB_SUCCESS) {
341 } else if (rlen < 0) {
342 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
343 "tdb_open: error %s reading %s",
344 strerror(errno), name);
346 } else if (rlen < sizeof(hdr)
347 || strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) {
348 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
349 "tdb_open: %s is not a tdb file", name);
353 if (hdr.version != TDB_VERSION) {
354 if (hdr.version == bswap_64(TDB_VERSION))
355 tdb->flags |= TDB_CONVERT;
358 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
360 " %s is unknown version 0x%llx",
361 name, (long long)hdr.version);
366 tdb_convert(tdb, &hdr, sizeof(hdr));
367 tdb->hash_seed = hdr.hash_seed;
368 hash_test = TDB_HASH_MAGIC;
369 hash_test = tdb_hash(tdb, &hash_test, sizeof(hash_test));
370 if (hdr.hash_test != hash_test) {
371 /* wrong hash variant */
372 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
374 " %s uses a different hash function",
379 tdb->name = strdup(name);
381 ecode = tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
382 "tdb_open: failed to allocate name");
386 /* Clear any features we don't understand. */
387 if ((open_flags & O_ACCMODE) != O_RDONLY) {
388 hdr.features_used &= TDB_FEATURE_MASK;
389 if (tdb_write_convert(tdb, offsetof(struct tdb_header,
392 sizeof(hdr.features_used)) == -1)
396 tdb_unlock_open(tdb);
398 /* This make sure we have current map_size and mmap. */
399 tdb->methods->oob(tdb, tdb->file->map_size + 1, true);
401 /* Now it's fully formed, recover if necessary. */
402 berr = tdb_needs_recovery(tdb);
403 if (unlikely(berr != false)) {
408 ecode = tdb_lock_and_recover(tdb);
409 if (ecode != TDB_SUCCESS) {
414 ecode = tdb_ftable_init(tdb);
415 if (ecode != TDB_SUCCESS) {
419 /* Add to linked list. */
424 /* Map ecode to some logical errno. */
427 case TDB_ERR_CORRUPT:
432 saved_errno = EWOULDBLOCK;
435 saved_errno = ENOMEM;
438 saved_errno = EINVAL;
441 saved_errno = EINVAL;
449 free((char *)tdb->name);
451 if (tdb->file->map_ptr) {
452 if (tdb->flags & TDB_INTERNAL) {
453 free(tdb->file->map_ptr);
455 tdb_munmap(tdb->file);
457 if (close(tdb->file->fd) != 0)
458 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
459 "tdb_open: failed to close tdb fd"
460 " on error: %s", strerror(errno));
461 free(tdb->file->lockrecs);
470 int tdb_close(struct tdb_context *tdb)
474 tdb_trace(tdb, "tdb_close");
476 if (tdb->transaction) {
477 tdb_transaction_cancel(tdb);
480 if (tdb->file->map_ptr) {
481 if (tdb->flags & TDB_INTERNAL)
482 free(tdb->file->map_ptr);
484 tdb_munmap(tdb->file);
486 free((char *)tdb->name);
489 ret = close(tdb->file->fd);
491 /* Remove from files list */
492 for (i = &files; *i; i = &(*i)->next) {
493 if (*i == tdb->file) {
494 *i = tdb->file->next;
498 free(tdb->file->lockrecs);