about summary refs log tree commit diff
path: root/nss/nss_files_data.c
blob: 80fbfe5fffcbf0dac45ad267b5626f13ab5d9319 (plain) (blame)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* Returns a pointer to the global nss_files data structure.
   Copyright (C) 2021 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 <nss_files.h>

#include <allocate_once.h>
#include <errno.h>
#include <netdb.h>
#include <nss.h>
#include <stdlib.h>

/* This collects all per file-data.   */
struct nss_files_data
{
  struct nss_files_per_file_data files[nss_file_count];
};

/* For use with allocate_once.  */
static void *nss_files_global;
static void *
nss_files_global_allocate (void *closure)
{
  struct nss_files_data *result = malloc (sizeof (*result));
  if (result != NULL)
    {
      for (int i = 0; i < nss_file_count; ++i)
        {
          result->files[i].stream = NULL;
          __libc_lock_init (result->files[i].lock);
        }
    }
  return result;
}
/* Like __nss_files_data_open, but does not perform the open call.  */
static enum nss_status
__nss_files_data_get (struct nss_files_per_file_data **pdata,
                      enum nss_files_file file, int *errnop, int *herrnop)
{
  struct nss_files_data *data = allocate_once (&nss_files_global,
                                               nss_files_global_allocate,
                                               NULL, NULL);
  if (data == NULL)
    {
      if (errnop != NULL)
        *errnop = errno;
      if (herrnop != NULL)
        {
          __set_h_errno (NETDB_INTERNAL);
          *herrnop = NETDB_INTERNAL;
        }
      return NSS_STATUS_TRYAGAIN;
    }

  *pdata = &data->files[file];
  __libc_lock_lock ((*pdata)->lock);
  return NSS_STATUS_SUCCESS;
}

/* Helper function for opening the backing file at PATH.  */
static enum nss_status
__nss_files_data_internal_open (struct nss_files_per_file_data *data,
                                const char *path)
{
  enum nss_status status = NSS_STATUS_SUCCESS;

  if (data->stream == NULL)
    {
      data->stream = __nss_files_fopen (path);

      if (data->stream == NULL)
        status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
    }

  return status;
}


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)
{
  enum nss_status status = __nss_files_data_get (pdata, file, errnop, herrnop);
  if (status != NSS_STATUS_SUCCESS)
    return status;

  /* Be prepared that the set*ent function was not called before.  */
  if ((*pdata)->stream == NULL)
    {
      int saved_errno = errno;
      status = __nss_files_data_internal_open (*pdata, path);
      __set_errno (saved_errno);
      if (status != NSS_STATUS_SUCCESS)
        __nss_files_data_put (*pdata);
    }

  return status;
}

libc_hidden_def (__nss_files_data_open)

void
__nss_files_data_put (struct nss_files_per_file_data *data)
{
  __libc_lock_unlock (data->lock);
}
libc_hidden_def (__nss_files_data_put)

enum nss_status
__nss_files_data_setent (enum nss_files_file file, const char *path)
{
  struct nss_files_per_file_data *data;
  enum nss_status status = __nss_files_data_get (&data, file, NULL, NULL);
  if (status != NSS_STATUS_SUCCESS)
    return status;

  if (data->stream == NULL)
    status = __nss_files_data_internal_open (data, path);
  else
    rewind (data->stream);

  __nss_files_data_put (data);
  return status;
}
libc_hidden_def (__nss_files_data_setent)

enum nss_status
__nss_files_data_endent (enum nss_files_file file)
{
  /* No cleanup is necessary if not initialized.  */
  struct nss_files_data *data = atomic_load_acquire (&nss_files_global);
  if (data == NULL)
    return NSS_STATUS_SUCCESS;

  struct nss_files_per_file_data *fdata = &data->files[file];
  __libc_lock_lock (fdata->lock);
  if (fdata->stream != NULL)
    {
      fclose (fdata->stream);
      fdata->stream = NULL;
    }
  __libc_lock_unlock (fdata->lock);

  return NSS_STATUS_SUCCESS;
}
libc_hidden_def (__nss_files_data_endent)