Blame resolv/gai_suspend.c

Packit 6c4009
/* Copyright (C) 2001-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@redhat.com>, 2001.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <netdb.h>
Packit 6c4009
#include <pthread.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <sys/time.h>
Packit 6c4009
Packit 6c4009
#include <gai_misc.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
gai_suspend (const struct gaicb *const list[], int ent,
Packit 6c4009
	     const struct timespec *timeout)
Packit 6c4009
{
Packit 6c4009
  struct waitlist waitlist[ent];
Packit 6c4009
  struct requestlist *requestlist[ent];
Packit 6c4009
#ifndef DONT_NEED_GAI_MISC_COND
Packit 6c4009
  pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
Packit 6c4009
#endif
Packit 6c4009
  int cnt;
Packit 6c4009
  unsigned int cntr = 1;
Packit 6c4009
  int none = 1;
Packit 6c4009
  int result;
Packit 6c4009
Packit 6c4009
  /* Request the mutex.  */
Packit 6c4009
  pthread_mutex_lock (&__gai_requests_mutex);
Packit 6c4009
Packit 6c4009
  /* There is not yet a finished request.  Signal the request that
Packit 6c4009
     we are working for it.  */
Packit 6c4009
  for (cnt = 0; cnt < ent; ++cnt)
Packit 6c4009
    if (list[cnt] != NULL && list[cnt]->__return == EAI_INPROGRESS)
Packit 6c4009
      {
Packit 6c4009
	requestlist[cnt] = __gai_find_request (list[cnt]);
Packit 6c4009
Packit 6c4009
	if (requestlist[cnt] != NULL)
Packit 6c4009
	  {
Packit 6c4009
#ifndef DONT_NEED_GAI_MISC_COND
Packit 6c4009
	    waitlist[cnt].cond = &cond;
Packit 6c4009
#endif
Packit 6c4009
	    waitlist[cnt].next = requestlist[cnt]->waiting;
Packit 6c4009
	    waitlist[cnt].counterp = &cntr;
Packit 6c4009
	    waitlist[cnt].sigevp = NULL;
Packit 6c4009
	    waitlist[cnt].caller_pid = 0;	/* Not needed.  */
Packit 6c4009
	    requestlist[cnt]->waiting = &waitlist[cnt];
Packit 6c4009
	    none = 0;
Packit 6c4009
	  }
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
  if (none)
Packit 6c4009
    {
Packit 6c4009
      if (cnt < ent)
Packit 6c4009
	/* There is an entry which is finished.  */
Packit 6c4009
	result = 0;
Packit 6c4009
      else
Packit 6c4009
	result = EAI_ALLDONE;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      /* There is no request done but some are still being worked on.  */
Packit 6c4009
      int oldstate;
Packit 6c4009
Packit 6c4009
      /* Since `pthread_cond_wait'/`pthread_cond_timedwait' are cancelation
Packit 6c4009
	 points we must be careful.  We added entries to the waiting lists
Packit 6c4009
	 which we must remove.  So defer cancelation for now.  */
Packit 6c4009
      pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &oldstate);
Packit 6c4009
Packit 6c4009
#ifdef DONT_NEED_GAI_MISC_COND
Packit 6c4009
      result = 0;
Packit 6c4009
      GAI_MISC_WAIT (result, cntr, timeout, 1);
Packit 6c4009
#else
Packit 6c4009
      if (timeout == NULL)
Packit 6c4009
	result = pthread_cond_wait (&cond, &__gai_requests_mutex);
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  /* We have to convert the relative timeout value into an
Packit 6c4009
	     absolute time value with pthread_cond_timedwait expects.  */
Packit 6c4009
	  struct timeval now;
Packit 6c4009
	  struct timespec abstime;
Packit 6c4009
Packit 6c4009
	  __gettimeofday (&now, NULL);
Packit 6c4009
	  abstime.tv_nsec = timeout->tv_nsec + now.tv_usec * 1000;
Packit 6c4009
	  abstime.tv_sec = timeout->tv_sec + now.tv_sec;
Packit 6c4009
	  if (abstime.tv_nsec >= 1000000000)
Packit 6c4009
	    {
Packit 6c4009
	      abstime.tv_nsec -= 1000000000;
Packit 6c4009
	      abstime.tv_sec += 1;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  result = pthread_cond_timedwait (&cond, &__gai_requests_mutex,
Packit 6c4009
					   &abstime);
Packit 6c4009
	}
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
      /* Now remove the entry in the waiting list for all requests
Packit 6c4009
	 which didn't terminate.  */
Packit 6c4009
      for (cnt = 0; cnt < ent; ++cnt)
Packit 6c4009
	if (list[cnt] != NULL && list[cnt]->__return == EAI_INPROGRESS
Packit 6c4009
	    && requestlist[cnt] != NULL)
Packit 6c4009
	  {
Packit 6c4009
	    struct waitlist **listp = &requestlist[cnt]->waiting;
Packit 6c4009
Packit 6c4009
	    /* There is the chance that we cannot find our entry anymore.
Packit 6c4009
	       This could happen if the request terminated and restarted
Packit 6c4009
	       again.  */
Packit 6c4009
	    while (*listp != NULL && *listp != &waitlist[cnt])
Packit 6c4009
	      listp = &(*listp)->next;
Packit 6c4009
Packit 6c4009
	    if (*listp != NULL)
Packit 6c4009
	      *listp = (*listp)->next;
Packit 6c4009
	  }
Packit 6c4009
Packit 6c4009
      /* Now it's time to restore the cancelation state.  */
Packit 6c4009
      pthread_setcancelstate (oldstate, NULL);
Packit 6c4009
Packit 6c4009
#ifndef DONT_NEED_GAI_MISC_COND
Packit 6c4009
      /* Release the conditional variable.  */
Packit 6c4009
      if (pthread_cond_destroy (&cond) != 0)
Packit 6c4009
	/* This must never happen.  */
Packit 6c4009
	abort ();
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
      if (result != 0)
Packit 6c4009
	{
Packit 6c4009
	  /* An error occurred.  Possibly it's EINTR.  We have to translate
Packit 6c4009
	     the timeout error report of `pthread_cond_timedwait' to the
Packit 6c4009
	     form expected from `gai_suspend'.  */
Packit 6c4009
	  if (__glibc_likely (result == ETIMEDOUT))
Packit 6c4009
	    result = EAI_AGAIN;
Packit 6c4009
	  else if (result == EINTR)
Packit 6c4009
	    result = EAI_INTR;
Packit 6c4009
	  else
Packit 6c4009
	    result = EAI_SYSTEM;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Release the mutex.  */
Packit 6c4009
  pthread_mutex_unlock (&__gai_requests_mutex);
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}