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

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