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