tst-mqueue3.c 5.9 KB

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