Blame rt/tst-mqueue3.c

Packit 6c4009
/* Test SIGEV_THREAD handling for POSIX message queues.
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 Ulrich Drepper <drepper@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 <signal.h>
Packit 6c4009
#include <stddef.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <sys/mman.h>
Packit 6c4009
#include <sys/wait.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
#if _POSIX_THREADS
Packit 6c4009
# include <pthread.h>
Packit 6c4009
Packit 6c4009
static pid_t pid;
Packit 6c4009
static mqd_t m;
Packit 6c4009
static const char message[] = "hello";
Packit 6c4009
Packit 6c4009
# define MAXMSG 10
Packit 6c4009
# define MSGSIZE 10
Packit 6c4009
# define UNIQUE 42
Packit 6c4009
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
fct (union sigval s)
Packit 6c4009
{
Packit 6c4009
  /* Put the mq in non-blocking mode.  */
Packit 6c4009
  struct mq_attr attr;
Packit 6c4009
  if (mq_getattr (m, &attr) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("%s: mq_getattr failed: %m\n", __FUNCTION__);
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
  attr.mq_flags |= O_NONBLOCK;
Packit 6c4009
  if (mq_setattr (m, &attr, NULL) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("%s: mq_setattr failed: %m\n", __FUNCTION__);
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Check the values.  */
Packit 6c4009
  if (attr.mq_maxmsg != MAXMSG)
Packit 6c4009
    {
Packit 6c4009
      printf ("%s: mq_maxmsg wrong: is %jd, expecte %d\n",
Packit 6c4009
	      __FUNCTION__, (intmax_t) attr.mq_maxmsg, MAXMSG);
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
  if (attr.mq_msgsize != MAXMSG)
Packit 6c4009
    {
Packit 6c4009
      printf ("%s: mq_msgsize wrong: is %jd, expecte %d\n",
Packit 6c4009
	      __FUNCTION__, (intmax_t) attr.mq_msgsize, MSGSIZE);
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Read the message.  */
Packit 6c4009
  char buf[attr.mq_msgsize];
Packit 6c4009
  ssize_t n = TEMP_FAILURE_RETRY (mq_receive (m, buf, attr.mq_msgsize, NULL));
Packit 6c4009
  if (n != sizeof (message))
Packit 6c4009
    {
Packit 6c4009
      printf ("%s: length of message wrong: is %zd, expected %zu\n",
Packit 6c4009
	      __FUNCTION__, n, sizeof (message));
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
  if (memcmp (buf, message, sizeof (message)) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("%s: message wrong: is \"%s\", expected \"%s\"\n",
Packit 6c4009
	      __FUNCTION__, buf, message);
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  exit (UNIQUE);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  char tmpfname[] = "/tmp/tst-mqueue3-barrier.XXXXXX";
Packit 6c4009
  int fd = mkstemp (tmpfname);
Packit 6c4009
  if (fd == -1)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot open temporary file: %m\n");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Make sure it is always removed.  */
Packit 6c4009
  unlink (tmpfname);
Packit 6c4009
Packit 6c4009
  /* Create one page of data.  */
Packit 6c4009
  size_t ps = sysconf (_SC_PAGESIZE);
Packit 6c4009
  char data[ps];
Packit 6c4009
  memset (data, '\0', ps);
Packit 6c4009
Packit 6c4009
  /* Write the data to the file.  */
Packit 6c4009
  if (write (fd, data, ps) != (ssize_t) ps)
Packit 6c4009
    {
Packit 6c4009
      puts ("short write");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  void *mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Packit 6c4009
  if (mem == MAP_FAILED)
Packit 6c4009
    {
Packit 6c4009
      printf ("mmap failed: %m\n");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  pthread_barrier_t *b;
Packit 6c4009
  b = (pthread_barrier_t *) (((uintptr_t) mem + __alignof (pthread_barrier_t))
Packit 6c4009
                             & ~(__alignof (pthread_barrier_t) - 1));
Packit 6c4009
Packit 6c4009
  pthread_barrierattr_t a;
Packit 6c4009
  if (pthread_barrierattr_init (&a) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("barrierattr_init failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (pthread_barrierattr_setpshared (&a, PTHREAD_PROCESS_SHARED) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("barrierattr_setpshared failed, could not test");
Packit 6c4009
      return 0;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (pthread_barrier_init (b, &a, 2) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("barrier_init failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (pthread_barrierattr_destroy (&a) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("barrierattr_destroy failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Name for the message queue.  */
Packit 6c4009
  char mqname[sizeof ("/tst-mqueue3-") + 3 * sizeof (pid_t)];
Packit 6c4009
  snprintf (mqname, sizeof (mqname) - 1, "/tst-mqueue3-%ld",
Packit 6c4009
	    (long int) getpid ());
Packit 6c4009
Packit 6c4009
  /* Create the message queue.  */
Packit 6c4009
  struct mq_attr attr = { .mq_maxmsg = MAXMSG, .mq_msgsize = MSGSIZE };
Packit 6c4009
  m = mq_open (mqname, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
Packit 6c4009
  if (m == -1)
Packit 6c4009
    {
Packit 6c4009
      if (errno == ENOSYS)
Packit 6c4009
	{
Packit 6c4009
	  puts ("not implemented");
Packit 6c4009
	  return 0;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      puts ("mq_open failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Unlink the message queue right away.  */
Packit 6c4009
  if (mq_unlink (mqname) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("mq_unlink failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  pid = fork ();
Packit 6c4009
  if (pid == -1)
Packit 6c4009
    {
Packit 6c4009
      puts ("fork failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  if (pid == 0)
Packit 6c4009
    {
Packit 6c4009
      /* Request notification via thread.  */
Packit 6c4009
      struct sigevent ev;
Packit 6c4009
      ev.sigev_notify = SIGEV_THREAD;
Packit 6c4009
      ev.sigev_notify_function = fct;
Packit 6c4009
      ev.sigev_value.sival_ptr = NULL;
Packit 6c4009
      ev.sigev_notify_attributes = NULL;
Packit 6c4009
Packit 6c4009
      /* Tell the kernel.  */
Packit 6c4009
      if (mq_notify (m,&ev) != 0)
Packit 6c4009
	{
Packit 6c4009
	  puts ("mq_notify failed");
Packit 6c4009
	  exit (1);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      /* Tell the parent we are ready.  */
Packit 6c4009
      (void) pthread_barrier_wait (b);
Packit 6c4009
Packit 6c4009
      /* Make sure the process goes away eventually.  */
Packit 6c4009
      alarm (10);
Packit 6c4009
Packit 6c4009
      /* Do nothing forever.  */
Packit 6c4009
      while (1)
Packit 6c4009
	pause ();
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Wait for the child process to register to notification method.  */
Packit 6c4009
  (void) pthread_barrier_wait (b);
Packit 6c4009
Packit 6c4009
  /* Send the message.  */
Packit 6c4009
  if (mq_send (m, message, sizeof (message), 1) != 0)
Packit 6c4009
    {
Packit 6c4009
      kill (pid, SIGKILL);
Packit 6c4009
      puts ("mq_send failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  int r;
Packit 6c4009
  if (TEMP_FAILURE_RETRY (waitpid (pid, &r, 0)) != pid)
Packit 6c4009
    {
Packit 6c4009
      kill (pid, SIGKILL);
Packit 6c4009
      puts ("waitpid failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return WIFEXITED (r) && WEXITSTATUS (r) == UNIQUE ? 0 : 1;
Packit 6c4009
}
Packit 6c4009
# define TEST_FUNCTION do_test ()
Packit 6c4009
#else
Packit 6c4009
# define TEST_FUNCTION 0
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#include "../test-skeleton.c"