1 /* Common code for file-based databases in nss_files module.
2 Copyright (C) 1996-1999,2001,2002,2004,2007 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 #include <kernel-features.h>
29 /* These symbols are defined by the including source file:
31 ENTNAME -- database name of the structure and functions (hostent, pwent).
32 STRUCTURE -- struct name, define only if not ENTNAME (passwd, group).
33 DATABASE -- string of the database file's name ("hosts", "passwd").
35 NEED_H_ERRNO - defined iff an arg `int *herrnop' is used.
37 Also see files-parse.c.
40 #define ENTNAME_r CONCAT(ENTNAME,_r)
42 #define DATAFILE "/etc/" DATABASE
46 # define H_ERRNO_PROTO , int *herrnop
47 # define H_ERRNO_ARG , herrnop
48 # define H_ERRNO_SET(val) (*herrnop = (val))
50 # define H_ERRNO_PROTO
52 # define H_ERRNO_SET(val) ((void) 0)
57 # define EXTRA_ARGS_DECL
58 # define EXTRA_ARGS_VALUE
61 /* Locks the static variables in this file. */
62 __libc_lock_define_initialized (static, lock
)
64 /* Maintenance of the shared stream open on the database file. */
67 static fpos_t position
;
68 static enum { nouse
, getent
, getby
} last_use
;
69 static int keep_stream
;
71 /* Open database file if not already opened. */
72 static enum nss_status
73 internal_setent (int stayopen
)
75 enum nss_status status
= NSS_STATUS_SUCCESS
;
79 stream
= fopen (DATAFILE
, "re");
82 status
= errno
== EAGAIN
? NSS_STATUS_TRYAGAIN
: NSS_STATUS_UNAVAIL
;
85 #if !defined O_CLOEXEC || !defined __ASSUME_O_CLOEXEC
87 if (__have_o_cloexec
<= 0)
90 /* We have to make sure the file is `closed on exec'. */
94 result
= flags
= fcntl (fileno (stream
), F_GETFD
, 0);
98 if (__have_o_cloexec
== 0)
99 __have_o_cloexec
= (flags
& FD_CLOEXEC
) == 0 ? -1 : 1;
100 if (__have_o_cloexec
< 0)
104 result
= fcntl (fileno (stream
), F_SETFD
, flags
);
109 /* Something went wrong. Close the stream and return a
113 status
= NSS_STATUS_UNAVAIL
;
122 /* Remember STAYOPEN flag. */
124 keep_stream
|= stayopen
;
130 /* Thread-safe, exported version of that. */
132 CONCAT(_nss_files_set
,ENTNAME
) (int stayopen
)
134 enum nss_status status
;
136 __libc_lock_lock (lock
);
138 status
= internal_setent (stayopen
);
140 if (status
== NSS_STATUS_SUCCESS
&& fgetpos (stream
, &position
) < 0)
144 status
= NSS_STATUS_UNAVAIL
;
149 __libc_lock_unlock (lock
);
155 /* Close the database file. */
157 internal_endent (void)
167 /* Thread-safe, exported version of that. */
169 CONCAT(_nss_files_end
,ENTNAME
) (void)
171 __libc_lock_lock (lock
);
175 /* Reset STAYOPEN flag. */
178 __libc_lock_unlock (lock
);
180 return NSS_STATUS_SUCCESS
;
183 /* Parsing the database file into `struct STRUCTURE' data structures. */
185 static enum nss_status
186 internal_getent (struct STRUCTURE
*result
,
187 char *buffer
, size_t buflen
, int *errnop H_ERRNO_PROTO
191 struct parser_data
*data
= (void *) buffer
;
192 int linebuflen
= buffer
+ buflen
- data
->linebuffer
;
195 if (buflen
< sizeof *data
+ 2)
198 H_ERRNO_SET (NETDB_INTERNAL
);
199 return NSS_STATUS_TRYAGAIN
;
204 /* Terminate the line so that we can test for overflow. */
205 ((unsigned char *) data
->linebuffer
)[linebuflen
- 1] = '\xff';
207 p
= fgets_unlocked (data
->linebuffer
, linebuflen
, stream
);
210 /* End of file or read error. */
211 H_ERRNO_SET (HOST_NOT_FOUND
);
212 return NSS_STATUS_NOTFOUND
;
214 else if (((unsigned char *) data
->linebuffer
)[linebuflen
- 1] != 0xff)
216 /* The line is too long. Give the user the opportunity to
217 enlarge the buffer. */
219 H_ERRNO_SET (NETDB_INTERNAL
);
220 return NSS_STATUS_TRYAGAIN
;
223 /* Skip leading blanks. */
227 while (*p
== '\0' || *p
== '#' /* Ignore empty and comment lines. */
228 /* Parse the line. If it is invalid, loop to get the next
229 line of the file to parse. */
230 || ! (parse_result
= parse_line (p
, result
, data
, buflen
, errnop
233 if (__builtin_expect (parse_result
== -1, 0))
235 H_ERRNO_SET (NETDB_INTERNAL
);
236 return NSS_STATUS_TRYAGAIN
;
239 /* Filled in RESULT with the next entry from the database file. */
240 return NSS_STATUS_SUCCESS
;
244 /* Return the next entry from the database file, doing locking. */
246 CONCAT(_nss_files_get
,ENTNAME_r
) (struct STRUCTURE
*result
, char *buffer
,
247 size_t buflen
, int *errnop H_ERRNO_PROTO
)
249 /* Return next entry in host file. */
250 enum nss_status status
= NSS_STATUS_SUCCESS
;
252 __libc_lock_lock (lock
);
254 /* Be prepared that the set*ent function was not called before. */
257 status
= internal_setent (0);
259 if (status
== NSS_STATUS_SUCCESS
&& fgetpos (stream
, &position
) < 0)
263 status
= NSS_STATUS_UNAVAIL
;
267 if (status
== NSS_STATUS_SUCCESS
)
269 /* If the last use was not by the getent function we need the
270 position the stream. */
271 if (last_use
!= getent
)
273 if (fsetpos (stream
, &position
) < 0)
274 status
= NSS_STATUS_UNAVAIL
;
279 if (status
== NSS_STATUS_SUCCESS
)
281 status
= internal_getent (result
, buffer
, buflen
, errnop
282 H_ERRNO_ARG EXTRA_ARGS_VALUE
);
284 /* Remember this position if we were successful. If the
285 operation failed we give the user a chance to repeat the
286 operation (perhaps the buffer was too small). */
287 if (status
== NSS_STATUS_SUCCESS
)
288 fgetpos (stream
, &position
);
290 /* We must make sure we reposition the stream the next call. */
295 __libc_lock_unlock (lock
);
300 /* Macro for defining lookup functions for this file-based database.
302 NAME is the name of the lookup; e.g. `hostbyname'.
304 KEYSIZE and KEYPATTERN are ignored here but used by ../nss_db/db-XXX.c.
306 PROTO describes the arguments for the lookup key;
307 e.g. `const char *hostname'.
309 BREAK_IF_MATCH is a block of code which compares `struct STRUCTURE *result'
310 to the lookup key arguments and does `break;' if they match. */
312 #define DB_LOOKUP(name, keysize, keypattern, break_if_match, proto...) \
314 _nss_files_get##name##_r (proto, \
315 struct STRUCTURE *result, char *buffer, \
316 size_t buflen, int *errnop H_ERRNO_PROTO) \
318 enum nss_status status; \
320 __libc_lock_lock (lock); \
322 /* Reset file pointer to beginning or open file. */ \
323 status = internal_setent (keep_stream); \
325 if (status == NSS_STATUS_SUCCESS) \
327 /* Tell getent function that we have repositioned the file pointer. */ \
330 while ((status = internal_getent (result, buffer, buflen, errnop \
331 H_ERRNO_ARG EXTRA_ARGS_VALUE)) \
332 == NSS_STATUS_SUCCESS) \
336 internal_endent (); \
339 __libc_lock_unlock (lock); \