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
, int buflen H_ERRNO_PROTO
)
104 enum nss_status status
;
108 /* Open the database. */
111 status
= internal_setent (DBFILE
, &db
);
112 if (status
!= NSS_STATUS_SUCCESS
)
114 H_ERRNO_SET (NETDB_INTERNAL
);
119 /* Succeed iff it matches a value that parses correctly. */
121 err
= DL_CALL_FCT (db
->get
, (db
->db
, NULL
, key
, &value
, 0));
124 if (err
== db_notfound
)
126 H_ERRNO_SET (HOST_NOT_FOUND
);
127 status
= NSS_STATUS_NOTFOUND
;
131 H_ERRNO_SET (NETDB_INTERNAL
);
132 status
= NSS_STATUS_UNAVAIL
;
135 else if (buflen
< value
.size
)
137 /* No room to copy the data to. */
138 __set_errno (ERANGE
);
139 H_ERRNO_SET (NETDB_INTERNAL
);
140 status
= NSS_STATUS_TRYAGAIN
;
144 /* Copy the result to a safe place. */
145 p
= (char *) memcpy (buffer
, value
.data
, value
.size
);
147 /* Skip leading blanks. */
151 err
= parse_line (p
, result
, buffer
, buflen
);
155 /* If the key begins with '0' we are trying to get the next
156 entry. We want to ignore unparsable lines in this case. */
157 if (((char *) key
->data
)[0] == '0')
159 /* Super magical return value. We need to tell our caller
160 that it should continue looping. This value cannot
161 happen in other cases. */
162 status
= NSS_STATUS_RETURN
;
166 H_ERRNO_SET (HOST_NOT_FOUND
);
167 status
= NSS_STATUS_NOTFOUND
;
172 H_ERRNO_SET (NETDB_INTERNAL
);
173 status
= NSS_STATUS_TRYAGAIN
;
176 status
= NSS_STATUS_SUCCESS
;
180 internal_endent (&db
);
186 /* Macro for defining lookup functions for this DB-based database.
188 NAME is the name of the lookup; e.g. `pwnam'.
190 KEYPATTERN gives `printf' args to construct a key string;
191 e.g. `(".%s", name)'.
193 KEYSIZE gives the allocation size of a buffer to construct it in;
194 e.g. `1 + strlen (name)'.
196 PROTO describes the arguments for the lookup key;
197 e.g. `const char *name'.
199 BREAK_IF_MATCH is ignored, but used by ../nss_files/files-XXX.c. */
201 #define DB_LOOKUP(name, keysize, keypattern, break_if_match, proto...) \
203 _nss_db_get##name##_r (proto, \
204 struct STRUCTURE *result, \
205 char *buffer, size_t buflen H_ERRNO_PROTO)\
208 enum nss_status status; \
209 const size_t size = (keysize) + 1; \
210 key.data = __alloca (size); \
211 key.size = KEYPRINTF keypattern; \
213 __libc_lock_lock (lock); \
214 status = lookup (&key, result, buffer, buflen H_ERRNO_ARG); \
215 __libc_lock_unlock (lock); \
219 #define KEYPRINTF(pattern, args...) snprintf (key.data, size, pattern ,##args)
224 /* Return the next entry from the database file, doing locking. */
226 CONCAT(_nss_db_get
,ENTNAME_r
) (struct STRUCTURE
*result
, char *buffer
,
227 size_t buflen H_ERRNO_PROTO
)
229 /* Return next entry in host file. */
230 enum nss_status status
;
234 __libc_lock_lock (lock
);
236 /* Loop until we find a valid entry or hit EOF. See above for the
237 special meaning of the status value. */
240 key
.size
= snprintf (key
.data
= buf
, sizeof buf
, "0%u", entidx
++);
242 status
= lookup (&key
, result
, buffer
, buflen H_ERRNO_ARG
);
243 if (status
== NSS_STATUS_TRYAGAIN
245 && *herrnop
== NETDB_INTERNAL
248 /* Give the user a chance to get the same entry with a larger
252 while (status
== NSS_STATUS_RETURN
);
254 __libc_lock_unlock (lock
);