| 1 | /*
|
|---|
| 2 | Unix SMB/CIFS implementation.
|
|---|
| 3 |
|
|---|
| 4 | trivial database library
|
|---|
| 5 |
|
|---|
| 6 | Copyright (C) Andrew Tridgell 1999-2005
|
|---|
| 7 | Copyright (C) Paul `Rusty' Russell 2000
|
|---|
| 8 | Copyright (C) Jeremy Allison 2000-2003
|
|---|
| 9 |
|
|---|
| 10 | ** NOTE! The following LGPL license applies to the tdb
|
|---|
| 11 | ** library. This does NOT imply that all of Samba is released
|
|---|
| 12 | ** under the LGPL
|
|---|
| 13 |
|
|---|
| 14 | This library is free software; you can redistribute it and/or
|
|---|
| 15 | modify it under the terms of the GNU Lesser General Public
|
|---|
| 16 | License as published by the Free Software Foundation; either
|
|---|
| 17 | version 2 of the License, or (at your option) any later version.
|
|---|
| 18 |
|
|---|
| 19 | This library is distributed in the hope that it will be useful,
|
|---|
| 20 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 22 | Lesser General Public License for more details.
|
|---|
| 23 |
|
|---|
| 24 | You should have received a copy of the GNU Lesser General Public
|
|---|
| 25 | License along with this library; if not, write to the Free Software
|
|---|
| 26 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | #include "tdb_private.h"
|
|---|
| 30 |
|
|---|
| 31 | /* all contexts, to ensure no double-opens (fcntl locks don't nest!) */
|
|---|
| 32 | static struct tdb_context *tdbs = NULL;
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 | /* This is based on the hash algorithm from gdbm */
|
|---|
| 36 | static unsigned int default_tdb_hash(TDB_DATA *key)
|
|---|
| 37 | {
|
|---|
| 38 | u32 value; /* Used to compute the hash value. */
|
|---|
| 39 | u32 i; /* Used to cycle through random values. */
|
|---|
| 40 |
|
|---|
| 41 | /* Set the initial value from the key size. */
|
|---|
| 42 | for (value = 0x238F13AF * key->dsize, i=0; i < key->dsize; i++)
|
|---|
| 43 | value = (value + (key->dptr[i] << (i*5 % 24)));
|
|---|
| 44 |
|
|---|
| 45 | return (1103515243 * value + 12345);
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 | /* initialise a new database with a specified hash size */
|
|---|
| 50 | static int tdb_new_database(struct tdb_context *tdb, int hash_size)
|
|---|
| 51 | {
|
|---|
| 52 | struct tdb_header *newdb;
|
|---|
| 53 | size_t size;
|
|---|
| 54 | int ret = -1;
|
|---|
| 55 | ssize_t written;
|
|---|
| 56 |
|
|---|
| 57 | /* We make it up in memory, then write it out if not internal */
|
|---|
| 58 | size = sizeof(struct tdb_header) + (hash_size+1)*sizeof(tdb_off_t);
|
|---|
| 59 | if (!(newdb = (struct tdb_header *)calloc(size, 1)))
|
|---|
| 60 | return TDB_ERRCODE(TDB_ERR_OOM, -1);
|
|---|
| 61 |
|
|---|
| 62 | /* Fill in the header */
|
|---|
| 63 | newdb->version = TDB_VERSION;
|
|---|
| 64 | newdb->hash_size = hash_size;
|
|---|
| 65 | if (tdb->flags & TDB_INTERNAL) {
|
|---|
| 66 | tdb->map_size = size;
|
|---|
| 67 | tdb->map_ptr = (char *)newdb;
|
|---|
| 68 | memcpy(&tdb->header, newdb, sizeof(tdb->header));
|
|---|
| 69 | /* Convert the `ondisk' version if asked. */
|
|---|
| 70 | CONVERT(*newdb);
|
|---|
| 71 | return 0;
|
|---|
| 72 | }
|
|---|
| 73 | if (lseek(tdb->fd, 0, SEEK_SET) == -1)
|
|---|
| 74 | goto fail;
|
|---|
| 75 |
|
|---|
| 76 | if (ftruncate(tdb->fd, 0) == -1)
|
|---|
| 77 | goto fail;
|
|---|
| 78 |
|
|---|
| 79 | /* This creates an endian-converted header, as if read from disk */
|
|---|
| 80 | CONVERT(*newdb);
|
|---|
| 81 | memcpy(&tdb->header, newdb, sizeof(tdb->header));
|
|---|
| 82 | /* Don't endian-convert the magic food! */
|
|---|
| 83 | memcpy(newdb->magic_food, TDB_MAGIC_FOOD, strlen(TDB_MAGIC_FOOD)+1);
|
|---|
| 84 | /* we still have "ret == -1" here */
|
|---|
| 85 | written = write(tdb->fd, newdb, size);
|
|---|
| 86 | if (written == size) {
|
|---|
| 87 | ret = 0;
|
|---|
| 88 | } else if (written != -1) {
|
|---|
| 89 | /* call write once again, this usually should return -1 and
|
|---|
| 90 | * set errno appropriately */
|
|---|
| 91 | size -= written;
|
|---|
| 92 | written = write(tdb->fd, newdb+written, size);
|
|---|
| 93 | if (written == size) {
|
|---|
| 94 | ret = 0;
|
|---|
| 95 | } else if (written >= 0) {
|
|---|
| 96 | /* a second incomplete write - we give up.
|
|---|
| 97 | * guessing the errno... */
|
|---|
| 98 | errno = ENOSPC;
|
|---|
| 99 | }
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | fail:
|
|---|
| 103 | SAFE_FREE(newdb);
|
|---|
| 104 | return ret;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 | static int tdb_already_open(dev_t device,
|
|---|
| 110 | ino_t ino)
|
|---|
| 111 | {
|
|---|
| 112 | struct tdb_context *i;
|
|---|
| 113 |
|
|---|
| 114 | for (i = tdbs; i; i = i->next) {
|
|---|
| 115 | if (i->device == device && i->inode == ino) {
|
|---|
| 116 | return 1;
|
|---|
| 117 | }
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | return 0;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | /* open the database, creating it if necessary
|
|---|
| 124 |
|
|---|
| 125 | The open_flags and mode are passed straight to the open call on the
|
|---|
| 126 | database file. A flags value of O_WRONLY is invalid. The hash size
|
|---|
| 127 | is advisory, use zero for a default value.
|
|---|
| 128 |
|
|---|
| 129 | Return is NULL on error, in which case errno is also set. Don't
|
|---|
| 130 | try to call tdb_error or tdb_errname, just do strerror(errno).
|
|---|
| 131 |
|
|---|
| 132 | @param name may be NULL for internal databases. */
|
|---|
| 133 | struct tdb_context *tdb_open(const char *name, int hash_size, int tdb_flags,
|
|---|
| 134 | int open_flags, mode_t mode)
|
|---|
| 135 | {
|
|---|
| 136 | return tdb_open_ex(name, hash_size, tdb_flags, open_flags, mode, NULL, NULL);
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | /* a default logging function */
|
|---|
| 140 | static void null_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
|
|---|
| 141 | static void null_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...)
|
|---|
| 142 | {
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 | struct tdb_context *tdb_open_ex(const char *name, int hash_size, int tdb_flags,
|
|---|
| 147 | int open_flags, mode_t mode,
|
|---|
| 148 | const struct tdb_logging_context *log_ctx,
|
|---|
| 149 | tdb_hash_func hash_fn)
|
|---|
| 150 | {
|
|---|
| 151 | struct tdb_context *tdb;
|
|---|
| 152 | struct stat st;
|
|---|
| 153 | int rev = 0, locked = 0;
|
|---|
| 154 | unsigned char *vp;
|
|---|
| 155 | u32 vertest;
|
|---|
| 156 |
|
|---|
| 157 | if (!(tdb = (struct tdb_context *)calloc(1, sizeof *tdb))) {
|
|---|
| 158 | /* Can't log this */
|
|---|
| 159 | errno = ENOMEM;
|
|---|
| 160 | goto fail;
|
|---|
| 161 | }
|
|---|
| 162 | tdb_io_init(tdb);
|
|---|
| 163 | tdb->fd = -1;
|
|---|
| 164 | tdb->name = NULL;
|
|---|
| 165 | tdb->map_ptr = NULL;
|
|---|
| 166 | tdb->flags = tdb_flags;
|
|---|
| 167 | tdb->open_flags = open_flags;
|
|---|
| 168 | if (log_ctx) {
|
|---|
| 169 | tdb->log = *log_ctx;
|
|---|
| 170 | } else {
|
|---|
| 171 | tdb->log.log_fn = null_log_fn;
|
|---|
| 172 | tdb->log.log_private = NULL;
|
|---|
| 173 | }
|
|---|
| 174 | tdb->hash_fn = hash_fn ? hash_fn : default_tdb_hash;
|
|---|
| 175 |
|
|---|
| 176 | #ifdef __OS2__
|
|---|
| 177 | if (!(tdb->flags & TDB_INTERNAL))
|
|---|
| 178 | {
|
|---|
| 179 | char szSem[_MAX_PATH];
|
|---|
| 180 | char drive[_MAX_DRIVE], dir[_MAX_DIR];
|
|---|
| 181 | char fname[_MAX_FNAME], ext[_MAX_EXT];
|
|---|
| 182 | APIRET rc;
|
|---|
| 183 | // extract path info
|
|---|
| 184 | _splitpath( name, drive, dir, fname, ext);
|
|---|
| 185 | sprintf( szSem, "\\SEM32\\TDB_GL_%s%s%s", dir, fname, ext);
|
|---|
| 186 | rc = DosCreateMutexSem( szSem, &tdb->hGlobalLock, 0, FALSE);
|
|---|
| 187 | if (rc == ERROR_DUPLICATE_NAME)
|
|---|
| 188 | rc = DosOpenMutexSem( szSem, &tdb->hGlobalLock);
|
|---|
| 189 | if (rc != NO_ERROR) {
|
|---|
| 190 | printf( "cannot open %s %d\n", szSem, rc);
|
|---|
| 191 | errno = EINVAL;
|
|---|
| 192 | goto fail;
|
|---|
| 193 | }
|
|---|
| 194 | TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_open_ex pid %d global handle %d\n", getpid(), tdb->hGlobalLock));
|
|---|
| 195 | sprintf( szSem, "\\SEM32\\TDB_AL_%s%s%s", dir, fname, ext);
|
|---|
| 196 | rc = DosCreateMutexSem( szSem, &tdb->hActiveLock, 0, FALSE);
|
|---|
| 197 | if (rc == ERROR_DUPLICATE_NAME)
|
|---|
| 198 | rc = DosOpenMutexSem( szSem, &tdb->hActiveLock);
|
|---|
| 199 | if (rc != NO_ERROR) {
|
|---|
| 200 | printf( "cannot open %s %d\n", szSem, rc);
|
|---|
| 201 | errno = EINVAL;
|
|---|
| 202 | goto fail;
|
|---|
| 203 | }
|
|---|
| 204 | TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_open_ex pid %d active handle %d\n", getpid(), tdb->hActiveLock));
|
|---|
| 205 | sprintf( szSem, "\\SEM32\\TDB_TL_%s%s%s", dir, fname, ext);
|
|---|
| 206 | rc = DosCreateMutexSem( szSem, &tdb->hTransactionLock, 0, FALSE);
|
|---|
| 207 | if (rc == ERROR_DUPLICATE_NAME)
|
|---|
| 208 | rc = DosOpenMutexSem( szSem, &tdb->hTransactionLock);
|
|---|
| 209 | if (rc != NO_ERROR) {
|
|---|
| 210 | printf( "cannot open %s %d\n", szSem, rc);
|
|---|
| 211 | errno = EINVAL;
|
|---|
| 212 | goto fail;
|
|---|
| 213 | }
|
|---|
| 214 | TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_open_ex pid %d transaction handle %d\n", getpid(), tdb->hTransactionLock));
|
|---|
| 215 | }
|
|---|
| 216 | #endif
|
|---|
| 217 |
|
|---|
| 218 | /* cache the page size */
|
|---|
| 219 | tdb->page_size = getpagesize();
|
|---|
| 220 | if (tdb->page_size <= 0) {
|
|---|
| 221 | tdb->page_size = 0x2000;
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | if ((open_flags & O_ACCMODE) == O_WRONLY) {
|
|---|
| 225 | TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: can't open tdb %s write-only\n",
|
|---|
| 226 | name));
|
|---|
| 227 | errno = EINVAL;
|
|---|
| 228 | goto fail;
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | if (hash_size == 0)
|
|---|
| 232 | hash_size = DEFAULT_HASH_SIZE;
|
|---|
| 233 | if ((open_flags & O_ACCMODE) == O_RDONLY) {
|
|---|
| 234 | tdb->read_only = 1;
|
|---|
| 235 | /* read only databases don't do locking or clear if first */
|
|---|
| 236 | tdb->flags |= TDB_NOLOCK;
|
|---|
| 237 | tdb->flags &= ~TDB_CLEAR_IF_FIRST;
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | /* internal databases don't mmap or lock, and start off cleared */
|
|---|
| 241 | if (tdb->flags & TDB_INTERNAL) {
|
|---|
| 242 | tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
|
|---|
| 243 | tdb->flags &= ~TDB_CLEAR_IF_FIRST;
|
|---|
| 244 | if (tdb_new_database(tdb, hash_size) != 0) {
|
|---|
| 245 | TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: tdb_new_database failed!"));
|
|---|
| 246 | goto fail;
|
|---|
| 247 | }
|
|---|
| 248 | goto internal;
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | if ((tdb->fd = open(name, open_flags, mode)) == -1) {
|
|---|
| 252 | TDB_LOG((tdb, TDB_DEBUG_WARNING, "tdb_open_ex: could not open file %s: %s\n",
|
|---|
| 253 | name, strerror(errno)));
|
|---|
| 254 | goto fail; /* errno set by open(2) */
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | /* ensure there is only one process initialising at once */
|
|---|
| 258 | if (tdb->methods->tdb_brlock(tdb, GLOBAL_LOCK, F_WRLCK, F_SETLKW, 0, 1) == -1) {
|
|---|
| 259 | TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: failed to get global lock on %s: %s\n",
|
|---|
| 260 | name, strerror(errno)));
|
|---|
| 261 | goto fail; /* errno set by tdb_brlock */
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| 264 | /* we need to zero database if we are the only one with it open */
|
|---|
| 265 | if ((tdb_flags & TDB_CLEAR_IF_FIRST)
|
|---|
| 266 | #ifndef __OS2__ // we already have global lock
|
|---|
| 267 | && (locked = (tdb->methods->tdb_brlock(tdb, ACTIVE_LOCK, F_WRLCK, F_SETLK, 0, 1) == 0))
|
|---|
| 268 | #endif
|
|---|
| 269 | ) {
|
|---|
| 270 | open_flags |= O_CREAT;
|
|---|
| 271 | if (ftruncate(tdb->fd, 0) == -1) {
|
|---|
| 272 | TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_open_ex: "
|
|---|
| 273 | "failed to truncate %s: %s\n",
|
|---|
| 274 | name, strerror(errno)));
|
|---|
| 275 | goto fail; /* errno set by ftruncate */
|
|---|
| 276 | }
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|
| 279 | errno = 0;
|
|---|
| 280 | if (read(tdb->fd, &tdb->header, sizeof(tdb->header)) != sizeof(tdb->header)
|
|---|
| 281 | || strcmp(tdb->header.magic_food, TDB_MAGIC_FOOD) != 0
|
|---|
| 282 | || (tdb->header.version != TDB_VERSION
|
|---|
| 283 | && !(rev = (tdb->header.version==TDB_BYTEREV(TDB_VERSION))))) {
|
|---|
| 284 | /* its not a valid database - possibly initialise it */
|
|---|
| 285 | if (!(open_flags & O_CREAT) || tdb_new_database(tdb, hash_size) == -1) {
|
|---|
| 286 | if (errno == 0) {
|
|---|
| 287 | errno = EIO; /* ie bad format or something */
|
|---|
| 288 | }
|
|---|
| 289 | goto fail;
|
|---|
| 290 | }
|
|---|
| 291 | rev = (tdb->flags & TDB_CONVERT);
|
|---|
| 292 | }
|
|---|
| 293 | vp = (unsigned char *)&tdb->header.version;
|
|---|
| 294 | vertest = (((u32)vp[0]) << 24) | (((u32)vp[1]) << 16) |
|
|---|
| 295 | (((u32)vp[2]) << 8) | (u32)vp[3];
|
|---|
| 296 | tdb->flags |= (vertest==TDB_VERSION) ? TDB_BIGENDIAN : 0;
|
|---|
| 297 | if (!rev)
|
|---|
| 298 | tdb->flags &= ~TDB_CONVERT;
|
|---|
| 299 | else {
|
|---|
| 300 | tdb->flags |= TDB_CONVERT;
|
|---|
| 301 | tdb_convert(&tdb->header, sizeof(tdb->header));
|
|---|
| 302 | }
|
|---|
| 303 | if (fstat(tdb->fd, &st) == -1)
|
|---|
| 304 | goto fail;
|
|---|
| 305 |
|
|---|
| 306 | if (tdb->header.rwlocks != 0) {
|
|---|
| 307 | TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: spinlocks no longer supported\n"));
|
|---|
| 308 | goto fail;
|
|---|
| 309 | }
|
|---|
| 310 |
|
|---|
| 311 | /* Is it already in the open list? If so, fail. */
|
|---|
| 312 | if (tdb_already_open(st.st_dev, st.st_ino)) {
|
|---|
| 313 | TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: "
|
|---|
| 314 | "%s (%d,%d) is already open in this process\n",
|
|---|
| 315 | name, (int)st.st_dev, (int)st.st_ino));
|
|---|
| 316 | errno = EBUSY;
|
|---|
| 317 | goto fail;
|
|---|
| 318 | }
|
|---|
| 319 |
|
|---|
| 320 | if (!(tdb->name = (char *)strdup(name))) {
|
|---|
| 321 | errno = ENOMEM;
|
|---|
| 322 | goto fail;
|
|---|
| 323 | }
|
|---|
| 324 |
|
|---|
| 325 | tdb->map_size = st.st_size;
|
|---|
| 326 | tdb->device = st.st_dev;
|
|---|
| 327 | tdb->inode = st.st_ino;
|
|---|
| 328 | tdb->max_dead_records = 0;
|
|---|
| 329 | tdb_mmap(tdb);
|
|---|
| 330 | if (locked) {
|
|---|
| 331 | #ifndef __OS2__
|
|---|
| 332 | if (tdb->methods->tdb_brlock(tdb, ACTIVE_LOCK, F_UNLCK, F_SETLK, 0, 1) == -1) {
|
|---|
| 333 | TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: "
|
|---|
| 334 | "failed to take ACTIVE_LOCK on %s: %s\n",
|
|---|
| 335 | name, strerror(errno)));
|
|---|
| 336 | goto fail;
|
|---|
| 337 | }
|
|---|
| 338 | #endif
|
|---|
| 339 | }
|
|---|
| 340 |
|
|---|
| 341 | /* We always need to do this if the CLEAR_IF_FIRST flag is set, even if
|
|---|
| 342 | we didn't get the initial exclusive lock as we need to let all other
|
|---|
| 343 | users know we're using it. */
|
|---|
| 344 |
|
|---|
| 345 | #ifndef __OS2__
|
|---|
| 346 | if (tdb_flags & TDB_CLEAR_IF_FIRST) {
|
|---|
| 347 | /* leave this lock in place to indicate it's in use */
|
|---|
| 348 | if (tdb->methods->tdb_brlock(tdb, ACTIVE_LOCK, F_RDLCK, F_SETLKW, 0, 1) == -1)
|
|---|
| 349 | goto fail;
|
|---|
| 350 | }
|
|---|
| 351 | #endif
|
|---|
| 352 |
|
|---|
| 353 | /* if needed, run recovery */
|
|---|
| 354 | if (tdb_transaction_recover(tdb) == -1) {
|
|---|
| 355 | goto fail;
|
|---|
| 356 | }
|
|---|
| 357 |
|
|---|
| 358 | // YD internal databases do not get global lock!
|
|---|
| 359 | if (tdb->methods->tdb_brlock(tdb, GLOBAL_LOCK, F_UNLCK, F_SETLKW, 0, 1) == -1)
|
|---|
| 360 | goto fail;
|
|---|
| 361 |
|
|---|
| 362 | internal:
|
|---|
| 363 | /* Internal (memory-only) databases skip all the code above to
|
|---|
| 364 | * do with disk files, and resume here by releasing their
|
|---|
| 365 | * global lock and hooking into the active list. */
|
|---|
| 366 | tdb->next = tdbs;
|
|---|
| 367 | tdbs = tdb;
|
|---|
| 368 | return tdb;
|
|---|
| 369 |
|
|---|
| 370 | fail:
|
|---|
| 371 | { int save_errno = errno;
|
|---|
| 372 | tdb->methods->tdb_brlock(tdb, GLOBAL_LOCK, F_UNLCK, F_SETLKW, 0, 1);
|
|---|
| 373 |
|
|---|
| 374 | if (!tdb)
|
|---|
| 375 | return NULL;
|
|---|
| 376 |
|
|---|
| 377 | if (tdb->map_ptr) {
|
|---|
| 378 | if (tdb->flags & TDB_INTERNAL)
|
|---|
| 379 | SAFE_FREE(tdb->map_ptr);
|
|---|
| 380 | else
|
|---|
| 381 | tdb_munmap(tdb);
|
|---|
| 382 | }
|
|---|
| 383 | SAFE_FREE(tdb->name);
|
|---|
| 384 | #ifdef __OS2__
|
|---|
| 385 | DosCloseMutexSem( tdb->hGlobalLock);
|
|---|
| 386 | tdb->hGlobalLock = 0;
|
|---|
| 387 | DosCloseMutexSem( tdb->hActiveLock);
|
|---|
| 388 | tdb->hActiveLock = 0;
|
|---|
| 389 | DosCloseMutexSem( tdb->hTransactionLock);
|
|---|
| 390 | tdb->hTransactionLock = 0;
|
|---|
| 391 | #endif
|
|---|
| 392 | if (tdb->fd != -1)
|
|---|
| 393 | if (close(tdb->fd) != 0)
|
|---|
| 394 | TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: failed to close tdb->fd on error!\n"));
|
|---|
| 395 | SAFE_FREE(tdb);
|
|---|
| 396 | errno = save_errno;
|
|---|
| 397 | return NULL;
|
|---|
| 398 | }
|
|---|
| 399 | }
|
|---|
| 400 |
|
|---|
| 401 | /*
|
|---|
| 402 | * Set the maximum number of dead records per hash chain
|
|---|
| 403 | */
|
|---|
| 404 |
|
|---|
| 405 | void tdb_set_max_dead(struct tdb_context *tdb, int max_dead)
|
|---|
| 406 | {
|
|---|
| 407 | tdb->max_dead_records = max_dead;
|
|---|
| 408 | }
|
|---|
| 409 |
|
|---|
| 410 | /**
|
|---|
| 411 | * Close a database.
|
|---|
| 412 | *
|
|---|
| 413 | * @returns -1 for error; 0 for success.
|
|---|
| 414 | **/
|
|---|
| 415 | int tdb_close(struct tdb_context *tdb)
|
|---|
| 416 | {
|
|---|
| 417 | struct tdb_context **i;
|
|---|
| 418 | int ret = 0;
|
|---|
| 419 |
|
|---|
| 420 | if (tdb->transaction) {
|
|---|
| 421 | tdb_transaction_cancel(tdb);
|
|---|
| 422 | }
|
|---|
| 423 |
|
|---|
| 424 | if (tdb->map_ptr) {
|
|---|
| 425 | if (tdb->flags & TDB_INTERNAL)
|
|---|
| 426 | SAFE_FREE(tdb->map_ptr);
|
|---|
| 427 | else
|
|---|
| 428 | tdb_munmap(tdb);
|
|---|
| 429 | }
|
|---|
| 430 | SAFE_FREE(tdb->name);
|
|---|
| 431 | // YD internal databases do not have a global lock
|
|---|
| 432 | if (!(tdb->flags & TDB_INTERNAL))
|
|---|
| 433 | tdb->methods->tdb_brlock(tdb, GLOBAL_LOCK, F_UNLCK, F_SETLK, 0, 1);
|
|---|
| 434 | if (tdb->fd != -1)
|
|---|
| 435 | ret = close(tdb->fd);
|
|---|
| 436 | SAFE_FREE(tdb->lockrecs);
|
|---|
| 437 | #ifdef __OS2__
|
|---|
| 438 | DosCloseMutexSem( tdb->hGlobalLock);
|
|---|
| 439 | tdb->hGlobalLock = 0;
|
|---|
| 440 | DosCloseMutexSem( tdb->hActiveLock);
|
|---|
| 441 | tdb->hActiveLock = 0;
|
|---|
| 442 | DosCloseMutexSem( tdb->hTransactionLock);
|
|---|
| 443 | tdb->hTransactionLock = 0;
|
|---|
| 444 | #endif
|
|---|
| 445 |
|
|---|
| 446 | /* Remove from contexts list */
|
|---|
| 447 | for (i = &tdbs; *i; i = &(*i)->next) {
|
|---|
| 448 | if (*i == tdb) {
|
|---|
| 449 | *i = tdb->next;
|
|---|
| 450 | break;
|
|---|
| 451 | }
|
|---|
| 452 | }
|
|---|
| 453 |
|
|---|
| 454 | memset(tdb, 0, sizeof(*tdb));
|
|---|
| 455 | SAFE_FREE(tdb);
|
|---|
| 456 |
|
|---|
| 457 | return ret;
|
|---|
| 458 | }
|
|---|
| 459 |
|
|---|
| 460 | /* register a loging function */
|
|---|
| 461 | void tdb_set_logging_function(struct tdb_context *tdb,
|
|---|
| 462 | const struct tdb_logging_context *log_ctx)
|
|---|
| 463 | {
|
|---|
| 464 | tdb->log = *log_ctx;
|
|---|
| 465 | }
|
|---|
| 466 |
|
|---|
| 467 | void *tdb_get_logging_private(struct tdb_context *tdb)
|
|---|
| 468 | {
|
|---|
| 469 | return tdb->log.log_private;
|
|---|
| 470 | }
|
|---|
| 471 |
|
|---|
| 472 | /* reopen a tdb - this can be used after a fork to ensure that we have an independent
|
|---|
| 473 | seek pointer from our parent and to re-establish locks */
|
|---|
| 474 | int tdb_reopen(struct tdb_context *tdb)
|
|---|
| 475 | {
|
|---|
| 476 | struct stat st;
|
|---|
| 477 |
|
|---|
| 478 | TDB_LOG((tdb, TDB_DEBUG_TRACE, "tdb_reopen pid %d\n", getpid()));
|
|---|
| 479 |
|
|---|
| 480 | if (tdb->flags & TDB_INTERNAL) {
|
|---|
| 481 | return 0; /* Nothing to do. */
|
|---|
| 482 | }
|
|---|
| 483 |
|
|---|
| 484 | #ifdef __OS2__
|
|---|
| 485 |
|
|---|
| 486 | DosCloseMutexSem( tdb->hGlobalLock);
|
|---|
| 487 | tdb->hGlobalLock = 0;
|
|---|
| 488 | DosCloseMutexSem( tdb->hActiveLock);
|
|---|
| 489 | tdb->hActiveLock = 0;
|
|---|
| 490 | DosCloseMutexSem( tdb->hTransactionLock);
|
|---|
| 491 | tdb->hTransactionLock = 0;
|
|---|
| 492 |
|
|---|
| 493 | if (!(tdb->flags & TDB_INTERNAL))
|
|---|
| 494 | {
|
|---|
| 495 | char szSem[_MAX_PATH];
|
|---|
| 496 | char drive[_MAX_DRIVE], dir[_MAX_DIR];
|
|---|
| 497 | char fname[_MAX_FNAME], ext[_MAX_EXT];
|
|---|
| 498 | APIRET rc;
|
|---|
| 499 | // extract path info
|
|---|
| 500 | _splitpath( tdb->name, drive, dir, fname, ext);
|
|---|
| 501 | sprintf( szSem, "\\SEM32\\TDB_GL_%s%s%s", dir, fname, ext);
|
|---|
| 502 | rc = DosCreateMutexSem( szSem, &tdb->hGlobalLock, 0, FALSE);
|
|---|
| 503 | if (rc == ERROR_DUPLICATE_NAME)
|
|---|
| 504 | rc = DosOpenMutexSem( szSem, &tdb->hGlobalLock);
|
|---|
| 505 | if (rc != NO_ERROR) {
|
|---|
| 506 | printf( "cannot open %s %d\n", szSem, rc);
|
|---|
| 507 | errno = EINVAL;
|
|---|
| 508 | goto fail;
|
|---|
| 509 | }
|
|---|
| 510 | TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_reopen pid %d global handle %d\n", getpid(), tdb->hGlobalLock));
|
|---|
| 511 | sprintf( szSem, "\\SEM32\\TDB_AL_%s%s%s", dir, fname, ext);
|
|---|
| 512 | rc = DosCreateMutexSem( szSem, &tdb->hActiveLock, 0, FALSE);
|
|---|
| 513 | if (rc == ERROR_DUPLICATE_NAME)
|
|---|
| 514 | rc = DosOpenMutexSem( szSem, &tdb->hActiveLock);
|
|---|
| 515 | if (rc != NO_ERROR) {
|
|---|
| 516 | printf( "cannot open %s %d\n", szSem, rc);
|
|---|
| 517 | errno = EINVAL;
|
|---|
| 518 | goto fail;
|
|---|
| 519 | }
|
|---|
| 520 | TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_reopen pid %d active handle %d\n", getpid(), tdb->hActiveLock));
|
|---|
| 521 | sprintf( szSem, "\\SEM32\\TDB_TL_%s%s%s", dir, fname, ext);
|
|---|
| 522 | rc = DosCreateMutexSem( szSem, &tdb->hTransactionLock, 0, FALSE);
|
|---|
| 523 | if (rc == ERROR_DUPLICATE_NAME)
|
|---|
| 524 | rc = DosOpenMutexSem( szSem, &tdb->hTransactionLock);
|
|---|
| 525 | if (rc != NO_ERROR) {
|
|---|
| 526 | printf( "cannot open %s %d\n", szSem, rc);
|
|---|
| 527 | errno = EINVAL;
|
|---|
| 528 | goto fail;
|
|---|
| 529 | }
|
|---|
| 530 | TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_reopen pid %d transaction handle %d\n", getpid(), tdb->hTransactionLock));
|
|---|
| 531 | }
|
|---|
| 532 | #endif
|
|---|
| 533 |
|
|---|
| 534 | if (tdb->num_locks != 0 || tdb->global_lock.count) {
|
|---|
| 535 | TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_reopen: reopen not allowed with locks held\n"));
|
|---|
| 536 | goto fail;
|
|---|
| 537 | }
|
|---|
| 538 |
|
|---|
| 539 | if (tdb->transaction != 0) {
|
|---|
| 540 | TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_reopen: reopen not allowed inside a transaction\n"));
|
|---|
| 541 | goto fail;
|
|---|
| 542 | }
|
|---|
| 543 |
|
|---|
| 544 | if (tdb_munmap(tdb) != 0) {
|
|---|
| 545 | TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: munmap failed (%s)\n", strerror(errno)));
|
|---|
| 546 | goto fail;
|
|---|
| 547 | }
|
|---|
| 548 | if (close(tdb->fd) != 0)
|
|---|
| 549 | TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: WARNING closing tdb->fd failed!\n"));
|
|---|
| 550 | tdb->fd = open(tdb->name, tdb->open_flags & ~(O_CREAT|O_TRUNC), 0);
|
|---|
| 551 | if (tdb->fd == -1) {
|
|---|
| 552 | TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: open failed (%s)\n", strerror(errno)));
|
|---|
| 553 | goto fail;
|
|---|
| 554 | }
|
|---|
| 555 | if ((tdb->flags & TDB_CLEAR_IF_FIRST) &&
|
|---|
| 556 | (tdb->methods->tdb_brlock(tdb, ACTIVE_LOCK, F_RDLCK, F_SETLKW, 0, 1) == -1)) {
|
|---|
| 557 | // YD sem handle was wrong, testme #ifndef __OS2__ // PS 20070511 hack for WINS server crash
|
|---|
| 558 | TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: failed to obtain active lock\n"));
|
|---|
| 559 | goto fail;
|
|---|
| 560 | //#endif
|
|---|
| 561 | }
|
|---|
| 562 | if (fstat(tdb->fd, &st) != 0) {
|
|---|
| 563 | TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: fstat failed (%s)\n", strerror(errno)));
|
|---|
| 564 | goto fail;
|
|---|
| 565 | }
|
|---|
| 566 | if (st.st_ino != tdb->inode || st.st_dev != tdb->device) {
|
|---|
| 567 | TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: file dev/inode has changed!\n"));
|
|---|
| 568 | goto fail;
|
|---|
| 569 | }
|
|---|
| 570 | tdb_mmap(tdb);
|
|---|
| 571 |
|
|---|
| 572 | return 0;
|
|---|
| 573 |
|
|---|
| 574 | fail:
|
|---|
| 575 | tdb_close(tdb);
|
|---|
| 576 | return -1;
|
|---|
| 577 | }
|
|---|
| 578 |
|
|---|
| 579 | /* reopen all tdb's */
|
|---|
| 580 | int tdb_reopen_all(int parent_longlived)
|
|---|
| 581 | {
|
|---|
| 582 | struct tdb_context *tdb;
|
|---|
| 583 |
|
|---|
| 584 | for (tdb=tdbs; tdb; tdb = tdb->next) {
|
|---|
| 585 | /*
|
|---|
| 586 | * If the parent is longlived (ie. a
|
|---|
| 587 | * parent daemon architecture), we know
|
|---|
| 588 | * it will keep it's active lock on a
|
|---|
| 589 | * tdb opened with CLEAR_IF_FIRST. Thus
|
|---|
| 590 | * for child processes we don't have to
|
|---|
| 591 | * add an active lock. This is essential
|
|---|
| 592 | * to improve performance on systems that
|
|---|
| 593 | * keep POSIX locks as a non-scalable data
|
|---|
| 594 | * structure in the kernel.
|
|---|
| 595 | */
|
|---|
| 596 | if (parent_longlived) {
|
|---|
| 597 | /* Ensure no clear-if-first. */
|
|---|
| 598 | tdb->flags &= ~TDB_CLEAR_IF_FIRST;
|
|---|
| 599 | }
|
|---|
| 600 |
|
|---|
| 601 | if (tdb_reopen(tdb) != 0)
|
|---|
| 602 | return -1;
|
|---|
| 603 | }
|
|---|
| 604 |
|
|---|
| 605 | return 0;
|
|---|
| 606 | }
|
|---|