Check floating-point ABI in ARM VALID_ELF_HEADER.
[glibc.git] / nss / nss_files / files-XXX.c
bloba6df3487271c76386a4ca33f08432b70088aa25f
1 /* Common code for file-based databases in nss_files module.
2 Copyright (C) 1996-1999,2001,2002,2004,2007,2008,2011
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #include <stdio.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <bits/libc-lock.h>
25 #include "nsswitch.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
44 #ifdef NEED_H_ERRNO
45 # include <netdb.h>
46 # define H_ERRNO_PROTO , int *herrnop
47 # define H_ERRNO_ARG , herrnop
48 # define H_ERRNO_SET(val) (*herrnop = (val))
49 #else
50 # define H_ERRNO_PROTO
51 # define H_ERRNO_ARG
52 # define H_ERRNO_SET(val) ((void) 0)
53 #endif
55 #ifndef EXTRA_ARGS
56 # define EXTRA_ARGS
57 # define EXTRA_ARGS_DECL
58 # define EXTRA_ARGS_VALUE
59 #endif
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. */
66 static FILE *stream;
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;
77 if (stream == NULL)
79 stream = fopen (DATAFILE, "rce");
81 if (stream == NULL)
82 status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
83 else
85 #if !defined O_CLOEXEC || !defined __ASSUME_O_CLOEXEC
86 # ifdef O_CLOEXEC
87 if (__have_o_cloexec <= 0)
88 # endif
90 /* We have to make sure the file is `closed on exec'. */
91 int result;
92 int flags;
94 result = flags = fcntl (fileno (stream), F_GETFD, 0);
95 if (result >= 0)
97 # ifdef O_CLOEXEC
98 if (__have_o_cloexec == 0)
99 __have_o_cloexec = (flags & FD_CLOEXEC) == 0 ? -1 : 1;
100 if (__have_o_cloexec < 0)
101 # endif
103 flags |= FD_CLOEXEC;
104 result = fcntl (fileno (stream), F_SETFD, flags);
107 if (result < 0)
109 /* Something went wrong. Close the stream and return a
110 failure. */
111 fclose (stream);
112 stream = NULL;
113 status = NSS_STATUS_UNAVAIL;
116 #endif
119 else
120 rewind (stream);
122 /* Remember STAYOPEN flag. */
123 if (stream != NULL)
124 keep_stream |= stayopen;
126 return status;
130 /* Thread-safe, exported version of that. */
131 enum nss_status
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)
142 fclose (stream);
143 stream = NULL;
144 status = NSS_STATUS_UNAVAIL;
147 last_use = getent;
149 __libc_lock_unlock (lock);
151 return status;
155 /* Close the database file. */
156 static void
157 internal_endent (void)
159 if (stream != NULL)
161 fclose (stream);
162 stream = NULL;
167 /* Thread-safe, exported version of that. */
168 enum nss_status
169 CONCAT(_nss_files_end,ENTNAME) (void)
171 __libc_lock_lock (lock);
173 internal_endent ();
175 /* Reset STAYOPEN flag. */
176 keep_stream = 0;
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
188 EXTRA_ARGS_DECL)
190 char *p;
191 struct parser_data *data = (void *) buffer;
192 int linebuflen = buffer + buflen - data->linebuffer;
193 int parse_result;
195 if (buflen < sizeof *data + 2)
197 *errnop = ERANGE;
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);
208 if (p == NULL)
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. */
218 *errnop = ERANGE;
219 H_ERRNO_SET (NETDB_INTERNAL);
220 return NSS_STATUS_TRYAGAIN;
223 /* Skip leading blanks. */
224 while (isspace (*p))
225 ++p;
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
231 EXTRA_ARGS)));
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. */
245 enum nss_status
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. */
255 if (stream == NULL)
257 int save_errno = errno;
259 status = internal_setent (0);
261 __set_errno (save_errno);
263 if (status == NSS_STATUS_SUCCESS && fgetpos (stream, &position) < 0)
265 fclose (stream);
266 stream = NULL;
267 status = NSS_STATUS_UNAVAIL;
271 if (status == NSS_STATUS_SUCCESS)
273 /* If the last use was not by the getent function we need the
274 position the stream. */
275 if (last_use != getent)
277 if (fsetpos (stream, &position) < 0)
278 status = NSS_STATUS_UNAVAIL;
279 else
280 last_use = getent;
283 if (status == NSS_STATUS_SUCCESS)
285 status = internal_getent (result, buffer, buflen, errnop
286 H_ERRNO_ARG EXTRA_ARGS_VALUE);
288 /* Remember this position if we were successful. If the
289 operation failed we give the user a chance to repeat the
290 operation (perhaps the buffer was too small). */
291 if (status == NSS_STATUS_SUCCESS)
292 fgetpos (stream, &position);
293 else
294 /* We must make sure we reposition the stream the next call. */
295 last_use = nouse;
299 __libc_lock_unlock (lock);
301 return status;
304 /* Macro for defining lookup functions for this file-based database.
306 NAME is the name of the lookup; e.g. `hostbyname'.
308 DB_CHAR, KEYPATTERN, KEYSIZE are ignored here but used by db-XXX.c
309 e.g. `1 + sizeof (id) * 4'.
311 PROTO is the potentially empty list of other parameters.
313 BREAK_IF_MATCH is a block of code which compares `struct STRUCTURE *result'
314 to the lookup key arguments and does `break;' if they match. */
316 #define DB_LOOKUP(name, db_char, keysize, keypattern, break_if_match, proto...)\
317 enum nss_status \
318 _nss_files_get##name##_r (proto, \
319 struct STRUCTURE *result, char *buffer, \
320 size_t buflen, int *errnop H_ERRNO_PROTO) \
322 enum nss_status status; \
324 __libc_lock_lock (lock); \
326 /* Reset file pointer to beginning or open file. */ \
327 status = internal_setent (keep_stream); \
329 if (status == NSS_STATUS_SUCCESS) \
331 /* Tell getent function that we have repositioned the file pointer. */ \
332 last_use = getby; \
334 while ((status = internal_getent (result, buffer, buflen, errnop \
335 H_ERRNO_ARG EXTRA_ARGS_VALUE)) \
336 == NSS_STATUS_SUCCESS) \
337 { break_if_match } \
339 if (! keep_stream) \
340 internal_endent (); \
343 __libc_lock_unlock (lock); \
345 return status; \