Blame resolv/tst-resolv-res_init-multi.c

Packit Service 82fcde
/* Multi-threaded test for resolver initialization.
Packit Service 82fcde
   Copyright (C) 2017-2018 Free Software Foundation, Inc.
Packit Service 82fcde
   This file is part of the GNU C Library.
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 <resolv.h>
Packit Service 82fcde
#include <stdlib.h>
Packit Service 82fcde
#include <support/check.h>
Packit Service 82fcde
#include <support/support.h>
Packit Service 82fcde
#include <support/xthread.h>
Packit Service 82fcde
Packit Service 82fcde
/* Whether name lookups succeed does not really matter.  We use this
Packit Service 82fcde
   to trigger initialization of the resolver.  */
Packit Service 82fcde
static const char *test_hostname = "www.gnu.org";
Packit Service 82fcde
Packit Service 82fcde
/* The different initialization methods.  */
Packit Service 82fcde
enum test_type { init, byname, gai };
Packit Service 82fcde
enum { type_count = 3 };
Packit Service 82fcde
Packit Service 82fcde
/* Thread function.  Perform a few resolver options.  */
Packit Service 82fcde
static void *
Packit Service 82fcde
thread_func (void *closure)
Packit Service 82fcde
{
Packit Service 82fcde
  enum test_type *ptype = closure;
Packit Service 82fcde
  /* Perform a few calls to the requested operation.  */
Packit Service 82fcde
  TEST_VERIFY (*ptype >= 0);
Packit Service 82fcde
  TEST_VERIFY (*ptype < (int) type_count);
Packit Service 82fcde
  for (int i = 0; i < 3; ++i)
Packit Service 82fcde
    switch (*ptype)
Packit Service 82fcde
      {
Packit Service 82fcde
      case init:
Packit Service 82fcde
	res_init ();
Packit Service 82fcde
	break;
Packit Service 82fcde
      case byname:
Packit Service 82fcde
	gethostbyname (test_hostname);
Packit Service 82fcde
	break;
Packit Service 82fcde
      case gai:
Packit Service 82fcde
	{
Packit Service 82fcde
	  struct addrinfo hints = { 0, };
Packit Service 82fcde
	  struct addrinfo *ai = NULL;
Packit Service 82fcde
	  if (getaddrinfo (test_hostname, "80", &hints, &ai) == 0)
Packit Service 82fcde
	    freeaddrinfo (ai);
Packit Service 82fcde
	}
Packit Service 82fcde
	break;
Packit Service 82fcde
      }
Packit Service 82fcde
  free (ptype);
Packit Service 82fcde
  return NULL;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
static int
Packit Service 82fcde
do_test (void)
Packit Service 82fcde
{
Packit Service 82fcde
  /* Start a small number of threads which perform resolver
Packit Service 82fcde
     operations.  */
Packit Service 82fcde
  enum { thread_count = 30 };
Packit Service 82fcde
Packit Service 82fcde
  pthread_t threads[thread_count];
Packit Service 82fcde
  for (int i = 0; i < thread_count; ++i)
Packit Service 82fcde
    {
Packit Service 82fcde
      enum test_type *ptype = xmalloc (sizeof (*ptype));
Packit Service 82fcde
      *ptype = i % type_count;
Packit Service 82fcde
      threads[i] = xpthread_create (NULL, thread_func, ptype);
Packit Service 82fcde
    }
Packit Service 82fcde
  for (int i = 0; i < type_count; ++i)
Packit Service 82fcde
    {
Packit Service 82fcde
      enum test_type *ptype = xmalloc (sizeof (*ptype));
Packit Service 82fcde
      *ptype = i;
Packit Service 82fcde
      thread_func (ptype);
Packit Service 82fcde
    }
Packit Service 82fcde
  for (int i = 0; i < thread_count; ++i)
Packit Service 82fcde
    xpthread_join (threads[i]);
Packit Service 82fcde
  return 0;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
#include <support/test-driver.c>