open.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*
  2. Unix SMB/CIFS implementation.
  3. trivial database library
  4. Copyright (C) Andrew Tridgell 1999-2005
  5. Copyright (C) Paul `Rusty' Russell 2000
  6. Copyright (C) Jeremy Allison 2000-2003
  7. ** NOTE! The following LGPL license applies to the tdb
  8. ** library. This does NOT imply that all of Samba is released
  9. ** under the LGPL
  10. This library is free software; you can redistribute it and/or
  11. modify it under the terms of the GNU Lesser General Public
  12. License as published by the Free Software Foundation; either
  13. version 3 of the License, or (at your option) any later version.
  14. This library is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. Lesser General Public License for more details.
  18. You should have received a copy of the GNU Lesser General Public
  19. License along with this library; if not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include "tdb_private.h"
  22. /* all contexts, to ensure no double-opens (fcntl locks don't nest!) */
  23. static struct tdb_context *tdbs = NULL;
  24. /* This is based on the hash algorithm from gdbm */
  25. static unsigned int default_tdb_hash(TDB_DATA *key)
  26. {
  27. uint32_t value; /* Used to compute the hash value. */
  28. uint32_t i; /* Used to cycle through random values. */
  29. /* Set the initial value from the key size. */
  30. for (value = 0x238F13AF * key->dsize, i=0; i < key->dsize; i++)
  31. value = (value + (key->dptr[i] << (i*5 % 24)));
  32. return (1103515243 * value + 12345);
  33. }
  34. /* initialise a new database with a specified hash size */
  35. static int tdb_new_database(struct tdb_context *tdb, int hash_size)
  36. {
  37. struct tdb_header *newdb;
  38. size_t size;
  39. int ret = -1;
  40. ssize_t written;
  41. /* We make it up in memory, then write it out if not internal */
  42. size = sizeof(struct tdb_header) + (hash_size+1)*sizeof(tdb_off_t);
  43. if (!(newdb = (struct tdb_header *)calloc(size, 1))) {
  44. tdb->ecode = TDB_ERR_OOM;
  45. return -1;
  46. }
  47. /* Fill in the header */
  48. newdb->version = TDB_VERSION;
  49. newdb->hash_size = hash_size;
  50. if (tdb->flags & TDB_INTERNAL) {
  51. tdb->map_size = size;
  52. tdb->map_ptr = (char *)newdb;
  53. memcpy(&tdb->header, newdb, sizeof(tdb->header));
  54. /* Convert the `ondisk' version if asked. */
  55. CONVERT(*newdb);
  56. return 0;
  57. }
  58. if (lseek(tdb->fd, 0, SEEK_SET) == -1)
  59. goto fail;
  60. if (ftruncate(tdb->fd, 0) == -1)
  61. goto fail;
  62. /* This creates an endian-converted header, as if read from disk */
  63. CONVERT(*newdb);
  64. memcpy(&tdb->header, newdb, sizeof(tdb->header));
  65. /* Don't endian-convert the magic food! */
  66. memcpy(newdb->magic_food, TDB_MAGIC_FOOD, strlen(TDB_MAGIC_FOOD)+1);
  67. /* we still have "ret == -1" here */
  68. written = write(tdb->fd, newdb, size);
  69. if (written == size) {
  70. ret = 0;
  71. } else if (written != -1) {
  72. /* call write once again, this usually should return -1 and
  73. * set errno appropriately */
  74. size -= written;
  75. written = write(tdb->fd, newdb+written, size);
  76. if (written == size) {
  77. ret = 0;
  78. } else if (written >= 0) {
  79. /* a second incomplete write - we give up.
  80. * guessing the errno... */
  81. errno = ENOSPC;
  82. }
  83. }
  84. fail:
  85. SAFE_FREE(newdb);
  86. return ret;
  87. }
  88. static int tdb_already_open(dev_t device,
  89. ino_t ino)
  90. {
  91. struct tdb_context *i;
  92. for (i = tdbs; i; i = i->next) {
  93. if (i->device == device && i->inode == ino) {
  94. return 1;
  95. }
  96. }
  97. return 0;
  98. }
  99. /* open the database, creating it if necessary
  100. The open_flags and mode are passed straight to the open call on the
  101. database file. A flags value of O_WRONLY is invalid. The hash size
  102. is advisory, use zero for a default value.
  103. Return is NULL on error, in which case errno is also set. Don't
  104. try to call tdb_error or tdb_errname, just do strerror(errno).
  105. @param name may be NULL for internal databases. */
  106. struct tdb_context *tdb_open(const char *name, int hash_size, int tdb_flags,
  107. int open_flags, mode_t mode)
  108. {
  109. return tdb_open_ex(name, hash_size, tdb_flags, open_flags, mode, NULL, NULL);
  110. }
  111. /* a default logging function */
  112. static void null_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
  113. static void null_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...)
  114. {
  115. }
  116. struct tdb_context *tdb_open_ex(const char *name, int hash_size, int tdb_flags,
  117. int open_flags, mode_t mode,
  118. const struct tdb_logging_context *log_ctx,
  119. tdb_hash_func hash_fn)
  120. {
  121. struct tdb_context *tdb;
  122. struct stat st;
  123. int rev = 0, locked = 0;
  124. unsigned char *vp;
  125. uint32_t vertest;
  126. unsigned v;
  127. if (!(tdb = (struct tdb_context *)calloc(1, sizeof *tdb))) {
  128. /* Can't log this */
  129. errno = ENOMEM;
  130. goto fail;
  131. }
  132. tdb_io_init(tdb);
  133. tdb->fd = -1;
  134. #ifdef TDB_TRACE
  135. tdb->tracefd = -1;
  136. #endif
  137. tdb->name = NULL;
  138. tdb->map_ptr = NULL;
  139. tdb->flags = tdb_flags;
  140. tdb->open_flags = open_flags;
  141. if (log_ctx) {
  142. tdb->log = *log_ctx;
  143. } else {
  144. tdb->log.log_fn = null_log_fn;
  145. tdb->log.log_private = NULL;
  146. }
  147. tdb->hash_fn = hash_fn ? hash_fn : default_tdb_hash;
  148. /* cache the page size */
  149. tdb->page_size = getpagesize();
  150. if (tdb->page_size <= 0) {
  151. tdb->page_size = 0x2000;
  152. }
  153. tdb->max_dead_records = (tdb_flags & TDB_VOLATILE) ? 5 : 0;
  154. if ((open_flags & O_ACCMODE) == O_WRONLY) {
  155. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: can't open tdb %s write-only\n",
  156. name));
  157. errno = EINVAL;
  158. goto fail;
  159. }
  160. if (hash_size == 0)
  161. hash_size = DEFAULT_HASH_SIZE;
  162. if ((open_flags & O_ACCMODE) == O_RDONLY) {
  163. tdb->read_only = 1;
  164. /* read only databases don't do locking or clear if first */
  165. tdb->flags |= TDB_NOLOCK;
  166. tdb->flags &= ~TDB_CLEAR_IF_FIRST;
  167. }
  168. if ((tdb->flags & TDB_ALLOW_NESTING) &&
  169. (tdb->flags & TDB_DISALLOW_NESTING)) {
  170. tdb->ecode = TDB_ERR_NESTING;
  171. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_open_ex: "
  172. "allow_nesting and disallow_nesting are not allowed together!"));
  173. errno = EINVAL;
  174. goto fail;
  175. }
  176. /*
  177. * TDB_DISALLOW_NESTING is the default behavior.
  178. */
  179. if (!(tdb->flags & TDB_ALLOW_NESTING)) {
  180. tdb->flags |= TDB_DISALLOW_NESTING;
  181. }
  182. /* internal databases don't mmap or lock, and start off cleared */
  183. if (tdb->flags & TDB_INTERNAL) {
  184. tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
  185. tdb->flags &= ~TDB_CLEAR_IF_FIRST;
  186. if (tdb_new_database(tdb, hash_size) != 0) {
  187. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: tdb_new_database failed!"));
  188. goto fail;
  189. }
  190. goto internal;
  191. }
  192. if ((tdb->fd = open(name, open_flags, mode)) == -1) {
  193. TDB_LOG((tdb, TDB_DEBUG_WARNING, "tdb_open_ex: could not open file %s: %s\n",
  194. name, strerror(errno)));
  195. goto fail; /* errno set by open(2) */
  196. }
  197. /* on exec, don't inherit the fd */
  198. v = fcntl(tdb->fd, F_GETFD, 0);
  199. fcntl(tdb->fd, F_SETFD, v | FD_CLOEXEC);
  200. /* ensure there is only one process initialising at once */
  201. if (tdb->methods->brlock(tdb, F_WRLCK, GLOBAL_LOCK, 1, TDB_LOCK_WAIT) == -1) {
  202. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: failed to get global lock on %s: %s\n",
  203. name, strerror(errno)));
  204. goto fail; /* errno set by tdb_brlock */
  205. }
  206. /* we need to zero database if we are the only one with it open */
  207. if ((tdb_flags & TDB_CLEAR_IF_FIRST) &&
  208. (!tdb->read_only) &&
  209. (locked = (tdb->methods->brlock(tdb, F_WRLCK, ACTIVE_LOCK, 1, TDB_LOCK_NOWAIT|TDB_LOCK_PROBE) == 0))) {
  210. open_flags |= O_CREAT;
  211. if (ftruncate(tdb->fd, 0) == -1) {
  212. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_open_ex: "
  213. "failed to truncate %s: %s\n",
  214. name, strerror(errno)));
  215. goto fail; /* errno set by ftruncate */
  216. }
  217. }
  218. errno = 0;
  219. if (read(tdb->fd, &tdb->header, sizeof(tdb->header)) != sizeof(tdb->header)
  220. || strcmp(tdb->header.magic_food, TDB_MAGIC_FOOD) != 0) {
  221. if (!(open_flags & O_CREAT) || tdb_new_database(tdb, hash_size) == -1) {
  222. if (errno == 0) {
  223. errno = EIO; /* ie bad format or something */
  224. }
  225. goto fail;
  226. }
  227. rev = (tdb->flags & TDB_CONVERT);
  228. } else if (tdb->header.version != TDB_VERSION
  229. && !(rev = (tdb->header.version==TDB_BYTEREV(TDB_VERSION)))) {
  230. /* wrong version */
  231. errno = EIO;
  232. goto fail;
  233. }
  234. vp = (unsigned char *)&tdb->header.version;
  235. vertest = (((uint32_t)vp[0]) << 24) | (((uint32_t)vp[1]) << 16) |
  236. (((uint32_t)vp[2]) << 8) | (uint32_t)vp[3];
  237. tdb->flags |= (vertest==TDB_VERSION) ? TDB_BIGENDIAN : 0;
  238. if (!rev)
  239. tdb->flags &= ~TDB_CONVERT;
  240. else {
  241. tdb->flags |= TDB_CONVERT;
  242. tdb_convert(&tdb->header, sizeof(tdb->header));
  243. }
  244. if (fstat(tdb->fd, &st) == -1)
  245. goto fail;
  246. if (tdb->header.rwlocks != 0) {
  247. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: spinlocks no longer supported\n"));
  248. goto fail;
  249. }
  250. /* Is it already in the open list? If so, fail. */
  251. if (tdb_already_open(st.st_dev, st.st_ino)) {
  252. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: "
  253. "%s (%d,%d) is already open in this process\n",
  254. name, (int)st.st_dev, (int)st.st_ino));
  255. errno = EBUSY;
  256. goto fail;
  257. }
  258. if (!(tdb->name = (char *)strdup(name))) {
  259. errno = ENOMEM;
  260. goto fail;
  261. }
  262. tdb->map_size = st.st_size;
  263. tdb->device = st.st_dev;
  264. tdb->inode = st.st_ino;
  265. tdb_mmap(tdb);
  266. if (locked) {
  267. if (tdb->methods->brunlock(tdb, F_WRLCK, ACTIVE_LOCK, 1) == -1) {
  268. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: "
  269. "failed to take ACTIVE_LOCK on %s: %s\n",
  270. name, strerror(errno)));
  271. goto fail;
  272. }
  273. }
  274. /* We always need to do this if the CLEAR_IF_FIRST flag is set, even if
  275. we didn't get the initial exclusive lock as we need to let all other
  276. users know we're using it. */
  277. if (tdb_flags & TDB_CLEAR_IF_FIRST) {
  278. /* leave this lock in place to indicate it's in use */
  279. if (tdb->methods->brlock(tdb, F_RDLCK, ACTIVE_LOCK, 1, TDB_LOCK_WAIT) == -1)
  280. goto fail;
  281. }
  282. /* if needed, run recovery */
  283. if (tdb_transaction_recover(tdb) == -1) {
  284. goto fail;
  285. }
  286. #ifdef TDB_TRACE
  287. {
  288. char tracefile[strlen(name) + 32];
  289. snprintf(tracefile, sizeof(tracefile),
  290. "%s.trace.%li", name, (long)getpid());
  291. tdb->tracefd = open(tracefile, O_WRONLY|O_CREAT|O_EXCL, 0600);
  292. if (tdb->tracefd >= 0) {
  293. tdb_enable_seqnum(tdb);
  294. tdb_trace_open(tdb, "tdb_open", hash_size, tdb_flags,
  295. open_flags);
  296. } else
  297. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: failed to open trace file %s!\n", tracefile));
  298. }
  299. #endif
  300. internal:
  301. /* Internal (memory-only) databases skip all the code above to
  302. * do with disk files, and resume here by releasing their
  303. * global lock and hooking into the active list. */
  304. if (tdb->methods->brunlock(tdb, F_WRLCK, GLOBAL_LOCK, 1) == -1)
  305. goto fail;
  306. tdb->next = tdbs;
  307. tdbs = tdb;
  308. return tdb;
  309. fail:
  310. { int save_errno = errno;
  311. if (!tdb)
  312. return NULL;
  313. #ifdef TDB_TRACE
  314. close(tdb->tracefd);
  315. #endif
  316. if (tdb->map_ptr) {
  317. if (tdb->flags & TDB_INTERNAL)
  318. SAFE_FREE(tdb->map_ptr);
  319. else
  320. tdb_munmap(tdb);
  321. }
  322. SAFE_FREE(tdb->name);
  323. if (tdb->fd != -1)
  324. if (close(tdb->fd) != 0)
  325. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: failed to close tdb->fd on error!\n"));
  326. SAFE_FREE(tdb);
  327. errno = save_errno;
  328. return NULL;
  329. }
  330. }
  331. /*
  332. * Set the maximum number of dead records per hash chain
  333. */
  334. void tdb_set_max_dead(struct tdb_context *tdb, int max_dead)
  335. {
  336. tdb->max_dead_records = max_dead;
  337. }
  338. /**
  339. * Close a database.
  340. *
  341. * @returns -1 for error; 0 for success.
  342. **/
  343. int tdb_close(struct tdb_context *tdb)
  344. {
  345. struct tdb_context **i;
  346. int ret = 0;
  347. if (tdb->transaction) {
  348. tdb_transaction_cancel(tdb);
  349. }
  350. tdb_trace(tdb, "tdb_close");
  351. if (tdb->map_ptr) {
  352. if (tdb->flags & TDB_INTERNAL)
  353. SAFE_FREE(tdb->map_ptr);
  354. else
  355. tdb_munmap(tdb);
  356. }
  357. SAFE_FREE(tdb->name);
  358. if (tdb->fd != -1) {
  359. ret = close(tdb->fd);
  360. tdb->fd = -1;
  361. }
  362. SAFE_FREE(tdb->lockrecs);
  363. /* Remove from contexts list */
  364. for (i = &tdbs; *i; i = &(*i)->next) {
  365. if (*i == tdb) {
  366. *i = tdb->next;
  367. break;
  368. }
  369. }
  370. #ifdef TDB_TRACE
  371. close(tdb->tracefd);
  372. #endif
  373. memset(tdb, 0, sizeof(*tdb));
  374. SAFE_FREE(tdb);
  375. return ret;
  376. }
  377. /* register a loging function */
  378. void tdb_set_logging_function(struct tdb_context *tdb,
  379. const struct tdb_logging_context *log_ctx)
  380. {
  381. tdb->log = *log_ctx;
  382. }
  383. void *tdb_get_logging_private(struct tdb_context *tdb)
  384. {
  385. return tdb->log.log_private;
  386. }
  387. static int tdb_reopen_internal(struct tdb_context *tdb, bool active_lock)
  388. {
  389. struct stat st;
  390. if (tdb->flags & TDB_INTERNAL) {
  391. return 0; /* Nothing to do. */
  392. }
  393. if (tdb->num_locks != 0 || tdb->global_lock.count) {
  394. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_reopen: reopen not allowed with locks held\n"));
  395. goto fail;
  396. }
  397. if (tdb->transaction != 0) {
  398. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_reopen: reopen not allowed inside a transaction\n"));
  399. goto fail;
  400. }
  401. if (tdb_munmap(tdb) != 0) {
  402. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: munmap failed (%s)\n", strerror(errno)));
  403. goto fail;
  404. }
  405. if (close(tdb->fd) != 0)
  406. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: WARNING closing tdb->fd failed!\n"));
  407. tdb->fd = open(tdb->name, tdb->open_flags & ~(O_CREAT|O_TRUNC), 0);
  408. if (tdb->fd == -1) {
  409. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: open failed (%s)\n", strerror(errno)));
  410. goto fail;
  411. }
  412. if (fstat(tdb->fd, &st) != 0) {
  413. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: fstat failed (%s)\n", strerror(errno)));
  414. goto fail;
  415. }
  416. if (st.st_ino != tdb->inode || st.st_dev != tdb->device) {
  417. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: file dev/inode has changed!\n"));
  418. goto fail;
  419. }
  420. tdb_mmap(tdb);
  421. if (active_lock &&
  422. (tdb->methods->brlock(tdb, F_RDLCK, ACTIVE_LOCK, 1, TDB_LOCK_WAIT) == -1)) {
  423. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: failed to obtain active lock\n"));
  424. goto fail;
  425. }
  426. return 0;
  427. fail:
  428. tdb_close(tdb);
  429. return -1;
  430. }
  431. /* reopen a tdb - this can be used after a fork to ensure that we have an independent
  432. seek pointer from our parent and to re-establish locks */
  433. int tdb_reopen(struct tdb_context *tdb)
  434. {
  435. return tdb_reopen_internal(tdb, tdb->flags & TDB_CLEAR_IF_FIRST);
  436. }
  437. /* reopen all tdb's */
  438. int tdb_reopen_all(int parent_longlived)
  439. {
  440. struct tdb_context *tdb;
  441. for (tdb=tdbs; tdb; tdb = tdb->next) {
  442. bool active_lock = (tdb->flags & TDB_CLEAR_IF_FIRST);
  443. /*
  444. * If the parent is longlived (ie. a
  445. * parent daemon architecture), we know
  446. * it will keep it's active lock on a
  447. * tdb opened with CLEAR_IF_FIRST. Thus
  448. * for child processes we don't have to
  449. * add an active lock. This is essential
  450. * to improve performance on systems that
  451. * keep POSIX locks as a non-scalable data
  452. * structure in the kernel.
  453. */
  454. if (parent_longlived) {
  455. /* Ensure no clear-if-first. */
  456. active_lock = false;
  457. }
  458. if (tdb_reopen_internal(tdb, active_lock) != 0)
  459. return -1;
  460. }
  461. return 0;
  462. }