1/* Internal routines for nss_files.
2 Copyright (C) 2020-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#ifndef _NSS_FILES_H
20#define _NSS_FILES_H
21
22#include <nss.h>
23#include <stdio.h>
24#if IS_IN (libc)
25#include <libc-lock.h>
26#endif
27
28/* Open PATH for reading, as a data source for nss_files. */
29FILE *__nss_files_fopen (const char *path);
30libc_hidden_proto (__nss_files_fopen)
31
32/* Read a line from FP, storing it BUF. Strip leading blanks and skip
33 comments. Sets errno and returns error code on failure. Special
34 failure: ERANGE means the buffer is too small. The function writes
35 the original offset to *POFFSET (which can be negative in the case
36 of non-seekable input). */
37int __nss_readline (FILE *fp, char *buf, size_t len, off64_t *poffset);
38libc_hidden_proto (__nss_readline)
39
40/* Seek FP to OFFSET. Sets errno and returns error code on failure.
41 On success, sets errno to ERANGE and returns ERANGE (to indicate
42 re-reading of the same input line to the caller). If OFFSET is
43 negative, fail with ESPIPE without seeking. Intended to be used
44 after parsing data read by __nss_readline failed with ERANGE. */
45int __nss_readline_seek (FILE *fp, off64_t offset) attribute_hidden;
46
47/* Handles the result of a parse_line call (as defined by
48 nss/nss_files/files-parse.c). Adjusts the file offset of FP as
49 necessary. Returns 0 on success, and updates errno on failure (and
50 returns that error code). */
51int __nss_parse_line_result (FILE *fp, off64_t offset, int parse_line_result);
52libc_hidden_proto (__nss_parse_line_result)
53
54/* Per-file data. Used by the *ent functions that need to preserve
55 state across calls. */
56struct nss_files_per_file_data
57{
58 FILE *stream;
59#if IS_IN (libc)
60 /* The size of locks changes between libc and nss_files, so this
61 member must be last and is only available in libc. */
62 __libc_lock_define (, lock);
63#endif
64};
65
66/* File index for __nss_files_data_get. */
67enum nss_files_file
68 {
69 nss_file_aliasent,
70 nss_file_etherent,
71 nss_file_grent,
72 nss_file_hostent,
73 nss_file_netent,
74 nss_file_protoent,
75 nss_file_pwent,
76 nss_file_rpcent,
77 nss_file_servent,
78 nss_file_sgent,
79 nss_file_spent,
80
81 nss_file_count
82 };
83
84/* Obtains a pointer to the per-file data for FILE, which is written
85 to *PDATA, and tries to open the file at PATH for it. On success,
86 returns NSS_STATUS_SUCCESS, and the caller must later call
87 __nss_files_data_put. On failure, NSS_STATUS_TRYAGAIN is returned,
88 and *ERRNOP and *HERRNOP are updated if these pointers are not
89 null. */
90enum nss_status __nss_files_data_open (struct nss_files_per_file_data **pdata,
91 enum nss_files_file file,
92 const char *path,
93 int *errnop, int *herrnop);
94libc_hidden_proto (__nss_files_data_open)
95
96/* Unlock the per-file data, previously obtained by
97 __nss_files_data_open. */
98void __nss_files_data_put (struct nss_files_per_file_data *data);
99libc_hidden_proto (__nss_files_data_put)
100
101/* Performs the set*ent operation for FILE. PATH is the file to
102 open. */
103enum nss_status __nss_files_data_setent (enum nss_files_file file,
104 const char *path);
105libc_hidden_proto (__nss_files_data_setent)
106
107/* Performs the end*ent operation for FILE. */
108enum nss_status __nss_files_data_endent (enum nss_files_file file);
109libc_hidden_proto (__nss_files_data_endent)
110
111struct parser_data;
112
113/* Instances of the parse_line function from
114 nss/nss_files/files-parse.c. */
115typedef int nss_files_parse_line (char *line, void *result,
116 struct parser_data *data,
117 size_t datalen, int *errnop);
118extern nss_files_parse_line _nss_files_parse_etherent;
119extern nss_files_parse_line _nss_files_parse_grent;
120extern nss_files_parse_line _nss_files_parse_netent;
121extern nss_files_parse_line _nss_files_parse_protoent;
122extern nss_files_parse_line _nss_files_parse_pwent;
123extern nss_files_parse_line _nss_files_parse_rpcent;
124extern nss_files_parse_line _nss_files_parse_servent;
125extern nss_files_parse_line _nss_files_parse_sgent;
126extern nss_files_parse_line _nss_files_parse_spent;
127
128libc_hidden_proto (_nss_files_parse_etherent)
129libc_hidden_proto (_nss_files_parse_grent)
130libc_hidden_proto (_nss_files_parse_netent)
131libc_hidden_proto (_nss_files_parse_protoent)
132libc_hidden_proto (_nss_files_parse_pwent)
133libc_hidden_proto (_nss_files_parse_rpcent)
134libc_hidden_proto (_nss_files_parse_servent)
135libc_hidden_proto (_nss_files_parse_sgent)
136libc_hidden_proto (_nss_files_parse_spent)
137
138NSS_DECLARE_MODULE_FUNCTIONS (files)
139#undef DEFINE_NSS_FUNCTION
140#define DEFINE_NSS_FUNCTION(x) libc_hidden_proto (_nss_files_##x)
141#include <nss/function.def>
142#undef DEFINE_NSS_FUNCTION
143
144void _nss_files_init (void (*cb) (size_t, struct traced_file *));
145libc_hidden_proto (_nss_files_init)
146
147/* Generic implementation of fget*ent_r. Reads lines from FP until
148 EOF or a successful parse into *RESULT using PARSER. Returns 0 on
149 success, ENOENT on EOF, ERANGE on too-small buffer. */
150int __nss_fgetent_r (FILE *fp, void *result,
151 char *buffer, size_t buffer_length,
152 nss_files_parse_line parser) attribute_hidden;
153
154#endif /* _NSS_FILES_H */
155

source code of glibc/include/nss_files.h