1 /* Common code for file-based databases in nss_files module.
2 Copyright (C) 1996-1999,2001,2002,2004 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
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)
55 # define EXTRA_ARGS_DECL
56 # define EXTRA_ARGS_VALUE
59 /* Locks the static variables in this file. */
60 __libc_lock_define_initialized (static, lock
)
62 /* Maintenance of the shared stream open on the database file. */
65 static fpos_t position
;
66 static enum { nouse
, getent
, getby
} last_use
;
67 static int keep_stream
;
69 /* Open database file if not already opened. */
70 static enum nss_status
71 internal_setent (int stayopen
)
73 enum nss_status status
= NSS_STATUS_SUCCESS
;
77 stream
= fopen (DATAFILE
, "r");
80 status
= errno
== EAGAIN
? NSS_STATUS_TRYAGAIN
: NSS_STATUS_UNAVAIL
;
83 /* We have to make sure the file is `closed on exec'. */
86 result
= flags
= fcntl (fileno (stream
), F_GETFD
, 0);
90 result
= fcntl (fileno (stream
), F_SETFD
, flags
);
94 /* Something went wrong. Close the stream and return a
98 status
= NSS_STATUS_UNAVAIL
;
105 /* Remember STAYOPEN flag. */
107 keep_stream
|= stayopen
;
113 /* Thread-safe, exported version of that. */
115 CONCAT(_nss_files_set
,ENTNAME
) (int stayopen
)
117 enum nss_status status
;
119 __libc_lock_lock (lock
);
121 status
= internal_setent (stayopen
);
123 if (status
== NSS_STATUS_SUCCESS
&& fgetpos (stream
, &position
) < 0)
127 status
= NSS_STATUS_UNAVAIL
;
132 __libc_lock_unlock (lock
);
138 /* Close the database file. */
140 internal_endent (void)
150 /* Thread-safe, exported version of that. */
152 CONCAT(_nss_files_end
,ENTNAME
) (void)
154 __libc_lock_lock (lock
);
158 /* Reset STAYOPEN flag. */
161 __libc_lock_unlock (lock
);
163 return NSS_STATUS_SUCCESS
;
166 /* Parsing the database file into `struct STRUCTURE' data structures. */
168 static enum nss_status
169 internal_getent (struct STRUCTURE
*result
,
170 char *buffer
, size_t buflen
, int *errnop H_ERRNO_PROTO
174 struct parser_data
*data
= (void *) buffer
;
175 int linebuflen
= buffer
+ buflen
- data
->linebuffer
;
178 if (buflen
< sizeof *data
+ 2)
181 H_ERRNO_SET (NETDB_INTERNAL
);
182 return NSS_STATUS_TRYAGAIN
;
187 /* Terminate the line so that we can test for overflow. */
188 ((unsigned char *) data
->linebuffer
)[linebuflen
- 1] = '\xff';
190 p
= fgets_unlocked (data
->linebuffer
, linebuflen
, stream
);
193 /* End of file or read error. */
194 H_ERRNO_SET (HOST_NOT_FOUND
);
195 return NSS_STATUS_NOTFOUND
;
197 else if (((unsigned char *) data
->linebuffer
)[linebuflen
- 1] != 0xff)
199 /* The line is too long. Give the user the opportunity to
200 enlarge the buffer. */
202 H_ERRNO_SET (NETDB_INTERNAL
);
203 return NSS_STATUS_TRYAGAIN
;
206 /* Skip leading blanks. */
210 while (*p
== '\0' || *p
== '#' /* Ignore empty and comment lines. */
211 /* Parse the line. If it is invalid, loop to get the next
212 line of the file to parse. */
213 || ! (parse_result
= parse_line (p
, result
, data
, buflen
, errnop
216 if (__builtin_expect (parse_result
== -1, 0))
218 H_ERRNO_SET (NETDB_INTERNAL
);
219 return NSS_STATUS_TRYAGAIN
;
222 /* Filled in RESULT with the next entry from the database file. */
223 return NSS_STATUS_SUCCESS
;
227 /* Return the next entry from the database file, doing locking. */
229 CONCAT(_nss_files_get
,ENTNAME_r
) (struct STRUCTURE
*result
, char *buffer
,
230 size_t buflen
, int *errnop H_ERRNO_PROTO
)
232 /* Return next entry in host file. */
233 enum nss_status status
= NSS_STATUS_SUCCESS
;
235 __libc_lock_lock (lock
);
237 /* Be prepared that the set*ent function was not called before. */
240 status
= internal_setent (0);
242 if (status
== NSS_STATUS_SUCCESS
&& fgetpos (stream
, &position
) < 0)
246 status
= NSS_STATUS_UNAVAIL
;
250 if (status
== NSS_STATUS_SUCCESS
)
252 /* If the last use was not by the getent function we need the
253 position the stream. */
254 if (last_use
!= getent
)
256 if (fsetpos (stream
, &position
) < 0)
257 status
= NSS_STATUS_UNAVAIL
;
262 if (status
== NSS_STATUS_SUCCESS
)
264 status
= internal_getent (result
, buffer
, buflen
, errnop
265 H_ERRNO_ARG EXTRA_ARGS_VALUE
);
267 /* Remember this position if we were successful. If the
268 operation failed we give the user a chance to repeat the
269 operation (perhaps the buffer was too small). */
270 if (status
== NSS_STATUS_SUCCESS
)
271 fgetpos (stream
, &position
);
273 /* We must make sure we reposition the stream the next call. */
278 __libc_lock_unlock (lock
);
283 /* Macro for defining lookup functions for this file-based database.
285 NAME is the name of the lookup; e.g. `hostbyname'.
287 KEYSIZE and KEYPATTERN are ignored here but used by ../nss_db/db-XXX.c.
289 PROTO describes the arguments for the lookup key;
290 e.g. `const char *hostname'.
292 BREAK_IF_MATCH is a block of code which compares `struct STRUCTURE *result'
293 to the lookup key arguments and does `break;' if they match. */
295 #define DB_LOOKUP(name, keysize, keypattern, break_if_match, proto...) \
297 _nss_files_get##name##_r (proto, \
298 struct STRUCTURE *result, char *buffer, \
299 size_t buflen, int *errnop H_ERRNO_PROTO) \
301 enum nss_status status; \
303 __libc_lock_lock (lock); \
305 /* Reset file pointer to beginning or open file. */ \
306 status = internal_setent (keep_stream); \
308 if (status == NSS_STATUS_SUCCESS) \
310 /* Tell getent function that we have repositioned the file pointer. */ \
313 while ((status = internal_getent (result, buffer, buflen, errnop \
314 H_ERRNO_ARG EXTRA_ARGS_VALUE)) \
315 == NSS_STATUS_SUCCESS) \
319 internal_endent (); \
322 __libc_lock_unlock (lock); \