libio: Avoid RMW of flags2 outside lock (BZ #27842)
[glibc.git] / nis / nis_getservlist.c
blob9a7f9b6ea10f1a8e5ead212c2ad23aec807b7a5d
1 /* Copyright (c) 1997-2022 Free Software Foundation, Inc.
2 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.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
18 #include <string.h>
19 #include <rpcsvc/nis.h>
20 #include <shlib-compat.h>
22 #include "nis_xdr.h"
23 #include "nis_intern.h"
25 nis_server **
26 nis_getservlist (const_nis_name dir)
28 nis_result *res;
29 nis_server **serv;
31 res = nis_lookup (dir, FOLLOW_LINKS);
33 if (res != NULL && NIS_RES_STATUS (res) == NIS_SUCCESS)
35 unsigned long i;
36 nis_server *server;
38 serv =
39 malloc (sizeof (nis_server *) *
40 (NIS_RES_OBJECT (res)->DI_data.do_servers.do_servers_len + 1));
41 if (__glibc_unlikely (serv == NULL))
43 nis_freeresult (res);
44 return NULL;
47 for (i = 0; i < NIS_RES_OBJECT (res)->DI_data.do_servers.do_servers_len;
48 ++i)
50 server =
51 &NIS_RES_OBJECT (res)->DI_data.do_servers.do_servers_val[i];
52 serv[i] = calloc (1, sizeof (nis_server));
53 if (__glibc_unlikely (serv[i] == NULL))
55 free_all:
56 while (i-- > 0)
58 free (serv[i]->pkey.n_bytes);
59 if (serv[i]->ep.ep_val != NULL)
61 unsigned long int j;
62 for (j = 0; j < serv[i]->ep.ep_len; ++j)
64 free (serv[i]->ep.ep_val[j].proto);
65 free (serv[i]->ep.ep_val[j].family);
66 free (serv[i]->ep.ep_val[j].uaddr);
68 free (serv[i]->ep.ep_val);
70 free (serv[i]->name);
71 free (serv[i]);
74 free (serv);
76 nis_freeresult (res);
78 return NULL;
81 if (server->name != NULL)
83 serv[i]->name = strdup (server->name);
84 if (__glibc_unlikely (serv[i]->name == NULL))
86 ++i;
87 goto free_all;
91 serv[i]->ep.ep_len = server->ep.ep_len;
92 if (serv[i]->ep.ep_len > 0)
94 unsigned long int j;
96 serv[i]->ep.ep_val =
97 malloc (server->ep.ep_len * sizeof (endpoint));
98 if (__glibc_unlikely (serv[i]->ep.ep_val == NULL))
100 ++i;
101 goto free_all;
104 for (j = 0; j < serv[i]->ep.ep_len; ++j)
106 if (server->ep.ep_val[j].uaddr)
107 serv[i]->ep.ep_val[j].uaddr =
108 strdup (server->ep.ep_val[j].uaddr);
109 else
110 serv[i]->ep.ep_val[j].uaddr = NULL;
111 if (server->ep.ep_val[j].family)
112 serv[i]->ep.ep_val[j].family =
113 strdup (server->ep.ep_val[j].family);
114 else
115 serv[i]->ep.ep_val[j].family = NULL;
116 if (server->ep.ep_val[j].proto)
117 serv[i]->ep.ep_val[j].proto =
118 strdup (server->ep.ep_val[j].proto);
119 else
120 serv[i]->ep.ep_val[j].proto = NULL;
124 serv[i]->key_type = server->key_type;
125 serv[i]->pkey.n_len = server->pkey.n_len;
126 if (server->pkey.n_len > 0)
128 serv[i]->pkey.n_bytes = malloc (server->pkey.n_len);
129 if (__glibc_unlikely (serv[i]->pkey.n_bytes == NULL))
131 ++i;
132 goto free_all;
134 memcpy (serv[i]->pkey.n_bytes, server->pkey.n_bytes,
135 server->pkey.n_len);
138 serv[i] = NULL;
140 else
142 serv = malloc (sizeof (nis_server *));
143 if (__glibc_unlikely (serv != NULL))
144 serv[0] = NULL;
147 nis_freeresult (res);
149 return serv;
151 libnsl_hidden_nolink_def (nis_getservlist, GLIBC_2_1)
153 void
154 nis_freeservlist (nis_server **serv)
156 int i;
158 if (serv == NULL)
159 return;
161 i = 0;
162 while (serv[i] != NULL)
164 xdr_free ((xdrproc_t)_xdr_nis_server, (char *)serv[i]);
165 free (serv[i]);
166 ++i;
168 free (serv);
170 libnsl_hidden_nolink_def (nis_freeservlist, GLIBC_2_1)