tst-mqueue3.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 <mqueue.h>
  18. #include <signal.h>
  19. #include <stddef.h>
  20. #include <stdint.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <sys/mman.h>
  25. #include <sys/wait.h>
  26. #include <unistd.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 %ld, expecte %d\n",
  55. __FUNCTION__, attr.mq_maxmsg, MAXMSG);
  56. exit (1);
  57. }
  58. if (attr.mq_msgsize != MAXMSG)
  59. {
  60. printf ("%s: mq_msgsize wrong: is %ld, expecte %d\n",
  61. __FUNCTION__, 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. {
  144. puts ("not implemented");
  145. return 0;
  146. }
  147. puts ("mq_open failed");
  148. return 1;
  149. }
  150. /* Unlink the message queue right away. */
  151. if (mq_unlink (mqname) != 0)
  152. {
  153. puts ("mq_unlink failed");
  154. return 1;
  155. }
  156. pid = fork ();
  157. if (pid == -1)
  158. {
  159. puts ("fork failed");
  160. return 1;
  161. }
  162. if (pid == 0)
  163. {
  164. /* Request notification via thread. */
  165. struct sigevent ev;
  166. ev.sigev_notify = SIGEV_THREAD;
  167. ev.sigev_notify_function = fct;
  168. ev.sigev_value.sival_ptr = NULL;
  169. ev.sigev_notify_attributes = NULL;
  170. /* Tell the kernel. */
  171. if (mq_notify (m,&ev) != 0)
  172. {
  173. puts ("mq_notify failed");
  174. exit (1);
  175. }
  176. /* Tell the parent we are ready. */
  177. (void) pthread_barrier_wait (b);
  178. /* Make sure the process goes away eventually. */
  179. alarm (10);
  180. /* Do nothing forever. */
  181. while (1)
  182. pause ();
  183. }
  184. /* Wait for the child process to register to notification method. */
  185. (void) pthread_barrier_wait (b);
  186. /* Send the message. */
  187. if (mq_send (m, message, sizeof (message), 1) != 0)
  188. {
  189. kill (pid, SIGKILL);
  190. puts ("mq_send failed");
  191. return 1;
  192. }
  193. int r;
  194. if (TEMP_FAILURE_RETRY (waitpid (pid, &r, 0)) != pid)
  195. {
  196. kill (pid, SIGKILL);
  197. puts ("waitpid failed");
  198. return 1;
  199. }
  200. return WIFEXITED (r) && WEXITSTATUS (r) == UNIQUE ? 0 : 1;
  201. }
  202. # define TEST_FUNCTION do_test ()
  203. #else
  204. # define TEST_FUNCTION 0
  205. #endif
  206. #include "../test-skeleton.c"