unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Florian Weimer via Libc-alpha <libc-alpha@sourceware.org>
To: libc-alpha@sourceware.org
Subject: [PATCH 30/30] nss: Directly load nss_dns, without going through dlsym/dlopen
Date: Thu, 08 Jul 2021 17:07:35 +0200	[thread overview]
Message-ID: <173b8b3328d263fd01e89562685a5dc14fd37b25.1625755446.git.fweimer@redhat.com> (raw)
In-Reply-To: <cover.1625755445.git.fweimer@redhat.com>

This partially fixes static-only NSS support (bug 27959): The dns
module no longer needs dlopen.  Support for the files module remains
to be added, and also support for disabling dlopen altogher.

This commit introduces module_load_builtin into nss/nss_module.c, which
handles the common parts of loading the built-in nss_files and nss_dns
modules.
---
 include/nss_dns.h          | 13 +++++----
 nss/nss_files_functions.c  |  6 -----
 nss/nss_module.c           | 55 +++++++++++++++++++++++++++++---------
 nss/nss_module.h           | 10 +++++--
 resolv/Makefile            |  1 +
 resolv/nss_dns_functions.c | 40 +++++++++++++++++++++++++++
 6 files changed, 99 insertions(+), 26 deletions(-)
 create mode 100644 resolv/nss_dns_functions.c

diff --git a/include/nss_dns.h b/include/nss_dns.h
index 63b5853870..53205b27a6 100644
--- a/include/nss_dns.h
+++ b/include/nss_dns.h
@@ -24,13 +24,16 @@
 NSS_DECLARE_MODULE_FUNCTIONS (dns)
 
 libc_hidden_proto (_nss_dns_getcanonname_r)
-libc_hidden_proto (_nss_dns_gethostbyname3_r)
-libc_hidden_proto (_nss_dns_gethostbyname2_r)
-libc_hidden_proto (_nss_dns_gethostbyname_r)
-libc_hidden_proto (_nss_dns_gethostbyname4_r)
 libc_hidden_proto (_nss_dns_gethostbyaddr2_r)
 libc_hidden_proto (_nss_dns_gethostbyaddr_r)
-libc_hidden_proto (_nss_dns_getnetbyname_r)
+libc_hidden_proto (_nss_dns_gethostbyname2_r)
+libc_hidden_proto (_nss_dns_gethostbyname3_r)
+libc_hidden_proto (_nss_dns_gethostbyname4_r)
+libc_hidden_proto (_nss_dns_gethostbyname_r)
 libc_hidden_proto (_nss_dns_getnetbyaddr_r)
+libc_hidden_proto (_nss_dns_getnetbyname_r)
+
+void __nss_dns_functions (nss_module_functions_untyped pointers)
+  attribute_hidden;
 
 #endif
diff --git a/nss/nss_files_functions.c b/nss/nss_files_functions.c
index 85720b4311..46040fff70 100644
--- a/nss/nss_files_functions.c
+++ b/nss/nss_files_functions.c
@@ -34,10 +34,4 @@ __nss_files_functions (nss_module_functions_untyped pointers)
 #undef DEFINE_NSS_FUNCTION
 #define DEFINE_NSS_FUNCTION(x) *fptr++ = _nss_files_##x;
 #include "function.def"
-
-#ifdef PTR_MANGLE
-  void **end = fptr;
-  for (fptr = pointers; fptr != end; ++fptr)
-    PTR_MANGLE (*fptr);
-#endif
 }
diff --git a/nss/nss_module.c b/nss/nss_module.c
index 7b42c585a4..a4a66866fb 100644
--- a/nss/nss_module.c
+++ b/nss/nss_module.c
@@ -26,11 +26,13 @@
 #include <dlfcn.h>
 #include <gnu/lib-names.h>
 #include <libc-lock.h>
+#include <nss_dns.h>
+#include <nss_files.h>
 #include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <nss_files.h>
+#include <sysdep.h>
 
 /* Suffix after .so of NSS service modules.  This is a bit of magic,
    but we assume LIBNSS_FILES_SO looks like "libnss_files.so.2" and we
@@ -111,18 +113,12 @@ static const function_name nss_function_name_array[] =
 #include "function.def"
   };
 
+/* Loads a built-in module, binding the symbols using the supplied
+   callback function.  Always returns true.  */
 static bool
-module_load_nss_files (struct nss_module *module)
+module_load_builtin (struct nss_module *module,
+		     void (*bind) (nss_module_functions_untyped))
 {
-  if (is_nscd)
-    {
-      void (*cb) (size_t, struct traced_file *) = nscd_init_cb;
-#  ifdef PTR_DEMANGLE
-      PTR_DEMANGLE (cb);
-#  endif
-      _nss_files_init (cb);
-    }
-
   /* Initialize the function pointers, following the double-checked
      locking idiom.  */
   __libc_lock_lock (nss_module_list_lock);
@@ -130,7 +126,13 @@ module_load_nss_files (struct nss_module *module)
     {
     case nss_module_uninitialized:
     case nss_module_failed:
-      __nss_files_functions (module->functions.untyped);
+      bind (module->functions.untyped);
+
+#ifdef PTR_MANGLE
+      for (int i = 0; i < nss_module_functions_count; ++i)
+	PTR_MANGLE (module->functions.untyped[i]);
+#endif
+
       module->handle = NULL;
       /* Synchronizes with unlocked __nss_module_load atomic_load_acquire.  */
       atomic_store_release (&module->state, nss_module_loaded);
@@ -143,12 +145,37 @@ module_load_nss_files (struct nss_module *module)
   return true;
 }
 
+/* Loads the built-in nss_files module.  */
+static bool
+module_load_nss_files (struct nss_module *module)
+{
+  if (is_nscd)
+    {
+      void (*cb) (size_t, struct traced_file *) = nscd_init_cb;
+#  ifdef PTR_DEMANGLE
+      PTR_DEMANGLE (cb);
+#  endif
+      _nss_files_init (cb);
+    }
+  return module_load_builtin (module, __nss_files_functions);
+}
+
+/* Loads the built-in nss_dns module.  */
+static bool
+module_load_nss_dns (struct nss_module *module)
+{
+  return module_load_builtin (module, __nss_dns_functions);
+}
+
+
 /* Internal implementation of __nss_module_load.  */
 static bool
 module_load (struct nss_module *module)
 {
   if (strcmp (module->name, "files") == 0)
     return module_load_nss_files (module);
+  if (strcmp (module->name, "dns") == 0)
+    return module_load_nss_dns (module);
 
   void *handle;
   {
@@ -396,7 +423,9 @@ __nss_module_freeres (void)
   struct nss_module *current = nss_module_list;
   while (current != NULL)
     {
-      if (current->state == nss_module_loaded && current->handle != NULL)
+      /* Ignore built-in modules (which have a NULL handle).  */
+      if (current->state == nss_module_loaded
+	  && current->handle != NULL)
         __libc_dlclose (current->handle);
 
       struct nss_module *next = current->next;
diff --git a/nss/nss_module.h b/nss/nss_module.h
index c1a1d90b60..b52c2935d2 100644
--- a/nss/nss_module.h
+++ b/nss/nss_module.h
@@ -33,10 +33,16 @@ struct nss_module_functions
 #include "function.def"
 };
 
+/* Number of elements of the nss_module_functions_untyped array.  */
+enum
+  {
+    nss_module_functions_count = (sizeof (struct nss_module_functions)
+                                  / sizeof (void *))
+  };
+
 /* Untyped version of struct nss_module_functions, for consistent
    processing purposes.  */
-typedef void *nss_module_functions_untyped[sizeof (struct nss_module_functions)
-                                           / sizeof (void *)];
+typedef void *nss_module_functions_untyped[nss_module_functions_count];
 
 /* Locate the nss_files functions, as if by dlopen/dlsym.  */
 void __nss_files_functions (nss_module_functions_untyped pointers)
diff --git a/resolv/Makefile b/resolv/Makefile
index dd0a98c74f..31d27454b4 100644
--- a/resolv/Makefile
+++ b/resolv/Makefile
@@ -48,6 +48,7 @@ routines := \
   ns_name_unpack \
   ns_samename \
   nsap_addr \
+  nss_dns_functions \
   res-close \
   res-name-checking \
   res-state \
diff --git a/resolv/nss_dns_functions.c b/resolv/nss_dns_functions.c
new file mode 100644
index 0000000000..158dafec90
--- /dev/null
+++ b/resolv/nss_dns_functions.c
@@ -0,0 +1,40 @@
+/* Direct access for nss_dns functions for NSS module loading.
+   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/nss_module.h>
+#include <nss_dns.h>
+#include <string.h>
+
+void
+__nss_dns_functions (nss_module_functions_untyped pointers)
+{
+  struct nss_module_functions typed =
+    {
+      .getcanonname_r = &_nss_dns_getcanonname_r,
+      .gethostbyname3_r = &_nss_dns_gethostbyname3_r,
+      .gethostbyname2_r = &_nss_dns_gethostbyname2_r,
+      .gethostbyname_r = &_nss_dns_gethostbyname_r,
+      .gethostbyname4_r = &_nss_dns_gethostbyname4_r,
+      .gethostbyaddr2_r = &_nss_dns_gethostbyaddr2_r,
+      .gethostbyaddr_r = &_nss_dns_gethostbyaddr_r,
+      .getnetbyname_r = &_nss_dns_getnetbyname_r,
+      .getnetbyaddr_r = &_nss_dns_getnetbyaddr_r,
+    };
+
+  memcpy (pointers, &typed, sizeof (nss_module_functions_untyped));
+}
-- 
2.31.1


  parent reply	other threads:[~2021-07-08 15:27 UTC|newest]

Thread overview: 70+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-08 14:59 [PATCH v2 00/30] nss_dns move into libc Florian Weimer via Libc-alpha
2021-07-08 14:59 ` [PATCH 01/30] socket: Add hidden prototype for setsockopt Florian Weimer via Libc-alpha
2021-07-08 18:02   ` Adhemerval Zanella via Libc-alpha
2021-07-15  4:59   ` Carlos O'Donell via Libc-alpha
2021-07-08 14:59 ` [PATCH 02/30] resolv: Deprecate legacy interfaces in <resolv.h> Florian Weimer via Libc-alpha
2021-07-08 18:32   ` Adhemerval Zanella via Libc-alpha
2021-07-15  4:59   ` Carlos O'Donell via Libc-alpha
2021-07-08 14:59 ` [PATCH 03/30] resolv: Sort Makefile routines and Versions lexicographically Florian Weimer via Libc-alpha
2021-07-08 18:33   ` Adhemerval Zanella via Libc-alpha
2021-07-15  4:59   ` Carlos O'Donell via Libc-alpha
2021-07-08 15:00 ` [PATCH 04/30] nss_dns: Do not use deprecated packet parsing functions Florian Weimer via Libc-alpha
2021-07-15  5:00   ` Carlos O'Donell via Libc-alpha
2021-07-08 15:00 ` [PATCH 05/30] resolv: Move ns_name_ntop to its own file and into libc Florian Weimer via Libc-alpha
2021-07-15  5:00   ` Carlos O'Donell via Libc-alpha
2021-07-08 15:00 ` [PATCH 06/30] resolv: Move ns_name_unpack " Florian Weimer via Libc-alpha
2021-07-15  5:00   ` Carlos O'Donell via Libc-alpha
2021-07-08 15:00 ` [PATCH 07/30] resolv: Move ns_name_skip " Florian Weimer via Libc-alpha
2021-07-15  5:00   ` Carlos O'Donell via Libc-alpha
2021-07-15  6:53     ` Florian Weimer via Libc-alpha
2021-07-08 15:00 ` [PATCH 08/30] resolv: Move ns_name_uncompress into " Florian Weimer via Libc-alpha
2021-07-15  5:00   ` Carlos O'Donell via Libc-alpha
2021-07-08 15:01 ` [PATCH 09/30] resolv: Move ns_name_pton " Florian Weimer via Libc-alpha
2021-07-15  5:00   ` Carlos O'Donell via Libc-alpha
2021-07-08 15:01 ` [PATCH 10/30] resolv: Move ns_name_pack " Florian Weimer via Libc-alpha
2021-07-15  5:00   ` Carlos O'Donell via Libc-alpha
2021-07-08 15:01 ` [PATCH 11/30] resolv: Move ns_name_compress " Florian Weimer via Libc-alpha
2021-07-15  5:01   ` Carlos O'Donell via Libc-alpha
2021-07-08 15:02 ` [PATCH 12/30] resolv: Move dn_expand to " Florian Weimer via Libc-alpha
2021-07-15  5:01   ` Carlos O'Donell via Libc-alpha
2021-07-08 15:02 ` [PATCH 13/30] resolv: Move _getlong, _getshort, __putlong, __putshort to res-putget Florian Weimer via Libc-alpha
2021-07-15  5:01   ` Carlos O'Donell via Libc-alpha
2021-07-08 15:03 ` [PATCH 14/30] resolv: Move dn_comp to its own file and into libc Florian Weimer via Libc-alpha
2021-07-15  5:01   ` Carlos O'Donell via Libc-alpha
2021-07-08 15:03 ` [PATCH 15/30] resolv: Move dn_skipname " Florian Weimer via Libc-alpha
2021-07-15  5:01   ` Carlos O'Donell via Libc-alpha
2021-07-08 15:03 ` [PATCH 16/30] resolv: Rename res_comp.c to res-name-checking.c and move " Florian Weimer via Libc-alpha
2021-07-15  5:01   ` Carlos O'Donell via Libc-alpha
2021-07-08 15:04 ` [PATCH 17/30] resolv: Remove unnecessary res_isourserver_p call from send_dg Florian Weimer via Libc-alpha
2021-07-15  5:01   ` Carlos O'Donell via Libc-alpha
2021-07-08 15:04 ` [PATCH 18/30] resolv: Move __res_get_nsaddr to its own file and into libc Florian Weimer via Libc-alpha
2021-07-15  5:01   ` Carlos O'Donell via Libc-alpha
2021-07-08 15:05 ` [PATCH 19/30] resolv: Move res_isourserver to its own file and reformat to GNU style Florian Weimer via Libc-alpha
2021-07-15  5:01   ` Carlos O'Donell via Libc-alpha
2021-07-08 15:05 ` [PATCH 20/30] resolv: Move ns_makecanon into its own file, and into libc Florian Weimer via Libc-alpha
2021-07-15  5:01   ` Carlos O'Donell via Libc-alpha
2021-07-08 15:05 ` [PATCH 21/30] resolv: Move ns_samename " Florian Weimer via Libc-alpha
2021-07-15  5:01   ` Carlos O'Donell via Libc-alpha
2021-07-08 15:05 ` [PATCH 22/30] resolv: Move res_nameinquery to its own file " Florian Weimer via Libc-alpha
2021-07-15  5:01   ` Carlos O'Donell via Libc-alpha
2021-07-08 15:06 ` [PATCH 23/30] resolv: Move res_queriesmatch " Florian Weimer via Libc-alpha
2021-07-15  5:02   ` Carlos O'Donell via Libc-alpha
2021-07-08 15:06 ` [PATCH 24/30] resolv: Move __res_context_hostalias into " Florian Weimer via Libc-alpha
2021-07-15  5:02   ` Carlos O'Donell via Libc-alpha
2021-07-08 15:06 ` [PATCH 25/30] resolv: Move res_hostalias into its own file, along with hostalias Florian Weimer via Libc-alpha
2021-07-15  5:02   ` Carlos O'Donell via Libc-alpha
2021-07-08 15:06 ` [PATCH 26/30] resolv: Move res_send, res_nsend into libc Florian Weimer via Libc-alpha
2021-07-15  5:02   ` Carlos O'Donell via Libc-alpha
2021-07-08 15:06 ` [PATCH 27/30] resolv: Move res_mkquery, res_nmkquery " Florian Weimer via Libc-alpha
2021-07-15  5:02   ` Carlos O'Donell via Libc-alpha
2021-07-08 15:07 ` [PATCH 28/30] resolv: Move res_query functions " Florian Weimer via Libc-alpha
2021-07-15  5:02   ` Carlos O'Donell via Libc-alpha
2021-07-08 15:07 ` [PATCH 29/30] resolv: Move nss_dns " Florian Weimer via Libc-alpha
2021-07-15  5:02   ` Carlos O'Donell via Libc-alpha
2021-07-08 15:07 ` Florian Weimer via Libc-alpha [this message]
2021-07-08 15:42   ` [PATCH 30/30] nss: Directly load nss_dns, without going through dlsym/dlopen Florian Weimer via Libc-alpha
2021-07-15  5:02   ` Carlos O'Donell via Libc-alpha
2021-07-15  7:09     ` Florian Weimer via Libc-alpha
2021-07-15 12:21       ` Carlos O'Donell via Libc-alpha
2021-07-15  4:58 ` [PATCH v2 00/30] nss_dns move into libc Carlos O'Donell via Libc-alpha
  -- strict thread matches above, loose matches on Subject: below --
2021-07-02 18:47 [PATCH 00/30] Move nss_dns " Florian Weimer via Libc-alpha
2021-07-02 18:50 ` [PATCH 30/30] nss: Directly load nss_dns, without going through dlsym/dlopen Florian Weimer via Libc-alpha

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/libc/involved.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=173b8b3328d263fd01e89562685a5dc14fd37b25.1625755446.git.fweimer@redhat.com \
    --to=libc-alpha@sourceware.org \
    --cc=fweimer@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).