Blame rt/tst-mqueue.h

Packit 6c4009
/* Common code for message queue passing tests.
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 <mqueue.h>
Packit 6c4009
#include <search.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <sys/uio.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
static int temp_mq_fd;
Packit 6c4009
Packit 6c4009
/* Add temporary files in list.  */
Packit 6c4009
static void
Packit 6c4009
__attribute__ ((unused))
Packit 6c4009
add_temp_mq (const char *name)
Packit 6c4009
{
Packit 6c4009
  struct iovec iov[2];
Packit 6c4009
  iov[0].iov_base = (char *) name;
Packit 6c4009
  iov[0].iov_len = strlen (name);
Packit 6c4009
  iov[1].iov_base = (char *) "\n";
Packit 6c4009
  iov[1].iov_len = 1;
Packit 6c4009
  if (writev (temp_mq_fd, iov, 2) != iov[0].iov_len + 1)
Packit 6c4009
    printf ("Could not record temp mq filename %s\n", name);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Delete all temporary message queues.  */
Packit 6c4009
static void
Packit 6c4009
do_cleanup (void)
Packit 6c4009
{
Packit 6c4009
  if (lseek (temp_mq_fd, 0, SEEK_SET) != 0)
Packit 6c4009
    return;
Packit 6c4009
Packit 6c4009
  FILE *f = fdopen (temp_mq_fd, "r");
Packit 6c4009
  if (f == NULL)
Packit 6c4009
    return;
Packit 6c4009
Packit 6c4009
  char *line = NULL;
Packit 6c4009
  size_t n = 0;
Packit 6c4009
  ssize_t rets;
Packit 6c4009
  while ((rets = getline (&line, &n, f)) > 0)
Packit 6c4009
    {
Packit 6c4009
      if (line[rets - 1] != '\n')
Packit 6c4009
        continue;
Packit 6c4009
Packit 6c4009
      line[rets - 1] = '\0';
Packit 6c4009
      mq_unlink (line);
Packit 6c4009
    }
Packit 6c4009
  fclose (f);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
do_prepare (void)
Packit 6c4009
{
Packit 6c4009
  char name [] = "/tmp/tst-mqueueN.XXXXXX";
Packit 6c4009
  temp_mq_fd = mkstemp (name);
Packit 6c4009
  if (temp_mq_fd == -1)
Packit 6c4009
    {
Packit 6c4009
      printf ("Could not create temporary file %s: %m\n", name);
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
  unlink (name);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define PREPARE(argc, argv) do_prepare ()
Packit 6c4009
#define CLEANUP_HANDLER	do_cleanup ()