mips: FIx clone3 implementation (BZ 31325)
[glibc.git] / nss / nsswitch.c
blob6bebf4124221e7a9a2d9e0db155be3a39f4b45f9
1 /* Copyright (C) 1996-2024 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 <ctype.h>
19 #include <dlfcn.h>
20 #include <errno.h>
21 #include <netdb.h>
22 #include <libc-lock.h>
23 #include <search.h>
24 #include <stdio.h>
25 #include <stdio_ext.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #include <aliases.h>
30 #include <grp.h>
31 #include <netinet/ether.h>
32 #include <pwd.h>
33 #include <shadow.h>
34 #include <unistd.h>
36 #if !defined DO_STATIC_NSS || defined SHARED
37 # include <gnu/lib-names.h>
38 #endif
40 #include "nsswitch.h"
41 #include "../nscd/nscd_proto.h"
42 #include <sysdep.h>
43 #include <config.h>
45 /* Declare external database variables. */
46 #define DEFINE_DATABASE(name) \
47 nss_action_list __nss_##name##_database attribute_hidden; \
48 weak_extern (__nss_##name##_database)
49 #include "databases.def"
50 #undef DEFINE_DATABASE
53 #ifdef USE_NSCD
54 /* Flags whether custom rules for database is set. */
55 bool __nss_database_custom[NSS_DBSIDX_max];
56 #endif
58 /*__libc_lock_define_initialized (static, lock)*/
60 /* -1 == not found
61 0 == function found
62 1 == finished */
63 int
64 __nss_lookup (nss_action_list *ni, const char *fct_name, const char *fct2_name,
65 void **fctp)
67 *fctp = __nss_lookup_function (*ni, fct_name);
68 if (*fctp == NULL && fct2_name != NULL)
69 *fctp = __nss_lookup_function (*ni, fct2_name);
71 while (*fctp == NULL
72 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
73 && (*ni)[1].module != NULL)
75 ++(*ni);
77 *fctp = __nss_lookup_function (*ni, fct_name);
78 if (*fctp == NULL && fct2_name != NULL)
79 *fctp = __nss_lookup_function (*ni, fct2_name);
82 return *fctp != NULL ? 0 : (*ni)[1].module == NULL ? 1 : -1;
84 libc_hidden_def (__nss_lookup)
87 /* -1 == not found
88 0 == adjusted for next function
89 1 == finished */
90 int
91 __nss_next2 (nss_action_list *ni, const char *fct_name, const char *fct2_name,
92 void **fctp, int status, int all_values)
94 if (all_values)
96 if (nss_next_action (*ni, NSS_STATUS_TRYAGAIN) == NSS_ACTION_RETURN
97 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_RETURN
98 && nss_next_action (*ni, NSS_STATUS_NOTFOUND) == NSS_ACTION_RETURN
99 && nss_next_action (*ni, NSS_STATUS_SUCCESS) == NSS_ACTION_RETURN)
100 return 1;
102 else
104 /* This is really only for debugging. */
105 if (__builtin_expect (NSS_STATUS_TRYAGAIN > status
106 || status > NSS_STATUS_RETURN, 0))
107 __libc_fatal ("Illegal status in __nss_next.\n");
109 if (nss_next_action (*ni, status) == NSS_ACTION_RETURN)
110 return 1;
113 if ((*ni)[1].module == NULL)
114 return -1;
118 ++(*ni);
120 *fctp = __nss_lookup_function (*ni, fct_name);
121 if (*fctp == NULL && fct2_name != NULL)
122 *fctp = __nss_lookup_function (*ni, fct2_name);
124 while (*fctp == NULL
125 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
126 && (*ni)[1].module != NULL);
128 return *fctp != NULL ? 0 : -1;
130 libc_hidden_def (__nss_next2)
132 void *
133 __nss_lookup_function (nss_action_list ni, const char *fct_name)
135 if (ni->module == NULL)
136 return NULL;
137 return __nss_module_get_function (ni->module, fct_name);
139 libc_hidden_def (__nss_lookup_function)