1 /* Common code for DB-based databases in nss_db module.
2 Copyright (C) 1996-2000, 2011 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
22 #include <bits/libc-lock.h>
26 /* The hashing function we use. */
27 #include "../intl/hash-string.h"
29 /* These symbols are defined by the including source file:
31 ENTNAME -- database name of the structure and functions (hostent, pwent).
32 STRUCTURE -- struct name, define only if not ENTNAME (passwd, group).
33 DATABASE -- database file name, ("hosts", "passwd")
35 NEED_H_ERRNO - defined iff an arg `int *herrnop' is used.
38 #define ENTNAME_r CONCAT(ENTNAME,_r)
41 #define DBFILE _PATH_VARDB DATABASE ".db"
44 # define H_ERRNO_PROTO , int *herrnop
45 # define H_ERRNO_ARG , herrnop
46 # define H_ERRNO_SET(val) (*herrnop = (val))
48 # define H_ERRNO_PROTO
50 # define H_ERRNO_SET(val) ((void) 0)
53 /* State for this database. */
54 static struct nss_db_map state
;
55 /* Lock to protect the state and global variables. */
56 __libc_lock_define (static , lock
);
58 /* Maintenance of the shared handle open on the database. */
60 static const char *entidx
;
63 /* Open the database. */
65 CONCAT(_nss_db_set
,ENTNAME
) (int stayopen
)
67 enum nss_status status
;
69 __libc_lock_lock (lock
);
71 status
= internal_setent (DBFILE
, &state
);
73 if (status
== NSS_STATUS_SUCCESS
)
75 /* Remember STAYOPEN flag. */
78 /* Reset the sequential index. */
79 entidx
= (const char *) state
.header
+ state
.header
->valstroffset
;
82 __libc_lock_unlock (lock
);
90 CONCAT(_nss_db_end
,ENTNAME
) (void)
92 __libc_lock_lock (lock
);
94 internal_endent (&state
);
96 /* Reset STAYOPEN flag. */
99 __libc_lock_unlock (lock
);
101 return NSS_STATUS_SUCCESS
;
105 /* Macro for defining lookup functions for this DB-based database.
107 NAME is the name of the lookup; e.g. `pwnam'.
109 DB_CHAR is index indicator for the database.
111 KEYPATTERN gives `printf' args to construct a key string;
114 KEYSIZE gives the allocation size of a buffer to construct it in;
115 e.g. `1 + sizeof (id) * 4'.
117 PROTO is the potentially empty list of other parameters.
119 BREAK_IF_MATCH is a block of code which compares `struct STRUCTURE *result'
120 to the lookup key arguments and does `break;' if they match. */
122 #define DB_LOOKUP(name, db_char, keysize, keypattern, break_if_match, proto...)\
124 _nss_db_get##name##_r (proto, struct STRUCTURE *result, \
125 char *buffer, size_t buflen, int *errnop H_ERRNO_PROTO)\
127 struct parser_data *data = (void *) buffer; \
129 if (buflen < sizeof *data) \
132 H_ERRNO_SET (NETDB_INTERNAL); \
133 return NSS_STATUS_TRYAGAIN; \
136 struct nss_db_map state = { NULL, 0 }; \
137 enum nss_status status = internal_setent (DBFILE, &state); \
138 if (status != NSS_STATUS_SUCCESS) \
141 H_ERRNO_SET (NETDB_INTERNAL); \
145 const struct nss_db_header *header = state.header; \
147 for (i = 0; i < header->ndbs; ++i) \
148 if (header->dbs[i].id == db_char) \
150 if (i == header->ndbs) \
152 status = NSS_STATUS_UNAVAIL; \
157 if (db_char == '.') \
158 key = (char *) IGNOREPATTERN keypattern; \
161 const size_t size = (keysize) + 1; \
162 key = alloca (size); \
164 KEYPRINTF keypattern; \
167 const stridx_t *hashtable \
168 = (const stridx_t *) ((const char *) header \
169 + header->dbs[i].hashoffset); \
170 const char *valstrtab = (const char *) header + header->valstroffset; \
171 uint32_t hashval = __hash_string (key); \
172 size_t hidx = hashval % header->dbs[i].hashsize; \
173 size_t hval2 = 1 + hashval % (header->dbs[i].hashsize - 2); \
175 status = NSS_STATUS_NOTFOUND; \
176 while (hashtable[hidx] != ~((stridx_t) 0)) \
178 const char *valstr = valstrtab + hashtable[hidx]; \
179 size_t len = strlen (valstr) + 1; \
182 /* No room to copy the data to. */ \
184 H_ERRNO_SET (NETDB_INTERNAL); \
185 status = NSS_STATUS_TRYAGAIN; \
189 /* Copy the string to a place where it can be modified. */ \
190 char *p = memcpy (buffer, valstr, len); \
192 int err = parse_line (p, result, data, buflen, errnop EXTRA_ARGS); \
195 status = NSS_STATUS_SUCCESS; \
197 status = NSS_STATUS_NOTFOUND; \
199 else if (err == -1) \
201 H_ERRNO_SET (NETDB_INTERNAL); \
202 status = NSS_STATUS_TRYAGAIN; \
206 if ((hidx += hval2) >= header->dbs[i].hashsize) \
207 hidx -= header->dbs[i].hashsize; \
210 if (status == NSS_STATUS_NOTFOUND) \
211 H_ERRNO_SET (HOST_NOT_FOUND); \
214 internal_endent (&state); \
219 #define KEYPRINTF(pattern, args...) snprintf (key, size, pattern ,##args)
220 #define IGNOREPATTERN(pattern, arg1, args...) (char *) (uintptr_t) arg1
225 /* Return the next entry from the database file, doing locking. */
227 CONCAT(_nss_db_get
,ENTNAME_r
) (struct STRUCTURE
*result
, char *buffer
,
228 size_t buflen
, int *errnop H_ERRNO_PROTO
)
230 /* Return next entry in host file. */
231 enum nss_status status
;
232 struct parser_data
*data
= (void *) buffer
;
234 if (buflen
< sizeof *data
)
237 H_ERRNO_SET (NETDB_INTERNAL
);
238 return NSS_STATUS_TRYAGAIN
;
241 __libc_lock_lock (lock
);
243 if (state
.header
== NULL
)
245 status
= internal_setent (DBFILE
, &state
);
246 if (status
!= NSS_STATUS_SUCCESS
)
249 H_ERRNO_SET (NETDB_INTERNAL
);
254 status
= NSS_STATUS_UNAVAIL
;
255 if (state
.header
!= MAP_FAILED
)
257 const char *const end
= ((const char *) state
.header
258 + state
.header
->valstroffset
259 + state
.header
->valstrlen
);
262 const char *next
= rawmemchr (entidx
, '\0') + 1;
263 size_t len
= next
- entidx
;
267 /* No room to copy the data to. */
269 H_ERRNO_SET (NETDB_INTERNAL
);
270 status
= NSS_STATUS_TRYAGAIN
;
274 /* Copy the string to a place where it can be modified. */
275 char *p
= memcpy (buffer
, entidx
, len
);
277 int err
= parse_line (p
, result
, data
, buflen
, errnop EXTRA_ARGS
);
281 status
= NSS_STATUS_SUCCESS
;
287 H_ERRNO_SET (HOST_NOT_FOUND
);
288 status
= NSS_STATUS_NOTFOUND
;
292 /* Continue with the next record, this one is ill-formed. */
298 __libc_lock_unlock (lock
);