PageRenderTime 22ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/release/src/router/samba3/source/tdb/common/open.c

https://gitlab.com/envieidoc/tomato
C | 482 lines | 325 code | 64 blank | 93 comment | 112 complexity | 6bdaf1f8c835e188883f5b6bda2c11f0 MD5 | raw file
  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 2 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, write to the Free Software
  20. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include "tdb_private.h"
  23. /* all contexts, to ensure no double-opens (fcntl locks don't nest!) */
  24. static struct tdb_context *tdbs = NULL;
  25. /* This is based on the hash algorithm from gdbm */
  26. static unsigned int default_tdb_hash(TDB_DATA *key)
  27. {
  28. u32 value; /* Used to compute the hash value. */
  29. u32 i; /* Used to cycle through random values. */
  30. /* Set the initial value from the key size. */
  31. for (value = 0x238F13AF * key->dsize, i=0; i < key->dsize; i++)
  32. value = (value + (key->dptr[i] << (i*5 % 24)));
  33. return (1103515243 * value + 12345);
  34. }
  35. /* initialise a new database with a specified hash size */
  36. static int tdb_new_database(struct tdb_context *tdb, int hash_size)
  37. {
  38. struct tdb_header *newdb;
  39. size_t size;
  40. int ret = -1;
  41. ssize_t written;
  42. /* We make it up in memory, then write it out if not internal */
  43. size = sizeof(struct tdb_header) + (hash_size+1)*sizeof(tdb_off_t);
  44. if (!(newdb = (struct tdb_header *)calloc(size, 1)))
  45. return TDB_ERRCODE(TDB_ERR_OOM, -1);
  46. /* Fill in the header */
  47. newdb->version = TDB_VERSION;
  48. newdb->hash_size = hash_size;
  49. if (tdb->flags & TDB_INTERNAL) {
  50. tdb->map_size = size;
  51. tdb->map_ptr = (char *)newdb;
  52. memcpy(&tdb->header, newdb, sizeof(tdb->header));
  53. /* Convert the `ondisk' version if asked. */
  54. CONVERT(*newdb);
  55. return 0;
  56. }
  57. if (lseek(tdb->fd, 0, SEEK_SET) == -1)
  58. goto fail;
  59. if (ftruncate(tdb->fd, 0) == -1)
  60. goto fail;
  61. /* This creates an endian-converted header, as if read from disk */
  62. CONVERT(*newdb);
  63. memcpy(&tdb->header, newdb, sizeof(tdb->header));
  64. /* Don't endian-convert the magic food! */
  65. memcpy(newdb->magic_food, TDB_MAGIC_FOOD, strlen(TDB_MAGIC_FOOD)+1);
  66. /* we still have "ret == -1" here */
  67. written = write(tdb->fd, newdb, size);
  68. if (written == size) {
  69. ret = 0;
  70. } else if (written != -1) {
  71. /* call write once again, this usually should return -1 and
  72. * set errno appropriately */
  73. size -= written;
  74. written = write(tdb->fd, newdb+written, size);
  75. if (written == size) {
  76. ret = 0;
  77. } else if (written >= 0) {
  78. /* a second incomplete write - we give up.
  79. * guessing the errno... */
  80. errno = ENOSPC;
  81. }
  82. }
  83. fail:
  84. SAFE_FREE(newdb);
  85. return ret;
  86. }
  87. static int tdb_already_open(dev_t device,
  88. ino_t ino)
  89. {
  90. struct tdb_context *i;
  91. for (i = tdbs; i; i = i->next) {
  92. if (i->device == device && i->inode == ino) {
  93. return 1;
  94. }
  95. }
  96. return 0;
  97. }
  98. /* open the database, creating it if necessary
  99. The open_flags and mode are passed straight to the open call on the
  100. database file. A flags value of O_WRONLY is invalid. The hash size
  101. is advisory, use zero for a default value.
  102. Return is NULL on error, in which case errno is also set. Don't
  103. try to call tdb_error or tdb_errname, just do strerror(errno).
  104. @param name may be NULL for internal databases. */
  105. struct tdb_context *tdb_open(const char *name, int hash_size, int tdb_flags,
  106. int open_flags, mode_t mode)
  107. {
  108. return tdb_open_ex(name, hash_size, tdb_flags, open_flags, mode, NULL, NULL);
  109. }
  110. /* a default logging function */
  111. static void null_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
  112. static void null_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...)
  113. {
  114. }
  115. struct tdb_context *tdb_open_ex(const char *name, int hash_size, int tdb_flags,
  116. int open_flags, mode_t mode,
  117. const struct tdb_logging_context *log_ctx,
  118. tdb_hash_func hash_fn)
  119. {
  120. struct tdb_context *tdb;
  121. struct stat st;
  122. int rev = 0, locked = 0;
  123. unsigned char *vp;
  124. u32 vertest;
  125. if (!(tdb = (struct tdb_context *)calloc(1, sizeof *tdb))) {
  126. /* Can't log this */
  127. errno = ENOMEM;
  128. goto fail;
  129. }
  130. tdb_io_init(tdb);
  131. tdb->fd = -1;
  132. tdb->name = NULL;
  133. tdb->map_ptr = NULL;
  134. tdb->flags = tdb_flags;
  135. tdb->open_flags = open_flags;
  136. if (log_ctx) {
  137. tdb->log = *log_ctx;
  138. } else {
  139. tdb->log.log_fn = null_log_fn;
  140. tdb->log.log_private = NULL;
  141. }
  142. tdb->hash_fn = hash_fn ? hash_fn : default_tdb_hash;
  143. /* cache the page size */
  144. tdb->page_size = getpagesize();
  145. if (tdb->page_size <= 0) {
  146. tdb->page_size = 0x2000;
  147. }
  148. if ((open_flags & O_ACCMODE) == O_WRONLY) {
  149. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: can't open tdb %s write-only\n",
  150. name));
  151. errno = EINVAL;
  152. goto fail;
  153. }
  154. if (hash_size == 0)
  155. hash_size = DEFAULT_HASH_SIZE;
  156. if ((open_flags & O_ACCMODE) == O_RDONLY) {
  157. tdb->read_only = 1;
  158. /* read only databases don't do locking or clear if first */
  159. tdb->flags |= TDB_NOLOCK;
  160. tdb->flags &= ~TDB_CLEAR_IF_FIRST;
  161. }
  162. /* internal databases don't mmap or lock, and start off cleared */
  163. if (tdb->flags & TDB_INTERNAL) {
  164. tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
  165. tdb->flags &= ~TDB_CLEAR_IF_FIRST;
  166. if (tdb_new_database(tdb, hash_size) != 0) {
  167. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: tdb_new_database failed!"));
  168. goto fail;
  169. }
  170. goto internal;
  171. }
  172. if ((tdb->fd = open(name, open_flags, mode)) == -1) {
  173. TDB_LOG((tdb, TDB_DEBUG_WARNING, "tdb_open_ex: could not open file %s: %s\n",
  174. name, strerror(errno)));
  175. goto fail; /* errno set by open(2) */
  176. }
  177. /* ensure there is only one process initialising at once */
  178. if (tdb->methods->tdb_brlock(tdb, GLOBAL_LOCK, F_WRLCK, F_SETLKW, 0, 1) == -1) {
  179. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: failed to get global lock on %s: %s\n",
  180. name, strerror(errno)));
  181. goto fail; /* errno set by tdb_brlock */
  182. }
  183. /* we need to zero database if we are the only one with it open */
  184. if ((tdb_flags & TDB_CLEAR_IF_FIRST) &&
  185. (locked = (tdb->methods->tdb_brlock(tdb, ACTIVE_LOCK, F_WRLCK, F_SETLK, 0, 1) == 0))) {
  186. open_flags |= O_CREAT;
  187. if (ftruncate(tdb->fd, 0) == -1) {
  188. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_open_ex: "
  189. "failed to truncate %s: %s\n",
  190. name, strerror(errno)));
  191. goto fail; /* errno set by ftruncate */
  192. }
  193. }
  194. errno = 0;
  195. if (read(tdb->fd, &tdb->header, sizeof(tdb->header)) != sizeof(tdb->header)
  196. || strcmp(tdb->header.magic_food, TDB_MAGIC_FOOD) != 0
  197. || (tdb->header.version != TDB_VERSION
  198. && !(rev = (tdb->header.version==TDB_BYTEREV(TDB_VERSION))))) {
  199. /* its not a valid database - possibly initialise it */
  200. if (!(open_flags & O_CREAT) || tdb_new_database(tdb, hash_size) == -1) {
  201. if (errno == 0) {
  202. errno = EIO; /* ie bad format or something */
  203. }
  204. goto fail;
  205. }
  206. rev = (tdb->flags & TDB_CONVERT);
  207. }
  208. vp = (unsigned char *)&tdb->header.version;
  209. vertest = (((u32)vp[0]) << 24) | (((u32)vp[1]) << 16) |
  210. (((u32)vp[2]) << 8) | (u32)vp[3];
  211. tdb->flags |= (vertest==TDB_VERSION) ? TDB_BIGENDIAN : 0;
  212. if (!rev)
  213. tdb->flags &= ~TDB_CONVERT;
  214. else {
  215. tdb->flags |= TDB_CONVERT;
  216. tdb_convert(&tdb->header, sizeof(tdb->header));
  217. }
  218. if (fstat(tdb->fd, &st) == -1)
  219. goto fail;
  220. if (tdb->header.rwlocks != 0) {
  221. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: spinlocks no longer supported\n"));
  222. goto fail;
  223. }
  224. /* Is it already in the open list? If so, fail. */
  225. if (tdb_already_open(st.st_dev, st.st_ino)) {
  226. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: "
  227. "%s (%d,%d) is already open in this process\n",
  228. name, (int)st.st_dev, (int)st.st_ino));
  229. errno = EBUSY;
  230. goto fail;
  231. }
  232. if (!(tdb->name = (char *)strdup(name))) {
  233. errno = ENOMEM;
  234. goto fail;
  235. }
  236. tdb->map_size = st.st_size;
  237. tdb->device = st.st_dev;
  238. tdb->inode = st.st_ino;
  239. tdb->max_dead_records = 0;
  240. tdb_mmap(tdb);
  241. if (locked) {
  242. if (tdb->methods->tdb_brlock(tdb, ACTIVE_LOCK, F_UNLCK, F_SETLK, 0, 1) == -1) {
  243. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: "
  244. "failed to take ACTIVE_LOCK on %s: %s\n",
  245. name, strerror(errno)));
  246. goto fail;
  247. }
  248. }
  249. /* We always need to do this if the CLEAR_IF_FIRST flag is set, even if
  250. we didn't get the initial exclusive lock as we need to let all other
  251. users know we're using it. */
  252. if (tdb_flags & TDB_CLEAR_IF_FIRST) {
  253. /* leave this lock in place to indicate it's in use */
  254. if (tdb->methods->tdb_brlock(tdb, ACTIVE_LOCK, F_RDLCK, F_SETLKW, 0, 1) == -1)
  255. goto fail;
  256. }
  257. /* if needed, run recovery */
  258. if (tdb_transaction_recover(tdb) == -1) {
  259. goto fail;
  260. }
  261. internal:
  262. /* Internal (memory-only) databases skip all the code above to
  263. * do with disk files, and resume here by releasing their
  264. * global lock and hooking into the active list. */
  265. if (tdb->methods->tdb_brlock(tdb, GLOBAL_LOCK, F_UNLCK, F_SETLKW, 0, 1) == -1)
  266. goto fail;
  267. tdb->next = tdbs;
  268. tdbs = tdb;
  269. return tdb;
  270. fail:
  271. { int save_errno = errno;
  272. if (!tdb)
  273. return NULL;
  274. if (tdb->map_ptr) {
  275. if (tdb->flags & TDB_INTERNAL)
  276. SAFE_FREE(tdb->map_ptr);
  277. else
  278. tdb_munmap(tdb);
  279. }
  280. SAFE_FREE(tdb->name);
  281. if (tdb->fd != -1)
  282. if (close(tdb->fd) != 0)
  283. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: failed to close tdb->fd on error!\n"));
  284. SAFE_FREE(tdb);
  285. errno = save_errno;
  286. return NULL;
  287. }
  288. }
  289. /*
  290. * Set the maximum number of dead records per hash chain
  291. */
  292. void tdb_set_max_dead(struct tdb_context *tdb, int max_dead)
  293. {
  294. tdb->max_dead_records = max_dead;
  295. }
  296. /**
  297. * Close a database.
  298. *
  299. * @returns -1 for error; 0 for success.
  300. **/
  301. int tdb_close(struct tdb_context *tdb)
  302. {
  303. struct tdb_context **i;
  304. int ret = 0;
  305. if (tdb->transaction) {
  306. tdb_transaction_cancel(tdb);
  307. }
  308. if (tdb->map_ptr) {
  309. if (tdb->flags & TDB_INTERNAL)
  310. SAFE_FREE(tdb->map_ptr);
  311. else
  312. tdb_munmap(tdb);
  313. }
  314. SAFE_FREE(tdb->name);
  315. if (tdb->fd != -1)
  316. ret = close(tdb->fd);
  317. SAFE_FREE(tdb->lockrecs);
  318. /* Remove from contexts list */
  319. for (i = &tdbs; *i; i = &(*i)->next) {
  320. if (*i == tdb) {
  321. *i = tdb->next;
  322. break;
  323. }
  324. }
  325. memset(tdb, 0, sizeof(*tdb));
  326. SAFE_FREE(tdb);
  327. return ret;
  328. }
  329. /* register a loging function */
  330. void tdb_set_logging_function(struct tdb_context *tdb,
  331. const struct tdb_logging_context *log_ctx)
  332. {
  333. tdb->log = *log_ctx;
  334. }
  335. void *tdb_get_logging_private(struct tdb_context *tdb)
  336. {
  337. return tdb->log.log_private;
  338. }
  339. /* reopen a tdb - this can be used after a fork to ensure that we have an independent
  340. seek pointer from our parent and to re-establish locks */
  341. int tdb_reopen(struct tdb_context *tdb)
  342. {
  343. struct stat st;
  344. if (tdb->flags & TDB_INTERNAL) {
  345. return 0; /* Nothing to do. */
  346. }
  347. if (tdb->num_locks != 0 || tdb->global_lock.count) {
  348. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_reopen: reopen not allowed with locks held\n"));
  349. goto fail;
  350. }
  351. if (tdb->transaction != 0) {
  352. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_reopen: reopen not allowed inside a transaction\n"));
  353. goto fail;
  354. }
  355. if (tdb_munmap(tdb) != 0) {
  356. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: munmap failed (%s)\n", strerror(errno)));
  357. goto fail;
  358. }
  359. if (close(tdb->fd) != 0)
  360. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: WARNING closing tdb->fd failed!\n"));
  361. tdb->fd = open(tdb->name, tdb->open_flags & ~(O_CREAT|O_TRUNC), 0);
  362. if (tdb->fd == -1) {
  363. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: open failed (%s)\n", strerror(errno)));
  364. goto fail;
  365. }
  366. if ((tdb->flags & TDB_CLEAR_IF_FIRST) &&
  367. (tdb->methods->tdb_brlock(tdb, ACTIVE_LOCK, F_RDLCK, F_SETLKW, 0, 1) == -1)) {
  368. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: failed to obtain active lock\n"));
  369. goto fail;
  370. }
  371. if (fstat(tdb->fd, &st) != 0) {
  372. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: fstat failed (%s)\n", strerror(errno)));
  373. goto fail;
  374. }
  375. if (st.st_ino != tdb->inode || st.st_dev != tdb->device) {
  376. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: file dev/inode has changed!\n"));
  377. goto fail;
  378. }
  379. tdb_mmap(tdb);
  380. return 0;
  381. fail:
  382. tdb_close(tdb);
  383. return -1;
  384. }
  385. /* reopen all tdb's */
  386. int tdb_reopen_all(int parent_longlived)
  387. {
  388. struct tdb_context *tdb;
  389. for (tdb=tdbs; tdb; tdb = tdb->next) {
  390. /*
  391. * If the parent is longlived (ie. a
  392. * parent daemon architecture), we know
  393. * it will keep it's active lock on a
  394. * tdb opened with CLEAR_IF_FIRST. Thus
  395. * for child processes we don't have to
  396. * add an active lock. This is essential
  397. * to improve performance on systems that
  398. * keep POSIX locks as a non-scalable data
  399. * structure in the kernel.
  400. */
  401. if (parent_longlived) {
  402. /* Ensure no clear-if-first. */
  403. tdb->flags &= ~TDB_CLEAR_IF_FIRST;
  404. }
  405. if (tdb_reopen(tdb) != 0)
  406. return -1;
  407. }
  408. return 0;
  409. }