update from main archive 961001
[glibc.git] / nss / nss_db / db-XXX.c
blob0c41761d679e05eb6367aca3563ecdc7eadbf3f0
1 /* Common code for DB-based databases in nss_db module.
2 Copyright (C) 1996 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA. */
20 #include <db.h>
21 #include <fcntl.h>
22 #include <libc-lock.h>
23 #include "nsswitch.h"
25 /* These symbols are defined by the including source file:
27 ENTNAME -- database name of the structure and functions (hostent, pwent).
28 STRUCTURE -- struct name, define only if not ENTNAME (passwd, group).
29 DATABASE -- database file name, ("hosts", "passwd")
31 NEED_H_ERRNO - defined iff an arg `int *herrnop' is used.
34 #define ENTNAME_r CONCAT(ENTNAME,_r)
36 #include <paths.h>
37 #define DBFILE _PATH_VARDB DATABASE ".db"
39 #ifdef NEED_H_ERRNO
40 #define H_ERRNO_PROTO , int *herrnop
41 #define H_ERRNO_ARG , herrnop
42 #define H_ERRNO_SET(val) (*herrnop = (val))
43 #else
44 #define H_ERRNO_PROTO
45 #define H_ERRNO_ARG
46 #define H_ERRNO_SET(val) ((void) 0)
47 #endif
49 /* Locks the static variables in this file. */
50 __libc_lock_define_initialized (static, lock)
52 /* Maintenance of the shared handle open on the database. */
54 static DB *db;
55 static int keep_db;
56 static unsigned int entidx; /* Index for `getENTNAME'. */
58 /* Open database file if not already opened. */
59 static enum nss_status
60 internal_setent (int stayopen)
62 enum nss_status status = NSS_STATUS_SUCCESS;
64 if (db == NULL)
66 db = dbopen (DBFILE, O_RDONLY, 0, DB_BTREE, NULL);
68 if (db == NULL)
69 status = NSS_STATUS_UNAVAIL;
72 /* Remember STAYOPEN flag. */
73 if (db != NULL)
74 keep_db |= stayopen;
76 return status;
80 /* Thread-safe, exported version of that. */
81 enum nss_status
82 CONCAT(_nss_db_set,ENTNAME) (int stayopen)
84 enum nss_status status;
86 __libc_lock_lock (lock);
88 status = internal_setent (stayopen);
90 /* Reset the sequential index. */
91 entidx = 0;
93 __libc_lock_unlock (lock);
95 return status;
99 /* Close the database file. */
100 static void
101 internal_endent (void)
103 if (db != NULL)
105 (*db->close) (db);
106 db = NULL;
111 /* Thread-safe, exported version of that. */
112 enum nss_status
113 CONCAT(_nss_db_end,ENTNAME) (void)
115 __libc_lock_lock (lock);
117 internal_endent ();
119 /* Reset STAYOPEN flag. */
120 keep_db = 0;
122 __libc_lock_unlock (lock);
124 return NSS_STATUS_SUCCESS;
127 /* Do a database lookup for KEY. */
128 static enum nss_status
129 lookup (const DBT *key, struct STRUCTURE *result,
130 void *buffer, int buflen H_ERRNO_PROTO)
132 enum nss_status status;
133 DBT value;
135 /* Open the database. */
136 status = internal_setent (keep_db);
137 if (status != NSS_STATUS_SUCCESS)
138 return status;
140 /* Succeed iff it matches a value that parses correctly. */
141 status = (((*db->get) (db, key, &value, 0) == 0 &&
142 parse_line (value.data, result, buffer, buflen))
143 ? NSS_STATUS_SUCCESS : NSS_STATUS_NOTFOUND);
145 if (! keep_db)
146 internal_endent ();
148 return status;
152 /* Macro for defining lookup functions for this DB-based database.
154 NAME is the name of the lookup; e.g. `pwnam'.
156 KEYPATTERN gives `printf' args to construct a key string;
157 e.g. `(".%s", name)'.
159 KEYSIZE gives the allocation size of a buffer to construct it in;
160 e.g. `1 + strlen (name)'.
162 PROTO describes the arguments for the lookup key;
163 e.g. `const char *name'.
165 BREAK_IF_MATCH is ignored, but used by ../nss_files/files-XXX.c. */
167 #define DB_LOOKUP(name, keysize, keypattern, break_if_match, proto...) \
168 enum nss_status \
169 _nss_db_get##name##_r (proto, \
170 struct STRUCTURE *result, \
171 char *buffer, int buflen H_ERRNO_PROTO) \
173 DBT key; \
174 enum nss_status status; \
175 const size_t size = (keysize); \
176 key.data = __alloca (size); \
177 key.size = KEYPRINTF keypattern; \
178 __libc_lock_lock (lock); \
179 status = lookup (&key, result, buffer, buflen H_ERRNO_ARG); \
180 __libc_lock_unlock (lock); \
181 return status; \
184 #define KEYPRINTF(pattern, args...) snprintf (key.data, size, pattern ,##args)
189 /* Return the next entry from the database file, doing locking. */
190 enum nss_status
191 CONCAT(_nss_db_get,ENTNAME_r) (struct STRUCTURE *result,
192 char *buffer, int buflen H_ERRNO_PROTO)
194 /* Return next entry in host file. */
195 enum nss_status status;
196 char buf[20];
197 DBT key;
199 __libc_lock_lock (lock);
200 key.size = 1 + snprintf (key.data = buf, sizeof buf, "0%u", entidx++);
201 status = lookup (&key, result, buffer, buflen H_ERRNO_ARG);
202 __libc_lock_unlock (lock);
204 return status;