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