2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1996-2002
5
* Sleepycat Software. All rights reserved.
11
static const char revid[] = "$Id: db_err.c,v 11.80 2002/07/30 01:21:53 bostic Exp $";
14
#ifndef NO_SYSTEM_INCLUDES
15
#include <sys/types.h>
23
#include "dbinc/db_page.h"
24
#include "dbinc/db_am.h"
25
#include "dbinc/db_shash.h"
26
#include "dbinc/lock.h"
27
#include "dbinc/log.h"
28
#include "dbinc/txn.h"
32
* General flags checking routine.
34
* PUBLIC: int __db_fchk __P((DB_ENV *, const char *, u_int32_t, u_int32_t));
37
__db_fchk(dbenv, name, flags, ok_flags)
40
u_int32_t flags, ok_flags;
42
return (LF_ISSET(~ok_flags) ? __db_ferr(dbenv, name, 0) : 0);
47
* General combination flags checking routine.
49
* PUBLIC: int __db_fcchk
50
* PUBLIC: __P((DB_ENV *, const char *, u_int32_t, u_int32_t, u_int32_t));
53
__db_fcchk(dbenv, name, flags, flag1, flag2)
56
u_int32_t flags, flag1, flag2;
58
return (LF_ISSET(flag1) &&
59
LF_ISSET(flag2) ? __db_ferr(dbenv, name, 1) : 0);
66
* PUBLIC: int __db_ferr __P((const DB_ENV *, const char *, int));
69
__db_ferr(dbenv, name, iscombo)
74
__db_err(dbenv, "illegal flag %sspecified to %s",
75
iscombo ? "combination " : "", name);
81
* Error when unable to retrieve a specified page.
83
* PUBLIC: void __db_pgerr __P((DB *, db_pgno_t, int));
86
__db_pgerr(dbp, pgno, errval)
92
* Three things are certain:
93
* Death, taxes, and lost data.
94
* Guess which has occurred.
97
"unable to create/retrieve page %lu", (u_long)pgno);
98
(void)__db_panic(dbp->dbenv, errval);
103
* Error when a page has the wrong format.
105
* PUBLIC: int __db_pgfmt __P((DB_ENV *, db_pgno_t));
108
__db_pgfmt(dbenv, pgno)
112
__db_err(dbenv, "page %lu: illegal page type or format", (u_long)pgno);
113
return (__db_panic(dbenv, EINVAL));
118
* Common operation not supported message.
120
* PUBLIC: int __db_eopnotsup __P((const DB_ENV *));
123
__db_eopnotsup(dbenv)
126
__db_err(dbenv, "operation not supported");
137
* Error when an assertion fails. Only checked if #DIAGNOSTIC defined.
139
* PUBLIC: #ifdef DIAGNOSTIC
140
* PUBLIC: void __db_assert __P((const char *, const char *, int));
144
__db_assert(failedexpr, file, line)
145
const char *failedexpr, *file;
148
(void)fprintf(stderr,
149
"__db_assert: \"%s\" failed: file \"%s\", line %d\n",
150
failedexpr, file, line);
151
(void)fflush(stderr);
153
/* We want a stack trace of how this could possibly happen. */
162
* Just report that someone else paniced.
164
* PUBLIC: int __db_panic_msg __P((DB_ENV *));
167
__db_panic_msg(dbenv)
170
__db_err(dbenv, "fatal region error detected; run recovery");
171
return (DB_RUNRECOVERY);
176
* Lock out the tree due to unrecoverable error.
178
* PUBLIC: int __db_panic __P((DB_ENV *, int));
181
__db_panic(dbenv, errval)
188
dbenv->panic_errval = errval;
190
__db_err(dbenv, "PANIC: %s", db_strerror(errval));
192
if (dbenv->db_paniccall != NULL)
193
dbenv->db_paniccall(dbenv, errval);
196
#if defined(DIAGNOSTIC) && !defined(CONFIG_TEST)
198
* We want a stack trace of how this could possibly happen.
200
* Don't drop core if it's the test suite -- it's reasonable for the
201
* test suite to check to make sure that DB_RUNRECOVERY is returned
202
* under certain conditions.
208
* Chaos reigns within.
209
* Reflect, repent, and reboot.
210
* Order shall return.
212
return (DB_RUNRECOVERY);
217
* ANSI C strerror(3) for DB.
219
* EXTERN: char *db_strerror __P((int));
226
return ("Successful return: 0");
228
return (strerror(error));
232
* The Tcl API requires that some of these return strings be compared
233
* against strings stored in application scripts. So, any of these
234
* errors that do not invariably result in a Tcl exception may not be
239
return ("DB_DONOTINDEX: Secondary index callback returns null");
241
return ("DB_KEYEMPTY: Non-existent key/data pair");
243
return ("DB_KEYEXIST: Key/data pair already exists");
244
case DB_LOCK_DEADLOCK:
246
("DB_LOCK_DEADLOCK: Locker killed to resolve a deadlock");
247
case DB_LOCK_NOTGRANTED:
248
return ("DB_LOCK_NOTGRANTED: Lock not granted");
250
return ("DB_NOSERVER: Fatal error, no server");
251
case DB_NOSERVER_HOME:
252
return ("DB_NOSERVER_HOME: Home unrecognized at server");
254
return ("DB_NOSERVER_ID: Identifier unrecognized at server");
256
return ("DB_NOTFOUND: No matching key/data pair found");
258
return ("DB_OLDVERSION: Database requires a version upgrade");
259
case DB_PAGE_NOTFOUND:
260
return ("DB_PAGE_NOTFOUND: Requested page not found");
261
case DB_REP_DUPMASTER:
262
return ("DB_REP_DUPMASTER: A second master site appeared");
263
case DB_REP_HOLDELECTION:
264
return ("DB_REP_HOLDELECTION: Need to hold an election");
265
case DB_REP_NEWMASTER:
266
return ("DB_REP_NEWMASTER: A new master has declared itself");
268
return ("DB_REP_NEWSITE: A new site has entered the system");
269
case DB_REP_OUTDATED:
271
("DB_REP_OUTDATED: Insufficient logs on master to recover");
273
return ("DB_REP_UNAVAIL: Unable to elect a master");
275
return ("DB_RUNRECOVERY: Fatal error, run database recovery");
276
case DB_SECONDARY_BAD:
278
("DB_SECONDARY_BAD: Secondary index item missing from primary");
280
return ("DB_VERIFY_BAD: Database verification failed");
284
* Room for a 64-bit number + slop. This buffer is only used
285
* if we're given an unknown error, which should never happen.
286
* Note, however, we're no longer thread-safe if it does.
288
static char ebuf[40];
290
(void)snprintf(ebuf, sizeof(ebuf), "Unknown error: %d", error);
298
* Standard DB error routine. The same as errx, except we don't write
299
* to stderr if no output mechanism was specified.
301
* PUBLIC: void __db_err __P((const DB_ENV *, const char *, ...));
305
__db_err(const DB_ENV *dbenv, const char *fmt, ...)
307
__db_err(dbenv, fmt, va_alist)
313
DB_REAL_ERR(dbenv, 0, 0, 0, fmt);
318
* Do the error message work for callback functions.
320
* PUBLIC: void __db_errcall
321
* PUBLIC: __P((const DB_ENV *, int, int, const char *, va_list));
324
__db_errcall(dbenv, error, error_set, fmt, ap)
326
int error, error_set;
331
char errbuf[2048]; /* !!!: END OF THE STACK DON'T TRUST SPRINTF. */
335
p += vsnprintf(errbuf, sizeof(errbuf), fmt, ap);
338
sizeof(errbuf) - (p - errbuf), ": %s", db_strerror(error));
341
* We're potentially manipulating strings handed us by the application,
342
* and on systems without a real snprintf() the sprintf() calls could
343
* have overflowed the buffer. We can't do anything about it now, but
344
* we don't want to return control to the application, we might have
345
* overwritten the stack with a Trojan horse. We're not trying to do
346
* anything recoverable here because systems without snprintf support
347
* are pretty rare anymore.
349
if ((size_t)(p - errbuf) > sizeof(errbuf)) {
350
(void)fprintf(stderr,
351
"Berkeley DB: error callback interface buffer overflow\n");
352
(void)fflush(stderr);
358
dbenv->db_errcall(dbenv->db_errpfx, errbuf);
363
* Do the error message work for FILE *s.
365
* PUBLIC: void __db_errfile
366
* PUBLIC: __P((const DB_ENV *, int, int, const char *, va_list));
369
__db_errfile(dbenv, error, error_set, fmt, ap)
371
int error, error_set;
377
fp = dbenv == NULL ||
378
dbenv->db_errfile == NULL ? stderr : dbenv->db_errfile;
380
if (dbenv != NULL && dbenv->db_errpfx != NULL)
381
(void)fprintf(fp, "%s: ", dbenv->db_errpfx);
383
(void)vfprintf(fp, fmt, ap);
385
(void)fprintf(fp, ": ");
388
(void)fprintf(fp, "%s", db_strerror(error));
389
(void)fprintf(fp, "\n");
395
* Write information into the DB log.
397
* PUBLIC: void __db_logmsg __P((const DB_ENV *,
398
* PUBLIC: DB_TXN *, const char *, u_int32_t, const char *, ...));
402
__db_logmsg(const DB_ENV *dbenv,
403
DB_TXN *txnid, const char *opname, u_int32_t flags, const char *fmt, ...)
405
__db_logmsg(dbenv, txnid, opname, flags, fmt, va_alist)
408
const char *opname, *fmt;
416
char __logbuf[2048]; /* !!!: END OF THE STACK DON'T TRUST SPRINTF. */
418
if (!LOGGING_ON(dbenv))
426
memset(&opdbt, 0, sizeof(opdbt));
427
opdbt.data = (void *)opname;
428
opdbt.size = (u_int32_t)(strlen(opname) + 1);
430
memset(&msgdbt, 0, sizeof(msgdbt));
431
msgdbt.data = __logbuf;
432
msgdbt.size = vsnprintf(__logbuf, sizeof(__logbuf), fmt, ap);
436
* Explicitly discard the const. Otherwise, we have to const DB_ENV
437
* references throughout the logging subsystem.
440
(DB_ENV *)dbenv, txnid, &lsn, flags, &opdbt, -1, &msgdbt, NULL, 0);
446
* __db_unknown_flag -- report internal error
448
* PUBLIC: int __db_unknown_flag __P((DB_ENV *, char *, u_int32_t));
451
__db_unknown_flag(dbenv, routine, flag)
456
__db_err(dbenv, "%s: Unknown flag: 0x%x", routine, flag);
462
* __db_unknown_type -- report internal error
464
* PUBLIC: int __db_unknown_type __P((DB_ENV *, char *, DBTYPE));
467
__db_unknown_type(dbenv, routine, type)
472
__db_err(dbenv, "%s: Unknown db type: 0x%x", routine, type);
479
* Check for common transaction errors.
481
* PUBLIC: int __db_check_txn __P((DB *, DB_TXN *, u_int32_t, int));
484
__db_check_txn(dbp, txn, assoc_lid, read_op)
495
* If we are in recovery or aborting a transaction, then we
496
* don't need to enforce the rules about dbp's not allowing
497
* transactional operations in non-transactional dbps and
498
* vica-versa. This happens all the time as the dbp during
499
* an abort may be transactional, but we undo operations
500
* outside a transaction since we're aborting.
502
if (IS_RECOVERING(dbenv) || F_ISSET(dbp, DB_AM_RECOVER))
506
* Check for common transaction errors:
507
* Failure to pass a transaction handle to a DB operation
508
* Failure to configure the DB handle in a proper environment
509
* Operation on a handle whose open commit hasn't completed.
511
* Read operations don't require a txn even if we've used one before
512
* with this handle, although if they do have a txn, we'd better be
516
if (!read_op && F_ISSET(dbp, DB_AM_TXN)) {
518
"DB handle previously used in transaction, missing transaction handle");
522
if (dbp->cur_lid >= TXN_MINIMUM)
525
if (dbp->cur_lid >= TXN_MINIMUM && dbp->cur_lid != txn->txnid)
529
return (__db_not_txn_env(dbenv));
531
if (!F_ISSET(dbp, DB_AM_TXN)) {
533
"Transaction specified for a DB handle opened outside a transaction");
539
* If dbp->associate_lid is not DB_LOCK_INVALIDID, that means we're in
540
* the middle of a DB->associate with DB_CREATE (i.e., a secondary index
543
* In addition to the usual transaction rules, we need to lock out
544
* non-transactional updates that aren't part of the associate (and
545
* thus are using some other locker ID).
547
* Transactional updates should simply block; from the time we
548
* decide to build the secondary until commit, we'll hold a write
549
* lock on all of its pages, so it should be safe to attempt to update
550
* the secondary in another transaction (presumably by updating the
553
if (!read_op && dbp->associate_lid != DB_LOCK_INVALIDID &&
554
txn != NULL && dbp->associate_lid != assoc_lid) {
556
"Operation forbidden while secondary index is being created");
563
"Transaction that opened the DB handle is still active");
568
* __db_not_txn_env --
569
* DB handle must be in an environment that supports transactions.
571
* PUBLIC: int __db_not_txn_env __P((DB_ENV *));
574
__db_not_txn_env(dbenv)
577
__db_err(dbenv, "DB environment not configured for transactions");