tst-mqueue.h 2.1 KB

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