Update.
[glibc.git] / db2 / common / db_err.c
blob98a414279e1b1d258d826fc35a62618160b72c4e
1 /*-
2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997, 1998
5 * Sleepycat Software. All rights reserved.
6 */
8 #include "config.h"
10 #ifndef lint
11 static const char sccsid[] = "@(#)db_err.c 10.25 (Sleepycat) 5/2/98";
12 #endif /* not lint */
14 #ifndef NO_SYSTEM_INCLUDES
15 #include <sys/types.h>
17 #include <errno.h>
19 #ifdef __STDC__
20 #include <stdarg.h>
21 #else
22 #include <varargs.h>
23 #endif
24 #endif
26 #include "db_int.h"
27 #include "common_ext.h"
29 static int __db_keyempty __P((const DB_ENV *));
30 static int __db_rdonly __P((const DB_ENV *, const char *));
33 * __db_err --
34 * Standard DB error routine.
36 * PUBLIC: #ifdef __STDC__
37 * PUBLIC: void __db_err __P((const DB_ENV *dbenv, const char *fmt, ...));
38 * PUBLIC: #else
39 * PUBLIC: void __db_err();
40 * PUBLIC: #endif
42 void
43 #ifdef __STDC__
44 __db_err(const DB_ENV *dbenv, const char *fmt, ...)
45 #else
46 __db_err(dbenv, fmt, va_alist)
47 const DB_ENV *dbenv;
48 const char *fmt;
49 va_dcl
50 #endif
52 va_list ap;
53 char errbuf[2048]; /* XXX: END OF THE STACK DON'T TRUST SPRINTF. */
55 if (dbenv == NULL)
56 return;
58 #ifdef __STDC__
59 va_start(ap, fmt);
60 #else
61 va_start(ap);
62 #endif
63 if (dbenv->db_errcall != NULL) {
64 (void)vsnprintf(errbuf, sizeof(errbuf), fmt, ap);
65 dbenv->db_errcall(dbenv->db_errpfx, errbuf);
67 if (dbenv->db_errfile != NULL) {
68 if (dbenv->db_errpfx != NULL)
69 (void)fprintf(dbenv->db_errfile, "%s: ",
70 dbenv->db_errpfx);
71 (void)vfprintf(dbenv->db_errfile, fmt, ap);
72 (void)fprintf(dbenv->db_errfile, "\n");
73 (void)fflush(dbenv->db_errfile);
75 va_end(ap);
79 * XXX
80 * Provide ANSI C prototypes for the panic functions. Some compilers, (e.g.,
81 * MS VC 4.2) get upset if they aren't here, even though the K&R declaration
82 * appears before the assignment in the __db__panic() call.
84 static int __db_ecursor __P((DB *, DB_TXN *, DBC **));
85 static int __db_edel __P((DB *, DB_TXN *, DBT *, u_int32_t));
86 static int __db_efd __P((DB *, int *));
87 static int __db_egp __P((DB *, DB_TXN *, DBT *, DBT *, u_int32_t));
88 static int __db_estat __P((DB *, void *, void *(*)(size_t), u_int32_t));
89 static int __db_esync __P((DB *, u_int32_t));
92 * __db_ecursor --
93 * After-panic cursor routine.
95 static int
96 __db_ecursor(a, b, c)
97 DB *a;
98 DB_TXN *b;
99 DBC **c;
101 COMPQUIET(a, NULL);
102 COMPQUIET(b, NULL);
103 COMPQUIET(c, NULL);
105 return (EPERM);
109 * __db_edel --
110 * After-panic delete routine.
112 static int
113 __db_edel(a, b, c, d)
114 DB *a;
115 DB_TXN *b;
116 DBT *c;
117 u_int32_t d;
119 COMPQUIET(a, NULL);
120 COMPQUIET(b, NULL);
121 COMPQUIET(c, NULL);
122 COMPQUIET(d, 0);
124 return (EPERM);
128 * __db_efd --
129 * After-panic fd routine.
131 static int
132 __db_efd(a, b)
133 DB *a;
134 int *b;
136 COMPQUIET(a, NULL);
137 COMPQUIET(b, NULL);
139 return (EPERM);
143 * __db_egp --
144 * After-panic get/put routine.
146 static int
147 __db_egp(a, b, c, d, e)
148 DB *a;
149 DB_TXN *b;
150 DBT *c, *d;
151 u_int32_t e;
153 COMPQUIET(a, NULL);
154 COMPQUIET(b, NULL);
155 COMPQUIET(c, NULL);
156 COMPQUIET(d, NULL);
157 COMPQUIET(e, 0);
159 return (EPERM);
163 * __db_estat --
164 * After-panic stat routine.
166 static int
167 __db_estat(a, b, c, d)
168 DB *a;
169 void *b;
170 void *(*c) __P((size_t));
171 u_int32_t d;
173 COMPQUIET(a, NULL);
174 COMPQUIET(b, NULL);
175 COMPQUIET(c, NULL);
176 COMPQUIET(d, 0);
178 return (EPERM);
182 * __db_esync --
183 * After-panic sync routine.
185 static int
186 __db_esync(a, b)
187 DB *a;
188 u_int32_t b;
190 COMPQUIET(a, NULL);
191 COMPQUIET(b, 0);
193 return (EPERM);
197 * __db_panic --
198 * Lock out the tree due to unrecoverable error.
200 * PUBLIC: int __db_panic __P((DB *));
203 __db_panic(dbp)
204 DB *dbp;
207 * XXX
208 * We should shut down all of the process's cursors, too.
210 * We should call mpool and have it shut down the file, so we get
211 * other processes sharing this file as well.
213 * Chaos reigns within.
214 * Reflect, repent, and reboot.
215 * Order shall return.
217 dbp->cursor = __db_ecursor;
218 dbp->del = __db_edel;
219 dbp->fd = __db_efd;
220 dbp->get = __db_egp;
221 dbp->put = __db_egp;
222 dbp->stat = __db_estat;
223 dbp->sync = __db_esync;
225 return (EPERM);
228 /* Check for invalid flags. */
229 #undef DB_CHECK_FLAGS
230 #define DB_CHECK_FLAGS(dbenv, name, flags, ok_flags) \
231 if ((flags) & ~(ok_flags)) \
232 return (__db_ferr(dbenv, name, 0));
233 /* Check for invalid flag combinations. */
234 #undef DB_CHECK_FCOMBO
235 #define DB_CHECK_FCOMBO(dbenv, name, flags, flag1, flag2) \
236 if ((flags) & (flag1) && (flags) & (flag2)) \
237 return (__db_ferr(dbenv, name, 1));
240 * __db_fchk --
241 * General flags checking routine.
243 * PUBLIC: int __db_fchk __P((DB_ENV *, const char *, u_int32_t, u_int32_t));
246 __db_fchk(dbenv, name, flags, ok_flags)
247 DB_ENV *dbenv;
248 const char *name;
249 u_int32_t flags, ok_flags;
251 DB_CHECK_FLAGS(dbenv, name, flags, ok_flags);
252 return (0);
256 * __db_fcchk --
257 * General combination flags checking routine.
259 * PUBLIC: int __db_fcchk
260 * PUBLIC: __P((DB_ENV *, const char *, u_int32_t, u_int32_t, u_int32_t));
263 __db_fcchk(dbenv, name, flags, flag1, flag2)
264 DB_ENV *dbenv;
265 const char *name;
266 u_int32_t flags, flag1, flag2;
268 DB_CHECK_FCOMBO(dbenv, name, flags, flag1, flag2);
269 return (0);
273 * __db_cdelchk --
274 * Common cursor delete argument checking routine.
276 * PUBLIC: int __db_cdelchk __P((const DB *, u_int32_t, int, int));
279 __db_cdelchk(dbp, flags, isrdonly, isvalid)
280 const DB *dbp;
281 u_int32_t flags;
282 int isrdonly, isvalid;
284 /* Check for changes to a read-only tree. */
285 if (isrdonly)
286 return (__db_rdonly(dbp->dbenv, "c_del"));
288 /* Check for invalid dbc->c_del() function flags. */
289 DB_CHECK_FLAGS(dbp->dbenv, "c_del", flags, 0);
292 * The cursor must be initialized, return -1 for an invalid cursor,
293 * otherwise 0.
295 return (isvalid ? 0 : EINVAL);
299 * __db_cgetchk --
300 * Common cursor get argument checking routine.
302 * PUBLIC: int __db_cgetchk __P((const DB *, DBT *, DBT *, u_int32_t, int));
305 __db_cgetchk(dbp, key, data, flags, isvalid)
306 const DB *dbp;
307 DBT *key, *data;
308 u_int32_t flags;
309 int isvalid;
311 int key_einval, key_flags;
313 key_flags = key_einval = 0;
315 /* Check for invalid dbc->c_get() function flags. */
316 switch (flags) {
317 case DB_CURRENT:
318 case DB_FIRST:
319 case DB_LAST:
320 case DB_NEXT:
321 case DB_PREV:
322 key_flags = 1;
323 break;
324 case DB_SET_RANGE:
325 key_einval = key_flags = 1;
326 break;
327 case DB_SET:
328 key_einval = 1;
329 break;
330 case DB_GET_RECNO:
331 if (!F_ISSET(dbp, DB_BT_RECNUM))
332 goto err;
333 break;
334 case DB_SET_RECNO:
335 if (!F_ISSET(dbp, DB_BT_RECNUM))
336 goto err;
337 key_einval = key_flags = 1;
338 break;
339 default:
340 err: return (__db_ferr(dbp->dbenv, "c_get", 0));
343 /* Check for invalid key/data flags. */
344 if (key_flags)
345 DB_CHECK_FLAGS(dbp->dbenv, "key", key->flags,
346 DB_DBT_MALLOC | DB_DBT_USERMEM | DB_DBT_PARTIAL);
347 DB_CHECK_FLAGS(dbp->dbenv, "data", data->flags,
348 DB_DBT_MALLOC | DB_DBT_USERMEM | DB_DBT_PARTIAL);
350 /* Check dbt's for valid flags when multi-threaded. */
351 if (F_ISSET(dbp, DB_AM_THREAD)) {
352 if (!F_ISSET(data, DB_DBT_USERMEM | DB_DBT_MALLOC))
353 return (__db_ferr(dbp->dbenv, "threaded data", 1));
354 if (key_flags &&
355 !F_ISSET(key, DB_DBT_USERMEM | DB_DBT_MALLOC))
356 return (__db_ferr(dbp->dbenv, "threaded key", 1));
359 /* Check for missing keys. */
360 if (key_einval && (key->data == NULL || key->size == 0))
361 return (__db_keyempty(dbp->dbenv));
364 * The cursor must be initialized for DB_CURRENT, return -1 for an
365 * invalid cursor, otherwise 0.
367 return (isvalid || flags != DB_CURRENT ? 0 : EINVAL);
371 * __db_cputchk --
372 * Common cursor put argument checking routine.
374 * PUBLIC: int __db_cputchk __P((const DB *,
375 * PUBLIC: const DBT *, DBT *, u_int32_t, int, int));
378 __db_cputchk(dbp, key, data, flags, isrdonly, isvalid)
379 const DB *dbp;
380 const DBT *key;
381 DBT *data;
382 u_int32_t flags;
383 int isrdonly, isvalid;
385 int key_einval, key_flags;
387 /* Check for changes to a read-only tree. */
388 if (isrdonly)
389 return (__db_rdonly(dbp->dbenv, "c_put"));
391 /* Check for invalid dbc->c_put() function flags. */
392 key_einval = key_flags = 0;
393 switch (flags) {
394 case DB_AFTER:
395 case DB_BEFORE:
396 if (dbp->type == DB_RECNO && !F_ISSET(dbp, DB_RE_RENUMBER))
397 goto err;
398 if (dbp->type != DB_RECNO && !F_ISSET(dbp, DB_AM_DUP))
399 goto err;
400 break;
401 case DB_CURRENT:
402 break;
403 case DB_KEYFIRST:
404 case DB_KEYLAST:
405 if (dbp->type == DB_RECNO)
406 goto err;
407 key_einval = key_flags = 1;
408 break;
409 default:
410 err: return (__db_ferr(dbp->dbenv, "c_put", 0));
413 /* Check for invalid key/data flags. */
414 if (key_flags)
415 DB_CHECK_FLAGS(dbp->dbenv, "key", key->flags,
416 DB_DBT_MALLOC | DB_DBT_USERMEM | DB_DBT_PARTIAL);
417 DB_CHECK_FLAGS(dbp->dbenv, "data", data->flags,
418 DB_DBT_MALLOC | DB_DBT_USERMEM | DB_DBT_PARTIAL);
420 /* Check for missing keys. */
421 if (key_einval && (key->data == NULL || key->size == 0))
422 return (__db_keyempty(dbp->dbenv));
425 * The cursor must be initialized for anything other than DB_KEYFIRST
426 * and DB_KEYLAST, return -1 for an invalid cursor, otherwise 0.
428 return (isvalid ||
429 (flags != DB_KEYFIRST && flags != DB_KEYLAST) ? 0 : EINVAL);
433 * __db_delchk --
434 * Common delete argument checking routine.
436 * PUBLIC: int __db_delchk __P((const DB *, DBT *, u_int32_t, int));
439 __db_delchk(dbp, key, flags, isrdonly)
440 const DB *dbp;
441 DBT *key;
442 u_int32_t flags;
443 int isrdonly;
445 /* Check for changes to a read-only tree. */
446 if (isrdonly)
447 return (__db_rdonly(dbp->dbenv, "delete"));
449 /* Check for invalid db->del() function flags. */
450 DB_CHECK_FLAGS(dbp->dbenv, "delete", flags, 0);
452 /* Check for missing keys. */
453 if (key->data == NULL || key->size == 0)
454 return (__db_keyempty(dbp->dbenv));
456 return (0);
460 * __db_getchk --
461 * Common get argument checking routine.
463 * PUBLIC: int __db_getchk __P((const DB *, const DBT *, DBT *, u_int32_t));
466 __db_getchk(dbp, key, data, flags)
467 const DB *dbp;
468 const DBT *key;
469 DBT *data;
470 u_int32_t flags;
472 /* Check for invalid db->get() function flags. */
473 DB_CHECK_FLAGS(dbp->dbenv,
474 "get", flags, F_ISSET(dbp, DB_BT_RECNUM) ? DB_SET_RECNO : 0);
476 /* Check for invalid key/data flags. */
477 DB_CHECK_FLAGS(dbp->dbenv, "key", key->flags, 0);
478 DB_CHECK_FLAGS(dbp->dbenv, "data", data->flags,
479 DB_DBT_MALLOC | DB_DBT_USERMEM | DB_DBT_PARTIAL);
480 DB_CHECK_FCOMBO(dbp->dbenv,
481 "data", data->flags, DB_DBT_MALLOC, DB_DBT_USERMEM);
482 if (F_ISSET(dbp, DB_AM_THREAD) &&
483 !F_ISSET(data, DB_DBT_MALLOC | DB_DBT_USERMEM))
484 return (__db_ferr(dbp->dbenv, "threaded data", 1));
486 /* Check for missing keys. */
487 if (key->data == NULL || key->size == 0)
488 return (__db_keyempty(dbp->dbenv));
490 return (0);
494 * __db_putchk --
495 * Common put argument checking routine.
497 * PUBLIC: int __db_putchk
498 * PUBLIC: __P((const DB *, DBT *, const DBT *, u_int32_t, int, int));
501 __db_putchk(dbp, key, data, flags, isrdonly, isdup)
502 const DB *dbp;
503 DBT *key;
504 const DBT *data;
505 u_int32_t flags;
506 int isrdonly, isdup;
508 /* Check for changes to a read-only tree. */
509 if (isrdonly)
510 return (__db_rdonly(dbp->dbenv, "put"));
512 /* Check for invalid db->put() function flags. */
513 DB_CHECK_FLAGS(dbp->dbenv, "put", flags,
514 DB_NOOVERWRITE | (dbp->type == DB_RECNO ? DB_APPEND : 0));
516 /* Check for invalid key/data flags. */
517 DB_CHECK_FLAGS(dbp->dbenv, "key", key->flags, 0);
518 DB_CHECK_FLAGS(dbp->dbenv, "data", data->flags,
519 DB_DBT_MALLOC | DB_DBT_USERMEM | DB_DBT_PARTIAL);
520 DB_CHECK_FCOMBO(dbp->dbenv,
521 "data", data->flags, DB_DBT_MALLOC, DB_DBT_USERMEM);
523 /* Check for missing keys. */
524 if (key->data == NULL || key->size == 0)
525 return (__db_keyempty(dbp->dbenv));
527 /* Check for partial puts in the presence of duplicates. */
528 if (isdup && F_ISSET(data, DB_DBT_PARTIAL)) {
529 __db_err(dbp->dbenv,
530 "a partial put in the presence of duplicates requires a cursor operation");
531 return (EINVAL);
534 return (0);
538 * __db_statchk --
539 * Common stat argument checking routine.
541 * PUBLIC: int __db_statchk __P((const DB *, u_int32_t));
544 __db_statchk(dbp, flags)
545 const DB *dbp;
546 u_int32_t flags;
548 /* Check for invalid db->stat() function flags. */
549 DB_CHECK_FLAGS(dbp->dbenv, "stat", flags, DB_RECORDCOUNT);
551 if (LF_ISSET(DB_RECORDCOUNT) &&
552 dbp->type == DB_BTREE && !F_ISSET(dbp, DB_BT_RECNUM))
553 return (__db_ferr(dbp->dbenv, "stat", 0));
555 return (0);
559 * __db_syncchk --
560 * Common sync argument checking routine.
562 * PUBLIC: int __db_syncchk __P((const DB *, u_int32_t));
565 __db_syncchk(dbp, flags)
566 const DB *dbp;
567 u_int32_t flags;
569 /* Check for invalid db->sync() function flags. */
570 DB_CHECK_FLAGS(dbp->dbenv, "sync", flags, 0);
572 return (0);
576 * __db_ferr --
577 * Common flag errors.
579 * PUBLIC: int __db_ferr __P((const DB_ENV *, const char *, int));
582 __db_ferr(dbenv, name, iscombo)
583 const DB_ENV *dbenv;
584 const char *name;
585 int iscombo;
587 __db_err(dbenv, "illegal flag %sspecified to %s",
588 iscombo ? "combination " : "", name);
589 return (EINVAL);
593 * __db_rdonly --
594 * Common readonly message.
596 static int
597 __db_rdonly(dbenv, name)
598 const DB_ENV *dbenv;
599 const char *name;
601 __db_err(dbenv, "%s: attempt to modify a read-only tree", name);
602 return (EACCES);
606 * __db_keyempty --
607 * Common missing or empty key value message.
609 static int
610 __db_keyempty(dbenv)
611 const DB_ENV *dbenv;
613 __db_err(dbenv, "missing or empty key value specified");
614 return (EINVAL);