nss_files.h 5.9 KB

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