tst-mqueue.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* Common code for message queue passing tests.
  2. Copyright (C) 2004 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #include <mqueue.h>
  18. #include <search.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <sys/uio.h>
  23. #include <unistd.h>
  24. static int temp_mq_fd;
  25. /* Add temporary files in list. */
  26. static void
  27. __attribute__ ((unused))
  28. add_temp_mq (const char *name)
  29. {
  30. struct iovec iov[2];
  31. iov[0].iov_base = (char *) name;
  32. iov[0].iov_len = strlen (name);
  33. iov[1].iov_base = (char *) "\n";
  34. iov[1].iov_len = 1;
  35. if (writev (temp_mq_fd, iov, 2) != iov[0].iov_len + 1)
  36. printf ("Could not record temp mq filename %s\n", name);
  37. }
  38. /* Delete all temporary message queues. */
  39. static void
  40. do_cleanup (void)
  41. {
  42. if (lseek (temp_mq_fd, 0, SEEK_SET) != 0)
  43. return;
  44. FILE *f = fdopen (temp_mq_fd, "r");
  45. if (f == NULL)
  46. return;
  47. char *line = NULL;
  48. size_t n = 0;
  49. ssize_t rets;
  50. while ((rets = getline (&line, &n, f)) > 0)
  51. {
  52. if (line[rets - 1] != '\n')
  53. continue;
  54. line[rets - 1] = '\0';
  55. mq_unlink (line);
  56. }
  57. fclose (f);
  58. }
  59. static void
  60. do_prepare (void)
  61. {
  62. char name [] = "/tmp/tst-mqueueN.XXXXXX";
  63. temp_mq_fd = mkstemp (name);
  64. if (temp_mq_fd == -1)
  65. {
  66. printf ("Could not create temporary file %s: %m\n", name);
  67. exit (1);
  68. }
  69. unlink (name);
  70. }
  71. #define PREPARE(argc, argv) do_prepare ()
  72. #define CLEANUP_HANDLER do_cleanup ()