* nscd/nscd_helper.c (wait_on_socket): New function.

(get_mapping): Use wait_on_socket instead of poll.  The former handles
	EINTR of poll correctly.
	(__nscd_open_socket): Likewise.
	(get_mapping): Make sure BUF is aligned correctly.
	(get_mapping): Use munmap on correct pointer.
This commit is contained in:
Ulrich Drepper 2005-07-13 22:47:24 +00:00
parent a9564ae94b
commit 7529e67ed8
2 changed files with 58 additions and 23 deletions

View File

@ -1,3 +1,12 @@
2005-07-13 Ulrich Drepper <drepper@redhat.com>
* nscd/nscd_helper.c (wait_on_socket): New function.
(get_mapping): Use wait_on_socket instead of poll. The former handles
EINTR of poll correctly.
(__nscd_open_socket): Likewise.
(get_mapping): Make sure BUF is aligned correctly.
(get_mapping): Use munmap on correct pointer.
2005-07-12 Ulrich Drepper <drepper@redhat.com> 2005-07-12 Ulrich Drepper <drepper@redhat.com>
* include/libc-symbols.h: Define macros for librt hidden symbols. * include/libc-symbols.h: Define macros for librt hidden symbols.

View File

@ -26,6 +26,7 @@
#include <sys/poll.h> #include <sys/poll.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/time.h>
#include <sys/uio.h> #include <sys/uio.h>
#include <sys/un.h> #include <sys/un.h>
#include <not-cancel.h> #include <not-cancel.h>
@ -135,6 +136,36 @@ __nscd_unmap (struct mapped_database *mapped)
} }
static int
wait_on_socket (int sock)
{
struct pollfd fds[1];
fds[0].fd = sock;
fds[0].events = POLLIN | POLLERR | POLLHUP;
int n = __poll (fds, 1, 5 * 1000);
if (n == -1 && __builtin_expect (errno == EINTR, 0))
{
/* Handle the case where the poll() call is interrupted by a
signal. We cannot just use TEMP_FAILURE_RETRY since it might
lead to infinite loops. */
struct timeval now;
(void) __gettimeofday (&now, NULL);
long int end = (now.tv_sec + 5) * 1000 + (now.tv_usec + 500) / 1000;
while (1)
{
long int timeout = end - (now.tv_sec * 1000
+ (now.tv_usec + 500) / 1000);
n = __poll (fds, 1, timeout);
if (n != -1 || errno != EINTR)
break;
(void) __gettimeofday (&now, NULL);
}
}
return n;
}
/* Try to get a file descriptor for the shared meory segment /* Try to get a file descriptor for the shared meory segment
containing the database. */ containing the database. */
static struct mapped_database * static struct mapped_database *
@ -176,24 +207,27 @@ get_mapping (request_type type, const char *key,
iov[0].iov_base = resdata; iov[0].iov_base = resdata;
iov[0].iov_len = keylen; iov[0].iov_len = keylen;
char buf[CMSG_SPACE (sizeof (int))]; union
{
struct cmsghdr hdr;
char bytes[CMSG_SPACE (sizeof (int))];
} buf;
struct msghdr msg = { .msg_iov = iov, .msg_iovlen = 1, struct msghdr msg = { .msg_iov = iov, .msg_iovlen = 1,
.msg_control = buf, .msg_controllen = sizeof (buf) }; .msg_control = buf.bytes,
.msg_controllen = sizeof (buf) };
struct cmsghdr *cmsg = CMSG_FIRSTHDR (&msg); struct cmsghdr *cmsg = CMSG_FIRSTHDR (&msg);
cmsg->cmsg_level = SOL_SOCKET; cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS; cmsg->cmsg_type = SCM_RIGHTS;
cmsg->cmsg_len = CMSG_LEN (sizeof (int)); cmsg->cmsg_len = CMSG_LEN (sizeof (int));
/* This access is well-aligned since BUF is correctly aligned for an
int and CMSG_DATA preserves this alignment. */
*(int *) CMSG_DATA (cmsg) = -1; *(int *) CMSG_DATA (cmsg) = -1;
msg.msg_controllen = cmsg->cmsg_len; msg.msg_controllen = cmsg->cmsg_len;
struct pollfd fds[1]; if (wait_on_socket (sock) <= 0)
fds[0].fd = sock;
fds[0].events = POLLIN | POLLERR | POLLHUP;
if (__poll (fds, 1, 5 * 1000) <= 0)
/* Failure or timeout. */
goto out_close2; goto out_close2;
#ifndef MSG_NOSIGNAL #ifndef MSG_NOSIGNAL
@ -236,13 +270,11 @@ get_mapping (request_type type, const char *key,
if (mapping != MAP_FAILED) if (mapping != MAP_FAILED)
{ {
/* Allocate a record for the mapping. */ /* Allocate a record for the mapping. */
struct mapped_database *newp; struct mapped_database *newp = malloc (sizeof (*newp));
newp = malloc (sizeof (*newp));
if (newp == NULL) if (newp == NULL)
{ {
/* Ugh, after all we went through the memory allocation failed. */ /* Ugh, after all we went through the memory allocation failed. */
__munmap (result, size); __munmap (mapping, size);
goto out_close; goto out_close;
} }
@ -372,20 +404,14 @@ __nscd_open_socket (const char *key, size_t keylen, request_type type,
vec[1].iov_len = keylen; vec[1].iov_len = keylen;
ssize_t nbytes = TEMP_FAILURE_RETRY (__writev (sock, vec, 2)); ssize_t nbytes = TEMP_FAILURE_RETRY (__writev (sock, vec, 2));
if (nbytes == (ssize_t) (sizeof (request_header) + keylen)) if (nbytes == (ssize_t) (sizeof (request_header) + keylen)
{
/* Wait for data. */ /* Wait for data. */
struct pollfd fds[1]; && wait_on_socket (sock) > 0)
fds[0].fd = sock;
fds[0].events = POLLIN | POLLERR | POLLHUP;
if (__poll (fds, 1, 5 * 1000) > 0)
{ {
nbytes = TEMP_FAILURE_RETRY (__read (sock, response, nbytes = TEMP_FAILURE_RETRY (__read (sock, response, responselen));
responselen));
if (nbytes == (ssize_t) responselen) if (nbytes == (ssize_t) responselen)
return sock; return sock;
} }
}
close_not_cancel_no_status (sock); close_not_cancel_no_status (sock);
} }