2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1999-2002
5
* Sleepycat Software. All rights reserved.
11
static const char revid[] = "$Id$";
14
#ifndef NO_SYSTEM_INCLUDES
15
#include <sys/types.h>
25
* This is the file that initializes the global array. Do it this way because
26
* people keep changing one without changing the other. Having declaration and
27
* initialization in one file will hopefully fix that.
29
#define DB_INITIALIZE_DB_GLOBALS 1
32
#include "dbinc/crypto.h"
33
#include "dbinc/hmac.h"
34
#include "dbinc/db_shash.h"
35
#include "dbinc/db_page.h"
36
#include "dbinc/db_am.h"
37
#include "dbinc/lock.h"
38
#include "dbinc/log.h"
40
#include "dbinc/rep.h"
41
#include "dbinc/txn.h"
44
#include "dbinc_auto/db_server.h"
45
#include "dbinc_auto/rpc_client_ext.h"
48
static void __dbenv_err __P((const DB_ENV *, int, const char *, ...));
49
static void __dbenv_errx __P((const DB_ENV *, const char *, ...));
50
static int __dbenv_init __P((DB_ENV *));
51
static int __dbenv_set_alloc __P((DB_ENV *, void *(*)(size_t),
52
void *(*)(void *, size_t), void (*)(void *)));
53
static int __dbenv_set_app_dispatch __P((DB_ENV *,
54
int (*)(DB_ENV *, DBT *, DB_LSN *, db_recops)));
55
static int __dbenv_set_data_dir __P((DB_ENV *, const char *));
56
static int __dbenv_set_encrypt __P((DB_ENV *, const char *, u_int32_t));
57
static void __dbenv_set_errcall __P((DB_ENV *, void (*)(const char *, char *)));
58
static void __dbenv_set_errfile __P((DB_ENV *, FILE *));
59
static void __dbenv_set_errpfx __P((DB_ENV *, const char *));
60
static int __dbenv_set_feedback __P((DB_ENV *, void (*)(DB_ENV *, int, int)));
61
static int __dbenv_set_flags __P((DB_ENV *, u_int32_t, int));
62
static int __dbenv_set_paniccall __P((DB_ENV *, void (*)(DB_ENV *, int)));
63
static int __dbenv_set_rpc_server_noclnt
64
__P((DB_ENV *, void *, const char *, long, long, u_int32_t));
65
static int __dbenv_set_shm_key __P((DB_ENV *, long));
66
static int __dbenv_set_tas_spins __P((DB_ENV *, u_int32_t));
67
static int __dbenv_set_tmp_dir __P((DB_ENV *, const char *));
68
static int __dbenv_set_verbose __P((DB_ENV *, u_int32_t, int));
74
* EXTERN: int db_env_create __P((DB_ENV **, u_int32_t));
77
db_env_create(dbenvpp, flags)
86
* Our caller has not yet had the opportunity to reset the panic
87
* state or turn off mutex locking, and so we can neither check
88
* the panic state or acquire a mutex in the DB_ENV create path.
91
* We can't call the flags-checking routines, we don't have an
94
if (flags != 0 && flags != DB_CLIENT)
97
if ((ret = __os_calloc(NULL, 1, sizeof(*dbenv), &dbenv)) != 0)
101
if (LF_ISSET(DB_CLIENT))
102
F_SET(dbenv, DB_ENV_RPCCLIENT);
104
ret = __dbenv_init(dbenv);
107
__os_free(NULL, dbenv);
117
* Initialize a DB_ENV structure.
125
* Our caller has not yet had the opportunity to reset the panic
126
* state or turn off mutex locking, and so we can neither check
127
* the panic state or acquire a mutex in the DB_ENV create path.
129
* Set up methods that are the same in both normal and RPC
131
dbenv->err = __dbenv_err;
132
dbenv->errx = __dbenv_errx;
133
dbenv->set_errcall = __dbenv_set_errcall;
134
dbenv->set_errfile = __dbenv_set_errfile;
135
dbenv->set_errpfx = __dbenv_set_errpfx;
138
if (F_ISSET(dbenv, DB_ENV_RPCCLIENT)) {
139
dbenv->close = __dbcl_env_close;
140
dbenv->dbremove = __dbcl_env_dbremove;
141
dbenv->dbrename = __dbcl_env_dbrename;
142
dbenv->open = __dbcl_env_open_wrap;
143
dbenv->remove = __dbcl_env_remove;
144
dbenv->set_alloc = __dbcl_env_alloc;
145
dbenv->set_app_dispatch = __dbcl_set_app_dispatch;
146
dbenv->set_data_dir = __dbcl_set_data_dir;
147
dbenv->set_encrypt = __dbcl_env_encrypt;
148
dbenv->set_feedback = __dbcl_env_set_feedback;
149
dbenv->set_flags = __dbcl_env_flags;
150
dbenv->set_paniccall = __dbcl_env_paniccall;
151
dbenv->set_rpc_server = __dbcl_envrpcserver;
152
dbenv->set_shm_key = __dbcl_set_shm_key;
153
dbenv->set_tas_spins = __dbcl_set_tas_spins;
154
dbenv->set_timeout = __dbcl_set_timeout;
155
dbenv->set_tmp_dir = __dbcl_set_tmp_dir;
156
dbenv->set_verbose = __dbcl_set_verbose;
159
dbenv->close = __dbenv_close;
160
dbenv->dbremove = __dbenv_dbremove;
161
dbenv->dbrename = __dbenv_dbrename;
162
dbenv->open = __dbenv_open;
163
dbenv->remove = __dbenv_remove;
164
dbenv->set_alloc = __dbenv_set_alloc;
165
dbenv->set_app_dispatch = __dbenv_set_app_dispatch;
166
dbenv->set_data_dir = __dbenv_set_data_dir;
167
dbenv->set_encrypt = __dbenv_set_encrypt;
168
dbenv->set_feedback = __dbenv_set_feedback;
169
dbenv->set_flags = __dbenv_set_flags;
170
dbenv->set_paniccall = __dbenv_set_paniccall;
171
dbenv->set_rpc_server = __dbenv_set_rpc_server_noclnt;
172
dbenv->set_shm_key = __dbenv_set_shm_key;
173
dbenv->set_tas_spins = __dbenv_set_tas_spins;
174
dbenv->set_tmp_dir = __dbenv_set_tmp_dir;
175
dbenv->set_verbose = __dbenv_set_verbose;
179
dbenv->shm_key = INVALID_REGION_SEGID;
182
__log_dbenv_create(dbenv); /* Subsystem specific. */
183
__lock_dbenv_create(dbenv);
184
__memp_dbenv_create(dbenv);
185
__rep_dbenv_create(dbenv);
186
__txn_dbenv_create(dbenv);
193
* Error message, including the standard error string.
197
__dbenv_err(const DB_ENV *dbenv, int error, const char *fmt, ...)
199
__dbenv_err(dbenv, error, fmt, va_alist)
206
DB_REAL_ERR(dbenv, error, 1, 1, fmt);
215
__dbenv_errx(const DB_ENV *dbenv, const char *fmt, ...)
217
__dbenv_errx(dbenv, fmt, va_alist)
223
DB_REAL_ERR(dbenv, 0, 0, 1, fmt);
227
__dbenv_set_alloc(dbenv, mal_func, real_func, free_func)
229
void *(*mal_func) __P((size_t));
230
void *(*real_func) __P((void *, size_t));
231
void (*free_func) __P((void *));
233
ENV_ILLEGAL_AFTER_OPEN(dbenv, "set_alloc");
235
dbenv->db_malloc = mal_func;
236
dbenv->db_realloc = real_func;
237
dbenv->db_free = free_func;
242
* __dbenv_set_app_dispatch --
243
* Set the transaction abort recover function.
246
__dbenv_set_app_dispatch(dbenv, app_dispatch)
248
int (*app_dispatch) __P((DB_ENV *, DBT *, DB_LSN *, db_recops));
250
ENV_ILLEGAL_AFTER_OPEN(dbenv, "set_app_dispatch");
252
dbenv->app_dispatch = app_dispatch;
257
__dbenv_set_encrypt(dbenv, passwd, flags)
263
DB_CIPHER *db_cipher;
266
ENV_ILLEGAL_AFTER_OPEN(dbenv, "set_encrypt");
267
#define OK_CRYPTO_FLAGS (DB_ENCRYPT_AES)
269
if (flags != 0 && LF_ISSET(~OK_CRYPTO_FLAGS))
270
return (__db_ferr(dbenv, "DB_ENV->set_encrypt", 0));
272
if (passwd == NULL || strlen(passwd) == 0) {
273
__db_err(dbenv, "Empty password specified to set_encrypt");
276
if (!CRYPTO_ON(dbenv)) {
277
if ((ret = __os_calloc(dbenv, 1, sizeof(DB_CIPHER), &db_cipher))
280
dbenv->crypto_handle = db_cipher;
282
db_cipher = (DB_CIPHER *)dbenv->crypto_handle;
284
if (dbenv->passwd != NULL)
285
__os_free(dbenv, dbenv->passwd);
286
if ((ret = __os_strdup(dbenv, passwd, &dbenv->passwd)) != 0) {
287
__os_free(dbenv, db_cipher);
291
* We're going to need this often enough to keep around
293
dbenv->passwd_len = strlen(dbenv->passwd) + 1;
295
* The MAC key is for checksumming, and is separate from
296
* the algorithm. So initialize it here, even if they
297
* are using CIPHER_ANY.
299
__db_derive_mac((u_int8_t *)dbenv->passwd,
300
dbenv->passwd_len, db_cipher->mac_key);
303
F_SET(db_cipher, CIPHER_ANY);
306
if ((ret = __crypto_algsetup(dbenv, db_cipher, CIPHER_AES, 0))
314
__os_free(dbenv, dbenv->passwd);
315
__os_free(dbenv, db_cipher);
316
dbenv->crypto_handle = NULL;
320
COMPQUIET(dbenv, NULL);
321
COMPQUIET(passwd, NULL);
324
return (__db_eopnotsup(dbenv));
329
__dbenv_set_flags(dbenv, flags, onoff)
335
(DB_AUTO_COMMIT | DB_CDB_ALLDB | DB_DIRECT_DB | DB_DIRECT_LOG | \
336
DB_NOLOCKING | DB_NOMMAP | DB_NOPANIC | DB_OVERWRITE | \
337
DB_PANIC_ENVIRONMENT | DB_REGION_INIT | DB_TXN_NOSYNC | \
338
DB_TXN_WRITE_NOSYNC | DB_YIELDCPU)
340
if (LF_ISSET(~OK_FLAGS))
341
return (__db_ferr(dbenv, "DB_ENV->set_flags", 0));
342
if (onoff && LF_ISSET(DB_TXN_WRITE_NOSYNC) && LF_ISSET(DB_TXN_NOSYNC))
343
return (__db_ferr(dbenv, "DB_ENV->set_flags", 1));
345
if (LF_ISSET(DB_AUTO_COMMIT)) {
347
F_SET(dbenv, DB_ENV_AUTO_COMMIT);
349
F_CLR(dbenv, DB_ENV_AUTO_COMMIT);
351
if (LF_ISSET(DB_CDB_ALLDB)) {
352
ENV_ILLEGAL_AFTER_OPEN(dbenv, "set_flags: DB_CDB_ALLDB");
354
F_SET(dbenv, DB_ENV_CDB_ALLDB);
356
F_CLR(dbenv, DB_ENV_CDB_ALLDB);
358
if (LF_ISSET(DB_DIRECT_DB)) {
360
F_SET(dbenv, DB_ENV_DIRECT_DB);
362
F_CLR(dbenv, DB_ENV_DIRECT_DB);
364
if (LF_ISSET(DB_DIRECT_LOG)) {
366
F_SET(dbenv, DB_ENV_DIRECT_LOG);
368
F_CLR(dbenv, DB_ENV_DIRECT_LOG);
370
if (LF_ISSET(DB_NOLOCKING)) {
372
F_SET(dbenv, DB_ENV_NOLOCKING);
374
F_CLR(dbenv, DB_ENV_NOLOCKING);
376
if (LF_ISSET(DB_NOMMAP)) {
378
F_SET(dbenv, DB_ENV_NOMMAP);
380
F_CLR(dbenv, DB_ENV_NOMMAP);
382
if (LF_ISSET(DB_NOPANIC)) {
384
F_SET(dbenv, DB_ENV_NOPANIC);
386
F_CLR(dbenv, DB_ENV_NOPANIC);
388
if (LF_ISSET(DB_OVERWRITE)) {
390
F_SET(dbenv, DB_ENV_OVERWRITE);
392
F_CLR(dbenv, DB_ENV_OVERWRITE);
394
if (LF_ISSET(DB_PANIC_ENVIRONMENT)) {
395
ENV_ILLEGAL_BEFORE_OPEN(dbenv,
396
"set_flags: DB_PANIC_ENVIRONMENT");
397
PANIC_SET(dbenv, onoff);
399
if (LF_ISSET(DB_REGION_INIT)) {
400
ENV_ILLEGAL_AFTER_OPEN(dbenv, "set_flags: DB_REGION_INIT");
402
F_SET(dbenv, DB_ENV_REGION_INIT);
404
F_CLR(dbenv, DB_ENV_REGION_INIT);
406
if (LF_ISSET(DB_TXN_NOSYNC)) {
408
F_SET(dbenv, DB_ENV_TXN_NOSYNC);
410
F_CLR(dbenv, DB_ENV_TXN_NOSYNC);
412
if (LF_ISSET(DB_TXN_WRITE_NOSYNC)) {
414
F_SET(dbenv, DB_ENV_TXN_WRITE_NOSYNC);
416
F_CLR(dbenv, DB_ENV_TXN_WRITE_NOSYNC);
418
if (LF_ISSET(DB_YIELDCPU)) {
420
F_SET(dbenv, DB_ENV_YIELDCPU);
422
F_CLR(dbenv, DB_ENV_YIELDCPU);
428
__dbenv_set_data_dir(dbenv, dir)
434
#define DATA_INIT_CNT 20 /* Start with 20 data slots. */
435
if (dbenv->db_data_dir == NULL) {
436
if ((ret = __os_calloc(dbenv, DATA_INIT_CNT,
437
sizeof(char **), &dbenv->db_data_dir)) != 0)
439
dbenv->data_cnt = DATA_INIT_CNT;
440
} else if (dbenv->data_next == dbenv->data_cnt - 1) {
441
dbenv->data_cnt *= 2;
442
if ((ret = __os_realloc(dbenv,
443
dbenv->data_cnt * sizeof(char **),
444
&dbenv->db_data_dir)) != 0)
447
return (__os_strdup(dbenv,
448
dir, &dbenv->db_data_dir[dbenv->data_next++]));
452
__dbenv_set_errcall(dbenv, errcall)
454
void (*errcall) __P((const char *, char *));
456
dbenv->db_errcall = errcall;
460
__dbenv_set_errfile(dbenv, errfile)
464
dbenv->db_errfile = errfile;
468
__dbenv_set_errpfx(dbenv, errpfx)
472
dbenv->db_errpfx = errpfx;
476
__dbenv_set_feedback(dbenv, feedback)
478
void (*feedback) __P((DB_ENV *, int, int));
480
dbenv->db_feedback = feedback;
485
__dbenv_set_paniccall(dbenv, paniccall)
487
void (*paniccall) __P((DB_ENV *, int));
489
dbenv->db_paniccall = paniccall;
494
__dbenv_set_shm_key(dbenv, shm_key)
496
long shm_key; /* !!!: really a key_t. */
498
ENV_ILLEGAL_AFTER_OPEN(dbenv, "set_shm_key");
500
dbenv->shm_key = shm_key;
505
__dbenv_set_tas_spins(dbenv, tas_spins)
509
dbenv->tas_spins = tas_spins;
514
__dbenv_set_tmp_dir(dbenv, dir)
518
if (dbenv->db_tmp_dir != NULL)
519
__os_free(dbenv, dbenv->db_tmp_dir);
520
return (__os_strdup(dbenv, dir, &dbenv->db_tmp_dir));
524
__dbenv_set_verbose(dbenv, which, onoff)
530
case DB_VERB_CHKPOINT:
531
case DB_VERB_DEADLOCK:
532
case DB_VERB_RECOVERY:
533
case DB_VERB_REPLICATION:
534
case DB_VERB_WAITSFOR:
536
FLD_SET(dbenv->verbose, which);
538
FLD_CLR(dbenv->verbose, which);
548
* Method illegally called with public environment.
550
* PUBLIC: int __db_mi_env __P((DB_ENV *, const char *));
553
__db_mi_env(dbenv, name)
557
__db_err(dbenv, "%s: method not permitted in shared environment", name);
563
* Method illegally called after open.
565
* PUBLIC: int __db_mi_open __P((DB_ENV *, const char *, int));
568
__db_mi_open(dbenv, name, after)
573
__db_err(dbenv, "%s: method not permitted %s open",
574
name, after ? "after" : "before");
580
* Method or function called without required configuration.
582
* PUBLIC: int __db_env_config __P((DB_ENV *, char *, u_int32_t));
585
__db_env_config(dbenv, i, flags)
606
sub = "<unspecified>";
610
"%s interface requires an environment configured for the %s subsystem",
616
__dbenv_set_rpc_server_noclnt(dbenv, cl, host, tsec, ssec, flags)
623
COMPQUIET(host, NULL);
630
"set_rpc_server method not permitted in non-RPC environment");
631
return (__db_eopnotsup(dbenv));