Blame nptl/tst-cancel4-common.c

Packit 6c4009
/* Common file for all tst-cancel4_*
Packit 6c4009
Packit 6c4009
   Copyright (C) 2016-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
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  if (socketpair (AF_UNIX, SOCK_STREAM, PF_UNIX, fds) != 0)
Packit 6c4009
    {
Packit 6c4009
      perror ("socketpair");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  set_socket_buffer (fds[1]);
Packit 6c4009
Packit 6c4009
  if (mktemp (fifoname) == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("%s: cannot generate temp file name: %m\n", __func__);
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  int result = 0;
Packit 6c4009
  size_t cnt;
Packit 6c4009
  for (cnt = 0; cnt < ntest_tf; ++cnt)
Packit 6c4009
    {
Packit 6c4009
      if (tests[cnt].only_early)
Packit 6c4009
	continue;
Packit 6c4009
Packit 6c4009
      if (pthread_barrier_init (&b2, NULL, tests[cnt].nb) != 0)
Packit 6c4009
	{
Packit 6c4009
	  puts ("b2 init failed");
Packit 6c4009
	  exit (1);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      /* Reset the counter for the cleanup handler.  */
Packit 6c4009
      cl_called = 0;
Packit 6c4009
Packit 6c4009
      pthread_t th;
Packit 6c4009
      if (pthread_create (&th, NULL, tests[cnt].tf, NULL) != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("create for '%s' test failed\n", tests[cnt].name);
Packit 6c4009
	  result = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      int r = pthread_barrier_wait (&b2;;
Packit 6c4009
      if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
Packit 6c4009
	{
Packit 6c4009
	  printf ("%s: barrier_wait failed\n", __func__);
Packit 6c4009
	  result = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      struct timespec  ts = { .tv_sec = 0, .tv_nsec = 100000000 };
Packit 6c4009
      while (nanosleep (&ts, &ts) != 0)
Packit 6c4009
	continue;
Packit 6c4009
Packit 6c4009
      if (pthread_cancel (th) != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("cancel for '%s' failed\n", tests[cnt].name);
Packit 6c4009
	  result = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      void *status;
Packit 6c4009
      if (pthread_join (th, &status) != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("join for '%s' failed\n", tests[cnt].name);
Packit 6c4009
	  result = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
      if (status != PTHREAD_CANCELED)
Packit 6c4009
	{
Packit 6c4009
	  printf ("thread for '%s' not canceled\n", tests[cnt].name);
Packit 6c4009
	  result = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (pthread_barrier_destroy (&b2) != 0)
Packit 6c4009
	{
Packit 6c4009
	  puts ("barrier_destroy failed");
Packit 6c4009
	  result = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (cl_called == 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("cleanup handler not called for '%s'\n", tests[cnt].name);
Packit 6c4009
	  result = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
      if (cl_called > 1)
Packit 6c4009
	{
Packit 6c4009
	  printf ("cleanup handler called more than once for '%s'\n",
Packit 6c4009
		  tests[cnt].name);
Packit 6c4009
	  result = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      printf ("in-time cancel test of '%s' successful\n", tests[cnt].name);
Packit 6c4009
Packit 6c4009
      if (tempfd != -1)
Packit 6c4009
	{
Packit 6c4009
	  close (tempfd);
Packit 6c4009
	  tempfd = -1;
Packit 6c4009
	}
Packit 6c4009
      if (tempfd2 != -1)
Packit 6c4009
	{
Packit 6c4009
	  close (tempfd2);
Packit 6c4009
	  tempfd2 = -1;
Packit 6c4009
	}
Packit 6c4009
      if (tempfname != NULL)
Packit 6c4009
	{
Packit 6c4009
	  unlink (tempfname);
Packit 6c4009
	  free (tempfname);
Packit 6c4009
	  tempfname = NULL;
Packit 6c4009
	}
Packit 6c4009
      if (tempmsg != -1)
Packit 6c4009
	{
Packit 6c4009
	  msgctl (tempmsg, IPC_RMID, NULL);
Packit 6c4009
	  tempmsg = -1;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  for (cnt = 0; cnt < ntest_tf; ++cnt)
Packit 6c4009
    {
Packit 6c4009
      if (pthread_barrier_init (&b2, NULL, tests[cnt].nb) != 0)
Packit 6c4009
	{
Packit 6c4009
	  puts ("b2 init failed");
Packit 6c4009
	  exit (1);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      /* Reset the counter for the cleanup handler.  */
Packit 6c4009
      cl_called = 0;
Packit 6c4009
Packit 6c4009
      pthread_t th;
Packit 6c4009
      if (pthread_create (&th, NULL, tests[cnt].tf, (void *) 1l) != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("create for '%s' test failed\n", tests[cnt].name);
Packit 6c4009
	  result = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      int r = pthread_barrier_wait (&b2;;
Packit 6c4009
      if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
Packit 6c4009
	{
Packit 6c4009
	  printf ("%s: barrier_wait failed\n", __func__);
Packit 6c4009
	  result = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (pthread_cancel (th) != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("cancel for '%s' failed\n", tests[cnt].name);
Packit 6c4009
	  result = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      r = pthread_barrier_wait (&b2;;
Packit 6c4009
      if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
Packit 6c4009
	{
Packit 6c4009
	  printf ("%s: barrier_wait failed\n", __func__);
Packit 6c4009
	  result = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      void *status;
Packit 6c4009
      if (pthread_join (th, &status) != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("join for '%s' failed\n", tests[cnt].name);
Packit 6c4009
	  result = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
      if (status != PTHREAD_CANCELED)
Packit 6c4009
	{
Packit 6c4009
	  printf ("thread for '%s' not canceled\n", tests[cnt].name);
Packit 6c4009
	  result = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (pthread_barrier_destroy (&b2) != 0)
Packit 6c4009
	{
Packit 6c4009
	  puts ("barrier_destroy failed");
Packit 6c4009
	  result = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (cl_called == 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("cleanup handler not called for '%s'\n", tests[cnt].name);
Packit 6c4009
	  result = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
      if (cl_called > 1)
Packit 6c4009
	{
Packit 6c4009
	  printf ("cleanup handler called more than once for '%s'\n",
Packit 6c4009
		  tests[cnt].name);
Packit 6c4009
	  result = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      printf ("early cancel test of '%s' successful\n", tests[cnt].name);
Packit 6c4009
Packit 6c4009
      if (tempfd != -1)
Packit 6c4009
	{
Packit 6c4009
	  close (tempfd);
Packit 6c4009
	  tempfd = -1;
Packit 6c4009
	}
Packit 6c4009
      if (tempfd2 != -1)
Packit 6c4009
	{
Packit 6c4009
	  close (tempfd2);
Packit 6c4009
	  tempfd2 = -1;
Packit 6c4009
	}
Packit 6c4009
      if (tempfname != NULL)
Packit 6c4009
	{
Packit 6c4009
	  unlink (tempfname);
Packit 6c4009
	  free (tempfname);
Packit 6c4009
	  tempfname = NULL;
Packit 6c4009
	}
Packit 6c4009
      if (tempmsg != -1)
Packit 6c4009
	{
Packit 6c4009
	  msgctl (tempmsg, IPC_RMID, NULL);
Packit 6c4009
	  tempmsg = -1;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TIMEOUT 60
Packit 6c4009
#include <support/test-driver.c>