Blame resolv/gai_notify.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 <netdb.h>
Packit Service 82fcde
#include <pthread.h>
Packit Service 82fcde
#include <stdlib.h>
Packit Service 82fcde
#include <gai_misc.h>
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
struct notify_func
Packit Service 82fcde
  {
Packit Service 82fcde
    void (*func) (sigval_t);
Packit Service 82fcde
    sigval_t value;
Packit Service 82fcde
  };
Packit Service 82fcde
Packit Service 82fcde
static void *
Packit Service 82fcde
notify_func_wrapper (void *arg)
Packit Service 82fcde
{
Packit Service 82fcde
  gai_start_notify_thread ();
Packit Service 82fcde
  struct notify_func *const n = arg;
Packit Service 82fcde
  void (*func) (sigval_t) = n->func;
Packit Service 82fcde
  sigval_t value = n->value;
Packit Service 82fcde
  free (n);
Packit Service 82fcde
  (*func) (value);
Packit Service 82fcde
  return NULL;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
int
Packit Service 82fcde
__gai_notify_only (struct sigevent *sigev, pid_t caller_pid)
Packit Service 82fcde
{
Packit Service 82fcde
  int result = 0;
Packit Service 82fcde
Packit Service 82fcde
  /* Send the signal to notify about finished processing of the request.  */
Packit Service 82fcde
  if (sigev->sigev_notify == SIGEV_THREAD)
Packit Service 82fcde
    {
Packit Service 82fcde
      /* We have to start a thread.  */
Packit Service 82fcde
      pthread_t tid;
Packit Service 82fcde
      pthread_attr_t attr, *pattr;
Packit Service 82fcde
Packit Service 82fcde
      pattr = (pthread_attr_t *) sigev->sigev_notify_attributes;
Packit Service 82fcde
      if (pattr == NULL)
Packit Service 82fcde
	{
Packit Service 82fcde
	  pthread_attr_init (&attr);
Packit Service 82fcde
	  pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
Packit Service 82fcde
	  pattr = &att;;
Packit Service 82fcde
	}
Packit Service 82fcde
Packit Service 82fcde
      /* SIGEV may be freed as soon as we return, so we cannot let the
Packit Service 82fcde
	 notification thread use that pointer.  Even though a sigval_t is
Packit Service 82fcde
	 only one word and the same size as a void *, we cannot just pass
Packit Service 82fcde
	 the value through pthread_create as the argument and have the new
Packit Service 82fcde
	 thread run the user's function directly, because on some machines
Packit Service 82fcde
	 the calling convention for a union like sigval_t is different from
Packit Service 82fcde
	 that for a pointer type like void *.  */
Packit Service 82fcde
      struct notify_func *nf = malloc (sizeof *nf);
Packit Service 82fcde
      if (nf == NULL)
Packit Service 82fcde
	result = -1;
Packit Service 82fcde
      else
Packit Service 82fcde
	{
Packit Service 82fcde
	  nf->func = sigev->sigev_notify_function;
Packit Service 82fcde
	  nf->value = sigev->sigev_value;
Packit Service 82fcde
	  if (pthread_create (&tid, pattr, notify_func_wrapper, nf) < 0)
Packit Service 82fcde
	    {
Packit Service 82fcde
	      free (nf);
Packit Service 82fcde
	      result = -1;
Packit Service 82fcde
	    }
Packit Service 82fcde
	}
Packit Service 82fcde
    }
Packit Service 82fcde
  else if (sigev->sigev_notify == SIGEV_SIGNAL)
Packit Service 82fcde
    /* We have to send a signal.  */
Packit Service 82fcde
    if (__gai_sigqueue (sigev->sigev_signo, sigev->sigev_value, caller_pid)
Packit Service 82fcde
	< 0)
Packit Service 82fcde
      result = -1;
Packit Service 82fcde
Packit Service 82fcde
  return result;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
void
Packit Service 82fcde
__gai_notify (struct requestlist *req)
Packit Service 82fcde
{
Packit Service 82fcde
  struct waitlist *waitlist;
Packit Service 82fcde
Packit Service 82fcde
  /* Now also notify possibly waiting threads.  */
Packit Service 82fcde
  waitlist = req->waiting;
Packit Service 82fcde
  while (waitlist != NULL)
Packit Service 82fcde
    {
Packit Service 82fcde
      struct waitlist *next = waitlist->next;
Packit Service 82fcde
Packit Service 82fcde
      if (waitlist->sigevp == NULL)
Packit Service 82fcde
	{
Packit Service 82fcde
#ifdef DONT_NEED_GAI_MISC_COND
Packit Service 82fcde
	  GAI_MISC_NOTIFY (waitlist);
Packit Service 82fcde
#else
Packit Service 82fcde
	  /* Decrement the counter.  */
Packit Service 82fcde
	  --*waitlist->counterp;
Packit Service 82fcde
Packit Service 82fcde
	  pthread_cond_signal (waitlist->cond);
Packit Service 82fcde
#endif
Packit Service 82fcde
	}
Packit Service 82fcde
      else
Packit Service 82fcde
	/* This is part of an asynchronous `getaddrinfo_a' operation.  If
Packit Service 82fcde
	   this request is the last one, send the signal.  */
Packit Service 82fcde
	if (--*waitlist->counterp == 0)
Packit Service 82fcde
	  {
Packit Service 82fcde
	    __gai_notify_only (waitlist->sigevp, waitlist->caller_pid);
Packit Service 82fcde
	    /* This is tricky.  See getaddrinfo_a.c for the reason why
Packit Service 82fcde
	       this works.  */
Packit Service 82fcde
	    free ((void *) waitlist->counterp);
Packit Service 82fcde
	  }
Packit Service 82fcde
Packit Service 82fcde
      waitlist = next;
Packit Service 82fcde
    }
Packit Service 82fcde
}