1 /* Common code for DB-based databases in nss_db module.
2 Copyright (C) 1996, 1997, 1998 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 not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
22 #include <bits/libc-lock.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)
37 #define DBFILE _PATH_VARDB DATABASE ".db"
40 #define H_ERRNO_PROTO , int *herrnop
41 #define H_ERRNO_ARG , herrnop
42 #define H_ERRNO_SET(val) (*herrnop = (val))
46 #define H_ERRNO_SET(val) ((void) 0)
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. */
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
;
67 err
= __nss_db_open (DBFILE
, DB_BTREE
, DB_RDONLY
, 0, NULL
, NULL
, &db
);
72 status
= err
== EAGAIN
? NSS_STATUS_TRYAGAIN
: NSS_STATUS_UNAVAIL
;
76 /* We have to make sure the file is `closed on exec'. */
80 err
= db
->fd (db
, &fd
);
87 result
= flags
= fcntl (fd
, F_GETFD
, 0);
91 result
= fcntl (fd
, F_SETFD
, flags
);
95 /* Something went wrong. Close the stream and return a
99 status
= NSS_STATUS_UNAVAIL
;
104 /* Remember STAYOPEN flag. */
112 /* Thread-safe, exported version of that. */
114 CONCAT(_nss_db_set
,ENTNAME
) (int stayopen
)
116 enum nss_status status
;
118 __libc_lock_lock (lock
);
120 status
= internal_setent (stayopen
);
122 /* Reset the sequential index. */
125 __libc_lock_unlock (lock
);
131 /* Close the database file. */
133 internal_endent (void)
143 /* Thread-safe, exported version of that. */
145 CONCAT(_nss_db_end
,ENTNAME
) (void)
147 __libc_lock_lock (lock
);
151 /* Reset STAYOPEN flag. */
154 __libc_lock_unlock (lock
);
156 return NSS_STATUS_SUCCESS
;
159 /* Do a database lookup for KEY. */
160 static enum nss_status
161 lookup (DBT
*key
, struct STRUCTURE
*result
,
162 void *buffer
, size_t buflen
, int *errnop H_ERRNO_PROTO
)
165 enum nss_status status
;
169 /* Open the database. */
170 status
= internal_setent (keep_db
);
171 if (status
!= NSS_STATUS_SUCCESS
)
174 H_ERRNO_SET (NETDB_INTERNAL
);
178 /* Succeed iff it matches a value that parses correctly. */
180 err
= db
->get (db
, NULL
, key
, &value
, 0);
183 if (err
== DB_NOTFOUND
)
185 H_ERRNO_SET (HOST_NOT_FOUND
);
186 status
= NSS_STATUS_NOTFOUND
;
191 H_ERRNO_SET (NETDB_INTERNAL
);
192 status
= NSS_STATUS_UNAVAIL
;
195 else if (buflen
< value
.size
)
197 /* No room to copy the data to. */
199 H_ERRNO_SET (NETDB_INTERNAL
);
200 status
= NSS_STATUS_TRYAGAIN
;
204 /* Copy the result to a safe place. */
205 p
= (char *) memcpy (buffer
, value
.data
, value
.size
);
207 /* Skip leading blanks. */
211 err
= parse_line (p
, result
, buffer
, buflen
, errnop
);
215 /* If the key begins with '0' we are trying to get the next
216 entry. We want to ignore unparsable lines in this case. */
217 if (((char *) key
->data
)[0] == '0')
219 /* Super magical return value. We need to tell our caller
220 that it should continue looping. This value cannot
221 happen in other cases. */
222 status
= NSS_STATUS_RETURN
;
226 H_ERRNO_SET (HOST_NOT_FOUND
);
227 status
= NSS_STATUS_NOTFOUND
;
232 H_ERRNO_SET (NETDB_INTERNAL
);
233 status
= NSS_STATUS_TRYAGAIN
;
236 status
= NSS_STATUS_SUCCESS
;
246 /* Macro for defining lookup functions for this DB-based database.
248 NAME is the name of the lookup; e.g. `pwnam'.
250 KEYPATTERN gives `printf' args to construct a key string;
251 e.g. `(".%s", name)'.
253 KEYSIZE gives the allocation size of a buffer to construct it in;
254 e.g. `1 + strlen (name)'.
256 PROTO describes the arguments for the lookup key;
257 e.g. `const char *name'.
259 BREAK_IF_MATCH is ignored, but used by ../nss_files/files-XXX.c. */
261 #define DB_LOOKUP(name, keysize, keypattern, break_if_match, proto...) \
263 _nss_db_get##name##_r (proto, \
264 struct STRUCTURE *result, \
265 char *buffer, size_t buflen, int *errnop H_ERRNO_PROTO)\
268 enum nss_status status; \
269 const size_t size = (keysize) + 1; \
270 key.data = __alloca (size); \
271 key.size = KEYPRINTF keypattern; \
273 __libc_lock_lock (lock); \
274 status = lookup (&key, result, buffer, buflen, errnop H_ERRNO_ARG); \
275 __libc_lock_unlock (lock); \
279 #define KEYPRINTF(pattern, args...) snprintf (key.data, size, pattern ,##args)
284 /* Return the next entry from the database file, doing locking. */
286 CONCAT(_nss_db_get
,ENTNAME_r
) (struct STRUCTURE
*result
, char *buffer
,
287 size_t buflen
, int *errnop H_ERRNO_PROTO
)
289 /* Return next entry in host file. */
290 enum nss_status status
;
294 __libc_lock_lock (lock
);
296 /* Loop until we find a valid entry or hit EOF. See above for the
297 special meaning of the status value. */
300 key
.size
= snprintf (key
.data
= buf
, sizeof buf
, "0%u", entidx
++);
302 status
= lookup (&key
, result
, buffer
, buflen
, errnop H_ERRNO_ARG
);
303 if (status
== NSS_STATUS_TRYAGAIN
305 && *herrnop
== NETDB_INTERNAL
307 && *errnop
== ERANGE
)
308 /* Give the user a chance to get the same entry with a larger
312 while (status
== NSS_STATUS_RETURN
);
314 __libc_lock_unlock (lock
);