Blame resolv/getaddrinfo_a.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 <unistd.h>
Packit 6c4009
Packit 6c4009
#include <gai_misc.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* We need this special structure to handle asynchronous I/O.  */
Packit 6c4009
struct async_waitlist
Packit 6c4009
  {
Packit 6c4009
    unsigned int counter;
Packit 6c4009
    struct sigevent sigev;
Packit 6c4009
    struct waitlist list[0];
Packit 6c4009
  };
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
getaddrinfo_a (int mode, struct gaicb *list[], int ent, struct sigevent *sig)
Packit 6c4009
{
Packit 6c4009
  struct sigevent defsigev;
Packit 6c4009
  struct requestlist *requests[ent];
Packit 6c4009
  int cnt;
Packit 6c4009
  volatile unsigned int total = 0;
Packit 6c4009
  int result = 0;
Packit 6c4009
Packit 6c4009
  /* Check arguments.  */
Packit 6c4009
  if (mode != GAI_WAIT && mode != GAI_NOWAIT)
Packit 6c4009
    {
Packit 6c4009
      __set_errno (EINVAL);
Packit 6c4009
      return EAI_SYSTEM;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (sig == NULL)
Packit 6c4009
    {
Packit 6c4009
      defsigev.sigev_notify = SIGEV_NONE;
Packit 6c4009
      sig = &defsigev;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Request the mutex.  */
Packit 6c4009
  pthread_mutex_lock (&__gai_requests_mutex);
Packit 6c4009
Packit 6c4009
  /* Now we can enqueue all requests.  Since we already acquired the
Packit 6c4009
     mutex the enqueue function need not do this.  */
Packit 6c4009
  for (cnt = 0; cnt < ent; ++cnt)
Packit 6c4009
    if (list[cnt] != NULL)
Packit 6c4009
      {
Packit 6c4009
	requests[cnt] = __gai_enqueue_request (list[cnt]);
Packit 6c4009
Packit 6c4009
	if (requests[cnt] != NULL)
Packit 6c4009
	  /* Successfully enqueued.  */
Packit 6c4009
	  ++total;
Packit 6c4009
	else
Packit 6c4009
	  /* Signal that we've seen an error.  `errno' and the error code
Packit 6c4009
	     of the gaicb will tell more.  */
Packit 6c4009
	  result = EAI_SYSTEM;
Packit 6c4009
      }
Packit 6c4009
    else
Packit 6c4009
      requests[cnt] = NULL;
Packit 6c4009
Packit 6c4009
  if (total == 0)
Packit 6c4009
    {
Packit 6c4009
      /* We don't have anything to do except signalling if we work
Packit 6c4009
	 asynchronously.  */
Packit 6c4009
Packit 6c4009
      /* Release the mutex.  We do this before raising a signal since the
Packit 6c4009
	 signal handler might do a `siglongjmp' and then the mutex is
Packit 6c4009
	 locked forever.  */
Packit 6c4009
      pthread_mutex_unlock (&__gai_requests_mutex);
Packit 6c4009
Packit 6c4009
      if (mode == GAI_NOWAIT)
Packit 6c4009
	__gai_notify_only (sig,
Packit 6c4009
			   sig->sigev_notify == SIGEV_SIGNAL ? getpid () : 0);
Packit 6c4009
Packit 6c4009
      return result;
Packit 6c4009
    }
Packit 6c4009
  else if (mode == GAI_WAIT)
Packit 6c4009
    {
Packit 6c4009
#ifndef DONT_NEED_GAI_MISC_COND
Packit 6c4009
      pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
Packit 6c4009
#endif
Packit 6c4009
      struct waitlist waitlist[ent];
Packit 6c4009
      int oldstate;
Packit 6c4009
Packit 6c4009
      total = 0;
Packit 6c4009
      for (cnt = 0; cnt < ent; ++cnt)
Packit 6c4009
	if (requests[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 = requests[cnt]->waiting;
Packit 6c4009
	    waitlist[cnt].counterp = &total;
Packit 6c4009
	    waitlist[cnt].sigevp = NULL;
Packit 6c4009
	    waitlist[cnt].caller_pid = 0;	/* Not needed.  */
Packit 6c4009
	    requests[cnt]->waiting = &waitlist[cnt];
Packit 6c4009
	    ++total;
Packit 6c4009
	  }
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
      while (total > 0)
Packit 6c4009
	{
Packit 6c4009
#ifdef DONT_NEED_GAI_MISC_COND
Packit 6c4009
	  int not_used __attribute__ ((unused));
Packit 6c4009
	  GAI_MISC_WAIT (not_used, total, NULL, 1);
Packit 6c4009
#else
Packit 6c4009
	  pthread_cond_wait (&cond, &__gai_requests_mutex);
Packit 6c4009
#endif
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
  else
Packit 6c4009
    {
Packit 6c4009
      struct async_waitlist *waitlist;
Packit 6c4009
Packit 6c4009
      waitlist = (struct async_waitlist *)
Packit 6c4009
	malloc (sizeof (struct async_waitlist)
Packit 6c4009
		+ (ent * sizeof (struct waitlist)));
Packit 6c4009
Packit 6c4009
      if (waitlist == NULL)
Packit 6c4009
	result = EAI_AGAIN;
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  pid_t caller_pid = sig->sigev_notify == SIGEV_SIGNAL ? getpid () : 0;
Packit 6c4009
	  total = 0;
Packit 6c4009
Packit 6c4009
	  for (cnt = 0; cnt < ent; ++cnt)
Packit 6c4009
	    if (requests[cnt] != NULL)
Packit 6c4009
	      {
Packit 6c4009
#ifndef DONT_NEED_GAI_MISC_COND
Packit 6c4009
		waitlist->list[cnt].cond = NULL;
Packit 6c4009
#endif
Packit 6c4009
		waitlist->list[cnt].next = requests[cnt]->waiting;
Packit 6c4009
		waitlist->list[cnt].counterp = &waitlist->counter;
Packit 6c4009
		waitlist->list[cnt].sigevp = &waitlist->sigev;
Packit 6c4009
		waitlist->list[cnt].caller_pid = caller_pid;
Packit 6c4009
		requests[cnt]->waiting = &waitlist->list[cnt];
Packit 6c4009
		++total;
Packit 6c4009
	      }
Packit 6c4009
Packit 6c4009
	  waitlist->counter = total;
Packit 6c4009
	  waitlist->sigev = *sig;
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
}