powerpc: Fix float128 IFUNC relocations [BZ #21707]
[glibc.git] / nss / nss_test1.c
blobb728e418a3dafabcaff4d253dd1a1e3177e0cb61
1 /* Template generic NSS service provider. See nss_test.h for usage.
2 Copyright (C) 2017 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 <http://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <nss.h>
21 #include <pthread.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <alloc_buffer.h>
27 /* We need to be able to handle NULLs "properly" within the testsuite,
28 to test known bad data. */
29 #define alloc_buffer_maybe_copy_string(b,s) s ? alloc_buffer_copy_string (b, s) : NULL;
31 /* This file is the master template. Other instances of this test
32 module should define NAME(x) to have their name instead of "test1",
33 then include this file.
35 #define NAME_(x,n) _nss_##n##_##x
36 #ifndef NAME
37 #define NAME(x) NAME_(x,test1)
38 #endif
39 #define NAMESTR__(x) #x
40 #define NAMESTR_(x) NAMESTR__(x)
41 #define NAMESTR(x) NAMESTR_(NAME(x))
43 #include "nss_test.h"
45 /* -------------------------------------------------- */
46 /* Default Data. */
48 static struct passwd default_pwd_data[] =
50 #define PWD(u) \
51 { .pw_name = (char *) "name" #u, .pw_passwd = (char *) "*", .pw_uid = u, \
52 .pw_gid = 100, .pw_gecos = (char *) "*", .pw_dir = (char *) "*", \
53 .pw_shell = (char *) "*" }
54 PWD (30),
55 PWD (100),
56 PWD (200),
57 PWD (60),
58 PWD (20000)
60 #define default_npwd_data (sizeof (pwd_data) / sizeof (pwd_data[0]))
62 static struct passwd *pwd_data = default_pwd_data;
63 static int npwd_data = default_npwd_data;
65 static struct group *grp_data = NULL;
66 static int ngrp_data = 0;
68 /* This function will get called, and once per session, look back into
69 the test case's executable for an init hook function, and call
70 it. */
72 static int initted = 0;
73 static void
74 init(void)
76 test_tables t;
77 int i;
79 if (initted)
80 return;
81 if (NAME(init_hook))
83 memset (&t, 0, sizeof(t));
84 NAME(init_hook)(&t);
86 if (t.pwd_table)
88 pwd_data = t.pwd_table;
89 for (i=0; ! PWD_ISLAST(& pwd_data[i]); i++)
91 npwd_data = i;
94 if (t.grp_table)
96 grp_data = t.grp_table;
97 for (i=0; ! GRP_ISLAST(& grp_data[i]); i++)
99 ngrp_data = i;
102 initted = 1;
105 /* -------------------------------------------------- */
106 /* Password handling. */
108 static size_t pwd_iter;
109 #define CURPWD pwd_data[pwd_iter]
111 static pthread_mutex_t pwd_lock = PTHREAD_MUTEX_INITIALIZER;
113 enum nss_status
114 NAME(setpwent) (int stayopen)
116 init();
117 pwd_iter = 0;
118 return NSS_STATUS_SUCCESS;
122 enum nss_status
123 NAME(endpwent) (void)
125 init();
126 return NSS_STATUS_SUCCESS;
129 static enum nss_status
130 copy_passwd (struct passwd *result, struct passwd *local,
131 char *buffer, size_t buflen, int *errnop)
133 struct alloc_buffer buf = alloc_buffer_create (buffer, buflen);
135 result->pw_name = alloc_buffer_maybe_copy_string (&buf, local->pw_name);
136 result->pw_passwd = alloc_buffer_maybe_copy_string (&buf, local->pw_passwd);
137 result->pw_uid = local->pw_uid;
138 result->pw_gid = local->pw_gid;
139 result->pw_gecos = alloc_buffer_maybe_copy_string (&buf, local->pw_gecos);
140 result->pw_dir = alloc_buffer_maybe_copy_string (&buf, local->pw_dir);
141 result->pw_shell = alloc_buffer_maybe_copy_string (&buf, local->pw_shell);
143 if (alloc_buffer_has_failed (&buf))
145 *errnop = ERANGE;
146 return NSS_STATUS_TRYAGAIN;
149 return NSS_STATUS_SUCCESS;
152 enum nss_status
153 NAME(getpwent_r) (struct passwd *result, char *buffer, size_t buflen,
154 int *errnop)
156 int res = NSS_STATUS_SUCCESS;
158 init();
159 pthread_mutex_lock (&pwd_lock);
161 if (pwd_iter >= npwd_data)
162 res = NSS_STATUS_NOTFOUND;
163 else
165 res = copy_passwd (result, &CURPWD, buffer, buflen, errnop);
166 ++pwd_iter;
169 pthread_mutex_unlock (&pwd_lock);
171 return res;
175 enum nss_status
176 NAME(getpwuid_r) (uid_t uid, struct passwd *result, char *buffer,
177 size_t buflen, int *errnop)
179 init();
180 for (size_t idx = 0; idx < npwd_data; ++idx)
181 if (pwd_data[idx].pw_uid == uid)
182 return copy_passwd (result, &pwd_data[idx], buffer, buflen, errnop);
184 return NSS_STATUS_NOTFOUND;
188 enum nss_status
189 NAME(getpwnam_r) (const char *name, struct passwd *result, char *buffer,
190 size_t buflen, int *errnop)
192 init();
193 for (size_t idx = 0; idx < npwd_data; ++idx)
194 if (strcmp (pwd_data[idx].pw_name, name) == 0)
195 return copy_passwd (result, &pwd_data[idx], buffer, buflen, errnop);
197 return NSS_STATUS_NOTFOUND;
200 /* -------------------------------------------------- */
201 /* Group handling. */
203 static size_t grp_iter;
204 #define CURGRP grp_data[grp_iter]
206 static pthread_mutex_t grp_lock = PTHREAD_MUTEX_INITIALIZER;
208 enum nss_status
209 NAME(setgrent) (int stayopen)
211 init();
212 grp_iter = 0;
213 return NSS_STATUS_SUCCESS;
217 enum nss_status
218 NAME(endgrent) (void)
220 init();
221 return NSS_STATUS_SUCCESS;
224 static enum nss_status
225 copy_group (struct group *result, struct group *local,
226 char *buffer, size_t buflen, int *errnop)
228 struct alloc_buffer buf = alloc_buffer_create (buffer, buflen);
229 char **memlist;
230 int i;
232 if (local->gr_mem)
234 i = 0;
235 while (local->gr_mem[i])
236 ++i;
238 memlist = alloc_buffer_alloc_array (&buf, char *, i + 1);
240 if (memlist) {
241 for (i = 0; local->gr_mem[i]; ++i)
242 memlist[i] = alloc_buffer_maybe_copy_string (&buf, local->gr_mem[i]);
243 memlist[i] = NULL;
246 result->gr_mem = memlist;
248 else
249 result->gr_mem = NULL;
251 result->gr_name = alloc_buffer_maybe_copy_string (&buf, local->gr_name);
252 result->gr_passwd = alloc_buffer_maybe_copy_string (&buf, local->gr_passwd);
253 result->gr_gid = local->gr_gid;
255 if (alloc_buffer_has_failed (&buf))
257 *errnop = ERANGE;
258 return NSS_STATUS_TRYAGAIN;
261 return NSS_STATUS_SUCCESS;
265 enum nss_status
266 NAME(getgrent_r) (struct group *result, char *buffer, size_t buflen,
267 int *errnop)
269 int res = NSS_STATUS_SUCCESS;
271 init();
272 pthread_mutex_lock (&grp_lock);
274 if (grp_iter >= ngrp_data)
275 res = NSS_STATUS_NOTFOUND;
276 else
278 res = copy_group (result, &CURGRP, buffer, buflen, errnop);
279 ++grp_iter;
282 pthread_mutex_unlock (&pwd_lock);
284 return res;
288 enum nss_status
289 NAME(getgrgid_r) (gid_t gid, struct group *result, char *buffer,
290 size_t buflen, int *errnop)
292 init();
293 for (size_t idx = 0; idx < ngrp_data; ++idx)
294 if (grp_data[idx].gr_gid == gid)
295 return copy_group (result, &grp_data[idx], buffer, buflen, errnop);
297 return NSS_STATUS_NOTFOUND;
301 enum nss_status
302 NAME(getgrnam_r) (const char *name, struct group *result, char *buffer,
303 size_t buflen, int *errnop)
305 init();
306 for (size_t idx = 0; idx < ngrp_data; ++idx)
307 if (strcmp (pwd_data[idx].pw_name, name) == 0)
309 return copy_group (result, &grp_data[idx], buffer, buflen, errnop);
312 return NSS_STATUS_NOTFOUND;