#include <db.h>int db_appinit(char *db_home, char *db_config, DB_ENV *dbenv, u_int32_t flags);
The db_appinit function provides a simple way to initialize and configure the Berkeley DB environment. It is not necessary that it be called, but it provides a structure for creating a consistent environment for processes using one or more of the features of Berkeley DB.
The db_home and db_config arguments to db_appinit are described in Berkeley DB File Naming.
The flags argument specifies the subsystems that are initialized and how the environment affects Berkeley DB file naming, among other things. The flags value is specified by logically OR'ing together one or more of the following values:
The db_appinit function returns successfully if DB_RECOVER is
specified and no log files exist, so it is necessary to ensure all
necessary log files are present before running recovery.
For further information, consult db_archive and db_recover.
The db_appinit function returns successfully if DB_RECOVER_FATAL
is specified and no log files exist, so it is necessary to ensure all
necessary log files are present before running recovery. For further
information, consult db_archive and db_recover.
The Berkeley DB environment is configured based on the dbenv argument to db_appinit which is a pointer to a structure of type DB_ENV. Applications normally use the same DB_ENV structure (initialized by db_appinit) as an argument to all of the subsystems in the Berkeley DB package.
References to the DB_ENV structure are maintained by Berkeley DB, so it may not be discarded until the last close function, corresponding to an open function for which it was an argument, has returned. To ensure compatibility with future releases of Berkeley DB, all fields of the DB_ENV structure that are not explicitly set should be initialized to 0 before the first time the structure is used. Do this by declaring the structure external or static, or by calling one of the C library routines bzero(3) or memset(3).
The fields of the DB_ENV structure used by db_appinit are described below. The dbenv argument may not be NULL. If any of the fields of the dbenv structure are set to 0, defaults appropriate for the system are used where possible.
The following fields in the DB_ENV structure may be initialized before calling db_appinit:
If db_paniccall is non-NULL and such an error occurs, it will
called. The dbenv argument is a reference to the current
environment, and the errval argument is the system errno value
that was returned when the error occurred.
If db_errcall is non-NULL, it may be called with additional error
information. The db_errpfx argument is the current environment's
db_errpfx field. The buffer argument contains a
nul-terminated string with the additional information.
This error logging facility does not slow performance or significantly
increase application size, and may be run during normal operation as
well as during debugging.
except that the error message is written to the OutputStream
represented by error_stream.
allowing errors to be
redirected to a C++ error stream. It is unwise to use both error_stream
with nonzero values of either errcall or errfile.
Each of the open functions that db_appinit may call (lock_open, log_open, memp_open and txn_open), is called as follows, where the DB_CREATE flag is optional:
XXX_open(NULL, DB_CREATE, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, dbenv);
This call will cause each subsystem to construct pathnames as described in Berkeley DB File Naming. The subsystem has permission to read and write underlying files as necessary, and optionally to create files. (All created files will be created readable and writeable by the owner and the group. The group ownership of created files is based on the system and directory defaults, and is not further specified by Berkeley DB.)
In addition, the dbenv argument is passed to the open functions of any subsystems initialized by db_appinit. For this reason the fields of the DB_ENV structure relevant to the subsystems being initialized must themselves be initialized before db_appinit is called. See the appropriate subsystem documentation for a list of these fields and their uses.
The return value from each of these calls is placed in the appropriate field of the DB_ENV structure:
In general, these fields are not directly used by applications; subsystems of Berkeley DB that use these fields simply reference them using the DB_ENV structure passed to the subsystem.
For example, an application using the Berkeley DB hash access method functions to access a database will first call db_open passing it the DB_ENV argument initialized by a call to db_appinit. Then, all future calls to the hash access method functions for that database will automatically use the underlying shared memory buffer pool that was specified by the mp_info field of that DB_ENV structure.
The single exception to this rule is the tx_info field, which applications must explicitly specify to the txn_begin, txn_checkpoint and txn_close functions.
The db_prefix field of DB_ENV allows the user to configure the error message prefix, and may be changed at any time, including after the call to db_appinit.
Otherwise, once the Berkeley DB environment has been initialized by a call to db_appinit, no fields should be modified.
On Windows/95, files that are opened by multiple processes do not share data correctly. To cause Berkeley DB to use the paging file to share memory among processes, use the DB_REGION_NAME flag of the db_value_set function. Obviously, you do not need to do this if only a single process will be accessing database files.
The db_appinit function returns the value of errno on failure, and 0 on success.
The db_appinit function may fail and return errno for any of the errors specified for the following Berkeley DB and C library functions: DB->close, abort(3), ctime(3), db_appexit, dbenv->tx_recover(3), fclose(3), fcntl(3), fflush(3), fgets(3), fopen(3), fprintf(3), free(3), getenv(3), getpid(3), getuid(3), isspace(3), lock_open, lock_unlink, log_compare, log_get, log_open, log_unlink, malloc(3), memcpy(3), memp_open, memp_unlink, memset(3), realloc(3), stat(3), strcat(3), strchr(3), strcmp(3), strcpy(3), strlen(3), time(3), txn_checkpoint, txn_open, txn_unlink, vfprintf(3), and vsnprintf(3).
In addition, the db_appinit function may fail and return errno for the following conditions:
The DB_THREAD flag was specified and spinlocks are not implemented for this architecture.
The DB_HOME or TMPDIR environment variables were set but empty.
An incorrectly formatted NAME VALUE entry or line was found.