Remove *xattr syscalls.
[glibc.git] / nss / nss_files / files-XXX.c
blobda840595a4db0f933e07591974985b94607bb743
1 /* Common code for file-based databases in nss_files module.
2 Copyright (C) 1996, 1997, 1998, 1999, 2001 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
18 02111-1307 USA. */
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 /* 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
42 #ifdef NEED_H_ERRNO
43 # include <netdb.h>
44 # define H_ERRNO_PROTO , int *herrnop
45 # define H_ERRNO_ARG , herrnop
46 # define H_ERRNO_SET(val) (*herrnop = (val))
47 #else
48 # define H_ERRNO_PROTO
49 # define H_ERRNO_ARG
50 # define H_ERRNO_SET(val) ((void) 0)
51 #endif
53 #ifndef EXTRA_ARGS
54 # define EXTRA_ARGS
55 # define EXTRA_ARGS_DECL
56 # define EXTRA_ARGS_VALUE
57 #endif
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. */
64 static FILE *stream;
65 static fpos_t position;
66 static enum { none, 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;
75 if (stream == NULL)
77 stream = fopen (DATAFILE, "r");
79 if (stream == NULL)
80 status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
81 else
83 /* We have to make sure the file is `closed on exec'. */
84 int result, flags;
86 result = flags = fcntl (fileno (stream), F_GETFD, 0);
87 if (result >= 0)
89 flags |= FD_CLOEXEC;
90 result = fcntl (fileno (stream), F_SETFD, flags);
92 if (result < 0)
94 /* Something went wrong. Close the stream and return a
95 failure. */
96 fclose (stream);
97 stream = NULL;
98 status = NSS_STATUS_UNAVAIL;
102 else
103 rewind (stream);
105 /* Remember STAYOPEN flag. */
106 if (stream != NULL)
107 keep_stream |= stayopen;
109 return status;
113 /* Thread-safe, exported version of that. */
114 enum nss_status
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)
125 fclose (stream);
126 stream = NULL;
127 status = NSS_STATUS_UNAVAIL;
130 last_use = getent;
132 __libc_lock_unlock (lock);
134 return status;
138 /* Close the database file. */
139 static void
140 internal_endent (void)
142 if (stream != NULL)
144 fclose (stream);
145 stream = NULL;
150 /* Thread-safe, exported version of that. */
151 enum nss_status
152 CONCAT(_nss_files_end,ENTNAME) (void)
154 __libc_lock_lock (lock);
156 internal_endent ();
158 /* Reset STAYOPEN flag. */
159 keep_stream = 0;
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
171 EXTRA_ARGS_DECL)
173 char *p;
174 struct parser_data *data = (void *) buffer;
175 int linebuflen = buffer + buflen - data->linebuffer;
176 int parse_result;
178 if (buflen < sizeof *data + 2)
180 *errnop = ERANGE;
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);
191 if (p == NULL)
193 /* End of file or read error. */
194 *errnop = ENOENT;
195 H_ERRNO_SET (HOST_NOT_FOUND);
196 return NSS_STATUS_NOTFOUND;
198 else if (((unsigned char *) data->linebuffer)[linebuflen - 1] != 0xff)
200 /* The line is too long. Give the user the opportunity to
201 enlarge the buffer. */
202 *errnop = ERANGE;
203 H_ERRNO_SET (NETDB_INTERNAL);
204 return NSS_STATUS_TRYAGAIN;
207 /* Skip leading blanks. */
208 while (isspace (*p))
209 ++p;
211 while (*p == '\0' || *p == '#' /* Ignore empty and comment lines. */
212 /* Parse the line. If it is invalid, loop to get the next
213 line of the file to parse. */
214 || ! (parse_result = parse_line (p, result, data, buflen, errnop
215 EXTRA_ARGS)));
217 /* Filled in RESULT with the next entry from the database file. */
218 return parse_result == -1 ? NSS_STATUS_TRYAGAIN : NSS_STATUS_SUCCESS;
222 /* Return the next entry from the database file, doing locking. */
223 enum nss_status
224 CONCAT(_nss_files_get,ENTNAME_r) (struct STRUCTURE *result, char *buffer,
225 size_t buflen, int *errnop H_ERRNO_PROTO)
227 /* Return next entry in host file. */
228 enum nss_status status = NSS_STATUS_SUCCESS;
230 __libc_lock_lock (lock);
232 /* Be prepared that the set*ent function was not called before. */
233 if (stream == NULL)
235 status = internal_setent (0);
237 if (status == NSS_STATUS_SUCCESS && fgetpos (stream, &position) < 0)
239 fclose (stream);
240 stream = NULL;
241 status = NSS_STATUS_UNAVAIL;
245 if (status == NSS_STATUS_SUCCESS)
247 /* If the last use was not by the getent function we need the
248 position the stream. */
249 if (last_use != getent)
251 if (fsetpos (stream, &position) < 0)
252 status = NSS_STATUS_UNAVAIL;
253 else
254 last_use = getent;
257 if (status == NSS_STATUS_SUCCESS)
259 status = internal_getent (result, buffer, buflen, errnop
260 H_ERRNO_ARG EXTRA_ARGS_VALUE);
262 /* Remember this position if we were successful. If the
263 operation failed we give the user a chance to repeat the
264 operation (perhaps the buffer was too small). */
265 if (status == NSS_STATUS_SUCCESS)
266 fgetpos (stream, &position);
267 else
268 /* We must make sure we reposition the stream the next call. */
269 last_use = none;
273 __libc_lock_unlock (lock);
275 return status;
278 /* Macro for defining lookup functions for this file-based database.
280 NAME is the name of the lookup; e.g. `hostbyname'.
282 KEYSIZE and KEYPATTERN are ignored here but used by ../nss_db/db-XXX.c.
284 PROTO describes the arguments for the lookup key;
285 e.g. `const char *hostname'.
287 BREAK_IF_MATCH is a block of code which compares `struct STRUCTURE *result'
288 to the lookup key arguments and does `break;' if they match. */
290 #define DB_LOOKUP(name, keysize, keypattern, break_if_match, proto...) \
291 enum nss_status \
292 _nss_files_get##name##_r (proto, \
293 struct STRUCTURE *result, char *buffer, \
294 size_t buflen, int *errnop H_ERRNO_PROTO) \
296 enum nss_status status; \
298 __libc_lock_lock (lock); \
300 /* Reset file pointer to beginning or open file. */ \
301 status = internal_setent (keep_stream); \
303 if (status == NSS_STATUS_SUCCESS) \
305 /* Tell getent function that we have repositioned the file pointer. */ \
306 last_use = getby; \
308 while ((status = internal_getent (result, buffer, buflen, errnop \
309 H_ERRNO_ARG EXTRA_ARGS_VALUE)) \
310 == NSS_STATUS_SUCCESS) \
311 { break_if_match } \
313 if (! keep_stream) \
314 internal_endent (); \
317 __libc_lock_unlock (lock); \
319 return status; \