2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 2000-2002
5
* Sleepycat Software. All rights reserved.
11
static const char revid[] = "$Id$";
14
#ifndef NO_SYSTEM_INCLUDES
15
#include <sys/types.h>
17
#if TIME_WITH_SYS_TIME
37
#include "dbinc_auto/db_server.h"
40
#include "dbinc_auto/clib_ext.h"
41
#include "dbinc/db_server_int.h"
42
#include "dbinc_auto/rpc_server_ext.h"
43
#include "dbinc_auto/common_ext.h"
45
extern int __dbsrv_main __P((void));
46
static int add_home __P((char *));
47
static int add_passwd __P((char *));
48
static int env_recover __P((char *));
49
static void __dbclear_child __P((ct_entry *));
51
static LIST_HEAD(cthead, ct_entry) __dbsrv_head;
52
static LIST_HEAD(homehead, home_entry) __dbsrv_home;
53
static long __dbsrv_defto = DB_SERVER_TIMEOUT;
54
static long __dbsrv_maxto = DB_SERVER_MAXTIMEOUT;
55
static long __dbsrv_idleto = DB_SERVER_IDLETIMEOUT;
56
static char *logfile = NULL;
59
static void usage __P((char *));
60
static void version_check __P((void));
62
int __dbsrv_verbose = 0;
80
* Check whether another server is running or not. There
81
* is a race condition where two servers could be racing to
82
* register with the portmapper. The goal of this check is to
83
* forbid running additional servers (like those started from
84
* the test suite) if the user is already running one.
87
* This does not solve nor prevent two servers from being
88
* started at the same time and running recovery at the same
89
* time on the same environments.
91
if ((cl = clnt_create("localhost",
92
DB_RPC_SERVERPROG, DB_RPC_SERVERVERS, "tcp")) != NULL) {
94
"%s: Berkeley DB RPC server already running.\n", prog);
96
return (EXIT_FAILURE);
99
LIST_INIT(&__dbsrv_home);
100
while ((ch = getopt(argc, argv, "h:I:L:P:t:T:Vv")) != EOF)
103
(void)add_home(optarg);
106
if (__db_getlong(NULL, prog,
107
optarg, 1, LONG_MAX, &__dbsrv_idleto))
108
return (EXIT_FAILURE);
114
passwd = strdup(optarg);
115
memset(optarg, 0, strlen(optarg));
116
if (passwd == NULL) {
117
fprintf(stderr, "%s: strdup: %s\n",
118
prog, strerror(errno));
119
return (EXIT_FAILURE);
121
if ((ret = add_passwd(passwd)) != 0) {
122
fprintf(stderr, "%s: strdup: %s\n",
123
prog, strerror(ret));
124
return (EXIT_FAILURE);
128
if (__db_getlong(NULL, prog,
129
optarg, 1, LONG_MAX, &__dbsrv_defto))
130
return (EXIT_FAILURE);
133
if (__db_getlong(NULL, prog,
134
optarg, 1, LONG_MAX, &__dbsrv_maxto))
135
return (EXIT_FAILURE);
138
printf("%s\n", db_version(NULL, NULL, NULL));
139
return (EXIT_SUCCESS);
147
* Check default timeout against maximum timeout
149
if (__dbsrv_defto > __dbsrv_maxto)
150
__dbsrv_defto = __dbsrv_maxto;
153
* Check default timeout against idle timeout
154
* It would be bad to timeout environments sooner than txns.
156
if (__dbsrv_defto > __dbsrv_idleto)
158
"%s: WARNING: Idle timeout %ld is less than resource timeout %ld\n",
159
prog, __dbsrv_idleto, __dbsrv_defto);
161
LIST_INIT(&__dbsrv_head);
164
* If a client crashes during an RPC, our reply to it
165
* generates a SIGPIPE. Ignore SIGPIPE so we don't exit unnecessarily.
168
signal(SIGPIPE, SIG_IGN);
171
if (logfile != NULL && __db_util_logset("berkeley_db_svc", logfile))
172
return (EXIT_FAILURE);
175
* Now that we are ready to start, run recovery on all the
176
* environments specified.
178
if (env_recover(prog) != 0)
179
return (EXIT_FAILURE);
182
* We've done our setup, now call the generated server loop
185
printf("%s: Ready to receive requests\n", prog);
196
fprintf(stderr, "usage: %s %s\n\t%s\n", prog,
197
"[-Vv] [-h home] [-P passwd]",
198
"[-I idletimeout] [-L logfile] [-t def_timeout] [-T maxtimeout]");
205
int v_major, v_minor, v_patch;
207
/* Make sure we're loaded with the right version of the DB library. */
208
(void)db_version(&v_major, &v_minor, &v_patch);
209
if (v_major != DB_VERSION_MAJOR ||
210
v_minor != DB_VERSION_MINOR || v_patch != DB_VERSION_PATCH) {
212
"%s: version %d.%d.%d doesn't match library version %d.%d.%d\n",
213
prog, DB_VERSION_MAJOR, DB_VERSION_MINOR,
214
DB_VERSION_PATCH, v_major, v_minor, v_patch);
220
* PUBLIC: void __dbsrv_settimeout __P((ct_entry *, u_int32_t));
223
__dbsrv_settimeout(ctp, to)
227
if (to > (u_int32_t)__dbsrv_maxto)
228
ctp->ct_timeout = __dbsrv_maxto;
230
ctp->ct_timeout = __dbsrv_defto;
232
ctp->ct_timeout = to;
236
* PUBLIC: void __dbsrv_timeout __P((int));
239
__dbsrv_timeout(force)
242
static long to_hint = -1;
245
ct_entry *ctp, *nextctp;
247
if ((t = time(NULL)) == -1)
251
* Check hint. If hint is further in the future
252
* than now, no work to do.
254
if (!force && to_hint > 0 && t < to_hint)
258
* Timeout transactions or cursors holding DB resources.
259
* Do this before timing out envs to properly release resources.
262
* We can just loop through this list looking for cursors and txns.
263
* We do not need to verify txn and cursor relationships at this
264
* point because we maintain the list in LIFO order *and* we
265
* maintain activity in the ultimate txn parent of any cursor
266
* so either everything in a txn is timing out, or nothing.
267
* So, since we are LIFO, we will correctly close/abort all the
268
* appropriate handles, in the correct order.
270
for (ctp = LIST_FIRST(&__dbsrv_head); ctp != NULL; ctp = nextctp) {
271
nextctp = LIST_NEXT(ctp, entries);
272
switch (ctp->ct_type) {
274
to = *(ctp->ct_activep) + ctp->ct_timeout;
278
printf("Timing out txn id %ld\n",
280
(void)((DB_TXN *)ctp->ct_anyp)->
281
abort((DB_TXN *)ctp->ct_anyp);
284
* If we timed out an txn, we may have closed
285
* all sorts of ctp's.
286
* So start over with a guaranteed good ctp.
288
nextctp = LIST_FIRST(&__dbsrv_head);
289
} else if ((to_hint > 0 && to_hint > to) ||
294
case (CT_JOINCUR | CT_CURSOR):
295
to = *(ctp->ct_activep) + ctp->ct_timeout;
299
printf("Timing out cursor %ld\n",
301
(void)__dbc_close_int(ctp);
303
* Start over with a guaranteed good ctp.
305
nextctp = LIST_FIRST(&__dbsrv_head);
306
} else if ((to_hint > 0 && to_hint > to) ||
315
* Timeout idle handles.
316
* If we are forcing a timeout, we'll close all env handles.
318
for (ctp = LIST_FIRST(&__dbsrv_head); ctp != NULL; ctp = nextctp) {
319
nextctp = LIST_NEXT(ctp, entries);
320
if (ctp->ct_type != CT_ENV)
322
to = *(ctp->ct_activep) + ctp->ct_idle;
324
if (to < t || force) {
326
printf("Timing out env id %ld\n", ctp->ct_id);
327
(void)__dbenv_close_int(ctp->ct_id, 0, 1);
329
* If we timed out an env, we may have closed
330
* all sorts of ctp's (maybe even all of them.
331
* So start over with a guaranteed good ctp.
333
nextctp = LIST_FIRST(&__dbsrv_head);
339
* RECURSIVE FUNCTION. We need to clear/free any number of levels of nested
343
__dbclear_child(parent)
346
ct_entry *ctp, *nextctp;
348
for (ctp = LIST_FIRST(&__dbsrv_head); ctp != NULL;
350
nextctp = LIST_NEXT(ctp, entries);
351
if (ctp->ct_type == 0)
353
if (ctp->ct_parent == parent) {
354
__dbclear_child(ctp);
356
* Need to do this here because le_next may
357
* have changed with the recursive call and we
358
* don't want to point to a removed entry.
360
nextctp = LIST_NEXT(ctp, entries);
367
* PUBLIC: void __dbclear_ctp __P((ct_entry *));
373
LIST_REMOVE(ctp, entries);
374
__os_free(NULL, ctp);
378
* PUBLIC: void __dbdel_ctp __P((ct_entry *));
384
__dbclear_child(parent);
385
__dbclear_ctp(parent);
389
* PUBLIC: ct_entry *new_ct_ent __P((int *));
396
ct_entry *ctp, *octp;
399
if ((ret = __os_malloc(NULL, sizeof(ct_entry), &ctp)) != 0) {
403
memset(ctp, 0, sizeof(ct_entry));
405
* Get the time as ID. We may service more than one request per
406
* second however. If we are, then increment id value until we
407
* find an unused one. We insert entries in LRU fashion at the
408
* head of the list. So, if the first entry doesn't match, then
409
* we know for certain that we can use our entry.
411
if ((t = time(NULL)) == -1) {
412
*errp = __os_get_errno();
413
__os_free(NULL, ctp);
416
octp = LIST_FIRST(&__dbsrv_head);
417
if (octp != NULL && octp->ct_id >= t)
420
ctp->ct_idle = __dbsrv_idleto;
421
ctp->ct_activep = &ctp->ct_active;
422
ctp->ct_origp = NULL;
423
ctp->ct_refcount = 1;
425
LIST_INSERT_HEAD(&__dbsrv_head, ctp, entries);
430
* PUBLIC: ct_entry *get_tableent __P((long));
438
for (ctp = LIST_FIRST(&__dbsrv_head); ctp != NULL;
439
ctp = LIST_NEXT(ctp, entries))
440
if (ctp->ct_id == id)
446
* PUBLIC: ct_entry *__dbsrv_sharedb __P((ct_entry *, const char *,
447
* PUBLIC: const char *, DBTYPE, u_int32_t));
450
__dbsrv_sharedb(db_ctp, name, subdb, type, flags)
452
const char *name, *subdb;
459
* Check if we can share a db handle. Criteria for sharing are:
460
* If any of the non-sharable flags are set, we cannot share.
461
* Must be a db ctp, obviously.
462
* Must share the same env parent.
463
* Must be the same type, or current one DB_UNKNOWN.
464
* Must be same byteorder, or current one must not care.
465
* All flags must match.
466
* Must be same name, but don't share in-memory databases.
467
* Must be same subdb name.
469
if (flags & DB_SERVER_DBNOSHARE)
471
for (ctp = LIST_FIRST(&__dbsrv_head); ctp != NULL;
472
ctp = LIST_NEXT(ctp, entries)) {
478
if (ctp->ct_type != CT_DB)
480
if (ctp->ct_envparent != db_ctp->ct_envparent)
482
if (type != DB_UNKNOWN && ctp->ct_dbdp.type != type)
484
if (ctp->ct_dbdp.dbflags != LF_ISSET(DB_SERVER_DBFLAGS))
486
if (db_ctp->ct_dbdp.setflags != 0 &&
487
ctp->ct_dbdp.setflags != db_ctp->ct_dbdp.setflags)
489
if (name == NULL || ctp->ct_dbdp.db == NULL ||
490
strcmp(name, ctp->ct_dbdp.db) != 0)
492
if (subdb != ctp->ct_dbdp.subdb &&
493
(subdb == NULL || ctp->ct_dbdp.subdb == NULL ||
494
strcmp(subdb, ctp->ct_dbdp.subdb) != 0))
497
* If we get here, then we match.
507
* PUBLIC: ct_entry *__dbsrv_shareenv __P((ct_entry *, home_entry *, u_int32_t));
510
__dbsrv_shareenv(env_ctp, home, flags)
518
* Check if we can share an env. Criteria for sharing are:
519
* Must be an env ctp, obviously.
520
* Must share the same home env.
521
* All flags must match.
523
for (ctp = LIST_FIRST(&__dbsrv_head); ctp != NULL;
524
ctp = LIST_NEXT(ctp, entries)) {
530
if (ctp->ct_type != CT_ENV)
532
if (ctp->ct_envdp.home != home)
534
if (ctp->ct_envdp.envflags != flags)
536
if (ctp->ct_envdp.onflags != env_ctp->ct_envdp.onflags)
538
if (ctp->ct_envdp.offflags != env_ctp->ct_envdp.offflags)
541
* If we get here, then we match. The only thing left to
542
* check is the timeout. Since the server timeout set by
543
* the client is a hint, for sharing we'll give them the
544
* benefit of the doubt and grant them the longer timeout.
546
if (ctp->ct_timeout < env_ctp->ct_timeout)
547
ctp->ct_timeout = env_ctp->ct_timeout;
556
* PUBLIC: void __dbsrv_active __P((ct_entry *));
567
if ((t = time(NULL)) == -1)
569
*(ctp->ct_activep) = t;
570
if ((envctp = ctp->ct_envparent) == NULL)
572
*(envctp->ct_activep) = t;
577
* PUBLIC: int __db_close_int __P((long, u_int32_t));
580
__db_close_int(id, flags)
589
ctp = get_tableent(id);
591
return (DB_NOSERVER_ID);
592
DB_ASSERT(ctp->ct_type == CT_DB);
593
if (__dbsrv_verbose && ctp->ct_refcount != 1)
594
printf("Deref'ing dbp id %ld, refcount %d\n",
595
id, ctp->ct_refcount);
596
if (--ctp->ct_refcount != 0)
600
printf("Closing dbp id %ld\n", id);
602
ret = dbp->close(dbp, flags);
608
* PUBLIC: int __dbc_close_int __P((ct_entry *));
611
__dbc_close_int(dbc_ctp)
618
dbc = (DBC *)dbc_ctp->ct_anyp;
620
ret = dbc->c_close(dbc);
622
* If this cursor is a join cursor then we need to fix up the
623
* cursors that it was joined from so that they are independent again.
625
if (dbc_ctp->ct_type & CT_JOINCUR)
626
for (ctp = LIST_FIRST(&__dbsrv_head); ctp != NULL;
627
ctp = LIST_NEXT(ctp, entries)) {
629
* Test if it is a join cursor, and if it is part
632
if ((ctp->ct_type & CT_JOIN) &&
633
ctp->ct_activep == &dbc_ctp->ct_active) {
634
ctp->ct_type &= ~CT_JOIN;
635
ctp->ct_activep = ctp->ct_origp;
639
__dbclear_ctp(dbc_ctp);
645
* PUBLIC: int __dbenv_close_int __P((long, u_int32_t, int));
648
__dbenv_close_int(id, flags, force)
658
ctp = get_tableent(id);
660
return (DB_NOSERVER_ID);
661
DB_ASSERT(ctp->ct_type == CT_ENV);
662
if (__dbsrv_verbose && ctp->ct_refcount != 1)
663
printf("Deref'ing env id %ld, refcount %d\n",
664
id, ctp->ct_refcount);
666
* If we are timing out, we need to force the close, no matter
669
if (--ctp->ct_refcount != 0 && !force)
671
dbenv = ctp->ct_envp;
673
printf("Closing env id %ld\n", id);
675
ret = dbenv->close(dbenv, flags);
684
home_entry *hp, *homep;
687
if ((ret = __os_malloc(NULL, sizeof(home_entry), &hp)) != 0)
689
if ((ret = __os_malloc(NULL, strlen(home)+1, &hp->home)) != 0)
691
memcpy(hp->home, home, strlen(home)+1);
695
* This loop is to remove any trailing path separators,
696
* to assure hp->name points to the last component.
698
hp->name = __db_rpath(home);
701
while (*(hp->name) == '\0') {
702
hp->name = __db_rpath(home);
707
* Now we have successfully added it. Make sure there are no
710
for (homep = LIST_FIRST(&__dbsrv_home); homep != NULL;
711
homep = LIST_NEXT(homep, entries))
712
if (strcmp(homep->name, hp->name) == 0) {
713
printf("Already added home name %s, at directory %s\n",
714
hp->name, homep->dir);
715
__os_free(NULL, hp->home);
719
LIST_INSERT_HEAD(&__dbsrv_home, hp, entries);
721
printf("Added home %s in dir %s\n", hp->name, hp->dir);
732
* We add the passwd to the last given home dir. If there
733
* isn't a home dir, or the most recent one already has a
734
* passwd, then there is a user error.
736
hp = LIST_FIRST(&__dbsrv_home);
737
if (hp == NULL || hp->passwd != NULL)
740
* We've already strdup'ed the passwd above, so we don't need
741
* to malloc new space, just point to it.
748
* PUBLIC: home_entry *get_home __P((char *));
756
for (hp = LIST_FIRST(&__dbsrv_home); hp != NULL;
757
hp = LIST_NEXT(hp, entries))
758
if (strcmp(name, hp->name) == 0)
764
env_recover(progname)
772
for (hp = LIST_FIRST(&__dbsrv_home); hp != NULL;
773
hp = LIST_NEXT(hp, entries)) {
775
if ((ret = db_env_create(&dbenv, 0)) != 0) {
776
fprintf(stderr, "%s: db_env_create: %s\n",
777
progname, db_strerror(ret));
780
if (__dbsrv_verbose == 1) {
781
(void)dbenv->set_verbose(dbenv, DB_VERB_RECOVERY, 1);
782
(void)dbenv->set_verbose(dbenv, DB_VERB_CHKPOINT, 1);
784
dbenv->set_errfile(dbenv, stderr);
785
dbenv->set_errpfx(dbenv, progname);
786
if (hp->passwd != NULL)
787
(void)dbenv->set_encrypt(dbenv, hp->passwd,
791
* Initialize the env with DB_RECOVER. That is all we
792
* have to do to run recovery.
795
printf("Running recovery on %s\n", hp->home);
796
flags = DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL |
797
DB_INIT_TXN | DB_USE_ENVIRON | DB_RECOVER;
798
if ((ret = dbenv->open(dbenv, hp->home, flags, 0)) != 0) {
799
dbenv->err(dbenv, ret, "DB_ENV->open");
806
if ((ret = dbenv->close(dbenv, 0)) != 0) {
808
fprintf(stderr, "%s: dbenv->close: %s\n",
809
progname, db_strerror(ret));