aboutsummaryrefslogtreecommitdiff
path: root/nss/nss_action.c
blob: 8ca72546ebafb9df3e9a2c1736944c10c4ccddc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* NSS actions, elements in a nsswitch.conf configuration line.
   Copyright (c) 2020-2023 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/>.  */

#include <nsswitch.h>

#include <string.h>
#include <libc-lock.h>

/* Maintain a global list of NSS action lists.  Since most databases
   use the same list of actions, this list is usually short.
   Deduplication in __nss_action_allocate ensures that the list does
   not grow without bounds.  */

struct nss_action_list_wrapper
{
  /* The next element of the list.  */
  struct nss_action_list_wrapper *next;

  /* Number of elements in the list (excluding the terminator).  */
  size_t count;

  /* NULL-terminated list of actions.  */
  struct nss_action actions[];
};

/* Toplevel list of allocated NSS action lists.  */
static struct nss_action_list_wrapper *nss_actions;

/* Lock covers the nss_actions list.  */
__libc_lock_define (static, nss_actions_lock);

/* Returns true if the actions are equal (same module, same actions
   array).  */
static bool
actions_equal (const struct nss_action *a, const struct nss_action *b)
{
  return a->module == b->module && a->action_bits == b->action_bits;
}


/* Returns true if COUNT actions at A and B are equal (according to
   actions_equal above). Caller must ensure that either A or B have at
   least COUNT actions.  */
static bool
action_lists_equal (const struct nss_action *a, const struct nss_action *b,
                    size_t count)
{
  for (size_t i = 0; i < count; ++i)
    if (!actions_equal (a + i, b + i))
      return false;
  return true;
}

/* Returns a pre-allocated action list for COUNT actions at ACTIONS,
   or NULL if no such list exists.  */
static nss_action_list
find_allocated (struct nss_action *actions, size_t count)
{
  for (struct nss_action_list_wrapper *p = nss_actions; p != NULL; p = p->next)
    if (p->count == count && action_lists_equal (p->actions, actions, count))
      return p->actions;
  return NULL;
}

nss_action_list
__nss_action_allocate (struct nss_action *actions, size_t count)
{
  nss_action_list result = NULL;
  __libc_lock_lock (nss_actions_lock);

  result = find_allocated (actions, count);
  if (result == NULL)
    {
      struct nss_action_list_wrapper *wrapper
        = malloc (sizeof (*wrapper) + sizeof (*actions) * count);
      if (wrapper != NULL)
        {
          wrapper->next = nss_actions;
          wrapper->count = count;
          memcpy (wrapper->actions, actions, sizeof (*actions) * count);
          nss_actions = wrapper;
          result = wrapper->actions;
        }
    }

  __libc_lock_unlock (nss_actions_lock);
  return result;
}

void __libc_freeres_fn_section
__nss_action_freeres (void)
{
  struct nss_action_list_wrapper *current = nss_actions;
  while (current != NULL)
    {
      struct nss_action_list_wrapper *next = current->next;
      free (current);
      current = next;
    }
  nss_actions = NULL;
}