1 /* Common code for DB-based databases in nss_db module.
2 Copyright (C) 1996, 1997 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;
66 db = __dbopen (DBFILE, O_RDONLY, 0, DB_BTREE, NULL);
69 status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
72 /* We have to make sure the file is `closed on exec'. */
75 result = flags = fcntl ((*db->fd) (db), F_GETFD, 0);
79 result = fcntl ((*db->fd) (db), F_SETFD, flags);
83 /* Something went wrong. Close the stream and return a
87 status = NSS_STATUS_UNAVAIL;
92 /* Remember STAYOPEN flag. */
100 /* Thread-safe, exported version of that. */
102 CONCAT(_nss_db_set,ENTNAME) (int stayopen)
104 enum nss_status status;
106 __libc_lock_lock (lock);
108 status = internal_setent (stayopen);
110 /* Reset the sequential index. */
113 __libc_lock_unlock (lock);
119 /* Close the database file. */
121 internal_endent (void)
131 /* Thread-safe, exported version of that. */
133 CONCAT(_nss_db_end,ENTNAME) (void)
135 __libc_lock_lock (lock);
139 /* Reset STAYOPEN flag. */
142 __libc_lock_unlock (lock);
144 return NSS_STATUS_SUCCESS;
147 /* Do a database lookup for KEY. */
148 static enum nss_status
149 lookup (const DBT *key, struct STRUCTURE *result,
150 void *buffer, int buflen H_ERRNO_PROTO)
152 enum nss_status status;
155 /* Open the database. */
156 status = internal_setent (keep_db);
157 if (status != NSS_STATUS_SUCCESS)
160 /* Succeed iff it matches a value that parses correctly. */
161 status = (((*db->get) (db, key, &value, 0) == 0 &&
162 parse_line (value.data, result, buffer, buflen))
163 ? NSS_STATUS_SUCCESS : NSS_STATUS_NOTFOUND);
172 /* Macro for defining lookup functions for this DB-based database.
174 NAME is the name of the lookup; e.g. `pwnam'.
176 KEYPATTERN gives `printf' args to construct a key string;
177 e.g. `(".%s", name)'.
179 KEYSIZE gives the allocation size of a buffer to construct it in;
180 e.g. `1 + strlen (name)'.
182 PROTO describes the arguments for the lookup key;
183 e.g. `const char *name'.
185 BREAK_IF_MATCH is ignored, but used by ../nss_files/files-XXX.c. */
187 #define DB_LOOKUP(name, keysize, keypattern, break_if_match, proto...) \
189 _nss_db_get##name##_r (proto, \
190 struct STRUCTURE *result, \
191 char *buffer, size_t buflen H_ERRNO_PROTO) \
194 enum nss_status status; \
195 const size_t size = (keysize); \
196 key.data = __alloca (size); \
197 key.size = KEYPRINTF keypattern; \
198 __libc_lock_lock (lock); \
199 status = lookup (&key, result, buffer, buflen H_ERRNO_ARG); \
200 __libc_lock_unlock (lock); \
204 #define KEYPRINTF(pattern, args...) snprintf (key.data, size, pattern ,##args)
209 /* Return the next entry from the database file, doing locking. */
211 CONCAT(_nss_db_get,ENTNAME_r) (struct STRUCTURE *result,
212 char *buffer, size_t buflen H_ERRNO_PROTO)
214 /* Return next entry in host file. */
215 enum nss_status status;
219 __libc_lock_lock (lock);
220 key.size = 1 + snprintf (key.data = buf, sizeof buf, "0%u", entidx++);
221 status = lookup (&key, result, buffer, buflen H_ERRNO_ARG);
222 __libc_lock_unlock (lock);