Yet more update tweaks.
[glibc.git] / glibc-compat / nss_db / db-XXX.c
blob8c05829656fc0aa460942c2a92e7b98a6055a3ca
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. */
20 #include <dlfcn.h>
21 #include <fcntl.h>
22 #include <bits/libc-lock.h>
23 #include "nsswitch.h"
24 #include "nss_db.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)
37 #include <paths.h>
38 #define DBFILE _PATH_VARDB DATABASE ".db"
40 #ifdef NEED_H_ERRNO
41 #define H_ERRNO_PROTO , int *herrnop
42 #define H_ERRNO_ARG , herrnop
43 #define H_ERRNO_SET(val) (*herrnop = (val))
44 #else
45 #define H_ERRNO_PROTO
46 #define H_ERRNO_ARG
47 #define H_ERRNO_SET(val) ((void) 0)
48 #endif
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. */
55 static NSS_DB *db;
56 static int keep_db;
57 static int entidx;
60 /* Open the database. */
61 enum nss_status
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. */
71 if (db != NULL)
72 keep_db |= stayopen;
73 /* Reset the sequential index. */
74 entidx = 0;
76 __libc_lock_unlock (lock);
78 return status;
82 /* Close it again. */
83 enum nss_status
84 CONCAT(_nss_db_end,ENTNAME) (void)
86 __libc_lock_lock (lock);
88 internal_endent (&db);
90 /* Reset STAYOPEN flag. */
91 keep_db = 0;
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)
103 char *p;
104 enum nss_status status;
105 int err;
106 DBT value;
108 /* Open the database. */
109 if (db == NULL)
111 status = internal_setent (DBFILE, &db);
112 if (status != NSS_STATUS_SUCCESS)
114 H_ERRNO_SET (NETDB_INTERNAL);
115 return status;
119 /* Succeed iff it matches a value that parses correctly. */
120 value.flags = 0;
121 err = DL_CALL_FCT (db->get, (db->db, NULL, key, &value, 0));
122 if (err != 0)
124 if (err == db_notfound)
126 H_ERRNO_SET (HOST_NOT_FOUND);
127 status = NSS_STATUS_NOTFOUND;
129 else
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;
142 else
144 /* Copy the result to a safe place. */
145 p = (char *) memcpy (buffer, value.data, value.size);
147 /* Skip leading blanks. */
148 while (isspace (*p))
149 ++p;
151 err = parse_line (p, result, buffer, buflen);
153 if (err == 0)
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;
164 else
166 H_ERRNO_SET (HOST_NOT_FOUND);
167 status = NSS_STATUS_NOTFOUND;
170 else if (err < 0)
172 H_ERRNO_SET (NETDB_INTERNAL);
173 status = NSS_STATUS_TRYAGAIN;
175 else
176 status = NSS_STATUS_SUCCESS;
179 if (! keep_db)
180 internal_endent (&db);
182 return status;
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...) \
202 enum nss_status \
203 _nss_db_get##name##_r (proto, \
204 struct STRUCTURE *result, \
205 char *buffer, size_t buflen H_ERRNO_PROTO)\
207 DBT key; \
208 enum nss_status status; \
209 const size_t size = (keysize) + 1; \
210 key.data = __alloca (size); \
211 key.size = KEYPRINTF keypattern; \
212 key.flags = 0; \
213 __libc_lock_lock (lock); \
214 status = lookup (&key, result, buffer, buflen H_ERRNO_ARG); \
215 __libc_lock_unlock (lock); \
216 return status; \
219 #define KEYPRINTF(pattern, args...) snprintf (key.data, size, pattern ,##args)
224 /* Return the next entry from the database file, doing locking. */
225 enum nss_status
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;
231 char buf[20];
232 DBT key;
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++);
241 key.flags = 0;
242 status = lookup (&key, result, buffer, buflen H_ERRNO_ARG);
243 if (status == NSS_STATUS_TRYAGAIN
244 #ifdef NEED_H_ERRNO
245 && *herrnop == NETDB_INTERNAL
246 #endif
247 && errno == ERANGE)
248 /* Give the user a chance to get the same entry with a larger
249 buffer. */
250 --entidx;
252 while (status == NSS_STATUS_RETURN);
254 __libc_lock_unlock (lock);
256 return status;