tst-mqueue.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <mqueue.h>
  17. #include <search.h>
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <sys/uio.h>
  22. #include <unistd.h>
  23. static int temp_mq_fd;
  24. /* Add temporary files in list. */
  25. static void
  26. __attribute__ ((unused))
  27. add_temp_mq (const char *name)
  28. {
  29. struct iovec iov[2];
  30. iov[0].iov_base = (char *) name;
  31. iov[0].iov_len = strlen (name);
  32. iov[1].iov_base = (char *) "\n";
  33. iov[1].iov_len = 1;
  34. if (writev (temp_mq_fd, iov, 2) != iov[0].iov_len + 1)
  35. printf ("Could not record temp mq filename %s\n", name);
  36. }
  37. /* Delete all temporary message queues. */
  38. static void
  39. do_cleanup (void)
  40. {
  41. if (lseek (temp_mq_fd, 0, SEEK_SET) != 0)
  42. return;
  43. FILE *f = fdopen (temp_mq_fd, "r");
  44. if (f == NULL)
  45. return;
  46. char *line = NULL;
  47. size_t n = 0;
  48. ssize_t rets;
  49. while ((rets = getline (&line, &n, f)) > 0)
  50. {
  51. if (line[rets - 1] != '\n')
  52. continue;
  53. line[rets - 1] = '\0';
  54. mq_unlink (line);
  55. }
  56. fclose (f);
  57. }
  58. static void
  59. do_prepare (void)
  60. {
  61. char name [] = "/tmp/tst-mqueueN.XXXXXX";
  62. temp_mq_fd = mkstemp (name);
  63. if (temp_mq_fd == -1)
  64. {
  65. printf ("Could not create temporary file %s: %m\n", name);
  66. exit (1);
  67. }
  68. unlink (name);
  69. }
  70. #define PREPARE(argc, argv) do_prepare ()
  71. #define CLEANUP_HANDLER do_cleanup ()