glibc/include/nss_files.h

155 lines
5.9 KiB
C
Raw Normal View History

/* Internal routines for nss_files.
Copyright (C) 2020-2024 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#ifndef _NSS_FILES_H
#define _NSS_FILES_H
#include <nss.h>
#include <stdio.h>
nss_files: Add generic code for set*ent, end*ent and file open This reduces RSS usage if nss_files is not actually used, and can be used later to make NSS data thread-specific. It also results in a small code size reduction. Before: text data bss dec hex filename 2288 0 72 2360 938 nss/files-alias.os 1807 0 72 1879 757 nss/files-ethers.os 1371 0 72 1443 5a3 nss/files-grp.os 6246 0 72 6318 18ae nss/files-hosts.os 869 0 0 869 365 nss/files-initgroups.os 666 0 0 666 29a nss/files-init.os 1934 0 0 1934 78e nss/files-netgrp.os 2353 0 72 2425 979 nss/files-network.os 2130 0 72 2202 89a nss/files-proto.os 1372 0 72 1444 5a4 nss/files-pwd.os 2124 0 72 2196 894 nss/files-rpc.os 2265 0 72 2337 921 nss/files-service.os 1125 0 72 1197 4ad nss/files-sgrp.os 1124 0 72 1196 4ac nss/files-spwd.os After: text data bss dec hex filename 2040 0 0 2040 7f8 nss/files-alias.os 1599 0 0 1599 63f nss/files-ethers.os 1155 0 0 1155 483 nss/files-grp.os 6010 0 0 6010 177a nss/files-hosts.os 869 0 0 869 365 nss/files-initgroups.os 666 0 0 666 29a nss/files-init.os 1934 0 0 1934 78e nss/files-netgrp.os 2129 0 0 2129 851 nss/files-network.os 1914 0 0 1914 77a nss/files-proto.os 1156 0 0 1156 484 nss/files-pwd.os 1908 0 0 1908 774 nss/files-rpc.os 2057 0 0 2057 809 nss/files-service.os 909 0 0 909 38d nss/files-sgrp.os 908 0 0 908 38c nss/files-spwd.os 1090 0 8 1098 44a nss/nss_files_data.os 27674 code bytes before, 26344 code bytes after, so it is an overall win despite the extra initialization code. Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
2021-07-07 16:33:52 +00:00
#if IS_IN (libc)
#include <libc-lock.h>
#endif
/* Open PATH for reading, as a data source for nss_files. */
FILE *__nss_files_fopen (const char *path);
libc_hidden_proto (__nss_files_fopen)
/* Read a line from FP, storing it BUF. Strip leading blanks and skip
comments. Sets errno and returns error code on failure. Special
failure: ERANGE means the buffer is too small. The function writes
the original offset to *POFFSET (which can be negative in the case
of non-seekable input). */
int __nss_readline (FILE *fp, char *buf, size_t len, off64_t *poffset);
libc_hidden_proto (__nss_readline)
/* Seek FP to OFFSET. Sets errno and returns error code on failure.
On success, sets errno to ERANGE and returns ERANGE (to indicate
re-reading of the same input line to the caller). If OFFSET is
negative, fail with ESPIPE without seeking. Intended to be used
after parsing data read by __nss_readline failed with ERANGE. */
int __nss_readline_seek (FILE *fp, off64_t offset) attribute_hidden;
/* Handles the result of a parse_line call (as defined by
nss/nss_files/files-parse.c). Adjusts the file offset of FP as
necessary. Returns 0 on success, and updates errno on failure (and
returns that error code). */
int __nss_parse_line_result (FILE *fp, off64_t offset, int parse_line_result);
libc_hidden_proto (__nss_parse_line_result)
nss_files: Add generic code for set*ent, end*ent and file open This reduces RSS usage if nss_files is not actually used, and can be used later to make NSS data thread-specific. It also results in a small code size reduction. Before: text data bss dec hex filename 2288 0 72 2360 938 nss/files-alias.os 1807 0 72 1879 757 nss/files-ethers.os 1371 0 72 1443 5a3 nss/files-grp.os 6246 0 72 6318 18ae nss/files-hosts.os 869 0 0 869 365 nss/files-initgroups.os 666 0 0 666 29a nss/files-init.os 1934 0 0 1934 78e nss/files-netgrp.os 2353 0 72 2425 979 nss/files-network.os 2130 0 72 2202 89a nss/files-proto.os 1372 0 72 1444 5a4 nss/files-pwd.os 2124 0 72 2196 894 nss/files-rpc.os 2265 0 72 2337 921 nss/files-service.os 1125 0 72 1197 4ad nss/files-sgrp.os 1124 0 72 1196 4ac nss/files-spwd.os After: text data bss dec hex filename 2040 0 0 2040 7f8 nss/files-alias.os 1599 0 0 1599 63f nss/files-ethers.os 1155 0 0 1155 483 nss/files-grp.os 6010 0 0 6010 177a nss/files-hosts.os 869 0 0 869 365 nss/files-initgroups.os 666 0 0 666 29a nss/files-init.os 1934 0 0 1934 78e nss/files-netgrp.os 2129 0 0 2129 851 nss/files-network.os 1914 0 0 1914 77a nss/files-proto.os 1156 0 0 1156 484 nss/files-pwd.os 1908 0 0 1908 774 nss/files-rpc.os 2057 0 0 2057 809 nss/files-service.os 909 0 0 909 38d nss/files-sgrp.os 908 0 0 908 38c nss/files-spwd.os 1090 0 8 1098 44a nss/nss_files_data.os 27674 code bytes before, 26344 code bytes after, so it is an overall win despite the extra initialization code. Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
2021-07-07 16:33:52 +00:00
/* Per-file data. Used by the *ent functions that need to preserve
state across calls. */
struct nss_files_per_file_data
{
FILE *stream;
#if IS_IN (libc)
/* The size of locks changes between libc and nss_files, so this
member must be last and is only available in libc. */
__libc_lock_define (, lock);
#endif
};
/* File index for __nss_files_data_get. */
enum nss_files_file
{
nss_file_aliasent,
nss_file_etherent,
nss_file_grent,
nss_file_hostent,
nss_file_netent,
nss_file_protoent,
nss_file_pwent,
nss_file_rpcent,
nss_file_servent,
nss_file_sgent,
nss_file_spent,
nss_file_count
};
/* Obtains a pointer to the per-file data for FILE, which is written
to *PDATA, and tries to open the file at PATH for it. On success,
returns NSS_STATUS_SUCCESS, and the caller must later call
__nss_files_data_put. On failure, NSS_STATUS_TRYAGAIN is returned,
and *ERRNOP and *HERRNOP are updated if these pointers are not
null. */
enum nss_status __nss_files_data_open (struct nss_files_per_file_data **pdata,
enum nss_files_file file,
const char *path,
int *errnop, int *herrnop);
libc_hidden_proto (__nss_files_data_open)
/* Unlock the per-file data, previously obtained by
__nss_files_data_open. */
void __nss_files_data_put (struct nss_files_per_file_data *data);
libc_hidden_proto (__nss_files_data_put)
/* Performs the set*ent operation for FILE. PATH is the file to
open. */
enum nss_status __nss_files_data_setent (enum nss_files_file file,
const char *path);
libc_hidden_proto (__nss_files_data_setent)
/* Performs the end*ent operation for FILE. */
enum nss_status __nss_files_data_endent (enum nss_files_file file);
libc_hidden_proto (__nss_files_data_endent)
struct parser_data;
/* Instances of the parse_line function from
nss/nss_files/files-parse.c. */
typedef int nss_files_parse_line (char *line, void *result,
struct parser_data *data,
size_t datalen, int *errnop);
extern nss_files_parse_line _nss_files_parse_etherent;
extern nss_files_parse_line _nss_files_parse_grent;
extern nss_files_parse_line _nss_files_parse_netent;
extern nss_files_parse_line _nss_files_parse_protoent;
extern nss_files_parse_line _nss_files_parse_pwent;
extern nss_files_parse_line _nss_files_parse_rpcent;
extern nss_files_parse_line _nss_files_parse_servent;
extern nss_files_parse_line _nss_files_parse_sgent;
extern nss_files_parse_line _nss_files_parse_spent;
libc_hidden_proto (_nss_files_parse_etherent)
libc_hidden_proto (_nss_files_parse_grent)
libc_hidden_proto (_nss_files_parse_netent)
libc_hidden_proto (_nss_files_parse_protoent)
libc_hidden_proto (_nss_files_parse_pwent)
libc_hidden_proto (_nss_files_parse_rpcent)
libc_hidden_proto (_nss_files_parse_servent)
libc_hidden_proto (_nss_files_parse_sgent)
libc_hidden_proto (_nss_files_parse_spent)
NSS_DECLARE_MODULE_FUNCTIONS (files)
#undef DEFINE_NSS_FUNCTION
#define DEFINE_NSS_FUNCTION(x) libc_hidden_proto (_nss_files_##x)
#include <nss/function.def>
#undef DEFINE_NSS_FUNCTION
void _nss_files_init (void (*cb) (size_t, struct traced_file *));
libc_hidden_proto (_nss_files_init)
/* Generic implementation of fget*ent_r. Reads lines from FP until
EOF or a successful parse into *RESULT using PARSER. Returns 0 on
success, ENOENT on EOF, ERANGE on too-small buffer. */
int __nss_fgetent_r (FILE *fp, void *result,
char *buffer, size_t buffer_length,
nss_files_parse_line parser) attribute_hidden;
#endif /* _NSS_FILES_H */