Blame resolv/getaddrinfo_a.c

Packit Service 82fcde
/* Copyright (C) 2001-2018 Free Software Foundation, Inc.
Packit Service 82fcde
   This file is part of the GNU C Library.
Packit Service 82fcde
   Contributed by Ulrich Drepper <drepper@redhat.com>, 2001.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 82fcde
   modify it under the terms of the GNU Lesser General Public
Packit Service 82fcde
   License as published by the Free Software Foundation; either
Packit Service 82fcde
   version 2.1 of the License, or (at your option) any later version.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 82fcde
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 82fcde
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 82fcde
   Lesser General Public License for more details.
Packit Service 82fcde
Packit Service 82fcde
   You should have received a copy of the GNU Lesser General Public
Packit Service 82fcde
   License along with the GNU C Library; if not, see
Packit Service 82fcde
   <http://www.gnu.org/licenses/>.  */
Packit Service 82fcde
Packit Service 82fcde
#include <errno.h>
Packit Service 82fcde
#include <netdb.h>
Packit Service 82fcde
#include <pthread.h>
Packit Service 82fcde
#include <stdlib.h>
Packit Service 82fcde
#include <unistd.h>
Packit Service 82fcde
Packit Service 82fcde
#include <gai_misc.h>
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
/* We need this special structure to handle asynchronous I/O.  */
Packit Service 82fcde
struct async_waitlist
Packit Service 82fcde
  {
Packit Service 82fcde
    unsigned int counter;
Packit Service 82fcde
    struct sigevent sigev;
Packit Service 82fcde
    struct waitlist list[0];
Packit Service 82fcde
  };
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
int
Packit Service 82fcde
getaddrinfo_a (int mode, struct gaicb *list[], int ent, struct sigevent *sig)
Packit Service 82fcde
{
Packit Service 82fcde
  struct sigevent defsigev;
Packit Service 82fcde
  struct requestlist *requests[ent];
Packit Service 82fcde
  int cnt;
Packit Service 82fcde
  volatile unsigned int total = 0;
Packit Service 82fcde
  int result = 0;
Packit Service 82fcde
Packit Service 82fcde
  /* Check arguments.  */
Packit Service 82fcde
  if (mode != GAI_WAIT && mode != GAI_NOWAIT)
Packit Service 82fcde
    {
Packit Service 82fcde
      __set_errno (EINVAL);
Packit Service 82fcde
      return EAI_SYSTEM;
Packit Service 82fcde
    }
Packit Service 82fcde
Packit Service 82fcde
  if (sig == NULL)
Packit Service 82fcde
    {
Packit Service 82fcde
      defsigev.sigev_notify = SIGEV_NONE;
Packit Service 82fcde
      sig = &defsigev;
Packit Service 82fcde
    }
Packit Service 82fcde
Packit Service 82fcde
  /* Request the mutex.  */
Packit Service 82fcde
  pthread_mutex_lock (&__gai_requests_mutex);
Packit Service 82fcde
Packit Service 82fcde
  /* Now we can enqueue all requests.  Since we already acquired the
Packit Service 82fcde
     mutex the enqueue function need not do this.  */
Packit Service 82fcde
  for (cnt = 0; cnt < ent; ++cnt)
Packit Service 82fcde
    if (list[cnt] != NULL)
Packit Service 82fcde
      {
Packit Service 82fcde
	requests[cnt] = __gai_enqueue_request (list[cnt]);
Packit Service 82fcde
Packit Service 82fcde
	if (requests[cnt] != NULL)
Packit Service 82fcde
	  /* Successfully enqueued.  */
Packit Service 82fcde
	  ++total;
Packit Service 82fcde
	else
Packit Service 82fcde
	  /* Signal that we've seen an error.  `errno' and the error code
Packit Service 82fcde
	     of the gaicb will tell more.  */
Packit Service 82fcde
	  result = EAI_SYSTEM;
Packit Service 82fcde
      }
Packit Service 82fcde
    else
Packit Service 82fcde
      requests[cnt] = NULL;
Packit Service 82fcde
Packit Service 82fcde
  if (total == 0)
Packit Service 82fcde
    {
Packit Service 82fcde
      /* We don't have anything to do except signalling if we work
Packit Service 82fcde
	 asynchronously.  */
Packit Service 82fcde
Packit Service 82fcde
      /* Release the mutex.  We do this before raising a signal since the
Packit Service 82fcde
	 signal handler might do a `siglongjmp' and then the mutex is
Packit Service 82fcde
	 locked forever.  */
Packit Service 82fcde
      pthread_mutex_unlock (&__gai_requests_mutex);
Packit Service 82fcde
Packit Service 82fcde
      if (mode == GAI_NOWAIT)
Packit Service 82fcde
	__gai_notify_only (sig,
Packit Service 82fcde
			   sig->sigev_notify == SIGEV_SIGNAL ? getpid () : 0);
Packit Service 82fcde
Packit Service 82fcde
      return result;
Packit Service 82fcde
    }
Packit Service 82fcde
  else if (mode == GAI_WAIT)
Packit Service 82fcde
    {
Packit Service 82fcde
#ifndef DONT_NEED_GAI_MISC_COND
Packit Service 82fcde
      pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
Packit Service 82fcde
#endif
Packit Service 82fcde
      struct waitlist waitlist[ent];
Packit Service 82fcde
      int oldstate;
Packit Service 82fcde
Packit Service 82fcde
      total = 0;
Packit Service 82fcde
      for (cnt = 0; cnt < ent; ++cnt)
Packit Service 82fcde
	if (requests[cnt] != NULL)
Packit Service 82fcde
	  {
Packit Service 82fcde
#ifndef DONT_NEED_GAI_MISC_COND
Packit Service 82fcde
	    waitlist[cnt].cond = &cond;
Packit Service 82fcde
#endif
Packit Service 82fcde
	    waitlist[cnt].next = requests[cnt]->waiting;
Packit Service 82fcde
	    waitlist[cnt].counterp = &total;
Packit Service 82fcde
	    waitlist[cnt].sigevp = NULL;
Packit Service 82fcde
	    waitlist[cnt].caller_pid = 0;	/* Not needed.  */
Packit Service 82fcde
	    requests[cnt]->waiting = &waitlist[cnt];
Packit Service 82fcde
	    ++total;
Packit Service 82fcde
	  }
Packit Service 82fcde
Packit Service 82fcde
      /* Since `pthread_cond_wait'/`pthread_cond_timedwait' are cancelation
Packit Service 82fcde
	 points we must be careful.  We added entries to the waiting lists
Packit Service 82fcde
	 which we must remove.  So defer cancelation for now.  */
Packit Service 82fcde
      pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &oldstate);
Packit Service 82fcde
Packit Service 82fcde
      while (total > 0)
Packit Service 82fcde
	{
Packit Service 82fcde
#ifdef DONT_NEED_GAI_MISC_COND
Packit Service 82fcde
	  int not_used __attribute__ ((unused));
Packit Service 82fcde
	  GAI_MISC_WAIT (not_used, total, NULL, 1);
Packit Service 82fcde
#else
Packit Service 82fcde
	  pthread_cond_wait (&cond, &__gai_requests_mutex);
Packit Service 82fcde
#endif
Packit Service 82fcde
	}
Packit Service 82fcde
Packit Service 82fcde
      /* Now it's time to restore the cancelation state.  */
Packit Service 82fcde
      pthread_setcancelstate (oldstate, NULL);
Packit Service 82fcde
Packit Service 82fcde
#ifndef DONT_NEED_GAI_MISC_COND
Packit Service 82fcde
      /* Release the conditional variable.  */
Packit Service 82fcde
      if (pthread_cond_destroy (&cond) != 0)
Packit Service 82fcde
	/* This must never happen.  */
Packit Service 82fcde
	abort ();
Packit Service 82fcde
#endif
Packit Service 82fcde
    }
Packit Service 82fcde
  else
Packit Service 82fcde
    {
Packit Service 82fcde
      struct async_waitlist *waitlist;
Packit Service 82fcde
Packit Service 82fcde
      waitlist = (struct async_waitlist *)
Packit Service 82fcde
	malloc (sizeof (struct async_waitlist)
Packit Service 82fcde
		+ (ent * sizeof (struct waitlist)));
Packit Service 82fcde
Packit Service 82fcde
      if (waitlist == NULL)
Packit Service 82fcde
	result = EAI_AGAIN;
Packit Service 82fcde
      else
Packit Service 82fcde
	{
Packit Service 82fcde
	  pid_t caller_pid = sig->sigev_notify == SIGEV_SIGNAL ? getpid () : 0;
Packit Service 82fcde
	  total = 0;
Packit Service 82fcde
Packit Service 82fcde
	  for (cnt = 0; cnt < ent; ++cnt)
Packit Service 82fcde
	    if (requests[cnt] != NULL)
Packit Service 82fcde
	      {
Packit Service 82fcde
#ifndef DONT_NEED_GAI_MISC_COND
Packit Service 82fcde
		waitlist->list[cnt].cond = NULL;
Packit Service 82fcde
#endif
Packit Service 82fcde
		waitlist->list[cnt].next = requests[cnt]->waiting;
Packit Service 82fcde
		waitlist->list[cnt].counterp = &waitlist->counter;
Packit Service 82fcde
		waitlist->list[cnt].sigevp = &waitlist->sigev;
Packit Service 82fcde
		waitlist->list[cnt].caller_pid = caller_pid;
Packit Service 82fcde
		requests[cnt]->waiting = &waitlist->list[cnt];
Packit Service 82fcde
		++total;
Packit Service 82fcde
	      }
Packit Service 82fcde
Packit Service 82fcde
	  waitlist->counter = total;
Packit Service 82fcde
	  waitlist->sigev = *sig;
Packit Service 82fcde
	}
Packit Service 82fcde
    }
Packit Service 82fcde
Packit Service 82fcde
  /* Release the mutex.  */
Packit Service 82fcde
  pthread_mutex_unlock (&__gai_requests_mutex);
Packit Service 82fcde
Packit Service 82fcde
  return result;
Packit Service 82fcde
}