2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997
5 * Sleepycat Software. All rights reserved.
11 static const char sccsid[] = "@(#)db_appinit.c 10.33 (Sleepycat) 8/28/97";
14 #ifndef NO_SYSTEM_INCLUDES
15 #include <sys/param.h>
35 #include "common_ext.h"
37 static int __db_home __P((DB_ENV *, const char *, int));
38 static int __db_parse __P((DB_ENV *, char *));
39 static int __db_tmp_dir __P((DB_ENV *, int));
40 static int __db_tmp_open __P((DB_ENV *, char *, int *));
44 * Return verision information.
47 db_version(majverp, minverp, patchp)
48 int *majverp, *minverp, *patchp;
51 *majverp = DB_VERSION_MAJOR;
53 *minverp = DB_VERSION_MINOR;
55 *patchp = DB_VERSION_PATCH;
56 return ((char *)DB_VERSION_STRING);
61 * Initialize the application environment.
64 db_appinit(db_home, db_config, dbenv, flags)
66 char * const *db_config;
72 char *lp, **p, buf[MAXPATHLEN * 2];
74 /* Validate arguments. */
79 (DB_CREATE | DB_NOMMAP | DB_THREAD | DB_INIT_LOCK | DB_INIT_LOG | \
80 DB_INIT_MPOOL | DB_INIT_TXN | DB_MPOOL_PRIVATE | DB_RECOVER | \
81 DB_RECOVER_FATAL | DB_TXN_NOSYNC | DB_USE_ENVIRON | DB_USE_ENVIRON_ROOT)
84 (DB_CREATE | DB_NOMMAP | DB_INIT_LOCK | DB_INIT_LOG | \
85 DB_INIT_MPOOL | DB_INIT_TXN | DB_MPOOL_PRIVATE | DB_RECOVER | \
86 DB_RECOVER_FATAL | DB_TXN_NOSYNC | DB_USE_ENVIRON | DB_USE_ENVIRON_ROOT)
88 if ((ret = __db_fchk(dbenv, "db_appinit", flags, OKFLAGS)) != 0)
91 #define RECOVERY_FLAGS (DB_CREATE | DB_INIT_TXN | DB_INIT_LOG)
92 if (LF_ISSET(DB_RECOVER | DB_RECOVER_FATAL) &&
93 LF_ISSET(RECOVERY_FLAGS) != RECOVERY_FLAGS)
94 return (__db_ferr(dbenv, "db_appinit", 1));
98 /* Set the database home. */
99 if ((ret = __db_home(dbenv, db_home, flags)) != 0)
102 /* Parse the config array. */
103 for (p = (char **)db_config; p != NULL && *p != NULL; ++p)
104 if ((ret = __db_parse(dbenv, *p)) != 0)
107 /* Parse the config file. */
108 if (dbenv->db_home != NULL) {
110 sizeof(buf), "%s/DB_CONFIG", dbenv->db_home);
111 if ((fp = fopen(buf, "r")) != NULL) {
112 while (fgets(buf, sizeof(buf), fp) != NULL) {
113 if ((lp = strchr(buf, '\n')) != NULL)
115 if ((ret = __db_parse(dbenv, buf)) != 0)
123 /* Set up the tmp directory path. */
124 if (dbenv->db_tmp_dir == NULL &&
125 (ret = __db_tmp_dir(dbenv, flags)) != 0)
128 /* Indicate that the path names have been set. */
129 F_SET(dbenv, DB_APP_INIT);
132 * If we are doing recovery, remove all the regions.
134 if (LF_ISSET(DB_RECOVER | DB_RECOVER_FATAL)) {
135 /* Remove all the old shared memory regions. */
136 if ((ret = log_unlink(NULL, 1 /* force */, dbenv)) != 0)
138 if ((ret = memp_unlink(NULL, 1 /* force */, dbenv)) != 0)
140 if ((ret = lock_unlink(NULL, 1 /* force */, dbenv)) != 0)
142 if ((ret = txn_unlink(NULL, 1 /* force */, dbenv)) != 0)
146 /* Transactions imply logging. */
147 if (LF_ISSET(DB_INIT_TXN))
150 /* Default permissions are 0660. */
152 #define DB_DEFPERM (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)
154 /* Initialize the subsystems. */
155 if (LF_ISSET(DB_INIT_LOCK) && (ret = lock_open(NULL,
156 LF_ISSET(DB_CREATE | DB_THREAD),
157 DB_DEFPERM, dbenv, &dbenv->lk_info)) != 0)
159 if (LF_ISSET(DB_INIT_LOG) && (ret = log_open(NULL,
160 LF_ISSET(DB_CREATE | DB_THREAD),
161 DB_DEFPERM, dbenv, &dbenv->lg_info)) != 0)
163 if (LF_ISSET(DB_INIT_MPOOL) && (ret = memp_open(NULL,
164 LF_ISSET(DB_CREATE | DB_MPOOL_PRIVATE | DB_NOMMAP | DB_THREAD),
165 DB_DEFPERM, dbenv, &dbenv->mp_info)) != 0)
167 if (LF_ISSET(DB_INIT_TXN) && (ret = txn_open(NULL,
168 LF_ISSET(DB_CREATE | DB_THREAD | DB_TXN_NOSYNC),
169 DB_DEFPERM, dbenv, &dbenv->tx_info)) != 0)
172 /* Initialize recovery. */
173 if (LF_ISSET(DB_INIT_TXN)) {
174 if ((ret = __bam_init_recover(dbenv)) != 0)
176 if ((ret = __db_init_recover(dbenv)) != 0)
178 if ((ret = __ham_init_recover(dbenv)) != 0)
180 if ((ret = __log_init_recover(dbenv)) != 0)
182 if ((ret = __txn_init_recover(dbenv)) != 0)
186 /* Run recovery if necessary. */
187 if (LF_ISSET(DB_RECOVER | DB_RECOVER_FATAL) && (ret =
188 __db_apprec(dbenv, LF_ISSET(DB_RECOVER | DB_RECOVER_FATAL))) != 0)
196 (void)db_appexit(dbenv);
202 * Close down the default application environment.
213 /* Close subsystems. */
214 if (dbenv->tx_info && (t_ret = txn_close(dbenv->tx_info)) != 0)
217 if (dbenv->mp_info && (t_ret = memp_close(dbenv->mp_info)) != 0)
220 if (dbenv->lg_info && (t_ret = log_close(dbenv->lg_info)) != 0)
223 if (dbenv->lk_info && (t_ret = lock_close(dbenv->lk_info)) != 0)
227 /* Free allocated memory. */
228 if (dbenv->db_home != NULL)
229 FREES(dbenv->db_home);
230 if ((p = dbenv->db_data_dir) != NULL) {
231 for (; *p != NULL; ++p)
233 FREE(dbenv->db_data_dir, dbenv->data_cnt * sizeof(char **));
235 if (dbenv->db_log_dir != NULL)
236 FREES(dbenv->db_log_dir);
237 if (dbenv->db_tmp_dir != NULL)
238 FREES(dbenv->db_tmp_dir);
243 #define DB_ADDSTR(str) { \
244 if ((str) != NULL) { \
245 /* If leading slash, start over. */ \
246 if (__db_abspath(str)) { \
250 /* Append to the current string. */ \
253 *p++ = PATH_SEPARATOR[0]; \
254 memcpy(p, str, len); \
256 slash = strchr(PATH_SEPARATOR, p[-1]) == NULL; \
262 * Given an optional DB environment, directory and file name and type
263 * of call, build a path based on the db_appinit(3) rules, and return
264 * it in allocated space.
266 * PUBLIC: int __db_appname __P((DB_ENV *,
267 * PUBLIC: APPNAME, const char *, const char *, int *, char **));
270 __db_appname(dbenv, appname, dir, file, fdp, namep)
273 const char *dir, *file;
279 int ret, slash, tmp_create, tmp_free;
280 const char *a, *b, *c;
286 tmp_create = tmp_free = 0;
289 * We don't return a name when creating temporary files, just an fd.
290 * Default to error now.
298 * Absolute path names are never modified. If the file is an absolute
299 * path, we're done. If the directory is, simply append the file and
302 if (file != NULL && __db_abspath(file))
303 return ((*namep = (char *)strdup(file)) == NULL ? ENOMEM : 0);
304 if (dir != NULL && __db_abspath(dir)) {
310 * DB_ENV DIR APPNAME RESULT
311 * -------------------------------------------
312 * null null none <tmp>/file
313 * null set none DIR/file
314 * set null none DB_HOME/file
315 * set set none DB_HOME/DIR/file
317 * DB_ENV FILE APPNAME RESULT
318 * -------------------------------------------
319 * null null DB_APP_DATA <tmp>/<create>
320 * null set DB_APP_DATA ./file
321 * set null DB_APP_DATA <tmp>/<create>
322 * set set DB_APP_DATA DB_HOME/DB_DATA_DIR/file
324 * DB_ENV DIR APPNAME RESULT
325 * -------------------------------------------
326 * null null DB_APP_LOG <tmp>/file
327 * null set DB_APP_LOG DIR/file
328 * set null DB_APP_LOG DB_HOME/DB_LOG_DIR/file
329 * set set DB_APP_LOG DB_HOME/DB_LOG_DIR/DIR/file
331 * DB_ENV APPNAME RESULT
332 * -------------------------------------------
333 * null DB_APP_TMP <tmp>/<create>
334 * set DB_APP_TMP DB_HOME/DB_TMP_DIR/<create>
336 retry: switch (appname) {
338 if (dbenv == NULL || !F_ISSET(dbenv, DB_APP_INIT)) {
350 "DB_APP_DATA: illegal directory specification");
358 if (dbenv == NULL || !F_ISSET(dbenv, DB_APP_INIT))
362 if (dbenv->db_data_dir != NULL &&
363 (b = dbenv->db_data_dir[++data_entry]) == NULL) {
365 b = dbenv->db_data_dir[0];
370 if (dbenv == NULL || !F_ISSET(dbenv, DB_APP_INIT)) {
376 b = dbenv->db_log_dir;
381 if (dir != NULL || file != NULL) {
383 "DB_APP_TMP: illegal directory or file specification");
388 if (dbenv == NULL || !F_ISSET(dbenv, DB_APP_INIT))
392 b = dbenv->db_tmp_dir;
397 /* Reference a file from the appropriate temporary directory. */
399 tmp: if (dbenv == NULL || !F_ISSET(dbenv, DB_APP_INIT)) {
400 memset(&etmp, 0, sizeof(etmp));
401 if ((ret = __db_tmp_dir(&etmp, DB_USE_ENVIRON)) != 0)
406 a = dbenv->db_tmp_dir;
410 (a == NULL ? 0 : strlen(a) + 1) +
411 (b == NULL ? 0 : strlen(b) + 1) +
412 (c == NULL ? 0 : strlen(c) + 1) +
413 (file == NULL ? 0 : strlen(file) + 1);
415 if ((start = (char *)malloc(len)) == NULL) {
416 __db_err(dbenv, "%s", strerror(ENOMEM));
418 FREES(etmp.db_tmp_dir);
430 * If we're opening a data file, see if it exists. If it does,
431 * return it, otherwise, try and find another one to open.
433 if (data_entry != -1 && __db_exists(start, NULL) != 0) {
439 /* Discard any space allocated to find the temp directory. */
441 FREES(etmp.db_tmp_dir);
443 /* Create the file if so requested. */
445 ret = __db_tmp_open(dbenv, start, fdp);
456 * Find the database home.
459 __db_home(dbenv, db_home, flags)
468 /* Use the environment if it's permitted and initialized. */
470 if (LF_ISSET(DB_USE_ENVIRON) ||
471 (LF_ISSET(DB_USE_ENVIRON_ROOT) && getuid() == 0)) {
473 if (LF_ISSET(DB_USE_ENVIRON)) {
475 if ((p = getenv("DB_HOME")) == NULL)
477 else if (p[0] == '\0') {
479 "illegal DB_HOME environment variable");
487 if ((dbenv->db_home = (char *)strdup(p)) == NULL) {
488 __db_err(dbenv, "%s", strerror(ENOMEM));
496 * Parse a single NAME VALUE pair.
504 char *local_s, *name, *value, **p, *tp;
509 * We need to strdup the argument in case the caller passed us
512 if ((local_s = (char *)strdup(s)) == NULL)
516 while ((name = strsep(&tp, " \t")) != NULL && *name == '\0');
519 while ((value = strsep(&tp, " \t")) != NULL && *value == '\0');
521 illegal: ret = EINVAL;
522 __db_err(dbenv, "illegal name-value pair: %s", s);
526 #define DATA_INIT_CNT 20 /* Start with 20 data slots. */
527 if (!strcmp(name, "DB_DATA_DIR")) {
528 if (dbenv->db_data_dir == NULL) {
529 if ((dbenv->db_data_dir = (char **)calloc(DATA_INIT_CNT,
530 sizeof(char **))) == NULL)
532 dbenv->data_cnt = DATA_INIT_CNT;
533 } else if (dbenv->data_next == dbenv->data_cnt - 1) {
534 dbenv->data_cnt *= 2;
535 if ((dbenv->db_data_dir =
536 (char **)realloc(dbenv->db_data_dir,
537 dbenv->data_cnt * sizeof(char **))) == NULL)
540 p = &dbenv->db_data_dir[dbenv->data_next++];
541 } else if (!strcmp(name, "DB_LOG_DIR")) {
542 if (dbenv->db_log_dir != NULL)
543 FREES(dbenv->db_log_dir);
544 p = &dbenv->db_log_dir;
545 } else if (!strcmp(name, "DB_TMP_DIR")) {
546 if (dbenv->db_tmp_dir != NULL)
547 FREES(dbenv->db_tmp_dir);
548 p = &dbenv->db_tmp_dir;
552 if ((*p = (char *)strdup(value)) == NULL) {
554 __db_err(dbenv, "%s", strerror(ENOMEM));
562 #include <TFileSpec.h>
564 static char *sTempFolder;
569 * Set the temporary directory path.
572 __db_tmp_dir(dbenv, flags)
576 static const char * list[] = { /* Ordered: see db_appinit(3). */
579 "/temp", /* WIN32. */
581 "C:/temp", /* WIN32. */
582 "C:/tmp", /* WIN32. */
587 /* Use the environment if it's permitted and initialized. */
590 if (LF_ISSET(DB_USE_ENVIRON) ||
591 (LF_ISSET(DB_USE_ENVIRON_ROOT) && getuid() == 0)) {
593 if (LF_ISSET(DB_USE_ENVIRON)) {
595 if ((p = getenv("TMPDIR")) != NULL && p[0] == '\0') {
596 __db_err(dbenv, "illegal TMPDIR environment variable");
600 if (p == NULL && (p = getenv("TEMP")) != NULL && p[0] == '\0') {
601 __db_err(dbenv, "illegal TEMP environment variable");
605 if (p == NULL && (p = getenv("TMP")) != NULL && p[0] == '\0') {
606 __db_err(dbenv, "illegal TMP environment variable");
611 (p = getenv("TempFolder")) != NULL && p[0] == '\0') {
613 "illegal TempFolder environment variable");
619 /* Get the path to the temporary folder. */
623 if (!Special2FSSpec(kTemporaryFolderType,
624 kOnSystemDisk, 0, &spec)) {
625 p = FSp2FullPath(&spec);
626 sTempFolder = malloc(strlen(p) + 1);
627 strcpy(sTempFolder, p);
633 /* Step through the list looking for a possibility. */
635 for (lp = list; *lp != NULL; ++lp)
636 if (__db_exists(p = *lp, NULL) == 0)
642 if ((dbenv->db_tmp_dir = (char *)strdup(p)) == NULL) {
643 __db_err(dbenv, "%s", strerror(ENOMEM));
651 * Create a temporary file.
654 __db_tmp_open(dbenv, dir, fdp)
659 #ifdef HAVE_SIGFILLSET
665 char *trv, buf[MAXPATHLEN];
668 * Check the target directory; if you have six X's and it doesn't
669 * exist, this runs for a *very* long time.
671 if ((ret = __db_exists(dir, &isdir)) != 0) {
672 __db_err(dbenv, "%s: %s", dir, strerror(ret));
676 __db_err(dbenv, "%s: %s", dir, strerror(EINVAL));
680 /* Build the path. */
681 #define DB_TRAIL "/XXXXXX"
682 if ((len = strlen(dir)) + sizeof(DB_TRAIL) > sizeof(buf)) {
684 "tmp_open: %s: %s", buf, strerror(ENAMETOOLONG));
685 return (ENAMETOOLONG);
687 (void)strcpy(buf, dir);
688 (void)strcpy(buf + len, DB_TRAIL);
689 buf[len] = PATH_SEPARATOR[0]; /* WIN32 */
692 * Replace the X's with the process ID. Pid should be a pid_t,
693 * but we use unsigned long for portability.
696 trv = buf + len + sizeof(DB_TRAIL) - 1; *--trv == 'X'; pid /= 10)
698 case 0: *trv = '0'; break;
699 case 1: *trv = '1'; break;
700 case 2: *trv = '2'; break;
701 case 3: *trv = '3'; break;
702 case 4: *trv = '4'; break;
703 case 5: *trv = '5'; break;
704 case 6: *trv = '6'; break;
705 case 7: *trv = '7'; break;
706 case 8: *trv = '8'; break;
707 case 9: *trv = '9'; break;
712 * Try and open a file. We block every signal we can get our hands
713 * on so that, if we're interrupted at the wrong time, the temporary
714 * file isn't left around -- of course, if we drop core in-between
715 * the calls we'll hang forever, but that's probably okay. ;-}
717 #ifdef HAVE_SIGFILLSET
718 (void)sigfillset(&set);
721 #ifdef HAVE_SIGFILLSET
722 (void)sigprocmask(SIG_BLOCK, &set, &oset);
724 #define DB_TEMPOPEN DB_CREATE | DB_EXCL | DB_TEMPORARY
725 if ((ret = __db_fdopen(buf,
726 DB_TEMPOPEN, DB_TEMPOPEN, S_IRUSR | S_IWUSR, fdp)) == 0) {
727 #ifdef HAVE_SIGFILLSET
728 (void)sigprocmask(SIG_SETMASK, &oset, NULL);
732 #ifdef HAVE_SIGFILLSET
733 (void)sigprocmask(SIG_SETMASK, &oset, NULL);
737 * If we don't get an EEXIST error, then there's something
738 * seriously wrong. Unfortunately, if the implementation
739 * doesn't return EEXIST for O_CREAT and O_EXCL regardless
740 * of other possible errors, we've lost.
744 "tmp_open: %s: %s", buf, strerror(ret));
749 * Tricky little algorithm for backward compatibility.
750 * Assumes the ASCII ordering of lower-case characters.