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. */
25 #include <bits/libc-lock.h>
28 /* These symbols are defined by the including source file:
30 ENTNAME -- database name of the structure and functions (hostent, pwent).
31 STRUCTURE -- struct name, define only if not ENTNAME (passwd, group).
32 DATABASE -- string of the database file's name ("hosts", "passwd").
34 NEED_H_ERRNO - defined iff an arg `int *herrnop' is used.
36 Also see files-parse.c.
39 #define ENTNAME_r CONCAT(ENTNAME,_r)
41 #define DATAFILE "/etc/" DATABASE
45 # define H_ERRNO_PROTO , int *herrnop
46 # define H_ERRNO_ARG , herrnop
47 # define H_ERRNO_SET(val) (*herrnop = (val))
49 # define H_ERRNO_PROTO
51 # define H_ERRNO_SET(val) ((void) 0)
54 /* Locks the static variables in this file. */
55 __libc_lock_define_initialized (static, lock)
57 /* Maintenance of the shared stream open on the database file. */
60 static fpos_t position;
61 static enum { none, getent, getby } last_use;
62 static int keep_stream;
64 /* Open database file if not already opened. */
65 static enum nss_status
66 internal_setent (int stayopen)
68 enum nss_status status = NSS_STATUS_SUCCESS;
72 stream = fopen (DATAFILE, "r");
75 status = NSS_STATUS_UNAVAIL;
78 /* We have to make sure the file is `closed on exec'. */
81 result = flags = fcntl (fileno (stream), F_GETFD, 0);
85 result = fcntl (fileno (stream), F_SETFD, flags);
89 /* Something went wrong. Close the stream and return a
93 status = NSS_STATUS_UNAVAIL;
100 /* Remember STAYOPEN flag. */
102 keep_stream |= stayopen;
108 /* Thread-safe, exported version of that. */
110 CONCAT(_nss_files_set,ENTNAME) (int stayopen)
112 enum nss_status status;
114 __libc_lock_lock (lock);
116 status = internal_setent (stayopen);
118 if (status == NSS_STATUS_SUCCESS && fgetpos (stream, &position) < 0)
122 status = NSS_STATUS_UNAVAIL;
127 __libc_lock_unlock (lock);
133 /* Close the database file. */
135 internal_endent (void)
145 /* Thread-safe, exported version of that. */
147 CONCAT(_nss_files_end,ENTNAME) (void)
149 __libc_lock_lock (lock);
153 /* Reset STAYOPEN flag. */
156 __libc_lock_unlock (lock);
158 return NSS_STATUS_SUCCESS;
161 /* Parsing the database file into `struct STRUCTURE' data structures. */
163 static enum nss_status
164 internal_getent (struct STRUCTURE *result,
165 char *buffer, int buflen H_ERRNO_PROTO)
168 struct parser_data *data = (void *) buffer;
169 int linebuflen = buffer + buflen - data->linebuffer;
172 if (buflen < (int) sizeof *data + 1)
174 __set_errno (ERANGE);
175 H_ERRNO_SET (NETDB_INTERNAL);
176 return NSS_STATUS_TRYAGAIN;
181 /* Terminate the line so that we can test for overflow. */
182 data->linebuffer[linebuflen - 1] = '\0';
184 p = fgets (data->linebuffer, linebuflen, stream);
187 /* End of file or read error. */
188 H_ERRNO_SET (HOST_NOT_FOUND);
189 return NSS_STATUS_NOTFOUND;
191 else if (data->linebuffer[linebuflen - 1] != '\0')
193 /* The line is too long. Give the user the opportunity to
194 enlarge the buffer. */
195 __set_errno (ERANGE);
196 H_ERRNO_SET (NETDB_INTERNAL);
197 return NSS_STATUS_TRYAGAIN;
200 /* Skip leading blanks. */
204 while (*p == '\0' || *p == '#' /* Ignore empty and comment lines. */
205 /* Parse the line. If it is invalid, loop to get the next
206 line of the file to parse. */
207 || ! (parse_result = parse_line (p, result, data, buflen)));
209 /* Filled in RESULT with the next entry from the database file. */
210 return parse_result == -1 ? NSS_STATUS_TRYAGAIN : NSS_STATUS_SUCCESS;
214 /* Return the next entry from the database file, doing locking. */
216 CONCAT(_nss_files_get,ENTNAME_r) (struct STRUCTURE *result,
217 char *buffer, size_t buflen H_ERRNO_PROTO)
219 /* Return next entry in host file. */
220 enum nss_status status = NSS_STATUS_SUCCESS;
222 __libc_lock_lock (lock);
224 /* Be prepared that the set*ent function was not called before. */
226 status = internal_setent (0);
228 if (status == NSS_STATUS_SUCCESS)
230 /* If the last use was not by the getent function we need the
231 position the stream. */
232 if (last_use != getent)
233 if (fsetpos (stream, &position) < 0)
234 status = NSS_STATUS_UNAVAIL;
238 if (status == NSS_STATUS_SUCCESS)
240 status = internal_getent (result, buffer, buflen H_ERRNO_ARG);
242 /* Remember this position if we were successful. If the
243 operation failed we give the user a chance to repeat the
244 operation (perhaps the buffer was too small). */
245 if (status == NSS_STATUS_SUCCESS)
246 fgetpos (stream, &position);
248 /* We must make sure we reposition the stream the next call. */
253 __libc_lock_unlock (lock);
258 /* Macro for defining lookup functions for this file-based database.
260 NAME is the name of the lookup; e.g. `hostbyname'.
262 KEYSIZE and KEYPATTERN are ignored here but used by ../nss_db/db-XXX.c.
264 PROTO describes the arguments for the lookup key;
265 e.g. `const char *hostname'.
267 BREAK_IF_MATCH is a block of code which compares `struct STRUCTURE *result'
268 to the lookup key arguments and does `break;' if they match. */
270 #define DB_LOOKUP(name, keysize, keypattern, break_if_match, proto...) \
272 _nss_files_get##name##_r (proto, \
273 struct STRUCTURE *result, \
274 char *buffer, size_t buflen H_ERRNO_PROTO) \
276 enum nss_status status; \
278 __libc_lock_lock (lock); \
280 /* Reset file pointer to beginning or open file. */ \
281 status = internal_setent (keep_stream); \
283 if (status == NSS_STATUS_SUCCESS) \
285 /* Tell getent function that we have repositioned the file pointer. */ \
288 while ((status = internal_getent (result, buffer, buflen H_ERRNO_ARG)) \
289 == NSS_STATUS_SUCCESS) \
293 internal_endent (); \
296 __libc_lock_unlock (lock); \