1 /* Common code for DB-based databases in nss_db module.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000 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>
26 /* These symbols are defined by the including source file:
28 ENTNAME -- database name of the structure and functions (hostent, pwent).
29 STRUCTURE -- struct name, define only if not ENTNAME (passwd, group).
30 DATABASE -- database file name, ("hosts", "passwd")
32 NEED_H_ERRNO - defined iff an arg `int *herrnop' is used.
35 #define ENTNAME_r CONCAT(ENTNAME,_r)
38 #define DBFILE _PATH_VARDB DATABASE ".db"
41 #define H_ERRNO_PROTO , int *herrnop
42 #define H_ERRNO_ARG , herrnop
43 #define H_ERRNO_SET(val) (*herrnop = (val))
47 #define H_ERRNO_SET(val) ((void) 0)
50 /* Locks the static variables in this file. */
51 __libc_lock_define_initialized (static, lock
)
53 /* Maintenance of the shared handle open on the database. */
60 /* Open the database. */
62 CONCAT(_nss_db_set
,ENTNAME
) (int stayopen
)
64 enum nss_status status
;
66 __libc_lock_lock (lock
);
68 status
= internal_setent (DBFILE
, &db
);
70 /* Remember STAYOPEN flag. */
73 /* Reset the sequential index. */
76 __libc_lock_unlock (lock
);
84 CONCAT(_nss_db_end
,ENTNAME
) (void)
86 __libc_lock_lock (lock
);
88 internal_endent (&db
);
90 /* Reset STAYOPEN flag. */
93 __libc_lock_unlock (lock
);
95 return NSS_STATUS_SUCCESS
;
98 /* Do a database lookup for KEY. */
99 static enum nss_status
100 lookup (DBT
*key
, struct STRUCTURE
*result
,
101 void *buffer
, size_t buflen
, int *errnop H_ERRNO_PROTO EXTRA_ARGS_DECL
)
104 enum nss_status status
;
108 /* Open the database. */
111 status
= internal_setent (DBFILE
, &db
);
112 if (status
!= NSS_STATUS_SUCCESS
)
115 H_ERRNO_SET (NETDB_INTERNAL
);
120 /* Succeed iff it matches a value that parses correctly. */
122 err
= DL_CALL_FCT (db
->get
, (db
->db
, NULL
, key
, &value
, 0));
125 if (err
== db_notfound
)
127 H_ERRNO_SET (HOST_NOT_FOUND
);
128 status
= NSS_STATUS_NOTFOUND
;
133 H_ERRNO_SET (NETDB_INTERNAL
);
134 status
= NSS_STATUS_UNAVAIL
;
137 else if (buflen
< value
.size
)
139 /* No room to copy the data to. */
141 H_ERRNO_SET (NETDB_INTERNAL
);
142 status
= NSS_STATUS_TRYAGAIN
;
146 /* Copy the result to a safe place. */
147 p
= (char *) memcpy (buffer
, value
.data
, value
.size
);
149 /* Skip leading blanks. */
153 err
= parse_line (p
, result
, buffer
, buflen
, errnop EXTRA_ARGS
);
157 /* If the key begins with '0' we are trying to get the next
158 entry. We want to ignore unparsable lines in this case. */
159 if (((char *) key
->data
)[0] == '0')
161 /* Super magical return value. We need to tell our caller
162 that it should continue looping. This value cannot
163 happen in other cases. */
164 status
= NSS_STATUS_RETURN
;
168 H_ERRNO_SET (HOST_NOT_FOUND
);
169 status
= NSS_STATUS_NOTFOUND
;
174 H_ERRNO_SET (NETDB_INTERNAL
);
175 status
= NSS_STATUS_TRYAGAIN
;
178 status
= NSS_STATUS_SUCCESS
;
182 internal_endent (&db
);
188 /* Macro for defining lookup functions for this DB-based database.
190 NAME is the name of the lookup; e.g. `pwnam'.
192 KEYPATTERN gives `printf' args to construct a key string;
193 e.g. `(".%s", name)'.
195 KEYSIZE gives the allocation size of a buffer to construct it in;
196 e.g. `1 + strlen (name)'.
198 PROTO describes the arguments for the lookup key;
199 e.g. `const char *name'.
201 BREAK_IF_MATCH is ignored, but used by ../nss_files/files-XXX.c. */
203 #define DB_LOOKUP(name, keysize, keypattern, break_if_match, proto...) \
205 _nss_db_get##name##_r (proto, \
206 struct STRUCTURE *result, \
207 char *buffer, size_t buflen, int *errnop H_ERRNO_PROTO)\
210 enum nss_status status; \
211 const size_t size = (keysize) + 1; \
212 key.data = __alloca (size); \
213 key.size = KEYPRINTF keypattern; \
215 __libc_lock_lock (lock); \
216 status = lookup (&key, result, buffer, buflen, errnop H_ERRNO_ARG \
218 __libc_lock_unlock (lock); \
222 #define KEYPRINTF(pattern, args...) snprintf (key.data, size, pattern ,##args)
227 /* Return the next entry from the database file, doing locking. */
229 CONCAT(_nss_db_get
,ENTNAME_r
) (struct STRUCTURE
*result
, char *buffer
,
230 size_t buflen
, int *errnop H_ERRNO_PROTO
)
232 /* Return next entry in host file. */
233 enum nss_status status
;
237 __libc_lock_lock (lock
);
239 /* Loop until we find a valid entry or hit EOF. See above for the
240 special meaning of the status value. */
243 key
.size
= snprintf (key
.data
= buf
, sizeof buf
, "0%u", entidx
++);
245 status
= lookup (&key
, result
, buffer
, buflen
, errnop H_ERRNO_ARG
247 if (status
== NSS_STATUS_TRYAGAIN
249 && *herrnop
== NETDB_INTERNAL
251 && *errnop
== ERANGE
)
252 /* Give the user a chance to get the same entry with a larger
256 while (status
== NSS_STATUS_RETURN
);
258 __libc_lock_unlock (lock
);