]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - nscd/nscd_helper.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / nscd / nscd_helper.c
index 80ee3e1dd9769db0423c45705f0100495a4511f0..d326a380ee4f14603978b4ae6d0d0e287e213d58 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1998-2007, 2008, 2009 Free Software Foundation, Inc.
+/* Copyright (C) 1998-2019 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
 
    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, write to the Free
-   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
-   02111-1307 USA.  */
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
 
 #include <assert.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <stdbool.h>
 #include <stddef.h>
+#include <stdlib.h>
 #include <string.h>
 #include <time.h>
 #include <unistd.h>
+#include <stdint.h>
 #include <sys/mman.h>
+#include <sys/param.h>
 #include <sys/poll.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
 #include <sys/uio.h>
 #include <sys/un.h>
 #include <not-cancel.h>
-#include <nis/rpcsvc/nis.h>
 #include <kernel-features.h>
+#include <nss.h>
 
 #include "nscd-client.h"
 
-
 /* Extra time we wait if the socket is still receiving data.  This
    value is in milliseconds.  Note that the other side is nscd on the
    local machine and it is already transmitting data.  So the wait
@@ -110,7 +111,7 @@ __readvall (int fd, const struct iovec *iov, int iovcnt)
   ssize_t ret = TEMP_FAILURE_RETRY (__readv (fd, iov, iovcnt));
   if (ret <= 0)
     {
-      if (__builtin_expect (ret == 0 || errno != EAGAIN, 1))
+      if (__glibc_likely (ret == 0 || errno != EAGAIN))
        /* A genuine error or no data to read.  */
        return ret;
 
@@ -165,41 +166,16 @@ open_socket (request_type type, const char *key, size_t keylen)
 {
   int sock;
 
-#ifdef SOCK_CLOEXEC
-# ifndef __ASSUME_SOCK_CLOEXEC
-  if (__have_sock_cloexec >= 0)
-# endif
-    {
-      sock = __socket (PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
-# ifndef __ASSUME_SOCK_CLOEXEC
-      if (__have_sock_cloexec == 0)
-       __have_sock_cloexec = sock != -1 || errno != EINVAL ? 1 : -1;
-# endif
-    }
-#endif
-#ifndef __ASSUME_SOCK_CLOEXEC
-# ifdef SOCK_CLOEXEC
-  if (__have_sock_cloexec < 0)
-# endif
-    sock = __socket (PF_UNIX, SOCK_STREAM, 0);
-#endif
+  sock = __socket (PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
   if (sock < 0)
     return -1;
 
+  size_t real_sizeof_reqdata = sizeof (request_header) + keylen;
   struct
   {
     request_header req;
-    char key[keylen];
-  } reqdata;
-  size_t real_sizeof_reqdata = sizeof (request_header) + keylen;
-
-#ifndef __ASSUME_SOCK_CLOEXEC
-# ifdef SOCK_NONBLOCK
-  if (__have_sock_cloexec < 0)
-# endif
-    /* Make socket non-blocking.  */
-    __fcntl (sock, F_SETFL, O_RDWR | O_NONBLOCK);
-#endif
+    char key[];
+  } *reqdata = alloca (real_sizeof_reqdata);
 
   struct sockaddr_un sun;
   sun.sun_family = AF_UNIX;
@@ -208,11 +184,11 @@ open_socket (request_type type, const char *key, size_t keylen)
       && errno != EINPROGRESS)
     goto out;
 
-  reqdata.req.version = NSCD_VERSION;
-  reqdata.req.type = type;
-  reqdata.req.key_len = keylen;
+  reqdata->req.version = NSCD_VERSION;
+  reqdata->req.type = type;
+  reqdata->req.key_len = keylen;
 
-  memcpy (reqdata.key, key, keylen);
+  memcpy (reqdata->key, key, keylen);
 
   bool first_try = true;
   struct timeval tvend;
@@ -223,10 +199,10 @@ open_socket (request_type type, const char *key, size_t keylen)
 #ifndef MSG_NOSIGNAL
 # define MSG_NOSIGNAL 0
 #endif
-      ssize_t wres = TEMP_FAILURE_RETRY (__send (sock, &reqdata,
+      ssize_t wres = TEMP_FAILURE_RETRY (__send (sock, reqdata,
                                                 real_sizeof_reqdata,
                                                 MSG_NOSIGNAL));
-      if (__builtin_expect (wres == (ssize_t) real_sizeof_reqdata, 1))
+      if (__glibc_likely (wres == (ssize_t) real_sizeof_reqdata))
        /* We managed to send the request.  */
        return sock;
 
@@ -260,7 +236,7 @@ open_socket (request_type type, const char *key, size_t keylen)
     }
 
  out:
-  close_not_cancel_no_status (sock);
+  __close_nocancel_nostatus (sock);
 
   return -1;
 }
@@ -277,9 +253,9 @@ __nscd_unmap (struct mapped_database *mapped)
 
 /* Try to get a file descriptor for the shared meory segment
    containing the database.  */
-static struct mapped_database *
-get_mapping (request_type type, const char *key,
-            struct mapped_database **mappedp)
+struct mapped_database *
+__nscd_get_mapping (request_type type, const char *key,
+                   struct mapped_database **mappedp)
 {
   struct mapped_database *result = NO_MAPPING;
 #ifdef SCM_RIGHTS
@@ -339,13 +315,13 @@ get_mapping (request_type type, const char *key,
   int *ip = (void *) CMSG_DATA (cmsg);
   mapfd = *ip;
 
-  if (__builtin_expect (n != keylen && n != keylen + sizeof (mapsize), 0))
+  if (__glibc_unlikely (n != keylen && n != keylen + sizeof (mapsize)))
     goto out_close;
 
-  if (__builtin_expect (strcmp (resdata, key) != 0, 0))
+  if (__glibc_unlikely (strcmp (resdata, key) != 0))
     goto out_close;
 
-  if (__builtin_expect (n == keylen, 0))
+  if (__glibc_unlikely (n == keylen))
     {
       struct stat64 st;
       if (__builtin_expect (fstat64 (mapfd, &st) != 0, 0)
@@ -358,7 +334,7 @@ get_mapping (request_type type, const char *key,
 
   /* The file is large enough, map it now.  */
   void *mapping = __mmap (NULL, mapsize, PROT_READ, MAP_SHARED, mapfd, 0);
-  if (__builtin_expect (mapping != MAP_FAILED, 1))
+  if (__glibc_likely (mapping != MAP_FAILED))
     {
       /* Check whether the database is correct and up-to-date.  */
       struct database_pers_head *head = mapping;
@@ -383,7 +359,7 @@ get_mapping (request_type type, const char *key,
                                               ALIGN)
                     + head->data_size);
 
-      if (__builtin_expect (mapsize < size, 0))
+      if (__glibc_unlikely (mapsize < size))
        goto out_unmap;
 
       /* Allocate a record for the mapping.  */
@@ -420,7 +396,6 @@ get_mapping (request_type type, const char *key,
   return result;
 }
 
-
 struct mapped_database *
 __nscd_get_map_ref (request_type type, const char *name,
                    volatile struct locked_map_ptr *mapptr, int *gc_cyclep)
@@ -429,30 +404,22 @@ __nscd_get_map_ref (request_type type, const char *name,
   if (cur == NO_MAPPING)
     return cur;
 
-  int cnt = 0;
-  while (__builtin_expect (atomic_compare_and_exchange_val_acq (&mapptr->lock,
-                                                               1, 0) != 0, 0))
-    {
-      // XXX Best number of rounds?
-      if (__builtin_expect (++cnt > 5, 0))
-       return NO_MAPPING;
-
-      atomic_delay ();
-    }
+  if (!__nscd_acquire_maplock (mapptr))
+    return NO_MAPPING;
 
   cur = mapptr->mapped;
 
-  if (__builtin_expect (cur != NO_MAPPING, 1))
+  if (__glibc_likely (cur != NO_MAPPING))
     {
       /* If not mapped or timestamp not updated, request new map.  */
       if (cur == NULL
          || (cur->head->nscd_certainly_running == 0
              && cur->head->timestamp + MAPPING_TIMEOUT < time (NULL))
          || cur->head->data_size > cur->datasize)
-       cur = get_mapping (type, name,
-                          (struct mapped_database **) &mapptr->mapped);
+       cur = __nscd_get_mapping (type, name,
+                                 (struct mapped_database **) &mapptr->mapped);
 
-      if (__builtin_expect (cur != NO_MAPPING, 1))
+      if (__glibc_likely (cur != NO_MAPPING))
        {
          if (__builtin_expect (((*gc_cyclep = cur->head->gc_cycle) & 1) != 0,
                                0))
@@ -468,6 +435,14 @@ __nscd_get_map_ref (request_type type, const char *name,
 }
 
 
+/* Using sizeof (hashentry) is not always correct to determine the size of
+   the data structure as found in the nscd cache.  The program could be
+   a 64-bit process and nscd could be a 32-bit process.  In this case
+   sizeof (hashentry) would overestimate the size.  The following is
+   the minimum size of such an entry, good enough for our tests here.  */
+#define MINIMUM_HASHENTRY_SIZE \
+  (offsetof (struct hashentry, dellist) + sizeof (int32_t))
+
 /* Don't return const struct datahead *, as eventhough the record
    is normally constant, it can change arbitrarily during nscd
    garbage collection.  */
@@ -475,21 +450,22 @@ struct datahead *
 __nscd_cache_search (request_type type, const char *key, size_t keylen,
                     const struct mapped_database *mapped, size_t datalen)
 {
-  unsigned long int hash = __nis_hash (key, keylen) % mapped->head->module;
+  unsigned long int hash = __nss_hash (key, keylen) % mapped->head->module;
   size_t datasize = mapped->datasize;
 
   ref_t trail = mapped->head->array[hash];
   trail = atomic_forced_read (trail);
   ref_t work = trail;
-  size_t loop_cnt = datasize / (offsetof (struct datahead, data) + datalen);
+  size_t loop_cnt = datasize / (MINIMUM_HASHENTRY_SIZE
+                               + offsetof (struct datahead, data) / 2);
   int tick = 0;
 
-  while (work != ENDREF && work + sizeof (struct hashentry) <= datasize)
+  while (work != ENDREF && work + MINIMUM_HASHENTRY_SIZE <= datasize)
     {
       struct hashentry *here = (struct hashentry *) (mapped->data + work);
       ref_t here_key, here_packet;
 
-#ifndef _STRING_ARCH_unaligned
+#if !_STRING_ARCH_unaligned
       /* Although during garbage collection when moving struct hashentry
         records around we first copy from old to new location and then
         adjust pointer from previous hashentry to it, there is no barrier
@@ -511,7 +487,7 @@ __nscd_cache_search (request_type type, const char *key, size_t keylen,
          struct datahead *dh
            = (struct datahead *) (mapped->data + here_packet);
 
-#ifndef _STRING_ARCH_unaligned
+#if !_STRING_ARCH_unaligned
          if ((uintptr_t) dh & (__alignof__ (*dh) - 1))
            return NULL;
 #endif
@@ -528,20 +504,20 @@ __nscd_cache_search (request_type type, const char *key, size_t keylen,
       work = atomic_forced_read (here->next);
       /* Prevent endless loops.  This should never happen but perhaps
         the database got corrupted, accidentally or deliberately.  */
-      if (work == trail || loop_cnt-- > 0)
+      if (work == trail || loop_cnt-- == 0)
        break;
       if (tick)
        {
          struct hashentry *trailelem;
          trailelem = (struct hashentry *) (mapped->data + trail);
 
-#ifndef _STRING_ARCH_unaligned
+#if !_STRING_ARCH_unaligned
          /* We have to redo the checks.  Maybe the data changed.  */
          if ((uintptr_t) trailelem & (__alignof__ (*trailelem) - 1))
            return NULL;
 #endif
 
-         if (trail + sizeof (struct hashentry) > datasize)
+         if (trail + MINIMUM_HASHENTRY_SIZE > datasize)
            return NULL;
 
          trail = atomic_forced_read (trailelem->next);
@@ -578,7 +554,7 @@ __nscd_open_socket (const char *key, size_t keylen, request_type type,
            return sock;
        }
 
-      close_not_cancel_no_status (sock);
+      __close_nocancel_nostatus (sock);
     }
 
   __set_errno (saved_errno);