1 /*-
2  * Copyright (c) 1991, 1993
3  *  The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. ***REMOVED*** - see
14  *    ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
15  * 4. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #if defined(LIBC_SCCS) && !defined(lint)
33 static char sccsid[] = "@(#)db.c    8.4 (Berkeley) 2/21/94";
34 #endif /* LIBC_SCCS and not lint */
35 
36 #ifndef __DBINTERFACE_PRIVATE
37 #define __DBINTERFACE_PRIVATE
38 #endif
39 #ifdef macintosh
40 #include <unix.h>
41 #else
42 #include <sys/types.h>
43 #endif
44 
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <stddef.h>
48 #include <stdio.h>
49 
50 #include "mcom_db.h"
51 
52 /* a global flag that locks closed all databases */
53 int all_databases_locked_closed = 0;
54 
55 /* set or unset a global lock flag to disable the
56  * opening of any DBM file
57  */
58 void
dbSetOrClearDBLock(DBLockFlagEnum type)59 dbSetOrClearDBLock(DBLockFlagEnum type)
60 {
61     if (type == LockOutDatabase)
62         all_databases_locked_closed = 1;
63     else
64         all_databases_locked_closed = 0;
65 }
66 
67 DB *
dbopen(const char * fname,int flags,int mode,DBTYPE type,const void * openinfo)68 dbopen(const char *fname, int flags, int mode, DBTYPE type, const void *openinfo)
69 {
70 
71     /* lock out all file databases.  Let in-memory databases through
72      */
73     if (all_databases_locked_closed && fname) {
74         errno = EINVAL;
75         return (NULL);
76     }
77 
78 #define DB_FLAGS (DB_LOCK | DB_SHMEM | DB_TXN)
79 
80 #if 0 /* most systems don't have EXLOCK and SHLOCK */
81 #define USE_OPEN_FLAGS                                     \
82     (O_CREAT | O_EXCL | O_EXLOCK | O_NONBLOCK | O_RDONLY | \
83      O_RDWR | O_SHLOCK | O_TRUNC)
84 #else
85 #define USE_OPEN_FLAGS             \
86     (O_CREAT | O_EXCL | O_RDONLY | \
87      O_RDWR | O_TRUNC)
88 #endif
89 
90     if ((flags & ~(USE_OPEN_FLAGS | DB_FLAGS)) == 0)
91         switch (type) {
92 /* we don't need btree and recno right now */
93 #if 0
94             case DB_BTREE:
95                 return (dbm_bt_open(fname, flags & USE_OPEN_FLAGS,
96                     mode, openinfo, flags & DB_FLAGS));
97             case DB_RECNO:
98                 return (dbm_rec_open(fname, flags & USE_OPEN_FLAGS,
99                     mode, openinfo, flags & DB_FLAGS));
100 #endif
101 
102             case DB_HASH:
103                 return (dbm_hash_open(fname, flags & USE_OPEN_FLAGS,
104                                       mode, (const HASHINFO *)openinfo, flags & DB_FLAGS));
105             default:
106                 break;
107         }
108     errno = EINVAL;
109     return (NULL);
110 }
111 
112 static int
dbm_dberr()113 dbm_dberr()
114 {
115     return (RET_ERROR);
116 }
117 
118 /*
119  * __DBPANIC -- Stop.
120  *
121  * Parameters:
122  *  dbp:    pointer to the DB structure.
123  */
124 void
dbm_dbpanic(DB * dbp)125 dbm_dbpanic(DB *dbp)
126 {
127     /* The only thing that can succeed is a close. */
128     dbp->del = (int (*)(const struct dbm_db *, const DBT *, uint))dbm_dberr;
129     dbp->fd = (int (*)(const struct dbm_db *))dbm_dberr;
130     dbp->get = (int (*)(const struct dbm_db *, const DBT *, DBT *, uint))dbm_dberr;
131     dbp->put = (int (*)(const struct dbm_db *, DBT *, const DBT *, uint))dbm_dberr;
132     dbp->seq = (int (*)(const struct dbm_db *, DBT *, DBT *, uint))dbm_dberr;
133     dbp->sync = (int (*)(const struct dbm_db *, uint))dbm_dberr;
134 }
135