tst-mqueue3.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* Test SIGEV_THREAD handling for POSIX message queues.
  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 <errno.h>
  16. #include <mqueue.h>
  17. #include <signal.h>
  18. #include <stddef.h>
  19. #include <stdint.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <sys/mman.h>
  24. #include <sys/wait.h>
  25. #include <unistd.h>
  26. #include <support/check.h>
  27. #if _POSIX_THREADS
  28. # include <pthread.h>
  29. static pid_t pid;
  30. static mqd_t m;
  31. static const char message[] = "hello";
  32. # define MAXMSG 10
  33. # define MSGSIZE 10
  34. # define UNIQUE 42
  35. static void
  36. fct (union sigval s)
  37. {
  38. /* Put the mq in non-blocking mode. */
  39. struct mq_attr attr;
  40. if (mq_getattr (m, &attr) != 0)
  41. {
  42. printf ("%s: mq_getattr failed: %m\n", __FUNCTION__);
  43. exit (1);
  44. }
  45. attr.mq_flags |= O_NONBLOCK;
  46. if (mq_setattr (m, &attr, NULL) != 0)
  47. {
  48. printf ("%s: mq_setattr failed: %m\n", __FUNCTION__);
  49. exit (1);
  50. }
  51. /* Check the values. */
  52. if (attr.mq_maxmsg != MAXMSG)
  53. {
  54. printf ("%s: mq_maxmsg wrong: is %jd, expected %d\n",
  55. __FUNCTION__, (intmax_t) attr.mq_maxmsg, MAXMSG);
  56. exit (1);
  57. }
  58. if (attr.mq_msgsize != MAXMSG)
  59. {
  60. printf ("%s: mq_msgsize wrong: is %jd, expected %d\n",
  61. __FUNCTION__, (intmax_t) attr.mq_msgsize, MSGSIZE);
  62. exit (1);
  63. }
  64. /* Read the message. */
  65. char buf[attr.mq_msgsize];
  66. ssize_t n = TEMP_FAILURE_RETRY (mq_receive (m, buf, attr.mq_msgsize, NULL));
  67. if (n != sizeof (message))
  68. {
  69. printf ("%s: length of message wrong: is %zd, expected %zu\n",
  70. __FUNCTION__, n, sizeof (message));
  71. exit (1);
  72. }
  73. if (memcmp (buf, message, sizeof (message)) != 0)
  74. {
  75. printf ("%s: message wrong: is \"%s\", expected \"%s\"\n",
  76. __FUNCTION__, buf, message);
  77. exit (1);
  78. }
  79. exit (UNIQUE);
  80. }
  81. int
  82. do_test (void)
  83. {
  84. char tmpfname[] = "/tmp/tst-mqueue3-barrier.XXXXXX";
  85. int fd = mkstemp (tmpfname);
  86. if (fd == -1)
  87. {
  88. printf ("cannot open temporary file: %m\n");
  89. return 1;
  90. }
  91. /* Make sure it is always removed. */
  92. unlink (tmpfname);
  93. /* Create one page of data. */
  94. size_t ps = sysconf (_SC_PAGESIZE);
  95. char data[ps];
  96. memset (data, '\0', ps);
  97. /* Write the data to the file. */
  98. if (write (fd, data, ps) != (ssize_t) ps)
  99. {
  100. puts ("short write");
  101. return 1;
  102. }
  103. void *mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  104. if (mem == MAP_FAILED)
  105. {
  106. printf ("mmap failed: %m\n");
  107. return 1;
  108. }
  109. pthread_barrier_t *b;
  110. b = (pthread_barrier_t *) (((uintptr_t) mem + __alignof (pthread_barrier_t))
  111. & ~(__alignof (pthread_barrier_t) - 1));
  112. pthread_barrierattr_t a;
  113. if (pthread_barrierattr_init (&a) != 0)
  114. {
  115. puts ("barrierattr_init failed");
  116. return 1;
  117. }
  118. if (pthread_barrierattr_setpshared (&a, PTHREAD_PROCESS_SHARED) != 0)
  119. {
  120. puts ("barrierattr_setpshared failed, could not test");
  121. return 0;
  122. }
  123. if (pthread_barrier_init (b, &a, 2) != 0)
  124. {
  125. puts ("barrier_init failed");
  126. return 1;
  127. }
  128. if (pthread_barrierattr_destroy (&a) != 0)
  129. {
  130. puts ("barrierattr_destroy failed");
  131. return 1;
  132. }
  133. /* Name for the message queue. */
  134. char mqname[sizeof ("/tst-mqueue3-") + 3 * sizeof (pid_t)];
  135. snprintf (mqname, sizeof (mqname) - 1, "/tst-mqueue3-%ld",
  136. (long int) getpid ());
  137. /* Create the message queue. */
  138. struct mq_attr attr = { .mq_maxmsg = MAXMSG, .mq_msgsize = MSGSIZE };
  139. m = mq_open (mqname, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
  140. if (m == -1)
  141. {
  142. if (errno == ENOSYS)
  143. FAIL_UNSUPPORTED ("mq_open not supported");
  144. printf ("mq_open failed with: %m\n");
  145. return 1;
  146. }
  147. /* Unlink the message queue right away. */
  148. if (mq_unlink (mqname) != 0)
  149. {
  150. puts ("mq_unlink failed");
  151. return 1;
  152. }
  153. pid = fork ();
  154. if (pid == -1)
  155. {
  156. puts ("fork failed");
  157. return 1;
  158. }
  159. if (pid == 0)
  160. {
  161. /* Request notification via thread. */
  162. struct sigevent ev;
  163. ev.sigev_notify = SIGEV_THREAD;
  164. ev.sigev_notify_function = fct;
  165. ev.sigev_value.sival_ptr = NULL;
  166. ev.sigev_notify_attributes = NULL;
  167. /* Tell the kernel. */
  168. if (mq_notify (m,&ev) != 0)
  169. {
  170. puts ("mq_notify failed");
  171. exit (1);
  172. }
  173. /* Tell the parent we are ready. */
  174. (void) pthread_barrier_wait (b);
  175. /* Make sure the process goes away eventually. */
  176. alarm (10);
  177. /* Do nothing forever. */
  178. while (1)
  179. pause ();
  180. }
  181. /* Wait for the child process to register to notification method. */
  182. (void) pthread_barrier_wait (b);
  183. /* Send the message. */
  184. if (mq_send (m, message, sizeof (message), 1) != 0)
  185. {
  186. kill (pid, SIGKILL);
  187. puts ("mq_send failed");
  188. return 1;
  189. }
  190. int r;
  191. if (TEMP_FAILURE_RETRY (waitpid (pid, &r, 0)) != pid)
  192. {
  193. kill (pid, SIGKILL);
  194. puts ("waitpid failed");
  195. return 1;
  196. }
  197. return WIFEXITED (r) && WEXITSTATUS (r) == UNIQUE ? 0 : 1;
  198. }
  199. # define TEST_FUNCTION do_test ()
  200. #else
  201. # define TEST_FUNCTION 0
  202. #endif
  203. #include "../test-skeleton.c"