1 /* NSS actions, elements in a nsswitch.conf configuration line.
2 Copyright (c) 2020-2023 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, see
17 <https://www.gnu.org/licenses/>. */
22 #include <libc-lock.h>
24 /* Maintain a global list of NSS action lists. Since most databases
25 use the same list of actions, this list is usually short.
26 Deduplication in __nss_action_allocate ensures that the list does
27 not grow without bounds. */
29 struct nss_action_list_wrapper
31 /* The next element of the list. */
32 struct nss_action_list_wrapper
*next
;
34 /* Number of elements in the list (excluding the terminator). */
37 /* NULL-terminated list of actions. */
38 struct nss_action actions
[];
41 /* Toplevel list of allocated NSS action lists. */
42 static struct nss_action_list_wrapper
*nss_actions
;
44 /* Lock covers the nss_actions list. */
45 __libc_lock_define (static, nss_actions_lock
);
47 /* Returns true if the actions are equal (same module, same actions
50 actions_equal (const struct nss_action
*a
, const struct nss_action
*b
)
52 return a
->module
== b
->module
&& a
->action_bits
== b
->action_bits
;
56 /* Returns true if COUNT actions at A and B are equal (according to
57 actions_equal above). Caller must ensure that either A or B have at
58 least COUNT actions. */
60 action_lists_equal (const struct nss_action
*a
, const struct nss_action
*b
,
63 for (size_t i
= 0; i
< count
; ++i
)
64 if (!actions_equal (a
+ i
, b
+ i
))
69 /* Returns a pre-allocated action list for COUNT actions at ACTIONS,
70 or NULL if no such list exists. */
71 static nss_action_list
72 find_allocated (struct nss_action
*actions
, size_t count
)
74 for (struct nss_action_list_wrapper
*p
= nss_actions
; p
!= NULL
; p
= p
->next
)
75 if (p
->count
== count
&& action_lists_equal (p
->actions
, actions
, count
))
81 __nss_action_allocate (struct nss_action
*actions
, size_t count
)
83 nss_action_list result
= NULL
;
84 __libc_lock_lock (nss_actions_lock
);
86 result
= find_allocated (actions
, count
);
89 struct nss_action_list_wrapper
*wrapper
90 = malloc (sizeof (*wrapper
) + sizeof (*actions
) * count
);
93 wrapper
->next
= nss_actions
;
94 wrapper
->count
= count
;
95 memcpy (wrapper
->actions
, actions
, sizeof (*actions
) * count
);
96 nss_actions
= wrapper
;
97 result
= wrapper
->actions
;
101 __libc_lock_unlock (nss_actions_lock
);
106 __nss_action_freeres (void)
108 struct nss_action_list_wrapper
*current
= nss_actions
;
109 while (current
!= NULL
)
111 struct nss_action_list_wrapper
*next
= current
->next
;