1*4500627aSkrw /* $OpenBSD: db.c,v 1.4 2017/01/21 08:18:53 krw Exp $ */
283dc3f48Sespie
383dc3f48Sespie /*-
483dc3f48Sespie * Copyright (c) 1991, 1993
583dc3f48Sespie * The Regents of the University of California. All rights reserved.
683dc3f48Sespie *
783dc3f48Sespie * Redistribution and use in source and binary forms, with or without
883dc3f48Sespie * modification, are permitted provided that the following conditions
983dc3f48Sespie * are met:
1083dc3f48Sespie * 1. Redistributions of source code must retain the above copyright
1183dc3f48Sespie * notice, this list of conditions and the following disclaimer.
1283dc3f48Sespie * 2. Redistributions in binary form must reproduce the above copyright
1383dc3f48Sespie * notice, this list of conditions and the following disclaimer in the
1483dc3f48Sespie * documentation and/or other materials provided with the distribution.
1583dc3f48Sespie * 3. Neither the name of the University nor the names of its contributors
1683dc3f48Sespie * may be used to endorse or promote products derived from this software
1783dc3f48Sespie * without specific prior written permission.
1883dc3f48Sespie *
1983dc3f48Sespie * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2083dc3f48Sespie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2183dc3f48Sespie * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2283dc3f48Sespie * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2383dc3f48Sespie * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2483dc3f48Sespie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2583dc3f48Sespie * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2683dc3f48Sespie * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2783dc3f48Sespie * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2883dc3f48Sespie * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2983dc3f48Sespie * SUCH DAMAGE.
3083dc3f48Sespie */
3183dc3f48Sespie
3283dc3f48Sespie #include <sys/types.h>
3383dc3f48Sespie
3483dc3f48Sespie #include <errno.h>
3583dc3f48Sespie #include <fcntl.h>
3683dc3f48Sespie #include <stddef.h>
3783dc3f48Sespie #include <stdio.h>
3883dc3f48Sespie
3983dc3f48Sespie #include <db.h>
4083dc3f48Sespie
4183dc3f48Sespie static int __dberr(void);
4283dc3f48Sespie
4383dc3f48Sespie DB *
dbopen(const char * fname,int flags,int mode,DBTYPE type,const void * openinfo)4483dc3f48Sespie dbopen(const char *fname, int flags, int mode, DBTYPE type,
4583dc3f48Sespie const void *openinfo)
4683dc3f48Sespie {
4783dc3f48Sespie
4883dc3f48Sespie #define DB_FLAGS (DB_LOCK | DB_SHMEM | DB_TXN)
4983dc3f48Sespie #define USE_OPEN_FLAGS \
5083dc3f48Sespie (O_CREAT | O_EXCL | O_EXLOCK | O_NOFOLLOW | O_NONBLOCK | \
5183dc3f48Sespie O_RDONLY | O_RDWR | O_SHLOCK | O_SYNC | O_TRUNC)
5283dc3f48Sespie
5383dc3f48Sespie if ((flags & ~(USE_OPEN_FLAGS | DB_FLAGS)) == 0)
5483dc3f48Sespie switch (type) {
5583dc3f48Sespie case DB_HASH:
5683dc3f48Sespie return (__hash_open(fname, flags & USE_OPEN_FLAGS,
5783dc3f48Sespie mode, openinfo, flags & DB_FLAGS));
589313a659Skrw default:
599313a659Skrw break;
6083dc3f48Sespie }
6183dc3f48Sespie errno = EINVAL;
6283dc3f48Sespie return (NULL);
6383dc3f48Sespie }
64a52dee1bSkettenis DEF_WEAK(dbopen);
6583dc3f48Sespie
6683dc3f48Sespie static int
__dberr(void)6783dc3f48Sespie __dberr(void)
6883dc3f48Sespie {
6983dc3f48Sespie return (RET_ERROR);
7083dc3f48Sespie }
7183dc3f48Sespie
7283dc3f48Sespie /*
7383dc3f48Sespie * __DBPANIC -- Stop.
7483dc3f48Sespie *
7583dc3f48Sespie * Parameters:
7683dc3f48Sespie * dbp: pointer to the DB structure.
7783dc3f48Sespie */
7883dc3f48Sespie void
__dbpanic(DB * dbp)7983dc3f48Sespie __dbpanic(DB *dbp)
8083dc3f48Sespie {
8183dc3f48Sespie /* The only thing that can succeed is a close. */
8283dc3f48Sespie dbp->del = (int (*)(const struct __db *, const DBT*, u_int))__dberr;
8383dc3f48Sespie dbp->fd = (int (*)(const struct __db *))__dberr;
8483dc3f48Sespie dbp->get = (int (*)(const struct __db *, const DBT*, DBT *, u_int))__dberr;
8583dc3f48Sespie dbp->put = (int (*)(const struct __db *, DBT *, const DBT *, u_int))__dberr;
8683dc3f48Sespie dbp->seq = (int (*)(const struct __db *, DBT *, DBT *, u_int))__dberr;
8783dc3f48Sespie dbp->sync = (int (*)(const struct __db *, u_int))__dberr;
8883dc3f48Sespie }
89