Blame rt/tst-mqueue8.c

Packit 6c4009
/* Copyright (C) 2004-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
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 <errno.h>
Packit 6c4009
#include <mqueue.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#if _POSIX_THREADS
Packit 6c4009
# include <pthread.h>
Packit 6c4009
Packit 6c4009
static pthread_barrier_t b;
Packit 6c4009
Packit 6c4009
/* Cleanup handling test.  */
Packit 6c4009
static int cl_called;
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
cl (void *arg)
Packit 6c4009
{
Packit 6c4009
  ++cl_called;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TF_MQ_RECEIVE		0L
Packit 6c4009
#define TF_MQ_TIMEDRECEIVE	1L
Packit 6c4009
#define TF_MQ_SEND		2L
Packit 6c4009
#define TF_MQ_TIMEDSEND		3L
Packit 6c4009
Packit 6c4009
static const char *names[]
Packit 6c4009
  = { "mq_receive", "mq_timedreceive", "mq_send", "mq_timedsend" };
Packit 6c4009
Packit 6c4009
static mqd_t q;
Packit 6c4009
static struct timespec never;
Packit 6c4009
Packit 6c4009
static void *
Packit 6c4009
tf (void *arg)
Packit 6c4009
{
Packit 6c4009
  int r = pthread_barrier_wait (&b);
Packit 6c4009
  if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
Packit 6c4009
    {
Packit 6c4009
      puts ("tf: barrier_wait failed");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  pthread_cleanup_push (cl, NULL);
Packit 6c4009
Packit 6c4009
  char c = ' ';
Packit 6c4009
Packit 6c4009
  switch ((long) arg)
Packit 6c4009
    {
Packit 6c4009
    case TF_MQ_SEND:
Packit 6c4009
      TEMP_FAILURE_RETRY (mq_send (q, &c, 1, 1));
Packit 6c4009
      break;
Packit 6c4009
    case TF_MQ_TIMEDSEND:
Packit 6c4009
      TEMP_FAILURE_RETRY (mq_timedsend (q, &c, 1, 1, &never));
Packit 6c4009
      break;
Packit 6c4009
    case TF_MQ_RECEIVE:
Packit 6c4009
      TEMP_FAILURE_RETRY (mq_receive (q, &c, 1, NULL));
Packit 6c4009
      break;
Packit 6c4009
    case TF_MQ_TIMEDRECEIVE:
Packit 6c4009
      TEMP_FAILURE_RETRY (mq_timedreceive (q, &c, 1, NULL, &never));
Packit 6c4009
      break;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  pthread_cleanup_pop (0);
Packit 6c4009
Packit 6c4009
  printf ("tf: %s returned\n", names[(long) arg]);
Packit 6c4009
Packit 6c4009
  exit (1);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  char name[sizeof "/tst-mqueue8-" + sizeof (pid_t) * 3];
Packit 6c4009
  snprintf (name, sizeof (name), "/tst-mqueue8-%u", getpid ());
Packit 6c4009
Packit 6c4009
  struct mq_attr attr = { .mq_maxmsg = 1, .mq_msgsize = 1 };
Packit 6c4009
  q = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
Packit 6c4009
Packit 6c4009
  if (q == (mqd_t) -1)
Packit 6c4009
    {
Packit 6c4009
      printf ("mq_open failed with: %m\n");
Packit 6c4009
      return 0;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (mq_unlink (name) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("mq_unlink failed with: %m\n");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (pthread_barrier_init (&b, NULL, 2) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("barrier_init failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (clock_gettime (CLOCK_REALTIME, &never) == 0)
Packit 6c4009
    never.tv_sec += 100;
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      never.tv_sec = time (NULL) + 100;
Packit 6c4009
      never.tv_nsec = 0;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  int result = 0;
Packit 6c4009
  for (long l = TF_MQ_RECEIVE; l <= TF_MQ_TIMEDSEND; ++l)
Packit 6c4009
    {
Packit 6c4009
      cl_called = 0;
Packit 6c4009
Packit 6c4009
      pthread_t th;
Packit 6c4009
      if (pthread_create (&th, NULL, tf, (void *) l) != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("1st %s create failed\n", names[l]);
Packit 6c4009
	  result = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      int r = pthread_barrier_wait (&b);
Packit 6c4009
      if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
Packit 6c4009
	{
Packit 6c4009
	  puts ("barrier_wait failed");
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
      printf ("going to cancel %s in-time\n", names[l]);
Packit 6c4009
      if (pthread_cancel (th) != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("1st cancel of %s failed\n", names[l]);
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 ("1st join of %s failed\n", names[l]);
Packit 6c4009
	  result = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
      if (status != PTHREAD_CANCELED)
Packit 6c4009
	{
Packit 6c4009
	  printf ("1st %s thread not canceled\n", names[l]);
Packit 6c4009
	  result = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (cl_called == 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("%s cleanup handler not called\n", names[l]);
Packit 6c4009
	  result = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
      if (cl_called > 1)
Packit 6c4009
	{
Packit 6c4009
	  printf ("%s cleanup handler called more than once\n", names[l]);
Packit 6c4009
	  result = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      printf ("in-time %s cancellation succeeded\n", names[l]);
Packit 6c4009
Packit 6c4009
      cl_called = 0;
Packit 6c4009
Packit 6c4009
      if (pthread_create (&th, NULL, tf, (void *) l) != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("2nd %s create failed\n", names[l]);
Packit 6c4009
	  result = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      printf ("going to cancel %s early\n", names[l]);
Packit 6c4009
      if (pthread_cancel (th) != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("2nd cancel of %s failed\n", names[l]);
Packit 6c4009
	  result = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      r = pthread_barrier_wait (&b);
Packit 6c4009
      if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
Packit 6c4009
	{
Packit 6c4009
	  puts ("barrier_wait failed");
Packit 6c4009
	  result = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (pthread_join (th, &status) != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("2nd join of %s failed\n", names[l]);
Packit 6c4009
	  result = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
      if (status != PTHREAD_CANCELED)
Packit 6c4009
	{
Packit 6c4009
	  printf ("2nd %s thread not canceled\n", names[l]);
Packit 6c4009
	  result = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (cl_called == 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("%s cleanup handler not called\n", names[l]);
Packit 6c4009
	  result = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
      if (cl_called > 1)
Packit 6c4009
	{
Packit 6c4009
	  printf ("%s cleanup handler called more than once\n", names[l]);
Packit 6c4009
	  result = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      printf ("early %s cancellation succeeded\n", names[l]);
Packit 6c4009
Packit 6c4009
      if (l == TF_MQ_TIMEDRECEIVE)
Packit 6c4009
	{
Packit 6c4009
	  /* mq_receive and mq_timedreceive are tested on empty mq.
Packit 6c4009
	     For mq_send and mq_timedsend we need to make it full.
Packit 6c4009
	     If this fails, there is no point in doing further testing.  */
Packit 6c4009
	  char c = ' ';
Packit 6c4009
	  if (mq_send (q, &c, 1, 1) != 0)
Packit 6c4009
	    {
Packit 6c4009
	      printf ("mq_send failed: %m\n");
Packit 6c4009
	      result = 1;
Packit 6c4009
	      break;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (mq_close (q) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("mq_close failed: %m\n");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
#else
Packit 6c4009
# define TEST_FUNCTION 0
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#include "../test-skeleton.c"