diff options
author | Samuel Thibault <samuel.thibault@ens-lyon.org> | 2018-12-27 16:39:27 +0000 |
---|---|---|
committer | Samuel Thibault <samuel.thibault@ens-lyon.org> | 2018-12-27 16:39:27 +0000 |
commit | 52629237a522c7c146d788ddaaf69946fd2729f9 (patch) | |
tree | 552402b085cff37bc251fc0f45ed9255b53cdd57 /nscd/grpcache.c | |
parent | 3896c5809b49e72fbadc57da2189ff42aa2a5d02 (diff) | |
parent | 064374be911f72dfaec8a75f06da1f9fc1827712 (diff) |
Merge commit 'refs/top-bases/t/hurdsig-boot-fix' into t/hurdsig-boot-fix
Diffstat (limited to 'nscd/grpcache.c')
-rw-r--r-- | nscd/grpcache.c | 109 |
1 files changed, 35 insertions, 74 deletions
diff --git a/nscd/grpcache.c b/nscd/grpcache.c index 38311701a7..b4c8ea9c56 100644 --- a/nscd/grpcache.c +++ b/nscd/grpcache.c @@ -1,5 +1,5 @@ /* Cache handling for group lookup. - Copyright (C) 1998-2016 Free Software Foundation, Inc. + Copyright (C) 1998-2018 Free Software Foundation, Inc. This file is part of the GNU C Library. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998. @@ -16,7 +16,6 @@ You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>. */ -#include <alloca.h> #include <assert.h> #include <errno.h> #include <error.h> @@ -32,12 +31,10 @@ #include <sys/mman.h> #include <sys/socket.h> #include <stackinfo.h> +#include <scratch_buffer.h> #include "nscd.h" #include "dbg_log.h" -#ifdef HAVE_SENDFILE -# include <kernel-features.h> -#endif /* This is the standard reply in case the service is disabled. */ static const gr_response_header disabled = @@ -205,10 +202,19 @@ cache_addgr (struct database_dyn *db, int fd, request_header *req, dataset = NULL; if (he == NULL) - dataset = (struct dataset *) mempool_alloc (db, total + n, 1); + { + /* Prevent an INVALIDATE request from pruning the data between + the two calls to cache_add. */ + if (db->propagate) + pthread_mutex_lock (&db->prune_run_lock); + dataset = (struct dataset *) mempool_alloc (db, total + n, 1); + } if (dataset == NULL) { + if (he == NULL && db->propagate) + pthread_mutex_unlock (&db->prune_run_lock); + /* We cannot permanently add the result in the moment. But we can provide the result as is. Store the data in some temporary memory. */ @@ -309,37 +315,9 @@ cache_addgr (struct database_dyn *db, int fd, request_header *req, unnecessarily let the receiver wait. */ assert (fd != -1); -#ifdef HAVE_SENDFILE - if (__builtin_expect (db->mmap_used, 1) && ! dataset_temporary) - { - assert (db->wr_fd != -1); - assert ((char *) &dataset->resp > (char *) db->data); - assert ((char *) dataset - (char *) db->head - + total - <= (sizeof (struct database_pers_head) - + db->head->module * sizeof (ref_t) - + db->head->data_size)); - ssize_t written = sendfileall (fd, db->wr_fd, - (char *) &dataset->resp - - (char *) db->head, - dataset->head.recsize); - if (written != dataset->head.recsize) - { -# ifndef __ASSUME_SENDFILE - if (written == -1 && errno == ENOSYS) - goto use_write; -# endif - all_written = false; - } - } - else -# ifndef __ASSUME_SENDFILE - use_write: -# endif -#endif - if (writeall (fd, &dataset->resp, dataset->head.recsize) - != dataset->head.recsize) - all_written = false; + if (writeall (fd, &dataset->resp, dataset->head.recsize) + != dataset->head.recsize) + all_written = false; } /* Add the record to the database. But only if it has not been @@ -396,6 +374,8 @@ cache_addgr (struct database_dyn *db, int fd, request_header *req, out: pthread_rwlock_unlock (&db->lock); + if (he == NULL && db->propagate) + pthread_mutex_unlock (&db->prune_run_lock); } } @@ -437,12 +417,12 @@ addgrbyX (struct database_dyn *db, int fd, request_header *req, look again in the table whether the dataset is now available. We simply insert it. It does not matter if it is in there twice. The pruning function only will look at the timestamp. */ - size_t buflen = 1024; - char *buffer = (char *) alloca (buflen); + struct group resultbuf; struct group *grp; - bool use_malloc = false; int errval = 0; + struct scratch_buffer tmpbuf; + scratch_buffer_init (&tmpbuf); if (__glibc_unlikely (debug_level > 0)) { @@ -452,43 +432,24 @@ addgrbyX (struct database_dyn *db, int fd, request_header *req, dbg_log (_("Reloading \"%s\" in group cache!"), keystr); } - while (lookup (req->type, key, &resultbuf, buffer, buflen, &grp) != 0 + while (lookup (req->type, key, &resultbuf, + tmpbuf.data, tmpbuf.length, &grp) != 0 && (errval = errno) == ERANGE) - { - errno = 0; - - if (__glibc_unlikely (buflen > 32768)) - { - char *old_buffer = buffer; - buflen *= 2; - buffer = (char *) realloc (use_malloc ? buffer : NULL, buflen); - if (buffer == NULL) - { - /* We ran out of memory. We cannot do anything but - sending a negative response. In reality this should - never happen. */ - grp = NULL; - buffer = old_buffer; - - /* We set the error to indicate this is (possibly) a - temporary error and that it does not mean the entry - is not available at all. */ - errval = EAGAIN; - break; - } - use_malloc = true; - } - else - /* Allocate a new buffer on the stack. If possible combine it - with the previously allocated buffer. */ - buffer = (char *) extend_alloca (buffer, buflen, 2 * buflen); - } + if (!scratch_buffer_grow (&tmpbuf)) + { + /* We ran out of memory. We cannot do anything but sending a + negative response. In reality this should never + happen. */ + grp = NULL; + /* We set the error to indicate this is (possibly) a temporary + error and that it does not mean the entry is not available + at all. */ + errval = EAGAIN; + break; + } time_t timeout = cache_addgr (db, fd, req, keystr, grp, uid, he, dh, errval); - - if (use_malloc) - free (buffer); - + scratch_buffer_free (&tmpbuf); return timeout; } |