tst-mqueue3.c 5.8 KB

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