1 /* Common code for DB-based databases in nss_db module.
2 Copyright (C) 1996-2013 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/>. */
23 #include <bits/libc-lock.h>
27 /* The hashing function we use. */
28 #include "../intl/hash-string.h"
30 /* These symbols are defined by the including source file:
32 ENTNAME -- database name of the structure and functions (hostent, pwent).
33 STRUCTURE -- struct name, define only if not ENTNAME (passwd, group).
34 DATABASE -- database file name, ("hosts", "passwd")
36 NEED_H_ERRNO - defined iff an arg `int *herrnop' is used.
39 #define ENTNAME_r CONCAT(ENTNAME,_r)
42 #define DBFILE _PATH_VARDB DATABASE ".db"
45 # define H_ERRNO_PROTO , int *herrnop
46 # define H_ERRNO_ARG , herrnop
47 # define H_ERRNO_SET(val) (*herrnop = (val))
49 # define H_ERRNO_PROTO
51 # define H_ERRNO_SET(val) ((void) 0)
54 /* State for this database. */
55 static struct nss_db_map state
;
56 /* Lock to protect the state and global variables. */
57 __libc_lock_define (static , lock
);
59 /* Maintenance of the shared handle open on the database. */
61 static const char *entidx
;
64 /* Open the database. */
66 CONCAT(_nss_db_set
,ENTNAME
) (int stayopen
)
68 enum nss_status status
;
70 __libc_lock_lock (lock
);
72 status
= internal_setent (DBFILE
, &state
);
74 if (status
== NSS_STATUS_SUCCESS
)
76 /* Remember STAYOPEN flag. */
79 /* Reset the sequential index. */
80 entidx
= (const char *) state
.header
+ state
.header
->valstroffset
;
83 __libc_lock_unlock (lock
);
91 CONCAT(_nss_db_end
,ENTNAME
) (void)
93 __libc_lock_lock (lock
);
95 internal_endent (&state
);
97 /* Reset STAYOPEN flag. */
100 __libc_lock_unlock (lock
);
102 return NSS_STATUS_SUCCESS
;
106 /* Macro for defining lookup functions for this DB-based database.
108 NAME is the name of the lookup; e.g. `pwnam'.
110 DB_CHAR is index indicator for the database.
112 KEYPATTERN gives `printf' args to construct a key string;
115 KEYSIZE gives the allocation size of a buffer to construct it in;
116 e.g. `1 + sizeof (id) * 4'.
118 PROTO is the potentially empty list of other parameters.
120 BREAK_IF_MATCH is a block of code which compares `struct STRUCTURE *result'
121 to the lookup key arguments and does `break;' if they match. */
123 #define DB_LOOKUP(name, db_char, keysize, keypattern, break_if_match, proto...)\
125 _nss_db_get##name##_r (proto, struct STRUCTURE *result, \
126 char *buffer, size_t buflen, int *errnop H_ERRNO_PROTO)\
128 struct parser_data *data = (void *) buffer; \
130 if (buflen < sizeof *data) \
133 H_ERRNO_SET (NETDB_INTERNAL); \
134 return NSS_STATUS_TRYAGAIN; \
137 struct nss_db_map state = { NULL, 0 }; \
138 enum nss_status status = internal_setent (DBFILE, &state); \
139 if (status != NSS_STATUS_SUCCESS) \
142 H_ERRNO_SET (NETDB_INTERNAL); \
146 const struct nss_db_header *header = state.header; \
148 for (i = 0; i < header->ndbs; ++i) \
149 if (header->dbs[i].id == db_char) \
151 if (i == header->ndbs) \
153 status = NSS_STATUS_UNAVAIL; \
158 if (db_char == '.') \
159 key = (char *) IGNOREPATTERN keypattern; \
162 const size_t size = (keysize) + 1; \
163 key = alloca (size); \
165 KEYPRINTF keypattern; \
168 const stridx_t *hashtable \
169 = (const stridx_t *) ((const char *) header \
170 + header->dbs[i].hashoffset); \
171 const char *valstrtab = (const char *) header + header->valstroffset; \
172 uint32_t hashval = __hash_string (key); \
173 size_t hidx = hashval % header->dbs[i].hashsize; \
174 size_t hval2 = 1 + hashval % (header->dbs[i].hashsize - 2); \
176 status = NSS_STATUS_NOTFOUND; \
177 while (hashtable[hidx] != ~((stridx_t) 0)) \
179 const char *valstr = valstrtab + hashtable[hidx]; \
180 size_t len = strlen (valstr) + 1; \
183 /* No room to copy the data to. */ \
185 H_ERRNO_SET (NETDB_INTERNAL); \
186 status = NSS_STATUS_TRYAGAIN; \
190 /* Copy the string to a place where it can be modified. */ \
191 char *p = memcpy (buffer, valstr, len); \
193 int err = parse_line (p, result, data, buflen, errnop EXTRA_ARGS); \
196 status = NSS_STATUS_SUCCESS; \
198 status = NSS_STATUS_NOTFOUND; \
200 else if (err == -1) \
202 H_ERRNO_SET (NETDB_INTERNAL); \
203 status = NSS_STATUS_TRYAGAIN; \
207 if ((hidx += hval2) >= header->dbs[i].hashsize) \
208 hidx -= header->dbs[i].hashsize; \
211 if (status == NSS_STATUS_NOTFOUND) \
212 H_ERRNO_SET (HOST_NOT_FOUND); \
215 internal_endent (&state); \
220 #define KEYPRINTF(pattern, args...) snprintf (key, size, pattern ,##args)
221 #define IGNOREPATTERN(pattern, arg1, args...) (char *) (uintptr_t) arg1
226 /* Return the next entry from the database file, doing locking. */
228 CONCAT(_nss_db_get
,ENTNAME_r
) (struct STRUCTURE
*result
, char *buffer
,
229 size_t buflen
, int *errnop H_ERRNO_PROTO
)
231 /* Return next entry in host file. */
232 enum nss_status status
;
233 struct parser_data
*data
= (void *) buffer
;
235 if (buflen
< sizeof *data
)
238 H_ERRNO_SET (NETDB_INTERNAL
);
239 return NSS_STATUS_TRYAGAIN
;
242 __libc_lock_lock (lock
);
244 if (state
.header
== NULL
)
246 status
= internal_setent (DBFILE
, &state
);
247 if (status
!= NSS_STATUS_SUCCESS
)
250 H_ERRNO_SET (NETDB_INTERNAL
);
255 status
= NSS_STATUS_UNAVAIL
;
256 if (state
.header
!= MAP_FAILED
)
258 const char *const end
= ((const char *) state
.header
259 + state
.header
->valstroffset
260 + state
.header
->valstrlen
);
263 const char *next
= rawmemchr (entidx
, '\0') + 1;
264 size_t len
= next
- entidx
;
268 /* No room to copy the data to. */
270 H_ERRNO_SET (NETDB_INTERNAL
);
271 status
= NSS_STATUS_TRYAGAIN
;
275 /* Copy the string to a place where it can be modified. */
276 char *p
= memcpy (buffer
, entidx
, len
);
278 int err
= parse_line (p
, result
, data
, buflen
, errnop EXTRA_ARGS
);
282 status
= NSS_STATUS_SUCCESS
;
288 H_ERRNO_SET (HOST_NOT_FOUND
);
289 status
= NSS_STATUS_NOTFOUND
;
293 /* Continue with the next record, this one is ill-formed. */
299 __libc_lock_unlock (lock
);