nss_action.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* NSS actions, elements in a nsswitch.conf configuration line.
  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_ACTION_H
  16. #define _NSS_ACTION_H
  17. #include <stddef.h>
  18. /* See nss_database.h for a summary of how this relates. */
  19. #include "nsswitch.h" /* For lookup_actions. */
  20. struct nss_module;
  21. /* A NSS action pairs a service module with the action for each result
  22. state. */
  23. struct nss_action
  24. {
  25. /* The service module that provides the functionality (potentially
  26. not yet loaded). */
  27. struct nss_module *module;
  28. /* Action according to result. Two bits for each lookup_actions
  29. value (from nsswitch.h), indexed by enum nss_status (from nss.h). */
  30. unsigned int action_bits;
  31. };
  32. /* Value to add to first nss_status value to get zero. */
  33. #define NSS_STATUS_BIAS 2
  34. /* Number of bits per lookup action. */
  35. #define NSS_BPL 2
  36. #define NSS_BPL_MASK ((1 << NSS_BPL) - 1)
  37. /* Index in actions of an NSS status. Note that in nss/nss.h the
  38. status starts at -2, and we shift that up to zero by adding 2.
  39. Thus for example NSS_STATUS_TRYAGAIN, which is -2, would index into
  40. the 0th bit place as expected. */
  41. static inline int
  42. nss_actions_bits_index (enum nss_status status)
  43. {
  44. return NSS_BPL * (NSS_STATUS_BIAS + status);
  45. }
  46. /* Returns the lookup_action value for STATUS in ACTION. */
  47. static inline lookup_actions
  48. nss_action_get (const struct nss_action *action, enum nss_status status)
  49. {
  50. return ((action->action_bits >> nss_actions_bits_index (status))
  51. & NSS_BPL_MASK);
  52. }
  53. /* Sets the lookup_action value for STATUS in ACTION. */
  54. static inline void
  55. nss_action_set (struct nss_action *action,
  56. enum nss_status status, lookup_actions actions)
  57. {
  58. int offset = nss_actions_bits_index (status);
  59. unsigned int mask = NSS_BPL_MASK << offset;
  60. action->action_bits = ((action->action_bits & ~mask)
  61. | ((unsigned int) actions << offset));
  62. }
  63. static inline void
  64. nss_action_set_all (struct nss_action *action, lookup_actions actions)
  65. {
  66. unsigned int bits = actions & NSS_BPL_MASK;
  67. action->action_bits = ( bits
  68. | (bits << (NSS_BPL * 1))
  69. | (bits << (NSS_BPL * 2))
  70. | (bits << (NSS_BPL * 3))
  71. | (bits << (NSS_BPL * 4))
  72. );
  73. }
  74. /* A list of struct nss_action objects in array terminated by an
  75. action with a NULL module. */
  76. typedef struct nss_action *nss_action_list;
  77. /* Returns a pointer to an allocated NSS action list that has COUNT
  78. actions that matches the array at ACTIONS. */
  79. nss_action_list __nss_action_allocate (struct nss_action *actions,
  80. size_t count) attribute_hidden;
  81. /* Returns a pointer to a list allocated by __nss_action_allocate, or
  82. NULL on error. ENOMEM means a (temporary) memory allocation error,
  83. EINVAL means that LINE is syntactically invalid. */
  84. nss_action_list __nss_action_parse (const char *line);
  85. #endif /* _NSS_ACTION_H */