1 From 6b99458d197ab779ebb6ff632c168e2cbfa4f543 Mon Sep 17 00:00:00 2001
2 From: Florian Weimer <fweimer@redhat.com>
3 Date: Fri, 11 Aug 2023 10:10:16 +0200
4 Subject: [PATCH 03/44] nscd: Do not rebuild getaddrinfo (bug 30709)
6 The nscd daemon caches hosts data from NSS modules verbatim, without
7 filtering protocol families or sorting them (otherwise separate caches
8 would be needed for certain ai_flags combinations). The cache
9 implementation is complete separate from the getaddrinfo code. This
10 means that rebuilding getaddrinfo is not needed. The only function
11 actually used is __bump_nl_timestamp from check_pf.c, and this change
12 moves it into nscd/connections.c.
14 Tested on x86_64-linux-gnu with -fexceptions, built with
15 build-many-glibcs.py. I also backported this patch into a distribution
16 that still supports nscd and verified manually that caching still works.
18 Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
19 (cherry picked from commit 039ff51ac7e02db1cfc0c23e38ac7bfbb00221d1)
21 include/ifaddrs.h | 4 ---
22 inet/check_pf.c | 9 ------
24 nscd/connections.c | 11 +++++++
25 nscd/gai.c | 50 ------------------------------
26 sysdeps/unix/sysv/linux/check_pf.c | 17 +---------
27 6 files changed, 13 insertions(+), 80 deletions(-)
28 delete mode 100644 nscd/gai.c
30 diff --git a/include/ifaddrs.h b/include/ifaddrs.h
31 index 416118f1b3..19a3afb19f 100644
32 --- a/include/ifaddrs.h
33 +++ b/include/ifaddrs.h
34 @@ -34,9 +34,5 @@ extern void __check_native (uint32_t a1_index, int *a1_native,
35 uint32_t a2_index, int *a2_native)
39 -extern uint32_t __bump_nl_timestamp (void) attribute_hidden;
42 # endif /* !_ISOMAC */
43 #endif /* ifaddrs.h */
44 diff --git a/inet/check_pf.c b/inet/check_pf.c
45 index 5310c99121..6d1475920f 100644
48 @@ -60,12 +60,3 @@ __free_in6ai (struct in6addrinfo *in6ai)
56 -__bump_nl_timestamp (void)
61 diff --git a/nscd/Makefile b/nscd/Makefile
62 index 2a0489f4cf..16b6460ee9 100644
65 @@ -35,7 +35,7 @@ nscd-modules := nscd connections pwdcache getpwnam_r getpwuid_r grpcache \
66 getgrnam_r getgrgid_r hstcache gethstbyad_r gethstbynm3_r \
67 getsrvbynm_r getsrvbypt_r servicescache \
68 dbg_log nscd_conf nscd_stat cache mem nscd_setup_thread \
69 - xmalloc xstrdup aicache initgrcache gai res_hconf \
70 + xmalloc xstrdup aicache initgrcache res_hconf \
71 netgroupcache cachedumper
73 ifeq ($(build-nscd)$(have-thread-library),yesyes)
74 diff --git a/nscd/connections.c b/nscd/connections.c
75 index a405a44a9b..15693e5090 100644
76 --- a/nscd/connections.c
77 +++ b/nscd/connections.c
78 @@ -256,6 +256,17 @@ int inotify_fd = -1;
80 /* Descriptor for netlink status updates. */
81 static int nl_status_fd = -1;
84 +__bump_nl_timestamp (void)
86 + static uint32_t nl_timestamp;
88 + if (atomic_fetch_add_relaxed (&nl_timestamp, 1) + 1 == 0)
89 + atomic_fetch_add_relaxed (&nl_timestamp, 1);
91 + return nl_timestamp;
95 /* Number of times clients had to wait. */
96 diff --git a/nscd/gai.c b/nscd/gai.c
97 deleted file mode 100644
98 index e29f3fe583..0000000000
102 -/* Copyright (C) 2004-2023 Free Software Foundation, Inc.
103 - This file is part of the GNU C Library.
105 - This program is free software; you can redistribute it and/or modify
106 - it under the terms of the GNU General Public License as published
107 - by the Free Software Foundation; version 2 of the License, or
108 - (at your option) any later version.
110 - This program is distributed in the hope that it will be useful,
111 - but WITHOUT ANY WARRANTY; without even the implied warranty of
112 - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
113 - GNU General Public License for more details.
115 - You should have received a copy of the GNU General Public License
116 - along with this program; if not, see <https://www.gnu.org/licenses/>. */
119 -#include <sys/stat.h>
121 -/* This file uses the getaddrinfo code but it compiles it without NSCD
122 - support. We just need a few symbol renames. */
123 -#define __ioctl ioctl
124 -#define __getsockname getsockname
125 -#define __socket socket
126 -#define __recvmsg recvmsg
128 -#define __sendto sendto
129 -#define __strchrnul strchrnul
130 -#define __getline getline
131 -#define __qsort_r qsort_r
132 -/* nscd uses 1MB or 2MB thread stacks. */
133 -#define __libc_use_alloca(size) (size <= __MAX_ALLOCA_CUTOFF)
134 -#define __getifaddrs getifaddrs
135 -#define __freeifaddrs freeifaddrs
137 -#define __fstat64 fstat64
139 -#define __stat64 stat64
141 -/* We are nscd, so we don't want to be talking to ourselves. */
144 -#include <getaddrinfo.c>
147 -#include <check_pf.c>
148 -#include <check_native.c>
150 -/* Some variables normally defined in libc. */
151 -nss_action_list __nss_hosts_database attribute_hidden;
152 diff --git a/sysdeps/unix/sysv/linux/check_pf.c b/sysdeps/unix/sysv/linux/check_pf.c
153 index 2b0b8b6368..3aa6a00348 100644
154 --- a/sysdeps/unix/sysv/linux/check_pf.c
155 +++ b/sysdeps/unix/sysv/linux/check_pf.c
156 @@ -66,25 +66,10 @@ static struct cached_data *cache;
157 __libc_lock_define_initialized (static, lock);
161 -static uint32_t nl_timestamp;
164 -__bump_nl_timestamp (void)
166 - if (atomic_fetch_add_relaxed (&nl_timestamp, 1) + 1 == 0)
167 - atomic_fetch_add_relaxed (&nl_timestamp, 1);
169 - return nl_timestamp;
173 static inline uint32_t
174 get_nl_timestamp (void)
177 - return nl_timestamp;
178 -#elif defined USE_NSCD
179 +#if defined USE_NSCD
180 return __nscd_get_nl_timestamp ();