1 /* Common code for file-based databases in nss_files 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. */
24 #include <bits/libc-lock.h>
27 /* These symbols are defined by the including source file:
29 ENTNAME -- database name of the structure and functions (hostent, pwent).
30 STRUCTURE -- struct name, define only if not ENTNAME (passwd, group).
31 DATABASE -- string of the database file's name ("hosts", "passwd").
33 NEED_H_ERRNO - defined iff an arg `int *herrnop' is used.
35 Also see files-parse.c.
38 #define ENTNAME_r CONCAT(ENTNAME,_r)
40 #define DATAFILE "/etc/" DATABASE
44 # define H_ERRNO_PROTO , int *herrnop
45 # define H_ERRNO_ARG , herrnop
46 # define H_ERRNO_SET(val) (*herrnop = (val))
48 # define H_ERRNO_PROTO
50 # define H_ERRNO_SET(val) ((void) 0)
53 /* Locks the static variables in this file. */
54 __libc_lock_define_initialized (static, lock)
56 /* Maintenance of the shared stream open on the database file. */
59 static fpos_t position;
60 static enum { none, getent, getby } last_use;
61 static int keep_stream;
63 /* Open database file if not already opened. */
64 static enum nss_status
65 internal_setent (int stayopen)
67 enum nss_status status = NSS_STATUS_SUCCESS;
71 stream = fopen (DATAFILE, "r");
74 status = NSS_STATUS_UNAVAIL;
79 /* Remember STAYOPEN flag. */
81 keep_stream |= stayopen;
87 /* Thread-safe, exported version of that. */
89 CONCAT(_nss_files_set,ENTNAME) (int stayopen)
91 enum nss_status status;
93 __libc_lock_lock (lock);
95 status = internal_setent (stayopen);
97 if (status == NSS_STATUS_SUCCESS && fgetpos (stream, &position) < 0)
101 status = NSS_STATUS_UNAVAIL;
106 __libc_lock_unlock (lock);
112 /* Close the database file. */
114 internal_endent (void)
124 /* Thread-safe, exported version of that. */
126 CONCAT(_nss_files_end,ENTNAME) (void)
128 __libc_lock_lock (lock);
132 /* Reset STAYOPEN flag. */
135 __libc_lock_unlock (lock);
137 return NSS_STATUS_SUCCESS;
140 /* Parsing the database file into `struct STRUCTURE' data structures. */
142 static enum nss_status
143 internal_getent (struct STRUCTURE *result,
144 char *buffer, int buflen H_ERRNO_PROTO)
147 struct parser_data *data = (void *) buffer;
148 int linebuflen = buffer + buflen - data->linebuffer;
151 if (buflen < (int) sizeof *data + 1)
153 __set_errno (ERANGE);
154 H_ERRNO_SET (NETDB_INTERNAL);
155 return NSS_STATUS_TRYAGAIN;
160 /* Terminate the line so that we can test for overflow. */
161 data->linebuffer[linebuflen - 1] = '\0';
163 p = fgets (data->linebuffer, linebuflen, stream);
166 /* End of file or read error. */
167 H_ERRNO_SET (HOST_NOT_FOUND);
168 return NSS_STATUS_NOTFOUND;
170 else if (data->linebuffer[linebuflen - 1] != '\0')
172 /* The line is too long. Give the user the opportunity to
173 enlarge the buffer. */
174 __set_errno (ERANGE);
175 H_ERRNO_SET (NETDB_INTERNAL);
176 return NSS_STATUS_TRYAGAIN;
179 /* Skip leading blanks. */
183 while (*p == '\0' || *p == '#' /* Ignore empty and comment lines. */
184 /* Parse the line. If it is invalid, loop to get the next
185 line of the file to parse. */
186 || ! (parse_result = parse_line (p, result, data, buflen)));
188 /* Filled in RESULT with the next entry from the database file. */
189 return parse_result == -1 ? NSS_STATUS_TRYAGAIN : NSS_STATUS_SUCCESS;
193 /* Return the next entry from the database file, doing locking. */
195 CONCAT(_nss_files_get,ENTNAME_r) (struct STRUCTURE *result,
196 char *buffer, size_t buflen H_ERRNO_PROTO)
198 /* Return next entry in host file. */
199 enum nss_status status = NSS_STATUS_SUCCESS;
201 __libc_lock_lock (lock);
203 /* Be prepared that the set*ent function was not called before. */
205 status = internal_setent (0);
207 if (status == NSS_STATUS_SUCCESS)
209 /* If the last use was not by the getent function we need the
210 position the stream. */
211 if (last_use != getent)
212 if (fsetpos (stream, &position) < 0)
213 status = NSS_STATUS_UNAVAIL;
217 if (status == NSS_STATUS_SUCCESS)
219 status = internal_getent (result, buffer, buflen H_ERRNO_ARG);
221 /* Remember this position if we were successful. If the
222 operation failed we give the user a chance to repeat the
223 operation (perhaps the buffer was too small). */
224 if (status == NSS_STATUS_SUCCESS)
225 fgetpos (stream, &position);
227 /* We must make sure we reposition the stream the next call. */
232 __libc_lock_unlock (lock);
237 /* Macro for defining lookup functions for this file-based database.
239 NAME is the name of the lookup; e.g. `hostbyname'.
241 KEYSIZE and KEYPATTERN are ignored here but used by ../nss_db/db-XXX.c.
243 PROTO describes the arguments for the lookup key;
244 e.g. `const char *hostname'.
246 BREAK_IF_MATCH is a block of code which compares `struct STRUCTURE *result'
247 to the lookup key arguments and does `break;' if they match. */
249 #define DB_LOOKUP(name, keysize, keypattern, break_if_match, proto...) \
251 _nss_files_get##name##_r (proto, \
252 struct STRUCTURE *result, \
253 char *buffer, size_t buflen H_ERRNO_PROTO) \
255 enum nss_status status; \
257 __libc_lock_lock (lock); \
259 /* Reset file pointer to beginning or open file. */ \
260 status = internal_setent (keep_stream); \
262 if (status == NSS_STATUS_SUCCESS) \
264 /* Tell getent function that we have repositioned the file pointer. */ \
267 while ((status = internal_getent (result, buffer, buflen H_ERRNO_ARG)) \
268 == NSS_STATUS_SUCCESS) \
272 internal_endent (); \
275 __libc_lock_unlock (lock); \