/* Cache handling for group lookup.
- Copyright (C) 1998-2002, 2003, 2004, 2005 Free Software Foundation, Inc.
+ Copyright (C) 1998-2013 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
- 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.
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; version 2 of the License, or
+ (at your option) any later version.
- The GNU C Library is distributed in the hope that it will be useful,
+ This program 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.
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU 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. */
+ 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 <string.h>
#include <unistd.h>
#include <sys/mman.h>
+#include <sys/socket.h>
#include <stackinfo.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 =
};
-static void
+static time_t
cache_addgr (struct database_dyn *db, int fd, request_header *req,
const void *key, struct group *grp, uid_t owner,
- struct hashentry *he, struct datahead *dh, int errval)
+ struct hashentry *const he, struct datahead *dh, int errval)
{
+ bool all_written = true;
ssize_t total;
- ssize_t written;
time_t t = time (NULL);
/* We allocate all data in one memory block: the iov vector,
assert (offsetof (struct dataset, resp) == offsetof (struct datahead, data));
+ time_t timeout = MAX_TIMEOUT_VALUE;
if (grp == NULL)
{
if (he != NULL && errval == EAGAIN)
/* Do not reset the value if we never not reload the record. */
dh->nreloads = reload_count - 1;
- written = total = 0;
+ /* Reload with the same time-to-live value. */
+ timeout = dh->timeout = t + db->postimeout;
+
+ total = 0;
}
else
{
case. */
total = sizeof (notfound);
- written = TEMP_FAILURE_RETRY (write (fd, ¬found, total));
+ if (fd != -1
+ && TEMP_FAILURE_RETRY (send (fd, ¬found, total,
+ MSG_NOSIGNAL)) != total)
+ all_written = false;
- dataset = mempool_alloc (db, sizeof (struct dataset) + req->key_len);
- /* If we cannot permanently store the result, so be it. */
- if (dataset != NULL)
+ /* If we have a transient error or cannot permanently store
+ the result, so be it. */
+ if (errno == EAGAIN || __builtin_expect (db->negtimeout == 0, 0))
+ {
+ /* Mark the old entry as obsolete. */
+ if (dh != NULL)
+ dh->usable = false;
+ }
+ else if ((dataset = mempool_alloc (db, sizeof (struct dataset) + req->key_len, 1)) != NULL)
{
dataset->head.allocsize = sizeof (struct dataset) + req->key_len;
dataset->head.recsize = total;
dataset->head.usable = true;
/* Compute the timeout time. */
- dataset->head.timeout = t + db->negtimeout;
+ timeout = dataset->head.timeout = t + db->negtimeout;
/* This is the reply. */
memcpy (&dataset->resp, ¬found, total);
+ sizeof (struct dataset) + req->key_len, MS_ASYNC);
}
- /* Now get the lock to safely insert the records. */
- pthread_rwlock_rdlock (&db->lock);
-
- if (cache_add (req->type, &dataset->strdata, req->key_len,
- &dataset->head, true, db, owner) < 0)
- /* Ensure the data can be recovered. */
- dataset->head.usable = false;
+ (void) cache_add (req->type, &dataset->strdata, req->key_len,
+ &dataset->head, true, db, owner, he == NULL);
pthread_rwlock_unlock (&db->lock);
if (dh != NULL)
dh->usable = false;
}
- else
- ++db->head->addfailed;
}
}
else
char *gr_name;
char *cp;
const size_t key_len = strlen (key);
- const size_t buf_len = 3 + sizeof (grp->gr_gid) + key_len + 1;
- char *buf = alloca (buf_len);
+ const size_t buf_len = 3 * sizeof (grp->gr_gid) + key_len + 1;
+ size_t alloca_used = 0;
+ char *buf = alloca_account (buf_len, alloca_used);
ssize_t n;
size_t cnt;
/* Determine the length of all members. */
while (grp->gr_mem[gr_mem_cnt])
++gr_mem_cnt;
- gr_mem_len = (uint32_t *) alloca (gr_mem_cnt * sizeof (uint32_t));
+ gr_mem_len = alloca_account (gr_mem_cnt * sizeof (uint32_t), alloca_used);
for (gr_mem_cnt = 0; grp->gr_mem[gr_mem_cnt]; ++gr_mem_cnt)
{
gr_mem_len[gr_mem_cnt] = strlen (grp->gr_mem[gr_mem_cnt]) + 1;
gr_mem_len_total += gr_mem_len[gr_mem_cnt];
}
- written = total = (sizeof (struct dataset)
- + gr_mem_cnt * sizeof (uint32_t)
- + gr_name_len + gr_passwd_len + gr_mem_len_total);
+ total = (offsetof (struct dataset, strdata)
+ + gr_mem_cnt * sizeof (uint32_t)
+ + gr_name_len + gr_passwd_len + gr_mem_len_total);
/* If we refill the cache, first assume the reconrd did not
change. Allocate memory on the cache since it is likely
discarded anyway. If it turns out to be necessary to have a
new record we can still allocate real memory. */
- bool alloca_used = false;
+ bool dataset_temporary = false;
+ bool dataset_malloced = false;
dataset = NULL;
if (he == NULL)
- {
- dataset = (struct dataset *) mempool_alloc (db, total + n);
- if (dataset == NULL)
- ++db->head->addfailed;
- }
+ dataset = (struct dataset *) mempool_alloc (db, total + n, 1);
if (dataset == NULL)
{
/* We cannot permanently add the result in the moment. But
we can provide the result as is. Store the data in some
temporary memory. */
- dataset = (struct dataset *) alloca (total + n);
+ if (! __libc_use_alloca (alloca_used + total + n))
+ {
+ dataset = malloc (total + n);
+ /* Perhaps we should log a message that we were unable
+ to allocate memory for a large request. */
+ if (dataset == NULL)
+ goto out;
+ dataset_malloced = true;
+ }
+ else
+ dataset = alloca_account (total + n, alloca_used);
/* We cannot add this record to the permanent database. */
- alloca_used = true;
+ dataset_temporary = true;
}
dataset->head.allocsize = total + n;
dataset->head.usable = true;
/* Compute the timeout time. */
- dataset->head.timeout = t + db->postimeout;
+ timeout = dataset->head.timeout = t + db->postimeout;
dataset->resp.version = NSCD_VERSION;
dataset->resp.found = 1;
char *key_copy = cp + key_offset;
assert (key_copy == (char *) rawmemchr (cp, '\0') + 1);
+ assert (cp == dataset->strdata + total - offsetof (struct dataset,
+ strdata));
+
/* Now we can determine whether on refill we have to create a new
record or not. */
if (he != NULL)
allocated on the stack and need not be freed. */
dh->timeout = dataset->head.timeout;
++dh->nreloads;
+
+ /* If the new record was allocated via malloc, then we must free
+ it here. */
+ if (dataset_malloced)
+ free (dataset);
}
else
{
/* We have to create a new record. Just allocate
appropriate memory and copy it. */
struct dataset *newp
- = (struct dataset *) mempool_alloc (db, total + n);
+ = (struct dataset *) mempool_alloc (db, total + n, 1);
if (newp != NULL)
{
/* Adjust pointers into the memory block. */
gr_name = (char *) newp + (gr_name - (char *) dataset);
cp = (char *) newp + (cp - (char *) dataset);
+ key_copy = (char *) newp + (key_copy - (char *) dataset);
dataset = memcpy (newp, dataset, total + n);
- alloca_used = false;
+ dataset_temporary = false;
}
/* Mark the old record as obsolete. */
unnecessarily let the receiver wait. */
assert (fd != -1);
- written = writeall (fd, &dataset->resp, total);
+#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;
}
/* Add the record to the database. But only if it has not been
stored on the stack. */
- if (! alloca_used)
+ if (! dataset_temporary)
{
/* If necessary, we also propagate the data to disk. */
if (db->persistent)
MS_ASYNC);
}
- /* Now get the lock to safely insert the records. */
- pthread_rwlock_rdlock (&db->lock);
-
/* NB: in the following code we always must add the entry
marked with FIRST first. Otherwise we end up with
dangling "pointers" in case a latter hash entry cannot be
added. */
- bool first = req->type == GETGRBYNAME;
+ bool first = true;
/* If the request was by GID, add that entry first. */
- if (req->type != GETGRBYNAME)
+ if (req->type == GETGRBYGID)
{
if (cache_add (GETGRBYGID, cp, key_offset, &dataset->head, true,
- db, owner) < 0)
- {
- /* Could not allocate memory. Make sure the data gets
- discarded. */
- dataset->head.usable = false;
- goto out;
- }
+ db, owner, he == NULL) < 0)
+ goto out;
+
+ first = false;
}
/* If the key is different from the name add a separate entry. */
else if (strcmp (key_copy, gr_name) != 0)
{
if (cache_add (GETGRBYNAME, key_copy, key_len + 1,
- &dataset->head, first, db, owner) < 0)
- {
- /* Could not allocate memory. Make sure the data gets
- discarded. */
- dataset->head.usable = false;
- goto out;
- }
+ &dataset->head, true, db, owner, he == NULL) < 0)
+ goto out;
first = false;
}
/* We have to add the value for both, byname and byuid. */
- if (__builtin_expect (cache_add (GETGRBYNAME, gr_name, gr_name_len,
- &dataset->head, first, db, owner)
- == 0, 1))
+ if ((req->type == GETGRBYNAME || db->propagate)
+ && __builtin_expect (cache_add (GETGRBYNAME, gr_name,
+ gr_name_len,
+ &dataset->head, first, db, owner,
+ he == NULL)
+ == 0, 1))
{
- if (req->type == GETGRBYNAME)
+ if (req->type == GETGRBYNAME && db->propagate)
(void) cache_add (GETGRBYGID, cp, key_offset, &dataset->head,
- req->type != GETGRBYNAME, db, owner);
+ false, db, owner, false);
}
- else if (first)
- /* Could not allocate memory. Make sure the data gets
- discarded. */
- dataset->head.usable = false;
out:
pthread_rwlock_unlock (&db->lock);
}
}
- if (__builtin_expect (written != total, 0) && debug_level > 0)
+ if (__builtin_expect (!all_written, 0) && debug_level > 0)
{
char buf[256];
dbg_log (_("short write in %s: %s"), __FUNCTION__,
strerror_r (errno, buf, sizeof (buf)));
}
+
+ return timeout;
}
}
-static void
+static time_t
addgrbyX (struct database_dyn *db, int fd, request_header *req,
union keytype key, const char *keystr, uid_t uid,
struct hashentry *he, struct datahead *dh)
dbg_log (_("Reloading \"%s\" in group cache!"), keystr);
}
-#if 0
- uid_t oldeuid = 0;
- if (db->secure)
- {
- oldeuid = geteuid ();
- pthread_seteuid_np (uid);
- }
-#endif
-
while (lookup (req->type, key, &resultbuf, buffer, buflen, &grp) != 0
&& (errval = errno) == ERANGE)
{
- char *old_buffer = buffer;
errno = 0;
if (__builtin_expect (buflen > 32768, 0))
{
+ char *old_buffer = buffer;
buflen *= 2;
buffer = (char *) realloc (use_malloc ? buffer : NULL, buflen);
if (buffer == NULL)
buffer = (char *) extend_alloca (buffer, buflen, 2 * buflen);
}
-#if 0
- if (db->secure)
- pthread_seteuid_np (oldeuid);
-#endif
-
- cache_addgr (db, fd, req, keystr, grp, uid, he, dh, errval);
+ time_t timeout = cache_addgr (db, fd, req, keystr, grp, uid, he, dh, errval);
if (use_malloc)
free (buffer);
+
+ return timeout;
}
}
-void
+time_t
readdgrbyname (struct database_dyn *db, struct hashentry *he,
struct datahead *dh)
{
};
union keytype u = { .v = db->data + he->key };
- addgrbyX (db, -1, &req, u, db->data + he->key, he->owner, he, dh);
+ return addgrbyX (db, -1, &req, u, db->data + he->key, he->owner, he, dh);
}
if (*(char *) key == '\0' || *ep != '\0') /* invalid numeric uid */
{
if (debug_level > 0)
- dbg_log (_("Invalid numeric gid \"%s\"!"), (char *) key);
+ dbg_log (_("Invalid numeric gid \"%s\"!"), (char *) key);
errno = EINVAL;
return;
}
-void
+time_t
readdgrbygid (struct database_dyn *db, struct hashentry *he,
struct datahead *dh)
{
};
union keytype u = { .g = gid };
- addgrbyX (db, -1, &req, u, db->data + he->key, he->owner, he, dh);
+ return addgrbyX (db, -1, &req, u, db->data + he->key, he->owner, he, dh);
}